Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #159250
| From | "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | container_of macro... |
| Date | 2021-03-08 17:24 -0800 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <s26io0$nne$1@gioia.aioe.org> (permalink) |
Its been a while since I used the container_of macro, however, it can
come in handy wrt creating linked data-structures. Here is an example I
coded up, just to see if I could do it from scratch, after all these years:
________________________________
#include <stdio.h>
#include <stddef.h>
#include <assert.h>
struct ct_slist_node
{
struct ct_slist_node* next;
};
struct ct_slist
{
struct ct_slist_node* head;
};
#define ct_container_of(m_ptr, m_type, m_member) \
((void*)((unsigned char*)(m_ptr) - offsetof(m_type, m_member)))
void
ct_slist_push(
struct ct_slist* self,
struct ct_slist_node* node
) {
node->next = self->head;
self->head = node;
}
struct ct_slist_node*
ct_slist_pop(
struct ct_slist* self
) {
struct ct_slist_node* node = self->head;
if (node) self->head = node->next;
return node;
}
struct foo
{
int a;
struct ct_slist_node next;
char b;
long c;
};
int main(void)
{
struct foo foo;
struct foo* pfoo = ct_container_of(&foo.next, struct foo, next);
printf("&foo.next = %p\n", &foo.next);
printf("&foo = %p\n", &foo);
printf("pfoo = %p\n", pfoo);
assert(&foo == pfoo);
{
#define N 42
struct foo foo[N];
struct ct_slist slist = { NULL };
printf("\npush...\n");
for (unsigned int i = 0; i < N; ++i)
{
printf("&foo[%u] = %p\n", i, foo + i);
ct_slist_push(&slist, &foo[i].next);
}
printf("\npop...\n");
for (unsigned int i = 0; i < N; ++i)
{
struct ct_slist_node* node = ct_slist_pop(&slist);
struct foo* pfoo = ct_container_of(node, struct foo, next);
printf("pfoo = %p = &foo[%u - %u - 1] = %p\n", pfoo, N, i,
&foo[N - i - 1]);
assert(pfoo == &foo[N - i - 1]);
}
}
return 0;
}
________________________________
How many people here use it?
Back to comp.lang.c | Previous | Next — Next in thread | Find similar | Unroll thread
container_of macro... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-03-08 17:24 -0800
Re: container_of macro... Öö Tiib <ootiib@hot.ee> - 2021-03-09 02:06 -0800
Re: container_of macro... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-03-10 17:02 -0800
Re: container_of macro... Kaz Kylheku <563-365-8930@kylheku.com> - 2021-03-09 15:59 +0000
Re: container_of macro... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-03-09 17:35 +0000
Re: container_of macro... Kaz Kylheku <563-365-8930@kylheku.com> - 2021-03-09 17:51 +0000
Re: container_of macro... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-03-10 17:13 -0800
Re: container_of macro... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-03-10 17:05 -0800
Re: container_of macro... William Ahern <william@25thandClement.com> - 2021-03-09 18:56 -0800
Re: container_of macro... Kaz Kylheku <563-365-8930@kylheku.com> - 2021-03-10 03:47 +0000
Re: container_of macro... William Ahern <william@25thandClement.com> - 2021-03-12 17:01 -0800
Re: container_of macro... Jim <jim.cromie@gmail.com> - 2021-03-16 22:14 -0700
Re: container_of macro... "Chris M. Thomasson" <chris.m.thomasson.1@gmail.com> - 2021-03-10 17:07 -0800
csiph-web