Add macro `KERNAUX_CONTAINER_OF` (#100)

* Add macro KERNAUX_CONTAINER_OF
* Add includes
* Add "examples/macro_container_of"
This commit is contained in:
Alex Kotov 2022-06-27 14:08:32 +03:00 committed by GitHub
parent f5ae8400c0
commit febd43c987
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 54 additions and 4 deletions

1
.gitignore vendored
View File

@ -111,6 +111,7 @@
/examples/generic_file
/examples/generic_malloc
/examples/generic_mutex
/examples/macro_container_of
/examples/memmap
/examples/ntoa
/examples/panic

View File

@ -45,6 +45,14 @@ TESTS += generic_mutex
generic_mutex_LDADD = $(top_builddir)/libkernaux.la
generic_mutex_SOURCES = main.c generic_mutex.c
######################
# macro_container_of #
######################
TESTS += macro_container_of
macro_container_of_LDADD = $(top_builddir)/libkernaux.la
macro_container_of_SOURCES = main.c macro_container_of.c
##########
# memmap #
##########

View File

@ -28,6 +28,9 @@ struct MyFile MyFile_create(char *ptr, size_t size);
// my_file.c
//===========
#include <kernaux/generic/file.h>
#include <stddef.h>
static int MyFile_putc(void *file, unsigned char c);
struct MyFile MyFile_create(char *const ptr, const size_t size)

View File

@ -0,0 +1,33 @@
#include <kernaux/macro.h>
#include <assert.h>
struct Foo {
const char *hello;
};
struct Bar {
unsigned char a;
unsigned int b;
struct Foo foo;
unsigned short c;
};
static const struct Bar bar = {
.a = 143,
.b = 820794098,
.foo = {
.hello = "Hello, World!",
},
.c = 10981,
};
void example_main()
{
const struct Bar *const bar_ptr =
KERNAUX_CONTAINER_OF(&bar.foo, struct Bar, foo);
assert(bar_ptr->a == 143);
assert(bar_ptr->b == 820794098);
assert(bar_ptr->c == 10981);
}

View File

@ -5,6 +5,12 @@
extern "C" {
#endif
#include <stddef.h>
#include <stdint.h>
#define KERNAUX_CONTAINER_OF(ptr, type, member) \
((type*)((uintptr_t)(ptr) - offsetof(type, member)))
#ifdef KERNAUX_ACCESS_PRIVATE
# define KERNAUX_PRIVATE_FIELD(id) id
# define KERNAUX_PROTECTED_FIELD(id) id

View File

@ -13,6 +13,7 @@
#include <kernaux/free_list.h>
#include <kernaux/generic/malloc.h>
#include <kernaux/generic/mutex.h>
#include <kernaux/macro.h>
#include <stddef.h>
#include <stdint.h>
@ -22,8 +23,6 @@
#define MIN_ZONE_SIZE (2 * NODE_HEADER_SIZE)
#define MIN_SPLIT_SIZE (NODE_HEADER_SIZE + 16)
#define CONTAINER_OF(ptr, type, member) ((type*)((uintptr_t)(ptr) - offsetof(type, member)))
//#define ALIGN_MASK(align) ((align) - 1) // align should be a power of 2
//#define ALIGN_UP(val, align) (((val) + ALIGN_MASK(align)) & ~ALIGN_MASK(align))
@ -131,7 +130,7 @@ void KernAux_FreeList_free(void *const malloc, void *const ptr)
LOCK(free_list);
KernAux_FreeList_Node node =
CONTAINER_OF(ptr, struct KernAux_FreeList_Node, block);
KERNAUX_CONTAINER_OF(ptr, struct KernAux_FreeList_Node, block);
KernAux_FreeList_Node last_node = NULL;
for (
@ -216,7 +215,7 @@ void *KernAux_FreeList_realloc(
LOCK(free_list);
KernAux_FreeList_Node node =
CONTAINER_OF(old_ptr, struct KernAux_FreeList_Node, block);
KERNAUX_CONTAINER_OF(old_ptr, struct KernAux_FreeList_Node, block);
const size_t old_size = node->size - NODE_HEADER_SIZE;
void *new_ptr = KernAux_FreeList_malloc(free_list, new_size);