EGmemPool


Detailed Description

This header contains the definitions of memory pools, this memory pools are used by some sub-libraries to ask for memory. These memory pools try to alloc big chunks of memory and give it to the system as you ask for it; it also try to re-utilize 'freed' memory by keeping the address of the 'freed' memory.

Once you free all memory inside the memory pool, all the structures that alloc'ed memory from this pool will be free so __BE__ __CAREFUL__.

Also, the memory pool can't alloc sizes under sizeof(void*), if you call memory from the pool of a smaller size you will recive a block of size sizeof(void*) anyway.

Finally you have to alloc and free memory pools by the functions EGnewMempool and EGfreeMempool

Another point is that the EGmemPoolFree can't deal with null pointers, you have to check if the pointer to free is null or not before calling it.

Version:
0.0.1
History:


Files

file  eg_mempool.h

Data Structures

struct  EGmemPool_t
 this structure holds a memory pool More...
#define __EGmemPoolClear(pool)
#define __EGmemPoolClearProfile(pool)
 Free internal memory non-allocated by the user in this structure.
#define EGmemPoolClear(pool)   if(pool) __EGmemPoolClear(pool)

Defines

#define __EG_MEMPOOL_BIGBUCK__(size)
#define __EG_MEMPOOL_DEBUGL__   200
 debug level for this set of functions. If the debug used at compile time was at least this level, we will generate tons of messages to keep track of what was going on
#define __EG_MEMPOOL_GET_FFILE(A)   (*((const char**)(((char*)A)+5*__EG_MEMPOOL_WORD__)))
#define __EG_MEMPOOL_GET_FILE(A)   (*((const char**)(((char*)A)+4*__EG_MEMPOOL_WORD__)))
#define __EG_MEMPOOL_GET_FLINE(A)   ((size_t)(*((void**)(((char*)A)+6*__EG_MEMPOOL_WORD__))))
#define __EG_MEMPOOL_GET_LINE(A)   ((size_t)(*((void**)(((char*)A)+2*__EG_MEMPOOL_WORD__))))
#define __EG_MEMPOOL_GET_SIZE(A)   ((size_t)(*((void**)(((char*)A)+1*__EG_MEMPOOL_WORD__))))
#define __EG_MEMPOOL_GET_STATUS(A)   ((size_t)(*((void**)(((char*)A)+3*__EG_MEMPOOL_WORD__))))
#define __EG_MEMPOOL_MALLOC_FREE_CHECK__   (!__EG_MEMPOOL_REDUCE_TO_MALLOC__ && 1 && DEBUG)
 this is used to enable malloc/free tracking and extra debugging
#define __EG_MEMPOOL_MEM_SHIFT__   8
#define __EG_MEMPOOL_N_BIG_CHUNKS__   50
#define __EG_MEMPOOL_OVERHEAD__   (8*(EG_MEM_ALIGNMENT))
 if we enable malloc checking, we will add to every piece of memory four void*, one for size, other for where we alloc'it, line, and a status
#define __EG_MEMPOOL_PROFILE__   (2 && (__EG_MEMPOOL_REDUCE_TO_MALLOC__ || !DEBUG ? 0 : 7))
 this is used for debugg and profile purposes only, when setted it keeps profiling information about the memory pool usage
#define __EG_MEMPOOL_REDUCE_TO_MALLOC__   0
 if we want to reduce all this to malloc/free we can do it at compile time by setting this constant to 1
