File: list.c

package info (click to toggle)
wcalc 2.2.2-2
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 2,228 kB
  • ctags: 698
  • sloc: ansic: 6,918; objc: 1,835; sh: 766; yacc: 644; lex: 573; makefile: 78
file content (453 lines) | stat: -rw-r--r-- 10,064 bytes parent folder | download
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
#include <stdlib.h>		       /* for free() */
#include <stdio.h>		       /* for fprintf() */
#include <string.h>		       /* for memset() */

#include "list.h"
#ifdef MEMWATCH
#include "memwatch.h"
#endif

/* structures for constructing the list */
struct _listheader
{
    struct _listheader *next;
    void *payload;
    struct _listheader *pool_next;
};

struct _list
{
    struct _listheader *head;
    struct _listheader *tail;
    unsigned long len;
    struct _list *pool_next;
    struct _list *pool_prev;
};

struct _list_iterator
{
    struct _listheader *cur;
    List l;
};

struct cleanup
{
    struct cleanup *next;
    void *data;
};

static List lPool = NULL;
static List lPoolUsed = NULL;
static unsigned long long lPoolSize = 0;	/* for exponential growth */
static struct cleanup *lCleanupPool = NULL;
static inline List get_l(void);
static void fill_l_pool(void);

static struct _listheader *lhPool = NULL;
static unsigned long long lhPoolSize = 0;	/* for exponential growth */
static struct cleanup *lhCleanupPool = NULL;
static inline struct _listheader * get_lh(void);
static void fill_lh_pool(void);
static void poolReturn(struct _listheader *lh);

/* This sets up the memory pool(s) */
void lists_init()
{				       /*{{{ */
    fill_lh_pool();
    fill_l_pool();
}				       /*}}} */

/* This clean up the memory pool(s) */
void lists_cleanup()
{				       /*{{{ */
    struct cleanup *freec;
    struct _listheader *freeme;
    List freeme2;

    while (lhCleanupPool != NULL) {
	freec = lhCleanupPool;
	freeme = freec->data;
	lhCleanupPool = lhCleanupPool->next;
	free(freeme);
	free(freec);
    }
    while (lCleanupPool != NULL) {
	freec = lCleanupPool;
	freeme2 = freec->data;
	lCleanupPool = lCleanupPool->next;
	free(freeme2);
	free(freec);
    }
}				       /*}}} */

/* This returns a struct to the pool */
void poolReturn(struct _listheader *lh)
{				       /*{{{ */
    memset(lh, 0, sizeof(struct _listheader));
    lh->pool_next = lhPool;
    lhPool = lh;
}				       /*}}} */

/* This fills the list header memory pool with 1024 items (or doubles the pool
 * size) */
static void fill_lh_pool()
{				       /*{{{ */
    struct _listheader *tlist;
    struct _listheader *temp;
    struct cleanup *ch;
    size_t newobjcount;
    size_t i;

    newobjcount = (lhPoolSize == 0) ? 1024 : lhPoolSize;
    tlist = calloc(newobjcount, sizeof(struct _listheader));
    ch = calloc(1, sizeof(struct cleanup));
    ch->data = tlist;
    ch->next = lhCleanupPool;
    lhCleanupPool = ch;
    for (i = 0; i < newobjcount; ++i) {
	temp = &(tlist[i]);
	temp->pool_next = lhPool;
	lhPool = temp;
    }
    lhPoolSize += newobjcount;
}				       /*}}} */

/* This fills the list item memory pool with 2 items (or doubles the pool
 * size) */
static void fill_l_pool()
{				       /*{{{ */
    size_t i, newobjcount;
    List temp;
    List tlist;
    struct cleanup *ch;

    newobjcount = (lPoolSize == 0) ? 2 : lPoolSize;
    tlist = calloc(newobjcount, sizeof(struct _list));
    ch = calloc(1, sizeof(struct cleanup));
    ch->data = tlist;
    ch->next = lCleanupPool;
    lCleanupPool = ch;
    for (i = 0; i < newobjcount; ++i) {
	temp = &(tlist[i]);
	temp->pool_next = lPool;
	lPool = temp;
    }
    lPoolSize += newobjcount;
}				       /*}}} */

