Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions include/rbs/util/rbs_allocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@ void *rbs_allocator_calloc_impl(rbs_allocator_t *, size_t count, size_t size, si
void *rbs_allocator_realloc_impl(rbs_allocator_t *, void *ptr, size_t old_size, size_t new_size, size_t alignment);

// Use this when allocating memory for a single instance of a type.
#define rbs_allocator_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), rbs_alignof(type)))
// Use this when allocating memory that will be immediately written to in full.
// Such as allocating strings
#define rbs_allocator_alloc_many(allocator, count, type) ((type *) rbs_allocator_malloc_many_impl((allocator), (count), sizeof(type), rbs_alignof(type)))
// Use this when allocating memory that will NOT be immediately written to in full.
// Such as allocating buffers
#define rbs_allocator_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), rbs_alignof(type)))
#define rbs_allocator_realloc(allocator, ptr, old_size, new_size, type) ((type *) rbs_allocator_realloc_impl((allocator), (ptr), (old_size), (new_size), rbs_alignof(type)))
#define rbs_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), rbs_alignof(type)))
// Use this when allocating memory that will be immediately written to in full. e.g. allocating strings
#define rbs_alloc_many(allocator, count, type) ((type *) rbs_allocator_malloc_many_impl((allocator), (count), sizeof(type), rbs_alignof(type)))
// Use this when allocating memory that will NOT be immediately written to in full, e.g. pre-allocating buffers
#define rbs_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), rbs_alignof(type)))
#define rbs_realloc(allocator, ptr, old_size, new_size, type) ((type *) rbs_allocator_realloc_impl((allocator), (ptr), (old_size), (new_size), rbs_alignof(type)))

#endif
164 changes: 83 additions & 81 deletions src/ast.c

Large diffs are not rendered by default.

22 changes: 14 additions & 8 deletions src/location.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,24 @@ void rbs_loc_alloc_children(rbs_allocator_t *allocator, rbs_location_t *loc, siz

loc->children = rbs_allocator_malloc_impl(allocator, RBS_LOC_CHILDREN_SIZE(capacity), rbs_alignof(rbs_loc_children));

loc->children->len = 0;
loc->children->required_p = 0;
loc->children->cap = capacity;
*loc->children = (rbs_loc_children) {
.len = 0,
.cap = capacity,
.required_p = 0,
.entries = { 0 },
};
}

void rbs_loc_add_optional_child(rbs_location_t *loc, rbs_constant_id_t name, rbs_range_t r) {
rbs_assert(loc->children != NULL, "All children should have been pre-allocated with rbs_loc_alloc_children()");
rbs_assert((loc->children->len + 1 <= loc->children->cap), "Not enough space was pre-allocated for the children. Children: %hu, Capacity: %hu", loc->children->len, loc->children->cap);

unsigned short i = loc->children->len++;
loc->children->entries[i].name = name;
loc->children->entries[i].rg = (rbs_loc_range) { r.start.char_pos, r.end.char_pos };

loc->children->entries[i] = (rbs_loc_entry) {
.name = name,
.rg = (rbs_loc_range) { r.start.char_pos, r.end.char_pos },
};
}

void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, rbs_range_t r) {
Expand All @@ -31,7 +37,7 @@ void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, rbs
}

