summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHASUMI Hitoshi <[email protected]>2024-02-27 14:25:22 +0900
committergit <[email protected]>2024-03-04 16:40:23 +0000
commitc4bd6da2988ecdf3e6d00be78e58a4eba6192bed (patch)
treef5cc0976ae7ff16bac9913f02ec4d32adef41160
parent61ea202f8b10060e000f79a936e5e608eacb50ee (diff)
[ruby/prism] 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 https://github.com/ruby/prism/commit/7a878af619
-rw-r--r--prism/defines.h23
-rw-r--r--prism/diagnostic.c12
-rw-r--r--prism/extension.c8
-rw-r--r--prism/options.c8
-rw-r--r--prism/prism.c74
-rw-r--r--prism/templates/ext/prism/api_node.c.erb8
-rw-r--r--prism/templates/src/node.c.erb6
-rw-r--r--prism/util/pm_buffer.c6
-rw-r--r--prism/util/pm_constant_pool.c16
-rw-r--r--prism/util/pm_integer.c10
-rw-r--r--prism/util/pm_list.c2
-rw-r--r--prism/util/pm_list.h2
-rw-r--r--prism/util/pm_newline_list.c8
-rw-r--r--prism/util/pm_string.c4
-rw-r--r--prism/util/pm_string_list.c4
15 files changed, 107 insertions, 84 deletions
diff --git a/prism/defines.h b/prism/defines.h
index 6c09f108c2..00ed80209a 100644
--- a/prism/defines.h
+++ b/prism/defines.h
@@ -125,4 +125,27 @@
# define isinf(x) (sizeof(x) == sizeof(float) ? !_finitef(x) : !_finite(x))
#endif
+/**
+ * If you build prism with a custom allocator, configure it with "-D PRISM_CUSTOM_ALLOCATOR"
+ * to use your own allocator that defines xmalloc, xrealloc, xcalloc, and xfree.
+ * For example, your `custom_allocator.h` file could look like this:
+ * ```
+ * #ifndef PRISM_CUSTOM_ALLOCATOR_H
+ * #define PRISM_CUSTOM_ALLOCATOR_H
+ * #define xmalloc my_malloc
+ * #define xrealloc my_realloc
+ * #define xcalloc my_calloc
+ * #define xfree my_free
+ * #endif
+ * ```
+ */
+#ifdef PRISM_CUSTOM_ALLOCATOR
+# include "custom_allocator.h"
+#else
+# define xmalloc malloc
+# define xrealloc realloc
+# define xcalloc calloc
+# define xfree free
+#endif
+
#endif
diff --git a/prism/diagnostic.c b/prism/diagnostic.c
index dc0836d67e..09ac9ee330 100644
--- a/prism/diagnostic.c
+++ b/prism/diagnostic.c
@@ -336,7 +336,7 @@ pm_diagnostic_level(pm_diagnostic_id_t diag_id) {
*/
bool
pm_diagnostic_list_append(pm_list_t *list, const uint8_t *start, const uint8_t *end, pm_diagnostic_id_t diag_id) {
- pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) calloc(sizeof(pm_diagnostic_t), 1);
+ pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) xcalloc(sizeof(pm_diagnostic_t), 1);
if (diagnostic == NULL) return false;
*diagnostic = (pm_diagnostic_t) {
@@ -367,15 +367,15 @@ pm_diagnostic_list_append_format(pm_list_t *list, const uint8_t *start, const ui
return false;
}
- pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) calloc(sizeof(pm_diagnostic_t), 1);
+ pm_diagnostic_t *diagnostic = (pm_diagnostic_t *) xcalloc(sizeof(pm_diagnostic_t), 1);
if (diagnostic == NULL) {
return false;
}
size_t length = (size_t) (result + 1);
- char *message = (char *) malloc(length);
+ char *message = (char *) xmalloc(length);
if (message == NULL) {
- free(diagnostic);
+ xfree(diagnostic);
return false;
}
@@ -404,8 +404,8 @@ pm_diagnostic_list_free(pm_list_t *list) {
while (node != NULL) {
pm_diagnostic_t *next = (pm_diagnostic_t *) node->node.next;
- if (node->owned) free((void *) node->message);
- free(node);
+ if (node->owned) xfree((void *) node->message);
+ xfree(node);
node = next;
}
diff --git a/prism/extension.c b/prism/extension.c
index 81a60e44a9..c14e5165db 100644
--- a/prism/extension.c
+++ b/prism/extension.c
@@ -310,7 +310,7 @@ dump(int argc, VALUE *argv, VALUE self) {
#ifdef PRISM_DEBUG_MODE_BUILD
size_t length = pm_string_length(&input);
- char* dup = malloc(length);
+ char* dup = xmalloc(length);
memcpy(dup, pm_string_source(&input), length);
pm_string_constant_init(&input, dup, length);
#endif
@@ -318,7 +318,7 @@ dump(int argc, VALUE *argv, VALUE self) {
VALUE value = dump_input(&input, &options);
#ifdef PRISM_DEBUG_MODE_BUILD
- free(dup);
+ xfree(dup);
#endif
pm_string_free(&input);
@@ -733,7 +733,7 @@ parse(int argc, VALUE *argv, VALUE self) {
#ifdef PRISM_DEBUG_MODE_BUILD
size_t length = pm_string_length(&input);
- char* dup = malloc(length);
+ char* dup = xmalloc(length);
memcpy(dup, pm_string_source(&input), length);
pm_string_constant_init(&input, dup, length);
#endif
@@ -741,7 +741,7 @@ parse(int argc, VALUE *argv, VALUE self) {
VALUE value = parse_input(&input, &options);
#ifdef PRISM_DEBUG_MODE_BUILD
- free(dup);
+ xfree(dup);
#endif
pm_string_free(&input);
diff --git a/prism/options.c b/prism/options.c
index ca0f150107..d94cfad550 100644
--- a/prism/options.c
+++ b/prism/options.c
@@ -86,7 +86,7 @@ pm_options_version_set(pm_options_t *options, const char *version, size_t length
PRISM_EXPORTED_FUNCTION bool
pm_options_scopes_init(pm_options_t *options, size_t scopes_count) {
options->scopes_count = scopes_count;
- options->scopes = calloc(scopes_count, sizeof(pm_options_scope_t));
+ options->scopes = xcalloc(scopes_count, sizeof(pm_options_scope_t));
return options->scopes != NULL;
}
@@ -105,7 +105,7 @@ pm_options_scope_get(const pm_options_t *options, size_t index) {
PRISM_EXPORTED_FUNCTION bool
pm_options_scope_init(pm_options_scope_t *scope, size_t locals_count) {
scope->locals_count = locals_count;
- scope->locals = calloc(locals_count, sizeof(pm_string_t));
+ scope->locals = xcalloc(locals_count, sizeof(pm_string_t));
return scope->locals != NULL;
}
@@ -132,10 +132,10 @@ pm_options_free(pm_options_t *options) {
pm_string_free(&scope->locals[local_index]);
}
- free(scope->locals);
+ xfree(scope->locals);
}
- free(options->scopes);
+ xfree(options->scopes);
}
/**
diff --git a/prism/prism.c b/prism/prism.c
index b25bea45c0..e2fe3d28a9 100644
--- a/prism/prism.c
+++ b/prism/prism.c
@@ -226,7 +226,7 @@ lex_mode_push(pm_parser_t *parser, pm_lex_mode_t lex_mode) {
parser->lex_modes.index++;
if (parser->lex_modes.index > PM_LEX_STACK_SIZE - 1) {
- parser->lex_modes.current = (pm_lex_mode_t *) malloc(sizeof(pm_lex_mode_t));
+ parser->lex_modes.current = (pm_lex_mode_t *) xmalloc(sizeof(pm_lex_mode_t));
if (parser->lex_modes.current == NULL) return false;
*parser->lex_modes.current = lex_mode;
@@ -387,7 +387,7 @@ lex_mode_pop(pm_parser_t *parser) {
} else {
parser->lex_modes.index--;
pm_lex_mode_t *prev = parser->lex_modes.current->prev;
- free(parser->lex_modes.current);
+ xfree(parser->lex_modes.current);
parser->lex_modes.current = prev;
}
}
@@ -1063,7 +1063,7 @@ parse_decimal_number(pm_parser_t *parser, const uint8_t *start, const uint8_t *e
assert(diff > 0 && ((unsigned long) diff < SIZE_MAX));
size_t length = (size_t) diff;
- char *digits = calloc(length + 1, sizeof(char));
+ char *digits = xcalloc(length + 1, sizeof(char));
memcpy(digits, start, length);
digits[length] = '\0';
@@ -1076,7 +1076,7 @@ parse_decimal_number(pm_parser_t *parser, const uint8_t *start, const uint8_t *e
value = UINT32_MAX;
}
- free(digits);
+ xfree(digits);
if (value > UINT32_MAX) {
pm_parser_err(parser, start, end, PM_ERR_INVALID_NUMBER_DECIMAL);
@@ -1138,7 +1138,7 @@ pm_statements_node_body_length(pm_statements_node_t *node);
*/
static inline void *
pm_alloc_node(PRISM_ATTRIBUTE_UNUSED pm_parser_t *parser, size_t size) {
- void *memory = calloc(1, size);
+ void *memory = xcalloc(1, size);
if (memory == NULL) {
fprintf(stderr, "Failed to allocate %d bytes\n", (int) size);
abort();
@@ -2120,7 +2120,7 @@ pm_call_write_read_name_init(pm_parser_t *parser, pm_constant_id_t *read_name, p
if (write_constant->length > 0) {
size_t length = write_constant->length - 1;
- void *memory = malloc(length);
+ void *memory = xmalloc(length);
memcpy(memory, write_constant->start, length);
*read_name = pm_constant_pool_insert_owned(&parser->constant_pool, (uint8_t *) memory, length);
@@ -2162,7 +2162,7 @@ pm_call_and_write_node_create(pm_parser_t *parser, pm_call_node_t *target, const
// Here we're going to free the target, since it is no longer necessary.
// However, we don't want to call `pm_node_destroy` because we want to keep
// around all of its children since we just reused them.
- free(target);
+ xfree(target);
return node;
}
@@ -2197,7 +2197,7 @@ pm_index_and_write_node_create(pm_parser_t *parser, pm_call_node_t *target, cons
// Here we're going to free the target, since it is no longer necessary.
// However, we don't want to call `pm_node_destroy` because we want to keep
// around all of its children since we just reused them.
- free(target);
+ xfree(target);
return node;
}
@@ -2234,7 +2234,7 @@ pm_call_operator_write_node_create(pm_parser_t *parser, pm_call_node_t *target,
// Here we're going to free the target, since it is no longer necessary.
// However, we don't want to call `pm_node_destroy` because we want to keep
// around all of its children since we just reused them.
- free(target);
+ xfree(target);
return node;
}
@@ -2269,7 +2269,7 @@ pm_index_operator_write_node_create(pm_parser_t *parser, pm_call_node_t *target,
// Here we're going to free the target, since it is no longer necessary.
// However, we don't want to call `pm_node_destroy` because we want to keep
// around all of its children since we just reused them.
- free(target);
+ xfree(target);
return node;
}
@@ -2306,7 +2306,7 @@ pm_call_or_write_node_create(pm_parser_t *parser, pm_call_node_t *target, const
// Here we're going to free the target, since it is no longer necessary.
// However, we don't want to call `pm_node_destroy` because we want to keep
// around all of its children since we just reused them.
- free(target);
+ xfree(target);
return node;
}
@@ -2341,7 +2341,7 @@ pm_index_or_write_node_create(pm_parser_t *parser, pm_call_node_t *target, const
// Here we're going to free the target, since it is no longer necessary.
// However, we don't want to call `pm_node_destroy` because we want to keep
// around all of its children since we just reused them.
- free(target);
+ xfree(target);
return node;
}
@@ -2369,7 +2369,7 @@ pm_call_target_node_create(pm_parser_t *parser, pm_call_node_t *target) {
// Here we're going to free the target, since it is no longer necessary.
// However, we don't want to call `pm_node_destroy` because we want to keep
// around all of its children since we just reused them.
- free(target);
+ xfree(target);
return node;
}
@@ -2399,7 +2399,7 @@ pm_index_target_node_create(pm_parser_t *parser, pm_call_node_t *target) {
// Here we're going to free the target, since it is no longer necessary.
// However, we don't want to call `pm_node_destroy` because we want to keep
// around all of its children since we just reused them.
- free(target);
+ xfree(target);
return node;
}
@@ -3206,7 +3206,7 @@ pm_double_parse(pm_parser_t *parser, const pm_token_t *token) {
// First, get a buffer of the content.
size_t length = (size_t) diff;
- char *buffer = malloc(sizeof(char) * (length + 1));
+ char *buffer = xmalloc(sizeof(char) * (length + 1));
memcpy((void *) buffer, token->start, length);
// Next, handle underscores by removing them from the buffer.
@@ -3232,7 +3232,7 @@ pm_double_parse(pm_parser_t *parser, const pm_token_t *token) {
// is in a valid format. However it's good to be safe.
if ((eptr != buffer + length) || (errno != 0 && errno != ERANGE)) {
PM_PARSER_ERR_TOKEN_FORMAT_CONTENT(parser, (*token), PM_ERR_FLOAT_PARSE);
- free((void *) buffer);
+ xfree((void *) buffer);
return 0.0;
}
@@ -3255,7 +3255,7 @@ pm_double_parse(pm_parser_t *parser, const pm_token_t *token) {
}
// Finally we can free the buffer and return the value.
- free((void *) buffer);
+ xfree((void *) buffer);
return value;
}
@@ -4967,7 +4967,7 @@ pm_multi_write_node_create(pm_parser_t *parser, pm_multi_target_node_t *target,
// Explicitly do not call pm_node_destroy here because we want to keep
// around all of the information within the MultiWriteNode node.
- free(target);
+ xfree(target);
return node;
}
@@ -6080,7 +6080,7 @@ pm_string_node_to_symbol_node(pm_parser_t *parser, pm_string_node_t *node, const
// We are explicitly _not_ using pm_node_destroy here because we don't want
// to trash the unescaped string. We could instead copy the string if we
// know that it is owned, but we're taking the fast path for now.
- free(node);
+ xfree(node);
return new_node;
}
@@ -6112,7 +6112,7 @@ pm_symbol_node_to_string_node(pm_parser_t *parser, pm_symbol_node_t *node) {
// We are explicitly _not_ using pm_node_destroy here because we don't want
// to trash the unescaped string. We could instead copy the string if we
// know that it is owned, but we're taking the fast path for now.
- free(node);
+ xfree(node);
return new_node;
}
@@ -6502,7 +6502,7 @@ pm_yield_node_create(pm_parser_t *parser, const pm_token_t *keyword, const pm_lo
*/
static bool
pm_parser_scope_push(pm_parser_t *parser, bool closed) {
- pm_scope_t *scope = (pm_scope_t *) malloc(sizeof(pm_scope_t));
+ pm_scope_t *scope = (pm_scope_t *) xmalloc(sizeof(pm_scope_t));
if (scope == NULL) return false;
*scope = (pm_scope_t) {
@@ -6741,7 +6741,7 @@ static void
pm_parser_scope_pop(pm_parser_t *parser) {
pm_scope_t *scope = parser->current_scope;
parser->current_scope = scope->previous;
- free(scope);
+ xfree(scope);
}
/******************************************************************************/
@@ -7074,7 +7074,7 @@ parser_lex_magic_comment(pm_parser_t *parser, bool semantic_token_seen) {
pm_string_shared_init(&key, key_start, key_end);
} else {
size_t width = (size_t) (key_end - key_start);
- uint8_t *buffer = malloc(width);
+ uint8_t *buffer = xmalloc(width);
if (buffer == NULL) break;
memcpy(buffer, key_start, width);
@@ -7116,7 +7116,7 @@ parser_lex_magic_comment(pm_parser_t *parser, bool semantic_token_seen) {
// Allocate a new magic comment node to append to the parser's list.
pm_magic_comment_t *magic_comment;
- if ((magic_comment = (pm_magic_comment_t *) calloc(sizeof(pm_magic_comment_t), 1)) != NULL) {
+ if ((magic_comment = (pm_magic_comment_t *) xcalloc(sizeof(pm_magic_comment_t), 1)) != NULL) {
magic_comment->key_start = key_start;
magic_comment->value_start = value_start;
magic_comment->key_length = (uint32_t) key_length;
@@ -7210,7 +7210,7 @@ context_recoverable(const pm_parser_t *parser, pm_token_t *token) {
static bool
context_push(pm_parser_t *parser, pm_context_t context) {
- pm_context_node_t *context_node = (pm_context_node_t *) malloc(sizeof(pm_context_node_t));
+ pm_context_node_t *context_node = (pm_context_node_t *) xmalloc(sizeof(pm_context_node_t));
if (context_node == NULL) return false;
*context_node = (pm_context_node_t) { .context = context, .prev = NULL };
@@ -7228,7 +7228,7 @@ context_push(pm_parser_t *parser, pm_context_t context) {
static void
context_pop(pm_parser_t *parser) {
pm_context_node_t *prev = parser->current_context->prev;
- free(parser->current_context);
+ xfree(parser->current_context);
parser->current_context = prev;
}
@@ -8555,7 +8555,7 @@ parser_lex_callback(pm_parser_t *parser) {
*/
static inline pm_comment_t *
parser_comment(pm_parser_t *parser, pm_comment_type_t type) {
- pm_comment_t *comment = (pm_comment_t *) calloc(sizeof(pm_comment_t), 1);
+ pm_comment_t *comment = (pm_comment_t *) xcalloc(sizeof(pm_comment_t), 1);
if (comment == NULL) return NULL;
*comment = (pm_comment_t) {
@@ -11397,7 +11397,7 @@ parse_write_name(pm_parser_t *parser, pm_constant_id_t *name_field) {
// append an =.
pm_constant_t *constant = pm_constant_pool_id_to_constant(&parser->constant_pool, *name_field);
size_t length = constant->length;
- uint8_t *name = calloc(length + 1, sizeof(uint8_t));
+ uint8_t *name = xcalloc(length + 1, sizeof(uint8_t));
if (name == NULL) return;
memcpy(name, constant->start, length);
@@ -14141,7 +14141,7 @@ parse_pattern_hash(pm_parser_t *parser, pm_node_t *first_node) {
}
pm_hash_pattern_node_t *node = pm_hash_pattern_node_node_list_create(parser, &assocs, rest);
- free(assocs.nodes);
+ xfree(assocs.nodes);
return node;
}
@@ -14576,7 +14576,7 @@ parse_pattern(pm_parser_t *parser, bool top_pattern, pm_diagnostic_id_t diag_id)
node = (pm_node_t *) pm_array_pattern_node_node_list_create(parser, &nodes);
}
- free(nodes.nodes);
+ xfree(nodes.nodes);
} else if (leading_rest) {
// Otherwise, if we parsed a single splat pattern, then we know we have an
// array pattern, so we can go ahead and create that node.
@@ -17355,7 +17355,7 @@ parse_regular_expression_named_captures(pm_parser_t *parser, const pm_string_t *
// in which case we need to copy it out into a new string.
location = call->receiver->location;
- void *memory = malloc(length);
+ void *memory = xmalloc(length);
if (memory == NULL) abort();
memcpy(memory, source, length);
@@ -17843,7 +17843,7 @@ parse_expression_infix(pm_parser_t *parser, pm_node_t *node, pm_binding_power_t
}
if (!interpolated && total_length > 0) {
- void *memory = malloc(total_length);
+ void *memory = xmalloc(total_length);
if (!memory) abort();
uint8_t *cursor = memory;
@@ -18519,7 +18519,7 @@ pm_parser_init(pm_parser_t *parser, const uint8_t *source, size_t size, const pm
const uint8_t *source = pm_string_source(local);
size_t length = pm_string_length(local);
- void *allocated = malloc(length);
+ void *allocated = xmalloc(length);
if (allocated == NULL) continue;
memcpy(allocated, source, length);
@@ -18566,7 +18566,7 @@ pm_comment_list_free(pm_list_t *list) {
next = node->next;
pm_comment_t *comment = (pm_comment_t *) node;
- free(comment);
+ xfree(comment);
}
}
@@ -18581,7 +18581,7 @@ pm_magic_comment_list_free(pm_list_t *list) {
next = node->next;
pm_magic_comment_t *magic_comment = (pm_magic_comment_t *) node;
- free(magic_comment);
+ xfree(magic_comment);
}
}
@@ -18732,7 +18732,7 @@ typedef struct {
static inline pm_error_t *
pm_parser_errors_format_sort(const pm_parser_t *parser, const pm_list_t *error_list, const pm_newline_list_t *newline_list) {
- pm_error_t *errors = calloc(error_list->size, sizeof(pm_error_t));
+ pm_error_t *errors = xcalloc(error_list->size, sizeof(pm_error_t));
if (errors == NULL) return NULL;
int32_t start_line = parser->start_line;
@@ -18989,7 +18989,7 @@ pm_parser_errors_format(const pm_parser_t *parser, pm_buffer_t *buffer, bool col
}
// Finally, we'll free the array of errors that we allocated.
- free(errors);
+ xfree(errors);
}
#undef PM_COLOR_GRAY
diff --git a/prism/templates/ext/prism/api_node.c.erb b/prism/templates/ext/prism/api_node.c.erb
index 1e7663ac85..d39952a9b3 100644
--- a/prism/templates/ext/prism/api_node.c.erb
+++ b/prism/templates/ext/prism/api_node.c.erb
@@ -76,7 +76,7 @@ typedef struct pm_node_stack_node {
static void
pm_node_stack_push(pm_node_stack_node_t **stack, const pm_node_t *visit) {
- pm_node_stack_node_t *node = malloc(sizeof(pm_node_stack_node_t));
+ pm_node_stack_node_t *node = xmalloc(sizeof(pm_node_stack_node_t));
node->prev = *stack;
node->visit = visit;
node->visited = false;
@@ -89,14 +89,14 @@ pm_node_stack_pop(pm_node_stack_node_t **stack) {
const pm_node_t *visit = current->visit;
*stack = current->prev;
- free(current);
+ xfree(current);
return visit;
}
VALUE
pm_ast_new(const pm_parser_t *parser, const pm_node_t *node, rb_encoding *encoding, VALUE source) {
- ID *constants = calloc(parser->constant_pool.size, sizeof(ID));
+ ID *constants = xcalloc(parser->constant_pool.size, sizeof(ID));
for (uint32_t index = 0; index < parser->constant_pool.size; index++) {
pm_constant_t *constant = &parser->constant_pool.constants[index];
@@ -235,7 +235,7 @@ pm_ast_new(const pm_parser_t *parser, const pm_node_t *node, rb_encoding *encodi
}
VALUE result = rb_ary_pop(value_stack);
- free(constants);
+ xfree(constants);
return result;
}
diff --git a/prism/templates/src/node.c.erb b/prism/templates/src/node.c.erb
index 7d2a9f58cb..419cb48bf0 100644
--- a/prism/templates/src/node.c.erb
+++ b/prism/templates/src/node.c.erb
@@ -26,7 +26,7 @@ bool
pm_node_list_grow(pm_node_list_t *list) {
if (list->size == list->capacity) {
list->capacity = list->capacity == 0 ? 4 : list->capacity * 2;
- list->nodes = (pm_node_t **) realloc(list->nodes, sizeof(pm_node_t *) * list->capacity);
+ list->nodes = (pm_node_t **) xrealloc(list->nodes, sizeof(pm_node_t *) * list->capacity);
return list->nodes != NULL;
}
return true;
@@ -60,7 +60,7 @@ pm_node_list_prepend(pm_node_list_t *list, pm_node_t *node) {
void
pm_node_list_free(pm_node_list_t *list) {
if (list->capacity > 0) {
- free(list->nodes);
+ xfree(list->nodes);
*list = (pm_node_list_t) { 0 };
}
}
@@ -123,7 +123,7 @@ pm_node_destroy(pm_parser_t *parser, pm_node_t *node) {
assert(false && "unreachable");
break;
}
- free(node);
+ xfree(node);
}
static void
diff --git a/prism/util/pm_buffer.c b/prism/util/pm_buffer.c
index 048bc97363..87f79ddd2c 100644
--- a/prism/util/pm_buffer.c
+++ b/prism/util/pm_buffer.c
@@ -16,7 +16,7 @@ pm_buffer_init_capacity(pm_buffer_t *buffer, size_t capacity) {
buffer->length = 0;
buffer->capacity = capacity;
- buffer->value = (char *) malloc(capacity);
+ buffer->value = (char *) xmalloc(capacity);
return buffer->value != NULL;
}
@@ -60,7 +60,7 @@ pm_buffer_append_length(pm_buffer_t *buffer, size_t length) {
buffer->capacity *= 2;
}
- buffer->value = realloc(buffer->value, buffer->capacity);
+ buffer->value = xrealloc(buffer->value, buffer->capacity);
if (buffer->value == NULL) return false;
}
@@ -288,5 +288,5 @@ pm_buffer_rstrip(pm_buffer_t *buffer) {
*/
void
pm_buffer_free(pm_buffer_t *buffer) {
- free(buffer->value);
+ xfree(buffer->value);
}
diff --git a/prism/util/pm_constant_pool.c b/prism/util/pm_constant_pool.c
index 0436086793..5f3b0e92c8 100644
--- a/prism/util/pm_constant_pool.c
+++ b/prism/util/pm_constant_pool.c
@@ -51,7 +51,7 @@ pm_constant_id_list_memsize(pm_constant_id_list_t *list) {
void
pm_constant_id_list_free(pm_constant_id_list_t *list) {
if (list->ids != NULL) {
- free(list->ids);
+ xfree(list->ids);
}
}
@@ -111,7 +111,7 @@ pm_constant_pool_resize(pm_constant_pool_t *pool) {
const uint32_t mask = next_capacity - 1;
const size_t element_size = sizeof(pm_constant_pool_bucket_t) + sizeof(pm_constant_t);
- void *next = calloc(next_capacity, element_size);
+ void *next = xcalloc(next_capacity, element_size);
if (next == NULL) return false;
pm_constant_pool_bucket_t *next_buckets = next;
@@ -145,7 +145,7 @@ pm_constant_pool_resize(pm_constant_pool_t *pool) {
// pool->constants and pool->buckets are allocated out of the same chunk
// of memory, with the buckets coming first.
- free(pool->buckets);
+ xfree(pool->buckets);
pool->constants = next_constants;
pool->buckets = next_buckets;
pool->capacity = next_capacity;
@@ -162,7 +162,7 @@ pm_constant_pool_init(pm_constant_pool_t *pool, uint32_t capacity) {
capacity = next_power_of_two(capacity);
const size_t element_size = sizeof(pm_constant_pool_bucket_t) + sizeof(pm_constant_t);
- void *memory = calloc(capacity, element_size);
+ void *memory = xcalloc(capacity, element_size);
if (memory == NULL) return false;
pool->buckets = memory;
@@ -237,12 +237,12 @@ pm_constant_pool_insert(pm_constant_pool_t *pool, const uint8_t *start, size_t l
// an existing constant, then either way we don't want the given
// memory. Either it's duplicated with the existing constant or
// it's not necessary because we have a shared version.
- free((void *) start);
+ xfree((void *) start);
} else if (bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) {
// If we're attempting to insert a shared constant and the
// existing constant is owned, then we can free the owned
// constant and replace it with the shared constant.
- free((void *) constant->start);
+ xfree((void *) constant->start);
constant->start = start;
bucket->type = (unsigned int) (PM_CONSTANT_POOL_BUCKET_DEFAULT & 0x3);
}
@@ -314,9 +314,9 @@ pm_constant_pool_free(pm_constant_pool_t *pool) {
// If an id is set on this constant, then we know we have content here.
if (bucket->id != PM_CONSTANT_ID_UNSET && bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) {
pm_constant_t *constant = &pool->constants[bucket->id - 1];
- free((void *) constant->start);
+ xfree((void *) constant->start);
}
}
- free(pool->buckets);
+ xfree(pool->buckets);
}
diff --git a/prism/util/pm_integer.c b/prism/util/pm_integer.c
index 98a09243ff..c03b930ad3 100644
--- a/prism/util/pm_integer.c
+++ b/prism/util/pm_integer.c
@@ -7,7 +7,7 @@ static pm_integer_word_t *
pm_integer_node_create(pm_integer_t *integer, uint32_t value) {
integer->length++;
- pm_integer_word_t *node = malloc(sizeof(pm_integer_word_t));
+ pm_integer_word_t *node = xmalloc(sizeof(pm_integer_word_t));
if (node == NULL) return NULL;
*node = (pm_integer_word_t) { .next = NULL, .value = value };
@@ -94,7 +94,7 @@ pm_integer_divide_word(pm_integer_t *integer, pm_integer_word_t *word, uint32_t
remainder = pm_integer_divide_word(integer, word->next, dividend);
if (integer->length > 0 && word->next->value == 0) {
- free(word->next);
+ xfree(word->next);
word->next = NULL;
integer->length--;
}
@@ -256,7 +256,7 @@ pm_integer_string(pm_buffer_t *buffer, const pm_integer_t *integer) {
default: {
// First, allocate a buffer that we'll copy the decimal digits into.
size_t length = (integer->length + 1) * 10;
- char *digits = calloc(length, sizeof(char));
+ char *digits = xcalloc(length, sizeof(char));
if (digits == NULL) return;
// Next, create a new integer that we'll use to store the result of
@@ -276,7 +276,7 @@ pm_integer_string(pm_buffer_t *buffer, const pm_integer_t *integer) {
// Finally, append the string to the buffer and free the digits.
pm_buffer_append_string(buffer, current + 1, (size_t) (ending - current));
- free(digits);
+ xfree(digits);
return;
}
}
@@ -291,7 +291,7 @@ pm_integer_word_destroy(pm_integer_word_t *integer) {
pm_integer_word_destroy(integer->next);
}
- free(integer);
+ xfree(integer);
}
/**
diff --git a/prism/util/pm_list.c b/prism/util/pm_list.c
index 62cfe47cfa..ad2294cd60 100644
--- a/prism/util/pm_list.c
+++ b/prism/util/pm_list.c
@@ -41,7 +41,7 @@ pm_list_free(pm_list_t *list) {
while (node != NULL) {
next = node->next;
- free(node);
+ xfree(node);
node = next;
}
diff --git a/prism/util/pm_list.h b/prism/util/pm_list.h
index d29fe07c52..3512dee979 100644
--- a/prism/util/pm_list.h
+++ b/prism/util/pm_list.h
@@ -33,7 +33,7 @@
* } pm_int_node_t;
*
* pm_list_t list = { 0 };
- * pm_int_node_t *node = malloc(sizeof(pm_int_node_t));
+ * pm_int_node_t *node = xmalloc(sizeof(pm_int_node_t));
* node->value = 5;
*
* pm_list_append(&list, &node->node);
diff --git a/prism/util/pm_newline_list.c b/prism/util/pm_newline_list.c
index 71226bb1de..f9dff4c166 100644
--- a/prism/util/pm_newline_list.c
+++ b/prism/util/pm_newline_list.c
@@ -6,7 +6,7 @@
*/
bool
pm_newline_list_init(pm_newline_list_t *list, const uint8_t *start, size_t capacity) {
- list->offsets = (size_t *) calloc(capacity, sizeof(size_t));
+ list->offsets = (size_t *) xcalloc(capacity, sizeof(size_t));
if (list->offsets == NULL) return false;
list->start = start;
@@ -29,11 +29,11 @@ pm_newline_list_append(pm_newline_list_t *list, const uint8_t *cursor) {
size_t *original_offsets = list->offsets;
list->capacity = (list->capacity * 3) / 2;
- list->offsets = (size_t *) calloc(list->capacity, sizeof(size_t));
+ list->offsets = (size_t *) xcalloc(list->capacity, sizeof(size_t));
if (list->offsets == NULL) return false;
memcpy(list->offsets, original_offsets, list->size * sizeof(size_t));
- free(original_offsets);
+ xfree(original_offsets);
}
assert(*cursor == '\n');
@@ -84,5 +84,5 @@ pm_newline_list_line_column(const pm_newline_list_t *list, const uint8_t *cursor
*/
void
pm_newline_list_free(pm_newline_list_t *list) {
- free(list->offsets);
+ xfree(list->offsets);
}
diff --git a/prism/util/pm_string.c b/prism/util/pm_string.c
index 4589f1e8c6..fea3f1d2cf 100644
--- a/prism/util/pm_string.c
+++ b/prism/util/pm_string.c
@@ -166,7 +166,7 @@ pm_string_ensure_owned(pm_string_t *string) {
size_t length = pm_string_length(string);
const uint8_t *source = pm_string_source(string);
- uint8_t *memory = malloc(length);
+ uint8_t *memory = xmalloc(length);
if (!memory) return;
pm_string_owned_init(string, memory, length);
@@ -217,7 +217,7 @@ pm_string_free(pm_string_t *string) {
void *memory = (void *) string->source;
if (string->type == PM_STRING_OWNED) {
- free(memory);
+ xfree(memory);
#ifdef PRISM_HAS_MMAP
} else if (string->type == PM_STRING_MAPPED && string->length) {
#if defined(_WIN32)
diff --git a/prism/util/pm_string_list.c b/prism/util/pm_string_list.c
index d49e4ed734..f6c2145987 100644
--- a/prism/util/pm_string_list.c
+++ b/prism/util/pm_string_list.c
@@ -12,7 +12,7 @@ pm_string_list_append(pm_string_list_t *string_list, pm_string_t *string) {
string_list->capacity *= 2;
}
- string_list->strings = realloc(string_list->strings, string_list->capacity * sizeof(pm_string_t));
+ string_list->strings = xrealloc(string_list->strings, string_list->capacity * sizeof(pm_string_t));
if (string_list->strings == NULL) abort();
}
@@ -24,5 +24,5 @@ pm_string_list_append(pm_string_list_t *string_list, pm_string_t *string) {
*/
void
pm_string_list_free(pm_string_list_t *string_list) {
- free(string_list->strings);
+ xfree(string_list->strings);
}