Ambiorix Explained (5): Lists and Queues — Why Is It Lighter Than GLib/STL?
Grow through practice
No need to finish reading — test nowPart 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;itis already inside your struct; - O(1) insert/delete (when you already have the
it); - cache-friendly (elements can be allocated together);
- the
*_it_get_datamacro turnsitback intoitem_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
| libamxc | GLib (GList/GQueue) | C++ STL (list/deque) | |
|---|---|---|---|
| Node allocation | zero (intrusive) | malloc one node per element | list allocates per node; deque in blocks |
| Typing | void* + macros | gpointer | templates, compile-time type safety |
| Dependencies | libc only | depends on GLib | depends on the C++ runtime/STL |
| Enqueue determinism | no malloc, deterministic | may fail to allocate | may allocate/throw |
| Serialization | containers + Variant are serializable | none | none (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:
- C projects: STL is not available;
- GLib is too heavy: AMX wants “small dependencies, portable to all kinds of embedded platforms”;
- 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.
Comments 0
No comments yet — be the first!