#define __EG_MEMPOOL_SET_ALLOCED(A)   (*((void**)(((char*)A)+3*__EG_MEMPOOL_WORD__))=0)
#define __EG_MEMPOOL_SET_FFILE(A, B)   (*((const char **)((char*)A+5*__EG_MEMPOOL_WORD__))=B)
#define __EG_MEMPOOL_SET_FILE(A, B)   (*((const char **)((char*)A+4*__EG_MEMPOOL_WORD__))=B)
#define __EG_MEMPOOL_SET_FLINE(A, B)   (*((void**)(((char*)A)+6*__EG_MEMPOOL_WORD__))=(void*)((size_t)B))
#define __EG_MEMPOOL_SET_FREEED(A)   (*((void**)(((char*)A)+3*__EG_MEMPOOL_WORD__))=(void*)((size_t)1))
#define __EG_MEMPOOL_SET_LINE(A, B)   (*((void**)(((char*)A)+2*__EG_MEMPOOL_WORD__))=(void*)((size_t)B))
#define __EG_MEMPOOL_SET_SIZE(A, B)   (*((void**)(((char*)A)+1*__EG_MEMPOOL_WORD__))=(void*)((size_t)B))
#define __EG_MEMPOOL_STATUS_ALLOCED__   0
#define __EG_MEMPOOL_STATUS_FREEED__   1
#define __EG_MEMPOOL_WORD__   EG_MEM_ALIGNMENT
#define __EGmemPoolInitProfile(lpool)
 If profiling is enabled, initiaslize the profiling information of the memory pool.
#define EGmemPoolFree(A, B, C)   __EGmemPoolFree(A,B,C,__FILE__,__LINE__)
 this function free to the memory pool a memory block of size 'size; It is __VERY__ important to free a block memory this way and to the apropiate memory pool, any error in this __WILL__ cause serious memory errors
#define EGmemPoolInit(pool, manage_limit, resize_fn, first_size)
 Initialize a memory pool structure with the given parameters.
#define EGmemPoolMalloc(A, B)   __EGmemPoolMalloc(A,B,__FILE__,__LINE__)
 this function ask to a memory pool a memory block of size 'size', in fact this is a macro that calls the real malloc function but that keeps track of who and where was it call
#define EGmemPoolSFree(mem, type, count, pool)   EGmemPoolFree(mem,sizeof(type)*(count),pool)
 this macro define a 'safe' free for the memory pool, it recives the type and how many elements of that type we want
#define EGmemPoolSMalloc(mem, type, count)   (type*)EGmemPoolMalloc(mem,sizeof(type)*(count))
 this macro define a 'safe' malloc for the memory pool, it recives the type and how many elements of that type we want
#define EGnewMemPool(A, B, C)   __EGnewMemPool(A,B,C)
#define nullCopyMP   ((EGcopyMP_f)0)
 NULL copy function.
#define nullFreeMP   ((EGfreeMP_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 *(* EGcopyMP_f )(void *p, EGmemPool_t *mem)
 this definition is intended to identify copy functions, these functions return copy of objects but with independent storage space, there are two versions, one that require a memory pool from where to look for memory, and another where we don't care about that.... the place from where the memory was asked for depend on the function, se the function definition for details. Note that if the is no more memory available the function should call exit(1).
typedef void(* EGfreeMP_f )(void *, EGmemPool_t *)
 type of the free functions that recive a pointer to the memory to be freed, and a pointer to the memory pool where store the 'freed' memory.

Functions

void __EGmemPoolFree (void *mem, size_t size, EGmemPool_t *mypool, const char *file, const int line)
void * __EGmemPoolMalloc (EGmemPool_t *const, size_t, const char *, const int)
EGmemPool_t__EGnewMemPool (const size_t manageLimit, size_t(*newsize)(size_t), const size_t initSize)
 this fucntion allocate a new memory pool, with resize function 'newsize' with manageLimit 'manageLimit' and with initial memory of size 'initSize'. note that a memory pool with 'manageLimit == 0' is equivalent to malloc/free
void EGfreeMemPool (EGmemPool_t *)
 this function free a memory pool
void EGmemPoolClear (EGmemPool_t *)
 this function liberate the memory stored in the memory pool
size_t EGmemPoolNewSize (size_t)
 this function offer a default memory resizer strategy, this function return 1 << (i+10)


Define Documentation

#define __EG_MEMPOOL_BIGBUCK__ size   ) 
 

