From: Alfred Z. <alf...@gm...> - 2011-09-21 01:41:04
|
Hi Dear ST Developers and Users, I am reading ST 1.9's source has encounter a piece of code that I can't figure out for a week. In stk.c, in the body of the function _st_stack_t *_st_stack_new(int stack_size) I understand this function is to allocate memory space for the stack. And update the stack records. There is this piece for (qp = _st_free_stacks.next; qp != &_st_free_stacks; qp = qp->next) //when is _st_free_stack.next!= qp? { ts = _ST_THREAD_STACK_PTR(qp); /* Here I lookup what _ST_THREAD_STACK_PTR does and found #define offsetof(type, identifier) (/***/ (size_t)&(/**/( (type *)0)->identifier/**/) /***/) It looks like this macro finds a instance of "_st_stack_t"'s element "links" 's address and cast it into size_t type? I am totally confused about this! Cast address 0 points to _st_stack_t type variable, but the space is never allocated yet. How does this work? #define _ST_THREAD_STACK_PTR(_qp) \ (/**/ (_st_stack_t *)(/***/ (char*)(_qp) - offsetof(_st_stack_t, links) /***/) /**/) cast _st_clist_t type variable _qp to into byte point and minus this "offset" that I don't understand. Confused again. _qp is of _st_clist_t type, */ if (ts->stk_size >= stack_size) { /* Found a stack that is big enough */ ST_REMOVE_LINK(&ts->links); _st_num_free_stacks--; ts->links.next = NULL; ts->links.prev = NULL; return ts; } } What does this for loop do? I notice at the beginning of stk.c, _st_clist_t _st_free_stacks = ST_INIT_STATIC_CLIST(&_st_free_stacks); This set _st_free stack 's next and prev pointer pointing to itself. Inside _st_stack_new(...), I don't see _st_free_stacks is changed. I guess _st_list_t type is a link list. However, it has only next and prev pointers. Where is the data? Thanks a lot for any suggestions! Alfred |