| 299 | } |
| 300 | |
| 301 | static bool dynbuf_ensure(DynBuf *b, int needed) { |
| 302 | if (b->len + needed <= b->cap) { |
| 303 | return true; |
| 304 | } |
| 305 | int newcap = b->cap == 0 ? INITIAL_PAGE_CAP : b->cap; |
| 306 | while (newcap < b->len + needed) { |
| 307 | newcap *= GROWTH_FACTOR; |
| 308 | } |
| 309 | uint8_t *p = (uint8_t *)realloc(b->data, newcap); |
| 310 | if (!p) { |
| 311 | (void)fprintf(stderr, "cbm_write_db: dynbuf realloc failed size=%d\n", newcap); |
| 312 | return false; |
| 313 | } |
| 314 | b->data = p; |
| 315 | b->cap = newcap; |
| 316 | return true; |
| 317 | } |
| 318 | |
| 319 | static bool dynbuf_append(DynBuf *b, const void *data, int len) { |
| 320 | if (len <= 0) { |