Hi, all i have a question about func as bellows /** * add_head - prepend a node to a list * @l: linked list * @n: list node * * add_head() takes a node @n and prepends it at the start of the list @l. */ LIST_INLINE void add_head(list *l, node *n) { node *z = l->head; n->next = z; n->prev = (node *) &l->head; z->prev = n; l->head = n; } isnt that we should change n->prev = (node *) &l->head to n->prev = (node *) &l->null
Hi!
isnt that we should change n->prev = (node *) &l->head to n->prev = (node *) &l->null
No. I admit the list structure is very tricky and also somewhat awkward, but it's both efficient and easy to manipulate once one understand the basic trick: The list head always contains two synthetic nodes which are always present in the list: the head and the tail. But because the `next' entry of the tail and the `prev' entry of the head are both NULL, the nodes can be overlayed over each other: head head_node.next null head_node.prev tail_node.next tail tail_node.prev Have a nice fortnight -- Martin `MJ' Mares <mj@ucw.cz> http://atrey.karlin.mff.cuni.cz/~mj/ Faculty of Math and Physics, Charles University, Prague, Czech Rep., Earth And God said: E = 1/2mv^2 - Ze^2/r ...and there *WAS* light!
Hi!
isnt that we should change n->prev = (node *) &l->head to n->prev = (node *) &l->null
No. I admit the list structure is very tricky and also somewhat awkward, but it's both efficient and easy to manipulate once one understand the basic trick: The list head always contains two synthetic nodes which are always present in the list: the head and the tail. But because the `next' entry of the tail and the `prev' entry of the head are both NULL, the nodes can be overlayed over each other:
head head_node.next null head_node.prev tail_node.next tail tail_node.prev
Maybe this mail should be taken as a comment and pasted at begining of lists.h? Pavel -- The best software in life is free (not shareware)! Pavel GCM d? s-: !g p?:+ au- a--@ w+ v- C++@ UL+++ L++ N++ E++ W--- M- Y- R+
Hi!
Maybe this mail should be taken as a comment and pasted at begining of lists.h?
Good idea, done that. Have a nice fortnight -- Martin `MJ' Mares <mj@ucw.cz> http://atrey.karlin.mff.cuni.cz/~mj/ Faculty of Math and Physics, Charles University, Prague, Czech Rep., Earth God is real, unless declared integer.
in void * lp_alloc(linpool *m, unsigned size) { byte *a = (byte *) ALIGN((unsigned long) m->ptr, CPU_STRUCT_ALIGN); byte *e = a + size; if (e <= m->end) { m->ptr = e; return a; } else { struct lp_chunk *c; if (size >= m->threshold) { /* Too large => allocate large chunk */ c = xmalloc(sizeof(struct lp_chunk) + size); m->total_large += size; c->next = m->first_large; m->first_large = c->next; c->size = size; } else { if (m->current) { /* Still have free chunks from previous incarnation (before lp_flush()) */ c = m->current; m->current = c->next; } else { /* Need to allocate a new chunk */ c = xmalloc(sizeof(struct lp_chunk) + m->chunk_size); m->total += m->chunk_size; *m->plast = c; m->plast = &c->next; c->next = NULL; c->size = m->chunk_size; } m->ptr = c->data + size; m->end = c->data + m->chunk_size; } return c->data; } } for large allocation, if u add new allocated block in front of old m->first_large do u have to set m->first_large = c instead of m->first_large = c->next
Hello!
for large allocation, if u add new allocated block in front of old m->first_large do u have to set m->first_large = c instead of m->first_large = c->next
Well spotted. I've just commited a fix to our CVS. Thanks. Have a nice fortnight -- Martin `MJ' Mares <mj@ucw.cz> http://atrey.karlin.mff.cuni.cz/~mj/ Faculty of Math and Physics, Charles University, Prague, Czech Rep., Earth "It's a lemon tree, my dear Watson." -- Sherlock Holmes (?)
participants (3)
-
Martin Mares -
Pavel Machek -
Zheng Yuan {ZYUAN1}