Skip to content

Commit 7a878af

Browse files
hasumikinkddnewton
authored andcommitted
Make alloc interface replaceable
- Add `x` prefix to malloc, calloc, realloc, and free (eg: malloc -> xmalloc) - By default, they are replaced with stdlib's functions at build - You can use custom functions by defining `PRISM_CUSTOM_ALLOCATOR` macro
1 parent 663c8b7 commit 7a878af

File tree

16 files changed

+117
-84
lines changed

16 files changed

+117
-84
lines changed

docs/build_system.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ For instance, you can build a static library `libprism.a` targeting the Arm Cort
8282

8383
The build process internally looks up `_POSIX_MAPPED_FILES` and `_WIN32` macros to determine whether the functions of the memory map are available on the target platform.
8484

85+
### Building prism with custom memory allocator
86+
87+
If you need to use memory allocation functions implemented outside of the standard library, follow these steps:
88+
89+
* Add `-D PRISM_CUSTOM_ALLOCATOR` to the build options
90+
* Additionally, include `-I [path/to/custom_allocator]` where your `custom_allocator.h` is located
91+
* Link the implementation of `custom_allocator.c` that contains functions declared in `custom_allocator.h`
92+
93+
For further clarity, refer to `include/prism/defines.h`.
94+
8595
### Building prism from source as a C library
8696

8797
All of the source files match `src/**/*.c` and all of the headers match `include/**/*.h`.

ext/prism/extension.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,15 +310,15 @@ dump(int argc, VALUE *argv, VALUE self) {
310310

311311
#ifdef PRISM_DEBUG_MODE_BUILD
312312
size_t length = pm_string_length(&input);
313-
char* dup = malloc(length);
313+
char* dup = xmalloc(length);
314314
memcpy(dup, pm_string_source(&input), length);
315315
pm_string_constant_init(&input, dup, length);
316316
#endif
317317

318318
VALUE value = dump_input(&input, &options);
319319

320320
#ifdef PRISM_DEBUG_MODE_BUILD
321-
free(dup);
321+
xfree(dup);
322322
#endif
323323

324324
pm_string_free(&input);
@@ -733,15 +733,15 @@ parse(int argc, VALUE *argv, VALUE self) {
733733

734734
#ifdef PRISM_DEBUG_MODE_BUILD
735735
size_t length = pm_string_length(&input);
736-
char* dup = malloc(length);
736+
char* dup = xmalloc(length);
737737
memcpy(dup, pm_string_source(&input), length);
738738
pm_string_constant_init(&input, dup, length);
739739
#endif
740740

741741
VALUE value = parse_input(&input, &options);
742742

743743
#ifdef PRISM_DEBUG_MODE_BUILD
744-
free(dup);
744+
xfree(dup);
745745
#endif
746746

747747
pm_string_free(&input);

include/prism/defines.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,27 @@
125125
# define isinf(x) (sizeof(x) == sizeof(float) ? !_finitef(x) : !_finite(x))
126126
#endif
127127

128+
/**
129+
* If you build prism with a custom allocator, configure it with "-D PRISM_CUSTOM_ALLOCATOR"
130+
* to use your own allocator that defines xmalloc, xrealloc, xcalloc, and xfree.
131+
* For example, your `custom_allocator.h` file could look like this:
132+
* ```
133+
* #ifndef PRISM_CUSTOM_ALLOCATOR_H
134+
* #define PRISM_CUSTOM_ALLOCATOR_H
135+
* #define xmalloc my_malloc
136+
* #define xrealloc my_realloc
137+
* #define xcalloc my_calloc
138+
* #define xfree my_free
139+
* #endif
140+
* ```
141+
*/
142+
#ifdef PRISM_CUSTOM_ALLOCATOR
143+
# include "custom_allocator.h"
144+
#else
145+
# define xmalloc malloc
146+
# define xrealloc realloc
147+
# define xcalloc calloc
148+
# define xfree free
149+
#endif
150+
128151
#endif

include/prism/util/pm_list.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* } pm_int_node_t;
3434
*
3535
* pm_list_t list = { 0 };
36-
* pm_int_node_t *node = malloc(sizeof(pm_int_node_t));
36+
* pm_int_node_t *node = xmalloc(sizeof(pm_int_node_t));
3737
* node->value = 5;
3838
*
3939
* pm_list_append(&list, &node->node);

src/diagnostic.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ pm_diagnostic_level(pm_diagnostic_id_t diag_id) {
336336
*/
337337
bool
338338
pm_diagnostic_list_append(pm_list_t *list, const uint8_t *start, const uint8_t *end, pm_diagnostic_id_t diag_id) {
339-
pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) calloc(sizeof(pm_diagnostic_t), 1);
339+
pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) xcalloc(sizeof(pm_diagnostic_t), 1);
340340
if (diagnostic == NULL) return false;
341341

342342
*diagnostic = (pm_diagnostic_t) {
@@ -367,15 +367,15 @@ pm_diagnostic_list_append_format(pm_list_t *list, const uint8_t *start, const ui
367367
return false;
368368
}
369369

370-
pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) calloc(sizeof(pm_diagnostic_t), 1);
370+
pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) xcalloc(sizeof(pm_diagnostic_t), 1);
371371
if (diagnostic == NULL) {
372372
return false;
373373
}
374374

375375
size_t length = (size_t) (result + 1);
376-
char *message = (char *) malloc(length);
376+
char *message = (char *) xmalloc(length);
377377
if (message == NULL) {
378-
free(diagnostic);
378+
xfree(diagnostic);
379379
return false;
380380
}
381381

@@ -404,8 +404,8 @@ pm_diagnostic_list_free(pm_list_t *list) {
404404
while (node != NULL) {
405405
pm_diagnostic_t *next = (pm_diagnostic_t *) node->node.next;
406406

407-
if (node->owned) free((void *) node->message);
408-
free(node);
407+
if (node->owned) xfree((void *) node->message);
408+
xfree(node);
409409

410410
node = next;
411411
}

src/options.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ pm_options_version_set(pm_options_t *options, const char *version, size_t length
8686
PRISM_EXPORTED_FUNCTION bool
8787
pm_options_scopes_init(pm_options_t *options, size_t scopes_count) {
8888
options->scopes_count = scopes_count;
89-
options->scopes = calloc(scopes_count, sizeof(pm_options_scope_t));
89+
options->scopes = xcalloc(scopes_count, sizeof(pm_options_scope_t));
9090
return options->scopes != NULL;
9191
}
9292

@@ -105,7 +105,7 @@ pm_options_scope_get(const pm_options_t *options, size_t index) {
105105
PRISM_EXPORTED_FUNCTION bool
106106
pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count) {
107107
scope->locals_count = locals_count;
108-
scope->locals = calloc(locals_count, sizeof(pm_string_t));
108+
scope->locals = xcalloc(locals_count, sizeof(pm_string_t));
109109
return scope->locals != NULL;
110110
}
111111

@@ -132,10 +132,10 @@ pm_options_free(pm_options_t *options) {
132132
pm_string_free(&scope->locals[local_index]);
133133
}
134134

135-
free(scope->locals);
135+
xfree(scope->locals);
136136
}
137137

138-
free(options->scopes);
138+
xfree(options->scopes);
139139
}
140140

141141
/**

0 commit comments

Comments
 (0)