From 2de2bd967983f4e607da72b1643f513d46aa873d Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Mon, 20 Jun 2022 18:19:15 +0300 Subject: [PATCH] Header macro for private struct attributes (#72) --- ChangeLog | 4 ++++ Makefile.am | 2 ++ README.md | 2 +- include/Makefile.am | 1 + include/kernaux.h.in | 1 + include/kernaux/alloc.h | 12 +++++++----- include/kernaux/macro.h | 18 ++++++++++++++++++ include/kernaux/memmap.h | 10 ++++++---- tests/test_alloc.c | 2 ++ tests/test_memmap.c | 2 ++ 10 files changed, 44 insertions(+), 10 deletions(-) create mode 100644 include/kernaux/macro.h diff --git a/ChangeLog b/ChangeLog index 43561ea..ce2d424 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,7 @@ +2022-06-20 Alex Kotov + + * include/kernaux/alloc.h: Finished, made stable + 2022-06-18 Alex Kotov * include/kernaux/mutex.h: Added diff --git a/Makefile.am b/Makefile.am index f47cf70..2f74607 100644 --- a/Makefile.am +++ b/Makefile.am @@ -19,6 +19,8 @@ endif libc/libc.la: $(MAKE) $(AM_MAKEFLAGS) -C $(top_builddir)/libc libc.la +AM_CFLAGS += -DKERNAUX_PRIVATE_NO + lib_LTLIBRARIES = libkernaux.la libkernaux_la_SOURCES = src/libc.h src/assert.c src/mutex.c diff --git a/README.md b/README.md index 892547c..807e2d0 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,7 @@ zero). Work-in-progress APIs can change at any time. * [Framebuffer](/include/kernaux/framebuffer.h) (*planned*) * USB (*planned*) * Algorithms - * [Memory allocator](/include/kernaux/alloc.h) (*work in progress*) + * [Memory allocator](/include/kernaux/alloc.h) (*non-breaking since* **?.?.?**) * [Simple command line parser](/include/kernaux/cmdline.h) (*non-breaking since* **0.2.0**) * [Example](/examples/cmdline.c) * [Page Frame Allocator](/include/kernaux/pfa.h) (*work in progress*) diff --git a/include/Makefile.am b/include/Makefile.am index eb4e592..8755e0f 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -4,6 +4,7 @@ nobase_include_HEADERS = \ kernaux/arch/riscv64.h \ kernaux/arch/x86_64.h \ kernaux/assert.h \ + kernaux/macro.h \ kernaux/mutex.h \ kernaux/version.h diff --git a/include/kernaux.h.in b/include/kernaux.h.in index f3e7ca2..eabdfee 100644 --- a/include/kernaux.h.in +++ b/include/kernaux.h.in @@ -4,6 +4,7 @@ */ #include +#include #include #include diff --git a/include/kernaux/alloc.h b/include/kernaux/alloc.h index 8e5f6c3..2a41107 100644 --- a/include/kernaux/alloc.h +++ b/include/kernaux/alloc.h @@ -5,19 +5,21 @@ extern "C" { #endif +#include #include #include typedef struct KernAux_Alloc_Node { - struct KernAux_Alloc_Node *next, *prev; - size_t size; - char *block; + struct KernAux_Alloc_Node *KERNAUX_PRIVATE_FIELD(next); + struct KernAux_Alloc_Node *KERNAUX_PRIVATE_FIELD(prev); + size_t KERNAUX_PRIVATE_FIELD(size); + char *KERNAUX_PRIVATE_FIELD(block); } *KernAux_Alloc_Node; typedef struct KernAux_Alloc { - KernAux_Mutex mutex; - KernAux_Alloc_Node head; + KernAux_Mutex KERNAUX_PRIVATE_FIELD(mutex); + KernAux_Alloc_Node KERNAUX_PRIVATE_FIELD(head); } *KernAux_Alloc; struct KernAux_Alloc KernAux_Alloc_create(KernAux_Mutex mutex); diff --git a/include/kernaux/macro.h b/include/kernaux/macro.h new file mode 100644 index 0000000..d00f2ce --- /dev/null +++ b/include/kernaux/macro.h @@ -0,0 +1,18 @@ +#ifndef KERNAUX_INCLUDED_MACRO +#define KERNAUX_INCLUDED_MACRO + +#ifdef __cplusplus +extern "C" { +#endif + +#ifdef KERNAUX_PRIVATE_NO +#define KERNAUX_PRIVATE_FIELD(id) id +#else +#define KERNAUX_PRIVATE_FIELD(id) _private_##id +#endif + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/include/kernaux/memmap.h b/include/kernaux/memmap.h index 837400f..27ca5bf 100644 --- a/include/kernaux/memmap.h +++ b/include/kernaux/memmap.h @@ -5,6 +5,8 @@ extern "C" { #endif +#include + #include #include @@ -20,10 +22,10 @@ typedef const struct KernAux_MemMap_Entry { } *KernAux_MemMap_Entry; typedef struct KernAux_MemMap { - bool is_finished; - size_t memory_size; - size_t entries_count; - struct KernAux_MemMap_Entry entries[KERNAUX_MEMMAP_ENTRIES_MAX]; + bool KERNAUX_PRIVATE_FIELD(is_finished); + size_t KERNAUX_PRIVATE_FIELD(memory_size); + size_t KERNAUX_PRIVATE_FIELD(entries_count); + struct KernAux_MemMap_Entry KERNAUX_PRIVATE_FIELD(entries)[KERNAUX_MEMMAP_ENTRIES_MAX]; } KernAux_MemMap[1]; struct KernAux_MemMap KernAux_MemMap_create(size_t memory_size); diff --git a/tests/test_alloc.c b/tests/test_alloc.c index 35c52ab..fe6a8c0 100644 --- a/tests/test_alloc.c +++ b/tests/test_alloc.c @@ -2,6 +2,8 @@ #include "config.h" #endif +#define KERNAUX_PRIVATE_NO + #include "helper.h" #include diff --git a/tests/test_memmap.c b/tests/test_memmap.c index 79ee953..f075acf 100644 --- a/tests/test_memmap.c +++ b/tests/test_memmap.c @@ -2,6 +2,8 @@ #include "config.h" #endif +#define KERNAUX_PRIVATE_NO + #include #include