From 833978f41433c526b9614f8c772896a0bd6e31cc Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Sat, 23 May 2026 11:35:31 -0400 Subject: core: tokbuf: Dynamically expand token buffer Signed-off-by: Ian Moffett --- core/tokbuf.c | 33 ++++++++++++++++++++++++++++++--- 1 file changed, 30 insertions(+), 3 deletions(-) (limited to 'core/tokbuf.c') diff --git a/core/tokbuf.c b/core/tokbuf.c index 05e8755..6df5686 100644 --- a/core/tokbuf.c +++ b/core/tokbuf.c @@ -5,6 +5,7 @@ #include #include +#include #include #include #include @@ -18,20 +19,33 @@ tokbuf_init(struct tokbuf *tokbuf) return -1; } - memset(tokbuf->buf, 0, sizeof(tokbuf->buf)); tokbuf->head = 0; + tokbuf->cap = TOKBUF_CAP; + tokbuf->buf = malloc(sizeof(struct token) * TOKBUF_CAP); + if (tokbuf->buf == NULL) { + return -1; + } + return 0; } int tokbuf_push(struct tokbuf *tokbuf, struct token *tok) { + void *p; + if (tokbuf == NULL || tok == NULL) { return -1; } - if ((tokbuf->head++) >= TOKBUF_CAP - 1) { - tokbuf->head = 0; + if ((tokbuf->head++) >= tokbuf->cap - 1) { + tokbuf->cap += 8; + p = realloc(tokbuf->buf, sizeof(struct token) * tokbuf->cap); + if (tokbuf->buf == NULL) { + return -1; + } + + tokbuf->buf = p; } tokbuf->buf[tokbuf->head] = *tok; @@ -60,3 +74,16 @@ tokbuf_noff(struct tokbuf *tokbuf, size_t noff, struct token *res) *res = tokbuf->buf[off]; return 0; } + +void +tokbuf_destroy(struct tokbuf *tokbuf) +{ + if (tokbuf == NULL) { + return; + } + + if (tokbuf->buf != NULL) { + free(tokbuf->buf); + tokbuf->buf = NULL; + } +} -- cgit v1.2.3