Value:

Definition at line 194 of file eg_mempool.h.

#define __EG_MEMPOOL_DEBUGL__   200
 

debug level for this set of functions. If the debug used at compile time was at least this level, we will generate tons of messages to keep track of what was going on

Definition at line 152 of file eg_mempool.h.

#define __EG_MEMPOOL_GET_FFILE  )     (*((const char**)(((char*)A)+5*__EG_MEMPOOL_WORD__)))
 

Definition at line 186 of file eg_mempool.h.

#define __EG_MEMPOOL_GET_FILE  )     (*((const char**)(((char*)A)+4*__EG_MEMPOOL_WORD__)))
 

Definition at line 178 of file eg_mempool.h.

#define __EG_MEMPOOL_GET_FLINE  )     ((size_t)(*((void**)(((char*)A)+6*__EG_MEMPOOL_WORD__))))
 

Definition at line 188 of file eg_mempool.h.

#define __EG_MEMPOOL_GET_LINE  )     ((size_t)(*((void**)(((char*)A)+2*__EG_MEMPOOL_WORD__))))
 

Definition at line 182 of file eg_mempool.h.

#define __EG_MEMPOOL_GET_SIZE  )     ((size_t)(*((void**)(((char*)A)+1*__EG_MEMPOOL_WORD__))))
 

Definition at line 180 of file eg_mempool.h.

#define __EG_MEMPOOL_GET_STATUS  )     ((size_t)(*((void**)(((char*)A)+3*__EG_MEMPOOL_WORD__))))
 

Definition at line 184 of file eg_mempool.h.

#define __EG_MEMPOOL_MALLOC_FREE_CHECK__   (!__EG_MEMPOOL_REDUCE_TO_MALLOC__ && 1 && DEBUG)
 

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

Definition at line 143 of file eg_mempool.h.

#define __EG_MEMPOOL_MEM_SHIFT__   8
 

Definition at line 193 of file eg_mempool.h.

#define __EG_MEMPOOL_N_BIG_CHUNKS__   50
 

Definition at line 192 of file eg_mempool.h.

#define __EG_MEMPOOL_OVERHEAD__   (8*(EG_MEM_ALIGNMENT))
 

if we enable malloc checking, we will add to every piece of memory four void*, one for size, other for where we alloc'it, line, and a status

Definition at line 160 of file eg_mempool.h.

#define __EG_MEMPOOL_PROFILE__   (2 && (__EG_MEMPOOL_REDUCE_TO_MALLOC__ || !DEBUG ? 0 : 7))
 

this is used for debugg and profile purposes only, when setted it keeps profiling information about the memory pool usage

Definition at line 137 of file eg_mempool.h.

#define __EG_MEMPOOL_REDUCE_TO_MALLOC__   0
 

if we want to reduce all this to malloc/free we can do it at compile time by setting this constant to 1

Definition at line 129 of file eg_mempool.h.

#define __EG_MEMPOOL_SET_ALLOCED  )     (*((void**)(((char*)A)+3*__EG_MEMPOOL_WORD__))=0)
 

Definition at line 168 of file eg_mempool.h.

#define __EG_MEMPOOL_SET_FFILE A,
 )     (*((const char **)((char*)A+5*__EG_MEMPOOL_WORD__))=B)
 

Definition at line 172 of file eg_mempool.h.

#define __EG_MEMPOOL_SET_FILE A,
 )     (*((const char **)((char*)A+4*__EG_MEMPOOL_WORD__))=B)
 

Definition at line 162 of file eg_mempool.h.

#define __EG_MEMPOOL_SET_FLINE A,
 )     (*((void**)(((char*)A)+6*__EG_MEMPOOL_WORD__))=(void*)((size_t)B))
 

Definition at line 174 of file eg_mempool.h.

#define __EG_MEMPOOL_SET_FREEED  )     (*((void**)(((char*)A)+3*__EG_MEMPOOL_WORD__))=(void*)((size_t)1))
 

