#include <stddef.h>
#include <stdlib.h>
#include "cbl/assert.h"
#include "cbl/except.h"
#include "memory.h"
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 . | |
void() | mem_log (FILE *fp, void freefunc(FILE *, const mem_loginfo_t *), void resizefunc(FILE *, const mem_loginfo_t *)) |
void() | mem_leak (void apply(const mem_loginfo_t *, void *), void *cl) |
Variables | |
const except_t | mem_exceptfail = { "Allocation failed" } |
exception for memory allocation failure. |
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:
Possible exceptions: mem_exceptfail, assert_exceptfail
Unchecked errors: none
[in] | n | size in bytes for storage to be allocated |
[in] | file | file name in which storage requested |
[in] | func | function name in which strage requested (if C99 supported) |
[in] | line | line number on which storage requested |
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
[in] | c | number of items to be allocated |
[in] | n | size in bytes for one item |
[in] | file | file name in which storage requested |
[in] | func | function name in which strage requested (if C99 supported) |
[in] | line | line number on which storage requested |
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
[in] | p | pointer to storage to release |
[in] | file | file name in which deallocation requested |
[in] | func | function name in which deallocation requested (if C99 supported) |
[in] | line | line number on which deallocation requested |
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
[in] | p | pointer to storage whose size to be adjusted |
[in] | n | new size for storage |
[in] | file | file name in which adjustment requested |
[in] | func | function name in which adjustment requested (if C99 supported) |
[in] | line | line number on which adjustment requested |