EGmem


Detailed Description

Here we define some usefull macros to deal with memory issues, for example, assert that we always return memory when posible, and if no memory is found, then we just exit to the system (because if there is trully no memory.... there is no much else to do... unless we start using shrinkable memory pools, like for example EGmemSlab , but that is still a long way off, it will also perform (if debugging enabled) some allocation / freeing checkings and so on.

Version:
0.0.1
History:
-2005-09-05


Files

file  eg_mem.h

Defines

#define __EG_MEM_FREE_CHECK__   (1 && DEBUG)
 this is used to enable malloc/free tracking and extra debugging
#define EG_MEM_ALIGN(ptr)   ((((size_t)ptr)+EG_MEM_ALIGNMENT-1)&(~(EG_MEM_ALIGNMENT-1)))
 Given a pointer, return it's aligned value.
#define EG_MEM_ALIGNMENT   8U
 memory aligment used by EG alloc functions.
#define EG_MEM_ALIGNMENT_SHIFT   3U
 $log_2(EG_MEM_ALIGNMENT)$.
#define EG_MEM_WORD_SIZE   (sizeof(void*))
 size of a normal word in this machine (a word is just big enough to store a pointer)
#define EGfree(A)
 This function replace free, the idea of this is to HOPEFULLY later develop a memory leack checker that tell us who and where asked for memory and didn't free it, in hte meantime they do nothing.
#define EGmalloc(A)
 this function replace malloc, check if the memory is not zero, if it is, it exit from the program, and display who called it and how much memory it tryed to alloc.
#define EGrealloc(__ptr, __sz)
 Realloc a given pointer to the new size, and check that we find enough memory to return. If we don't, we exit the execution.
#define EGsMalloc(type, count)   (type*)EGmalloc(sizeof(type)*(count))
 This function allocate 'count' elements of type 'type' and return a pointer of type 'type*'. If the memory is not available the program will exit indicating where it was trying to get memory and how much, it will also check some common errors like allocating zero bytes.
#define nullConstructor   ((EGconstructor_f)0)
 Null constructor function (do nothing).
#define nullDestructor   ((EGdestructor_f)0)
 Null destructor function (do nothing).
#define nullFree   ((EGfree_f)0)
 this is the the data free that does nothing, use it when you don't want/need to free the internal list data becouse you will do it elsewere

Typedefs

typedef void(* EGconstructor_f )(void *)
 type for constructor functions. Given a pointer to an element of some type, do the internal initialization necesary so that we can work with the lement, such initialization may include allocating some internal memory needed by the structure (not done by the user). This functions must never fail. if some unexpected error does happen inside, then the function should not return. (a call to exit(1) would do the trick).
typedef void(* EGdestructor_f )(void *)
 type for destructor functions. Given a pointer to an element of some type, free all internal memory related to the element allocated during the construction phase. (but not the pointer itself). This function must always succed, if an error happen, the function should never return. (a call to exit(1) would do the trick).
typedef void(* EGfree_f )(void *)
 type of the free functions that recive only one parameter


Define Documentation

#define __EG_MEM_FREE_CHECK__   (1 && DEBUG)
 

this is used to enable malloc/free tracking and extra debugging

Definition at line 159 of file eg_mem.h.

#define EG_MEM_ALIGN ptr   )     ((((size_t)ptr)+EG_MEM_ALIGNMENT-1)&(~(EG_MEM_ALIGNMENT-1)))
 

Given a pointer, return it's aligned value.

Definition at line 73 of file eg_mem.h.

#define EG_MEM_ALIGNMENT   8U
 

memory aligment used by EG alloc functions.

Definition at line 65 of file eg_mem.h.

#define EG_MEM_ALIGNMENT_SHIFT   3U
 

$log_2(EG_MEM_ALIGNMENT)$.

Definition at line 69 of file eg_mem.h.

#define EG_MEM_WORD_SIZE   (sizeof(void*))
 

size of a normal word in this machine (a word is just big enough to store a pointer)

Definition at line 62 of file eg_mem.h.

#define EGfree  ) 
 

Value:

({\
    EXIT(((A) && !(((size_t)(A))>>19)),"Trying to free pointer "#A\
        " with value %zd\nThis is probably an error",(size_t)(A));\
    if(A) free(A);\
    else WARNING(1,"Trying to free "#A", a NULL pointer");\
    (A) = 0;})
This function replace free, the idea of this is to HOPEFULLY later develop a memory leack checker that tell us who and where asked for memory and didn't free it, in hte meantime they do nothing.

Parameters:
A pointer to the piece of memory to be freed, if debuging is enabled, the function will test for freing NULL pointers, for suspicios address freing and so on. note that the given pointer will point to NULL after this call, thus reducing the posibility of freeing multiple times the same piece of memory, or of allocating it after freeing it.

Definition at line 172 of file eg_mem.h.

#define EGmalloc  ) 
 

Value:

({\
  size_t const _EGmp_sz_ = (A);\
  void * _EGmp_res_ = 0;\
  /*WARNINGL(0,!_EGmp_sz_,"Allocating 0 bytes");*/\
  if(_EGmp_sz_)\
  {\
    _EGmp_res_ = calloc((size_t)1,_EGmp_sz_);\
    EXIT(!_EGmp_res_,"Not enough memory while allocating %zd bytes",_EGmp_sz_);\
  }\
  _EGmp_res_;})
this function replace malloc, check if the memory is not zero, if it is, it exit from the program, and display who called it and how much memory it tryed to alloc.

Parameters:
A number of bytes to allocate.
Returns:
a void* pointer to the newly allocated memory, note that if the function returns at all, it will return with the amount of memory required, so no NULL checking is ever necesary after an EGmalloc call.

Definition at line 120 of file eg_mem.h.

#define EGrealloc __ptr,
__sz   ) 
 

Value:

({\
  const size_t ____sz = (size_t)(__sz);\
  (__ptr) = realloc((__ptr),____sz);\
  EXIT(!(__ptr)&&(____sz),"not enough memory while reallocating %zd",____sz);\
  (__ptr);})
Realloc a given pointer to the new size, and check that we find enough memory to return. If we don't, we exit the execution.

Parameters:
__ptr pointer to reallocate.
__sz new number of bytes to reallocate.
Returns:
pointer to the new block of memory

Definition at line 150 of file eg_mem.h.

#define EGsMalloc type,
count   )     (type*)EGmalloc(sizeof(type)*(count))
 

