HP-UX Reference (11i v1 05/09) - 3 Library Functions A-M (vol 6)
m
malloc(3C) malloc(3C)
alloca() Allocates space from the stack of the caller for a block of at least size bytes, but does
not initialize the space. The space is automatically freed when the calling routine
exits.
Memory returned by
alloca() is not related to memory allocated by other
memory allocation functions. Behavior of addresses returned by
alloca() as
parameters to other memory functions is undefined.
The implementation of this routine is system dependent and its use is discouraged.
RETURN VALUE
Upon 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.
DIAGNOSTICS
malloc(), 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 gen-
erated by malloc(), realloc(), calloc(),orvalloc() 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,orifcmd or value is invalid, nonzero is returned. Otherwise, it returns zero.
ERRORS
[ENOMEM] malloc(), realloc(), calloc(), and valloc() set errno to [ENOMEM]
and return a NULL pointer when an out-of-memory condition arises.
[EINVAL] malloc(), realloc() , calloc(), and valloc()
set errno to [EINVAL]
and return a NULL pointer when the memory being managed by
malloc() has
been detectably corrupted.
EXTERNAL INFLUENCES
The 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 1 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 alloca-
tor, 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,
Section 3−−544 Hewlett-Packard Company − 3 − HP-UX 11i Version 1: September 2005