Repository: tailix/libkernaux (language: python)

Issue:
Header macro for private struct attributes



Discussion / hints:

Fix patch:
diff --git a/ChangeLog b/ChangeLog
index 43561ea5..ce2d424c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,7 @@
+2022-06-20  Alex Kotov  <kotovalexarian@gmail.com>
+
+	* include/kernaux/alloc.h: Finished, made stable
+
 2022-06-18  Alex Kotov  <kotovalexarian@gmail.com>
 
 	* include/kernaux/mutex.h: Added
diff --git a/Makefile.am b/Makefile.am
index f47cf700..2f746071 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 892547c2..807e2d00 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 eb4e592c..8755e0f4 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 f3e7ca2a..eabdfee0 100644
--- a/include/kernaux.h.in
+++ b/include/kernaux.h.in
@@ -4,6 +4,7 @@
 */
 
 #include <kernaux/assert.h>
+#include <kernaux/macro.h>
 #include <kernaux/mutex.h>
 #include <kernaux/version.h>
 
diff --git a/include/kernaux/alloc.h b/include/kernaux/alloc.h
index 8e5f6c37..2a411077 100644
--- a/include/kernaux/alloc.h
+++ b/include/kernaux/alloc.h
@@ -5,19 +5,21 @@
 extern "C" {
 #endif
 
+#include <kernaux/macro.h>
 #include <kernaux/mutex.h>
 
 #include <stddef.h>
 
 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 00000000..d00f2cec
--- /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 837400f1..27ca5bf0 100644
--- a/include/kernaux/memmap.h
+++ b/include/kernaux/memmap.h
@@ -5,6 +5,8 @@
 extern "C" {
 #endif
 
+#include <kernaux/macro.h>
+
 #include <stdbool.h>
 #include <stddef.h>
 
@@ -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);


Test patch:
diff --git a/tests/test_alloc.c b/tests/test_alloc.c
index 35c52ab4..fe6a8c05 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 <kernaux/alloc.h>
diff --git a/tests/test_memmap.c b/tests/test_memmap.c
index 79ee9533..f075acf1 100644
--- a/tests/test_memmap.c
+++ b/tests/test_memmap.c
@@ -2,6 +2,8 @@
 #include "config.h"
 #endif
 
+#define KERNAUX_PRIVATE_NO
+
 #include <kernaux/assert.h>
 #include <kernaux/memmap.h>