Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[doubly-linked list] Applying Memory API #22

Merged
merged 6 commits into from
Feb 16, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions include/collect-c/dlist.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ typedef void (*collect_c_dlist_pfn_free)(

struct collect_c_dlist_t
{
collect_c_mem_api_t mem_api;
size_t el_size; /*! The element size. */
size_t num_spares; /*! */
size_t size; /*! */
Expand Down Expand Up @@ -435,6 +436,13 @@ collect_c_dlist_push_front_by_ref(
#define COLLECT_C_DLIST_EMPTY_INITIALIZER_(l_el_type, l_flags, l_storage, elf_fn, elf_param) \
\
{ \
.mem_api = \
{ \
.pfn_alloc = collect_c_mem_std_alloc, \
.pfn_realloc = collect_c_mem_std_realloc, \
.pfn_free = collect_c_mem_std_free, \
.param = NULL, \
}, \
.el_size = sizeof(l_el_type), \
.num_spares = 0, \
.size = 0, \
Expand Down
50 changes: 27 additions & 23 deletions src/dlist.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,17 @@ typedef collect_c_dlist_node_t node_t;
* helper functions and macros
*/

#define COLLECT_C_DLIST_INTERNAL_sizeof_node_(el_size) (offsetof(collect_c_dlist_node_t, data) + el_size)
#define COLLECT_C_DLIST_INTERNAL_sizeof_node_(el_size) (offsetof(node_t, data) + el_size)

static
node_t*
clc_c_dl_alloc_node_(
size_t el_size
collect_c_mem_api_t* mem_api
, size_t el_size
)
{
size_t const cb = COLLECT_C_DLIST_INTERNAL_sizeof_node_(el_size);
node_t* const nd = malloc(cb);
node_t* const nd = (*mem_api->pfn_alloc)(mem_api->param, cb);

return nd;
}
Expand All @@ -75,7 +76,7 @@ clc_c_dl_obtain_node_(
}
else
{
nd = clc_c_dl_alloc_node_(el_size);
nd = clc_c_dl_alloc_node_(&l->mem_api, el_size);

if (NULL == nd)
{
Expand Down Expand Up @@ -110,21 +111,24 @@ clc_dlist_free_storage(
assert(NULL != l);

{
for (collect_c_dlist_node_t* n = l->head; NULL != n; )
collect_c_mem_api_t* const mem_api = &l->mem_api;
size_t const cb = COLLECT_C_DLIST_INTERNAL_sizeof_node_(l->el_size);

for (node_t* n = l->head; NULL != n; )
{
collect_c_dlist_node_t* const n2 = n;
node_t* const n2 = n;

n = n->next;

free(n2);
(mem_api->pfn_free)(mem_api->param, n2, cb);
}

l->head = l->tail = NULL;
l->size = 0;

for (collect_c_dlist_node_t* n = l->spares; NULL != n; )
for (node_t* n = l->spares; NULL != n; )
{
collect_c_dlist_node_t* const n2 = n;
node_t* const n2 = n;

n = n->next;

Expand Down Expand Up @@ -164,9 +168,9 @@ collect_c_dlist_clear(

*num_dropped = 0;

for (collect_c_dlist_node_t* n = l->head; NULL != n; )
for (node_t* n = l->head; NULL != n; )
{
collect_c_dlist_node_t* const n2 = n;
node_t* const n2 = n;

n = n->next;

Expand Down Expand Up @@ -295,7 +299,7 @@ collect_c_dlist_find_node(

*num_searched = 0;

for (collect_c_dlist_node_t* n = l->head; NULL != n; n = n->next)
for (node_t* n = l->head; NULL != n; n = n->next)
{
int const r = (*pfn)(l, p_lhs, &n->data->data[0]);

Expand Down Expand Up @@ -343,7 +347,7 @@ collect_c_dlist_rfind_node(

*num_searched = 0;

for (collect_c_dlist_node_t* n = l->tail; NULL != n; n = n->prev)
for (node_t* n = l->tail; NULL != n; n = n->prev)
{
int const r = (*pfn)(l, p_lhs, &n->data->data[0]);

Expand Down Expand Up @@ -377,7 +381,7 @@ collect_c_dlist_insert_after(
assert(NULL != ptr_new_el);

{
collect_c_dlist_node_t* dummy;
node_t* dummy;

if (NULL == new_node)
{
Expand Down Expand Up @@ -424,7 +428,7 @@ collect_c_dlist_insert_before(
assert(NULL != ptr_new_el);

{
collect_c_dlist_node_t* dummy;
node_t* dummy;

if (NULL == new_node)
{
Expand Down Expand Up @@ -471,7 +475,7 @@ collect_c_dlist_push_back_by_ref(
{
if (NULL == l->head)
{
collect_c_dlist_node_t* const nd = clc_c_dl_obtain_node_(l, NULL, NULL, l->el_size, ptr_new_el);
node_t* const nd = clc_c_dl_obtain_node_(l, NULL, NULL, l->el_size, ptr_new_el);

if (NULL == nd)
{
Expand All @@ -486,9 +490,9 @@ collect_c_dlist_push_back_by_ref(
}
else
{
collect_c_dlist_node_t* const prev = l->tail;
collect_c_dlist_node_t* const next = NULL;
collect_c_dlist_node_t* const nd = clc_c_dl_obtain_node_(l, prev, next, l->el_size, ptr_new_el);
node_t* const prev = l->tail;
node_t* const next = NULL;
node_t* const nd = clc_c_dl_obtain_node_(l, prev, next, l->el_size, ptr_new_el);

if (NULL == nd)
{
Expand Down Expand Up @@ -519,7 +523,7 @@ collect_c_dlist_push_front_by_ref(
{
if (NULL == l->head)
{
collect_c_dlist_node_t* const nd = clc_c_dl_obtain_node_(l, NULL, NULL, l->el_size, ptr_new_el);
node_t* const nd = clc_c_dl_obtain_node_(l, NULL, NULL, l->el_size, ptr_new_el);

if (NULL == nd)
{
Expand All @@ -534,9 +538,9 @@ collect_c_dlist_push_front_by_ref(
}
else
{
collect_c_dlist_node_t* const prev = NULL;
collect_c_dlist_node_t* const next = l->head;
collect_c_dlist_node_t* const nd = clc_c_dl_obtain_node_(l, prev, next, l->el_size, ptr_new_el);
node_t* const prev = NULL;
node_t* const next = l->head;
node_t* const nd = clc_c_dl_obtain_node_(l, prev, next, l->el_size, ptr_new_el);

if (NULL == nd)
{
Expand Down