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 heron.birch.relay.mailchannels.net (heron.birch.relay.mailchannels.net [23.83.209.82]) by mail-a.sr.ht (Postfix) with ESMTPS id AFE9D20108 for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 20 Feb 2024 17:40:47 +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 9587E941132 for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 20 Feb 2024 17:40:34 +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 D07C99437F5 for <~johnnyrichard/olang-devel@lists.sr.ht>; Tue, 20 Feb 2024 17:40:33 +0000 (UTC) ARC-Seal: i=1; s=arc-2022; d=mailchannels.net; t=1708450834; a=rsa-sha256; cv=none; b=IVCW1h/zuVtP2C1j2wI4qWJPEosIG84kqeZLbH588YSYFAcun2BMTMPxZW6QQqCLdtWHte iKOr2zbx8acM2ep5wJmMPcZv6vLDJdlGmsfJFoyaDjWvtfJdNJqUYYYfReFU8o+w8Tj21i vGiwyTesPCgWgJQ5Cxl/lXHD7Qnnp7UYzOGm7sDxqpZTCDBgWgEVajM/d5zfcll9xRZ01W BWyocK8hB1qnK5byD+yvURKKaEC6qr5hEWFzwpDfJD9HyP2YCbl/6btYCpHaQ4Qh42Zovi yAJnU0VP2zwlyUSjgZWzN2VhHZtPgH0LR9Ob2+dpZfj0MqtHtQX9lny4eVojJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=mailchannels.net; s=arc-2022; t=1708450834; 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=TpX79TqKLH0JOlQmvlHWOZIu18Th5gt9ZXyzMhYGdMA=; b=92dtoPwwYZ1hYh7GJwmEKvGFFED85InuuF+LDH839104gbZL2QxY6SwpOgw2lAqodA8/mr TJnCjd/cNVUqvpXr0yP/o1zMNnCBriqGT9aKqu/HVgQ8SO0PR8yzR68PTLL2U8omijRsIz ck9ctp2YS2LeMbdvTjR8JzdGhE154nff3qvCfgVtSdzEuQ7QroK+EINYwa5gT9PmQmt/DC JRjCY6ic/4ZdpYIy8n39oGxBus3uPyXW2+gr2GJFPxrGfZPl/BC2zqhJCiqZGat4y0Pwd8 b6e/yBo/7SC7ejg9cbolu20vAZ0ptkh0FDYdPF/LXGbaa+tndLHofkE1aVlaLg== ARC-Authentication-Results: i=1; rspamd-6bdc45795d-stsw5; 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-Scare-Trail: 15fabdf347acb9bb_1708450834374_352377251 X-MC-Loop-Signature: 1708450834374:2288744347 X-MC-Ingress-Time: 1708450834374 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.120.66.49 (trex/6.9.2); Tue, 20 Feb 2024 17:40:34 +0000 From: Carlos Maniero DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=maniero.me; s=hostingermail1; t=1708450831; 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=TpX79TqKLH0JOlQmvlHWOZIu18Th5gt9ZXyzMhYGdMA=; b=gwzNelpy2CFSOtu+c8eY7LM6oNECrRL09fchZcuDHUTbcM9uYMQhJlq1LjGPosjyU93Hnt +7aKvqV4kaIKC1s0ugSFWTGAnuHHflZD9PCGHDRR0JP+vW86WMpOHAoFNk3QqgszPAMAhI GmxtTTEYwwbLtnL72uLGQxZdmE6vbi4dgv+tZWzS9RX2sJYrmjEzylHLnPKazG/fkFAXKE d/Zw1IRnIZZwHFJlzpEZ3AUv0dibxCsUPpLoItfOk9jGD1FSaDO2VaMI8sBC8wltKjyoUj CLkZLZXWH5mxIg2yH3BT3lxFQiJTzc/2rzCTJM4NbYB7ainAzBYNBWCXD1AfVg== To: ~johnnyrichard/olang-devel@lists.sr.ht Cc: Carlos Maniero Subject: [PATCH olang v3] utils: add arena Date: Tue, 20 Feb 2024 14:35:54 -0300 Message-Id: <20240220173554.2644499-1-carlos@maniero.me> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-CM-Analysis: v=2.4 cv=DIOJ4TNb c=1 sm=1 tr=0 ts=65d4e40f 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=AYHgycvw1umsTwmq_K9G:22 a=_FVE-zBwftR9WsbkzFJk:22 X-CM-Envelope: MS4xfC6wEwF5qqVzpmCPlzEVaIpRRvlM3LSmecw7lEXPQUgx0QVsD9zzWGQwcNbh033YGiKIiKF2APe8fvN06Uj8v5FSrcCdVT9VkJkPrQczYGDXRoz5wdKM Hw6DzSGKXNCYTOAtujmPulSJUjrwGTDyitXX3hyhXNBaKjtk6kXuh04kLj2tff+UhoHzt1Qveguz0xM/kpbPHT/BgEsx+f6AkntaXo01rA2E91aNpOsDDKm9 X-AuthUser: carlos@maniero.me X-TUID: 2nznywli02zx 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 --- .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..ae33e6a --- /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..157165c --- /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