klib
[Ported from github.com/attractivechaos/klib] added block arena
Brought by drakestapleton
klib / kbarena.c
| 1 | #include <stdint.h> |
| 2 | #include <stdlib.h> |
| 3 | #include <string.h> |
| 4 | #include "kbarena.h" |
| 5 | |
| 6 | #define kom_malloc(type, cnt) ((type*)malloc((cnt) * sizeof(type))) |
| 7 | #define kom_calloc(type, cnt) ((type*)calloc((cnt), sizeof(type))) |
| 8 | #define kom_realloc(type, ptr, cnt) ((type*)realloc((ptr), (cnt) * sizeof(type))) |
| 9 | |
| 10 | #define kom_grow(type, ptr, __i, __m, __succ) do { /* make enough room for ptr[__i] */ \ |
| 11 | *(__succ) = 1; \ |
| 12 | if ((__i) >= (__m)) { \ |
| 13 | size_t new_m = (__i) + 16ULL + (((__i) + 1ULL) >> 1); \ |
| 14 | type *new_p; \ |
| 15 | new_p = kom_realloc(type, (ptr), new_m); \ |
| 16 | if (new_p) (__m) = new_m, (ptr) = new_p; \ |
| 17 | else *(__succ) = 0; \ |
| 18 | } \ |
| 19 | } while (0) |
| 20 | |
| 21 | typedef struct { |
| 22 | uint32_t blen, max_blk; // block length and max number of blocks |
| 23 | uint32_t bi, bo; // current block index and block offset |
| 24 | uint8_t **b; // blocks |
| 25 | uint32_t n_stack, m_stack; // stack size and capacity |
| 26 | uint64_t *stack; // saved states (two uint32_t packed together) |
| 27 | } kbarena_t; |
| 28 | |
| 29 | void *kba_init(uint32_t blen) |
| 30 | { |
| 31 | kbarena_t *ba; |
| 32 | if (blen == 0) return 0; |
| 33 | ba = kom_calloc(kbarena_t, 1); |
| 34 | if (ba == 0) return 0; |
| 35 | ba->blen = (blen + 7) / 8 * 8; // round up to 8 bytes |
| 36 | ba->max_blk = 4; |
| 37 | ba->b = kom_calloc(uint8_t*, ba->max_blk); |
| 38 | ba->b[0] = kom_malloc(uint8_t, ba->blen); // TODO: use aligned alloc |
| 39 | return ba; |
| 40 | } |
| 41 | |
| 42 | void kba_destroy(void *ba_) |
| 43 | { |
| 44 | kbarena_t *ba = (kbarena_t*)ba_; |
| 45 | uint32_t i; |
| 46 | if (ba == 0) return; |
| 47 | for (i = 0; i < ba->max_blk; ++i) |
| 48 | if (ba->b[i]) free(ba->b[i]); |
| 49 | free(ba->b); free(ba->stack); free(ba); |
| 50 | } |
| 51 | |
| 52 | size_t kba_capacity(const void *ba_) |
| 53 | { |
| 54 | const kbarena_t *ba = (const kbarena_t*)ba_; |
| 55 | uint32_t i; |
| 56 | size_t cap = 0; |
| 57 | for (i = 0; i < ba->max_blk; ++i) |
| 58 | if (ba->b[i]) cap += ba->blen; |
| 59 | return cap; |
| 60 | } |
| 61 | |
| 62 | void *kba_alloc(void *ba_, uint32_t sz, uint32_t aln) |
| 63 | { |
| 64 | kbarena_t *ba = (kbarena_t*)ba_; |
| 65 | uint32_t pad = -(int64_t)ba->bo & (aln - 1); |
| 66 | void *p; |
| 67 | if (sz == 0 || sz > ba->blen || (aln & (aln - 1)) != 0) return 0; |
| 68 | if ((uint64_t)ba->bo + sz + pad > ba->blen) { // no room in the current block |
| 69 | uint32_t old_max = ba->max_blk; |
| 70 | int succ; |
| 71 | kom_grow(uint8_t*, ba->b, ba->bi + 1, ba->max_blk, &succ); |
| 72 | if (!succ) return 0; |
| 73 | if (ba->max_blk > old_max) |
| 74 | memset(&ba->b[old_max], 0, (ba->max_blk - old_max) * sizeof(void*)); |
| 75 | if (ba->b[ba->bi + 1] == 0) { |
| 76 | ba->b[ba->bi + 1] = kom_malloc(uint8_t, ba->blen); // TODO: use aligned malloc |
| 77 | if (ba->b[ba->bi + 1] == 0) return 0; |
| 78 | } |
| 79 | ++ba->bi, ba->bo = 0, pad = 0; |
| 80 | } |
| 81 | p = (void*)&ba->b[ba->bi][ba->bo + pad]; |
| 82 | ba->bo += sz + pad; |
| 83 | return p; |
| 84 | } |
| 85 | |
| 86 | int kba_save(void *ba_) |
| 87 | { |
| 88 | kbarena_t *ba = (kbarena_t*)ba_; |
| 89 | int succ; |
| 90 | kom_grow(uint64_t, ba->stack, ba->n_stack, ba->m_stack, &succ); |
| 91 | if (!succ) return -1; |
| 92 | ba->stack[ba->n_stack++] = (uint64_t)ba->bi << 32 | ba->bo; |
| 93 | return 0; |
| 94 | } |
| 95 | |
| 96 | int kba_restore(void *ba_) |
| 97 | { |
| 98 | uint64_t x; |
| 99 | kbarena_t *ba = (kbarena_t*)ba_; |
| 100 | if (ba->n_stack == 0) return -1; |
| 101 | x = ba->stack[--ba->n_stack]; |
| 102 | ba->bi = x>>32, ba->bo = (uint32_t)x; |
| 103 | return 0; |
| 104 | } |