Definition at line 170 of file eg_mempool.h.

#define __EG_MEMPOOL_SET_LINE A,
 )     (*((void**)(((char*)A)+2*__EG_MEMPOOL_WORD__))=(void*)((size_t)B))
 

Definition at line 166 of file eg_mempool.h.

#define __EG_MEMPOOL_SET_SIZE A,
 )     (*((void**)(((char*)A)+1*__EG_MEMPOOL_WORD__))=(void*)((size_t)B))
 

Definition at line 164 of file eg_mempool.h.

#define __EG_MEMPOOL_STATUS_ALLOCED__   0
 

Definition at line 176 of file eg_mempool.h.

#define __EG_MEMPOOL_STATUS_FREEED__   1
 

Definition at line 177 of file eg_mempool.h.

#define __EG_MEMPOOL_WORD__   EG_MEM_ALIGNMENT
 

Definition at line 161 of file eg_mempool.h.

#define __EGmemPoolClear pool   ) 
 

Value:

({\
  EGmemPool_t*const __EGmp = (pool);\
  __EGmemPoolClearProfile(__EGmp);\
  while(__EGmp->MemoryListSize--) EGfree(__EGmp->MemoryList[__EGmp->MemoryListSize]);\
  EGfree(__EGmp->MemoryList);\
  if(__EGmp->manageLimit) EGfree(__EGmp->freedMemory);})

Definition at line 459 of file eg_mempool.h.

#define __EGmemPoolClearProfile pool   ) 
 

Free internal memory non-allocated by the user in this structure.

Parameters:
pool memory pool to be cleared

Definition at line 415 of file eg_mempool.h.

#define __EGmemPoolInitProfile lpool   ) 
 

Value:

{\
  (lpool)->memAllocationFile = __FILE__;\
  (lpool)->memAllocationLine = __LINE__;\
  (lpool)->totalMem = (lpool)->freeArraySize;\
  if((lpool)->manageLimit) (lpool)->maxUsedMem = EGsMalloc(size_t,((lpool)->manageLimit >> EG_MEM_ALIGNMENT_SHIFT)+1);\
  else (lpool)->maxUsedMem = 0;\
  if((lpool)->manageLimit) (lpool)->curUsedMem = EGsMalloc(size_t,((lpool)->manageLimit >> EG_MEM_ALIGNMENT_SHIFT)+1);\
  else (lpool)->curUsedMem = 0;}
If profiling is enabled, initiaslize the profiling information of the memory pool.

Parameters:
lpool memory pool to initialize profiling information.

Definition at line 377 of file eg_mempool.h.

#define EGmemPoolClear pool   )     if(pool) __EGmemPoolClear(pool)
 

Definition at line 467 of file eg_mempool.h.

#define EGmemPoolFree A,
B,
 )     __EGmemPoolFree(A,B,C,__FILE__,__LINE__)
 

this function free to the memory pool a memory block of size 'size; It is __VERY__ important to free a block memory this way and to the apropiate memory pool, any error in this __WILL__ cause serious memory errors

Definition at line 332 of file eg_mempool.h.

#define EGmemPoolInit pool,
manage_limit,
resize_fn,
first_size   ) 
 

Value:

({\
  EGmemPool_t *const __EGmp = (pool);\
  (__EGmp)->manageLimit = (manage_limit);\
  (__EGmp)->newsize = (resize_fn);\
  if((__EGmp)->manageLimit) (__EGmp)->freedMemory = EGsMalloc(void*,((__EGmp)->manageLimit >> EG_MEM_ALIGNMENT_SHIFT)+1);\
  else (__EGmp)->freedMemory = 0;\
  (__EGmp)->freeArraySize = (first_size);\
  if((signed)((__EGmp)->freeArraySize) < sysconf(_SC_PAGESIZE)) (__EGmp)->freeArraySize = sysconf(_SC_PAGESIZE);\
  (__EGmp)->MemoryListSize = 1;\
  (__EGmp)->MemoryList = EGsMalloc(void*,1);\
  (__EGmp)->freeArrayHead = (__EGmp)->MemoryList[0] = (void*)EGmalloc((__EGmp)->freeArraySize);\
  __EGmemPoolInitProfile(__EGmp);})
