The malloc() and free() functions provide a simple general-purpose memory allocation package that is suitable for use in high performance multithreaded applications. The suggested use of this library is in multithreaded applications; it can be used for single
threaded applications, but there is no advantage in doing so. This library cannot be dynamically loaded via dlopen() during runtime because there must be only one manager of the process heap.
The malloc() function returns a pointer to a block of at least size bytes suitably aligned for any use.
The argument to free() is a pointer to a block previously allocated by malloc(), calloc() or realloc(). After free() is performed this space is available for further allocation. If ptr
is a null pointer, no action occurs.
Undefined results will occur if the space assigned by malloc() is overrun or if a random number is handed to free(). A freed pointer that is passed to free() will send a SIGABRT signal to the calling
process. This behavior is controlled by mallocctl().
The calloc() function allocates a zero-initialized space for an array of nelem elements of size elsize.
The memalign() function allocates size bytes on a specified alignment boundary and returns a pointer to the allocated block. The value of the returned address is guaranteed to be an even multiple of alignment. Note that the value of alignment must be a power of two, and must be greater than or equal to the size of a word.
The realloc() function changes the size of the block pointed to by ptr to size bytes and returns a pointer to the (possibly moved) block. The contents will be unchanged up to the lesser of the new and old sizes. If ptr
is NULL, realloc() behaves like malloc() for the specified size. If size is 0 and ptr is not a null pointer, the object pointed to is freed.
The valloc() function has the same effect as malloc(), except that the allocated memory will be aligned to a multiple of the value returned by sysconf(_SC_PAGESIZE).
After possible pointer coercion, each allocation routine returns a pointer to a space that is suitably aligned for storage of any type of object.
The malloc(), realloc(), calloc(), memalign(), and valloc() functions will fail if there is not enough available memory.
The mallocctl() function controls the behavior of the malloc library. The options fall into two general classes, debugging options and performance options.
-
MTDOUBLEFREE
- Allows double free of a pointer. Setting value to 1 means yes and 0 means no. The default behavior of double free results in a core dump.
-
MTDEBUGPATTERN
- Writes misaligned data into the buffer after free(). When the buffer is reallocated, the contents are verified to ensure that there was no access to the buffer after the free. If the buffer has been dirtied, a SIGABRT signal is delivered to the process. Setting value to 1 means yes and 0 means no. The default behavior is to not
write misaligned data. The pattern used is 0xdeadbeef. Use of this option results in a performance penalty.
-
MTINITBUFFER
- Writes misaligned data into the newly allocated buffer. This option is useful for detecting some accesses before initialization. Setting value to 1 means yes and 0 means no. The default behavior is to not write misaligned data to the newly allocated buffer. The pattern used is 0xbaddcafe. Use of this option results in a performance penalty.
-
MTCHUNKSIZE
- This option changes the size of allocated memory when a pool has exhausted all available memory in the buffer. Increasing this value allocates more memory for the application. A substantial performance
gain can occur because the library makes fewer calls to the OS for more memory. Acceptable number values are between 9 and 256; the default value is 9. This value is multiplied
by 8192.
|