メインページ | アルファベット順一覧 | 構成 | ファイル一覧 | 構成メンバ | ファイルメンバ | 関連ページ

# dkcMemoryPool.c

説明を見る。

    00001 #include "dkcMemoryPool.h"
    00002
    00003
    00004
    00005
    00007 static DKC_INLINE DKC_SAME_OBJECT_POOL *alloc_sameobjectpool(){
    00008     return dkcAllocate(sizeof(DKC_SAME_OBJECT_POOL));
    00009
    00010 }
    00011
    00013 static DKC_INLINE int free_sameobjectpool(DKC_SAME_OBJECT_POOL *p){
    00014     return dkcFree(&p);
    00015 }
    00016
    00017 static DKC_INLINE DKC_SAME_OBJECT_POOL *alloc_sameobjectpool_outer(DKC_ALLOC_FUNC_TYPE func){
    00018     /*if(NULL==func){
    00019         return alloc_sameobjectpool();
    00020     }*/
    00021     return func(sizeof(DKC_SAME_OBJECT_POOL));
    00022 }
    00023 static DKC_INLINE int free_sameobjectpool_outer(void *p,DKC_FREE_FUNC_TYPE func){
    00024     if(!p){
    00025         return edk_FAILED;
    00026     }
    00027     func(p);
    00028     return edk_SUCCEEDED;
    00029 }
    00030
    00031
    00032
    00033 DKC_INLINE int WINAPI dkcSameObjectPoolInit(
    00034     DKC_SAME_OBJECT_POOL *p,size_t object_size,size_t max_num,
    00035     DKC_ALLOC_FUNC_TYPE alloc_f,DKC_FREE_FUNC_TYPE free_f)
    00036 {
    00037     BOOL af,ff;
    00038     if(NULL==p){
    00039         return edk_FAILED;
    00040     }
    00041     af = (NULL==alloc_f);
    00042     ff = (NULL==free_f);
    00043     if(af && ff){//二つともNULLだったらでふぉるとアロケータを入れる。
    00044         alloc_f = dkcMallocAdapter;
    00045         free_f = dkcFreeAdapter;
    00046         goto PROC;
    00047     }
    00048     if(NULL==alloc_f || NULL==free_f){
    00049         return edk_ArgumentException;
    00050     }
    00051 PROC:
    00052
    00053     p->alloc_f = alloc_f;
    00054     p->free_f = free_f;
    00055
    00056     p->max_num = max_num;
    00057     p->now_num = 0;
    00058     p->obj_size = object_size;
    00059     return edk_SUCCEEDED;
    00060 }
    00061
    00063 DKC_INLINE void WINAPI dkcSameObjectPoolUninit(DKC_SAME_OBJECT_POOL *p)
    00064 {
    00065     DKC_SAME_OBJECT_POOL_NODE *t;
    00066     for(;p->root != NULL;){
    00067         t = p->root->next;
    00068         p->free_f(p->root);
    00069         p->root = t;
    00070     }
    00071 }
    00072
    00073 DKC_SAME_OBJECT_POOL *WINAPI
    00074     dkcAllocSameObjectPool(size_t object_size,size_t max_num,
    00075                                                 DKC_ALLOC_FUNC_TYPE alloc_f,DKC_FREE_FUNC_TYPE free_f)
    00076 {
    00077     DKC_SAME_OBJECT_POOL *p = alloc_sameobjectpool();
    00078     if(NULL==p) return NULL;
    00079
    00080     if(DKUTIL_FAILED(dkcSameObjectPoolInit(p,object_size,max_num,alloc_f,free_f)))
    00081     {
    00082         goto Error;
    00083     }
    00084     if(FALSE==dkcSameObjectPoolReserve(p)){
    00085         goto Error;
    00086     }
    00087     return p;
    00088 Error:
    00089     free_sameobjectpool(p);
    00090     return NULL;
    00091 }
    00092
    00093
    00094 DKC_INLINE DKC_SAME_OBJECT_POOL *WINAPI dkcAllocSameObjectPoolDynamic(size_t object_size){
    00095     return dkcAllocSameObjectPool(object_size,256,NULL,NULL);
    00096 }
    00097
    00098
    00099
    00100
    00101 int WINAPI dkcFreeSameObjectPool(DKC_SAME_OBJECT_POOL **pp){
    00102     DKC_SAME_OBJECT_POOL *p = (*pp);
    00103     if(NULL==pp || NULL==p){
    00104         return edk_FAILED;
    00105     }
    00106     //ノードを開放
    00107     dkcSameObjectPoolUninit(p);
    00108
    00109
    00110     //最終領域を開放
    00111     return free_sameobjectpool(p);
    00112 }
    00113
    00114
    00115 DKC_INLINE BOOL WINAPI dkcSameObjectPoolReserveFast(DKC_SAME_OBJECT_POOL *p)
    00116 {
    00117     size_t max = p->max_num,now = p->now_num ;
    00118     size_t size = (p->obj_size > sizeof(DKC_SAME_OBJECT_POOL_NODE *))
    00119         ? p->obj_size  : sizeof(DKC_SAME_OBJECT_POOL_NODE *);
    00120     DKC_SAME_OBJECT_POOL_NODE* runner;
    00121     size_t i;
    00122
    00123     if(max <= now){
    00124         return FALSE;
    00125     }
    00126
    00127     if(!p->root){
    00128         runner = dkcAllocateFast(size);
    00129         //runner = p->alloc_f(size);
    00130         runner->next = NULL;
    00131         p->root = runner;
    00132         dkcmNOT_ASSERT(0!=now);
    00133         now++;
    00134         for (i = now;i<max ; i++) {
    00135             runner->next = dkcAllocateFast(size);
    00136             //runner->next = p->alloc_f(size);
    00137             runner = runner->next;
    00138             now++;
    00139         }
    00140
    00141     }else{
    00142
    00143         dkcmNOT_ASSERT(max < now);
    00144         runner = p->tail;
    00145         max -= now;
    00146         for(i = 0;i<max;i++){
    00147             runner->next = dkcAllocateFast(size);
    00148             //runner->next = p->alloc_f(size);
    00149             runner = runner->next;
    00150             now++;
    00151         }
    00152
    00153     }
    00154     runner->next = NULL;
    00155     p->tail = runner;
    00156     p->now_num = now;
    00157     return TRUE;
    00158 }
    00159
    00160
    00162 DKC_INLINE void *dkcSameObjectPoolAlloc(DKC_SAME_OBJECT_POOL *p){
    00163     DKC_SAME_OBJECT_POOL_NODE *n = p->root;
    00164     int tr;
    00165
    00166     if(0==p->now_num){
    00167         dkcmNOT_ASSERT(p->root != NULL);
    00168         if(p->alloc_f == dkcMallocAdapter){
    00169             tr = dkcSameObjectPoolReserveFast(p);
    00170         }else{
    00171             tr = dkcSameObjectPoolReserve(p);
    00172         }
    00173         if(FALSE==tr){
    00174             return NULL;
    00175         }
    00176         return dkcSameObjectPoolAlloc(p);
    00177     }
    00178
    00179
    00180     p->root = n->next;
    00181
    00182     //マイナス
    00183     p->now_num--;
    00184
    00185     return n;
    00186 }
    00187
    00188 /*
    00189 void free(void * const chunk)
    00190 {
    00191     nextof(chunk) = first;
    00192
    00193   first = chunk;
    00194 }
    00195 // for the sake of code readability :)
    00196 static void * & nextof(void * const ptr)
    00197 {
    00198     return *(static_cast<void **>(ptr));
    00199 }
    00200 */
    00201 // for the sake of code readability :)
    00202
    00203 DKC_INLINE void WINAPI dkcSameObjectPoolRecycle(DKC_SAME_OBJECT_POOL *p,void *pv){
    00204     /*
    00205     DKC_SAME_OBJECT_POOL_NODE* head = pv;
    00206     head->next = p->root;
    00207     p->root = head;
    00208     p->now_num ++;
    00209     */
    00210     *((DKC_SAME_OBJECT_POOL_NODE **)pv) = (DKC_SAME_OBJECT_POOL_NODE *)p->root;
    00211     p->root = (DKC_SAME_OBJECT_POOL_NODE *)pv;
    00212     p->now_num++;
    00213
    00214 }
    00215
    00216
    00217 DKC_INLINE BOOL WINAPI dkcSameObjectPoolReserve(DKC_SAME_OBJECT_POOL *p){
    00218     size_t max = p->max_num,now = p->now_num ;
    00219     size_t size = (p->obj_size > sizeof(DKC_SAME_OBJECT_POOL_NODE *))
    00220         ? p->obj_size  : sizeof(DKC_SAME_OBJECT_POOL_NODE *);
    00221     DKC_SAME_OBJECT_POOL_NODE* runner;
    00222     size_t i;
    00223
    00224     if(max <= now){
    00225         return FALSE;
    00226     }
    00227
    00228     if(!p->root){
    00229         //runner = dkcAllocateFast(size);
    00230         runner = p->alloc_f(size);
    00231         runner->next = NULL;
    00232         p->root = runner;
    00233         dkcmNOT_ASSERT(0!=now);
    00234         now++;
    00235         for (i = now;i<max ; i++) {
    00236             //runner->next = dkcAllocateFast(size);
    00237             runner->next = p->alloc_f(size);
    00238             runner = runner->next;
    00239             now++;
    00240         }
    00241
    00242     }else{
    00243
    00244         dkcmNOT_ASSERT(max < now);
    00245         runner = p->tail;
    00246         max -= now;
    00247         for(i = 0;i<max;i++){
    00248             //runner->next = dkcAllocateFast(size);
    00249             runner->next = p->alloc_f(size);
    00250             runner = runner->next;
    00251             now++;
    00252         }
    00253
    00254     }
    00255     runner->next = NULL;
    00256     p->tail = runner;
    00257     p->now_num = now;
    00258     return TRUE;
    00259 }
    00260
    00261
    00262 #if 0
    00263
    00264 #endif
    00265
    00266 #if 0
    00267 DKC_MEMORY_POOL *WINAPI dkcAllocMemoryPool(size_t poolsize,UINT flag){
    00268     DKC_MEMORY_POOL *p = dkcAllocate(sizeof(DKC_MEMORY_POOL));
    00269     void *a = NULL;
    00270
    00271     if(NULL==p){
    00272         return NULL;
    00273     }
    00274     a = dkcAllocate(size);
    00275     if(NULL==a){
    00276         goto Error;
    00277     }
    00278
    00279     p->mpool = a;
    00280
    00281
    00282
    00283     p->mAobo = dkcAllocArrayOneByOneDynamic(sizeof(void *),256);
    00284     if(NULL==p->mAobo){
    00285         goto Error;
    00286     }
    00287     return p;
    00288
    00289 Error:
    00290     dkcFree(&a);
    00291     dkcFree(&p);
    00292
    00293     return NULL;
    00294
    00295 }
    00296
    00297 int WINAPI dkcFreeMemoryPool(DKC_MEMORY_POOL **p){
    00298     if(NULL==p)
    00299         return edk_FAILED;
    00300
    00301     dkcFreeArrayOneByOne(&((*p)->mAobo));
    00302     dkcFree(&((*p)->mpool));
    00303     return dkcFree(p);
    00304 }
    00305
    00306
    00307 void *WINAPI dkcMemoryPoolAlloc(size_t size){
    00308
    00309
    00310
    00311 }
    00312
    00313 int WINAPI dkcMemoryPoolFree(void *p){
    00314
    00315 }
    00316
    00317 int WINAPI dkcMemoryPoolSafeFree(void **pp){
    00318
    00319 }
    00320 #endif