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 buffalo.birch.relay.mailchannels.net (buffalo.birch.relay.mailchannels.net [23.83.209.24]) by mail-a.sr.ht (Postfix) with ESMTPS id CD4B7200ED for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 20 Feb 2024 16:45:14 +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 D8BFD803BB4 for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 20 Feb 2024 16:45:06 +0000 (UTC) Received: from nl-srv-smtpout4.hostinger.io (unknown [127.0.0.6]) (Authenticated sender: hostingeremail) by relay.mailchannels.net (Postfix) with ESMTPA id 06B51802265 for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 20 Feb 2024 16:45:05 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1708447506; a=rsa-sha256; cv=none; b=2jfMs7zEwjRB/6LcxDRLUCbc5XudwvJ/aOc2Zot1oOrVXAGLp+Cy2nT2e3+82MwPvGNDBR KGYHIYlBavpxstPUG5NejVogS3TA0aYM7QNPwBlNe1LKFodBKGxq5pein+CRtx/qfouYFE q0e4aI9gW2q1wVWwgH4aO+iEs1WXyu1/vqrvrxjgAw3o9IFofgThEE+OsUHeCbc1b/Vgoe qAe32C9ljnvynAgQ2RLNuZhs3E+KOKLbU08+lwCHdWyxjFoF6589HJSQk4rq5nrsuwOz1D Un8TS+7GtWUrhFGiuvXnCa3DvtUcc/0iKwbyIOH+XcVTkpHY4T6T4N0DKGMHbA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1708447506; 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=s5dl4diH/AwrqH7V1JB1x0YRoKHK3XN3xsGEyBgqrVs=; b=dgsaTpWrLOmBIVNDFCXam71xt42wCTWEFtxMNbiNzs7i5xmiVWHrv7+9LF7nvdGel5bVu3 NznFZ+6MVDUKwqF51oTkUCJobxphbh8Yss10kgKqrKg/BQOMDlXc7DhGESdyZwas1kuPMI TOof2roIXb4YDBqN8235n6IynNPYduSpUXiTTxKOC9MqknP5QE3Mz3AuzR4B7FaZ2sUGnB VnmTaExcWazNb+whycWf/vSytbYnE0JFwgrN13QdrJhNrR6O8nrmYXuGu6Weido9G6MVFc HHr2dbl9DpHc+4BiiWAM/ibe4lS2ftS94GnyQ+Jr6q9jfTqtVvnpFpbMMFFTkA== ARC-Authentication-Results: i=1; rspamd-55b4bfd7cb-djncz; 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-Callous-Lettuce: 30fd9e800293875d_1708447506548_2433956236 X-MC-Loop-Signature: 1708447506548:2062857514 X-MC-Ingress-Time: 1708447506548 Received: from nl-srv-smtpout4.hostinger.io (nl-srv-smtpout4.hostinger.io [145.14.159.45]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384) by 100.107.7.251 (trex/6.9.2); Tue, 20 Feb 2024 16:45:06 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1708447504; 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=s5dl4diH/AwrqH7V1JB1x0YRoKHK3XN3xsGEyBgqrVs=; b=pT4ObQixEkCzyLAjxiTY78SUd+m7jE/cL4dHgNufDQ1SleeqFDAAAKw6Ub4RfMStVGfZ87 jjEVjL9AtEqIhitOu9DKKnftfUk5Ztn2ogoMs7ouAmrV06nnEFW7l6ILpwLzmA9KkJQwTm Z5ixLPPtwpCecW+8/NuKJwQpgaOQBN3vNSNP2jwKuDdX6W56GYM4Bx4y5QireXtcygTZAJ bwfnAe7pAD4MXnwi1uCpZeMSq2kX0p1RJ12VLOEb4i1JpYlR6xSxkf00AFg+tdPL3Pcuwm 2YpkkGTVHbL+GeLGfvY9/YaIMs3n1RRWAoXIY1lqGqoKcTahWNzwjh8ORaO8eg== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang v2] utils: add arena Date: Tue, 20 Feb 2024 13:39:22 -0300 Message-Id: <20240220163921.2589292-1-carlos@maniero.me> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-Envelope: MS4xfHOXP88pCbNZwGWiWgNgo5XYtJru9d8az0JQ5Chaw1Y/y6cQSdVd0ssk0RXfTAlN31Megm+u66LoffRS92LTReI2FcD67WnSTAs/ZA7L2Kea0nu/CWJt 8yzmY7+rQtq6m5Ly53alUkqnXFXTBIYEFq3XEcl6vElcFpkVVKqL4SZ3GNmKRe3lqMioPZD/F3IQx0EG33CsXLQ6vMjPn6j7+Y81H6qFxTrS8FDM+h0vzyFZ M3ODBopqqbTS+n7fxoTSDg== X-CM-Analysis: v=2.4 cv=DIOJ4TNb c=1 sm=1 tr=0 ts=65d4d710 a=5+VMC1FZ3J4mVPAKpPmAqg==:117 a=5+VMC1FZ3J4mVPAKpPmAqg==:17 a=MKtGQD3n3ToA:10 a=1oJP67jkp3AA:10 a=BXDaF_L80NYA:10 a=mDV3o1hIAAAA:8 a=iN48IfCQSmxRoK7lwpgA:9 a=ECJJpeYe7XspiZ0Pv2Ui:22 a=_FVE-zBwftR9WsbkzFJk:22 X-AuthUser: carlos@maniero.me X-TUID: cDF3rYhD1Lc/ 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 --- 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 . + */ +#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