-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkevil.h
144 lines (107 loc) · 4.9 KB
/
linkevil.h
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
/* Evil linked-list implementation in C with macros
Copyright (C) 1999 Evin Robertson
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
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, USA.
The author can be reached at [email protected]
*/
#define LEaddm(list, member, mallocfunc) do { \
void *temppointer = list; \
list = mallocfunc(sizeof(*list)); \
*list = member; \
list->next = temppointer; \
} while(0)
#define LEadd(list, member) LEaddm(list, member, n_malloc)
#define LEremovem(list, freefunc) do { \
if(list) { \
void *temppointer = list; \
list = list->next; \
freefunc(temppointer); \
} \
} while(0);
#define LEremove(list) LEremovem(list, n_free)
#define LEdestructm(list, destruct, freefunc) do { \
while(list) { \
void *temppointer = list; \
destruct; \
list = list->next; \
freefunc(temppointer); \
} \
} while(0)
#define LEdestruct(list, destruct) LEdestructm(list, destruct, n_free)
#define LEdestroym(list, freefunc) LEdestructm(list, , freefunc)
#define LEdestroy(list) LEdestroym(list, n_free)
#define LEsearch(list, dest, cond) \
do { \
for(dest = list; dest; dest=dest->next) \
if(cond) break; \
} while(0)
/* temp != NULL if it successfully performed the removal */
#define LEsearchremovem(list, dest, temp, cond, destruct, freefunc) \
do { \
dest = list; \
if(dest && (cond)) { \
temp = dest; \
destruct; \
LEremovem(list, freefunc); \
} else { \
for(temp = list; temp; temp=temp->next) { \
dest=temp->next; \
if(dest && (cond)) { \
temp->next = dest->next; \
destruct; \
freefunc(dest); \
} \
} \
} \
} while(0)
#define LEsearchremove(list, dest, temp, cond, destruct) LEsearchremovem(list, dest, temp, cond, destruct, n_free)
#define LEappend(dest, src) do { \
void *temppointer = dest; \
if(dest) { \
while(dest->next) \
dest = dest->next; \
dest->next = src; \
dest = temppointer; \
} else { \
dest = scr; \
} \
} while(0)
/* This is just an example: */
#if 0
/*
Use: Declare a struct like this:
*/
typedef struct {
struct mylist *next; /* The evil linked list library depends on this */
int myint;
char *mystring;
} mylist;
void foo() {
mylist *foo = NULL;
mylist *found;
mylist *another = NULL;
mylist mymember1 = { NULL, 42, "frogs" };
mylist mymember2 = { NULL, 38, "potato" };
mylist mymember3 = { NULL, 21, "monkey" };
LEremove(foo); /* Remove nonexistent head element - this is safe */
LEadd(foo, mymember1); /* Add an element to the head */
LEadd(foo, mymember2); /* Add another element to the head */
LEadd(foo, mymember3);
LEadd(foo, mymember1);
LEremove(foo); /* Remove the head element */
LEsearch(foo, found, (strcmp(found->mystring, "potato") == 0));
LEdup(another, found);
LEdestroy(foo);
LEadd(foo, mymember3);
LEprepend(another, foo);
}
#endif