I'm exploring queue(3) framework - both its public API and internal implementation in sys/queue.h and I'm a little bit confused about one thing in structure that is used for {S}TAILQ head:
I can't comprehend why we store pointer to the first element (struct type *) in stqh_first, but we store 'address of last next element' (struct type **) in stqh_last? Honestly, not only can't I understand what 'last next element' means, but also why can't we just store a pointer to the last element (struct type *) in stqh_last, as we do for stqh_first?
The man page though says:
but the actual implementaion differs, as we can see. It seems that the header file sys/queue.h doesn't explain this choice either.
Does anyone know (or have any guesses) why this structure is implemented in this particular way? What does this approach allow?
C:
#define STAILQ_HEAD(name, type) \
struct name { \
struct type *stqh_first; /* first element */ \
struct type **stqh_last; /* addr of last next element */ \
}
I can't comprehend why we store pointer to the first element (struct type *) in stqh_first, but we store 'address of last next element' (struct type **) in stqh_last? Honestly, not only can't I understand what 'last next element' means, but also why can't we just store a pointer to the last element (struct type *) in stqh_last, as we do for stqh_first?
The man page though says:
Code:
A singly-linked tail queue is headed by a structure defined by the STAILQ_HEAD macro.
This structure contains a pair of pointers, one to the first element in the tail queue
and the other to the last element in the tail queue.
but the actual implementaion differs, as we can see. It seems that the header file sys/queue.h doesn't explain this choice either.
Does anyone know (or have any guesses) why this structure is implemented in this particular way? What does this approach allow?