|
|
HP-UX Reference Volume 4 of 5 > mmalloc(3C) |
|
NAMEmalloc(), free(), realloc(), calloc(), valloc(), mallopt(), mallinfo(), memorymap(), alloca() — main memory allocator SYSTEM V SYNOPSIS (HP-UX)#include <malloc.h> char *malloc(unsigned size); void free(char *ptr); char *realloc(char *ptr, unsigned size); char *calloc(unsigned nelem, unsigned elsize); int mallopt(int cmd, int value); struct mallinfo mallinfo(void); RemarksThe functionality in the old malloc(3X) package has been incorporated into malloc(3C). The library (/usr/lib/libmalloc.a) corresponding to the -lmalloc linker option is now an empty library. Makefiles that reference this library will continue to work. Applications that used the malloc(3X) package should still work properly with the new malloc(3C) package. If the old versions must be used, they are provided in files /usr/old/libmalloc3x.a and /usr/old/libmalloc3c.o for Release 8.07 only. DESCRIPTIONThe functions described in this manual entry provide a simple, general purpose memory allocation package:
Each of the allocation routines returns a pointer to space suitably aligned (after possible pointer coercion) for storage of any type of object.
APPLICATION USAGEmalloc(), free(), realloc(), calloc(), valloc(), mallopt(), mallinfo(), memorymap() and alloca() are thread-safe. These interfaces are not async-cancel-safe. RETURN VALUEUpon successful completion, malloc(), realloc(), calloc(), and valloc() return a pointer to space suitably aligned (after possible pointer coercion) for storage of any type of object. Otherwise, they return a NULL pointer. If realloc() returns a NULL pointer, the memory pointed to by the original pointer is left intact. mallopt() returns zero for success and nonzero for failure. DIAGNOSTICSmalloc(), realloc(), calloc(), and valloc() return a NULL pointer if there is no available memory, or if the memory managed by malloc() has been detectably corrupted. This memory may become corrupted if data is stored outside the bounds of a block, or if an invalid pointer (a pointer not generated by malloc(), realloc(), or calloc()) is passed as an argument to free() or realloc(). If mallopt() is called after any allocation of a small block and cmd is not set to M_BLOCK or M_UBLOCK, or if cmd or value is invalid, nonzero is returned. Otherwise, it returns zero. ERRORS
EXTERNAL INFLUENCESThe performance of the malloc() family can be tuned via two new environment variables, _M_ARENA_OPTS and _M_SBA_OPTS. For threaded applications, malloc() uses multiple arenas. Memory requests from different threads are handled by different arenas. _M_ARENA_OPTS can be used to adjust the number of arenas and how many pages each time an arena expands itself (the expansion factor), assuming that the page size is 4096 bytes. In general, the more threads in an application, the more arenas should be used for better performance. The number of arenas can be from 4 to 64 for threaded applications. For non-threaded applications, only one arena is used. If the environment variable is not set, or the number of arenas is set to be out of the range, the default number of 8 will be used. The expansion factor is from 1 to 4096, default value is 32. Again, if the factor is out of the range, the default value will be used. Here is an example of how to use _M_ARENA_OPTS, $ export _M_ARENA_OPTS = 16:8 This means that the number of arenas is 16, and the expansion size is 8*4096 bytes. In general, the more arenas you use, the smaller the expansion factor should be, and vice versa. _M_SBA_OPTS is to turn on the small block allocator, and to set up parameters for the small block allocator, namely, maxfast , grain , and numlblks. Applications with small block allocator turned on usually run faster than with it turned off. Small block allocator can be turned on through mallopt(); however, it is not early enough for C++/Java applications. The environment variable turns it on before the application starts. The mallopt() call can still be used the same way. If the environment variable is set, and no small block allocator has been used, the subsequent mallopt() calls can still overwrite whatever is set through _M_SBA_OPTS. If the environment variable is set, and small block allocator has been used, then mallopt() will have no effect. To use this environment variable, $ export _M_SBA_OPTS = 512:100:16 This means that the maxfast size is 512, the number of small blocks is 100, and the grain size is 16. You have to supply all 3 values, and in that order. If not, the default values will be used instead. _M_ARENA_OPTS has no effects on non-threaded applications, while _M_SBA_OPTS has. WARNINGSmalloc() functions use brk() and sbrk() (see brk(2)) to increase the address space of a process. Therefore, an application program that uses brk() or sbrk() must not use them to decrease the address space, because this confuses the malloc() functions. free() and realloc() do not check their pointer argument for validity. If free() or realloc() is passed a pointer that was not the result of a call to malloc(), realloc(), calloc(), or valloc(), or if space assigned by an allocation function is overrun, loss of data, a memory fault, bus error, or an infinite loop may occur at that time or during any subsequent call to malloc(), realloc(), calloc(), valloc(), or free(). The following actions are not supported and cause undesirable effects:
The following actions are strongly discouraged and may be unsupported in a future implementation of malloc():
Undocumented features of earlier memory allocators have not been duplicated. COMPATIBILITYThe only external difference between the old malloc(3X) allocator and the malloc(3C) allocator is that the old allocator would return a NULL pointer for a request of zero bytes. The malloc(3C) allocator returns a valid memory address. This is not a concern for most applications. Although the current implementation of malloc(3C) allows for freeing a block twice and does not corrupt the contents of a block after it is freed (until the next call to realloc(), calloc(), malloc(), or valloc()), support for these features may be discontinued in a future implementation of malloc(3C) and should not be used. STANDARDS CONFORMANCEmalloc(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C calloc(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C free(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C mallinfo(): SVID2, XPG2 mallopt(): SVID2, SVID3, XPG2 realloc(): AES, SVID2, SVID3, XPG2, XPG3, XPG4, FIPS 151-2, POSIX.1, ANSI C |
|