00001 00005 #ifndef __ALLOCATE_H_INCLUDED__ 00006 #define __ALLOCATE_H_INCLUDED__ 00007 00008 #include <malloc.h> 00009 00010 #if defined(__GNUC__) || defined(__cplusplus) 00011 #define ALLOC_INLINE inline 00012 #else 00013 #define ALLOC_INLINE 00014 #endif 00015 00016 #define DEFINE_ALLOCATOR(Type,Count,NextLink) \ 00017 \ 00018 static Type *free_list_##Type; \ 00019 static int alloc_count_##Type; \ 00020 \ 00021 static ALLOC_INLINE Type * \ 00022 ALLOC_##Type(void) \ 00023 { \ 00024 Type *p; \ 00025 int i; \ 00026 if(!free_list_##Type) { \ 00027 if(alloc_count_##Type == 0) { \ 00028 alloc_count_##Type = Count * sizeof(Type); \ 00029 for(i = 3; ; i++) { \ 00030 if(((1 << i) - 32) >= alloc_count_##Type) { \ 00031 alloc_count_##Type = ((1 << i) - 32) / sizeof(Type); \ 00032 break; \ 00033 } \ 00034 } \ 00035 } \ 00036 if(!(p = malloc(sizeof(Type) * alloc_count_##Type))) { \ 00037 return(NULL); \ 00038 } \ 00039 for(i = 0; i < alloc_count_##Type; i++) { \ 00040 p->NextLink = free_list_##Type; \ 00041 free_list_##Type = p; \ 00042 p++; \ 00043 } \ 00044 } \ 00045 p = free_list_##Type; \ 00046 free_list_##Type = p->NextLink; \ 00047 return(p); \ 00048 } \ 00049 \ 00050 static ALLOC_INLINE void \ 00051 FREE_##Type(Type *p) \ 00052 { \ 00053 p->NextLink = free_list_##Type; \ 00054 free_list_##Type = p; \ 00055 } \ 00056 \ 00057 static Type *free_list_##Type = NULL; \ 00058 static int alloc_count_##Type = 0 00059 00060 #endif