This function allocate 'count' elements of type 'type' and return a pointer of type 'type*'. If the memory is not available the program will exit indicating where it was trying to get memory and how much, it will also check some common errors like allocating zero bytes.

Parameters:
type type of the element required.
count number of contiguous elements of the given type required.
Returns:
pointer to the beggining of the allocated array of the apropiate type (so no casting is needed). Note that if this function returns at all, then the memory has been allocated and thus no NULL checking return is necesary.

Definition at line 142 of file eg_mem.h.

#define nullConstructor   ((EGconstructor_f)0)
 

Null constructor function (do nothing).

Definition at line 97 of file eg_mem.h.

#define nullDestructor   ((EGdestructor_f)0)
 

Null destructor function (do nothing).

Definition at line 109 of file eg_mem.h.

#define nullFree   ((EGfree_f)0)
 

this is the the data free that does nothing, use it when you don't want/need to free the internal list data becouse you will do it elsewere

Definition at line 84 of file eg_mem.h.


Typedef Documentation

typedef void(* EGconstructor_f)(void *)
 

type for constructor functions. Given a pointer to an element of some type, do the internal initialization necesary so that we can work with the lement, such initialization may include allocating some internal memory needed by the structure (not done by the user). This functions must never fail. if some unexpected error does happen inside, then the function should not return. (a call to exit(1) would do the trick).

Definition at line 93 of file eg_mem.h.

typedef void(* EGdestructor_f)(void *)
 

type for destructor functions. Given a pointer to an element of some type, free all internal memory related to the element allocated during the construction phase. (but not the pointer itself). This function must always succed, if an error happen, the function should never return. (a call to exit(1) would do the trick).

Definition at line 105 of file eg_mem.h.

typedef void(* EGfree_f)(void *)
 

type of the free functions that recive only one parameter

Definition at line 78 of file eg_mem.h.


Generated on Wed Nov 21 09:38:35 2007 for MTgomory by  doxygen 1.4.6