kjb_malloc - Allocates memory from the heap.


#include "l/l_sys_mal.h"

Example compile flags (system dependent):
   -L/home/kobus/misc/load/linux_x86_64_opteron -L/usr/lib/x86_64-linux-gnu
  -lKJB                               -lfftw3  -lgsl -lgslcblas -ljpeg  -lSVM -lstdc++                    -lpthread -lSLATEC -lg2c    -lacml -lacml_mv -lblas -lg2c      -lncursesw 

void *kjb_malloc(Malloc_size num_bytes);


This routine is a replacement for malloc. It provides some error checking in the case of development code (compiled with -DTEST), some platform independence with respect to malloc's behaviour, and error reporting consistent with the KJB library. If TRACK_MEMORY_ALLOCATION is defined (on unix, this is normally defined if TEST is #defined) then this routine is #define'd to be debug_kjb_malloc, which is the version available in the development library. In development code, memory is tracked so that memory leaks can be found more easily. Furthermore, all memory free'd is checked that it was allocated by a KJB library routine, and that it has not already been freed (this is a common source of nasty bugs). Finally, memory is checked for overruns. Most of these problems are spotted when the code calls kjb_free. If a problem is spotted, then you are notified, and asked if you want to abort. If you then abort, then you simply need to go up a few stack frames with the debugger to find ut where you were free'ing something that you should not be, or what was freed that was over-run. The memory leak check is simply a report supplied at program exit of the memory not freed, and where it was allocated. Since it is often more useful that the allocation locatation is specified with respect to higher level operations (such as creating a matrix), some routines such as create_matrix will set it up so that the location reported is the matrix creation call, not the call to kjb_malloc. The routine kjb_free should be used to free memory allocated by this routine. kjb_free is normally accessed through the macro kjb_free.


On error, this routine returns NULL, with an error message being set. On success it returns a pointer to the memory, just like malloc.


Normally kjb_malloc should be accessed through macros to provide the casts. The available macros are: KJB_MALLOC(x), TYPE_MALLOC(t), N_TYPE_MALLOC(t,n), VOID_MALLOC(x), UCHAR_MALLOC(x), BYTE_MALLOC(x), STR_MALLOC(x), SHORT_MALLOC(x), INT_MALLOC(x), LONG_MALLOC(x), FLT_MALLOC(x), DBL_MALLOC(x), INT16_MALLOC(x), UINT16_MALLOC(x), INT32_MALLOC(x), UINT32_MALLOC(x), DBL_MALLOC(x) The use of these is relatively obvious, except possibly N_TYPE_MALLOC, which takes a type name as the first argument, and the number that you want as the second.


This software is not adequatedly tested. It is recomended that results are checked independantly where appropriate.


Kobus Barnard


Kobus Barnard


memory_used , kjb_calloc , kjb_realloc , watch_for_free , check_initialization , optimize_free , optimize_free_2 , kjb_free