00001
00013
00014 #include <string.h>
00015 #include "NewtMem.h"
00016
00017
00018
00026 newtPool NewtPoolAlloc(int32_t expandspace)
00027 {
00028 newtPool pool;
00029
00030 pool = (newtPool)calloc(1, sizeof(newtpool_t));
00031
00032 if (pool != NULL)
00033 {
00034 pool->maxspace = pool->expandspace = expandspace;
00035 }
00036
00037 return pool;
00038 }
00039
00040
00041
00052 void * NewtMemAlloc(newtPool pool, size_t size)
00053 {
00054 if (pool != NULL)
00055 {
00056 }
00057
00058 return malloc(size);
00059 }
00060
00061
00062
00074 void * NewtMemCalloc(newtPool pool, size_t number, size_t size)
00075 {
00076 if (pool != NULL)
00077 {
00078 }
00079
00080 return calloc(number, size);
00081 }
00082
00083
00084
00096 void * NewtMemRealloc(newtPool pool, void * ptr, size_t size)
00097 {
00098 if (pool != NULL)
00099 {
00100 }
00101
00102 return realloc(ptr, size);
00103 }
00104
00105
00106
00116 void NewtMemFree(void * ptr)
00117 {
00118 free(ptr);
00119 }
00120
00121
00122 #pragma mark -
00123
00134 void NewtStackSetup(newtStack * stackinfo,
00135 newtPool pool, uint32_t datasize, uint32_t blocksize)
00136 {
00137 memset(stackinfo, 0, sizeof(newtStack));
00138
00139 stackinfo->pool = pool;
00140 stackinfo->datasize = datasize;
00141 stackinfo->blocksize = blocksize;
00142 }
00143
00144
00145
00153 void NewtStackFree(newtStack * stackinfo)
00154 {
00155 if (stackinfo->stackp != NULL)
00156 {
00157 NewtMemFree(stackinfo->stackp);
00158 stackinfo->stackp = NULL;
00159 }
00160
00161 stackinfo->nums = 0;
00162 stackinfo->sp = 0;
00163 }
00164
00165
00166
00176 bool NewtStackExpand(newtStack * stackinfo, uint32_t n)
00177 {
00178 if (stackinfo->nums < n)
00179 {
00180 uint32_t newsize;
00181 uint32_t nums;
00182 void * newp;
00183
00184 nums = stackinfo->nums + stackinfo->blocksize;
00185 newsize = stackinfo->datasize * nums;
00186
00187 newp = NewtMemRealloc(stackinfo->pool, stackinfo->stackp, newsize);
00188
00189 if (newp == NULL) return false;
00190
00191 stackinfo->stackp = newp;
00192 stackinfo->nums = nums;
00193 }
00194
00195 return true;
00196 }
00197
00198
00199
00208 void NewtStackSlim(newtStack * stackinfo, uint32_t n)
00209 {
00210 if (0 < n)
00211 {
00212 uint32_t newsize;
00213 void * newp;
00214
00215 newsize = stackinfo->datasize * n;
00216 newp = NewtMemRealloc(stackinfo->pool, stackinfo->stackp, newsize);
00217
00218 if (newp != NULL)
00219 {
00220 stackinfo->stackp = newp;
00221 stackinfo->nums = n;
00222 }
00223 }
00224 else
00225 {
00226 NewtStackFree(stackinfo);
00227 }
00228 }
00229
00230
00231 #pragma mark -
00232
00241 uint32_t NewtAlign(uint32_t n, uint16_t byte)
00242 {
00243 uint32_t mod;
00244
00245 mod = n % byte;
00246 if (0 < mod) n += byte - mod;
00247
00248 return n;
00249 }
00250