Skip to content

Commit 38d4fd9

Browse files
authored
C: Migrate hb_array to use hb_allocator_T infrastructure (#1323)
With the new `hb_allocator_realloc` implemented in #1322 we can now port `hb_array` to use the `hb_allocator_T` infrastructure. Resolves #1291
1 parent 1ce90c7 commit 38d4fd9

File tree

11 files changed

+123
-83
lines changed

11 files changed

+123
-83
lines changed

src/analyze/analyze.c

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -287,9 +287,9 @@ static size_t process_case_structure(
287287
AST_ERB_CONTENT_NODE_T* erb_node = get_erb_content_at(array, index);
288288
if (!erb_node) { return index; }
289289

290-
hb_array_T* when_conditions = hb_array_init(8);
291-
hb_array_T* in_conditions = hb_array_init(8);
292-
hb_array_T* non_when_non_in_children = hb_array_init(8);
290+
hb_array_T* when_conditions = hb_array_init(8, allocator);
291+
hb_array_T* in_conditions = hb_array_init(8, allocator);
292+
hb_array_T* non_when_non_in_children = hb_array_init(8, allocator);
293293

294294
analyzed_ruby_T* analyzed = erb_node->analyzed_ruby;
295295
bool has_inline_when = has_case_node(analyzed) && has_when_node(analyzed);
@@ -309,7 +309,7 @@ static size_t process_case_structure(
309309
// Create a synthetic when/in node for inline when/in (e.g., <% case variable when "a" %>),
310310
if (has_inline_when || has_inline_in) {
311311
hb_array_T* statements = non_when_non_in_children;
312-
non_when_non_in_children = hb_array_init(8);
312+
non_when_non_in_children = hb_array_init(8, allocator);
313313

314314
position_T start_position =
315315
erb_node->tag_closing ? erb_node->tag_closing->location.end : erb_node->content->location.end;
@@ -329,7 +329,7 @@ static size_t process_case_structure(
329329
statements,
330330
start_position,
331331
end_position,
332-
hb_array_init(0),
332+
hb_array_init(0, allocator),
333333
allocator
334334
);
335335

@@ -343,7 +343,7 @@ static size_t process_case_structure(
343343
statements,
344344
start_position,
345345
end_position,
346-
hb_array_init(0),
346+
hb_array_init(0, allocator),
347347
allocator
348348
);
349349

@@ -367,7 +367,7 @@ static size_t process_case_structure(
367367
control_type_t next_type = detect_control_type(next_erb);
368368

369369
if (next_type == CONTROL_TYPE_WHEN || next_type == CONTROL_TYPE_IN) {
370-
hb_array_T* statements = hb_array_init(8);
370+
hb_array_T* statements = hb_array_init(8, allocator);
371371
index++;
372372
index = process_block_children(node, array, index, statements, context, next_type);
373373

@@ -422,7 +422,7 @@ static size_t process_case_structure(
422422
AST_ERB_CONTENT_NODE_T* next_erb = NULL;
423423

424424
if (peek_control_type(array, index, &next_type, &next_erb) && next_type == CONTROL_TYPE_ELSE) {
425-
hb_array_T* else_children = hb_array_init(8);
425+
hb_array_T* else_children = hb_array_init(8, allocator);
426426
index++;
427427

428428
index = process_block_children(node, array, index, else_children, context, CONTROL_TYPE_CASE);
@@ -518,7 +518,7 @@ static size_t process_begin_structure(
518518
hb_allocator_T* allocator = context->allocator;
519519
AST_ERB_CONTENT_NODE_T* erb_node = get_erb_content_at(array, index);
520520
if (!erb_node) { return index; }
521-
hb_array_T* children = hb_array_init(8);
521+
hb_array_T* children = hb_array_init(8, allocator);
522522

523523
index++;
524524
index = process_block_children(node, array, index, children, context, CONTROL_TYPE_BEGIN);
@@ -537,7 +537,7 @@ static size_t process_begin_structure(
537537
}
538538

539539
if (peek_control_type(array, index, &next_type, &next_erb) && next_type == CONTROL_TYPE_ELSE) {
540-
hb_array_T* else_children = hb_array_init(8);
540+
hb_array_T* else_children = hb_array_init(8, allocator);
541541
index++;
542542

543543
index = process_block_children(node, array, index, else_children, context, CONTROL_TYPE_BEGIN);
@@ -560,7 +560,7 @@ static size_t process_begin_structure(
560560
}
561561

562562
if (peek_control_type(array, index, &next_type, &next_erb) && next_type == CONTROL_TYPE_ENSURE) {
563-
hb_array_T* ensure_children = hb_array_init(8);
563+
hb_array_T* ensure_children = hb_array_init(8, allocator);
564564
index++;
565565

566566
const control_type_t ensure_stop[] = { CONTROL_TYPE_END };
@@ -635,7 +635,7 @@ static size_t process_generic_structure(
635635
hb_allocator_T* allocator = context->allocator;
636636
AST_ERB_CONTENT_NODE_T* erb_node = get_erb_content_at(array, index);
637637
if (!erb_node) { return index; }
638-
hb_array_T* children = hb_array_init(8);
638+
hb_array_T* children = hb_array_init(8, allocator);
639639

640640
index++;
641641
index = process_block_children(node, array, index, children, context, initial_type);
@@ -690,7 +690,7 @@ static size_t process_subsequent_block(
690690
if (!erb_node) { return index; }
691691

692692
control_type_t type = detect_control_type(erb_node);
693-
hb_array_T* children = hb_array_init(8);
693+
hb_array_T* children = hb_array_init(8, allocator);
694694

695695
index++;
696696

@@ -772,7 +772,7 @@ static size_t process_block_children(
772772
if (is_terminator_type(parent_type, child_type)) { break; }
773773

774774
if (is_compound_control_type(child_type)) {
775-
hb_array_T* temp_array = hb_array_init(1);
775+
hb_array_T* temp_array = hb_array_init(1, context->allocator);
776776
size_t new_index = process_control_structure(node, array, index, temp_array, context, child_type);
777777

778778
if (hb_array_size(temp_array) > 0) { hb_array_append(children_array, hb_array_first(temp_array)); }
@@ -792,7 +792,7 @@ static size_t process_block_children(
792792

793793
hb_array_T* rewrite_node_array(AST_NODE_T* node, hb_array_T* array, analyze_ruby_context_T* context) {
794794
hb_allocator_T* allocator = context->allocator;
795-
hb_array_T* new_array = hb_array_init(hb_array_size(array));
795+
hb_array_T* new_array = hb_array_init(hb_array_size(array), allocator);
796796
size_t index = 0;
797797

798798
while (index < hb_array_size(array)) {
@@ -847,7 +847,7 @@ void herb_analyze_parse_tree(
847847
analyze_ruby_context_T context = {
848848
.document = document,
849849
.parent = NULL,
850-
.ruby_context_stack = hb_array_init(8),
850+
.ruby_context_stack = hb_array_init(8, allocator),
851851
.allocator = allocator,
852852
};
853853

src/analyze/conditional_elements.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ static void rewrite_conditional_elements(hb_array_T* nodes, hb_array_T* document
241241
}
242242
}
243243

244-
hb_array_T* open_stack = hb_array_init(8);
244+
hb_array_T* open_stack = hb_array_init(8, allocator);
245245

246246
for (size_t node_index = 0; node_index < hb_array_size(nodes); node_index++) {
247247
AST_NODE_T* node = (AST_NODE_T*) hb_array_get(nodes, node_index);
@@ -271,7 +271,7 @@ static void rewrite_conditional_elements(hb_array_T* nodes, hb_array_T* document
271271
}
272272
}
273273

274-
hb_array_T* consumed_indices = hb_array_init(8);
274+
hb_array_T* consumed_indices = hb_array_init(8, allocator);
275275

276276
for (size_t node_index = 0; node_index < hb_array_size(nodes); node_index++) {
277277
AST_NODE_T* node = (AST_NODE_T*) hb_array_get(nodes, node_index);
@@ -345,7 +345,7 @@ static void rewrite_conditional_elements(hb_array_T* nodes, hb_array_T* document
345345

346346
if (!matched_open) { continue; }
347347

348-
hb_array_T* body = hb_array_init(8);
348+
hb_array_T* body = hb_array_init(8, allocator);
349349

350350
for (size_t body_index = matched_open->open_index + 1; body_index < node_index; body_index++) {
351351
AST_NODE_T* body_node = (AST_NODE_T*) hb_array_get(nodes, body_index);
@@ -355,7 +355,7 @@ static void rewrite_conditional_elements(hb_array_T* nodes, hb_array_T* document
355355

356356
position_T start_position = matched_open->open_conditional->location.start;
357357
position_T end_position = node->location.end;
358-
hb_array_T* errors = hb_array_init(8);
358+
hb_array_T* errors = hb_array_init(0, allocator);
359359

360360
AST_HTML_CONDITIONAL_ELEMENT_NODE_T* conditional_element = ast_html_conditional_element_node_init(
361361
matched_open->condition,
@@ -406,7 +406,7 @@ static void rewrite_conditional_elements(hb_array_T* nodes, hb_array_T* document
406406
hb_array_free(&open_stack);
407407

408408
if (hb_array_size(consumed_indices) > 0) {
409-
hb_array_T* new_nodes = hb_array_init(hb_array_size(nodes));
409+
hb_array_T* new_nodes = hb_array_init(hb_array_size(nodes), allocator);
410410

411411
for (size_t node_index = 0; node_index < hb_array_size(nodes); node_index++) {
412412
bool consumed = false;

src/analyze/conditional_open_tags.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ static void add_multiple_tags_error_to_erb_node(
265265
allocator
266266
);
267267

268-
if (!erb_node->errors) { erb_node->errors = hb_array_init(1); }
268+
if (!erb_node->errors) { erb_node->errors = hb_array_init(0, allocator); }
269269

270270
hb_array_append(erb_node->errors, error);
271271
}
@@ -340,7 +340,7 @@ static void rewrite_conditional_open_tags(hb_array_T* nodes, hb_array_T* documen
340340

341341
if (!nodes || hb_array_size(nodes) == 0) { return; }
342342

343-
hb_array_T* consumed_indices = hb_array_init(8);
343+
hb_array_T* consumed_indices = hb_array_init(8, allocator);
344344

345345
for (size_t i = 0; i < hb_array_size(nodes); i++) {
346346
AST_NODE_T* node = (AST_NODE_T*) hb_array_get(nodes, i);
@@ -379,7 +379,7 @@ static void rewrite_conditional_open_tags(hb_array_T* nodes, hb_array_T* documen
379379

380380
if (close_index == (size_t) -1 || !close_tag) { continue; }
381381

382-
hb_array_T* body = hb_array_init(8);
382+
hb_array_T* body = hb_array_init(8, allocator);
383383

384384
for (size_t j = i + 1; j < close_index; j++) {
385385
AST_NODE_T* body_node = (AST_NODE_T*) hb_array_get(nodes, j);
@@ -389,7 +389,7 @@ static void rewrite_conditional_open_tags(hb_array_T* nodes, hb_array_T* documen
389389
position_T start_position = conditional_node->location.start;
390390
position_T end_position = close_tag->base.location.end;
391391

392-
hb_array_T* conditional_open_tag_errors = hb_array_init(1);
392+
hb_array_T* conditional_open_tag_errors = hb_array_init(0, allocator);
393393

394394
AST_HTML_CONDITIONAL_OPEN_TAG_NODE_T* conditional_open_tag = ast_html_conditional_open_tag_node_init(
395395
conditional_node,
@@ -401,7 +401,7 @@ static void rewrite_conditional_open_tags(hb_array_T* nodes, hb_array_T* documen
401401
allocator
402402
);
403403

404-
hb_array_T* element_errors = hb_array_init(1);
404+
hb_array_T* element_errors = hb_array_init(0, allocator);
405405

406406
AST_HTML_ELEMENT_NODE_T* element = ast_html_element_node_init(
407407
(AST_NODE_T*) conditional_open_tag,
@@ -429,7 +429,7 @@ static void rewrite_conditional_open_tags(hb_array_T* nodes, hb_array_T* documen
429429
}
430430

431431
if (hb_array_size(consumed_indices) > 0) {
432-
hb_array_T* new_nodes = hb_array_init(hb_array_size(nodes));
432+
hb_array_T* new_nodes = hb_array_init(hb_array_size(nodes), allocator);
433433

434434
for (size_t i = 0; i < hb_array_size(nodes); i++) {
435435
bool consumed = false;

src/ast_node.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,22 @@ size_t ast_node_sizeof(void) {
1515
return sizeof(struct AST_NODE_STRUCT);
1616
}
1717

18-
void ast_node_init(AST_NODE_T* node, const ast_node_type_T type, position_T start, position_T end, hb_array_T* errors) {
18+
void ast_node_init(
19+
AST_NODE_T* node,
20+
const ast_node_type_T type,
21+
position_T start,
22+
position_T end,
23+
hb_array_T* errors,
24+
hb_allocator_T* allocator
25+
) {
1926
if (!node) { return; }
2027

2128
node->type = type;
2229
node->location.start = start;
2330
node->location.end = end;
2431

2532
if (errors == NULL) {
26-
node->errors = hb_array_init(8);
33+
node->errors = hb_array_init(0, allocator);
2734
} else {
2835
node->errors = errors;
2936
}
@@ -34,7 +41,7 @@ AST_LITERAL_NODE_T* ast_literal_node_init_from_token(const token_T* token, hb_al
3441

3542
if (!literal) { return NULL; }
3643

37-
ast_node_init(&literal->base, AST_LITERAL_NODE, token->location.start, token->location.end, NULL);
44+
ast_node_init(&literal->base, AST_LITERAL_NODE, token->location.start, token->location.end, NULL, allocator);
3845

3946
literal->content = hb_string_copy(token->value, allocator);
4047

src/herb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ HERB_EXPORTED_FUNCTION hb_array_T* herb_lex(const char* source, hb_allocator_T*
1717
lexer_init(&lexer, source, allocator);
1818

1919
token_T* token = NULL;
20-
hb_array_T* tokens = hb_array_init(128);
20+
hb_array_T* tokens = hb_array_init(128, allocator);
2121

2222
while ((token = lexer_next_token(&lexer))->type != TOKEN_EOF) {
2323
hb_array_append(tokens, token);

src/include/ast_node.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,14 @@
77
#include "token_struct.h"
88
#include "util/hb_allocator.h"
99

10-
void ast_node_init(AST_NODE_T* node, ast_node_type_T type, position_T start, position_T end, hb_array_T* errors);
10+
void ast_node_init(
11+
AST_NODE_T* node,
12+
ast_node_type_T type,
13+
position_T start,
14+
position_T end,
15+
hb_array_T* errors,
16+
hb_allocator_T* allocator
17+
);
1118
void ast_node_free(AST_NODE_T* node, hb_allocator_T* allocator);
1219

1320
AST_LITERAL_NODE_T* ast_literal_node_init_from_token(const token_T* token, hb_allocator_T* allocator);

src/include/util/hb_array.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,16 @@
44
#include <stdbool.h>
55
#include <stdlib.h>
66

7+
struct hb_allocator;
8+
79
typedef struct HB_ARRAY_STRUCT {
810
void** items;
911
size_t size;
1012
size_t capacity;
13+
struct hb_allocator* allocator;
1114
} hb_array_T;
1215

13-
hb_array_T* hb_array_init(size_t capacity);
16+
hb_array_T* hb_array_init(size_t capacity, struct hb_allocator* allocator);
1417

1518
void* hb_array_get(const hb_array_T* array, size_t index);
1619
void* hb_array_first(hb_array_T* array);

0 commit comments

Comments
 (0)