Memory Handling

These macros provide an alternative to the normal C library functions for dynamically allocating memory. More...

Defines

#define GGZ_MEM_DEBUG   "ggz_mem"
 Debugging type for memory debugging.
#define ggz_malloc(size)   _ggz_malloc(size, _GGZFUNCTION_ " in " __FILE__, __LINE__)
 Macro for memory allocation.
#define ggz_realloc(mem, size)   _ggz_realloc(mem, size, _GGZFUNCTION_ " in " __FILE__, __LINE__)
 Macro for resizing previously allocated memory.
#define ggz_free(mem)   _ggz_free(mem, _GGZFUNCTION_ " in " __FILE__, __LINE__)
 Macro for freeing memory previously allocated.
#define ggz_strdup(string)   _ggz_strdup(string, _GGZFUNCTION_ " in " __FILE__, __LINE__)
 Macro for duplicating string.

Functions

void * _ggz_malloc (const size_t size, const char *tag, int line)
 Function to actually perform memory allocation.
void * _ggz_realloc (const void *ptr, const size_t size, const char *tag, int line) ggz__attribute((warn_unused_result))
 Function to perform memory reallocation.
int _ggz_free (const void *ptr, const char *tag, int line)
 Function to free allocated memory.
char * _ggz_strdup (const char *ptr, const char *tag, int line) ggz__attribute((warn_unused_result))
 Function to copy a string.
char * ggz_strncpy (char *dst, const char *src, size_t n)
 Safe version of strncpy.
int ggz_memory_check (void)
 Check memory allocated against memory freed and display any discrepancies.

Detailed Description

These macros provide an alternative to the normal C library functions for dynamically allocating memory.

They keep track of memory allocated by storing the name of the function and file in which they were called.

You can then call ggz_memory_check() to make sure all allocated memory has been freed. Note that you will need to enable MEMORY debugging to see this.


Define Documentation

#define GGZ_MEM_DEBUG   "ggz_mem"

Debugging type for memory debugging.

See also:
ggz_debug_enable

#define ggz_malloc ( size   )     _ggz_malloc(size, _GGZFUNCTION_ " in " __FILE__, __LINE__)

Macro for memory allocation.

Parameters:
size the size of memory to allocate, in bytes
Returns:
a pointer to the newly allocated and zeroed memory

#define ggz_realloc ( mem,
size   )     _ggz_realloc(mem, size, _GGZFUNCTION_ " in " __FILE__, __LINE__)

Macro for resizing previously allocated memory.

Parameters:
mem pointer to memory to reallocate
size new size requested, in bytes
Returns:
pointer to allocated memory

#define ggz_free ( mem   )     _ggz_free(mem, _GGZFUNCTION_ " in " __FILE__, __LINE__)

Macro for freeing memory previously allocated.

Parameters:
mem pointer to allocated memory
Returns:
failure code

#define ggz_strdup ( string   )     _ggz_strdup(string, _GGZFUNCTION_ " in " __FILE__, __LINE__)

Macro for duplicating string.

Parameters:
string string to duplicate
Returns:
pointer to new string
Note:
It is safe to pass a NULL string.


Function Documentation

void* _ggz_malloc ( const size_t  size,
const char *  tag,
int  line 
)

Function to actually perform memory allocation.

Don't call this directly. Instead, call ggz_malloc().

Parameters:
size size of memory to allocate, in bytes
tag string describing the calling function
line linenumber
Returns:
pointer to newly allocated, zeroed memory

void* _ggz_realloc ( const void *  ptr,
const size_t  size,
const char *  tag,
int  line 
)

Function to perform memory reallocation.

Don't call this directly. Instead, call ggz_realloc().

Parameters:
ptr pointer to memory to reallocate
size new size, in bytes
tag string describing the calling function
line linenumber
Returns:
pointer to allocated memory

int _ggz_free ( const void *  ptr,
const char *  tag,
int  line 
)

Function to free allocated memory.

Don't call this directly. Instead, call ggz_free().

Parameters:
ptr pointer to memory
tag string describing the calling function
line linenumber
Returns:
0 on success, -1 on error

char* _ggz_strdup ( const char *  ptr,
const char *  tag,
int  line 
)

Function to copy a string.

Don't call this directly. Instead, call ggz_strdup().

Parameters:
ptr string to duplicate
tag string describing the calling function
line linenumber
Returns:
newly allocated string
Note:
It is safe to pass a NULL string.

char* ggz_strncpy ( char *  dst,
const char *  src,
size_t  n 
)

Safe version of strncpy.

This function will behave like strncpy(), except that the result string is always guaranteed to be NULL-terminated. This matches the behaviour of snprintf() and leads to generally safer code.

Parameters:
dst destination string buffer
src source string
n number of bytes to copy, should be less than buffer size
Returns:
destination buffer
Note:
Passing a NULL string will result in an empty string

int ggz_memory_check ( void   ) 

Check memory allocated against memory freed and display any discrepancies.

Returns:
0 if no allocated memory remains, -1 otherwise


Generated on Fri Nov 30 14:58:02 2007 for LibGGZ by  doxygen 1.5.1