|MTMALLOC(3MALLOC)||Memory Allocation Library Functions||MTMALLOC(3MALLOC)|
mtmalloc, mallocctl - MT hot memory allocator
#include <mtmalloc.h> cc -o a.out -lthread -lmtmalloc void *malloc(size_t size);
void free(void *ptr);
void *memalign(size_t alignment, size_t size);
void *realloc(void *ptr, size_t size);
void *valloc(size_t size);
void mallocctl(int cmd, long value);
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 with dlopen(3C) 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() or realloc(). After free() is performed this space is available for further allocation. If ptr is a null pointer, no action occurs. The free() function does not set errno.
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 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 the new size of the block requires movement of the block, the space for the previous instantiation of the block is freed. If the new size is larger, the contents of the newly allocated portion of the block are unspecified. If ptr is NULL, realloc() behaves like malloc() for the specified size. If size is 0 and ptr is not a null pointer, the space 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(), 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.
If there is no available memory, malloc(), realloc(), memalign(), and valloc() return a null pointer. When realloc() is called with size > 0 and returns NULL, the block pointed to by ptr is left intact. If size, nelem, or elsize is 0, either a null pointer or a unique pointer that can be passed to free() is returned.
If malloc() or realloc() returns unsuccessfully, errno will be set to indicate the error.
The malloc() and realloc() functions will fail if:
Comparative features of the various allocation libraries can be found in the umem_alloc(3MALLOC) manual page.
See attributes(7) for descriptions of the following attributes:
|ATTRIBUTE TYPE||ATTRIBUTE VALUE|
Undefined results will occur if the size requested for a block of memory exceeds the maximum size of a process's heap. This information may be obtained using getrlimit().
|March 21, 2005||OmniOS|