← All posts

Ambiorix Explained (5): Lists and Queues — Why Is It Lighter Than GLib/STL?

Grow through practice

No need to finish reading — test now

Part 5 of the series. Example code: github.com/zishuowang696/amx-examples.

Start with code: the list

libamxc's list is intrusive — amxc_llist_it_t lives inside your struct:

typedef struct { amxc_llist_it_t it; int v; } item_t;

amxc_llist_t list;
amxc_llist_init(&list);
item_t a = { .v = 1 }, b = { .v = 2 };
amxc_llist_append(&list, &a.it);
amxc_llist_append(&list, &b.it);

amxc_llist_for_each(it, (&list)) {
    item_t* p = amxc_llist_it_get_data(it, item_t, it);
    printf("%d ", p->v);
}
amxc_llist_clean(&list, NULL);

What makes it different:

  • Zero node allocation: linking does not malloc; it is already inside your struct;
  • O(1) insert/delete (when you already have the it);
  • cache-friendly (elements can be allocated together);
  • the *_it_get_data macro turns it back into item_t* — C's version of “generics”.

Queue and stack: just “take from the other end”

Queue = list + take_first; stack = list + take_last. No second implementation, no extra allocation.

/* queue: FIFO */
amxc_lqueue_add(&q, &a.it);
amxc_lqueue_add(&q, &b.it);
amxc_lqueue_add(&q, &c.it);
/* remove → take_first */

/* stack: LIFO */
amxc_lstack_push(&s, &d.it);
amxc_lstack_push(&s, &e.it);
amxc_lstack_push(&s, &f.it);
/* pop → take_last */

Measured output:

queue: 1 2 3
stack: 3 2 1

The array variants work the same way: amxc_aqueue_add(queue, data) / amxc_astack_push(stack, data) store void*, for cases where the element should not embed an it.

Compared with GLib / STL

libamxcGLib (GList/GQueue)C++ STL (list/deque)
Node allocationzero (intrusive)malloc one node per elementlist allocates per node; deque in blocks
Typingvoid* + macrosgpointertemplates, compile-time type safety
Dependencieslibc onlydepends on GLibdepends on the C++ runtime/STL
Enqueue determinismno malloc, deterministicmay fail to allocatemay allocate/throw
Serializationcontainers + Variant are serializablenonenone (you write it yourself)

Why it is lighter: intrusive + zero allocation + pure C. libamxc.so is about 187K after strip and depends only on libc.

Then why “rewrite a set”? — More than lightness

You may ask: GLib and STL already exist, why write another one? My understanding so far has three layers:

  1. C projects: STL is not available;
  1. GLib is too heavy: AMX wants “small dependencies, portable to all kinds of embedded platforms”;
  1. Most importantly: serving serialization/the data model.

amxc_var_t (Variant) can hold int/string/bool/list/hash; containers and Variant together form a dumpable, serializable data tree — exactly the foundation ODL assembly, RPC, and the data model need. GLib/STL containers do not automatically become a “data model”.

So it is not “reinventing the wheel”; it is building a foundation for a “serializable component system”.

One sentence

Intrusive + zero allocation = light; containers + Variant = serializable. Lightness is the surface; “serving the data model” is the real reason it rewrote a set.


Next: “libamxc Variant: C's ‘Any Type’” — how one variable can hold everything.

Practice

Found this useful?

I write about AI agents × embedded / edge AI. Subscribe, or email me a concrete problem.

Guestbook →Subscribe (RSS)Support

Comments 0

No comments yet — be the first!

No login required; name is optional.