Wrap all calls to malloc and friends
This commit is contained in:
parent
19b776e74d
commit
87316f22f3
9 changed files with 79 additions and 22 deletions
20
script/check-mallocs
Executable file
20
script/check-mallocs
Executable file
|
|
@ -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
|
||||
|
|
@ -3,4 +3,5 @@
|
|||
set -e
|
||||
|
||||
script/fetch-fixtures
|
||||
script/check-mallocs
|
||||
script/test
|
||||
|
|
|
|||
30
src/runtime/alloc.h
Normal file
30
src/runtime/alloc.h
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
#ifndef RUNTIME_ALLOC_H_
|
||||
#define RUNTIME_ALLOC_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
|
||||
#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_
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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 *));
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
#include "runtime/string_input.h"
|
||||
#include "runtime/alloc.h"
|
||||
#include <string.h>
|
||||
|
||||
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);
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
#include <stdbool.h>
|
||||
#include <stdio.h>
|
||||
#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;
|
||||
|
|
|
|||
|
|
@ -8,6 +8,7 @@ extern "C" {
|
|||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue