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 libc free. Do not use this function to deallocate memory blocks previously allocated by the libc malloc.

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(), or allinea_safe_calloc().

void * allinea_safe_calloc(size_t nmemb, size_t size)

An async-signal-safe version of calloc. Allocates size and nmemb 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 to allinea_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 by allinea_safe_open(async-signal-safe). A replacement for close.

When used in conjunction with allinea_safe_read() and allinea_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 and errno 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 for ptr.

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() or allinea_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.