Actual source code: run-map.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-map.h"
     10:
     11: #if defined(__cplusplus)
     13: #endif

     15: void* ad_map_init(int dsize, int msize, int bsize, int asize)
     16: {
     17:   int         i;
     18:   MapEntry*        entry;
     19:   char*        pblock;
     20:
     21:   desc_size = dsize;
     22:   entry_size = dsize + sizeof(Pair) - sizeof(double);
     23:   if (!asize) {
     24:     buckets_per_block = DEFAULT_BUCKETS_PER_BLOCK;
     25:   } else {
     26:     buckets_per_block = asize;
     27:   }
     28:
     29:   if (!msize) {
     30:       map_size = DEFAULT_MAP_SIZE;
     31:   } else {
     32:     map_size = msize;
     33:   }
     34:   if (!bsize) {
     35:     entries_per_bucket = DEFAULT_BUCKET_SIZE;
     36:   } else {
     37:     entries_per_bucket = bsize;
     38:   }
     39:   bucket_size = entries_per_bucket*entry_size;
     40:   if (map_def) {
     41:     free(map_def);
     42:   }
     43:   map_def = (MapEntry*)calloc(map_size, sizeof(MapEntry) + bucket_size);
     44:   entry = map_def;
     45:   pblock = (char*)(map_def + map_size);
     46:   for (i = 0; i < map_size; i++) {
     47:     entry->next = (Pair*)pblock;
     48:     pblock += bucket_size;
     49:     entry++;
     50:   }
     51:
     52:   freeList = 0;
     53:   blockList = 0;
     54:   curBlock = 0;
     55:   return(map_def);
     56: }
     57:
     58: void ad_map_cleanup()
     59: {
     60:   if (blockList) {
     61:     genlist_t* block = blockList;
     62:     genlist_t* tmp;
     63:     while ((tmp = block->next)) {
     64:       free(block);
     65:       block = tmp;
     66:     }
     67:     free(block);
     68:     blockList = 0;
     69:   }
     70:
     71:   free(map_def);
     72:   map_def = 0;
     73: }

     75: /*
     76: void* ad_map_reg_array_d(double* base, int size)
     77: {
     78:   assert(!array.base);
     79:   array.base = base;
     80:   array.top = base + size;
     81:   array.desc = calloc(size, desc_size);
     82:   array.isSingle = 0;
     83: }
     84: */

     86: /*
     87: void* ad_map_reg_array_s(float* base, int size)
     88: {
     89:   assert(!array.base);
     90:   array.base = base;
     91:   array.top = base + size/2;
     92:   array.desc = calloc(size, desc_size);
     93:   array.isSingle = 1;
     94: }
     95: */

     98: void* ad_map_get(void* key)
     99: {
    100:   Pair *pa;
    101:   /*
    102:   if (key < array.top && key >= array.base) {
    103:     if (array.isSingle) {
    104:       return array.desc + ((single*)key - (single*)array.base);
    105:     }
    106:     else {
    107:       return array.desc + ((double*)key - array.base);
    108:     }
    109:   }
    110:   */
    111:   MapEntry*        entry = map_def + (((long)key>>3) % map_size);
    112:   if (entry->cache && entry->cache->key == key) {
    113:     return entry->cache->val;
    114:   }
    115:   while (1) {
    116:     int         i = 0;
    117:     pa = entry->next;
    118:     while (++i < entries_per_bucket) {
    119:
    120:       if (pa->key == key) {
    121:         entry->cache = pa;
    122:         return pa->val;
    123:       }
    124:       else if (!pa->key) {
    125:         pa->key = key;
    126:         entry->cache = pa;
    127:         return pa->val;
    128:       }
    129:       else {
    130:         pa = (Pair*)((char*)pa + entry_size);
    131:       }
    132:     }
    133:     if (pa->key) {
    134:       pa = (Pair*)pa->key;
    135:     }
    136:     else {
    137:       Pair* tmp = (Pair*)ad_map_alloc_bucket();
    138:       pa->key = tmp;
    139:       tmp->key = key;
    140:       entry->cache = tmp;
    141:       return tmp->val;
    142:     }
    143:   }
    144: }

    147: static void* ad_map_alloc_bucket(void)
    148: {
    149: #if defined(DEBUG)
    150:   static         count = 0;
    151:   if (++count >= gdebug.nTokens) {
    152:     msg("Allocated %d tokens", count);
    153:     count = 0;
    154:   }
    155: #endif
    156:   static int        nBlocks;
    157:   static int        nCurBucket;
    158:
    159:   if (!curBlock || nCurBucket >= buckets_per_block) {
    160:     if (freeList) {
    161:       curBlock = freeList;
    162:       freeList = freeList->next;
    163:     }
    164:     else {
    165:       curBlock = (genlist_t*)calloc(sizeof(genlist_t) - sizeof(double) +
    166:                                     buckets_per_block * bucket_size, 1);
    167:       curBlock->next = blockList;
    168:       blockList = curBlock;
    169:       nBlocks++;
    170:     }
    171:     nCurBucket = 0;
    172:   }
    173:   return (char*)curBlock->data + (nCurBucket++)*bucket_size;
    174: }

    177: void* ad_map_free_bucket(void* ptr)
    178: {
    179: #if defined(DEBUG)
    180:   static         count = 0;
    181:   if (++count >= gdebug.nTokens) {
    182:     msg("Freed %d tokens", count);
    183:     count = 0;
    184:   }
    185: #endif
    186:
    187:   genlist_t*        list = freeList;
    188:   freeList = (genlist_t*)ptr;
    189:   freeList->next = list;
    190:   return(freeList);
    191: }

    193: void* ad_map_free(void* key)
    194: {
    195:   void** p = (void**)ad_map_get(key);
    196:   *(p-1) = (void*)1;
    197:   return(*p);
    198: }

    201: #if defined(__cplusplus)
    202: }
    203: #endif