00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #ifndef _H_memalloc
00026
00027 #include <string.h>
00028
00029 #define _H_memalloc
00030
00031 struct chunkInfo;
00032 struct blockInfo;
00033 struct memoryPtr;
00034
00035 #define MEM_TABLE_SIZE 500
00036
00037 #ifdef LOCALE
00038 #undef LOCALE
00039 #endif
00040
00041 #ifdef _MEMORY_SOURCE_
00042 #define LOCALE
00043 #else
00044 #define LOCALE extern
00045 #endif
00046
00047 struct chunkInfo
00048 {
00049 struct chunkInfo *prevChunk;
00050 struct chunkInfo *nextFree;
00051 struct chunkInfo *lastFree;
00052 long int size;
00053 };
00054
00055 struct blockInfo
00056 {
00057 struct blockInfo *nextBlock;
00058 struct blockInfo *prevBlock;
00059 struct chunkInfo *nextFree;
00060 long int size;
00061 };
00062
00063 struct memoryPtr
00064 {
00065 struct memoryPtr *next;
00066 };
00067
00068 #define get_struct(theEnv,type) \
00069 ((MemoryData(theEnv)->MemoryTable[sizeof(struct type)] == NULL) ? \
00070 ((struct type *) genalloc(theEnv,sizeof(struct type))) :\
00071 ((MemoryData(theEnv)->TempMemoryPtr = MemoryData(theEnv)->MemoryTable[sizeof(struct type)]),\
00072 MemoryData(theEnv)->MemoryTable[sizeof(struct type)] = MemoryData(theEnv)->TempMemoryPtr->next,\
00073 ((struct type *) MemoryData(theEnv)->TempMemoryPtr)))
00074
00075 #define rtn_struct(theEnv,type,struct_ptr) \
00076 (MemoryData(theEnv)->TempMemoryPtr = (struct memoryPtr *) struct_ptr,\
00077 MemoryData(theEnv)->TempMemoryPtr->next = MemoryData(theEnv)->MemoryTable[sizeof(struct type)], \
00078 MemoryData(theEnv)->MemoryTable[sizeof(struct type)] = MemoryData(theEnv)->TempMemoryPtr)
00079
00080 #define rtn_sized_struct(theEnv,size,struct_ptr) \
00081 (MemoryData(theEnv)->TempMemoryPtr = (struct memoryPtr *) struct_ptr,\
00082 MemoryData(theEnv)->TempMemoryPtr->next = MemoryData(theEnv)->MemoryTable[size], \
00083 MemoryData(theEnv)->MemoryTable[size] = MemoryData(theEnv)->TempMemoryPtr)
00084
00085 #define get_var_struct(theEnv,type,vsize) \
00086 ((((sizeof(struct type) + vsize) < MEM_TABLE_SIZE) ? \
00087 (MemoryData(theEnv)->MemoryTable[sizeof(struct type) + vsize] == NULL) : 1) ? \
00088 ((struct type *) genalloc(theEnv,(sizeof(struct type) + vsize))) :\
00089 ((MemoryData(theEnv)->TempMemoryPtr = MemoryData(theEnv)->MemoryTable[sizeof(struct type) + vsize]),\
00090 MemoryData(theEnv)->MemoryTable[sizeof(struct type) + vsize] = MemoryData(theEnv)->TempMemoryPtr->next,\
00091 ((struct type *) MemoryData(theEnv)->TempMemoryPtr)))
00092
00093 #define rtn_var_struct(theEnv,type,vsize,struct_ptr) \
00094 (MemoryData(theEnv)->TempSize = sizeof(struct type) + vsize, \
00095 ((MemoryData(theEnv)->TempSize < MEM_TABLE_SIZE) ? \
00096 (MemoryData(theEnv)->TempMemoryPtr = (struct memoryPtr *) struct_ptr,\
00097 MemoryData(theEnv)->TempMemoryPtr->next = MemoryData(theEnv)->MemoryTable[MemoryData(theEnv)->TempSize], \
00098 MemoryData(theEnv)->MemoryTable[MemoryData(theEnv)->TempSize] = MemoryData(theEnv)->TempMemoryPtr) : \
00099 (genfree(theEnv,(void *) struct_ptr,MemoryData(theEnv)->TempSize),(struct memoryPtr *) struct_ptr)))
00100
00101 #define get_mem(theEnv,size) \
00102 (((size < MEM_TABLE_SIZE) ? \
00103 (MemoryData(theEnv)->MemoryTable[size] == NULL) : 1) ? \
00104 ((struct type *) genalloc(theEnv,(size_t) (size))) :\
00105 ((MemoryData(theEnv)->TempMemoryPtr = MemoryData(theEnv)->MemoryTable[size]),\
00106 MemoryData(theEnv)->MemoryTable[size] = MemoryData(theEnv)->TempMemoryPtr->next,\
00107 ((struct type *) MemoryData(theEnv)->TempMemoryPtr)))
00108
00109 #define rtn_mem(theEnv,size,ptr) \
00110 (MemoryData(theEnv)->TempSize = size, \
00111 ((MemoryData(theEnv)->TempSize < MEM_TABLE_SIZE) ? \
00112 (MemoryData(theEnv)->TempMemoryPtr = (struct memoryPtr *) ptr,\
00113 MemoryData(theEnv)->TempMemoryPtr->next = MemoryData(theEnv)->MemoryTable[MemoryData(theEnv)->TempSize], \
00114 MemoryData(theEnv)->MemoryTable[MemoryData(theEnv)->TempSize] = MemoryData(theEnv)->TempMemoryPtr) : \
00115 (genfree(theEnv,(void *) ptr,MemoryData(theEnv)->TempSize),(struct memoryPtr *) ptr)))
00116
00117 #define GenCopyMemory(type,cnt,dst,src) \
00118 memcpy((void *) (dst),(void *) (src),sizeof(type) * (size_t) (cnt))
00119
00120 #define MEMORY_DATA 59
00121
00122 struct memoryData
00123 {
00124 long int MemoryAmount;
00125 long int MemoryCalls;
00126 intBool ConserveMemory;
00127 int (*OutOfMemoryFunction)(void *,size_t);
00128 #if BLOCK_MEMORY
00129 struct blockInfo *TopMemoryBlock;
00130 int BlockInfoSize;
00131 int ChunkInfoSize;
00132 int BlockMemoryInitialized;
00133 #endif
00134 struct memoryPtr *TempMemoryPtr;
00135 struct memoryPtr **MemoryTable;
00136 size_t TempSize;
00137 };
00138
00139 #define MemoryData(theEnv) ((struct memoryData *) GetEnvironmentData(theEnv,MEMORY_DATA))
00140
00141 #define GetConserveMemory() EnvGetConserveMemory(GetCurrentEnvironment())
00142 #define MemRequests() EnvMemRequests(GetCurrentEnvironment())
00143 #define MemUsed() EnvMemUsed(GetCurrentEnvironment())
00144 #define ReleaseMem(a,b) EnvReleaseMem(GetCurrentEnvironment(),a,b)
00145 #define SetConserveMemory(a) EnvSetConserveMemory(GetCurrentEnvironment(),a)
00146 #define SetOutOfMemoryFunction(a) EnvSetOutOfMemoryFunction(GetCurrentEnvironment(),a)
00147
00148 LOCALE void InitializeMemory(void *);
00149 LOCALE void *genalloc(void *,size_t);
00150 LOCALE int DefaultOutOfMemoryFunction(void *,size_t);
00151 LOCALE int (*EnvSetOutOfMemoryFunction(void *,int (*)(void *,size_t)))(void *,size_t);
00152 LOCALE int genfree(void *,void *,size_t);
00153 LOCALE void *genrealloc(void *,void *,size_t,size_t);
00154 LOCALE long EnvMemUsed(void *);
00155 LOCALE long EnvMemRequests(void *);
00156 LOCALE long UpdateMemoryUsed(void *,long int);
00157 LOCALE long UpdateMemoryRequests(void *,long int);
00158 LOCALE long EnvReleaseMem(void *,long,int);
00159 LOCALE void *gm1(void *,size_t);
00160 LOCALE void *gm2(void *,size_t);
00161 LOCALE void *gm3(void *,size_t);
00162 LOCALE int rm(void *,void *,size_t);
00163 LOCALE int rm3(void *,void *,size_t);
00164 LOCALE unsigned long PoolSize(void *);
00165 LOCALE unsigned long ActualPoolSize(void *);
00166 LOCALE void *RequestChunk(void *,size_t);
00167 LOCALE int ReturnChunk(void *,void *,size_t);
00168 LOCALE intBool EnvSetConserveMemory(void *,intBool);
00169 LOCALE intBool EnvGetConserveMemory(void *);
00170 LOCALE void genmemcpy(char *,char *,unsigned long);
00171 LOCALE void ReturnAllBlocks(void *);
00172
00173 #endif
00174
00175
00176
00177
00178
00179