/* This frees up the memory allocated by the list */
void freeList(List * lst)
{				       /*{{{ */
    List thelist;

    if (!lst) {
	return;
    }

    thelist = *lst;
    if (!thelist) {
	return;
    }

    while (thelist->len > 0 && thelist->head != NULL) {
	struct _listheader *h = thelist->head;

	thelist->head = h->next;
	thelist->len--;
	poolReturn(h);
    }
    if (lPoolUsed == thelist) {
	lPoolUsed = thelist->pool_next;
    }
    if (thelist->pool_next) {
	thelist->pool_next->pool_prev = thelist->pool_prev;
    }
    if (thelist->pool_prev) {
	thelist->pool_prev->pool_next = thelist->pool_next;
    }
    thelist->pool_next = lPool;
    thelist->pool_prev = NULL;
    lPool = thelist;
    *lst = NULL;
}				       /*}}} */

/* this fetches a new list header from the pool */
static inline struct _listheader *get_lh()
{				       /*{{{ */
    struct _listheader *lh;

    if (NULL == lhPool) {
	fill_lh_pool();
    }
    lh = lhPool;
    lhPool = lh->pool_next;
    lh->pool_next = NULL;
    return lh;
}				       /*}}} */

/* this fetches a new list from the pool */
static inline List get_l()
{				       /*{{{ */
    List l;

    if (NULL == lPool) {
	fill_l_pool();
    }
    l = lPool;
    lPool = l->pool_next;
    l->pool_next = lPoolUsed;
    if (lPoolUsed) {
	lPoolUsed->pool_prev = l;
    }
    lPoolUsed = l;
    lPoolUsed->pool_prev = NULL;
    return l;
}				       /*}}} */

/* This adds "addme" to the end of the list */
void addToList(List * lst, void *addme)
{				       /*{{{ */
    List list;
    struct _listheader *pl;

    pl = get_lh();
    pl->payload = addme;
    pl->next = NULL;
    if (!lst) {
	fprintf(stderr, "null list passed\n");
	exit(EXIT_FAILURE);
    }
    list = *lst;
    if (!list) {
	list = *lst = get_l();
	list->tail = list->head = pl;
	list->len = 1;
    } else {
	if (list->tail) {
	    list->tail->next = pl;
	    list->tail = pl;
	    list->len++;
	} else {
	    list->tail = list->head = pl;
	    list->len = 1;
	}
    }
}				       /*}}} */

/* This adds "addme" to the head of the list */
void addToListHead(List * lst, void *addme)
{				       /*{{{ */
    List list;
    struct _listheader *pl;

    pl = get_lh();
    pl->payload = addme;
    pl->next = NULL;
    if (!lst) {
	fprintf(stderr, "null list passed\n");
	exit(EXIT_FAILURE);
    }
    list = *lst;
    if (!list) {
	list = *lst = get_l();
	list->tail = list->head = pl;
	list->len = 1;
    } else {
	if (list->head) {
	    pl->next = list->head;
	    list->head = pl;
	    list->len++;
	} else {
	    list->tail = list->head = pl;
	    list->len = 1;
	}
    }
}				       /*}}} */

/* This removes the front of the list from the list and returns it */
void *getHeadOfList(List list)
{				       /*{{{ */
    void *payload;
    struct _listheader *pl;

    if (!list || !list->head) {
	return NULL;
    }
    payload = list->head->payload;
    pl = list->head;
    list->head = list->head->next;
    poolReturn(pl);
    list->len--;
    if (list->len == 0) {
	list->head = list->tail = NULL;
    }
    return payload;
}				       /*}}} */

