C Standard Library Extensions  6.2.0
Functions
POSIX-compatible extended memory handling

Functions

void * qfits_memory_malloc (size_t size, const char *filename, int lineno)
 Allocate memory. More...
 
void * qfits_memory_calloc (size_t nmemb, size_t size, const char *filename, int lineno)
 Allocate memory. More...
 
char * qfits_memory_falloc (char *name, size_t offs, size_t *size, const char *srcname, int srclin)
 Map a file's contents to memory as a char pointer. More...
 
void qfits_memory_fdealloc (void *ptr, size_t offs, size_t size, const char *filename, int lineno)
 Free memory that has been allocated with falloc. More...
 
void qfits_memory_free (void *ptr, const char *filename, int lineno)
 Free memory. More...
 
void * qfits_memory_realloc (void *ptr, size_t size, const char *filename, int lineno)
 Re-Allocate memory. More...
 
char * qfits_memory_strdup (const char *s, const char *filename, int lineno)
 Duplicate a string using calloc. More...
 
void qfits_memory_status (void)
 Display memory status information. More...
 
int qfits_memory_is_empty (void)
 Tell if there is still some memory allocated. More...
 

Detailed Description

qfits_memory is a small and efficient module offering memory extension capabitilies to ANSI C programs running on POSIX-compliant systems. It offers several useful features such as memory leak detection, protection for free on NULL or unallocated pointers, and virtually unlimited memory space. qfits_memory requires the mmap() system call to be implemented in the local C library to function. This module has been tested on a number of current Unix * flavours and is reported to work fine. The current limitation is the limited number of pointers it can handle at the same time.

Function Documentation

void* qfits_memory_calloc ( size_t  nmemb,
size_t  size,
const char *  filename,
int  lineno 
)

Allocate memory.

Parameters
nmembNumber of elements to allocate.
sizeSize (in bytes) of each element.
filenameName of the file where the alloc took place.
linenoLine number in the file.
Returns
1 newly allocated pointer.

This function is a replacement call for calloc. It should never be called directly but through a macro instead, as:

qfits_memory_calloc(nmemb, size, __FILE__, __LINE__)

References qfits_memory_malloc().

char* qfits_memory_falloc ( char *  name,
size_t  offs,
size_t *  size,
const char *  srcname,
int  srclin 
)

Map a file's contents to memory as a char pointer.

Parameters
nameName of the file to map
offsOffset to the first mapped byte in file.
sizeReturned size of the mapped file in bytes.
srcnameName of the source file making the call.
srclinLine # where the call was made.
Returns
A pointer to char, to be freed using qfits_memory_free().

This function takes in input the name of a file. It tries to map the file into memory and if it succeeds, returns the file's contents as a char pointer. It also modifies the input size variable to be the size of the mapped file in bytes. This function is normally never directly called but through the falloc() macro.

The offset indicates the starting point for the mapping, i.e. if you are not interested in mapping the whole file but only from a given place.

The returned pointer ptr must be deallocated with qfits_memory_fdealloc(ptr)

void qfits_memory_fdealloc ( void *  ptr,
size_t  offs,
size_t  size,
const char *  filename,
int  lineno 
)

Free memory that has been allocated with falloc.

Parameters
ptrPointer to free.
offsOffset to the first mapped byte in file.
sizesize to unmap
filenameName of the file where the dealloc took place.
linenoLine number in the file.
Returns
void
void qfits_memory_free ( void *  ptr,
const char *  filename,
int  lineno 
)

Free memory.

Parameters
ptrPointer to free.
filenameName of the file where the dealloc took place.
linenoLine number in the file.
Returns
void

Free the memory associated to a given pointer. Prints out a warning on stderr if the requested pointer is NULL or cannot be found in the extended memory table.

Referenced by qfits_memory_realloc().

int qfits_memory_is_empty ( void  )

Tell if there is still some memory allocated.

Returns
1 if the memory table is tempty, 0 if no, -1 if the memory model is off
void* qfits_memory_malloc ( size_t  size,
const char *  filename,
int  lineno 
)

Allocate memory.

Parameters
sizeSize (in bytes) to allocate.
filenameName of the file where the alloc took place.
linenoLine number in the file.
Returns
1 newly allocated pointer.

This function is a replacement call for malloc. It should never be called directly but through a macro instead, as:

qfits_memory_malloc(size, __FILE__, __LINE__)

Referenced by qfits_memory_calloc(), qfits_memory_realloc(), and qfits_memory_strdup().

void* qfits_memory_realloc ( void *  ptr,
size_t  size,
const char *  filename,
int  lineno 
)

Re-Allocate memory.

Parameters
ptrPointer to free.
sizeSize (in bytes) to allocate.
filenameName of the file where the alloc took place.
linenoLine number in the file.
Returns
1 newly allocated pointer.

This function is a replacement call for realloc. It should never be called directly but through a macro instead, as:

qfits_memory_realloc(nmemb, size, __FILE__, __LINE__)

References qfits_memory_free(), and qfits_memory_malloc().

void qfits_memory_status ( void  )

Display memory status information.

Returns
void

This function is meant for debugging purposes, but it is recommended to call it at the end of every executable making use of the extended memory features.

char* qfits_memory_strdup ( const char *  s,
const char *  filename,
int  lineno 
)

Duplicate a string using calloc.

Parameters
sString to duplicate.
filenameName of the file where the call took place.
linenoLine number in the file.
Returns
1 newly allocated character string.

This function calls in turn calloc to perform the allocation. It should never be called directly but only through a macro, like:

qfits_memory_strdup(s, __FILE__, __LINE__)

This function calls qfits_memory_malloc() to do the allocation.

References qfits_memory_malloc().