summaryrefslogtreecommitdiff
path: root/firmware/common/lists.c
blob: 0699531e2059ab93bbf15ec1179d8c14a2d74c0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2002 by Linus Nielsen Feltzing
 *
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/
/*
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

  Description:
  
  This file contains functions implementing a double linked list. 
  The functions uses the types LIST and LISTNODE.
  There is a trick with the three nodes in LIST. By placing the 
  tail_pred field in middle, makes it possible to avoid special code
  to handle nodes in the beginning or end of the list.
  
  The 'head' and 'tail' field in LIST are never NULL, even if the
  list is empty.
  
  The 'succ' and 'pred' field in a LIST_NODE that is in the list,
  are never NULL. The 'pred' field for the first LIST_NODE points
  to the 'head' field in LIST. The 'succ' node for the last LIST_NODE
  points the the tail_pred field in LIST.
  
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
*/
#include "lists.h"

/****************************************************************************
** FUNCTION:    list_init
**
** DESCRIPTION: Initiate a LIST structure. 
**              If max_num_nodes is set to NO_SIZE_CHECK there 
**              will be no check of the length of the list.
**
** RETURN:      Nothing
******************************************************************************/
void list_init(LIST *list,         /* The list to initiate */
	       int max_num_nodes)  /* Maximum number of nodes */
{
   list->first = (LIST_NODE *)&list->last_prev;
   list->last_prev = NULL;
   list->last = (LIST_NODE *)&list->first;
   list->num_nodes = 0;
   list->max_num_nodes = max_num_nodes;
}

/****************************************************************************
** FUNCTION:    list_insert_before
**
** DESCRIPTION: Insert a LIST_NODE in a list before another node in a LIST.
**
** RETURN:      Nothing
******************************************************************************/
void list_insert_before(LIST_NODE *ref,    /* The reference node */
			     LIST_NODE *ln)     /* The node to insert */
{
   ln->next = ref;               /* ref is after us */
   ln->prev = ref->prev;         /* ref's prev is before us */
   ref->prev = ln;               /* we are before ref */
   ln->prev->next = ln;          /* we are after ref's prev */
}

/****************************************************************************
** FUNCTION:    list_insert_after
**
** DESCRIPTION: Insert a LIST_NODE in a list after another node in a LIST.
**
** RETURN:      Nothing
*****************************************************************************/
void list_insert_after(LIST_NODE *ref,     /* The reference node */
			    LIST_NODE *ln)      /* The node to insert */
{
   ln->prev = ref;               /* ref is before us */
   ln->next = ref->next;         /* ref's next is after us */
   ref->next = ln;               /* we are after ref */
   ln->next->prev = ln;          /* we are before ref's next */
}

/****************************************************************************
** FUNCTION:    list_extract_node
**
** DESCRIPTION: Extract a LIST_NODE from a list.
**
** RETURN:      The same LIST_NODE pointer that was passed as a parameter.
*****************************************************************************/
LIST_NODE *list_extract_node(LIST_NODE *ln)     /* The node to extract */
{
  ln->prev->next = ln->next;      /* Our prev's next points to our next */
  ln->next->prev = ln->prev;      /* Our next's prev points to our prev */
  return ln;
}

/******************************************************************************
** FUNCTION:    list_add_first
**
** DESCRIPTION: Add a LIST_NODE at the beginning of a LIST.
**
** RETURN:      1 if OK
**              0 if list was full
******************************************************************************/
int list_add_first(LIST *list,             /* The list to add to */
                 LIST_NODE *ln)         /* The node to add */
{
   if (NO_SIZE_CHECK != list->max_num_nodes)
   {
      if(list->num_nodes >= list->max_num_nodes)  /* List full? */
      {
	 return 0;
      }
   }
   list_insert_after((LIST_NODE *)list, ln);
   list->num_nodes++;            /* Increment node counter */
   return 1;
}

/******************************************************************************
** FUNCTION:    list_extract_first
**
** DESCRIPTION: Extract a LIST_NODE from the beginning of a LIST.
**
** RETURN:      The extracted LIST_NODE or NULL if the list is empty
******************************************************************************/
LIST_NODE *list_extract_first(LIST *list)       /* The list to extract from */
{
   LIST_NODE *ln;
  
   if(list_empty(list))             /* Return NULL if the list is empty */
   {
      return NULL;
   }
  
   ln = list_extract_node((LIST_NODE *)list->first);  /* Get first node */
  
   list->num_nodes--;                    /* Decrement node counter */
  
   return ln;
}