/* This returns the n'th element of the list, as if the list was an array */
void *peekListElement(List list, size_t n)
{				       /*{{{ */
    struct _listheader *pl;
    size_t counter;

    if (!list || !list->head || list->len <= n) {
	if (!list) {
	    fprintf(stderr, "peekListElement: null List passed\n");
	}
	return NULL;
    }
    pl = list->head;
    for (counter = 1; counter <= n; counter++) {
	if (pl->next == NULL) {
	    return NULL;
	}
	pl = pl->next;
    }
    return pl->payload;
}				       /*}}} */

/* This returns the n'th element of the list, as if the list was an array,
 * and removes it from the list */
void *getListElement(List list, size_t n)
{				       /*{{{ */
    struct _listheader *pl;
    size_t counter;
    void *payload = NULL;
    struct _listheader *returnme;

    if (!list || !list->head || list->len <= n) {
	if (!list) {
	    fprintf(stderr, "getListElement: null List passed\n");
	}
	return NULL;
    }
    pl = list->head;
    if (n > 0) {
	for (counter = 1; counter < n; counter++) {
	    if (pl->next == NULL) {
		return NULL;
	    }
	    pl = pl->next;
	}
	if (pl->next) {
	    payload = pl->next->payload;
	    returnme = pl->next;
	    pl->next = pl->next->next;
	    poolReturn(returnme);
	}
    } else {
	payload = pl->payload;
	returnme = pl;
	list->head = pl->next;
	if (list->tail == returnme) {
	    list->tail = NULL;
	}
    }
    list->len --;
    return payload;
}				       /*}}} */

/* This returns the head of the list */
inline void *peekAheadInList(List list)
{				       /*{{{ */
    if (list && list->head) {
	return list->head->payload;
    }
    return NULL;
}				       /*}}} */

/* This tells you how many items there are in the list */
inline unsigned long listLen(List list)
{				       /*{{{ */
    if (list) {
	return list->len;
    } else {
	return 0;
    }
}				       /*}}} */

/* This returns a list iterator to a list */
ListIterator getListIterator(List list)
{				       /*{{{ */
    ListIterator li;

    if (!list) {
	return NULL;
    }
    li = malloc(sizeof(struct _list_iterator));
    li->cur = list->head;
    li->l = list;
    return li;
}				       /*}}} */

/* This returns the value of the current element in the list Iterator */
void *currentListElement(ListIterator li)
{				       /*{{{ */
    if (!li || !(li->cur)) {
	return NULL;
    }
    return li->cur->payload;
}				       /*}}} */

/* This iterates a list iterator */
void *nextListElement(ListIterator li)
{				       /*{{{ */
    void *payload;

    if (!li || !(li->cur)) {
	return NULL;
    }
    payload = li->cur->payload;
    li->cur = li->cur->next;
    return payload;
}				       /*}}} */

/* This sets the iterator back to the beginning */
void resetListIterator(ListIterator li)
{				       /*{{{ */
    if (li) {
	li->cur = li->l->head;
    }
}				       /*}}} */

/* This frees up a list iterator */
void freeListIterator(ListIterator li)
{				       /*{{{ */
    if (li) {
	free(li);
    }
}				       /*}}} */

/* This searches the list for a specific value, and removes it */
void removeFromList(List list, void * item)
{
    struct _listheader *pl;
    struct _listheader *returnme;

    if (!list || !list->head) {
	if (!list) {
	    fprintf(stderr, "getListElement: null List passed\n");
	}
	return;
    }
    pl = list->head;
    if (pl->payload == item) {
	list->head = list->head->next;
	poolReturn(pl);
    } else {
	while (pl->next && pl->next != list->tail) {
	    if (pl->next->payload == item) {
		break;
	    }
	    if (pl->next->next == NULL) {
		return;
	    }
	    pl = pl->next;
	}
	if (pl->next && pl->next->payload == item) {
	    returnme = pl->next;
	    pl->next = pl->next->next;
	    poolReturn(returnme);
	}
    }
}