public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
* [PATCH olang v2] utils: add arena
@ 2024-02-20 16:39 Carlos Maniero
  2024-02-20 16:45 ` [olang/patches/.build.yml] build failed builds.sr.ht
  2024-02-20 19:13 ` [PATCH olang v2] utils: add arena Carlos Maniero
  0 siblings, 2 replies; 3+ messages in thread
From: Carlos Maniero @ 2024-02-20 16:39 UTC (permalink / raw)
  To: ~johnnyrichard/olang-devel; +Cc: Carlos Maniero

This is a simple implementation of arena. At this point arena is not
expandable. Since the size of arena is predicable and basically used
used to store pointers, but we can make it more robust in the future.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
v2:
  - fix lint
  - improve commit message

 .gitignore              |  2 +-
 Makefile                |  8 ++++--
 src/arena.c             | 53 +++++++++++++++++++++++++++++++++++
 src/arena.h             | 41 +++++++++++++++++++++++++++
 tests/unit/Makefile     | 19 ++++++++-----
 tests/unit/arena_test.c | 62 +++++++++++++++++++++++++++++++++++++++++
 6 files changed, 175 insertions(+), 10 deletions(-)
 create mode 100644 src/arena.c
 create mode 100644 src/arena.h
 create mode 100644 tests/unit/arena_test.c

diff --git a/.gitignore b/.gitignore
index 92496d7..fc7d161 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,4 +2,4 @@
 build
 *.o
 docs/site.tar.gz
-tests/integration/*_test
+tests/*/*_test
diff --git a/Makefile b/Makefile
index 555029f..ea6ba53 100644
--- a/Makefile
+++ b/Makefile
@@ -20,11 +20,13 @@ $(BUILD_DIR):
 linter: $(SRCS) $(HEADERS)
 	clang-format --dry-run --Werror $?
 	$(MAKE) -C tests/integration/ linter
+	$(MAKE) -C tests/unit/ linter
 
 .PHONY: linter-fix
 linter-fix: $(SRCS) $(HEADERS)
 	clang-format -i $?
 	$(MAKE) -C tests/integration/ linter-fix
+	$(MAKE) -C tests/unit/ linter-fix
 
 .PHONY: integration-test
 integration-test:
@@ -33,12 +35,14 @@ integration-test:
 
 .PHONY: unit-test
 unit-test:
+	$(MAKE)
 	$(MAKE) -C tests/unit/
 
 .PHONY: check
 check:
-	$(MAKE) integration-test
-	$(MAKE) unit-test
+	$(MAKE)
+	$(MAKE) -C tests/integration/
+	$(MAKE) -C tests/unit/
 
 .PHONY: docs
 docs:
diff --git a/src/arena.c b/src/arena.c
new file mode 100644
index 0000000..3d084ab
--- /dev/null
+++ b/src/arena.c
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2024 olang maintainers
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+#include "arena.h"
+#include <stdlib.h>
+#include <stdio.h>
+
+arena_t
+arena_new(size_t size)
+{
+    arena_t arena;
+    arena.offset = 0;
+    arena.region = malloc(sizeof(uint8_t) * size);
+    arena.size = size;
+    return arena;
+}
+
+void *
+arena_alloc(arena_t *arena, size_t size)
+{
+    if ((arena->offset + size) > arena->size) {
+        return NULL;
+    }
+    void *pointer = arena->region + arena->offset;
+    arena->offset += size;
+    return pointer;
+}
+
+void
+arena_release(arena_t *arena)
+{
+    arena->offset = 0;
+}
+
+void
+arena_free(arena_t *arena)
+{
+    arena->size = 0;
+    free(arena->region);
+}
diff --git a/src/arena.h b/src/arena.h
new file mode 100644
index 0000000..a3e97aa
--- /dev/null
+++ b/src/arena.h
@@ -0,0 +1,41 @@
+/*
+ * Copyright (C) 2024 olang maintainers
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+#ifndef ARENA_H
+#define ARENA_H
+#include <stdlib.h>
+#include <stdint.h>
+
+typedef struct arena
+{
+    size_t offset;
+    size_t size;
+    uint8_t *region;
+} arena_t;
+
+arena_t
+arena_new(size_t size);
+
+void *
+arena_alloc(arena_t *arena, size_t size);
+
+void
+arena_release(arena_t *arena);
+
+void
+arena_free(arena_t *arena);
+
+#endif
diff --git a/tests/unit/Makefile b/tests/unit/Makefile
index ab250cf..7c6a8b3 100644
--- a/tests/unit/Makefile
+++ b/tests/unit/Makefile
@@ -1,10 +1,11 @@
-SRCS        := $(wildcard *_test.c)
-OBJS        := $(patsubst %_test.c, %_test.o, $(SRCS))
-CFLAGS      := -I../../src -I../shared
-TESTS       := $(patsubst %_test.c, %_test, $(SRCS))
-EXEC_TESTS  := $(patsubst %_test, ./%_test, $(TESTS))
-MUNIT_SRC   := ../shared/munit.c
-MUNIT       := ./munit.o
+SRCS         := $(wildcard *_test.c)
+OBJS         := $(patsubst %_test.c, %_test.o, $(SRCS))
+SUBJECT_OBJS := $(filter-out ../../build/0c.o, $(wildcard ../../build/*.o))
+CFLAGS       := -I../../src -I../shared
+TESTS        := $(patsubst %_test.c, %_test, $(SRCS))
+EXEC_TESTS   := $(patsubst %_test, ./%_test, $(TESTS))
+MUNIT_SRC    := ../shared/munit.c
+MUNIT        := ./munit.o
 
 .PHONY: all
 all: $(MUNIT) $(TESTS)
@@ -15,6 +16,7 @@ all: $(MUNIT) $(TESTS)
 .PHONY: clean
 clean:
 	$(RM) *.o *_test
+	$(RM) -rfv lib
 
 .PHONY: linter
 linter: $(SRCS)
@@ -24,5 +26,8 @@ linter: $(SRCS)
 linter-fix: $(SRCS)
 	clang-format -i $?
 
+%_test: $(MUNIT) $(SUBJECT_OBJS) %_test.c
+	$(CC) $? $(CFLAGS) -o $@
+
 $(MUNIT):
 	$(CC) -c $(MUNIT_SRC) $(CFLAGS) -o $(MUNIT)
diff --git a/tests/unit/arena_test.c b/tests/unit/arena_test.c
new file mode 100644
index 0000000..6310795
--- /dev/null
+++ b/tests/unit/arena_test.c
@@ -0,0 +1,62 @@
+/*
+ * Copyright (C) 2024 olang maintainers
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+#define MUNIT_ENABLE_ASSERT_ALIASES
+#include "arena.h"
+#include "munit.h"
+
+static MunitResult
+arena_test(const MunitParameter params[], void *user_data_or_fixture)
+{
+    arena_t arena = arena_new(sizeof(int) * 2);
+
+    int *a = arena_alloc(&arena, sizeof(int));
+    *a = 1;
+
+    int *b = arena_alloc(&arena, sizeof(int));
+    *b = 2;
+
+    munit_assert_int(*a, ==, 1);
+    munit_assert_int(*b, ==, 2);
+
+    arena_release(&arena);
+
+    int *c = arena_alloc(&arena, sizeof(int));
+    *c = 3;
+
+    munit_assert_int(*c, ==, 3);
+    munit_assert_int(*a, ==, 3);
+    munit_assert_int(*b, ==, 2);
+
+    munit_assert_ptr_not_null(arena_alloc(&arena, sizeof(int)));
+    munit_assert_ptr_null(arena_alloc(&arena, 1));
+
+    arena_free(&arena);
+
+    return MUNIT_OK;
+}
+
+static MunitTest tests[] = { { "/arena_test", arena_test, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL },
+                             { NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL } };
+
+static const MunitSuite suite = { "/cli_test", tests, NULL, 1, MUNIT_SUITE_OPTION_NONE };
+
+int
+main(int argc, char *argv[])
+{
+    return munit_suite_main(&suite, NULL, argc, argv);
+    return EXIT_SUCCESS;
+}
-- 
2.34.1


^ permalink raw reply	[flat|nested] 3+ messages in thread

* [olang/patches/.build.yml] build failed
  2024-02-20 16:39 [PATCH olang v2] utils: add arena Carlos Maniero
@ 2024-02-20 16:45 ` builds.sr.ht
  2024-02-20 19:13 ` [PATCH olang v2] utils: add arena Carlos Maniero
  1 sibling, 0 replies; 3+ messages in thread
From: builds.sr.ht @ 2024-02-20 16:45 UTC (permalink / raw)
  To: Carlos Maniero; +Cc: ~johnnyrichard/olang-devel

olang/patches/.build.yml: FAILED in 36s

[utils: add arena][0] v2 from [Carlos Maniero][1]

[0]: https://lists.sr.ht/~johnnyrichard/olang-devel/patches/49708
[1]: mailto:carlos@maniero.me

✗ #1154158 FAILED olang/patches/.build.yml https://builds.sr.ht/~johnnyrichard/job/1154158

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH olang v2] utils: add arena
  2024-02-20 16:39 [PATCH olang v2] utils: add arena Carlos Maniero
  2024-02-20 16:45 ` [olang/patches/.build.yml] build failed builds.sr.ht
@ 2024-02-20 19:13 ` Carlos Maniero
  1 sibling, 0 replies; 3+ messages in thread
From: Carlos Maniero @ 2024-02-20 19:13 UTC (permalink / raw)
  To: Carlos Maniero, ~johnnyrichard/olang-devel

I sent a v3 fixing the issues.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-02-20 19:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-20 16:39 [PATCH olang v2] utils: add arena Carlos Maniero
2024-02-20 16:45 ` [olang/patches/.build.yml] build failed builds.sr.ht
2024-02-20 19:13 ` [PATCH olang v2] utils: add arena Carlos Maniero

Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox