mongory 2.1.0-aarch64-linux → 2.2.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 (76) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +30 -0
  3. data/README.md +55 -73
  4. data/docs/clang_bridge.md +2 -2
  5. data/docs/performance.md +43 -7
  6. data/lib/core/2.6/mongory_ext.so +0 -0
  7. data/lib/core/2.7/mongory_ext.so +0 -0
  8. data/lib/core/3.0/mongory_ext.so +0 -0
  9. data/lib/core/3.1/mongory_ext.so +0 -0
  10. data/lib/core/3.2/mongory_ext.so +0 -0
  11. data/lib/core/3.3/mongory_ext.so +0 -0
  12. data/lib/core/3.4/mongory_ext.so +0 -0
  13. data/lib/core/4.0/mongory_ext.so +0 -0
  14. data/lib/mongory/converters/abstract_converter.rb +5 -5
  15. data/lib/mongory/converters/converted.rb +0 -4
  16. data/lib/mongory/converters/key_converter.rb +13 -36
  17. data/lib/mongory/converters/value_converter.rb +4 -4
  18. data/lib/mongory/matchers.rb +8 -7
  19. data/lib/mongory/query_builder.rb +9 -0
  20. data/lib/mongory/utils/context.rb +0 -6
  21. data/lib/mongory/utils/singleton_builder.rb +1 -4
  22. data/lib/mongory/version.rb +1 -1
  23. data/lib/mongory.rb +0 -1
  24. data/mongory.gemspec +13 -12
  25. data/script/bench.rb +77 -4
  26. data/script/coverage.rb +84 -0
  27. metadata +9 -54
  28. data/ext/mongory_ext/extconf.rb +0 -115
  29. data/ext/mongory_ext/mongory-core/include/mongory-core/adapters/json.h +0 -62
  30. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/array.h +0 -122
  31. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +0 -217
  32. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +0 -82
  33. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -115
  34. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/table.h +0 -127
  35. data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/value.h +0 -175
  36. data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +0 -103
  37. data/ext/mongory_ext/mongory-core/include/mongory-core.h +0 -12
  38. data/ext/mongory_ext/mongory-core/src/foundations/array.c +0 -336
  39. data/ext/mongory_ext/mongory-core/src/foundations/array_private.h +0 -20
  40. data/ext/mongory_ext/mongory-core/src/foundations/config.c +0 -335
  41. data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +0 -49
  42. data/ext/mongory_ext/mongory-core/src/foundations/error.c +0 -48
  43. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +0 -406
  44. data/ext/mongory_ext/mongory-core/src/foundations/memory_pool_private.h +0 -45
  45. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +0 -117
  46. data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +0 -43
  47. data/ext/mongory_ext/mongory-core/src/foundations/table.c +0 -502
  48. data/ext/mongory_ext/mongory-core/src/foundations/utils.c +0 -206
  49. data/ext/mongory_ext/mongory-core/src/foundations/utils.h +0 -77
  50. data/ext/mongory_ext/mongory-core/src/foundations/value.c +0 -548
  51. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +0 -193
  52. data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +0 -117
  53. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +0 -241
  54. data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.h +0 -83
  55. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +0 -730
  56. data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +0 -131
  57. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.c +0 -158
  58. data/ext/mongory_ext/mongory-core/src/matchers/existence_matcher.h +0 -48
  59. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +0 -199
  60. data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +0 -58
  61. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.c +0 -204
  62. data/ext/mongory_ext/mongory-core/src/matchers/implicit_array_matcher.h +0 -47
  63. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +0 -124
  64. data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.h +0 -46
  65. data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +0 -529
  66. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -117
  67. data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -29
  68. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +0 -61
  69. data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +0 -23
  70. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.c +0 -416
  71. data/ext/mongory_ext/mongory-core/src/matchers/value_matcher.h +0 -97
  72. data/ext/mongory_ext/mongory_ext.c +0 -66
  73. data/ext/mongory_ext/mongory_ext_internal.h +0 -74
  74. data/ext/mongory_ext/rb_mongory_adapters.c +0 -140
  75. data/ext/mongory_ext/rb_mongory_matcher.c +0 -440
  76. data/ext/mongory_ext/rb_mongory_value.c +0 -327
