| 1 |
#include "bkblock.h" |
| 2 |
|
| 3 |
static void bkblock_acells(bk_Block *b, uint32_t len) { |
| 4 |
if (len <= b->length + b->free) { |
| 5 |
// We have enough space |
| 6 |
b->free -= len - b->length; |
| 7 |
b->length = len; |
| 8 |
} else { |
| 9 |
// allocate space |
| 10 |
b->length = len; |
| 11 |
b->free = (len >> 1) & 0xFFFFFF; |
| 12 |
RESIZE(b->cells, b->length + b->free); |
| 13 |
} |
| 14 |
} |
| 15 |
bool bk_cellIsPointer(bk_Cell *cell) { |
| 16 |
return cell->t >= p16; |
| 17 |
} |
| 18 |
|
| 19 |
static bk_Cell *bkblock_grow(bk_Block *b, uint32_t len) { |
| 20 |
uint32_t olen = b->length; |
| 21 |
bkblock_acells(b, olen + len); |
| 22 |
return &(b->cells[olen]); |
| 23 |
} |
| 24 |
|
| 25 |
bk_Block *_bkblock_init() { |
| 26 |
bk_Block *b; |
| 27 |
NEW(b); |
| 28 |
bkblock_acells(b, 0); |
| 29 |
return b; |
| 30 |
} |
| 31 |
|
| 32 |
static void bkblock_pushint(bk_Block *b, bk_CellType type, uint32_t x) { |
| 33 |
bk_Cell *cell = bkblock_grow(b, 1); |
| 34 |
cell->t = type; |
| 35 |
cell->z = x; |
| 36 |
} |
| 37 |
static void bkblock_pushptr(bk_Block *b, bk_CellType type, bk_Block *p) { |
| 38 |
bk_Cell *cell = bkblock_grow(b, 1); |
| 39 |
cell->t = type; |
| 40 |
cell->p = p; |
| 41 |
} |
| 42 |
|
| 43 |
static void vbkpushitems(bk_Block *b, bk_CellType type0, va_list ap) { |
| 44 |
bk_CellType curtype = type0; |
| 45 |
while (curtype) { |
| 46 |
if (curtype == bkcopy || curtype == bkembed) { |
| 47 |
bk_Block *par = va_arg(ap, bk_Block *); |
| 48 |
if (par && par->cells) { |
| 49 |
for (uint32_t j = 0; j < par->length; j++) { |
| 50 |
if (bk_cellIsPointer(par->cells + j)) { |
| 51 |
bkblock_pushptr(b, par->cells[j].t, par->cells[j].p); |
| 52 |
} else { |
| 53 |
bkblock_pushint(b, par->cells[j].t, par->cells[j].z); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
if (curtype == bkembed && par) { |
| 58 |
FREE(par->cells); |
| 59 |
FREE(par); |
| 60 |
} |
| 61 |
} else if (curtype < p16) { |
| 62 |
uint32_t par = va_arg(ap, int); |
| 63 |
bkblock_pushint(b, curtype, par); |
| 64 |
} else { |
| 65 |
bk_Block *par = va_arg(ap, bk_Block *); |
| 66 |
bkblock_pushptr(b, curtype, par); |
| 67 |
} |
| 68 |
curtype = va_arg(ap, int); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
bk_Block *bk_new_Block(int type0, ...) { |
| 73 |
va_list ap; |
| 74 |
va_start(ap, type0); |
| 75 |
bk_Block *b = _bkblock_init(); |
| 76 |
vbkpushitems(b, type0, ap); |
| 77 |
va_end(ap); |
| 78 |
return b; |
| 79 |
} |
| 80 |
|
| 81 |
bk_Block *bk_push(bk_Block *b, int type0, ...) { |
| 82 |
va_list ap; |
| 83 |
va_start(ap, type0); |
| 84 |
vbkpushitems(b, type0, ap); |
| 85 |
va_end(ap); |
| 86 |
return b; |
| 87 |
} |
| 88 |
|
| 89 |
bk_Block *bk_newBlockFromStringLen(size_t len, const char *str) { |
| 90 |
if (!str) return NULL; |
| 91 |
bk_Block *b = bk_new_Block(bkover); |
| 92 |
for (size_t j = 0; j < len; j++) { |
| 93 |
bkblock_pushint(b, b8, str[j]); |
| 94 |
} |
| 95 |
return b; |
| 96 |
} |
| 97 |
|
| 98 |
bk_Block *bk_newBlockFromBuffer(MOVE caryll_Buffer *buf) { |
| 99 |
if (!buf) return NULL; |
| 100 |
bk_Block *b = bk_new_Block(bkover); |
| 101 |
for (size_t j = 0; j < buf->size; j++) { |
| 102 |
bkblock_pushint(b, b8, buf->data[j]); |
| 103 |
} |
| 104 |
buffree(buf); |
| 105 |
return b; |
| 106 |
} |
| 107 |
bk_Block *bk_newBlockFromBufferCopy(OBSERVE caryll_Buffer *buf) { |
| 108 |
if (!buf) return NULL; |
| 109 |
bk_Block *b = bk_new_Block(bkover); |
| 110 |
for (size_t j = 0; j < buf->size; j++) { |
| 111 |
bkblock_pushint(b, b8, buf->data[j]); |
| 112 |
} |
| 113 |
return b; |
| 114 |
} |
| 115 |
|
| 116 |
void bk_printBlock(bk_Block *b) { |
| 117 |
fprintf(stderr, "Block size %08x\n", (uint32_t)b->length); |
| 118 |
fprintf(stderr, "------------------\n"); |
| 119 |
for (uint32_t j = 0; j < b->length; j++) { |
| 120 |
if (bk_cellIsPointer(b->cells + j)) { |
| 121 |
if (b->cells[j].p) { |
| 122 |
fprintf(stderr, " %3d %p[%d]\n", b->cells[j].t, b->cells[j].p, |
| 123 |
b->cells[j].p->_index); |
| 124 |
} else { |
| 125 |
fprintf(stderr, " %3d [NULL]\n", b->cells[j].t); |
| 126 |
} |
| 127 |
} else { |
| 128 |
fprintf(stderr, " %3d %d\n", b->cells[j].t, b->cells[j].z); |
| 129 |
} |
| 130 |
} |
| 131 |
fprintf(stderr, "------------------\n"); |
| 132 |
} |