@@ -3733,7 +3733,7 @@ static void cypher_stable_sort(int *idx, int n, cypher_index_cmp_fn cmp, const v
37333733 if (n < PAIR_LEN ) {
37343734 return ;
37353735 }
3736- int * tmp = malloc ( (size_t )n * sizeof (int ));
3736+ int * tmp = cbm_alloc ( CBM_MEM_CLASS_OTHER , (size_t )n * sizeof (int ));
37373737 if (!tmp ) {
37383738 for (int i = SKIP_ONE ; i < n ; i ++ ) {
37393739 for (int j = i ; j > 0 && cmp (ctx , idx [j - SKIP_ONE ], idx [j ]) > 0 ; j -- ) {
@@ -3763,7 +3763,7 @@ static void cypher_stable_sort(int *idx, int n, cypher_index_cmp_fn cmp, const v
37633763 }
37643764 memcpy (idx , tmp , (size_t )n * sizeof (int ));
37653765 }
3766- free ( tmp );
3766+ cbm_free ( CBM_MEM_CLASS_OTHER , tmp );
37673767}
37683768
37693769/* String key → the index it was first recorded with. Keys are copied: the
@@ -3797,13 +3797,13 @@ static void key_index_add(cypher_key_index_t *ix, const char *key, int index) {
37973797 }
37983798 if (ix -> table && ix -> count >= ix -> cap ) {
37993799 int cap = ix -> cap ? ix -> cap * PAIR_LEN : CBM_SZ_256 ;
3800- char * * keys = realloc ( ix -> keys , (size_t )cap * sizeof (char * ));
3800+ char * * keys = cbm_realloc ( CBM_MEM_CLASS_OTHER , ix -> keys , (size_t )cap * sizeof (char * ));
38013801 if (keys ) {
38023802 ix -> keys = keys ;
38033803 ix -> cap = cap ;
38043804 }
38053805 }
3806- char * copy = ix -> table && ix -> count < ix -> cap ? heap_strdup ( key ) : NULL ;
3806+ char * copy = ix -> table && ix -> count < ix -> cap ? cbm_mem_strdup ( CBM_MEM_CLASS_OTHER , key ) : NULL ;
38073807 if (!copy ) {
38083808 ix -> failed = true;
38093809 return ;
@@ -3817,9 +3817,9 @@ static void key_index_free(cypher_key_index_t *ix) {
38173817 cbm_ht_free (ix -> table );
38183818 }
38193819 for (int i = 0 ; i < ix -> count ; i ++ ) {
3820- free ( ix -> keys [i ]);
3820+ cbm_free ( CBM_MEM_CLASS_OTHER , ix -> keys [i ]);
38213821 }
3822- free ( ix -> keys );
3822+ cbm_free ( CBM_MEM_CLASS_OTHER , ix -> keys );
38233823 memset (ix , 0 , sizeof (* ix ));
38243824}
38253825
@@ -3925,12 +3925,12 @@ static void rb_apply_order_by(result_builder_t *rb, const cbm_return_clause_t *r
39253925 }
39263926 int n = rb -> row_count ;
39273927 rb_order_ctx_t ctx = {.cols = cols , .numeric = numeric , .descs = descs , .keys = keys };
3928- int * idx = malloc ( (size_t )n * sizeof (int ));
3929- const char * * * rows = malloc ( (size_t )n * sizeof (const char * * ));
3928+ int * idx = cbm_alloc ( CBM_MEM_CLASS_OTHER , (size_t )n * sizeof (int ));
3929+ const char * * * rows = cbm_alloc ( CBM_MEM_CLASS_OTHER , (size_t )n * sizeof (const char * * ));
39303930 if (!idx || !rows ) {
39313931 /* No scratch memory: insertion sort in place, same comparisons. */
3932- free ( idx );
3933- free ( rows );
3932+ cbm_free ( CBM_MEM_CLASS_OTHER , idx );
3933+ cbm_free ( CBM_MEM_CLASS_OTHER , rows );
39343934 for (int i = SKIP_ONE ; i < n ; i ++ ) {
39353935 for (int j = i ; j > 0 && rb_order_compare_rows (& ctx , rb -> rows [j - SKIP_ONE ],
39363936 rb -> rows [j ], NULL , NULL ) > 0 ;
@@ -3942,7 +3942,7 @@ static void rb_apply_order_by(result_builder_t *rb, const cbm_return_clause_t *r
39423942 }
39433943 return ;
39443944 }
3945- int * values = malloc ( (size_t )n * (size_t )keys * sizeof (int ));
3945+ int * values = cbm_alloc ( CBM_MEM_CLASS_OTHER , (size_t )n * (size_t )keys * sizeof (int ));
39463946 for (int r = 0 ; r < n ; r ++ ) {
39473947 idx [r ] = r ;
39483948 rows [r ] = rb -> rows [r ];
@@ -3957,9 +3957,9 @@ static void rb_apply_order_by(result_builder_t *rb, const cbm_return_clause_t *r
39573957 for (int r = 0 ; r < n ; r ++ ) {
39583958 rb -> rows [r ] = rows [idx [r ]];
39593959 }
3960- free ( values );
3961- free ( rows );
3962- free ( idx );
3960+ cbm_free ( CBM_MEM_CLASS_OTHER , values );
3961+ cbm_free ( CBM_MEM_CLASS_OTHER , rows );
3962+ cbm_free ( CBM_MEM_CLASS_OTHER , idx );
39633963}
39643964
39653965static void rb_apply_skip_limit (result_builder_t * rb , int skip_n , int limit ,
@@ -4008,7 +4008,7 @@ static const char *rb_row_key(const result_builder_t *rb, const char **row, char
40084008 need += strlen (row [c ]) + CBM_SZ_32 ;
40094009 }
40104010 if (need > * cap ) {
4011- char * grown = realloc ( * buf , need );
4011+ char * grown = cbm_realloc ( CBM_MEM_CLASS_OTHER , * buf , need );
40124012 if (!grown ) {
40134013 return NULL ;
40144014 }
@@ -4075,7 +4075,7 @@ static void rb_apply_distinct(result_builder_t *rb) {
40754075 }
40764076 rb -> row_count = kept ;
40774077 key_index_free (& seen );
4078- free ( key_buf );
4078+ cbm_free ( CBM_MEM_CLASS_OTHER , key_buf );
40794079}
40804080
40814081static void rb_free (result_builder_t * rb ) {
@@ -4457,17 +4457,18 @@ static void sort_bindings(binding_t *vbindings, int count, const cbm_return_clau
44574457 size_t keys = (size_t )wc -> order_key_count ;
44584458 size_t cells = (size_t )count * keys ;
44594459 with_order_ctx_t ctx = {.wc = wc ,
4460- .values = calloc ( cells , sizeof (char * )),
4461- .numbered = calloc ( cells , sizeof (bool )),
4462- .numbers = calloc ( cells , sizeof (double ))};
4463- int * idx = malloc ( (size_t )count * sizeof (int ));
4464- binding_t * sorted = malloc ( (size_t )count * sizeof (binding_t ));
4460+ .values = cbm_calloc ( CBM_MEM_CLASS_OTHER , cells * sizeof (char * )),
4461+ .numbered = cbm_calloc ( CBM_MEM_CLASS_OTHER , cells * sizeof (bool )),
4462+ .numbers = cbm_calloc ( CBM_MEM_CLASS_OTHER , cells * sizeof (double ))};
4463+ int * idx = cbm_alloc ( CBM_MEM_CLASS_OTHER , (size_t )count * sizeof (int ));
4464+ binding_t * sorted = cbm_alloc ( CBM_MEM_CLASS_OTHER , (size_t )count * sizeof (binding_t ));
44654465 bool ready = ctx .values && ctx .numbered && ctx .numbers && idx && sorted ;
44664466 for (int i = 0 ; ready && i < count ; i ++ ) {
44674467 idx [i ] = i ;
44684468 for (size_t k = 0 ; ready && k < keys ; k ++ ) {
44694469 size_t cell = (size_t )i * keys + k ;
4470- ctx .values [cell ] = heap_strdup (order_key_value (& vbindings [i ], wc -> order_keys [k ]));
4470+ ctx .values [cell ] = cbm_mem_strdup (CBM_MEM_CLASS_OTHER ,
4471+ order_key_value (& vbindings [i ], wc -> order_keys [k ]));
44714472 ready = ctx .values [cell ] != NULL ;
44724473 char * end = NULL ;
44734474 ctx .numbers [cell ] = ready ? strtod (ctx .values [cell ], & end ) : 0 ;
@@ -4493,13 +4494,13 @@ static void sort_bindings(binding_t *vbindings, int count, const cbm_return_clau
44934494 }
44944495 }
44954496 for (size_t c = 0 ; ctx .values && c < cells ; c ++ ) {
4496- free ( ctx .values [c ]);
4497+ cbm_free ( CBM_MEM_CLASS_OTHER , ctx .values [c ]);
44974498 }
4498- free ( ctx .values );
4499- free ( ctx .numbered );
4500- free ( ctx .numbers );
4501- free ( idx );
4502- free ( sorted );
4499+ cbm_free ( CBM_MEM_CLASS_OTHER , ctx .values );
4500+ cbm_free ( CBM_MEM_CLASS_OTHER , ctx .numbered );
4501+ cbm_free ( CBM_MEM_CLASS_OTHER , ctx .numbers );
4502+ cbm_free ( CBM_MEM_CLASS_OTHER , idx );
4503+ cbm_free ( CBM_MEM_CLASS_OTHER , sorted );
45034504}
45044505
45054506/* Apply skip and limit to a binding array, freeing discarded entries */
@@ -4629,7 +4630,8 @@ static int with_agg_find_or_create(with_agg_t **aggs, int *agg_cnt, int *agg_cap
46294630 (* aggs )[found ].maxs = calloc (wc -> count , sizeof (double ));
46304631 (* aggs )[found ].distinct_lists = calloc (wc -> count , sizeof (char * * ));
46314632 (* aggs )[found ].distinct_n = calloc (wc -> count , sizeof (int ));
4632- (* aggs )[found ].distinct_sets = calloc (wc -> count , sizeof (CBMHashTable * ));
4633+ (* aggs )[found ].distinct_sets =
4634+ cbm_calloc (CBM_MEM_CLASS_OTHER , (size_t )wc -> count * sizeof (CBMHashTable * ));
46334635 (* aggs )[found ].group_nodes = calloc (wc -> count , sizeof (cbm_node_t ));
46344636 for (int ci = 0 ; ci < wc -> count ; ci ++ ) {
46354637 (* aggs )[found ].mins [ci ] = CYP_DBL_MAX ;
@@ -4724,7 +4726,7 @@ static void with_agg_free(with_agg_t *aggs, int agg_cnt, int item_count) {
47244726 free (aggs [a ].maxs );
47254727 free (aggs [a ].distinct_lists );
47264728 free (aggs [a ].distinct_n );
4727- free ( aggs [a ].distinct_sets );
4729+ cbm_free ( CBM_MEM_CLASS_OTHER , aggs [a ].distinct_sets );
47284730 if (aggs [a ].group_nodes ) {
47294731 for (int ci = 0 ; ci < item_count ; ci ++ ) {
47304732 node_fields_free (& aggs [a ].group_nodes [ci ]);
@@ -5134,7 +5136,8 @@ static void ret_agg_init_group(ret_agg_entry_t *entry, const char *key, int item
51345136 entry -> maxs = calloc (item_count , sizeof (double ));
51355137 entry -> collect_lists = calloc (item_count , sizeof (char * * ));
51365138 entry -> collect_counts = calloc (item_count , sizeof (int ));
5137- entry -> distinct_sets = calloc (item_count , sizeof (CBMHashTable * ));
5139+ entry -> distinct_sets =
5140+ cbm_calloc (CBM_MEM_CLASS_OTHER , (size_t )item_count * sizeof (CBMHashTable * ));
51385141 for (int ci = 0 ; ci < item_count ; ci ++ ) {
51395142 entry -> mins [ci ] = CYP_DBL_MAX ;
51405143 entry -> maxs [ci ] = - CYP_DBL_MAX ;
@@ -5217,7 +5220,7 @@ static void ret_agg_free(ret_agg_entry_t *aggs, int agg_count, int item_count) {
52175220 free (aggs [a ].maxs );
52185221 free (aggs [a ].collect_lists );
52195222 free (aggs [a ].collect_counts );
5220- free ( aggs [a ].distinct_sets );
5223+ cbm_free ( CBM_MEM_CLASS_OTHER , aggs [a ].distinct_sets );
52215224 }
52225225 free (aggs );
52235226}
0 commit comments