rbs_location_t *rbs_location_new(rbs_allocator_t *allocator, rbs_range_t rg) {
rbs_location_t *location = rbs_allocator_alloc(allocator, rbs_location_t);
rbs_location_t *location = rbs_alloc(allocator, rbs_location_t);
*location = (rbs_location_t) {
.rg = rg,
.children = NULL,
Expand All @@ -41,7 +47,7 @@ rbs_location_t *rbs_location_new(rbs_allocator_t *allocator, rbs_range_t rg) {
}

rbs_location_list_t *rbs_location_list_new(rbs_allocator_t *allocator) {
rbs_location_list_t *list = rbs_allocator_alloc(allocator, rbs_location_list_t);
rbs_location_list_t *list = rbs_alloc(allocator, rbs_location_list_t);
*list = (rbs_location_list_t) {
.allocator = allocator,
.head = NULL,
Expand All @@ -53,7 +59,7 @@ rbs_location_list_t *rbs_location_list_new(rbs_allocator_t *allocator) {
}

void rbs_location_list_append(rbs_location_list_t *list, rbs_location_t *loc) {
rbs_location_list_node_t *node = rbs_allocator_alloc(list->allocator, rbs_location_list_node_t);
rbs_location_list_node_t *node = rbs_alloc(list->allocator, rbs_location_list_node_t);
*node = (rbs_location_list_node_t) {
.loc = loc,
.next = NULL,
Expand Down
45 changes: 26 additions & 19 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -837,9 +837,12 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse
);
}

(*result)->function = function;
(*result)->block = block;
(*result)->function_self_type = function_self_type;
**result = (parse_function_result) {
.function = function,
.block = block,
.function_self_type = function_self_type,
};

return true;
}

Expand All @@ -849,7 +852,7 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse
NODISCARD
static bool parse_proc_type(rbs_parser_t *parser, rbs_types_proc_t **proc, bool self_allowed) {
rbs_position_t start = parser->current_token.range.start;
parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
parse_function_result *result = rbs_alloc(ALLOCATOR(), parse_function_result);
CHECK_PARSE(parse_function(parser, true, &result, self_allowed));

rbs_position_t end = parser->current_token.range.end;
Expand Down Expand Up @@ -1563,7 +1566,7 @@ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type
rbs_range_t type_range;
type_range.start = parser->next_token.range.start;

parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
parse_function_result *result = rbs_alloc(ALLOCATOR(), parse_function_result);
CHECK_PARSE(parse_function(parser, false, &result, true));

rg.end = parser->current_token.range.end;
Expand Down Expand Up @@ -1787,8 +1790,10 @@ static bool parse_method_name(rbs_parser_t *parser, rbs_range_t *range, rbs_ast_
case tULLIDENT:
KEYWORD_CASES
if (parser->next_token.type == pQUESTION && parser->current_token.range.end.byte_pos == parser->next_token.range.start.byte_pos) {
range->start = parser->current_token.range.start;
range->end = parser->next_token.range.end;
*range = (rbs_range_t) {
.start = parser->current_token.range.start,
.end = parser->next_token.range.end,
};
rbs_parser_advance(parser);

rbs_constant_id_t constant_id = rbs_constant_pool_insert_shared_with_encoding(
Expand Down Expand Up @@ -3259,7 +3264,7 @@ static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *c
}

static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, rbs_token_t comment_token, rbs_comment_t *last_comment) {
rbs_comment_t *new_comment = rbs_allocator_alloc(allocator, rbs_comment_t);
rbs_comment_t *new_comment = rbs_alloc(allocator, rbs_comment_t);

*new_comment = (rbs_comment_t) {
.start = comment_token.range.start,
Expand Down Expand Up @@ -3343,20 +3348,20 @@ bool rbs_parse_type_params(rbs_parser_t *parser, bool module_type_params, rbs_no
}

id_table *alloc_empty_table(rbs_allocator_t *allocator) {
id_table *table = rbs_allocator_alloc(allocator, id_table);
id_table *table = rbs_alloc(allocator, id_table);

*table = (id_table) {
.size = 10,
.count = 0,
.ids = rbs_allocator_calloc(allocator, 10, rbs_constant_id_t),
.ids = rbs_calloc(allocator, 10, rbs_constant_id_t),
.next = NULL,
};

return table;
}

id_table *alloc_reset_table(rbs_allocator_t *allocator) {
id_table *table = rbs_allocator_alloc(allocator, id_table);
id_table *table = rbs_alloc(allocator, id_table);

*table = (id_table) {
.size = 0,
Expand Down Expand Up @@ -3393,7 +3398,7 @@ bool rbs_parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id) {
// expand
rbs_constant_id_t *ptr = table->ids;
table->size += 10;
table->ids = rbs_allocator_calloc(ALLOCATOR(), table->size, rbs_constant_id_t);
table->ids = rbs_calloc(ALLOCATOR(), table->size, rbs_constant_id_t);
memcpy(table->ids, ptr, sizeof(rbs_constant_id_t) * table->count);
}

Expand Down Expand Up @@ -3455,7 +3460,7 @@ rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line
}

rbs_lexer_t *rbs_lexer_new(rbs_allocator_t *allocator, rbs_string_t string, const rbs_encoding_t *encoding, int start_pos, int end_pos) {
rbs_lexer_t *lexer = rbs_allocator_alloc(allocator, rbs_lexer_t);
rbs_lexer_t *lexer = rbs_alloc(allocator, rbs_lexer_t);

rbs_position_t start_position = (rbs_position_t) {
.byte_pos = 0,
Expand Down Expand Up @@ -3486,7 +3491,7 @@ rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding
rbs_allocator_t *allocator = rbs_allocator_init();

rbs_lexer_t *lexer = rbs_lexer_new(allocator, string, encoding, start_pos, end_pos);
rbs_parser_t *parser = rbs_allocator_alloc(allocator, rbs_parser_t);
rbs_parser_t *parser = rbs_alloc(allocator, rbs_parser_t);

*parser = (rbs_parser_t) {
.rbs_lexer_t = lexer,
Expand Down Expand Up @@ -3547,16 +3552,18 @@ void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_err
int length = vsnprintf(NULL, 0, fmt, args);
va_end(args);

char *message = rbs_allocator_alloc_many(ALLOCATOR(), length + 1, char);
char *message = rbs_alloc_many(ALLOCATOR(), length + 1, char);

va_start(args, fmt);
vsnprintf(message, length + 1, fmt, args);
va_end(args);

parser->error = rbs_allocator_alloc(ALLOCATOR(), rbs_error_t);
parser->error->token = tok;
parser->error->message = message;
parser->error->syntax_error = syntax_error;
parser->error = rbs_alloc(ALLOCATOR(), rbs_error_t);
*parser->error = (rbs_error_t) {
.message = message,
.token = tok,
.syntax_error = syntax_error,
};
}

/*
Expand Down
4 changes: 2 additions & 2 deletions src/util/rbs_buffer.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ bool rbs_buffer_init_with_capacity(rbs_allocator_t *allocator, rbs_buffer_t *buf
*buffer = (rbs_buffer_t) {
.length = 0,
.capacity = capacity,
.value = rbs_allocator_calloc(allocator, capacity, char),
.value = rbs_calloc(allocator, capacity, char),
};

return buffer->value != NULL;
Expand Down Expand Up @@ -43,7 +43,7 @@ void rbs_buffer_append_string(rbs_allocator_t *allocator, rbs_buffer_t *buffer,
new_capacity *= 2;
}

char *new_value = rbs_allocator_realloc(allocator, buffer->value, old_capacity, new_capacity, char);
char *new_value = rbs_realloc(allocator, buffer->value, old_capacity, new_capacity, char);
rbs_assert(new_value != NULL, "Failed to append to buffer. Old capacity: %zu, new capacity: %zu", old_capacity, new_capacity);

buffer->value = new_value;
Expand Down
2 changes: 1 addition & 1 deletion src/util/rbs_unescape.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ rbs_string_t unescape_string(rbs_allocator_t *allocator, const rbs_string_t stri
size_t len = string.end - string.start;
const char *input = string.start;

char *output = rbs_allocator_alloc_many(allocator, len + 1, char);
char *output = rbs_alloc_many(allocator, len + 1, char);
if (!output) return RBS_STRING_NULL;

size_t i = 0, j = 0;
Expand Down
22 changes: 12 additions & 10 deletions templates/src/ast.c.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const char *rbs_node_type_name(rbs_node_t *node) {
/* rbs_node_list */

rbs_node_list_t *rbs_node_list_new(rbs_allocator_t *allocator) {
rbs_node_list_t *list = rbs_allocator_alloc(allocator, rbs_node_list_t);
rbs_node_list_t *list = rbs_alloc(allocator, rbs_node_list_t);
*list = (rbs_node_list_t) {
.allocator = allocator,
.head = NULL,
Expand All @@ -32,7 +32,7 @@ rbs_node_list_t *rbs_node_list_new(rbs_allocator_t *allocator) {
}

void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node) {
rbs_node_list_node_t *new_node = rbs_allocator_alloc(list->allocator, rbs_node_list_node_t);
rbs_node_list_node_t *new_node = rbs_alloc(list->allocator, rbs_node_list_node_t);
*new_node = (rbs_node_list_node_t) {
.node = node,
.next = NULL,
Expand All @@ -52,7 +52,7 @@ void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node) {
/* rbs_hash */

rbs_hash_t *rbs_hash_new(rbs_allocator_t *allocator) {
rbs_hash_t *hash = rbs_allocator_alloc(allocator, rbs_hash_t);
rbs_hash_t *hash = rbs_alloc(allocator, rbs_hash_t);
*hash = (rbs_hash_t) {
.allocator = allocator,
.head = NULL,
Expand Down Expand Up @@ -104,10 +104,12 @@ void rbs_hash_set(rbs_hash_t *hash, rbs_node_t *key, rbs_node_t *value) {
return;
}

rbs_hash_node_t *new_node = rbs_allocator_alloc(hash->allocator, rbs_hash_node_t);
new_node->key = key;
new_node->value = value;
new_node->next = NULL;
rbs_hash_node_t *new_node = rbs_alloc(hash->allocator, rbs_hash_node_t);
*new_node = (rbs_hash_node_t) {
.key = key,
.value = value,
.next = NULL,
};

if (hash->tail == NULL) {
hash->head = new_node;
Expand All @@ -124,7 +126,7 @@ rbs_node_t *rbs_hash_get(rbs_hash_t *hash, rbs_node_t *key) {
}

rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_constant_id_t constant_id) {
rbs_keyword_t *instance = rbs_allocator_alloc(allocator, rbs_keyword_t);
rbs_keyword_t *instance = rbs_alloc(allocator, rbs_keyword_t);

*instance = (rbs_keyword_t) {
.base = (rbs_node_t) {
Expand All @@ -138,7 +140,7 @@ rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *allocator, rbs_location_t *locat
}

rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_constant_pool_t *constant_pool, rbs_constant_id_t constant_id) {
rbs_ast_symbol_t *instance = rbs_allocator_alloc(allocator, rbs_ast_symbol_t);
rbs_ast_symbol_t *instance = rbs_alloc(allocator, rbs_ast_symbol_t);

*instance = (rbs_ast_symbol_t) {
.base = (rbs_node_t) {
Expand All @@ -154,7 +156,7 @@ rbs_ast_symbol_t *rbs_ast_symbol_new(rbs_allocator_t *allocator, rbs_location_t
<%- nodes.each do |node| -%>
#line <%= __LINE__ + 1 %> "prism/templates/src/<%= File.basename(__FILE__) %>"
<%= node.c_type_name %> *<%= node.c_constructor_function_name %>(<%= node.constructor_params.map(&:parameter_decl).join(", ") %>) {
<%= node.c_type_name %> *instance = rbs_allocator_alloc(allocator, <%= node.c_type_name %>);
<%= node.c_type_name %> *instance = rbs_alloc(allocator, <%= node.c_type_name %>);
<%- node.fields.filter { |f| f.c_type == "VALUE" }.each do |f| -%>
rb_gc_register_mark_object(<%= f.c_name %>);
<%- end -%>
Expand Down
Loading