mongory 0.9.1-aarch64-linux → 2.0.0-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.
Files changed (93) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +13 -0
  3. data/README.md +115 -420
  4. data/SUBMODULE_INTEGRATION.md +2 -2
  5. data/docs/clang_bridge.md +28 -59
  6. data/docs/migration.md +17 -0
  7. data/docs/performance.md +14 -51
  8. data/examples/benchmark.rb +10 -10
  9. data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +62 -0
  10. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +31 -0
  11. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +3 -0
  12. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +20 -0
  13. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +31 -4
  14. data/ext/mongory_ext/mongory-core/src/foundations/array.c +48 -6
  15. data/ext/mongory_ext/mongory-core/src/foundations/config.c +30 -2
  16. data/ext/mongory_ext/mongory-core/src/foundations/error.c +10 -0
  17. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +78 -19
  18. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +73 -19
  19. data/ext/mongory_ext/mongory-core/src/foundations/table.c +8 -3
  20. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +44 -55
  21. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +7 -0
  22. data/ext/mongory_ext/mongory-core/src/foundations/value.c +3 -0
  23. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +68 -0
  24. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +14 -0
  25. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +9 -4
  26. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +132 -29
  27. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +1 -1
  28. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.c → existence_matcher.c} +10 -12
  29. data/ext/mongory_ext/mongory-core/src/matchers/{existance_matcher.h → existence_matcher.h} +4 -4
  30. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +18 -6
  31. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.c → implicit_array_matcher.c} +57 -38
  32. data/ext/mongory_ext/mongory-core/src/matchers/{array_record_matcher.h → implicit_array_matcher.h} +5 -5
  33. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +8 -10
  34. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +337 -96
  35. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +59 -21
  36. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +7 -1
  37. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +4 -4
  38. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +1 -1
  39. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.c → value_matcher.c} +104 -60
  40. data/ext/mongory_ext/mongory-core/src/matchers/{literal_matcher.h → value_matcher.h} +13 -13
  41. data/ext/mongory_ext/mongory_ext.c +2 -2
  42. data/ext/mongory_ext/mongory_ext_internal.h +2 -2
  43. data/ext/mongory_ext/{rb_mongory_cmatcher.c → rb_mongory_matcher.c} +157 -23
  44. data/lib/core/2.6/mongory_ext.so +0 -0
  45. data/lib/core/2.7/mongory_ext.so +0 -0
  46. data/lib/core/3.0/mongory_ext.so +0 -0
  47. data/lib/core/3.1/mongory_ext.so +0 -0
  48. data/lib/core/3.2/mongory_ext.so +0 -0
  49. data/lib/core/3.3/mongory_ext.so +0 -0
  50. data/lib/core/3.4/mongory_ext.so +0 -0
  51. data/lib/core/4.0/mongory_ext.so +0 -0
  52. data/lib/generators/mongory/install/templates/initializer.rb.erb +0 -5
  53. data/lib/generators/mongory/matcher/templates/matcher.rb.erb +4 -15
  54. data/lib/mongory/converters/converted.rb +28 -6
  55. data/lib/mongory/converters/key_converter.rb +2 -0
  56. data/lib/mongory/matcher.rb +63 -0
  57. data/lib/mongory/matchers/abstract_matcher.rb +18 -192
  58. data/lib/mongory/matchers.rb +68 -84
  59. data/lib/mongory/query_builder.rb +30 -41
  60. data/lib/mongory/utils/tree_renderer.rb +108 -0
  61. data/lib/mongory/utils.rb +2 -67
  62. data/lib/mongory/version.rb +1 -1
  63. data/lib/mongory.rb +9 -59
  64. data/script/bench.rb +54 -0
  65. metadata +13 -37
  66. data/lib/mongory/c_matcher.rb +0 -57
  67. data/lib/mongory/c_query_builder.rb +0 -44
  68. data/lib/mongory/matchers/abstract_multi_matcher.rb +0 -150
  69. data/lib/mongory/matchers/and_matcher.rb +0 -73
  70. data/lib/mongory/matchers/array_record_matcher.rb +0 -105
  71. data/lib/mongory/matchers/elem_match_matcher.rb +0 -57
  72. data/lib/mongory/matchers/eq_matcher.rb +0 -48
  73. data/lib/mongory/matchers/every_matcher.rb +0 -56
  74. data/lib/mongory/matchers/exists_matcher.rb +0 -53
  75. data/lib/mongory/matchers/field_matcher.rb +0 -160
  76. data/lib/mongory/matchers/gt_matcher.rb +0 -41
  77. data/lib/mongory/matchers/gte_matcher.rb +0 -41
  78. data/lib/mongory/matchers/hash_condition_matcher.rb +0 -80
  79. data/lib/mongory/matchers/in_matcher.rb +0 -84
  80. data/lib/mongory/matchers/literal_matcher.rb +0 -126
  81. data/lib/mongory/matchers/lt_matcher.rb +0 -41
  82. data/lib/mongory/matchers/lte_matcher.rb +0 -41
  83. data/lib/mongory/matchers/ne_matcher.rb +0 -39
  84. data/lib/mongory/matchers/nin_matcher.rb +0 -64
  85. data/lib/mongory/matchers/not_matcher.rb +0 -40
  86. data/lib/mongory/matchers/or_matcher.rb +0 -69
  87. data/lib/mongory/matchers/present_matcher.rb +0 -55
  88. data/lib/mongory/matchers/regex_matcher.rb +0 -109
  89. data/lib/mongory/matchers/size_matcher.rb +0 -54
  90. data/lib/mongory/query_matcher.rb +0 -93
  91. data/lib/mongory/rails.rb +0 -15
  92. data/lib/mongory/utils/debugger.rb +0 -125
  93. data/lib/mongory/utils/rails_patch.rb +0 -22
