What is the difference between xmalloc and malloc?

前端 未结 6 1182
萌比男神i
萌比男神i 2021-01-31 13:57

What is the difference between xmalloc() and malloc() for memory allocation?
Is there any pro of using xmalloc()?

6条回答
  •  一个人的身影
    2021-01-31 14:48

    I have seen xmalloc while working on IBM AIX. xmalloc is a kernel service provided by AIX.

    Nothing can explain a function better than the function's man page in my opinion. So I am pasting the below details from the man page

    Purpose: Allocates memory.

    Syntax:

    caddr_t xmalloc ( size, align, heap)

    Parameters:

    size: Specifies the number of bytes to allocate.

    align: Specifies the alignment characteristics for the allocated memory.

    heap : Specifies the address of the heap from which the memory is to be allocated.

    Description:

    The xmalloc kernel service allocates an area of memory out of the heap specified by the heap parameter. This area is the number of bytes in length specified by the size parameter and is aligned on the byte boundary specified by the align parameter. The align parameter is actually the log base 2 of the desired address boundary. For example, an align value of 4 requests that the allocated area be aligned on a 2^4 (16) byte boundary.

    There are multiple heaps provided by the kernel for use by kernel extensions. Two primary kernel heaps are kernel_heap and pinned_heap. Kernel extensions should use the kernel_heap value when allocating memory that is not pinned, and should use the pinned_heap value when allocating memory that should always be pinned or pinned for long periods of time. When allocating from the pinned_heap heap, the xmalloc kernel service will pin the memory before a successful return. The pin and unpin kernel services should be used to pin and unpin memory from the kernel_heap heap when the memory should only be pinned for a limited amount of time. Memory from the kernel_heap heap must be unpinned before freeing it. Memory from the pinned_heap heap should not be unpinned.

    If one is interested in knowing more about this function can visit the following link: IBM AIX Support

提交回复
热议问题