FreeBASIC  0.91.0
list.c
Go to the documentation of this file.
1 /* generic internal lists based on static arrays */
2 
3 #include "fb.h"
4 
14 void fb_hListInit( FB_LIST *list, void *table, size_t elem_size, size_t size )
15 {
16  size_t i;
17  FB_LISTELEM *next;
18  unsigned char *elem = (unsigned char *)table;
19 
20  fb_hListDynInit( list );
21 
22  list->fhead = (FB_LISTELEM *)elem;
23 
24  for( i = 0; i < size; i++ )
25  {
26  if( i < size-1 )
27  next = (FB_LISTELEM *)(elem + elem_size);
28  else
29  next = NULL;
30  ((FB_LISTELEM *)elem)->prev = NULL;
31  ((FB_LISTELEM *)elem)->next = next;
32 
33  elem += elem_size;
34  }
35 }
36 
48 {
49  FB_LISTELEM *elem;
50 
51  /* take from free list */
52  elem = list->fhead;
53  if( elem == NULL )
54  return NULL;
55 
56  list->fhead = elem->next;
57 
58  /* add to entry used list */
59  fb_hListDynElemAdd( list, elem );
60 
61  return elem;
62 }
63 
75 {
76  /* remove entry from the list of used elements */
77  fb_hListDynElemRemove( list, elem );
78 
79  /* add to free list */
80  elem->next = list->fhead;
81  list->fhead = elem;
82 }