@@ -1,406 +0,0 @@
1
- /**
2
- * @file memory_pool.c
3
- * @brief Implements a memory pool for the Mongory library.
4
- *
5
- * This implementation uses a linked list of memory chunks. Allocations are
6
- * made from the current chunk. If the current chunk is full, a new, larger
7
- * chunk is allocated and added to the list. Freeing the pool deallocates all
8
- * chunks. It also supports tracing externally allocated memory.
9
- */
10
- #if !defined(_WIN32) && (!defined(_POSIX_C_SOURCE) || _POSIX_C_SOURCE < 200809L)
11
- # define _POSIX_C_SOURCE 200809L
12
- #endif
13
-
14
- #include "memory_pool_private.h"
15
- #include <mongory-core/foundations/memory_pool.h>
16
- #include <stdint.h> // For SIZE_MAX
17
- #include <stdio.h> // For NULL, though stdlib.h or stddef.h is more common
18
- #include <stdlib.h> // For calloc, free, etc.
19
- #include <string.h> // For memset
20
-
21
- #if defined(_WIN32)
22
- # ifndef WIN32_LEAN_AND_MEAN
23
- # define WIN32_LEAN_AND_MEAN
24
- # endif
25
- # include <windows.h>
26
- typedef CRITICAL_SECTION mongory_mutex_t;
27
- static inline void mongory_mutex_init(mongory_mutex_t *mtx) { InitializeCriticalSection(mtx); }
28
- static inline void mongory_mutex_lock(mongory_mutex_t *mtx) { EnterCriticalSection(mtx); }
29
- static inline void mongory_mutex_unlock(mongory_mutex_t *mtx) { LeaveCriticalSection(mtx); }
30
- static inline void mongory_mutex_destroy(mongory_mutex_t *mtx) { DeleteCriticalSection(mtx); }
31
- #else
32
- # include <pthread.h>
33
- typedef pthread_mutex_t mongory_mutex_t;
34
- static inline void mongory_mutex_init(mongory_mutex_t *mtx) {
35
- pthread_mutexattr_t attr;
36
- pthread_mutexattr_init(&attr);
37
- pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
38
- pthread_mutex_init(mtx, &attr);
39
- pthread_mutexattr_destroy(&attr);
40
- }
41
- static inline void mongory_mutex_lock(mongory_mutex_t *mtx) { pthread_mutex_lock(mtx); }
42
- static inline void mongory_mutex_unlock(mongory_mutex_t *mtx) { pthread_mutex_unlock(mtx); }
43
- static inline void mongory_mutex_destroy(mongory_mutex_t *mtx) { pthread_mutex_destroy(mtx); }
44
- #endif
45
-
46
- /**
47
- * @def MONGORY_INITIAL_CHUNK_SIZE
48
- * @brief The initial size in bytes for the first memory chunk allocated by the
49
- * pool.
50
- */
51
- #define MONGORY_INITIAL_CHUNK_SIZE 2048
52
-
53
- /**
54
- * @def MONGORY_ALIGN8(size)
55
- * @brief Macro to align a given size to an 8-byte boundary.
56
- * This is useful for ensuring that memory allocations are suitably aligned
57
- * for various data types.
58
- * @param size The original size.
59
- * @return The size aligned up to the nearest multiple of 8.
60
- */
61
- #define MONGORY_ALIGN8(size) (((size) + 7) & ~((size_t)7))
62
-
63
- /**
64
- * @struct mongory_memory_node
65
- * @brief Represents a node in the linked list of memory chunks within the pool.
66
- *
67
- * Each node holds a pointer to a block of memory (`ptr`), its total `size`,
68
- * how much of it is `used`, and a pointer to the `next` node in the list.
69
- * This structure is also used by the tracing mechanism to track external
70
- * allocations.
71
- */
72
- typedef struct mongory_memory_node {
73
- void *ptr; /**< Pointer to the allocated memory block. */
74
- size_t size; /**< Total size of the memory block. */
75
- size_t used; /**< Bytes used in this memory block. */
76
- struct mongory_memory_node *next; /**< Pointer to the next memory node. */
77
- } mongory_memory_node;
78
-
79
- /**
80
- * @struct mongory_memory_pool_ctx
81
- * @brief Internal context for a mongory_memory_pool.
82
- *
83
- * Stores the current `chunk_size` (which doubles on growth), pointers to
84
- * the `head` and `current` memory nodes for allocations, and a list (`extra`)
85
- * for traced external allocations.
86
- */
87
- typedef struct mongory_memory_pool_ctx {
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. */
92
- mongory_memory_node *head; /**< Head of the list of memory chunks. */
93
- mongory_memory_node *current; /**< Current chunk to allocate from. */
94
- mongory_memory_node *extra; /**< Head of list for externally traced memory. */
95
- mongory_mutex_t build_lock; /**< Recursive lock for lazy matcher construction. */
96
- } mongory_memory_pool_ctx;
97
-
98
- /**
99
- * @brief Allocates a new memory chunk (node and its associated memory block).
100
- *
101
- * Uses `calloc` to ensure memory is zero-initialized.
102
- *
103
- * @param chunk_size The size of the memory block to allocate for this chunk.
104
- * @return mongory_memory_node* Pointer to the new memory node, or NULL on
105
- * failure.
106
- */
107
- static inline mongory_memory_node *mongory_memory_chunk_new(size_t chunk_size) {
108
- mongory_memory_node *node = (mongory_memory_node *)calloc(1, sizeof(mongory_memory_node));
109
- if (!node) {
110
- return NULL; // Failed to allocate node structure.
111
- }
112
-
113
- void *mem = calloc(1, chunk_size);
114
- if (!mem) {
115
- free(node); // Clean up allocated node structure.
116
- return NULL; // Failed to allocate memory block for the chunk.
117
- }
118
-
119
- node->ptr = mem;
120
- node->size = chunk_size;
121
- node->used = 0;
122
- node->next = NULL;
123
-
124
- return node;
125
- }
126
-
127
- /**
128
- * @brief Grows the memory pool by advancing to a reusable chunk or adding a new chunk.
129
- *
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.
132
- *
133
- * @param ctx Pointer to the memory pool's context.
134
- * @param request_size The minimum size required from the new chunk for an
135
- * upcoming allocation.
136
- * @return true if growth was successful, false otherwise.
137
- */
138
- static inline bool mongory_memory_pool_grow(mongory_memory_pool_ctx *ctx, size_t request_size) {
139
- // Reuse already-allocated chunks after reset() if one has enough capacity.
140
- while (ctx->current->next) {
141
- ctx->current = ctx->current->next;
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
- }
155
- }
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
- }
167
- }
168
-
169
- mongory_memory_node *new_chunk = mongory_memory_chunk_new(alloc_size);
170
- if (!new_chunk) {
171
- return false; // Failed to create a new chunk.
172
- }
173
-
174
- ctx->chunk_size = next_chunk_size;
175
- ctx->total_bytes += alloc_size;
176
-
177
- // Link the new chunk and update the current pointer.
178
- ctx->current->next = new_chunk;
179
- ctx->current = new_chunk;
180
-
181
- return true;
182
- }
183
-
184
- /**
185
- * @brief Allocates memory from the pool. Implements `pool->alloc`.
186
- *
187
- * Allocates `size` bytes (aligned to 8 bytes) from the `current` chunk.
188
- * If the current chunk doesn't have enough space, `mongory_memory_pool_grow`
189
- * is called to add a new chunk.
190
- *
191
- * @param pool Pointer to the `mongory_memory_pool`.
192
- * @param size The number of bytes to allocate.
193
- * @return void* Pointer to the allocated memory, or NULL on failure.
194
- */
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
- }
203
- mongory_memory_pool_ctx *pool_ctx = (mongory_memory_pool_ctx *)pool->ctx;
204
- size = MONGORY_ALIGN8(size); // Ensure 8-byte alignment.
205
-
206
- size_t balance = pool_ctx->current->size - pool_ctx->current->used;
207
- if (size > balance) {
208
- // Not enough space in current chunk, try to grow.
209
- if (!mongory_memory_pool_grow(pool_ctx, size)) {
210
- pool->error = &MONGORY_ALLOC_ERROR;
211
- return NULL; // Growth failed.
212
- }
213
- // After successful growth, pool_ctx->current points to a chunk with >= size bytes.
214
- }
215
-
216
- // Allocate from the current chunk.
217
- void *ptr = (char *)pool_ctx->current->ptr + pool_ctx->current->used;
218
- pool_ctx->current->used += size;
219
-
220
- return ptr;
221
- }
222
-
223
- static inline void mongory_memory_pool_reset(mongory_memory_pool *pool) {
224
- if (!pool || !pool->ctx) {
225
- return;
226
- }
227
- mongory_memory_pool_ctx *pool_ctx = (mongory_memory_pool_ctx *)pool->ctx;
228
- pool_ctx->current = pool_ctx->head;
229
- mongory_memory_node *node = pool_ctx->head;
230
- while (node) {
231
- node->used = 0;
232
- node = node->next;
233
- }
234
- }
235
-
236
- /**
237
- * @brief Frees a linked list of memory nodes.
238
- *
239
- * Iterates through the list, freeing the memory block (`node->ptr`) and then
240
- * the node structure itself for each node. Memory blocks are zeroed out before
241
- * freeing for security/safety.
242
- *
243
- * @param head Pointer to the head of the memory node list to free.
244
- */
245
- static inline void mongory_memory_pool_node_list_free(mongory_memory_node *head) {
246
- mongory_memory_node *node = head;
247
- while (node) {
248
- mongory_memory_node *next = node->next;
249
- if (node->ptr) {
250
- memset(node->ptr, 0, node->size); // Clear memory for safety.
251
- free(node->ptr); // Free the actual memory block.
252
- }
253
- memset(node, 0,
254
- sizeof(mongory_memory_node)); // Clear the node structure itself.
255
- free(node); // Free the node structure.
256
- node = next;
257
- }
258
- }
259
-
260
- /**
261
- * @brief Destroys the memory pool and frees all associated memory.
262
- * Implements `pool->free`.
263
- *
264
- * Frees all memory chunks allocated by the pool (`ctx->head` list) and all
265
- * externally traced memory blocks (`ctx->extra` list). Then frees the pool
266
- * context and the pool structure itself.
267
- *
268
- * @param pool Pointer to the `mongory_memory_pool` to destroy.
269
- */
270
- static inline void mongory_memory_pool_destroy(mongory_memory_pool *pool) {
271
- if (!pool)
272
- return;
273
- mongory_memory_pool_ctx *ctx = (mongory_memory_pool_ctx *)pool->ctx;
274
-
275
- if (ctx) {
276
- // Free the main list of memory chunks.
277
- mongory_memory_pool_node_list_free(ctx->head);
278
- // Free the list of externally traced memory chunks.
279
- mongory_memory_pool_node_list_free(ctx->extra);
280
- mongory_mutex_destroy(&ctx->build_lock);
281
-
282
- memset(ctx, 0,
283
- sizeof(mongory_memory_pool_ctx)); // Clear the context structure.
284
- free(ctx); // Free the context structure.
285
- }
286
-
287
- memset(pool, 0, sizeof(mongory_memory_pool)); // Clear the pool structure.
288
- free(pool); // Free the pool structure.
289
- }
290
-
291
- /**
292
- * @brief Traces an externally allocated piece of memory. Implements
293
- * `pool->trace`.
294
- *
295
- * Creates a new `mongory_memory_node` to track the external memory block and
296
- * adds it to the `extra` list in the pool's context. This memory will be
297
- * freed when `mongory_memory_pool_destroy` is called if this trace function
298
- * is used.
299
- *
300
- * @param pool Pointer to the `mongory_memory_pool`.
301
- * @param ptr Pointer to the externally allocated memory.
302
- * @param size Size of the externally allocated memory.
303
- */
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
- }
308
- mongory_memory_pool_ctx *pool_ctx = (mongory_memory_pool_ctx *)pool->ctx;
309
-
310
- // Create a new node to trace this external allocation.
311
- mongory_memory_node *extra_alloc_tracer = (mongory_memory_node *)calloc(1, sizeof(mongory_memory_node));
312
- if (!extra_alloc_tracer) {
313
- pool->error = &MONGORY_ALLOC_ERROR;
314
- return;
315
- }
316
- extra_alloc_tracer->ptr = ptr; // This is the externally allocated memory.
317
- extra_alloc_tracer->size = size; // Its size.
318
- extra_alloc_tracer->used = size; // Mark as fully "used" in context of tracing.
319
- extra_alloc_tracer->next = pool_ctx->extra; // Prepend to extra list.
320
- pool_ctx->extra = extra_alloc_tracer;
321
- }
322
-
323
- /**
324
- * @brief Creates and initializes a new memory pool.
325
- *
326
- * Allocates the `mongory_memory_pool` structure, its internal
327
- * `mongory_memory_pool_ctx`, and the first memory chunk. Sets up the function
328
- * pointers for `alloc`, `free`, and `trace`.
329
- *
330
- * @return mongory_memory_pool* Pointer to the new pool, or NULL on failure.
331
- */
332
- mongory_memory_pool *mongory_memory_pool_new() {
333
- // Allocate the main pool structure.
334
- mongory_memory_pool *pool = (mongory_memory_pool *)calloc(1, sizeof(mongory_memory_pool));
335
- if (!pool) {
336
- return NULL;
337
- }
338
-
339
- // Allocate the internal context for the pool.
340
- mongory_memory_pool_ctx *ctx = (mongory_memory_pool_ctx *)calloc(1, sizeof(mongory_memory_pool_ctx));
341
- if (!ctx) {
342
- free(pool); // Clean up partially allocated pool.
343
- return NULL;
344
- }
345
-
346
- // Allocate the first memory chunk.
347
- mongory_memory_node *first_chunk = mongory_memory_chunk_new(MONGORY_INITIAL_CHUNK_SIZE);
348
- if (!first_chunk) {
349
- free(ctx); // Clean up context.
350
- free(pool); // Clean up pool structure.
351
- return NULL;
352
- }
353
-
354
- // Initialize context fields.
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;
359
- ctx->head = first_chunk;
360
- ctx->current = first_chunk;
361
- ctx->extra = NULL; // No extra traced allocations initially.
362
- mongory_mutex_init(&ctx->build_lock);
363
-
364
- // Initialize pool fields.
365
- pool->ctx = ctx;
366
- pool->alloc = mongory_memory_pool_alloc;
367
- pool->reset = mongory_memory_pool_reset;
368
- pool->free = mongory_memory_pool_destroy;
369
- pool->trace = mongory_memory_pool_trace;
370
- pool->error = NULL; // No error initially.
371
-
372
- return pool;
373
- }
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
-
392
- void mongory_memory_pool_lock(mongory_memory_pool *pool) {
393
- if (!pool || !pool->ctx || pool->alloc != mongory_memory_pool_alloc) {
394
- return;
395
- }
396
- mongory_memory_pool_ctx *ctx = (mongory_memory_pool_ctx *)pool->ctx;
397
- mongory_mutex_lock(&ctx->build_lock);
398
- }
399
-
400
- void mongory_memory_pool_unlock(mongory_memory_pool *pool) {
401
- if (!pool || !pool->ctx || pool->alloc != mongory_memory_pool_alloc) {
402
- return;
403
- }
404
- mongory_memory_pool_ctx *ctx = (mongory_memory_pool_ctx *)pool->ctx;
405
- mongory_mutex_unlock(&ctx->build_lock);
406
- }
@@ -1,45 +0,0 @@
1
- #ifndef MONGORY_FOUNDATIONS_MEMORY_POOL_PRIVATE_H
2
- #define MONGORY_FOUNDATIONS_MEMORY_POOL_PRIVATE_H
3
-
4
- /**
5
- * @file memory_pool_private.h
6
- * @brief Internal synchronization and atomic helpers for mongory-core.
7
- *
8
- * Keeps platform-specific primitives out of public headers while enabling
9
- * thread-safe double-checked lazy initialization on shared matcher pools.
10
- */
11
-
12
- #include "mongory-core/foundations/memory_pool.h"
13
-
14
- #if defined(_MSC_VER)
15
- # ifndef WIN32_LEAN_AND_MEAN
16
- # define WIN32_LEAN_AND_MEAN
17
- # endif
18
- # include <windows.h>
19
- # define mongory_atomic_load_ptr_acquire(ptr) \
20
- InterlockedCompareExchangePointer((PVOID volatile *)(ptr), NULL, NULL)
21
- # define mongory_atomic_load_ptr_relaxed(ptr) (*(ptr))
22
- # define mongory_atomic_store_ptr_release(ptr, val) \
23
- InterlockedExchangePointer((PVOID volatile *)(ptr), (PVOID)(val))
24
- #elif defined(__GNUC__) || defined(__clang__)
25
- # define mongory_atomic_load_ptr_acquire(ptr) __atomic_load_n((ptr), __ATOMIC_ACQUIRE)
26
- # define mongory_atomic_load_ptr_relaxed(ptr) __atomic_load_n((ptr), __ATOMIC_RELAXED)
27
- # define mongory_atomic_store_ptr_release(ptr, val) __atomic_store_n((ptr), (val), __ATOMIC_RELEASE)
28
- #else
29
- # define mongory_atomic_load_ptr_acquire(ptr) (*(ptr))
30
- # define mongory_atomic_load_ptr_relaxed(ptr) (*(ptr))
31
- # define mongory_atomic_store_ptr_release(ptr, val) (*(ptr) = (val))
32
- #endif
33
-
34
- /**
35
- * @brief Acquires the pool's internal recursive build lock.
36
- * Safe no-op if `pool` is NULL or uses a custom allocator without a core ctx.
37
- */
38
- void mongory_memory_pool_lock(mongory_memory_pool *pool);
39
-
40
- /**
41
- * @brief Releases the pool's internal recursive build lock.
42
- */
43
- void mongory_memory_pool_unlock(mongory_memory_pool *pool);
44
-
45
- #endif /* MONGORY_FOUNDATIONS_MEMORY_POOL_PRIVATE_H */
@@ -1,117 +0,0 @@
1
- #include "string_buffer.h"
2
- #include <mongory-core.h>
3
- #include <stdarg.h>
4
- #include <stdint.h>
5
- #include <stdio.h>
6
- #include <string.h>
7
-
8
- #define MONGORY_STRING_BUFFER_INITIAL_CAPACITY 16
9
-
10
- mongory_string_buffer *mongory_string_buffer_new(mongory_memory_pool *pool) {
11
- if (!pool || !pool->alloc) {
12
- return NULL;
13
- }
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
- }
24
- buffer->pool = pool;
25
- buffer->buffer = data;
26
- buffer->size = 0;
27
- buffer->capacity = MONGORY_STRING_BUFFER_INITIAL_CAPACITY;
28
- buffer->buffer[0] = '\0';
29
- return buffer;
30
- }
31
-
32
- static inline bool mongory_string_buffer_grow(mongory_string_buffer *buffer, size_t additional_size) {
33
- if (!buffer || !buffer->pool) {
34
- return false;
35
- }
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;
51
- }
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;
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;
65
- }
66
-
67
- void mongory_string_buffer_append(mongory_string_buffer *buffer, const char *str) {
68
- if (!buffer || !str) {
69
- return;
70
- }
71
- size_t len = strlen(str);
72
- if (!mongory_string_buffer_grow(buffer, len)) {
73
- return;
74
- }
75
- memcpy(buffer->buffer + buffer->size, str, len + 1);
76
- buffer->size += len;
77
- }
78
-
79
- void mongory_string_buffer_appendf(mongory_string_buffer *buffer, const char *format, ...) {
80
- if (!buffer || !format) {
81
- return;
82
- }
83
- va_list args;
84
- va_start(args, format);
85
- int len = vsnprintf(NULL, 0, format, args);
86
- va_end(args);
87
- if (len < 0) {
88
- return;
89
- }
90
- if (!mongory_string_buffer_grow(buffer, (size_t)len)) {
91
- return;
92
- }
93
- va_start(args, format);
94
- vsnprintf(buffer->buffer + buffer->size, buffer->capacity - buffer->size, format, args);
95
- va_end(args);
96
- buffer->size += (size_t)len;
97
- buffer->buffer[buffer->size] = '\0';
98
- }
99
-
100
- char *mongory_string_buffer_cstr(mongory_string_buffer *buffer) {
101
- return buffer ? buffer->buffer : NULL;
102
- }
103
-
104
- void mongory_string_buffer_clear(mongory_string_buffer *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;
114
- buffer->capacity = MONGORY_STRING_BUFFER_INITIAL_CAPACITY;
115
- buffer->size = 0;
116
- buffer->buffer[0] = '\0';
117
- }
@@ -1,43 +0,0 @@
1
- #include <mongory-core.h>
2
-
3
- #ifndef MONGORY_STRING_BUFFER_H
4
- #define MONGORY_STRING_BUFFER_H
5
-
6
- typedef struct mongory_string_buffer {
7
- mongory_memory_pool *pool;
8
- char *buffer;
9
- size_t size;
10
- size_t capacity;
11
- } mongory_string_buffer;
12
-
13
- mongory_string_buffer *mongory_string_buffer_new(mongory_memory_pool *pool);
14
-
15
- /**
16
- * @brief Appends a string to the buffer.
17
- * @param buffer The buffer to append to.
18
- * @param str The string to append.
19
- */
20
- void mongory_string_buffer_append(mongory_string_buffer *buffer, const char *str);
21
-
22
- /**
23
- * @brief Appends a formatted string to the buffer.
24
- * @param buffer The buffer to append to.
25
- * @param format The format string.
26
- * @param ... The arguments to the format string.
27
- */
28
- void mongory_string_buffer_appendf(mongory_string_buffer *buffer, const char *format, ...);
29
-
30
- /**
31
- * @brief Returns a C string representation of the buffer.
32
- * @param buffer The buffer to get the C string from.
33
- * @return A C string representation of the buffer.
34
- */
35
- char *mongory_string_buffer_cstr(mongory_string_buffer *buffer);
36
-
37
- /**
38
- * @brief Clears the buffer.
39
- * @param buffer The buffer to clear.
40
- */
41
- void mongory_string_buffer_clear(mongory_string_buffer *buffer);
42
-
43
- #endif