mongory 0.9.1-aarch64-linux → 2.0.1-aarch64-linux
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -0
- data/README.md +115 -420
- data/SUBMODULE_INTEGRATION.md +7 -20
- data/docs/clang_bridge.md +28 -63
- data/docs/migration.md +17 -0
- data/docs/performance.md +14 -51
- data/examples/benchmark.rb +10 -26
- data/ext/mongory_ext/extconf.rb +1 -1
- data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +62 -0
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +31 -0
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +3 -0
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +20 -0
- data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +31 -4
- data/ext/mongory_ext/mongory-core/src/foundations/array.c +48 -6
- data/ext/mongory_ext/mongory-core/src/foundations/config.c +30 -2
- data/ext/mongory_ext/mongory-core/src/foundations/error.c +10 -0
- data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +78 -19
- data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +73 -19
- data/ext/mongory_ext/mongory-core/src/foundations/table.c +8 -3
- data/ext/mongory_ext/mongory-core/src/foundations/utils.c +44 -55
- data/ext/mongory_ext/mongory-core/src/foundations/utils.h +7 -0
- data/ext/mongory_ext/mongory-core/src/foundations/value.c +3 -0
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +68 -0
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +14 -0
- data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +9 -4
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +132 -29
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +1 -1
- data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.c → existence_matcher.c} +10 -12
- data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.h → existence_matcher.h} +4 -4
- data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +18 -6
- data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.c → implicit_array_matcher.c} +57 -38
- data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.h → implicit_array_matcher.h} +5 -5
- data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +8 -10
- data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +337 -96
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +59 -21
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +7 -1
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +4 -4
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -1
- data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.c → value_matcher.c} +104 -60
- data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.h → value_matcher.h} +13 -13
- data/ext/mongory_ext/mongory_ext.c +2 -2
- data/ext/mongory_ext/mongory_ext_internal.h +2 -3
- data/ext/mongory_ext/{rb_mongory_cmatcher.c → rb_mongory_matcher.c} +145 -91
- data/ext/mongory_ext/rb_mongory_value.c +67 -0
- data/lib/core/2.6/mongory_ext.so +0 -0
- data/lib/core/2.7/mongory_ext.so +0 -0
- data/lib/core/3.0/mongory_ext.so +0 -0
- data/lib/core/3.1/mongory_ext.so +0 -0
- data/lib/core/3.2/mongory_ext.so +0 -0
- data/lib/core/3.3/mongory_ext.so +0 -0
- data/lib/core/3.4/mongory_ext.so +0 -0
- data/lib/core/4.0/mongory_ext.so +0 -0
- data/lib/generators/mongory/install/templates/initializer.rb.erb +0 -5
- data/lib/generators/mongory/matcher/templates/matcher.rb.erb +4 -15
- data/lib/mongory/converters/condition_converter.rb +1 -1
- data/lib/mongory/converters/converted.rb +28 -6
- data/lib/mongory/converters/key_converter.rb +2 -0
- data/lib/mongory/matcher.rb +50 -0
- data/lib/mongory/matchers/abstract_matcher.rb +18 -192
- data/lib/mongory/matchers.rb +68 -84
- data/lib/mongory/query_builder.rb +29 -42
- data/lib/mongory/utils/context.rb +10 -15
- data/lib/mongory/utils/tree_renderer.rb +98 -0
- data/lib/mongory/utils.rb +2 -67
- data/lib/mongory/version.rb +1 -1
- data/lib/mongory.rb +9 -59
- data/script/bench.rb +54 -0
- data/scripts/build_with_core.sh +0 -1
- metadata +13 -37
- data/lib/mongory/c_matcher.rb +0 -57
- data/lib/mongory/c_query_builder.rb +0 -44
- data/lib/mongory/matchers/abstract_multi_matcher.rb +0 -150
- data/lib/mongory/matchers/and_matcher.rb +0 -73
- data/lib/mongory/matchers/array_record_matcher.rb +0 -105
- data/lib/mongory/matchers/elem_match_matcher.rb +0 -57
- data/lib/mongory/matchers/eq_matcher.rb +0 -48
- data/lib/mongory/matchers/every_matcher.rb +0 -56
- data/lib/mongory/matchers/exists_matcher.rb +0 -53
- data/lib/mongory/matchers/field_matcher.rb +0 -160
- data/lib/mongory/matchers/gt_matcher.rb +0 -41
- data/lib/mongory/matchers/gte_matcher.rb +0 -41
- data/lib/mongory/matchers/hash_condition_matcher.rb +0 -80
- data/lib/mongory/matchers/in_matcher.rb +0 -84
- data/lib/mongory/matchers/literal_matcher.rb +0 -126
- data/lib/mongory/matchers/lt_matcher.rb +0 -41
- data/lib/mongory/matchers/lte_matcher.rb +0 -41
- data/lib/mongory/matchers/ne_matcher.rb +0 -39
- data/lib/mongory/matchers/nin_matcher.rb +0 -64
- data/lib/mongory/matchers/not_matcher.rb +0 -40
- data/lib/mongory/matchers/or_matcher.rb +0 -69
- data/lib/mongory/matchers/present_matcher.rb +0 -55
- data/lib/mongory/matchers/regex_matcher.rb +0 -109
- data/lib/mongory/matchers/size_matcher.rb +0 -54
- data/lib/mongory/query_matcher.rb +0 -93
- data/lib/mongory/rails.rb +0 -15
- data/lib/mongory/utils/debugger.rb +0 -125
- data/lib/mongory/utils/rails_patch.rb +0 -22
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* `mongory_array` with capacity information and the actual item storage.
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
|
+
#include <stdint.h>
|
|
11
12
|
#include <string.h>
|
|
12
13
|
#include "array_private.h"
|
|
13
14
|
#include <mongory-core/foundations/array.h>
|
|
@@ -36,6 +37,10 @@
|
|
|
36
37
|
*/
|
|
37
38
|
bool mongory_array_resize(mongory_array *self, size_t size) {
|
|
38
39
|
mongory_array_private *internal = (mongory_array_private *)self;
|
|
40
|
+
if (size > SIZE_MAX / sizeof(mongory_value *)) {
|
|
41
|
+
self->pool->error = &MONGORY_ALLOC_ERROR;
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
39
44
|
|
|
40
45
|
// Allocate a new, larger block of memory for the items.
|
|
41
46
|
mongory_value **new_items = MG_ALLOC_ARY(self->pool, mongory_value*, size);
|
|
@@ -73,10 +78,18 @@ static inline bool mongory_array_grow_if_needed(mongory_array *self, size_t size
|
|
|
73
78
|
if (size < internal->capacity) {
|
|
74
79
|
return true;
|
|
75
80
|
}
|
|
81
|
+
if (size == SIZE_MAX) {
|
|
82
|
+
self->pool->error = &MONGORY_ALLOC_ERROR;
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
76
85
|
|
|
77
86
|
// Calculate new capacity, typically doubling, until it's sufficient.
|
|
78
|
-
size_t new_capacity = internal->capacity
|
|
87
|
+
size_t new_capacity = internal->capacity > 0 ? internal->capacity : MONGORY_ARRAY_INIT_SIZE;
|
|
79
88
|
while (new_capacity <= size) {
|
|
89
|
+
if (new_capacity > SIZE_MAX / 2) {
|
|
90
|
+
new_capacity = size + 1;
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
80
93
|
new_capacity *= 2;
|
|
81
94
|
}
|
|
82
95
|
|
|
@@ -232,18 +245,31 @@ mongory_array *mongory_array_nested_wrap(mongory_memory_pool *pool, int argc, ..
|
|
|
232
245
|
}
|
|
233
246
|
va_list args;
|
|
234
247
|
va_start(args, argc);
|
|
248
|
+
bool ok = true;
|
|
235
249
|
for (int i = 0; i < argc; i++) {
|
|
236
250
|
mongory_value *value = va_arg(args, mongory_value *);
|
|
237
|
-
array->push(array, value)
|
|
251
|
+
if (ok && (!value || !array->push(array, value))) {
|
|
252
|
+
ok = false;
|
|
253
|
+
}
|
|
238
254
|
}
|
|
239
255
|
va_end(args);
|
|
256
|
+
if (!ok || pool->error != NULL) {
|
|
257
|
+
return NULL;
|
|
258
|
+
}
|
|
240
259
|
return array;
|
|
241
260
|
}
|
|
242
261
|
|
|
243
262
|
typedef size_t(*mongory_array_sort_cb)(mongory_value *value, void *ctx);
|
|
244
263
|
|
|
245
264
|
static mongory_value** mongory_array_merge_sort_finalize(mongory_memory_pool *pool, mongory_value **left, mongory_value **right, size_t count, void *ctx, mongory_array_sort_cb callback) {
|
|
265
|
+
if (!left || !right) {
|
|
266
|
+
return NULL;
|
|
267
|
+
}
|
|
246
268
|
mongory_value **result = MG_ALLOC_ARY(pool, mongory_value*, count);
|
|
269
|
+
if (!result) {
|
|
270
|
+
pool->error = &MONGORY_ALLOC_ERROR;
|
|
271
|
+
return NULL;
|
|
272
|
+
}
|
|
247
273
|
size_t i = 0, j = 0, k = 0;
|
|
248
274
|
while (i < count / 2 && j < count - count / 2) {
|
|
249
275
|
if (callback(left[i], ctx) < callback(right[j], ctx)) {
|
|
@@ -267,19 +293,35 @@ static mongory_value** mongory_array_merge_sort(mongory_memory_pool *pool, mongo
|
|
|
267
293
|
}
|
|
268
294
|
size_t mid = count / 2;
|
|
269
295
|
mongory_value **left = mongory_array_merge_sort(pool, origin_items, mid, ctx, callback);
|
|
296
|
+
if (!left) {
|
|
297
|
+
return NULL;
|
|
298
|
+
}
|
|
270
299
|
mongory_value **right = mongory_array_merge_sort(pool, origin_items + mid, count - mid, ctx, callback);
|
|
300
|
+
if (!right) {
|
|
301
|
+
return NULL;
|
|
302
|
+
}
|
|
271
303
|
return mongory_array_merge_sort_finalize(pool, left, right, count, ctx, callback);
|
|
272
304
|
}
|
|
273
305
|
|
|
274
306
|
mongory_array *mongory_array_sort_by(mongory_array *self, mongory_memory_pool *temp_pool, void *ctx, mongory_array_sort_cb callback) {
|
|
307
|
+
if (!self || !temp_pool) {
|
|
308
|
+
return NULL;
|
|
309
|
+
}
|
|
275
310
|
mongory_array_private *internal = (mongory_array_private *)self;
|
|
276
311
|
|
|
277
312
|
mongory_value **new_items = mongory_array_merge_sort(temp_pool, internal->items, self->count, ctx, callback);
|
|
278
|
-
|
|
279
|
-
|
|
313
|
+
if (!new_items) {
|
|
314
|
+
self->pool->error = &MONGORY_ALLOC_ERROR;
|
|
315
|
+
return NULL;
|
|
316
|
+
}
|
|
317
|
+
mongory_array_private *new_array = (mongory_array_private *)mongory_array_new_with_capacity(self->pool, internal->capacity);
|
|
318
|
+
if (!new_array) {
|
|
319
|
+
return NULL;
|
|
320
|
+
}
|
|
280
321
|
new_array->base.count = self->count;
|
|
281
|
-
|
|
282
|
-
|
|
322
|
+
if (self->count > 0) {
|
|
323
|
+
memcpy(new_array->items, new_items, sizeof(mongory_value *) * self->count);
|
|
324
|
+
}
|
|
283
325
|
return &new_array->base;
|
|
284
326
|
}
|
|
285
327
|
|
|
@@ -12,9 +12,9 @@
|
|
|
12
12
|
#include "../matchers/base_matcher.h" // For mongory_matcher_build_func
|
|
13
13
|
#include "../matchers/compare_matcher.h" // For specific matcher constructors
|
|
14
14
|
#include "../matchers/composite_matcher.h" // For specific matcher constructors
|
|
15
|
-
#include "../matchers/
|
|
15
|
+
#include "../matchers/existence_matcher.h" // For specific matcher constructors
|
|
16
16
|
#include "../matchers/inclusion_matcher.h" // For specific matcher constructors
|
|
17
|
-
#include "../matchers/
|
|
17
|
+
#include "../matchers/value_matcher.h" // For specific matcher constructors
|
|
18
18
|
#include "../matchers/external_matcher.h" // For specific matcher constructors
|
|
19
19
|
#include "config_private.h" // For mongory_regex_adapter, mongory_value_converter, etc.
|
|
20
20
|
#include "mongory-core/foundations/memory_pool.h"
|
|
@@ -66,6 +66,8 @@ mongory_matcher_custom_adapter mongory_custom_matcher_adapter = {
|
|
|
66
66
|
};
|
|
67
67
|
|
|
68
68
|
bool mongory_matcher_trace_result_colorful = true;
|
|
69
|
+
static size_t mongory_compile_max_depth = MONGORY_DEFAULT_MAX_DEPTH;
|
|
70
|
+
static size_t mongory_compile_max_nodes = MONGORY_DEFAULT_MAX_NODES;
|
|
69
71
|
|
|
70
72
|
/**
|
|
71
73
|
* @brief Initializes the internal memory pool if it hasn't been already.
|
|
@@ -245,6 +247,32 @@ void mongory_matcher_trace_result_colorful_set(bool colorful) {
|
|
|
245
247
|
mongory_matcher_trace_result_colorful = colorful;
|
|
246
248
|
}
|
|
247
249
|
|
|
250
|
+
void mongory_max_depth_set(size_t max_depth) {
|
|
251
|
+
mongory_config_lock();
|
|
252
|
+
mongory_compile_max_depth = max_depth;
|
|
253
|
+
mongory_config_unlock();
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
size_t mongory_max_depth_get(void) {
|
|
257
|
+
mongory_config_lock();
|
|
258
|
+
size_t val = mongory_compile_max_depth;
|
|
259
|
+
mongory_config_unlock();
|
|
260
|
+
return val;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
void mongory_max_nodes_set(size_t max_nodes) {
|
|
264
|
+
mongory_config_lock();
|
|
265
|
+
mongory_compile_max_nodes = max_nodes;
|
|
266
|
+
mongory_config_unlock();
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
size_t mongory_max_nodes_get(void) {
|
|
270
|
+
mongory_config_lock();
|
|
271
|
+
size_t val = mongory_compile_max_nodes;
|
|
272
|
+
mongory_config_unlock();
|
|
273
|
+
return val;
|
|
274
|
+
}
|
|
275
|
+
|
|
248
276
|
/**
|
|
249
277
|
* @brief Initializes all core Mongory library components.
|
|
250
278
|
* This includes the internal memory pool, regex adapter, matcher mapping table,
|
|
@@ -35,4 +35,14 @@ mongory_error MONGORY_ALLOC_ERROR = {
|
|
|
35
35
|
.type = MONGORY_ERROR_MEMORY,
|
|
36
36
|
.message = "Memory Allocation Failed",
|
|
37
37
|
};
|
|
38
|
+
|
|
39
|
+
mongory_error MONGORY_DEPTH_LIMIT_ERROR = {
|
|
40
|
+
.type = MONGORY_ERROR_LIMIT_EXCEEDED,
|
|
41
|
+
.message = "Maximum condition nesting depth exceeded",
|
|
42
|
+
};
|
|
43
|
+
|
|
44
|
+
mongory_error MONGORY_NODE_LIMIT_ERROR = {
|
|
45
|
+
.type = MONGORY_ERROR_LIMIT_EXCEEDED,
|
|
46
|
+
.message = "Maximum matcher node count exceeded",
|
|
47
|
+
};
|
|
38
48
|
#endif
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
|
|
14
14
|
#include "memory_pool_private.h"
|
|
15
15
|
#include <mongory-core/foundations/memory_pool.h>
|
|
16
|
+
#include <stdint.h> // For SIZE_MAX
|
|
16
17
|
#include <stdio.h> // For NULL, though stdlib.h or stddef.h is more common
|
|
17
18
|
#include <stdlib.h> // For calloc, free, etc.
|
|
18
19
|
#include <string.h> // For memset
|
|
@@ -85,6 +86,9 @@ typedef struct mongory_memory_node {
|
|
|
85
86
|
*/
|
|
86
87
|
typedef struct mongory_memory_pool_ctx {
|
|
87
88
|
size_t chunk_size; /**< Current preferred size for new chunks. */
|
|
89
|
+
size_t max_chunk_size; /**< Upper cap for geometric chunk doubling. */
|
|
90
|
+
size_t max_total_bytes; /**< Max cumulative chunk bytes (0 = unlimited). */
|
|
91
|
+
size_t total_bytes; /**< Total bytes allocated across arena chunks. */
|
|
88
92
|
mongory_memory_node *head; /**< Head of the list of memory chunks. */
|
|
89
93
|
mongory_memory_node *current; /**< Current chunk to allocate from. */
|
|
90
94
|
mongory_memory_node *extra; /**< Head of list for externally traced memory. */
|
|
@@ -101,14 +105,14 @@ typedef struct mongory_memory_pool_ctx {
|
|
|
101
105
|
* failure.
|
|
102
106
|
*/
|
|
103
107
|
static inline mongory_memory_node *mongory_memory_chunk_new(size_t chunk_size) {
|
|
104
|
-
mongory_memory_node *node = calloc(1, sizeof(mongory_memory_node));
|
|
108
|
+
mongory_memory_node *node = (mongory_memory_node *)calloc(1, sizeof(mongory_memory_node));
|
|
105
109
|
if (!node) {
|
|
106
110
|
return NULL; // Failed to allocate node structure.
|
|
107
111
|
}
|
|
108
112
|
|
|
109
113
|
void *mem = calloc(1, chunk_size);
|
|
110
114
|
if (!mem) {
|
|
111
|
-
free(node);
|
|
115
|
+
free(node); // Clean up allocated node structure.
|
|
112
116
|
return NULL; // Failed to allocate memory block for the chunk.
|
|
113
117
|
}
|
|
114
118
|
|
|
@@ -121,10 +125,10 @@ static inline mongory_memory_node *mongory_memory_chunk_new(size_t chunk_size) {
|
|
|
121
125
|
}
|
|
122
126
|
|
|
123
127
|
/**
|
|
124
|
-
* @brief Grows the memory pool by adding a new
|
|
128
|
+
* @brief Grows the memory pool by advancing to a reusable chunk or adding a new chunk.
|
|
125
129
|
*
|
|
126
|
-
*
|
|
127
|
-
*
|
|
130
|
+
* Geometric growth is capped at `ctx->max_chunk_size` and total cumulative bytes
|
|
131
|
+
* are bounded by `ctx->max_total_bytes` when non-zero.
|
|
128
132
|
*
|
|
129
133
|
* @param ctx Pointer to the memory pool's context.
|
|
130
134
|
* @param request_size The minimum size required from the new chunk for an
|
|
@@ -132,21 +136,44 @@ static inline mongory_memory_node *mongory_memory_chunk_new(size_t chunk_size) {
|
|
|
132
136
|
* @return true if growth was successful, false otherwise.
|
|
133
137
|
*/
|
|
134
138
|
static inline bool mongory_memory_pool_grow(mongory_memory_pool_ctx *ctx, size_t request_size) {
|
|
135
|
-
|
|
139
|
+
// Reuse already-allocated chunks after reset() if one has enough capacity.
|
|
140
|
+
while (ctx->current->next) {
|
|
136
141
|
ctx->current = ctx->current->next;
|
|
137
|
-
|
|
142
|
+
if (ctx->current->size - ctx->current->used >= request_size) {
|
|
143
|
+
return true;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
size_t next_chunk_size = ctx->chunk_size;
|
|
148
|
+
size_t cap = ctx->max_chunk_size > 0 ? ctx->max_chunk_size : MONGORY_DEFAULT_MAX_CHUNK_SIZE;
|
|
149
|
+
if (next_chunk_size < cap) {
|
|
150
|
+
if (next_chunk_size > cap / 2) {
|
|
151
|
+
next_chunk_size = cap;
|
|
152
|
+
} else {
|
|
153
|
+
next_chunk_size *= 2;
|
|
154
|
+
}
|
|
138
155
|
}
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
ctx->
|
|
156
|
+
size_t alloc_size = next_chunk_size >= request_size ? next_chunk_size : request_size;
|
|
157
|
+
|
|
158
|
+
if (ctx->max_total_bytes > 0) {
|
|
159
|
+
if (alloc_size > ctx->max_total_bytes || ctx->total_bytes > ctx->max_total_bytes - alloc_size) {
|
|
160
|
+
// Try exact request_size if geometric growth overshot the budget.
|
|
161
|
+
if (request_size <= ctx->max_total_bytes && ctx->total_bytes <= ctx->max_total_bytes - request_size) {
|
|
162
|
+
alloc_size = request_size;
|
|
163
|
+
} else {
|
|
164
|
+
return false;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
143
167
|
}
|
|
144
168
|
|
|
145
|
-
mongory_memory_node *new_chunk = mongory_memory_chunk_new(
|
|
169
|
+
mongory_memory_node *new_chunk = mongory_memory_chunk_new(alloc_size);
|
|
146
170
|
if (!new_chunk) {
|
|
147
171
|
return false; // Failed to create a new chunk.
|
|
148
172
|
}
|
|
149
173
|
|
|
174
|
+
ctx->chunk_size = next_chunk_size;
|
|
175
|
+
ctx->total_bytes += alloc_size;
|
|
176
|
+
|
|
150
177
|
// Link the new chunk and update the current pointer.
|
|
151
178
|
ctx->current->next = new_chunk;
|
|
152
179
|
ctx->current = new_chunk;
|
|
@@ -166,6 +193,13 @@ static inline bool mongory_memory_pool_grow(mongory_memory_pool_ctx *ctx, size_t
|
|
|
166
193
|
* @return void* Pointer to the allocated memory, or NULL on failure.
|
|
167
194
|
*/
|
|
168
195
|
static inline void *mongory_memory_pool_alloc(mongory_memory_pool *pool, size_t size) {
|
|
196
|
+
if (!pool || !pool->ctx) {
|
|
197
|
+
return NULL;
|
|
198
|
+
}
|
|
199
|
+
if (size > SIZE_MAX - 7) {
|
|
200
|
+
pool->error = &MONGORY_ALLOC_ERROR;
|
|
201
|
+
return NULL;
|
|
202
|
+
}
|
|
169
203
|
mongory_memory_pool_ctx *pool_ctx = (mongory_memory_pool_ctx *)pool->ctx;
|
|
170
204
|
size = MONGORY_ALIGN8(size); // Ensure 8-byte alignment.
|
|
171
205
|
|
|
@@ -173,9 +207,10 @@ static inline void *mongory_memory_pool_alloc(mongory_memory_pool *pool, size_t
|
|
|
173
207
|
if (size > balance) {
|
|
174
208
|
// Not enough space in current chunk, try to grow.
|
|
175
209
|
if (!mongory_memory_pool_grow(pool_ctx, size)) {
|
|
210
|
+
pool->error = &MONGORY_ALLOC_ERROR;
|
|
176
211
|
return NULL; // Growth failed.
|
|
177
212
|
}
|
|
178
|
-
// After successful growth, pool_ctx->current points to
|
|
213
|
+
// After successful growth, pool_ctx->current points to a chunk with >= size bytes.
|
|
179
214
|
}
|
|
180
215
|
|
|
181
216
|
// Allocate from the current chunk.
|
|
@@ -186,6 +221,9 @@ static inline void *mongory_memory_pool_alloc(mongory_memory_pool *pool, size_t
|
|
|
186
221
|
}
|
|
187
222
|
|
|
188
223
|
static inline void mongory_memory_pool_reset(mongory_memory_pool *pool) {
|
|
224
|
+
if (!pool || !pool->ctx) {
|
|
225
|
+
return;
|
|
226
|
+
}
|
|
189
227
|
mongory_memory_pool_ctx *pool_ctx = (mongory_memory_pool_ctx *)pool->ctx;
|
|
190
228
|
pool_ctx->current = pool_ctx->head;
|
|
191
229
|
mongory_memory_node *node = pool_ctx->head;
|
|
@@ -264,14 +302,15 @@ static inline void mongory_memory_pool_destroy(mongory_memory_pool *pool) {
|
|
|
264
302
|
* @param size Size of the externally allocated memory.
|
|
265
303
|
*/
|
|
266
304
|
static inline void mongory_memory_pool_trace(mongory_memory_pool *pool, void *ptr, size_t size) {
|
|
305
|
+
if (!pool || !pool->ctx || !ptr) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
267
308
|
mongory_memory_pool_ctx *pool_ctx = (mongory_memory_pool_ctx *)pool->ctx;
|
|
268
309
|
|
|
269
310
|
// Create a new node to trace this external allocation.
|
|
270
|
-
mongory_memory_node *extra_alloc_tracer = calloc(1, sizeof(mongory_memory_node));
|
|
311
|
+
mongory_memory_node *extra_alloc_tracer = (mongory_memory_node *)calloc(1, sizeof(mongory_memory_node));
|
|
271
312
|
if (!extra_alloc_tracer) {
|
|
272
|
-
|
|
273
|
-
// This might lead to a leak of 'ptr' if the caller expects the pool to
|
|
274
|
-
// manage it. Consider error reporting.
|
|
313
|
+
pool->error = &MONGORY_ALLOC_ERROR;
|
|
275
314
|
return;
|
|
276
315
|
}
|
|
277
316
|
extra_alloc_tracer->ptr = ptr; // This is the externally allocated memory.
|
|
@@ -292,13 +331,13 @@ static inline void mongory_memory_pool_trace(mongory_memory_pool *pool, void *pt
|
|
|
292
331
|
*/
|
|
293
332
|
mongory_memory_pool *mongory_memory_pool_new() {
|
|
294
333
|
// Allocate the main pool structure.
|
|
295
|
-
mongory_memory_pool *pool = calloc(1, sizeof(mongory_memory_pool));
|
|
334
|
+
mongory_memory_pool *pool = (mongory_memory_pool *)calloc(1, sizeof(mongory_memory_pool));
|
|
296
335
|
if (!pool) {
|
|
297
336
|
return NULL;
|
|
298
337
|
}
|
|
299
338
|
|
|
300
339
|
// Allocate the internal context for the pool.
|
|
301
|
-
mongory_memory_pool_ctx *ctx = calloc(1, sizeof(mongory_memory_pool_ctx));
|
|
340
|
+
mongory_memory_pool_ctx *ctx = (mongory_memory_pool_ctx *)calloc(1, sizeof(mongory_memory_pool_ctx));
|
|
302
341
|
if (!ctx) {
|
|
303
342
|
free(pool); // Clean up partially allocated pool.
|
|
304
343
|
return NULL;
|
|
@@ -314,6 +353,9 @@ mongory_memory_pool *mongory_memory_pool_new() {
|
|
|
314
353
|
|
|
315
354
|
// Initialize context fields.
|
|
316
355
|
ctx->chunk_size = MONGORY_INITIAL_CHUNK_SIZE;
|
|
356
|
+
ctx->max_chunk_size = MONGORY_DEFAULT_MAX_CHUNK_SIZE;
|
|
357
|
+
ctx->max_total_bytes = 0;
|
|
358
|
+
ctx->total_bytes = MONGORY_INITIAL_CHUNK_SIZE;
|
|
317
359
|
ctx->head = first_chunk;
|
|
318
360
|
ctx->current = first_chunk;
|
|
319
361
|
ctx->extra = NULL; // No extra traced allocations initially.
|
|
@@ -330,6 +372,23 @@ mongory_memory_pool *mongory_memory_pool_new() {
|
|
|
330
372
|
return pool;
|
|
331
373
|
}
|
|
332
374
|
|
|
375
|
+
void mongory_memory_pool_set_limits(mongory_memory_pool *pool, size_t max_chunk_size, size_t max_total_bytes) {
|
|
376
|
+
if (!pool || !pool->ctx || pool->alloc != mongory_memory_pool_alloc) {
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
mongory_memory_pool_ctx *ctx = (mongory_memory_pool_ctx *)pool->ctx;
|
|
380
|
+
ctx->max_chunk_size = max_chunk_size > 0 ? max_chunk_size : MONGORY_DEFAULT_MAX_CHUNK_SIZE;
|
|
381
|
+
ctx->max_total_bytes = max_total_bytes;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
size_t mongory_memory_pool_total_bytes(const mongory_memory_pool *pool) {
|
|
385
|
+
if (!pool || !pool->ctx || pool->alloc != mongory_memory_pool_alloc) {
|
|
386
|
+
return 0;
|
|
387
|
+
}
|
|
388
|
+
const mongory_memory_pool_ctx *ctx = (const mongory_memory_pool_ctx *)pool->ctx;
|
|
389
|
+
return ctx->total_bytes;
|
|
390
|
+
}
|
|
391
|
+
|
|
333
392
|
void mongory_memory_pool_lock(mongory_memory_pool *pool) {
|
|
334
393
|
if (!pool || !pool->ctx || pool->alloc != mongory_memory_pool_alloc) {
|
|
335
394
|
return;
|
|
@@ -1,62 +1,116 @@
|
|
|
1
1
|
#include "string_buffer.h"
|
|
2
2
|
#include <mongory-core.h>
|
|
3
3
|
#include <stdarg.h>
|
|
4
|
+
#include <stdint.h>
|
|
4
5
|
#include <stdio.h>
|
|
5
6
|
#include <string.h>
|
|
6
7
|
|
|
7
8
|
#define MONGORY_STRING_BUFFER_INITIAL_CAPACITY 16
|
|
8
9
|
|
|
9
10
|
mongory_string_buffer *mongory_string_buffer_new(mongory_memory_pool *pool) {
|
|
11
|
+
if (!pool || !pool->alloc) {
|
|
12
|
+
return NULL;
|
|
13
|
+
}
|
|
10
14
|
mongory_string_buffer *buffer = MG_ALLOC_PTR(pool, mongory_string_buffer);
|
|
15
|
+
if (!buffer) {
|
|
16
|
+
pool->error = &MONGORY_ALLOC_ERROR;
|
|
17
|
+
return NULL;
|
|
18
|
+
}
|
|
19
|
+
char *data = (char *)MG_ALLOC(pool, MONGORY_STRING_BUFFER_INITIAL_CAPACITY);
|
|
20
|
+
if (!data) {
|
|
21
|
+
pool->error = &MONGORY_ALLOC_ERROR;
|
|
22
|
+
return NULL;
|
|
23
|
+
}
|
|
11
24
|
buffer->pool = pool;
|
|
12
|
-
buffer->buffer =
|
|
25
|
+
buffer->buffer = data;
|
|
13
26
|
buffer->size = 0;
|
|
14
27
|
buffer->capacity = MONGORY_STRING_BUFFER_INITIAL_CAPACITY;
|
|
15
28
|
buffer->buffer[0] = '\0';
|
|
16
29
|
return buffer;
|
|
17
30
|
}
|
|
18
31
|
|
|
19
|
-
static inline
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
buffer->capacity *= 2;
|
|
23
|
-
trigger_grow = true;
|
|
32
|
+
static inline bool mongory_string_buffer_grow(mongory_string_buffer *buffer, size_t additional_size) {
|
|
33
|
+
if (!buffer || !buffer->pool) {
|
|
34
|
+
return false;
|
|
24
35
|
}
|
|
25
|
-
if (
|
|
26
|
-
|
|
36
|
+
if (additional_size > SIZE_MAX - 1 || buffer->size > SIZE_MAX - 1 - additional_size) {
|
|
37
|
+
buffer->pool->error = &MONGORY_ALLOC_ERROR;
|
|
38
|
+
return false;
|
|
39
|
+
}
|
|
40
|
+
size_t required = buffer->size + additional_size + 1;
|
|
41
|
+
if (required <= buffer->capacity) {
|
|
42
|
+
return true;
|
|
43
|
+
}
|
|
44
|
+
size_t new_capacity = buffer->capacity > 0 ? buffer->capacity : MONGORY_STRING_BUFFER_INITIAL_CAPACITY;
|
|
45
|
+
while (new_capacity < required) {
|
|
46
|
+
if (new_capacity > SIZE_MAX / 2) {
|
|
47
|
+
new_capacity = required;
|
|
48
|
+
break;
|
|
49
|
+
}
|
|
50
|
+
new_capacity *= 2;
|
|
27
51
|
}
|
|
28
|
-
char *
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
52
|
+
char *new_buffer = (char *)MG_ALLOC(buffer->pool, new_capacity);
|
|
53
|
+
if (!new_buffer) {
|
|
54
|
+
buffer->pool->error = &MONGORY_ALLOC_ERROR;
|
|
55
|
+
return false;
|
|
32
56
|
}
|
|
57
|
+
if (buffer->buffer) {
|
|
58
|
+
memcpy(new_buffer, buffer->buffer, buffer->size + 1);
|
|
59
|
+
} else {
|
|
60
|
+
new_buffer[0] = '\0';
|
|
61
|
+
}
|
|
62
|
+
buffer->buffer = new_buffer;
|
|
63
|
+
buffer->capacity = new_capacity;
|
|
64
|
+
return true;
|
|
33
65
|
}
|
|
34
66
|
|
|
35
67
|
void mongory_string_buffer_append(mongory_string_buffer *buffer, const char *str) {
|
|
68
|
+
if (!buffer || !str) {
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
36
71
|
size_t len = strlen(str);
|
|
37
|
-
mongory_string_buffer_grow(buffer, len)
|
|
38
|
-
|
|
72
|
+
if (!mongory_string_buffer_grow(buffer, len)) {
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
memcpy(buffer->buffer + buffer->size, str, len + 1);
|
|
39
76
|
buffer->size += len;
|
|
40
|
-
buffer->buffer[buffer->size] = '\0';
|
|
41
77
|
}
|
|
42
78
|
|
|
43
79
|
void mongory_string_buffer_appendf(mongory_string_buffer *buffer, const char *format, ...) {
|
|
80
|
+
if (!buffer || !format) {
|
|
81
|
+
return;
|
|
82
|
+
}
|
|
44
83
|
va_list args;
|
|
45
84
|
va_start(args, format);
|
|
46
85
|
int len = vsnprintf(NULL, 0, format, args);
|
|
47
86
|
va_end(args);
|
|
48
|
-
|
|
87
|
+
if (len < 0) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
if (!mongory_string_buffer_grow(buffer, (size_t)len)) {
|
|
91
|
+
return;
|
|
92
|
+
}
|
|
49
93
|
va_start(args, format);
|
|
50
94
|
vsnprintf(buffer->buffer + buffer->size, buffer->capacity - buffer->size, format, args);
|
|
51
95
|
va_end(args);
|
|
52
|
-
buffer->size += len;
|
|
96
|
+
buffer->size += (size_t)len;
|
|
53
97
|
buffer->buffer[buffer->size] = '\0';
|
|
54
98
|
}
|
|
55
99
|
|
|
56
|
-
char *mongory_string_buffer_cstr(mongory_string_buffer *buffer) {
|
|
100
|
+
char *mongory_string_buffer_cstr(mongory_string_buffer *buffer) {
|
|
101
|
+
return buffer ? buffer->buffer : NULL;
|
|
102
|
+
}
|
|
57
103
|
|
|
58
104
|
void mongory_string_buffer_clear(mongory_string_buffer *buffer) {
|
|
59
|
-
buffer
|
|
105
|
+
if (!buffer || !buffer->pool) {
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
char *new_buffer = (char *)MG_ALLOC(buffer->pool, MONGORY_STRING_BUFFER_INITIAL_CAPACITY);
|
|
109
|
+
if (!new_buffer) {
|
|
110
|
+
buffer->pool->error = &MONGORY_ALLOC_ERROR;
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
buffer->buffer = new_buffer;
|
|
60
114
|
buffer->capacity = MONGORY_STRING_BUFFER_INITIAL_CAPACITY;
|
|
61
115
|
buffer->size = 0;
|
|
62
116
|
buffer->buffer[0] = '\0';
|
|
@@ -15,6 +15,7 @@
|
|
|
15
15
|
#include <mongory-core/foundations/value.h>
|
|
16
16
|
#include "utils.h"
|
|
17
17
|
#include <stdarg.h>
|
|
18
|
+
#include <stdint.h>
|
|
18
19
|
#include <string.h> // For strcmp, strlen
|
|
19
20
|
|
|
20
21
|
/**
|
|
@@ -172,6 +173,10 @@ static inline bool mongory_table_rehash(mongory_table *self) {
|
|
|
172
173
|
|
|
173
174
|
mongory_value **original_items_ptr = old_array_private_view->items;
|
|
174
175
|
size_t original_capacity = internal->capacity;
|
|
176
|
+
if (original_capacity > SIZE_MAX / 2) {
|
|
177
|
+
self->pool->error = &MONGORY_ALLOC_ERROR;
|
|
178
|
+
return false;
|
|
179
|
+
}
|
|
175
180
|
size_t new_capacity = next_prime(original_capacity * 2);
|
|
176
181
|
|
|
177
182
|
// Reset the count of the existing array before resizing, as resize
|
|
@@ -467,10 +472,10 @@ mongory_table *mongory_table_nested_wrap(mongory_memory_pool *pool, int argc, ..
|
|
|
467
472
|
va_start(args, argc);
|
|
468
473
|
for (int i = 0; i < argc; i++) {
|
|
469
474
|
char *key = va_arg(args, char *);
|
|
470
|
-
MONGORY_VALIDATE_PTR(pool, key);
|
|
471
475
|
mongory_value *value = va_arg(args, mongory_value *);
|
|
472
|
-
MONGORY_VALIDATE_PTR(pool, value)
|
|
473
|
-
|
|
476
|
+
if (pool->error == NULL && MONGORY_VALIDATE_PTR(pool, key) && MONGORY_VALIDATE_PTR(pool, value)) {
|
|
477
|
+
mongory_table_set(table, key, value);
|
|
478
|
+
}
|
|
474
479
|
}
|
|
475
480
|
va_end(args);
|
|
476
481
|
if (pool->error != NULL) {
|