From: Michael A. <ya...@ya...> - 2011-09-23 16:00:26
|
> for (qp = _st_free_stacks.next; qp != &_st_free_stacks; qp = qp->next) > when is _st_free_stack.next!= qp? _st_free_stacks is a circular linked list. When the list is empty, _st_free_stacks.next == _st_free_stacks.prev == &_st_free_stacks. To traverse the list the loop starts with _st_free_stacks.next and follows the chain via qp->next until it reaches the end of the list, when qp->next == &_st_free_stacks. > How does [offsetof] work? The offsetof macro returns the offset in bytes of a structure member from the address of the struct. It achieves this by pretending it has an object of the struct type at address 0, then asking the compiler what the address is of the member of that struct. This is perfectly safe and legitimate because it never dereferences the 0 pointer, it just does pointer math. > #define _ST_THREAD_STACK_PTR(_qp) The purpose of this macro is to find the address of (i.e., a pointer to the) _st_stack_t object given the address of the object's "links" member. When the CLIST macros put an object on a linked list, they do so using a member of the structure. To get the structure itself it is necessary to subtract the offset of that member. For structures such as _st_stack the links member is the first member so the address of the structure is the same as the address of the structure's links member (sp == &sp->links). But look at _st_thread: it has multiple _st_clist_t members because it can be on different lists. It's linked into those lists via its "links" or "wait_links" members not the structure itself. So to determine a (_st_thread_t *) from the links member it subtracts the offset of links. tp == &tp->links - offsetof(_st_thread_t, links). |