Actual source code: run-alloc.c

      1: #include "petscconf.h"
      2: #if defined(PETSC_HAVE_STDLIB_H)
      3: #include <stdlib.h>
      4: #endif
      5: #if defined(PETSC_HAVE_MALLOC_H)
      6: #include <malloc.h>
      7: #endif

      9: #include "adic/run-alloc.h"

     11: #if defined(__cplusplus)
     13: #endif
     14:
     15: #define DEFAULT_BUCKET_SIZE                1000
     16: #define DEFAULT_DERIV_SIZE                sizeof(void*)
     17:
     18: static int bucket_size = 0;
     19: static int deriv_size = 0;

     21: typedef struct genlist {
     22:   struct genlist *next;
     23:   struct genlist *prev;
     24:   char data[1];
     25: } genlist_t;
     26: static genlist_t* freeList = 0;
     27: static genlist_t* bucketList = 0;
     28: static genlist_t* curBucket = 0;
     29: static int nCount = 0;

     33: void* ad_adic_deriv_init(int dsize, int bsize)
     34: {
     35:   if (!dsize) {
     36:     deriv_size = DEFAULT_DERIV_SIZE;
     37:   } else {
     38:     deriv_size = dsize;
     39:   }
     40:   if (!bsize) {
     41:     bucket_size = DEFAULT_BUCKET_SIZE;
     42:   } else {
     43:     bucket_size = bsize;
     44:   }
     45:
     46:   curBucket = (genlist_t*)malloc(deriv_size * bucket_size);
     47:   curBucket->next = 0;
     48:   curBucket->prev = 0;
     49:
     50:   freeList = 0;
     51:   bucketList = curBucket;
     52:   nCount = 0;
     53:   return(bucketList);
     54: }

     56: void ad_adic_deriv_final(void)
     57: {
     58:   if (bucketList) {
     59:     genlist_t* block = bucketList;
     60:     genlist_t* tmp;
     61:     while ((tmp = block->next)) {
     62:       free(block);
     63:       block = tmp;
     64:     }
     65:     free(block);
     66:     bucketList = 0;
     67:   }
     68: }

     70: void* ad_adic_deriv_alloc(void)
     71: {
     72:
     73: #if defined(DEBUG)
     74:   static         count = 0;
     75:   if (++count >= gdebug.nTokens) {
     76:     msg("Allocated %d-th deriv obj", count);
     77:     count = 0;
     78:   }
     79: #endif
     80:
     81:   if (freeList) {
     82:     void* pobj = freeList;
     83:     freeList = freeList->next;
     84:     return pobj;
     85:   }
     86:   else if (nCount >= bucket_size) {
     87:     curBucket = (genlist_t*)malloc(deriv_size * bucket_size);
     88:     curBucket->next = bucketList;
     89:     bucketList->prev = curBucket;
     90:     bucketList = curBucket;
     91:     nCount = 1;
     92:     return curBucket->data;
     93:   }
     94:   else {
     95:     return curBucket->data + (deriv_size * nCount++);
     96:   }
     97: }

     99: void ad_adic_deriv_free(void* ptr)
    100: {
    101: #if defined(DEBUG)
    102:   static         count = 0;
    103:   if (++count >= gdebug.nTokens) {
    104:     msg("Freed %d tokens", count);
    105:     count = 0;
    106:   }
    107: #endif
    108:
    109:   genlist_t*        list = freeList;
    110:   freeList = (genlist_t*)ptr;
    111:   freeList->next = list;
    112: }

    114: #if defined(__cplusplus)
    115: }
    116: #endif