Initialize a memory pool structure with the given parameters.

Parameters:
pool memory pool to initialize.
manage_limit maximum size of blocks to pool.
resize_fn function that dictaminates the grow of the pool.
first_size initial size of the memory allocated in the pool.
Returns:
zero on success, non-zero otherwise.

Definition at line 397 of file eg_mempool.h.

#define EGmemPoolMalloc A,
 )     __EGmemPoolMalloc(A,B,__FILE__,__LINE__)
 

this function ask to a memory pool a memory block of size 'size', in fact this is a macro that calls the real malloc function but that keeps track of who and where was it call

Definition at line 307 of file eg_mempool.h.

#define EGmemPoolSFree mem,
type,
count,
pool   )     EGmemPoolFree(mem,sizeof(type)*(count),pool)
 

this macro define a 'safe' free for the memory pool, it recives the type and how many elements of that type we want

Definition at line 349 of file eg_mempool.h.

#define EGmemPoolSMalloc mem,
type,
count   )     (type*)EGmemPoolMalloc(mem,sizeof(type)*(count))
 

this macro define a 'safe' malloc for the memory pool, it recives the type and how many elements of that type we want

Definition at line 323 of file eg_mempool.h.

#define EGnewMemPool A,
B,
 )     __EGnewMemPool(A,B,C)
 

Examples:
eg_net.ex.c.

Definition at line 292 of file eg_mempool.h.

#define nullCopyMP   ((EGcopyMP_f)0)
 

NULL copy function.

Definition at line 365 of file eg_mempool.h.

#define nullFreeMP   ((EGfreeMP_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 282 of file eg_mempool.h.


Typedef Documentation

typedef void*(* EGcopyMP_f)(void *p, EGmemPool_t *mem)
 

this definition is intended to identify copy functions, these functions return copy of objects but with independent storage space, there are two versions, one that require a memory pool from where to look for memory, and another where we don't care about that.... the place from where the memory was asked for depend on the function, se the function definition for details. Note that if the is no more memory available the function should call exit(1).

Definition at line 360 of file eg_mempool.h.

typedef void(* EGfreeMP_f)(void *, EGmemPool_t *)
 

type of the free functions that recive a pointer to the memory to be freed, and a pointer to the memory pool where store the 'freed' memory.

Definition at line 275 of file eg_mempool.h.


Function Documentation

void __EGmemPoolFree void *  mem,
size_t  size,
EGmemPool_t mypool,
const char *  file,
const int  line
 

Definition at line 337 of file eg_mempool.c.

void* __EGmemPoolMalloc EGmemPool_t const,
size_t  ,
const char *  ,
const   int
 

Definition at line 153 of file eg_mempool.c.

EGmemPool_t* __EGnewMemPool const size_t  manageLimit,
size_t(*)(size_t)  newsize,
const size_t  initSize
 

this fucntion allocate a new memory pool, with resize function 'newsize' with manageLimit 'manageLimit' and with initial memory of size 'initSize'. note that a memory pool with 'manageLimit == 0' is equivalent to malloc/free

Definition at line 95 of file eg_mempool.c.

void EGfreeMemPool EGmemPool_t  ) 
 

this function free a memory pool

Definition at line 125 of file eg_mempool.c.

void EGmemPoolClear EGmemPool_t  ) 
 

this function liberate the memory stored in the memory pool

size_t EGmemPoolNewSize size_t   ) 
 

this function offer a default memory resizer strategy, this function return 1 << (i+10)

Definition at line 467 of file eg_mempool.c.


Generated on Mon Jan 30 08:55:34 2006 for EGlib by  doxygen 1.4.5