mongory 0.7.1 → 0.7.5
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/.rubocop.yml +2 -0
- data/README.md +34 -0
- data/Rakefile +28 -9
- data/ext/mongory_ext/mongory_ext.c +77 -29
- data/lib/mongory/matchers/in_matcher.rb +2 -2
- data/lib/mongory/matchers/nin_matcher.rb +2 -2
- data/lib/mongory/version.rb +1 -1
- data/lib/mongory.rb +4 -2
- metadata +7 -62
- data/ext/mongory_ext/mongory-core/LICENSE +0 -3
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/array.h +0 -105
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/config.h +0 -206
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/error.h +0 -82
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/memory_pool.h +0 -95
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/table.h +0 -108
- data/ext/mongory_ext/mongory-core/include/mongory-core/foundations/value.h +0 -175
- data/ext/mongory_ext/mongory-core/include/mongory-core/matchers/matcher.h +0 -76
- data/ext/mongory_ext/mongory-core/include/mongory-core.h +0 -12
- data/ext/mongory_ext/mongory-core/src/foundations/array.c +0 -246
- data/ext/mongory_ext/mongory-core/src/foundations/array_private.h +0 -18
- data/ext/mongory_ext/mongory-core/src/foundations/config.c +0 -406
- data/ext/mongory_ext/mongory-core/src/foundations/config_private.h +0 -30
- data/ext/mongory_ext/mongory-core/src/foundations/error.c +0 -30
- data/ext/mongory_ext/mongory-core/src/foundations/memory_pool.c +0 -298
- data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.c +0 -65
- data/ext/mongory_ext/mongory-core/src/foundations/string_buffer.h +0 -49
- data/ext/mongory_ext/mongory-core/src/foundations/table.c +0 -458
- data/ext/mongory_ext/mongory-core/src/foundations/value.c +0 -459
- data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.c +0 -309
- data/ext/mongory_ext/mongory-core/src/matchers/array_record_matcher.h +0 -47
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.c +0 -161
- data/ext/mongory_ext/mongory-core/src/matchers/base_matcher.h +0 -115
- data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.c +0 -210
- data/ext/mongory_ext/mongory-core/src/matchers/compare_matcher.h +0 -83
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.c +0 -539
- data/ext/mongory_ext/mongory-core/src/matchers/composite_matcher.h +0 -125
- data/ext/mongory_ext/mongory-core/src/matchers/existance_matcher.c +0 -144
- data/ext/mongory_ext/mongory-core/src/matchers/existance_matcher.h +0 -48
- data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.c +0 -121
- data/ext/mongory_ext/mongory-core/src/matchers/external_matcher.h +0 -46
- data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.c +0 -199
- data/ext/mongory_ext/mongory-core/src/matchers/inclusion_matcher.h +0 -46
- data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.c +0 -334
- data/ext/mongory_ext/mongory-core/src/matchers/literal_matcher.h +0 -97
- data/ext/mongory_ext/mongory-core/src/matchers/matcher.c +0 -198
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.c +0 -107
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_explainable.h +0 -50
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.c +0 -55
- data/ext/mongory_ext/mongory-core/src/matchers/matcher_traversable.h +0 -23
- data/mongory.gemspec +0 -46
@@ -1,105 +0,0 @@
|
|
1
|
-
#ifndef MONGORY_ARRAY
|
2
|
-
#define MONGORY_ARRAY
|
3
|
-
|
4
|
-
/**
|
5
|
-
* @file array.h
|
6
|
-
* @brief Defines the mongory_array structure and its associated operations.
|
7
|
-
*
|
8
|
-
* mongory_array is a dynamic array implementation that stores mongory_value
|
9
|
-
* pointers. It handles its own memory management for the array storage using a
|
10
|
-
* provided mongory_memory_pool. The array can grow automatically as new
|
11
|
-
* elements are added.
|
12
|
-
*/
|
13
|
-
|
14
|
-
#include "mongory-core/foundations/memory_pool.h"
|
15
|
-
#include "mongory-core/foundations/value.h"
|
16
|
-
#include <stdbool.h>
|
17
|
-
|
18
|
-
/**
|
19
|
-
* @brief Forward declaration for the mongory_array structure.
|
20
|
-
*/
|
21
|
-
typedef struct mongory_array mongory_array;
|
22
|
-
|
23
|
-
/**
|
24
|
-
* @brief Callback function type used by mongory_array_each_func.
|
25
|
-
*
|
26
|
-
* This function is called for each item in the array during an iteration.
|
27
|
-
*
|
28
|
-
* @param item A pointer to the current mongory_value item in the array.
|
29
|
-
* @param acc An accumulator or context pointer passed through the iteration.
|
30
|
-
* @return bool Return true to continue iteration, false to stop.
|
31
|
-
*/
|
32
|
-
typedef bool (*mongory_array_callback_func)(mongory_value *item, void *acc);
|
33
|
-
|
34
|
-
/**
|
35
|
-
* @brief Function pointer type for iterating over the array.
|
36
|
-
* @param self A pointer to the mongory_array instance.
|
37
|
-
* @param acc An accumulator or context pointer to be passed to the callback.
|
38
|
-
* @param func The callback function to be executed for each item.
|
39
|
-
* @return bool Returns true if the iteration completed fully, false if it was
|
40
|
-
* stopped by a callback.
|
41
|
-
*/
|
42
|
-
typedef bool (*mongory_array_each_func)(mongory_array *self, void *acc, mongory_array_callback_func func);
|
43
|
-
|
44
|
-
/**
|
45
|
-
* @brief Function pointer type for adding an element to the end of the array.
|
46
|
-
* @param self A pointer to the mongory_array instance.
|
47
|
-
* @param value A pointer to the mongory_value to add.
|
48
|
-
* @return bool Returns true if the value was successfully added, false
|
49
|
-
* otherwise (e.g., memory allocation failure).
|
50
|
-
*/
|
51
|
-
typedef bool (*mongory_array_push_func)(mongory_array *self, mongory_value *value);
|
52
|
-
|
53
|
-
/**
|
54
|
-
* @brief Function pointer type for retrieving an element at a specific index.
|
55
|
-
* @param self A pointer to the mongory_array instance.
|
56
|
-
* @param index The index of the element to retrieve.
|
57
|
-
* @return mongory_value* A pointer to the mongory_value at the given index, or
|
58
|
-
* NULL if the index is out of bounds.
|
59
|
-
*/
|
60
|
-
typedef mongory_value *(*mongory_array_get_func)(mongory_array *self, size_t index);
|
61
|
-
|
62
|
-
/**
|
63
|
-
* @brief Function pointer type for setting or replacing an element at a
|
64
|
-
* specific index.
|
65
|
-
*
|
66
|
-
* If the index is beyond the current count, the array will be grown, and
|
67
|
-
* intermediate elements will be initialized to NULL.
|
68
|
-
*
|
69
|
-
* @param self A pointer to the mongory_array instance.
|
70
|
-
* @param index The index at which to set the value.
|
71
|
-
* @param value A pointer to the mongory_value to set.
|
72
|
-
* @return bool Returns true if the value was successfully set, false otherwise
|
73
|
-
* (e.g., memory allocation failure).
|
74
|
-
*/
|
75
|
-
typedef bool (*mongory_array_set_func)(mongory_array *self, size_t index, mongory_value *value);
|
76
|
-
|
77
|
-
/**
|
78
|
-
* @brief Creates a new mongory_array instance.
|
79
|
-
*
|
80
|
-
* The array is initialized with a default capacity and will use the provided
|
81
|
-
* memory pool for all its internal allocations related to storing elements.
|
82
|
-
*
|
83
|
-
* @param pool A pointer to the mongory_memory_pool to be used for allocations.
|
84
|
-
* @return mongory_array* A pointer to the newly created mongory_array, or NULL
|
85
|
-
* if creation fails (e.g., memory allocation failure).
|
86
|
-
*/
|
87
|
-
mongory_array *mongory_array_new(mongory_memory_pool *pool);
|
88
|
-
|
89
|
-
/**
|
90
|
-
* @struct mongory_array
|
91
|
-
* @brief Represents a dynamic array of mongory_value pointers.
|
92
|
-
*
|
93
|
-
* This structure provides function pointers for common array operations,
|
94
|
-
* allowing for a somewhat object-oriented interface in C.
|
95
|
-
*/
|
96
|
-
struct mongory_array {
|
97
|
-
size_t count; /**< The current number of elements in the array. */
|
98
|
-
mongory_memory_pool *pool; /**< The memory pool used for allocations. */
|
99
|
-
mongory_array_each_func each; /**< Function to iterate over elements in the array. */
|
100
|
-
mongory_array_push_func push; /**< Function to add an element to the end of the array. */
|
101
|
-
mongory_array_get_func get; /**< Function to retrieve an element by index. */
|
102
|
-
mongory_array_set_func set; /**< Function to set or replace an element at an index. */
|
103
|
-
};
|
104
|
-
|
105
|
-
#endif /* MONGORY_ARRAY */
|
@@ -1,206 +0,0 @@
|
|
1
|
-
#ifndef MONGORY_FOUNDATIONS_CONFIG_H
|
2
|
-
#define MONGORY_FOUNDATIONS_CONFIG_H
|
3
|
-
|
4
|
-
/**
|
5
|
-
* @file config.h
|
6
|
-
* @brief Defines global configuration functions and types for the Mongory
|
7
|
-
* library.
|
8
|
-
*
|
9
|
-
* This includes initialization and cleanup routines, as well as functions
|
10
|
-
* for setting up custom behaviors like regex matching and value conversion.
|
11
|
-
* It also provides a string copying utility that uses the library's memory
|
12
|
-
* pool.
|
13
|
-
*/
|
14
|
-
|
15
|
-
#include "mongory-core/foundations/memory_pool.h"
|
16
|
-
#include "mongory-core/foundations/value.h"
|
17
|
-
#include <stdbool.h>
|
18
|
-
|
19
|
-
/**
|
20
|
-
* @brief Function pointer type for custom regex matching.
|
21
|
-
*
|
22
|
-
* This function is called when a regex match is required.
|
23
|
-
*
|
24
|
-
* @param pool The memory pool to use for any allocations during the match.
|
25
|
-
* @param pattern The regex pattern (as a mongory_value, typically a string or
|
26
|
-
* regex type).
|
27
|
-
* @param value The value to match against (as a mongory_value, typically a
|
28
|
-
* string).
|
29
|
-
* @return bool True if the value matches the pattern, false otherwise.
|
30
|
-
*/
|
31
|
-
typedef bool (*mongory_regex_func)(mongory_memory_pool *pool, mongory_value *pattern, mongory_value *value);
|
32
|
-
|
33
|
-
/**
|
34
|
-
* @brief Function pointer type for stringifying a regex pattern.
|
35
|
-
*
|
36
|
-
* This function is called when a regex pattern needs to be stringified.
|
37
|
-
*
|
38
|
-
* @param pool The memory pool to use for any allocations during the
|
39
|
-
* stringification.
|
40
|
-
* @param pattern The regex pattern (as a mongory_value, typically a string or
|
41
|
-
* regex type).
|
42
|
-
* @return char* A stringified representation of the regex pattern, or NULL on failure.
|
43
|
-
*/
|
44
|
-
typedef char* (*mongory_regex_stringify_func)(mongory_memory_pool *pool, mongory_value *pattern);
|
45
|
-
|
46
|
-
/**
|
47
|
-
* @brief Function pointer type for deep conversion of an external value to a
|
48
|
-
* mongory_value.
|
49
|
-
*
|
50
|
-
* "Deep" implies that if the external value is a complex type (e.g., a struct
|
51
|
-
* representing a document or array), its contents should also be converted.
|
52
|
-
*
|
53
|
-
* @param pool The memory pool to use for allocating the new mongory_value and
|
54
|
-
* its data.
|
55
|
-
* @param value A pointer to the external value to convert.
|
56
|
-
* @return mongory_value* A new mongory_value representing the converted data,
|
57
|
-
* or NULL on failure.
|
58
|
-
*/
|
59
|
-
typedef mongory_value *(*mongory_deep_convert_func)(mongory_memory_pool *pool, void *value);
|
60
|
-
|
61
|
-
/**
|
62
|
-
* @brief Function pointer type for shallow conversion of an external value to a
|
63
|
-
* mongory_value.
|
64
|
-
*
|
65
|
-
* "Shallow" implies that if the external value is complex, only a wrapper or
|
66
|
-
* pointer might be stored in the mongory_value, without deeply converting its
|
67
|
-
* contents.
|
68
|
-
*
|
69
|
-
* @param pool The memory pool to use for allocating the new mongory_value.
|
70
|
-
* @param value A pointer to the external value to convert.
|
71
|
-
* @return mongory_value* A new mongory_value, or NULL on failure.
|
72
|
-
*/
|
73
|
-
typedef mongory_value *(*mongory_shallow_convert_func)(mongory_memory_pool *pool, void *value);
|
74
|
-
|
75
|
-
/**
|
76
|
-
* @brief Function pointer type for recovering an external value from a
|
77
|
-
* mongory_value.
|
78
|
-
*
|
79
|
-
* This is the reverse of a conversion, intended to get back the original
|
80
|
-
* external data representation if it was stored or wrapped.
|
81
|
-
*
|
82
|
-
* @param pool The memory pool (can be used if recovery involves allocation,
|
83
|
-
* though often it might just retrieve a stored pointer).
|
84
|
-
* @param value The mongory_value from which to recover the external data.
|
85
|
-
* @return void* A pointer to the recovered external value, or NULL if not
|
86
|
-
* possible.
|
87
|
-
*/
|
88
|
-
typedef void *(*mongory_recover_func)(mongory_memory_pool *pool, mongory_value *value);
|
89
|
-
|
90
|
-
/**
|
91
|
-
* @brief Initializes the Mongory library.
|
92
|
-
*
|
93
|
-
* Sets up internal structures, including the global memory pool,
|
94
|
-
* regex adapter, matcher mapping, and value converter. This function
|
95
|
-
* should be called before any other Mongory library functions are used.
|
96
|
-
* It also registers the default set of matchers (e.g., $in, $eq).
|
97
|
-
*/
|
98
|
-
void mongory_init();
|
99
|
-
|
100
|
-
/**
|
101
|
-
* @brief Cleans up resources used by the Mongory library.
|
102
|
-
*
|
103
|
-
* Frees the internal memory pool and resets global pointers. This should
|
104
|
-
* be called when the library is no longer needed to prevent memory leaks.
|
105
|
-
*/
|
106
|
-
void mongory_cleanup();
|
107
|
-
|
108
|
-
/**
|
109
|
-
* @brief Sets the custom regex matching function.
|
110
|
-
*
|
111
|
-
* If not called, a default function that always returns false is used.
|
112
|
-
*
|
113
|
-
* @param func The custom regex function to use.
|
114
|
-
*/
|
115
|
-
void mongory_regex_func_set(mongory_regex_func func);
|
116
|
-
|
117
|
-
/**
|
118
|
-
* @brief Sets the custom regex stringify function.
|
119
|
-
*
|
120
|
-
* If not called, a default function that always returns NULL is used.
|
121
|
-
*
|
122
|
-
* @param func The custom regex stringify function to use.
|
123
|
-
*/
|
124
|
-
void mongory_regex_stringify_func_set(mongory_regex_stringify_func func);
|
125
|
-
|
126
|
-
/**
|
127
|
-
* @brief Sets the custom deep value conversion function.
|
128
|
-
* @param deep_convert The function to use for deep conversions.
|
129
|
-
*/
|
130
|
-
void mongory_value_converter_deep_convert_set(mongory_deep_convert_func deep_convert);
|
131
|
-
|
132
|
-
/**
|
133
|
-
* @brief Sets the custom shallow value conversion function.
|
134
|
-
* @param shallow_convert The function to use for shallow conversions.
|
135
|
-
*/
|
136
|
-
void mongory_value_converter_shallow_convert_set(mongory_shallow_convert_func shallow_convert);
|
137
|
-
|
138
|
-
/**
|
139
|
-
* @brief Sets the custom value recovery function.
|
140
|
-
* @param recover The function to use for recovering external values.
|
141
|
-
*/
|
142
|
-
void mongory_value_converter_recover_set(mongory_recover_func recover);
|
143
|
-
|
144
|
-
/**
|
145
|
-
* @brief Function pointer type for matching a value against an external matcher.
|
146
|
-
* @param external_matcher The external reference to the matcher.
|
147
|
-
* @param value The value to match against.
|
148
|
-
* @return bool True if the value matches the matcher, false otherwise.
|
149
|
-
*/
|
150
|
-
typedef struct mongory_matcher_custom_context {
|
151
|
-
char *name; // Name of the custom matcher.
|
152
|
-
void *external_matcher; // External reference to the custom matcher.
|
153
|
-
} mongory_matcher_custom_context;
|
154
|
-
|
155
|
-
/**
|
156
|
-
* @brief Function pointer type for matching a value against an external matcher.
|
157
|
-
*
|
158
|
-
* This function is called when a value needs to be matched against an external
|
159
|
-
* matcher.
|
160
|
-
*
|
161
|
-
* @param external_matcher The external reference to the matcher.
|
162
|
-
* @param value The value to match against.
|
163
|
-
* @return bool True if the value matches the matcher, false otherwise.
|
164
|
-
*/
|
165
|
-
typedef struct mongory_matcher_custom_adapter {
|
166
|
-
bool (*match)(void *external_matcher, mongory_value *value); // Match a value against an external matcher.
|
167
|
-
mongory_matcher_custom_context *(*build)(char *key, mongory_value *condition, void *extern_ctx); // Build an external matcher reference.
|
168
|
-
bool (*lookup)(char *key); // Lookup a matcher reference by key.
|
169
|
-
} mongory_matcher_custom_adapter;
|
170
|
-
|
171
|
-
extern mongory_matcher_custom_adapter *mongory_custom_matcher_adapter;
|
172
|
-
|
173
|
-
void mongory_custom_matcher_match_func_set(bool (*match)(void *external_matcher, mongory_value *value));
|
174
|
-
void mongory_custom_matcher_build_func_set(mongory_matcher_custom_context *(*build)(char *key, mongory_value *condition, void *extern_ctx));
|
175
|
-
void mongory_custom_matcher_lookup_func_set(bool (*lookup)(char *key));
|
176
|
-
|
177
|
-
/**
|
178
|
-
* @brief Copies a string using the Mongory library's memory pool.
|
179
|
-
*
|
180
|
-
* Allocates memory from the given pool and copies the source string into it.
|
181
|
-
* The returned string is null-terminated.
|
182
|
-
*
|
183
|
-
* @param pool The memory pool to use for allocating the new string.
|
184
|
-
* @param str The source string to copy. If NULL, returns NULL.
|
185
|
-
* @return char* A pointer to the newly allocated and copied string, or NULL
|
186
|
-
* if allocation fails or str is NULL. The caller does not own this memory
|
187
|
-
* directly; it will be freed when the pool is freed.
|
188
|
-
*/
|
189
|
-
char *mongory_string_cpy(mongory_memory_pool *pool, char *str);
|
190
|
-
|
191
|
-
/**
|
192
|
-
* @brief Copies a formatted string using the Mongory library's memory pool.
|
193
|
-
*
|
194
|
-
* Allocates memory from the given pool and copies the formatted string into it.
|
195
|
-
* The returned string is null-terminated.
|
196
|
-
*
|
197
|
-
* @param pool The memory pool to use for allocating the new string.
|
198
|
-
* @param format The format string to copy.
|
199
|
-
* @param ... The arguments to be formatted into the string.
|
200
|
-
* @return char* A pointer to the newly allocated and copied string, or NULL
|
201
|
-
* if allocation fails or format is NULL. The caller does not own this memory
|
202
|
-
* directly; it will be freed when the pool is freed.
|
203
|
-
*/
|
204
|
-
char *mongory_string_cpyf(mongory_memory_pool *pool, char *format, ...);
|
205
|
-
|
206
|
-
#endif /* MONGORY_FOUNDATIONS_CONFIG_H */
|
@@ -1,82 +0,0 @@
|
|
1
|
-
#ifndef MONGORY_ERROR
|
2
|
-
#define MONGORY_ERROR
|
3
|
-
|
4
|
-
/**
|
5
|
-
* @file error.h
|
6
|
-
* @brief Defines error types and structures for the Mongory library.
|
7
|
-
*
|
8
|
-
* This file provides an enumeration of possible error codes and a structure
|
9
|
-
* for representing error information, typically consisting of an error type
|
10
|
-
* and an associated message.
|
11
|
-
*/
|
12
|
-
|
13
|
-
#include <stdbool.h>
|
14
|
-
|
15
|
-
/**
|
16
|
-
* @def MONGORY_ERROR_TYPE_MAGIC
|
17
|
-
* @brief A magic number used in generating enum values for error types.
|
18
|
-
* Helps in creating somewhat unique integer values for the enums.
|
19
|
-
*/
|
20
|
-
#define MONGORY_ERROR_TYPE_MAGIC 107
|
21
|
-
|
22
|
-
/**
|
23
|
-
* @def MONGORY_ERROR_TYPE_MACRO
|
24
|
-
* @brief X-Macro for defining error types.
|
25
|
-
* This macro allows defining the error enum, string conversion, etc.,
|
26
|
-
* from a single list, promoting consistency.
|
27
|
-
* Each entry takes the enum name, a numeric suffix, and a string description.
|
28
|
-
*/
|
29
|
-
#define MONGORY_ERROR_TYPE_MACRO(_) \
|
30
|
-
_(MONGORY_ERROR_NONE, 10, "No Error") \
|
31
|
-
_(MONGORY_ERROR_MEMORY, 11, "Memory Allocation Error") \
|
32
|
-
_(MONGORY_ERROR_INVALID_TYPE, 12, "Invalid Type Error") \
|
33
|
-
_(MONGORY_ERROR_OUT_OF_BOUNDS, 13, "Out of Bounds Error") \
|
34
|
-
_(MONGORY_ERROR_UNSUPPORTED_OPERATION, 14, "Unsupported Operation Error") \
|
35
|
-
_(MONGORY_ERROR_INVALID_ARGUMENT, 15, "Invalid Argument Error") \
|
36
|
-
_(MONGORY_ERROR_IO, 16, "I/O Error") \
|
37
|
-
_(MONGORY_ERROR_PARSE, 17, "Parse Error") \
|
38
|
-
_(MONGORY_ERROR_UNKNOWN, 99, "Unknown Error")
|
39
|
-
|
40
|
-
/**
|
41
|
-
* @enum mongory_error_type
|
42
|
-
* @brief Enumerates the types of errors that can occur within the Mongory
|
43
|
-
* library. Values are generated using MONGORY_ERROR_TYPE_MACRO and
|
44
|
-
* MONGORY_ERROR_TYPE_MAGIC.
|
45
|
-
*/
|
46
|
-
typedef enum mongory_error_type {
|
47
|
-
#define DEFINE_ERROR_ENUM(name, num, str) name = num * MONGORY_ERROR_TYPE_MAGIC,
|
48
|
-
MONGORY_ERROR_TYPE_MACRO(DEFINE_ERROR_ENUM)
|
49
|
-
#undef DEFINE_ERROR_ENUM
|
50
|
-
} mongory_error_type;
|
51
|
-
|
52
|
-
/**
|
53
|
-
* @brief Converts a mongory_error_type enum to its string representation.
|
54
|
-
*
|
55
|
-
* @param type The error type enum value.
|
56
|
-
* @return const char* A string describing the error type. Returns "Unknown
|
57
|
-
* Error Type" if the type is not recognized.
|
58
|
-
*/
|
59
|
-
const char *mongory_error_type_to_string(enum mongory_error_type type);
|
60
|
-
|
61
|
-
/**
|
62
|
-
* @struct mongory_error
|
63
|
-
* @brief Structure to hold error information.
|
64
|
-
*
|
65
|
-
* Contains the type of error and an optional descriptive message.
|
66
|
-
* The message string is expected to be a literal or have a lifetime
|
67
|
-
* managed elsewhere (e.g., allocated from a memory pool).
|
68
|
-
*/
|
69
|
-
typedef struct mongory_error {
|
70
|
-
mongory_error_type type; /**< The type of the error. */
|
71
|
-
const char *message; /**< A descriptive message for the error. This message
|
72
|
-
might be a string literal or allocated from a
|
73
|
-
memory pool. Its lifetime should be considered
|
74
|
-
carefully. */
|
75
|
-
} mongory_error;
|
76
|
-
|
77
|
-
static mongory_error MONGORY_ALLOC_ERROR = {
|
78
|
-
.type = MONGORY_ERROR_MEMORY,
|
79
|
-
.message = "Memory Allocation Failed",
|
80
|
-
};
|
81
|
-
|
82
|
-
#endif /* MONGORY_ERROR */
|
@@ -1,95 +0,0 @@
|
|
1
|
-
#ifndef MONGORY_MEMORY_POOL
|
2
|
-
#define MONGORY_MEMORY_POOL
|
3
|
-
|
4
|
-
/**
|
5
|
-
* @file memory_pool.h
|
6
|
-
* @brief Defines the mongory_memory_pool structure and its associated
|
7
|
-
* operations.
|
8
|
-
*
|
9
|
-
* A memory pool is used for efficient memory management within the Mongory
|
10
|
-
* library. It allows for allocating blocks of memory that can be freed
|
11
|
-
* all at once when the pool itself is destroyed. This can reduce the overhead
|
12
|
-
* of individual allocations and deallocations and help prevent memory leaks.
|
13
|
-
*/
|
14
|
-
|
15
|
-
#include "mongory-core/foundations/error.h"
|
16
|
-
#include <stdbool.h>
|
17
|
-
#include <stdlib.h> // For size_t
|
18
|
-
|
19
|
-
// Forward declaration of the memory pool structure.
|
20
|
-
typedef struct mongory_memory_pool mongory_memory_pool;
|
21
|
-
|
22
|
-
#define MG_ALLOC(p, n) (p->alloc(p->ctx, n))
|
23
|
-
#define MG_ALLOC_PTR(p, t) ((t*)MG_ALLOC(p, sizeof(t)))
|
24
|
-
#define MG_ALLOC_OBJ(p, t) ((t)MG_ALLOC(p, sizeof(t)))
|
25
|
-
#define MG_ALLOC_ARY(p, t, n) ((t*)MG_ALLOC(p, sizeof(t) * (n)))
|
26
|
-
/**
|
27
|
-
* @struct mongory_memory_pool
|
28
|
-
* @brief Represents a memory pool for managing memory allocations.
|
29
|
-
*
|
30
|
-
* The pool uses a context (`ctx`) to store its internal state, which typically
|
31
|
-
* includes a list of allocated memory chunks.
|
32
|
-
* It provides function pointers for allocation, tracing (optional), and freeing
|
33
|
-
* the entire pool. An error field can store information about the last error
|
34
|
-
* encountered during pool operations.
|
35
|
-
*/
|
36
|
-
struct mongory_memory_pool {
|
37
|
-
/**
|
38
|
-
* @brief Allocates a block of memory from the pool.
|
39
|
-
* @param ctx A pointer to the pool's internal context.
|
40
|
-
* @param size The number of bytes to allocate.
|
41
|
-
* @return void* A pointer to the allocated memory block, or NULL on failure.
|
42
|
-
* Memory allocated this way is typically aligned.
|
43
|
-
*/
|
44
|
-
void *(*alloc)(void *ctx, size_t size);
|
45
|
-
|
46
|
-
/**
|
47
|
-
* @brief Traces an externally allocated memory block, associating it with the
|
48
|
-
* pool.
|
49
|
-
*
|
50
|
-
* This is useful if memory is allocated outside the pool's `alloc` function
|
51
|
-
* (e.g., by an external library) but its lifecycle should be tied to the
|
52
|
-
* pool. When the pool is freed, traced memory blocks might also be freed
|
53
|
-
* depending on the pool's implementation.
|
54
|
-
*
|
55
|
-
* @param ctx A pointer to the pool's internal context.
|
56
|
-
* @param ptr A pointer to the memory block to trace.
|
57
|
-
* @param size The size of the memory block.
|
58
|
-
*/
|
59
|
-
void (*trace)(void *ctx, void *ptr, size_t size);
|
60
|
-
|
61
|
-
/**
|
62
|
-
* @brief Resets the memory pool to its initial state.
|
63
|
-
* @param ctx A pointer to the pool's internal context.
|
64
|
-
*/
|
65
|
-
void (*reset)(void *ctx);
|
66
|
-
|
67
|
-
/**
|
68
|
-
* @brief Frees the entire memory pool, including all memory blocks allocated
|
69
|
-
* from it and any traced memory blocks (depending on implementation).
|
70
|
-
* @param self A pointer to the mongory_memory_pool instance to be freed.
|
71
|
-
*/
|
72
|
-
void (*free)(mongory_memory_pool *self);
|
73
|
-
|
74
|
-
void *ctx; /**< Pointer to the internal context/state of the memory
|
75
|
-
pool. This is managed by the pool implementation. */
|
76
|
-
mongory_error *error; /**< Pointer to a mongory_error structure. If an
|
77
|
-
operation fails (e.g., allocation), this may be set
|
78
|
-
to describe the error. The memory for this error
|
79
|
-
struct itself is typically allocated from the pool or
|
80
|
-
is a static error object. */
|
81
|
-
};
|
82
|
-
|
83
|
-
/**
|
84
|
-
* @brief Creates a new mongory_memory_pool instance.
|
85
|
-
*
|
86
|
-
* Initializes the pool structure and its internal context, preparing it for
|
87
|
-
* allocations.
|
88
|
-
*
|
89
|
-
* @return mongory_memory_pool* A pointer to the newly created memory pool, or
|
90
|
-
* NULL if creation fails (e.g., initial memory allocation for the pool's
|
91
|
-
* context failed).
|
92
|
-
*/
|
93
|
-
mongory_memory_pool *mongory_memory_pool_new();
|
94
|
-
|
95
|
-
#endif /* MONGORY_MEMORY_POOL */
|
@@ -1,108 +0,0 @@
|
|
1
|
-
#ifndef MONGORY_TABLE_H
|
2
|
-
#define MONGORY_TABLE_H
|
3
|
-
|
4
|
-
/**
|
5
|
-
* @file table.h
|
6
|
-
* @brief Defines the mongory_table structure (a hash table) and its
|
7
|
-
* associated operations.
|
8
|
-
*
|
9
|
-
* mongory_table implements a hash table mapping string keys to mongory_value
|
10
|
-
* pointers. It uses a mongory_memory_pool for its allocations and handles
|
11
|
-
* operations like get, set, delete, and iteration over key-value pairs.
|
12
|
-
* The table automatically resizes (rehashes) when its load factor is exceeded.
|
13
|
-
*/
|
14
|
-
|
15
|
-
#include "mongory-core/foundations/array.h" // Used internally by the table
|
16
|
-
#include "mongory-core/foundations/memory_pool.h"
|
17
|
-
#include "mongory-core/foundations/value.h"
|
18
|
-
#include <stdbool.h>
|
19
|
-
#include <stddef.h> // For size_t
|
20
|
-
|
21
|
-
// Forward declaration of the mongory_table structure.
|
22
|
-
struct mongory_table;
|
23
|
-
/**
|
24
|
-
* @brief Alias for `struct mongory_table`.
|
25
|
-
*/
|
26
|
-
typedef struct mongory_table mongory_table;
|
27
|
-
|
28
|
-
/**
|
29
|
-
* @brief Callback function type for iterating over key-value pairs in a table.
|
30
|
-
* @param key The current key (null-terminated string).
|
31
|
-
* @param value A pointer to the current mongory_value.
|
32
|
-
* @param acc An accumulator or context pointer passed through the iteration.
|
33
|
-
* @return bool Return true to continue iteration, false to stop.
|
34
|
-
*/
|
35
|
-
typedef bool (*mongory_table_each_pair_callback_func)(char *key, mongory_value *value, void *acc);
|
36
|
-
|
37
|
-
/**
|
38
|
-
* @brief Function pointer type for retrieving a value by its key.
|
39
|
-
* @param self A pointer to the mongory_table instance.
|
40
|
-
* @param key The null-terminated string key.
|
41
|
-
* @return mongory_value* A pointer to the mongory_value associated with the
|
42
|
-
* key, or NULL if the key is not found.
|
43
|
-
*/
|
44
|
-
typedef mongory_value *(*mongory_table_get_func)(mongory_table *self, char *key);
|
45
|
-
|
46
|
-
/**
|
47
|
-
* @brief Function pointer type for setting (adding or updating) a key-value
|
48
|
-
* pair.
|
49
|
-
* @param self A pointer to the mongory_table instance.
|
50
|
-
* @param key The null-terminated string key. The table will make its own copy
|
51
|
-
* of this key.
|
52
|
-
* @param value A pointer to the mongory_value to associate with the key.
|
53
|
-
* @return bool True if the operation was successful, false otherwise (e.g.,
|
54
|
-
* memory allocation failure).
|
55
|
-
*/
|
56
|
-
typedef bool (*mongory_table_set_func)(mongory_table *self, char *key, mongory_value *value);
|
57
|
-
|
58
|
-
/**
|
59
|
-
* @brief Function pointer type for iterating over all key-value pairs in the
|
60
|
-
* table.
|
61
|
-
* @param self A pointer to the mongory_table instance.
|
62
|
-
* @param acc An accumulator or context pointer to be passed to the callback.
|
63
|
-
* @param callback The function to be executed for each key-value pair.
|
64
|
-
* @return bool True if the iteration completed fully, false if it was stopped
|
65
|
-
* by a callback.
|
66
|
-
*/
|
67
|
-
typedef bool (*mongory_table_each_func)(mongory_table *self, void *acc, mongory_table_each_pair_callback_func callback);
|
68
|
-
|
69
|
-
/**
|
70
|
-
* @brief Function pointer type for deleting a key-value pair from the table.
|
71
|
-
* @param self A pointer to the mongory_table instance.
|
72
|
-
* @param key The null-terminated string key of the pair to delete.
|
73
|
-
* @return bool True if the key was found and deleted, false otherwise.
|
74
|
-
*/
|
75
|
-
typedef bool (*mongory_table_del_func)(mongory_table *self, char *key);
|
76
|
-
|
77
|
-
/**
|
78
|
-
* @brief Creates a new mongory_table instance.
|
79
|
-
*
|
80
|
-
* Initializes the hash table with a default capacity and associates it with the
|
81
|
-
* provided memory pool for all internal allocations.
|
82
|
-
*
|
83
|
-
* @param pool A pointer to the mongory_memory_pool to be used for allocations.
|
84
|
-
* @return mongory_table* A pointer to the newly created mongory_table, or NULL
|
85
|
-
* if creation fails (e.g., memory allocation failure).
|
86
|
-
*/
|
87
|
-
mongory_table *mongory_table_new(mongory_memory_pool *pool);
|
88
|
-
|
89
|
-
/**
|
90
|
-
* @struct mongory_table
|
91
|
-
* @brief Represents a hash table mapping string keys to mongory_value pointers.
|
92
|
-
*
|
93
|
-
* Provides function pointers for common table operations, similar to an
|
94
|
-
* object-oriented interface. The `count` field indicates the number of
|
95
|
-
* key-value pairs currently in the table and should be treated as read-only by
|
96
|
-
* users of the table.
|
97
|
-
*/
|
98
|
-
struct mongory_table {
|
99
|
-
mongory_memory_pool *pool; /**< The memory pool used for allocations. */
|
100
|
-
size_t count; /**< Read-only. The current number of key-value pairs in the
|
101
|
-
table. */
|
102
|
-
mongory_table_get_func get; /**< Function to get a value by key. */
|
103
|
-
mongory_table_each_func each; /**< Function to iterate over key-value pairs. */
|
104
|
-
mongory_table_set_func set; /**< Function to set a key-value pair. */
|
105
|
-
mongory_table_del_func del; /**< Function to delete a key-value pair. */
|
106
|
-
};
|
107
|
-
|
108
|
-
#endif /* MONGORY_TABLE_H */
|