Memory management functions
Async signal safe replacements for memory management functions.
Because metric library functions need to be async signal safe, the standard
libc memory management functions (such as malloc
, free
, new
,
delete
) cannot be used. The following memory management functions
can safely be used by the metric plugin libraries even if they are
called from inside a signal handler.
void * allinea_safe_malloc(size_t size)
An async-signal-safe version of
malloc
.Allocates a memory region of size bytes. To be used instead of the libc
malloc
.If memory is exhausted, an error is printed to
stderr
and the process is aborted.Memory allocated by this function must be released by a call to
allinea_safe_free()
.Do not use the libc
free()
to free memory allocated by this function.Parameters
Direction
Parameter
Description
[in]
size
The number of bytes of memory to allocate.
Returns
A pointer to the start of the allocated memory region.
void allinea_safe_free(void * ptr)
An async-signal-safe version of
free
.Frees a memory region previously allocated with
allinea_safe_malloc
. Use this instead of the libcfree
. Do not use this function to deallocate memory blocks previously allocated by the libcmalloc
.Parameters
Direction
Parameter
Description
[in,out]
ptr
A pointer to the start of the memory region to free. This must be previously allocated with
allinea_safe_malloc()
,allinea_safe_realloc()
, orallinea_safe_calloc()
.void * allinea_safe_calloc(size_t nmemb, size_t size)
An async-signal-safe version of
calloc
. Allocatessize
andnmemb
bytes and zero-initializes the memory.void* allinea_safe_calloc ( size_t *nmemb*, size_t *size* )
Use this instead of the libc
calloc
.If memory is exhausted, an error is printed to
stderr
and the process is aborted. Memory allocated by this function must be released by a call toallinea_safe_free()
.Do not use libc
free
to free memory allocated by this function.Parameters
Direction
Parameter
Description
[in]
nmemb
The number of bytes per element to allocate.
[in]
size
The number of elements to allocate.
Returns
A pointer to the start of the allocated memory region.
int allinea_safe_close ( int fd )
Closes the file descriptor
fd
previously opened byallinea_safe_open(async-signal-safe)
. A replacement forclose
.When used in conjunction with
allinea_safe_read()
andallinea_safe_write()
, the bytes read or bytes written are not included in the I/O accounting of the enclosing profiler.Parameters
Parameter
Description
fd
The file descriptor to close.
Returns
0
on success;-1
on failure anderrno set
.Examples
See custom1.c.
void * allinea_safe_realloc(void * ptr, size_t size)
An async-signal-safe version of
realloc
.Reallocates a memory region if necessary, or allocates a new one if
NULL
is supplied forptr
.void* allinea_safe_realloc ( void * ptr, size_t size )
Use instead of the libc
realloc
.If memory is exhausted, an error is printed to
stderr
and the process is aborted.Pointers to memory regions supplied to this function must be allocated by a call to
allinea_safe_malloc()
,allinea_safe_calloc()
orallinea_safe_realloc()
.Memory allocated by this function must be released by a call to
allinea_safe_free()
.Do not use libc
free
to free memory allocated by this function.Parameters
Direction
Parameter
Description
[in]
ptr
The starting address of the memory region to reallocate.
[in]
size
The new minimum size to request.
Returns
A pointer to a memory region with at least
size
bytes available.