| 89 | } |
| 90 | |
| 91 | void *cif_alloc(cif_context *c, size_t count, size_t width) { |
| 92 | if (!count || !width) { |
| 93 | return NULL; |
| 94 | } |
| 95 | if (count > SIZE_MAX / width) { |
| 96 | cif_fail(c, CBM_INVENTORY_LIMIT); |
| 97 | return NULL; |
| 98 | } |
| 99 | size_t n = count * width; |
| 100 | cbm_inventory_filter_t *o = c->owner; |
| 101 | size_t available = o->limits.max_arena_bytes - o->limits.max_ignore_arena_bytes; |
| 102 | if (n > available - o->usage.non_ignore_arena_requested_bytes) { |
| 103 | cif_fail(c, CBM_INVENTORY_LIMIT); |
| 104 | return NULL; |
| 105 | } |
| 106 | if (!cif_poll(c) || !cif_arena_room(c, n)) { |
| 107 | return NULL; |
| 108 | } |
| 109 | o->usage.non_ignore_arena_requested_bytes += n; |
| 110 | void *result = cbm_arena_alloc(&o->arena, n); |
| 111 | if (!result) { |
| 112 | cif_fail(c, CBM_INVENTORY_OOM); |
| 113 | return NULL; |
| 114 | } |
| 115 | if (!cif_poll(c) || !cif_copy(c, result, NULL, n)) { |
| 116 | return NULL; |
| 117 | } |
| 118 | return result; |
| 119 | } |
| 120 | |
| 121 | unsigned char *cif_string(cif_context *c, const unsigned char *source, size_t length) { |
| 122 | if (length == SIZE_MAX) { |
no test coverage detected