| 46 | } |
| 47 | |
| 48 | void *tpt_alloc(tpt_context *c, size_t count, size_t size) { |
| 49 | cbm_pinned_tree_t *t = c->tree; |
| 50 | if (!tpt_poll(c)) |
| 51 | return NULL; |
| 52 | if (!count || !size) |
| 53 | return NULL; |
| 54 | if (count > SIZE_MAX / size) { |
| 55 | tpt_fail(c, CBM_PINNED_TREE_LIMIT, "allocation multiplication"); |
| 56 | return NULL; |
| 57 | } |
| 58 | size_t n = count * size; |
| 59 | if (n > t->limits.max_arena_bytes - t->allocated || n > SIZE_MAX - 7 || |
| 60 | t->arena.used > SIZE_MAX - ((n + 7) & ~(size_t)7) || |
| 61 | t->arena.total_alloc > SIZE_MAX - ((n + 7) & ~(size_t)7)) { |
| 62 | tpt_fail(c, CBM_PINNED_TREE_LIMIT, "materializer arena limit"); |
| 63 | return NULL; |
| 64 | } |
| 65 | size_t aligned = (n + 7) & ~(size_t)7; |
| 66 | if (!t->arena.nblocks || aligned > t->arena.block_size - t->arena.used) { |
| 67 | size_t block = aligned > t->arena.grow_size ? aligned : t->arena.grow_size; |
| 68 | if (block > SIZE_MAX / 2) { |
| 69 | tpt_fail(c, CBM_PINNED_TREE_LIMIT, "arena growth arithmetic bound"); |
| 70 | return NULL; |
| 71 | } |
| 72 | } |
| 73 | t->allocated += n; |
| 74 | void *memory = cbm_arena_alloc(&t->arena, n); |
| 75 | if (!memory) { |
| 76 | tpt_fail(c, CBM_PINNED_TREE_OOM, "materializer allocation"); |
| 77 | return NULL; |
| 78 | } |
| 79 | unsigned char *p = memory; |
| 80 | while (n) { |
| 81 | size_t chunk = n > TPT_CHUNK ? TPT_CHUNK : n; |
| 82 | if (!tpt_poll(c)) |
| 83 | return NULL; |
| 84 | memset(p, 0, chunk); |
| 85 | p += chunk; |
| 86 | n -= chunk; |
| 87 | } |
| 88 | return memory; |
| 89 | } |
| 90 | |
| 91 | bool tpt_copy(tpt_context *c, void *target, const void *source, size_t n) { |
| 92 | unsigned char *to = target; |
no test coverage detected