@@ -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); // Clean up allocated node structure.
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, larger chunk.
128
+ * @brief Grows the memory pool by advancing to a reusable chunk or adding a new chunk.
125
129
  *
126
- * The new chunk size is at least double the previous chunk size, and large
127
- * enough to satisfy `request_size`. The new chunk becomes the `current` chunk.
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
- if (ctx->current->next) {
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
- return true; // Already have a next chunk.
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
- // Double the chunk size, ensuring it's at least as large as request_size.
140
- ctx->chunk_size *= 2;
141
- while (request_size > ctx->chunk_size) {
142
- ctx->chunk_size *= 2;
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(ctx->chunk_size);
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 the new chunk.
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
- // Allocation of tracer node failed; cannot trace.
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 = (char *)MG_ALLOC(pool, MONGORY_STRING_BUFFER_INITIAL_CAPACITY);
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 void mongory_string_buffer_grow(mongory_string_buffer *buffer, size_t additional_size) {
20
- bool trigger_grow = false;
21
- while (buffer->size + additional_size + 1 > buffer->capacity) {
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 (!trigger_grow) {
26
- return;
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 *previous_buffer = buffer->buffer;
29
- buffer->buffer = (char *)MG_ALLOC(buffer->pool, buffer->capacity);
30
- if (previous_buffer) {
31
- strcpy(buffer->buffer, previous_buffer);
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
- strcpy(buffer->buffer + buffer->size, str);
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
- mongory_string_buffer_grow(buffer, len);
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) { return 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->buffer = (char *)MG_ALLOC(buffer->pool, MONGORY_STRING_BUFFER_INITIAL_CAPACITY);
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
- mongory_table_set(table, key, value);
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) {
@@ -83,17 +83,23 @@ char *mongory_string_cpy(mongory_memory_pool *pool, char *str) {
83
83
  }
84
84
 
85
85
  char *mongory_string_cpyf(mongory_memory_pool *pool, char *format, ...) {
86
+ if (!pool || !format) {
87
+ return NULL;
88
+ }
86
89
  va_list args;
87
90
  va_start(args, format);
88
91
  int len = vsnprintf(NULL, 0, format, args);
89
92
  va_end(args);
90
- char *new_str = (char *)MG_ALLOC(pool, len + 1);
93
+ if (len < 0) {
94
+ return NULL;
95
+ }
96
+ char *new_str = (char *)MG_ALLOC(pool, (size_t)len + 1);
91
97
  if (new_str == NULL) {
92
98
  pool->error = &MONGORY_ALLOC_ERROR;
93
99
  return NULL;
94
100
  }
95
101
  va_start(args, format);
96
- vsnprintf(new_str, len + 1, format, args);
102
+ vsnprintf(new_str, (size_t)len + 1, format, args);
97
103
  va_end(args);
98
104
  new_str[len] = '\0';
99
105
  return new_str;
@@ -103,15 +109,29 @@ double mongory_log(double x, double base) {
103
109
  return log(x) / log(base);
104
110
  }
105
111
 
112
+ void mongory_error_set(mongory_memory_pool *pool, mongory_error_type type, const char *message) {
113
+ if (!pool || pool->error != NULL) {
114
+ return;
115
+ }
116
+ mongory_error *error = MG_ALLOC_PTR(pool, mongory_error);
117
+ if (error == NULL) {
118
+ pool->error = &MONGORY_ALLOC_ERROR;
119
+ return;
120
+ }
121
+ error->type = type;
122
+ error->message = message;
123
+ pool->error = error;
124
+ }
125
+
106
126
  bool mongory_validate_ptr(mongory_memory_pool *pool, char *name, void *ptr, char *file, int line) {
107
- if (pool->error != NULL) {
127
+ if (!pool || pool->error != NULL) {
108
128
  return false;
109
129
  }
110
130
  if (ptr == NULL) {
111
- mongory_error *error = MG_ALLOC_PTR(pool, mongory_error);
112
- error->type = MONGORY_ERROR_INVALID_ARGUMENT;
113
- error->message = mongory_string_cpyf(pool, "Null pointer: %s (at %s:%d)", name, file, line);
114
- pool->error = error;
131
+ char *msg = mongory_string_cpyf(pool, "Null pointer: %s (at %s:%d)", name, file, line);
132
+ if (pool->error == NULL) {
133
+ mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, msg ? msg : "Null pointer");
134
+ }
115
135
  return false;
116
136
  }
117
137
  return true;
@@ -122,94 +142,63 @@ static char *error_format =
122
142
  "%s needs %s, got %s\n"
123
143
  "(%s:%d)\n";
124
144
 
145
+ static bool mongory_validate_type_error(mongory_memory_pool *pool, char *name, const char *expected,
146
+ mongory_value *value, char *file, int line) {
147
+ char *msg = mongory_string_cpyf(pool, error_format, name, expected, mongory_type_to_string(value), file, line);
148
+ if (pool->error == NULL) {
149
+ mongory_error_set(pool, MONGORY_ERROR_INVALID_ARGUMENT, msg ? msg : "Invalid argument type");
150
+ }
151
+ return false;
152
+ }
153
+
125
154
  bool mongory_validate_table(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line) {
126
- if (pool->error != NULL) {
155
+ if (!pool || pool->error != NULL) {
127
156
  return false;
128
157
  }
129
158
  if (!mongory_validate_ptr(pool, name, value, file, line)) {
130
159
  return false;
131
160
  }
132
161
  if (value->type != MONGORY_TYPE_TABLE) {
133
- mongory_error *error = MG_ALLOC_PTR(pool, mongory_error);
134
- error->type = MONGORY_ERROR_INVALID_ARGUMENT;
135
- error->message = mongory_string_cpyf(pool, error_format,
136
- name,
137
- "Table",
138
- mongory_type_to_string(value),
139
- file,
140
- line
141
- );
142
- pool->error = error;
143
- return false;
162
+ return mongory_validate_type_error(pool, name, "Table", value, file, line);
144
163
  }
145
164
  return true;
146
165
  }
147
166
 
148
167
  bool mongory_validate_array(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line) {
149
- if (pool->error != NULL) {
168
+ if (!pool || pool->error != NULL) {
150
169
  return false;
151
170
  }
152
171
  if (!mongory_validate_ptr(pool, name, value, file, line)) {
153
172
  return false;
154
173
  }
155
174
  if (value->type != MONGORY_TYPE_ARRAY) {
156
- mongory_error *error = MG_ALLOC_PTR(pool, mongory_error);
157
- error->type = MONGORY_ERROR_INVALID_ARGUMENT;
158
- error->message = mongory_string_cpyf(pool, error_format,
159
- name,
160
- "Array",
161
- mongory_type_to_string(value),
162
- file,
163
- line
164
- );
165
- pool->error = error;
166
- return false;
175
+ return mongory_validate_type_error(pool, name, "Array", value, file, line);
167
176
  }
168
177
  return true;
169
178
  }
170
179
 
171
180
  bool mongory_validate_string(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line) {
172
- if (pool->error != NULL) {
181
+ if (!pool || pool->error != NULL) {
173
182
  return false;
174
183
  }
175
184
  if (!mongory_validate_ptr(pool, name, value, file, line)) {
176
185
  return false;
177
186
  }
178
187
  if (value->type != MONGORY_TYPE_STRING) {
179
- mongory_error *error = MG_ALLOC_PTR(pool, mongory_error);
180
- error->type = MONGORY_ERROR_INVALID_ARGUMENT;
181
- error->message = mongory_string_cpyf(pool, error_format,
182
- name,
183
- "String",
184
- mongory_type_to_string(value),
185
- file,
186
- line
187
- );
188
- pool->error = error;
189
- return false;
188
+ return mongory_validate_type_error(pool, name, "String", value, file, line);
190
189
  }
191
190
  return true;
192
191
  }
193
192
 
194
193
  bool mongory_validate_number(mongory_memory_pool *pool, char *name, mongory_value *value, char *file, int line) {
195
- if (pool->error != NULL) {
194
+ if (!pool || pool->error != NULL) {
196
195
  return false;
197
196
  }
198
197
  if (!mongory_validate_ptr(pool, name, value, file, line)) {
199
198
  return false;
200
199
  }
201
200
  if (value->type != MONGORY_TYPE_INT && value->type != MONGORY_TYPE_DOUBLE) {
202
- mongory_error *error = MG_ALLOC_PTR(pool, mongory_error);
203
- error->type = MONGORY_ERROR_INVALID_ARGUMENT;
204
- error->message = mongory_string_cpyf(pool, error_format,
205
- name,
206
- "Number",
207
- mongory_type_to_string(value),
208
- file,
209
- line
210
- );
211
- pool->error = error;
212
- return false;
201
+ return mongory_validate_type_error(pool, name, "Number", value, file, line);
213
202
  }
214
203
  return true;
215
204
  }
@@ -52,6 +52,13 @@ char *mongory_string_cpyf(mongory_memory_pool *pool, char *format, ...);
52
52
 
53
53
  double mongory_log(double x, double base);
54
54
 
55
+ /**
56
+ * @brief Sets `pool->error` safely without dereferencing NULL on OOM.
57
+ * If allocating `mongory_error` from `pool` fails, falls back to the static
58
+ * `&MONGORY_ALLOC_ERROR`. Does not overwrite an already-set `pool->error`.
59
+ */
60
+ void mongory_error_set(mongory_memory_pool *pool, mongory_error_type type, const char *message);
61
+
55
62
  bool mongory_validate_ptr(mongory_memory_pool *pool, char *name, void *ptr, char *file, int line);
56
63
  #define MONGORY_VALIDATE_PTR(pool, ptr) mongory_validate_ptr(pool, #ptr, ptr, __FILE__, __LINE__)
57
64
 
@@ -240,6 +240,9 @@ mongory_value *mongory_value_wrap_s(mongory_memory_pool *pool, char *s_val) {
240
240
  // when `s_val` was not NULL, but this requires a more complex error handling
241
241
  // strategy with the memory pool.
242
242
  value->data.s = mongory_string_cpy(pool, s_val);
243
+ if (s_val != NULL && value->data.s == NULL) {
244
+ return NULL;
245
+ }
243
246
  value->comp = mongory_value_string_compare;
244
247
  value->to_str = mongory_value_string_to_str;
245
248
  return value;
@@ -15,6 +15,65 @@
15
15
  #include "matcher_traversable.h"
16
16
  #include "../foundations/utils.h"
17
17
 
18
+ #if defined(_MSC_VER)
19
+ # define MONGORY_THREAD_LOCAL __declspec(thread)
20
+ #else
21
+ # define MONGORY_THREAD_LOCAL __thread
22
+ #endif
23
+
24
+ static MONGORY_THREAD_LOCAL mongory_compile_budget mongory_tls_compile_budget = {0};
25
+
26
+ mongory_compile_budget mongory_compile_budget_begin(void) {
27
+ mongory_compile_budget saved = mongory_tls_compile_budget;
28
+ mongory_tls_compile_budget.active = true;
29
+ mongory_tls_compile_budget.depth = 0;
30
+ mongory_tls_compile_budget.nodes = 0;
31
+ mongory_tls_compile_budget.max_depth = mongory_max_depth_get();
32
+ mongory_tls_compile_budget.max_nodes = mongory_max_nodes_get();
33
+ return saved;
34
+ }
35
+
36
+ void mongory_compile_budget_end(mongory_compile_budget saved) {
37
+ mongory_tls_compile_budget = saved;
38
+ }
39
+
40
+ bool mongory_compile_budget_enter_depth(mongory_memory_pool *pool) {
41
+ if (!mongory_tls_compile_budget.active) {
42
+ return true;
43
+ }
44
+ mongory_tls_compile_budget.depth++;
45
+ if (mongory_tls_compile_budget.max_depth > 0 &&
46
+ mongory_tls_compile_budget.depth > mongory_tls_compile_budget.max_depth) {
47
+ mongory_tls_compile_budget.depth--;
48
+ if (pool && pool->error == NULL) {
49
+ pool->error = &MONGORY_DEPTH_LIMIT_ERROR;
50
+ }
51
+ return false;
52
+ }
53
+ return true;
54
+ }
55
+
56
+ void mongory_compile_budget_leave_depth(void) {
57
+ if (mongory_tls_compile_budget.active && mongory_tls_compile_budget.depth > 0) {
58
+ mongory_tls_compile_budget.depth--;
59
+ }
60
+ }
61
+
62
+ bool mongory_compile_budget_count_node(mongory_memory_pool *pool) {
63
+ if (!mongory_tls_compile_budget.active) {
64
+ return true;
65
+ }
66
+ mongory_tls_compile_budget.nodes++;
67
+ if (mongory_tls_compile_budget.max_nodes > 0 &&
68
+ mongory_tls_compile_budget.nodes > mongory_tls_compile_budget.max_nodes) {
69
+ if (pool && pool->error == NULL) {
70
+ pool->error = &MONGORY_NODE_LIMIT_ERROR;
71
+ }
72
+ return false;
73
+ }
74
+ return true;
75
+ }
76
+
18
77
  /**
19
78
  * @brief Allocates and initializes common fields of a `mongory_matcher`.
20
79
  *
@@ -35,6 +94,9 @@ mongory_matcher *mongory_matcher_base_new(mongory_memory_pool *pool, mongory_val
35
94
  if (!pool || !pool->alloc) {
36
95
  return NULL; // Invalid memory pool.
37
96
  }
97
+ if (!mongory_compile_budget_count_node(pool)) {
98
+ return NULL;
99
+ }
38
100
  mongory_matcher *matcher = MG_ALLOC_PTR(pool, mongory_matcher);
39
101
  if (matcher == NULL) {
40
102
  // Allocation failed, pool->alloc might set pool->error.
@@ -86,6 +148,9 @@ mongory_matcher *mongory_matcher_always_true_new(mongory_memory_pool *pool, mong
86
148
  matcher->match = mongory_matcher_always_true_match;
87
149
  matcher->original_match = mongory_matcher_always_true_match;
88
150
  matcher->name = mongory_string_cpy(pool, "Always True");
151
+ if (!matcher->name) {
152
+ return NULL;
153
+ }
89
154
  matcher->explain = mongory_matcher_base_explain;
90
155
  // Optionally set original_match as well if it's a strict policy
91
156
  // matcher->context.original_match = mongory_matcher_always_true_match;
@@ -119,6 +184,9 @@ mongory_matcher *mongory_matcher_always_false_new(mongory_memory_pool *pool, mon
119
184
  matcher->match = mongory_matcher_always_false_match;
120
185
  matcher->original_match = mongory_matcher_always_false_match;
121
186
  matcher->name = mongory_string_cpy(pool, "Always False");
187
+ if (!matcher->name) {
188
+ return NULL;
189
+ }
122
190
  matcher->explain = mongory_matcher_base_explain;
123
191
  // matcher->context.original_match = mongory_matcher_always_false_match;
124
192
  return matcher;