Next: 4.10.6 mq Up: 4.10 Classes Previous: 4.10.4 mb Contents Index
Subsections

- 4.10.5.1 API

##
4.10.5 mem

The mem class implements a memory allocation (malloc) wrapper. For the debug version of libonyx, extra information is hashed for each memory allocation that allows tracking of the following:

- File/line number of allocation.
- Double allocation/deallocation of the same address.
- Memory leaks (memory left allocated at mem destruction time).

If any memory leaks are detected, diagnostic output is printed to stderr.

Also, the debug version of libonyx sets all newly allocated bytes to `0xa5`, and all deallocated bytes to `0x5a` (except in the case of mem_calloc()). This tends to cause things to break sooner when uninitialized or deallocated memory is referenced.

In general, the mem class doesn't need to be used directly. Instead, there are several preprocessor macros that can be used: cw_malloc(), cw_calloc(), cw_realloc(), and cw_free().

The mema class encapsulates a set of pointers to allocation functions. It is used by the ch and dch classes.

### 4.10.5.1 API

cw_mema_t * mema_new(cw_mema_t *a_mema, cw_opaque_alloc_t *a_alloc, cw_opaque_calloc_t *a_calloc, cw_opaque_realloc_t *a_realloc, cw_opaque_dealloc_t *a_dealloc, void *a_arg):

  Input(s):
    a_mema:
      Pointer to space for a mema, or NULL.
    a_alloc:
      Pointer to an allocation function.
    a_alloc:
      Pointer to a zero-ing allocation function.
    a_alloc:
      Pointer to a reallocation function.
    a_dealloc:
      Pointer to a deallocation function.
    a_arg:
      Opaque pointer to pass to a_alloc(), a_calloc(), a_realloc(), and a_dealloc().
  Output(s):
    retval:
      Pointer to a mema.
  Exception(s):
    CW_ONYXX_OOM.
  Description:
    Constructor.

void mema_delete(cw_mema_t *a_mema):

  Input(s):
    a_mema:
      Pointer to a mema.
  Output(s):
    None.
  Exception(s):
    None.
  Description:
    Destructor.

cw_opaque_alloc_t * mema_alloc_get(cw_mema_t *a_mema):

  Input(s):
    a_mema:
      Pointer to a mema.
  Output(s):
    retval:
      Pointer to an allocation function.
  Exception(s):
    None.
  Description:
    Return a pointer to an allocation function.

cw_opaque_calloc_t * mema_calloc_get(cw_mema_t *a_mema):

  Input(s):
    a_mema:
      Pointer to a mema.
  Output(s):
    retval:
      Pointer to a zero-ing allocation function.
  Exception(s):
    None.
  Description:
    Return a pointer to a zero-ing allocation function.

cw_opaque_realloc_t * mema_realloc_get(cw_mema_t *a_mema):

  Input(s):
    a_mema:
      Pointer to a mema.
  Output(s):
    retval:
      Pointer to a reallocation function.
  Exception(s):
    None.
  Description:
    Return a pointer to a reallocation function.

cw_opaque_dealloc_t * mema_dealloc_get(cw_mema_t *a_mema):

  Input(s):
    a_mema:
      Pointer to a mema.
  Output(s):
    retval:
      Pointer to a deallocation function.
  Exception(s):
    None.
  Description:
    Return a pointer to a deallocation function.

cw_opaque_arg_t * mema_arg_get(cw_mema_t *a_mema):

  Input(s):
    a_mema:
      Pointer to a mema.
  Output(s):
    retval:
      Opaque pointer to pass to a_alloc(), a_calloc(), a_realloc(), and a_dealloc().
  Exception(s):
    None.
  Description:
    Return an opaque pointer to pass to the allocation functions returned by mema_alloc_get(a_mema), mema_calloc_get(a_mema), mema_realloc_get(a_mema), and mema_dealloc_get(a_mema).

cw_mem_t * mem_new(cw_mem_t *a_mem, cw_mem_t *a_internal):

  Input(s):
    a_mem:
      Pointer to space for a mem, or NULL.
    a_internal:
      Pointer to a mem to use for internal memory allocation, or NULL.
  Output(s):
    retval:
      Pointer to a mem.
  Exception(s):
    CW_ONYXX_OOM.
  Description:
    Constructor.

void mem_delete(cw_mem_t *a_mem):

  Input(s):
    a_mem:
      Pointer to a mem.
  Output(s):
    None.
  Exception(s):
    None.
  Description:
    Destructor.

void * mem_malloc_e(cw_mem_t *a_mem, size_t a_size, const char *a_filename, cw_uint32_t a_line_num):

void * mem_malloc(cw_mem_t *a_mem, size_t a_size):

void * cw_malloc(size_t a_size):

  Input(s):
    a_mem:
      Pointer to a mem.
    a_size:
      Size of memory range to allocate.
    a_filename:
      Should be __FILE__.
    a_line_num:
      Should be __LINE__.
  Output(s):
    retval:
      Pointer to a memory range.
  Exception(s):
    CW_ONYXX_OOM.
  Description:
    malloc() wrapper.

void * mem_calloc_e(cw_mem_t *a_mem, size_t a_number, size_t a_size, const char *a_filename, cw_uint32_t a_line_num):

void * mem_calloc(cw_mem_t *a_mem, size_t a_number, size_t a_size):

void * cw_calloc(size_t a_number, size_t a_size):

  Input(s):
    a_mem:
      Pointer to a mem.
    a_number:
      Number of elements to allocate.
    a_size:
      Size of each element to allocate.
    a_filename:
      Should be __FILE__.
    a_line_num:
      Should be __LINE__.
  Output(s):
    retval:
      Pointer to a zeroed memory range.
  Exception(s):
    CW_ONYXX_OOM.
  Description:
    calloc() wrapper.

void * mem_realloc_e(cw_mem_t *a_mem, void *a_ptr, size_t a_size, size_t a_old_size, const char *a_filename, cw_uint32_t a_line_num):

void * mem_realloc(cw_mem_t *a_mem, void *a_ptr, size_t a_size):

void * cw_realloc(void *a_ptr, size_t a_size):

  Input(s):
    a_mem:
      Pointer to a mem.
    a_ptr:
      Pointer to memory range to be reallocated.
    a_size:
      Size of memory range to allocate.
    a_old_size:
      Size of memory range previously pointed to by a_ptr.
    a_filename:
      Should be __FILE__.
    a_line_num:
      Should be __LINE__.
  Output(s):
    retval:
      Pointer to a memory range.
  Exception(s):
    CW_ONYXX_OOM.
  Description:
    realloc() wrapper.

void mem_free_e(cw_mem_t *a_mem, void *a_ptr, size_t a_size, const char *a_filename, cw_uint32_t a_line_num):

void mem_free(cw_mem_t *a_mem, void *a_ptr, size_t a_size):

void cw_free(void *a_ptr):

  Input(s):
    a_mem:
      Pointer to a mem.
    a_ptr:
      Pointer to to memory range to be freed.
    a_size:
      Sizef of memory range pointed to by a_ptr.
    a_filename:
      Should be __FILE__.
    a_line_num:
      Should be __LINE__.
  Output(s):
    None.
  Exception(s):
    None.
  Description:
    free() wrapper.

Next: 4.10.6 mq Up: 4.10 Classes Previous: 4.10.4 mb Contents Index