libgslutil  1.2.3
Functions
Safely allocate memory

Functions

void * safe_malloc (size_t amt)
 Allocate memory. More...
 
void * safe_realloc (void *p, size_t amt)
 Dynamically resize allocated memory. More...
 
char * safe_strdup (const char *str)
 Copy a string. More...
 

Detailed Description

This module contains a set of functions for safely allocating memory. This may involve zeroing the memory before it is returned and aborting if the allocation fails.

Function Documentation

◆ safe_malloc()

void* safe_malloc ( size_t  amt)

Allocate memory.

Parameters
amtAmount (in bytes) to allocate.
Returns
Valid pointer to new memory.

This function will allocate amt bytes of memory, returning a pointer to the new block. Should the allocation fail, it is logged to syslog and the program aborted with abort(). The newly allocated block is zeroed with memset(). Returns a unique pointer even if amt is zero.

◆ safe_realloc()

void* safe_realloc ( void *  p,
size_t  amt 
)

Dynamically resize allocated memory.

Parameters
pExisting pointer (may be 0).
amtNew size to allocate.
Returns
Valid pointer to new memory.

This function will allocate amt bytes of memory, possibly by resizing the block at p (if it is not null), but in any case keeping any existing contents of p. Should the allocation fail, it is logged to syslog and the program aborted with abort(). Returns a unique pointer even if amt is zero.

◆ safe_strdup()

char* safe_strdup ( const char *  str)

Copy a string.

Parameters
strExisting string (may not be 0).
Returns
Valid pointer to newly-allocated and copied string.

This function will copy str (which may not be 0), allocating the required amount of memory for it. Should the allocation fail, it is logged to syslog and the program aborted with abort().