btcs

bitcoin script parser/evaluator/compiler/decompiler
git clone git://jb55.com/btcs
Log | Files | Refs | README | LICENSE

alloc.c (2234B)


      1 
      2 
      3 #include <stdlib.h>
      4 #include <string.h>
      5 #include <assert.h>
      6 #include "alloc.h"
      7 #include "stack.h"
      8 
      9 static struct arenas g_arenas;
     10 
     11 void
     12 alloc_arenas() {
     13   alloc_arena_sizes(0, MAX_STACK_SIZE, MAX_STACK_SIZE * MAX_STACK_SIZE);
     14 }
     15 
     16 void
     17 alloc_arena_sizes(struct arenas *arenas, const int nums, const int bytes) {
     18   if (!arenas) arenas = &g_arenas;
     19   arenas->nums = (struct num*)calloc(nums, sizeof(struct num));
     20   assert(arenas->nums);
     21   arenas->bytes = (u8*)calloc(bytes, 1);
     22   stack_init_size(&arenas->bytes_map, bytes);
     23   arenas->bytes_top = arenas->bytes;
     24   arenas->num_count = 0;
     25   arenas->nbytes = bytes;
     26 }
     27 
     28 struct num *
     29 num_pool_get(const u16 ind) {
     30   return &g_arenas.nums[ind];
     31 }
     32 
     33 
     34 struct num *
     35 num_pool_pop() {
     36   assert(g_arenas.num_count > 0);
     37   return &g_arenas.nums[g_arenas.num_count--];
     38 }
     39 
     40 
     41 struct num *
     42 num_pool_new(u16 *ind) {
     43   *ind = g_arenas.num_count++;
     44   struct num *p;
     45   p = &g_arenas.nums[*ind];
     46   assert(p);
     47   assert(p->val == 0 && p->ind == 0);
     48   p->ind = *ind;
     49   return p;
     50 }
     51 
     52 u8 *
     53 byte_pool_new(u32 len, u16 *ind) {
     54   assert((g_arenas.bytes_top - g_arenas.bytes + len) <= g_arenas.nbytes);
     55   u8 *start = g_arenas.bytes_top;
     56   u32 *c = (u32*)g_arenas.bytes_top;
     57   *c++ = len;
     58   u8 *p = (u8*)c;
     59   p += len;
     60   *ind = stack_size(&g_arenas.bytes_map);
     61   stack_push(&g_arenas.bytes_map, (void*)start);
     62   g_arenas.bytes_top = p;
     63   assert(*p == 0);
     64   assert(((g_arenas.bytes_top - g_arenas.bytes) + len) <= g_arenas.nbytes);
     65   return p - len;
     66 }
     67 
     68 
     69 // useful for quick alloc/deallocs
     70 u8 *
     71 byte_pool_pop() {
     72   u8 *p = (u8*)stack_pop(&g_arenas.bytes_map);
     73   u32 *c = (u32*)p;
     74   memset(p, 0, *c + sizeof(u32));
     75   return p;
     76 }
     77 
     78 
     79 u8 *
     80 byte_pool_get(int ind, u32 *len) {
     81   assert(ind <= stack_size(&g_arenas.bytes_map) - 1);
     82   void **vp;
     83   u8 *p;
     84   u32 *up;
     85   vp = g_arenas.bytes_map.bottom + ind;
     86   p = (u8*)(*vp);
     87   //assert((g_arenas.bytes_top - g_arenas.bytes + *len) <= g_arenas.nbytes);
     88   assert(p);
     89   up = (u32*)p;
     90   *len = *up++;
     91   p = (u8*)up;
     92   assert((g_arenas.bytes_top - g_arenas.bytes + *len) <= g_arenas.nbytes);
     93   return p;
     94 }
     95 
     96 void
     97 free_arenas(struct arenas *arenas) {
     98   if (!arenas) arenas = &g_arenas;
     99   if (!arenas) return;
    100   stack_free(&arenas->bytes_map);
    101   free(arenas->bytes);
    102   free(arenas->nums);
    103 }
    104