From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: mail-a.sr.ht; dkim=pass header.d=maniero.me header.i=@maniero.me Received: from bisque.elm.relay.mailchannels.net (bisque.elm.relay.mailchannels.net [23.83.212.18]) by mail-a.sr.ht (Postfix) with ESMTPS id E61A7200BC for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 20 Feb 2024 16:15:03 +0000 (UTC) X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id D42166C3B98 for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 20 Feb 2024 16:15:01 +0000 (UTC) Received: from fr-int-smtpout5.hostinger.io (unknown [127.0.0.6]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id C783E6C3AFF for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 20 Feb 2024 16:15:00 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1708445701; a=rsa-sha256; cv=none; b=SZIopF8uvu2N5b5O8axtZdRpOrpGzMeO5TiQr6hZ1jJeVjNaJDr8BBVY/pYfSCeIlZ+rjQ HuA6lIDzKWGfBdepSP5Fc4nRy5IYCu/5/gE3gYv1skuOD1iEcE9zY63sX28S5s1QjVVj0h YPPwrNEgLkiEz8wtfkF32pELnDB79QoeTTsU1H9aR+YlNemE7VwLeCu0bFes9TeiEotCww qx9SKC19afX0v2pRXxxMrBVd7AiC2tyl3cZ5Twk0h45L3bKeL/bK4cAMvwKUM7PYd8vrXm AiopA/z8gW4EzlKfZgKJQ8tPc+ze6wPo51bzagGRjQihDUL6q+RxCzeqNeQ8uQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1708445701; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding:dkim-signature; bh=Z4TjeCGI+rNvPTArZ7JrOehcO2ht2PTYvg/xi24etYk=; b=4YntExczzr1vE+IP38Uha4LHrzuy4YaelDVAgwB9Ko/sWMA5moGPlZ0EIl4hsmRFzIQsIz CLnt2yf7Oq+Ykb+XaYYXh6oSMvDLC/YzFgNB7H4Pne5VMftfFcpFbM/mxcSzLdEOmv706O Epq+M5+YbbfKk0C+4Hzkw16694TpUmVqdW5YSeWDAEUC6/nIVoE63slagClJzcAoQhInoV jTJ6fzCxfXiStwKppfp/FwZOFMj7WPEbyioYWSCuo/6UGTmLnip3uEubIT28v2aBa9vBen pBEW8scucPgEsYULmJAe51dvvlZDpS66fW7PQPwPCG49BAcxb+/jQBOjez//wQ== ARC-Authentication-Results: i=1; rspamd-6bdc45795d-xsq8r; auth=pass smtp.auth=hostingeremail smtp.mailfrom=carlos@maniero.me X-Sender-Id: hostingeremail|x-authuser|carlos@maniero.me X-MC-Relay: Neutral X-MailChannels-SenderId: hostingeremail|x-authuser|carlos@maniero.me X-MailChannels-Auth-Id: hostingeremail X-Broad-Wide-Eyed: 2af7d39d26f3eb35_1708445701395_841373775 X-MC-Loop-Signature: 1708445701395:1116029963 X-MC-Ingress-Time: 1708445701395 Received: from fr-int-smtpout5.hostinger.io ([UNAVAILABLE]. [89.116.146.168]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.119.153.87 (trex/6.9.2); Tue, 20 Feb 2024 16:15:01 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1708445698; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=Z4TjeCGI+rNvPTArZ7JrOehcO2ht2PTYvg/xi24etYk=; b=v1Q3jSPK+VtKbGKXvhNutiF39+MIs2KG1NFAG5Bf43KUgqFAR97zkhSI2X/LiSj2I7H23N EQavOF5f4zw+uS81xSpNCbTVwtdtQ4OKK003FxJmISsR0Mx4S6m3VfMG0hKZ5AG+RLYV/R SealOUpJYsnbKq6hY3W6iTbouKIwvH+wYkuR593yBEWy5WnFq+jfhFpkQAvqtD2KwhnCEL HaiYCKVRHdeCHSYiycUiN4Mdar+mlPShJHhQMgRUCMqlEsEtzA7z2TGWbbB1R2pcJKlGGS pMJxLq3nNPiKkeLjXO35/HxDcc1Ooc7xlOakmmRzyXyBrjx/vADuzNPk3dlCOA== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang] utils: add arena Date: Tue, 20 Feb 2024 13:10:17 -0300 Message-Id: <20240220161017.2571592-1-carlos@maniero.me> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-Envelope: MS4xfKrGTFup2i6QhjHu4Wogv04PLCj0mLFCL+RHyUU4FSWNMuZNe0NupWL22rleylOubpBtujIjip4/WQ3lKSGGOpws/zs+AeTcjBajjjguU7bIkgVsnlMu zIzYMRXRrPFmdFugYWbhD2Kh/klHUN7Gr71xwwlKY6fCE2z9B2EnlAek1y3+uZtyV1B+Mgk7wK5+3XWN2V3qHB/3bJsXFO39StfVraY/edC6T/O1Bc2KApIZ tDioEdtxzm3qPubW2wMQ0Q== X-CM-Analysis: v=2.4 cv=FdIxxo+6 c=1 sm=1 tr=0 ts=65d4d002 a=5+VMC1FZ3J4mVPAKpPmAqg==:117 a=5+VMC1FZ3J4mVPAKpPmAqg==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=BXDaF_L80NYA:10 a=mDV3o1hIAAAA:8 a=34dqGL1_60QQHZ6mx_YA:9 a=ECJJpeYe7XspiZ0Pv2Ui:22 a=_FVE-zBwftR9WsbkzFJk:22 X-AuthUser: carlos@maniero.me X-TUID: sZzl6hoIvgSA This is a simple implementation of arena. At this point arena is not expandable. We can add support to it if neded after we implement linked lists. Since arena was the first code unit-tested, I've completed the unit tests setup at this commit. Signed-off-by: Carlos Maniero --- .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 . + */ +#include "arena.h" +#include +#include + +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 . + */ +#ifndef ARENA_H +#define ARENA_H +#include +#include + +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 . + */ +#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