nginx has a macro-only queue implementation

As I discovered when reading the HTTP parser code, nginx relies heavily on macros. Perhaps this has something to do with the “uncertainty” of the inline keyword - it is not an “obligation”, more like a compiler “hint”. Which doesn’t give guarantees that the code will get inlined.

Another example of heavy macro usage is the queue implementation (ngx_queue.h). For example, some of the utilities are:

#define ngx_queue_init(q)                                                     \
    (q)->prev = q;                                                            \
    (q)->next = q

#define ngx_queue_empty(h)                                                    \
    (h == (h)->prev)

#define ngx_queue_insert_head(h, x)                                           \
    (x)->next = (h)->next;                                                    \
    (x)->next->prev = x;                                                      \
    (x)->prev = h;                                                            \
    (h)->next = x

#define ngx_queue_insert_tail(h, x)                                           \
    (x)->prev = (h)->prev;                                                    \
    (x)->prev->next = x;                                                      \
    (x)->next = h;                                                            \
    (h)->prev = x

The whole ngx_queue.h is pretty awesome. ngx_queue.c also contains a pretty succinct implementation of an insertion sort.