Skip to content

Commit d437e12

Browse files
committed
Shorten allocation macro names
1 parent 68988bf commit d437e12

7 files changed

Lines changed: 172 additions & 174 deletions

File tree

include/rbs/util/rbs_allocator.h

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,11 @@ void *rbs_allocator_calloc_impl(rbs_allocator_t *, size_t count, size_t size, si
2626
void *rbs_allocator_realloc_impl(rbs_allocator_t *, void *ptr, size_t old_size, size_t new_size, size_t alignment);
2727

2828
// Use this when allocating memory for a single instance of a type.
29-
#define rbs_allocator_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), alignof(type)))
30-
// Use this when allocating memory that will be immediately written to in full.
31-
// Such as allocating strings
32-
#define rbs_allocator_alloc_many(allocator, count, type) ((type *) rbs_allocator_malloc_many_impl((allocator), (count), sizeof(type), alignof(type)))
33-
// Use this when allocating memory that will NOT be immediately written to in full.
34-
// Such as allocating buffers
35-
#define rbs_allocator_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), alignof(type)))
36-
#define rbs_allocator_realloc(allocator, ptr, old_size, new_size, type) ((type *) rbs_allocator_realloc_impl((allocator), (ptr), (old_size), (new_size), alignof(type)))
29+
#define rbs_alloc(allocator, type) ((type *) rbs_allocator_malloc_impl((allocator), sizeof(type), alignof(type)))
30+
// Use this when allocating memory that will be immediately written to in full, such as allocating strings
31+
#define rbs_alloc_many(allocator, count, type) ((type *) rbs_allocator_malloc_many_impl((allocator), (count), sizeof(type), alignof(type)))
32+
// Use this when allocating memory that will NOT be immediately written to in full, such as allocating buffers
33+
#define rbs_calloc(allocator, count, type) ((type *) rbs_allocator_calloc_impl((allocator), (count), sizeof(type), alignof(type)))
34+
#define rbs_realloc(allocator, ptr, old_size, new_size, type) ((type *) rbs_allocator_realloc_impl((allocator), (ptr), (old_size), (new_size), alignof(type)))
3735

3836
#endif

src/ast.c

Lines changed: 142 additions & 142 deletions
Large diffs are not rendered by default.

src/location.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void rbs_loc_add_required_child(rbs_location_t *loc, rbs_constant_id_t name, rbs
3131
}
3232

3333
rbs_location_t *rbs_location_new(rbs_allocator_t *allocator, rbs_range_t rg) {
34-
rbs_location_t *location = rbs_allocator_alloc(allocator, rbs_location_t);
34+
rbs_location_t *location = rbs_alloc(allocator, rbs_location_t);
3535
*location = (rbs_location_t) {
3636
.rg = rg,
3737
.children = NULL,
@@ -41,7 +41,7 @@ rbs_location_t *rbs_location_new(rbs_allocator_t *allocator, rbs_range_t rg) {
4141
}
4242

4343
rbs_location_list_t *rbs_location_list_new(rbs_allocator_t *allocator) {
44-
rbs_location_list_t *list = rbs_allocator_alloc(allocator, rbs_location_list_t);
44+
rbs_location_list_t *list = rbs_alloc(allocator, rbs_location_list_t);
4545
*list = (rbs_location_list_t) {
4646
.allocator = allocator,
4747
.head = NULL,
@@ -53,7 +53,7 @@ rbs_location_list_t *rbs_location_list_new(rbs_allocator_t *allocator) {
5353
}
5454

5555
void rbs_location_list_append(rbs_location_list_t *list, rbs_location_t *loc) {
56-
rbs_location_list_node_t *node = rbs_allocator_alloc(list->allocator, rbs_location_list_node_t);
56+
rbs_location_list_node_t *node = rbs_alloc(list->allocator, rbs_location_list_node_t);
5757
*node = (rbs_location_list_node_t) {
5858
.loc = loc,
5959
.next = NULL,

src/parser.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,7 @@ static bool parse_function(rbs_parser_t *parser, bool accept_type_binding, parse
807807
NODISCARD
808808
static bool parse_proc_type(rbs_parser_t *parser, rbs_types_proc_t **proc) {
809809
rbs_position_t start = parser->current_token.range.start;
810-
parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
810+
parse_function_result *result = rbs_alloc(ALLOCATOR(), parse_function_result);
811811
CHECK_PARSE(parse_function(parser, true, &result));
812812

813813
rbs_position_t end = parser->current_token.range.end;
@@ -1472,7 +1472,7 @@ bool rbs_parse_method_type(rbs_parser_t *parser, rbs_method_type_t **method_type
14721472
rbs_range_t type_range;
14731473
type_range.start = parser->next_token.range.start;
14741474

1475-
parse_function_result *result = rbs_allocator_alloc(ALLOCATOR(), parse_function_result);
1475+
parse_function_result *result = rbs_alloc(ALLOCATOR(), parse_function_result);
14761476
CHECK_PARSE(parse_function(parser, false, &result));
14771477

14781478
rg.end = parser->current_token.range.end;
@@ -3167,7 +3167,7 @@ static void comment_insert_new_line(rbs_allocator_t *allocator, rbs_comment_t *c
31673167
}
31683168

31693169
static rbs_comment_t *alloc_comment(rbs_allocator_t *allocator, rbs_token_t comment_token, rbs_comment_t *last_comment) {
3170-
rbs_comment_t *new_comment = rbs_allocator_alloc(allocator, rbs_comment_t);
3170+
rbs_comment_t *new_comment = rbs_alloc(allocator, rbs_comment_t);
31713171

31723172
*new_comment = (rbs_comment_t) {
31733173
.start = comment_token.range.start,
@@ -3251,20 +3251,20 @@ bool rbs_parse_type_params(rbs_parser_t *parser, bool module_type_params, rbs_no
32513251
}
32523252

32533253
id_table *alloc_empty_table(rbs_allocator_t *allocator) {
3254-
id_table *table = rbs_allocator_alloc(allocator, id_table);
3254+
id_table *table = rbs_alloc(allocator, id_table);
32553255

32563256
*table = (id_table) {
32573257
.size = 10,
32583258
.count = 0,
3259-
.ids = rbs_allocator_calloc(allocator, 10, rbs_constant_id_t),
3259+
.ids = rbs_calloc(allocator, 10, rbs_constant_id_t),
32603260
.next = NULL,
32613261
};
32623262

32633263
return table;
32643264
}
32653265

32663266
id_table *alloc_reset_table(rbs_allocator_t *allocator) {
3267-
id_table *table = rbs_allocator_alloc(allocator, id_table);
3267+
id_table *table = rbs_alloc(allocator, id_table);
32683268

32693269
*table = (id_table) {
32703270
.size = 0,
@@ -3301,7 +3301,7 @@ bool rbs_parser_insert_typevar(rbs_parser_t *parser, rbs_constant_id_t id) {
33013301
// expand
33023302
rbs_constant_id_t *ptr = table->ids;
33033303
table->size += 10;
3304-
table->ids = rbs_allocator_calloc(ALLOCATOR(), table->size, rbs_constant_id_t);
3304+
table->ids = rbs_calloc(ALLOCATOR(), table->size, rbs_constant_id_t);
33053305
memcpy(table->ids, ptr, sizeof(rbs_constant_id_t) * table->count);
33063306
}
33073307

@@ -3363,7 +3363,7 @@ rbs_ast_comment_t *rbs_parser_get_comment(rbs_parser_t *parser, int subject_line
33633363
}
33643364

33653365
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) {
3366-
rbs_lexer_t *lexer = rbs_allocator_alloc(allocator, rbs_lexer_t);
3366+
rbs_lexer_t *lexer = rbs_alloc(allocator, rbs_lexer_t);
33673367

33683368
rbs_position_t start_position = (rbs_position_t) {
33693369
.byte_pos = 0,
@@ -3394,7 +3394,7 @@ rbs_parser_t *rbs_parser_new(rbs_string_t string, const rbs_encoding_t *encoding
33943394
rbs_allocator_t *allocator = rbs_allocator_init();
33953395

33963396
rbs_lexer_t *lexer = rbs_lexer_new(allocator, string, encoding, start_pos, end_pos);
3397-
rbs_parser_t *parser = rbs_allocator_alloc(allocator, rbs_parser_t);
3397+
rbs_parser_t *parser = rbs_alloc(allocator, rbs_parser_t);
33983398

33993399
*parser = (rbs_parser_t) {
34003400
.rbs_lexer_t = lexer,
@@ -3455,13 +3455,13 @@ void rbs_parser_set_error(rbs_parser_t *parser, rbs_token_t tok, bool syntax_err
34553455
int length = vsnprintf(NULL, 0, fmt, args);
34563456
va_end(args);
34573457

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

34603460
va_start(args, fmt);
34613461
vsnprintf(message, length + 1, fmt, args);
34623462
va_end(args);
34633463

3464-
parser->error = rbs_allocator_alloc(ALLOCATOR(), rbs_error_t);
3464+
parser->error = rbs_alloc(ALLOCATOR(), rbs_error_t);
34653465
parser->error->token = tok;
34663466
parser->error->message = message;
34673467
parser->error->syntax_error = syntax_error;

src/util/rbs_buffer.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ bool rbs_buffer_init_with_capacity(rbs_allocator_t *allocator, rbs_buffer_t *buf
1515
*buffer = (rbs_buffer_t) {
1616
.length = 0,
1717
.capacity = capacity,
18-
.value = rbs_allocator_calloc(allocator, capacity, char),
18+
.value = rbs_calloc(allocator, capacity, char),
1919
};
2020

2121
return buffer->value != NULL;
@@ -43,7 +43,7 @@ void rbs_buffer_append_string(rbs_allocator_t *allocator, rbs_buffer_t *buffer,
4343
new_capacity *= 2;
4444
}
4545

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

4949
buffer->value = new_value;

src/util/rbs_unescape.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ rbs_string_t unescape_string(rbs_allocator_t *allocator, const rbs_string_t stri
5656
size_t len = string.end - string.start;
5757
const char *input = string.start;
5858

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

6262
size_t i = 0, j = 0;

templates/src/ast.c.erb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const char *rbs_node_type_name(rbs_node_t *node) {
2020
/* rbs_node_list */
2121

2222
rbs_node_list_t *rbs_node_list_new(rbs_allocator_t *allocator) {
23-
rbs_node_list_t *list = rbs_allocator_alloc(allocator, rbs_node_list_t);
23+
rbs_node_list_t *list = rbs_alloc(allocator, rbs_node_list_t);
2424
*list = (rbs_node_list_t) {
2525
.allocator = allocator,
2626
.head = NULL,
@@ -32,7 +32,7 @@ rbs_node_list_t *rbs_node_list_new(rbs_allocator_t *allocator) {
3232
}
3333

3434
void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node) {
35-
rbs_node_list_node_t *new_node = rbs_allocator_alloc(list->allocator, rbs_node_list_node_t);
35+
rbs_node_list_node_t *new_node = rbs_alloc(list->allocator, rbs_node_list_node_t);
3636
*new_node = (rbs_node_list_node_t) {
3737
.node = node,
3838
.next = NULL,
@@ -52,7 +52,7 @@ void rbs_node_list_append(rbs_node_list_t *list, rbs_node_t *node) {
5252
/* rbs_hash */
5353

5454
rbs_hash_t *rbs_hash_new(rbs_allocator_t *allocator) {
55-
rbs_hash_t *hash = rbs_allocator_alloc(allocator, rbs_hash_t);
55+
rbs_hash_t *hash = rbs_alloc(allocator, rbs_hash_t);
5656
*hash = (rbs_hash_t) {
5757
.allocator = allocator,
5858
.head = NULL,
@@ -104,7 +104,7 @@ void rbs_hash_set(rbs_hash_t *hash, rbs_node_t *key, rbs_node_t *value) {
104104
return;
105105
}
106106

107-
rbs_hash_node_t *new_node = rbs_allocator_alloc(hash->allocator, rbs_hash_node_t);
107+
rbs_hash_node_t *new_node = rbs_alloc(hash->allocator, rbs_hash_node_t);
108108
new_node->key = key;
109109
new_node->value = value;
110110
new_node->next = NULL;
@@ -124,7 +124,7 @@ rbs_node_t *rbs_hash_get(rbs_hash_t *hash, rbs_node_t *key) {
124124
}
125125

126126
rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *allocator, rbs_location_t *location, rbs_constant_id_t constant_id) {
127-
rbs_keyword_t *instance = rbs_allocator_alloc(allocator, rbs_keyword_t);
127+
rbs_keyword_t *instance = rbs_alloc(allocator, rbs_keyword_t);
128128

129129
*instance = (rbs_keyword_t) {
130130
.base = (rbs_node_t) {
@@ -138,7 +138,7 @@ rbs_keyword_t *rbs_keyword_new(rbs_allocator_t *allocator, rbs_location_t *locat
138138
}
139139

140140
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) {
141-
rbs_ast_symbol_t *instance = rbs_allocator_alloc(allocator, rbs_ast_symbol_t);
141+
rbs_ast_symbol_t *instance = rbs_alloc(allocator, rbs_ast_symbol_t);
142142

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

0 commit comments

Comments
 (0)