From 87316f22f3c76340ccfeec6c781f464099598aa4 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Fri, 15 Jan 2016 15:08:42 -0800 Subject: [PATCH] Wrap all calls to malloc and friends --- script/check-mallocs | 20 ++++++++++++++++++++ script/ci | 1 + src/runtime/alloc.h | 30 ++++++++++++++++++++++++++++++ src/runtime/document.c | 5 +++-- src/runtime/parser.c | 7 ++++--- src/runtime/stack.c | 15 ++++++++------- src/runtime/string_input.c | 3 ++- src/runtime/tree.c | 11 ++++++----- src/runtime/vector.h | 9 +++++---- 9 files changed, 79 insertions(+), 22 deletions(-) create mode 100755 script/check-mallocs create mode 100644 src/runtime/alloc.h diff --git a/script/check-mallocs b/script/check-mallocs new file mode 100755 index 00000000..0bd064d0 --- /dev/null +++ b/script/check-mallocs @@ -0,0 +1,20 @@ +#!/usr/bin/env bash + +src_dir="src/runtime" + +allocation_functions=( + malloc + calloc + realloc + free +) + +for function in ${allocation_functions[@]}; do + usages=$(grep --line-number -E "\b${function}\(" -r "${src_dir}" --exclude alloc.h ) + + if [[ ! -z $usages ]]; then + echo "The ${function} function should not be called directly, but is called here:" + echo "$usages" + exit 1 + fi +done diff --git a/script/ci b/script/ci index 2aefb67a..70035525 100755 --- a/script/ci +++ b/script/ci @@ -3,4 +3,5 @@ set -e script/fetch-fixtures +script/check-mallocs script/test diff --git a/src/runtime/alloc.h b/src/runtime/alloc.h new file mode 100644 index 00000000..27f38cdb --- /dev/null +++ b/src/runtime/alloc.h @@ -0,0 +1,30 @@ +#ifndef RUNTIME_ALLOC_H_ +#define RUNTIME_ALLOC_H_ + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +static inline void *ts_malloc(size_t size) { + return malloc(size); +} + +static inline void *ts_calloc(size_t count, size_t size) { + return calloc(count, size); +} + +static inline void *ts_realloc(void *buffer, size_t size) { + return realloc(buffer, size); +} + +static inline void ts_free(void *buffer) { + return free(buffer); +} + +#ifdef __cplusplus +} +#endif + +#endif // RUNTIME_ALLOC_H_ diff --git a/src/runtime/document.c b/src/runtime/document.c index 40b509ec..340996e5 100644 --- a/src/runtime/document.c +++ b/src/runtime/document.c @@ -1,4 +1,5 @@ #include "tree_sitter/parser.h" +#include "runtime/alloc.h" #include "runtime/node.h" #include "runtime/tree.h" #include "runtime/parser.h" @@ -6,7 +7,7 @@ #include "runtime/document.h" TSDocument *ts_document_make() { - TSDocument *document = calloc(sizeof(TSDocument), 1); + TSDocument *document = ts_calloc(1, sizeof(TSDocument)); document->parser = ts_parser_make(); return document; } @@ -15,7 +16,7 @@ void ts_document_free(TSDocument *self) { ts_parser_destroy(&self->parser); if (self->tree) ts_tree_release(self->tree); - free(self); + ts_free(self); } const TSLanguage *ts_document_language(TSDocument *self) { diff --git a/src/runtime/parser.c b/src/runtime/parser.c index 7ff35f25..a254316a 100644 --- a/src/runtime/parser.c +++ b/src/runtime/parser.c @@ -9,6 +9,7 @@ #include "runtime/length.h" #include "runtime/vector.h" #include "runtime/language.h" +#include "runtime/alloc.h" /* * Debugging @@ -80,7 +81,7 @@ static void ts_parser__breakdown_top_of_stack(TSParser *self, int head) { assert((i == 0) ^ merged); } - free(removed_trees); + ts_free(removed_trees); } while (last_child && last_child->child_count > 0); } @@ -511,8 +512,8 @@ static void ts_parser__accept(TSParser *self, int head) { TSTree *root = pop_result->trees[i]; size_t leading_extra_count = i; size_t trailing_extra_count = pop_result->tree_count - 1 - i; - TSTree **new_children = malloc( - (root->child_count + leading_extra_count + trailing_extra_count) * + TSTree **new_children = ts_calloc( + root->child_count + leading_extra_count + trailing_extra_count, sizeof(TSTree *)); memcpy(new_children, pop_result->trees, leading_extra_count * sizeof(TSTree *)); diff --git a/src/runtime/stack.c b/src/runtime/stack.c index ce9c0794..158e4e3c 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -1,4 +1,5 @@ #include "tree_sitter/parser.h" +#include "runtime/alloc.h" #include "runtime/tree.h" #include "runtime/vector.h" #include "runtime/stack.h" @@ -42,9 +43,9 @@ static TSTree *ts_stack__default_tree_selection(void *p, TSTree *t1, TSTree *t2) } Stack *ts_stack_new() { - Stack *self = malloc(sizeof(Stack)); + Stack *self = ts_malloc(sizeof(Stack)); *self = (Stack){ - .heads = calloc(INITIAL_HEAD_CAPACITY, sizeof(StackNode *)), + .heads = ts_calloc(INITIAL_HEAD_CAPACITY, sizeof(StackNode *)), .head_count = 1, .head_capacity = INITIAL_HEAD_CAPACITY, .tree_selection_payload = NULL, @@ -58,8 +59,8 @@ Stack *ts_stack_new() { void ts_stack_delete(Stack *self) { vector_delete(&self->pop_results); vector_delete(&self->pop_paths); - free(self->heads); - free(self); + ts_free(self->heads); + ts_free(self); } /* @@ -119,7 +120,7 @@ static bool stack_node_release(StackNode *self) { for (int i = 0; i < self->successor_count; i++) stack_node_release(self->successors[i]); ts_tree_release(self->entry.tree); - free(self); + ts_free(self); return true; } else { return false; @@ -127,7 +128,7 @@ static bool stack_node_release(StackNode *self) { } static StackNode *stack_node_new(StackNode *next, TSStateId state, TSTree *tree) { - StackNode *self = malloc(sizeof(StackNode)); + StackNode *self = ts_malloc(sizeof(StackNode)); assert(tree->ref_count > 0); ts_tree_retain(tree); stack_node_retain(next); @@ -179,7 +180,7 @@ static int ts_stack__add_head(Stack *self, StackNode *node) { if (self->head_count == self->head_capacity) { self->head_capacity += 3; self->heads = - realloc(self->heads, self->head_capacity * sizeof(StackNode *)); + ts_realloc(self->heads, self->head_capacity * sizeof(StackNode *)); } int new_index = self->head_count++; self->heads[new_index] = node; diff --git a/src/runtime/string_input.c b/src/runtime/string_input.c index 5f91e759..2ebd9c00 100644 --- a/src/runtime/string_input.c +++ b/src/runtime/string_input.c @@ -1,4 +1,5 @@ #include "runtime/string_input.h" +#include "runtime/alloc.h" #include typedef struct { @@ -26,7 +27,7 @@ int ts_string_input_seek(void *payload, size_t character, size_t byte) { } TSInput ts_string_input_make(const char *string) { - TSStringInput *input = malloc(sizeof(TSStringInput)); + TSStringInput *input = ts_malloc(sizeof(TSStringInput)); input->string = string; input->position = 0; input->length = strlen(string); diff --git a/src/runtime/tree.c b/src/runtime/tree.c index 1442c28f..0e04267a 100644 --- a/src/runtime/tree.c +++ b/src/runtime/tree.c @@ -4,6 +4,7 @@ #include #include #include "tree_sitter/parser.h" +#include "runtime/alloc.h" #include "runtime/tree.h" #include "runtime/length.h" @@ -12,7 +13,7 @@ TSStateId TS_TREE_STATE_ERROR = USHRT_MAX - 1; TSTree *ts_tree_make_leaf(TSSymbol sym, TSLength padding, TSLength size, TSSymbolMetadata metadata) { - TSTree *result = malloc(sizeof(TSTree)); + TSTree *result = ts_malloc(sizeof(TSTree)); *result = (TSTree){ .ref_count = 1, .symbol = sym, @@ -46,7 +47,7 @@ TSTree *ts_tree_make_error(TSLength size, TSLength padding, char lookahead_char) } TSTree *ts_tree_make_copy(TSTree *self) { - TSTree *result = malloc(sizeof(TSTree)); + TSTree *result = ts_malloc(sizeof(TSTree)); *result = *self; return result; } @@ -125,8 +126,8 @@ void ts_tree_release(TSTree *self) { for (size_t i = 0; i < self->child_count; i++) ts_tree_release(self->children[i]); if (self->child_count > 0) - free(self->children); - free(self); + ts_free(self->children); + ts_free(self); } } @@ -252,7 +253,7 @@ char *ts_tree_string(const TSTree *self, const char **symbol_names, static char SCRATCH[1]; size_t size = 1 + ts_tree__write_to_string(self, symbol_names, SCRATCH, 0, true, include_anonymous); - char *result = malloc(size * sizeof(char)); + char *result = ts_malloc(size * sizeof(char)); ts_tree__write_to_string(self, symbol_names, result, size, true, include_anonymous); return result; diff --git a/src/runtime/vector.h b/src/runtime/vector.h index 00019ca0..c5b1f059 100644 --- a/src/runtime/vector.h +++ b/src/runtime/vector.h @@ -8,6 +8,7 @@ extern "C" { #include #include #include +#include "runtime/alloc.h" typedef struct { void *contents; @@ -18,7 +19,7 @@ typedef struct { static inline Vector vector_new(size_t element_size, size_t capacity) { Vector result; - result.contents = malloc(capacity * element_size); + result.contents = ts_calloc(capacity, element_size); result.size = 0; result.capacity = capacity; result.element_size = element_size; @@ -26,7 +27,7 @@ static inline Vector vector_new(size_t element_size, size_t capacity) { } static inline void vector_delete(Vector *self) { - free(self->contents); + ts_free(self->contents); } static inline void *vector_get(Vector *self, size_t index) { @@ -56,7 +57,7 @@ static inline void vector_push(Vector *self, void *entry) { if (self->size == self->capacity) { self->capacity += 4; self->contents = - realloc(self->contents, self->capacity * self->element_size); + ts_realloc(self->contents, self->capacity * self->element_size); } char *contents = (char *)self->contents; @@ -80,7 +81,7 @@ static inline void vector_reverse(Vector *self) { static inline Vector vector_copy(Vector *self) { Vector copy = *self; - copy.contents = memcpy(malloc(self->capacity * self->element_size), + copy.contents = memcpy(ts_calloc(self->capacity, self->element_size), self->contents, self->size * self->element_size); return copy; }