| 834 | #endif /* CBM_BIND_TS_ALLOCATOR */ |
| 835 | |
| 836 | void cbm_alloc_init(void) { |
| 837 | #if defined(CBM_BIND_TS_ALLOCATOR) && CBM_BIND_TS_ALLOCATOR |
| 838 | static int alloc_bound = 0; /* single-threaded startup; plain int is fine */ |
| 839 | if (alloc_bound) { |
| 840 | return; |
| 841 | } |
| 842 | alloc_bound = 1; |
| 843 | |
| 844 | /* tree-sitter runtime (was previously bound in cbm_init; consolidated here). */ |
| 845 | ts_set_allocator(cbm_ts_malloc, cbm_ts_calloc, cbm_ts_realloc, cbm_ts_free); |
| 846 | |
| 847 | /* sqlite3. SQLITE_CONFIG_MALLOC MUST run before sqlite3_initialize / the |
| 848 | * first sqlite3_open* — otherwise sqlite3_config returns SQLITE_MISUSE |
| 849 | * silently and the binding is ignored. cbm_alloc_init() runs as the very |
| 850 | * first statement of main(), before cbm_mcp_server_new → cbm_store_open*. */ |
| 851 | static sqlite3_mem_methods cbm_sqlite_mem = { |
| 852 | cbm_sqlite_malloc, /* xMalloc */ |
| 853 | cbm_sqlite_free, /* xFree */ |
| 854 | cbm_sqlite_realloc, /* xRealloc */ |
| 855 | cbm_sqlite_size, /* xSize */ |
| 856 | cbm_sqlite_roundup, /* xRoundup */ |
| 857 | cbm_sqlite_meminit, /* xInit */ |
| 858 | cbm_sqlite_memshutdown, /* xShutdown */ |
| 859 | NULL, /* pAppData */ |
| 860 | }; |
| 861 | int sqlite_rc = sqlite3_config(SQLITE_CONFIG_MALLOC, &cbm_sqlite_mem); |
| 862 | assert(sqlite_rc == SQLITE_OK && "SQLITE_CONFIG_MALLOC must run before sqlite3_initialize"); |
| 863 | (void)sqlite_rc; |
| 864 | #endif /* CBM_BIND_TS_ALLOCATOR */ |
| 865 | } |
| 866 | |
| 867 | // --- Init/Shutdown --- |
| 868 | |