The Memory Management Library  0.2.1
Functions | Variables
memory.c File Reference

Source for Memory Management Library - Production Version (CBL) More...

#include <stddef.h>
#include <stdlib.h>
#include "cbl/assert.h"
#include "cbl/except.h"
#include "memory.h"
Include dependency graph for memory.c:

Functions

void *() mem_alloc (size_t n, const char *file, int line)
 allocates storage of the size n in bytes.
void *() mem_calloc (size_t c, size_t n, const char *file, int line)
 allocates zero-filled storage of the size c * n in bytes.
void() mem_free (void *p, const char *file, int line)
 deallocates storage pointed to by p.
void *() mem_resize (void *p, size_t n, const char *file, int line)
 adjust the size of storage pointed to by p to n.

Variables

const except_t mem_exceptfail = { "Allocation failed" }
 exception for memory allocation failure.

Detailed Description

Source for Memory Management Library - Production Version (CBL)


Function Documentation

void*() mem_alloc ( size_t  n,
const char *  file,
int  line 
)

allocates storage of the size n in bytes.

mem_alloc() does the same job as malloc() except:

  • mem_alloc() raises an exception when fails the requested allocation;
  • mem_alloc() does not take 0 as the byte length to preclude the possibility of returning a null pointer;
  • mem_alloc() never returns a null pointer.

Possible exceptions: mem_exceptfail, assert_exceptfail

Unchecked errors: none

Parameters:
[in]nsize in bytes for storage to be allocated
[in]filefile name in which storage requested
[in]funcfunction name in which strage requested (if C99 supported)
[in]lineline number on which storage requested
Returns:
pointer to allocated storage
Warning:
mam_alloc() returns no null pointer in any case. Allocation failure triggers an exception, so no need to handle an exceptional case with the return value.

Here is the caller graph for this function:

void*() mem_calloc ( size_t  c,
size_t  n,
const char *  file,
int  line 
)

allocates zero-filled storage of the size c * n in bytes.

mem_calloc() does the same job as mem_alloc() except that the storage it allocates are zero- filled. The similar explanation as for mem_alloc() applies to mem_calloc() too; see mem_alloc() for details.

Possible exceptions: mem_exceptfail, assert_exceptfail

Unchecked errors: none

Parameters:
[in]cnumber of items to be allocated
[in]nsize in bytes for one item
[in]filefile name in which storage requested
[in]funcfunction name in which strage requested (if C99 supported)
[in]lineline number on which storage requested
Returns:
pointer to allocated (zero-filled) storage
void() mem_free ( void *  p,
const char *  file,
int  line 
)

deallocates storage pointed to by p.

mem_free() is a simple wrapper function for free().

The additional parameters, file, func (if C99 supported), line are for the consistent form in the calling sites; the debugging version of this library takes advantage of them to raise an exception when something goes wrong in mem_free(). When using the debugging version, some of the following unchecked errors are to be detected.

Possible exceptions: none

Unchecked errors: foreign value given for p

Warning:
A "foreign" value also includes a pointer value which points to storage already moved to a different address by, say, mem_resize().
Parameters:
[in]ppointer to storage to release
[in]filefile name in which deallocation requested
[in]funcfunction name in which deallocation requested (if C99 supported)
[in]lineline number on which deallocation requested
Returns:
nothing

Here is the caller graph for this function:

void*() mem_resize ( void *  p,
size_t  n,
const char *  file,
int  line 
)

adjust the size of storage pointed to by p to n.

mem_resize() does the main job of realloc(); adjusting the size of storage already allocated by mem_alloc() or mem_calloc(). While realloc() deallocates like free() when the given size is 0 and allocates like malloc() when the given pointer is a null pointer, mem_resize() accepts neither a null pointer nor zero as its arguments. The similar explanation as for mem_alloc() also applies to mem_resize(). See mem_alloc() for details.

Possible exceptions: mem_exceptfail, assert_exceptfail

Unchecked errors: foreign value given for p

Parameters:
[in]ppointer to storage whose size to be adjusted
[in]nnew size for storage in bytes
[in]filefile name in which adjustment requested
[in]funcfunction name in which adjustment requested (if C99 supported)
[in]lineline number on which adjustment requested
Returns:
pointer to size-adjusted storage

Here is the caller graph for this function:

 All Data Structures Files Functions Variables Typedefs Defines