From e6bbab41e5f19a1922401a94e25b07e57fe89592 Mon Sep 17 00:00:00 2001 From: Max Brunsfeld Date: Sat, 30 Aug 2014 21:39:55 -0700 Subject: [PATCH] Realloc parse stack when it grows to its capacity --- src/runtime/stack.c | 5 +++++ src/runtime/stack.h | 1 + 2 files changed, 6 insertions(+) diff --git a/src/runtime/stack.c b/src/runtime/stack.c index 50f9f763..e272157c 100644 --- a/src/runtime/stack.c +++ b/src/runtime/stack.c @@ -8,6 +8,7 @@ static TSStateId INITIAL_STATE = 0; TSStack ts_stack_make() { TSStack result = { .entries = calloc(INITIAL_STACK_SIZE, sizeof(*result.entries)), .size = 0, + .capacity = INITIAL_STACK_SIZE, }; return result; } @@ -30,6 +31,10 @@ TSTree *ts_stack_top_node(const TSStack *stack) { } void ts_stack_push(TSStack *stack, TSStateId state, TSTree *node) { + if (stack->size == stack->capacity) { + stack->capacity *= 2; + stack->entries = realloc(stack->entries, stack->capacity * sizeof(*stack->entries)); + } stack->entries[stack->size].state = state; stack->entries[stack->size].node = node; stack->size++; diff --git a/src/runtime/stack.h b/src/runtime/stack.h index ba523e56..2ba2582a 100644 --- a/src/runtime/stack.h +++ b/src/runtime/stack.h @@ -9,6 +9,7 @@ extern "C" { typedef struct { size_t size; + size_t capacity; struct { TSTree *node; TSStateId state;