2 * Author: Humberto Naves (hsnaves@gmail.com)
12 struct _listpool
*const pool
;
15 struct _element
*next
;
16 struct _element
*prev
;
22 struct _listpool
*const pool
;
23 struct _element
*head
;
24 struct _element
*tail
;
33 listpool
listpool_create (size_t numelms
, size_t numlsts
)
35 listpool result
= (listpool
) xmalloc (sizeof (struct _listpool
));
36 result
->elmpool
= fixedpool_create (sizeof (struct _element
), numelms
, 0);
37 result
->lstpool
= fixedpool_create (sizeof (struct _list
), numlsts
, 0);
41 void listpool_destroy (listpool pool
)
43 fixedpool_destroy (pool
->lstpool
, NULL
, NULL
);
44 fixedpool_destroy (pool
->elmpool
, NULL
, NULL
);
49 list
list_alloc (listpool pool
)
53 l
= fixedpool_alloc (pool
->lstpool
);
55 ptr
= (listpool
*) &l
->pool
;
57 l
->head
= l
->tail
= NULL
;
62 void list_free (list l
)
67 fixedpool_free (pool
->lstpool
, l
);
70 void list_reset (list l
)
73 for (el
= l
->head
; el
; el
= ne
) {
77 l
->head
= l
->tail
= NULL
;
81 int list_size (list l
)
86 element
list_head (list l
)
91 void *list_headvalue (list l
)
94 return element_getvalue (list_head (l
));
98 element
list_tail (list l
)
103 void *list_tailvalue (list l
)
106 return element_getvalue (list_tail (l
));
110 element
list_inserthead (list l
, void *val
)
112 element el
= element_alloc (l
->pool
, val
);
116 l
->head
= l
->tail
= el
;
118 element_insertbefore (l
->head
, el
);
123 element
list_inserttail (list l
, void *val
)
125 element el
= element_alloc (l
->pool
, val
);
129 l
->head
= l
->tail
= el
;
131 element_insertafter (l
->tail
, el
);
136 void *list_removehead (list l
)
140 if (!el
) return NULL
;
141 return element_free (el
);
144 void *list_removetail (list l
)
148 if (!el
) return NULL
;
149 return element_free (el
);
154 element
element_alloc (listpool pool
, void *val
)
158 el
= fixedpool_alloc (pool
->elmpool
);
160 ptr
= (listpool
*) &el
->pool
;
170 void element_remove (element el
)
176 if (l
) l
->tail
= el
->prev
;
178 el
->next
->prev
= el
->prev
;
182 if (l
) l
->head
= el
->next
;
184 el
->prev
->next
= el
->next
;
186 el
->next
= el
->prev
= NULL
;
190 void *element_free (element el
)
194 val
= element_getvalue (el
);
197 fixedpool_free (pool
->elmpool
, el
);
201 void *element_getvalue (element el
)
206 void element_setvalue (element el
, void *val
)
211 element
element_next (element el
)
216 element
element_previous (element el
)
222 void element_insertbefore (element el
, element inserted
)
224 inserted
->lst
= el
->lst
;
225 inserted
->prev
= el
->prev
;
227 if (el
->prev
) el
->prev
->next
= inserted
;
231 inserted
->lst
->size
++;
232 if (!inserted
->prev
) inserted
->lst
->head
= inserted
;
236 void element_insertafter (element el
, element inserted
)
238 inserted
->lst
= el
->lst
;
239 inserted
->next
= el
->next
;
241 if (el
->next
) el
->next
->prev
= inserted
;
245 inserted
->lst
->size
++;
246 if (!inserted
->next
) inserted
->lst
->tail
= inserted
;