/******************************************************************************
** FUNCTION:    list_add_last
**
** DESCRIPTION: Add a LIST_NODE at the end of a LIST.
**
** RETURN:      NULL if OK
**              0 if list was full
******************************************************************************/
int list_add_last(LIST *list,              /* The list to add to */
                LIST_NODE *ln)          /* The node to add */
{
   if (NO_SIZE_CHECK != list->max_num_nodes)
   {
      if(list->num_nodes >= list->max_num_nodes)  /* List full? */
      {
	 return 0;      
      }
   }
   list_insert_before((LIST_NODE *)&list->last_prev, ln);
   list->num_nodes++;                    /* Increment node counter */
   return 1;
}

/******************************************************************************
** FUNCTION:    list_extract_last
**
** DESCRIPTION: Extract a LIST_NODE from the end of a LIST.
**
** RETURN:      The extracted LIST_NODE or NULL if the list is empty
******************************************************************************/
LIST_NODE *list_extract_last(LIST *list)        /* The list to extract from */
{
   LIST_NODE *ln;
  
   if(list_empty(list))             /* Return NULL if the list is empty */
   {
      return NULL;
   }
  
   ln = list_extract_node((LIST_NODE *)list->last);
  
   list->num_nodes--;    /* Decrement node counter */
  
   return ln;           /* Is NULL if the list is empty */
}

/******************************************************************************
** FUNCTION:    list_last_in_list
**
** DESCRIPTION: Check if a LIST_NODE is last in the list
**
** RETURN:      1 if last in list
******************************************************************************/
int list_last_in_list(LIST_NODE *ln)    /* The node to check */
{
   return ln->next->next == NULL;
}

/*****************************************************************************
** FUNCTION:    list_first_in_list
**
** DESCRIPTION: Check if a LIST_NODE is first in the list
**
** RETURN:      1 if first in list
******************************************************************************/
int list_first_in_list(LIST_NODE *ln)   /* The node to check */
{
   return ln->prev->prev == NULL;
}

/******************************************************************************
** FUNCTION:    list_empty
**
** DESCRIPTION: Check if a LIST is empty
**
** RETURN:      1 if list is empty
******************************************************************************/
int list_empty(LIST *list)         /* The list to check */
{
   return list->first == (LIST_NODE *)&list->last_prev;
}

/******************************************************************************
** FUNCTION:    list_get_first
**
** DESCRIPTION: Return a LIST_NODE from the beginning of a LIST.
**
** RETURN:      The first LIST_NODE or NULL if the list is empty
******************************************************************************/
LIST_NODE *list_get_first(LIST *lh)   /* The list to read from */
{
   if(list_empty(lh))       /* Return NULL if the list is empty */
   {
      return NULL;
   }
  
   return lh->first;     /* Get first node */
}

/******************************************************************************
** FUNCTION:    list_get_last
**
** DESCRIPTION: Return a LIST_NODE from the end of a LIST.
**
** RETURN:      The last LIST_NODE or NULL if the list is empty
******************************************************************************/
LIST_NODE *list_get_last(LIST *lh)    /* The list to read from */
{
   if(list_empty(lh))               /* Return NULL if the list is empty */
   {
      return NULL;
   }
  
   return lh->last;
}

/******************************************************************************
** FUNCTION:    list_get_next
**
** DESCRIPTION: Return the LIST_NODE following the specified one.
**
** RETURN:      Next LIST_NODE or NULL if the list ends here
*******************************************************************************/
LIST_NODE *list_get_next(LIST_NODE *ln) /* The list node to get next from */
{
   if(list_last_in_list(ln))       /* Return NULL if this is the end of list */
   {
      return NULL;
   }
  
   return ln->next;
}

/******************************************************************************
** FUNCTION:    list_get_prev
**
** DESCRIPTION: Return the LIST_NODE preceding the specified one.
**
** RETURN:      Previous LIST_NODE or NULL if the list ends here
*******************************************************************************/
LIST_NODE *list_get_prev(LIST_NODE *ln) /* The list node to get previous from */
{
   if(list_first_in_list(ln))            /* Return NULL if this is the start of list */
   {
      return NULL;
   }
  
   return ln->prev;
}