summaryrefslogtreecommitdiff
path: root/src/libs/list/list.c
blob: 9d86538a533b480645ef780e500ea2dfa5fe513d (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
/*
 * Copyright (C) 2005  Serge van den Boom
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of version 2 of the GNU General Public License as
 * published by the Free Software Foundation.
 * Nota bene: later versions of the GNU General Public License do not apply
 * to this program.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */

#ifndef LIST_INTERNAL
	// If list is already defined, this file is included
	// as a template. In this case list.h has already been included.
#	define LIST_INTERNAL
#	include "list.h"
#endif

#include "libs/memlib.h"
#define malloc HMalloc
#define free HFree
#define realloc HRealloc

#include <assert.h>

static inline LIST_(List) *LIST_(allocList)(void);
static inline void LIST_(freeList)(LIST_(List) *list);
static inline LIST_(Link) * LIST_(allocLink)(void);
static inline void LIST_(freeLink)(LIST_(Link) *link);


LIST_(List) *
LIST_(newList)(void) {
	LIST_(List) *list;

	list = LIST_(allocList)();
	if (list == NULL)
		return NULL;

	list->first = NULL;
	list->end = &list->first;
	return list;
}

void
LIST_(deleteList)(LIST_(List) *list)
{
	LIST_(Link) *link;
	LIST_(Link) *next;

	for (link = list->first; link != NULL; link = next)
	{
		next = link->next;
		LIST_(freeLink)(link);
	}

	LIST_(freeList)(list);
}

void
LIST_(add)(LIST_(List) *list, LIST_(Entry) entry) {
	LIST_(Link) *link;

	link = LIST_(allocLink)();
	link->entry = entry;
	link->next = NULL;
	*list->end = link;
	list->end = &link->next;
}

static inline LIST_(Link) **
LIST_(findLink)(LIST_(List) *list, LIST_(Entry) entry) {
	LIST_(Link) **linkPtr;

	for (linkPtr = &list->first; *linkPtr != NULL;
			linkPtr = &(*linkPtr)->next) {
		if ((*linkPtr)->entry == entry)
			return linkPtr;
	}
	return NULL;
}

static inline void
LIST_(removeLink)(LIST_(List) *list, LIST_(Link) **linkPtr) {
	LIST_(Link) *link = *linkPtr;

	*linkPtr = link->next;
	if (&link->next == list->end)
		list->end = linkPtr;
	LIST_(freeLink)(link);
}

void
LIST_(remove)(LIST_(List) *list, LIST_(Entry) entry) {
	LIST_(Link) **linkPtr;

	linkPtr = LIST_(findLink)(list, entry);
	assert(linkPtr != NULL);
	LIST_(removeLink)(list, linkPtr);
}


static inline LIST_(List) *
LIST_(allocList)(void) {
	return malloc(sizeof (LIST_(List)));
}

static inline void
LIST_(freeList)(LIST_(List) *list) {
	free(list);
}

static inline LIST_(Link) *
LIST_(allocLink)(void) {
	return malloc(sizeof (LIST_(Link)));
}

static inline void
LIST_(freeLink)(LIST_(Link) *link) {
	free(link);
}