| 310 | } |
| 311 | |
| 312 | static bool dynbuf_ensure(DynBuf *b, int needed) { |
| 313 | if (b->len + needed <= b->cap) { |
| 314 | return true; |
| 315 | } |
| 316 | int newcap = b->cap == 0 ? INITIAL_PAGE_CAP : b->cap; |
| 317 | while (newcap < b->len + needed) { |
| 318 | newcap *= GROWTH_FACTOR; |
| 319 | } |
| 320 | uint8_t *p = (uint8_t *)realloc(b->data, newcap); |
| 321 | if (!p) { |
| 322 | (void)fprintf(stderr, "cbm_write_db: dynbuf realloc failed size=%d\n", newcap); |
| 323 | return false; |
| 324 | } |
| 325 | b->data = p; |
| 326 | b->cap = newcap; |
| 327 | return true; |
| 328 | } |
| 329 | |
| 330 | static bool dynbuf_append(DynBuf *b, const void *data, int len) { |
| 331 | if (len <= 0) { |