From f41d3a949cc413ff3171e81494213c563a0c9f4d Mon Sep 17 00:00:00 2001 From: Camden Dixie O'Brien Date: Sat, 9 Aug 2025 17:01:56 +0100 Subject: [PATCH] Create simple, bump allocator store --- lib/CMakeLists.txt | 1 + lib/include/store.h | 16 ++++++++++++++++ lib/store.c | 14 ++++++++++++++ tests/CMakeLists.txt | 1 + tests/store_tests.c | 34 ++++++++++++++++++++++++++++++++++ 5 files changed, 66 insertions(+) create mode 100644 lib/include/store.h create mode 100644 lib/store.c create mode 100644 tests/store_tests.c diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 09dd5fc..3055247 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -1,5 +1,6 @@ add_library(imp memory_stream.c + store.c token.c ) target_include_directories(imp PUBLIC include) diff --git a/lib/include/store.h b/lib/include/store.h new file mode 100644 index 0000000..73c540e --- /dev/null +++ b/lib/include/store.h @@ -0,0 +1,16 @@ +#ifndef STORE_H +#define STORE_H + +#include "expr.h" + +#define STORE_SIZE 256U + +typedef struct { + expr_t *free; + expr_t buffer[STORE_SIZE]; +} store_t; + +void store_init(store_t *store); +expr_t *store_alloc(store_t *store); + +#endif diff --git a/lib/store.c b/lib/store.c new file mode 100644 index 0000000..b944f05 --- /dev/null +++ b/lib/store.c @@ -0,0 +1,14 @@ +#include "store.h" + +#include + +void store_init(store_t *store) +{ + memset(store, 0, sizeof(store_t)); + store->free = store->buffer; +} + +expr_t *store_alloc(store_t *store) +{ + return store->free++; +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1e1b47b..ddf0a5f 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -9,5 +9,6 @@ function(add_test_suites) endfunction() add_test_suites( + store_tests.c token_tests.c ) diff --git a/tests/store_tests.c b/tests/store_tests.c new file mode 100644 index 0000000..d09ff29 --- /dev/null +++ b/tests/store_tests.c @@ -0,0 +1,34 @@ +#include "store.h" +#include "unity.h" + +static store_t store; + +void setUp(void) +{ + store_init(&store); +} + +void tearDown(void) +{ +} + +static void test_alloc_returns_non_null_after_init(void) +{ + const expr_t *const expr = store_alloc(&store); + TEST_ASSERT_NOT_NULL(expr); +} + +static void test_two_calls_to_alloc_return_distinct(void) +{ + const expr_t *const a = store_alloc(&store); + const expr_t *const b = store_alloc(&store); + TEST_ASSERT_NOT_EQUAL(a, b); +} + +int main(void) +{ + UNITY_BEGIN(); + RUN_TEST(test_alloc_returns_non_null_after_init); + RUN_TEST(test_two_calls_to_alloc_return_distinct); + return UNITY_END(); +}