00001 #ifndef __J2K__FreeList_H__ 00002 #define __J2K__FreeList_H__ 1 00003 00004 #include <stdio.h> 00005 #include <string.h> 00006 #include <malloc.h> 00007 #include <assert.h> 00008 00009 /* One FreeList link implemented as a single link-list stack */ 00010 typedef struct _FreeLink_t 00011 { 00012 _FreeLink_t* next; /* Next element pointer */ 00013 00014 } FreeLink_t; 00015 00016 /* 00017 The FreeList itself containing the size to be allocated 00018 and safely upgraded if too small 00019 (e.g. less than 4 bytes on 32-bit machines) 00020 */ 00021 typedef struct _FreeList_t 00022 { 00023 FreeLink_t* head; /* Top of the FreeList stack */ 00024 size_t size; /* Size to be allocated on each FreeLink */ 00025 00026 } FreeList_t; 00027 00028 /* 00029 This function must be call in order to create a valid FreeList. 00030 00031 GOTCHA: 00032 ======= 00033 1. Don't hard code 4 bytes (32-bit address space), due to 16 and 64 bit machines ! 00034 00035 2. Doing a FreeList of char is equivalent to doing a FreeList of int or double ! 00036 00037 3. Notice that this way we save 4 bytes per FreeLink, so basically the FreeList 00038 don't cost any space per item except the FreeList container itself 00039 which is merely 8 bytes ! 00040 */ 00041 FreeList_t* FreeList_create( register size_t sz ); 00042 00043 /* 00044 Allocate the desired amount of memory, 00045 if no FreeList link available 00046 */ 00047 void* FreeList_calloc( FreeList_t* this ); 00048 00049 /* 00050 Falsely delete the dead structure, 00051 by stacking it up to the FreeList, 00052 for future usage. 00053 */ 00054 void FreeList_free( FreeList_t* this, void* dead ); 00055 00056 /* 00057 Really destroy the entire FreeList elements 00058 */ 00059 void FreeList_purge( FreeList_t* this ); 00060 00061 /* 00062 Really destroy the entire FreeList elements 00063 including the FreeList itself ! 00064 */ 00065 void FreeList_delete( FreeList_t* this ); 00066 00067 00068 #endif