forked from hercules-390/hyperion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
chain.h
310 lines (259 loc) · 10.1 KB
/
chain.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
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
/* CHAIN.H (c) Mark L. Gaubatz, 1985-2012 */
/* Chain and queue macros and inline routines */
/* Adapted for use by Hercules */
/* */
/* Released under "The Q Public License Version 1" */
/* (http://www.hercules-390.org/herclic.html) as modifications to */
/* Hercules. */
/* */
/*********************************************************************/
#ifndef _CHAIN_H_
#define _CHAIN_H_
/*-------------------------------------------------------------------*/
/* Define local macros for inline routines */
/*-------------------------------------------------------------------*/
#define anchor_id "Anchor->"
#define aeyesize 8
#define eyesize 8
#define raise_chain_validation_error \
CRASH();
#define chain_validate_pointer(_arg) \
if (_arg == 0 || _arg == NULL) \
raise_chain_validation_error;
/*-------------------------------------------------------------------*/
/* Chain Block */
/*-------------------------------------------------------------------*/
typedef struct _CHAINBLK { /* Chain definition block */
char anchoreye[aeyesize]; /* Anchor identifier */
char eyecatcher[eyesize]; /* Eyecatcher to check */
u_int containersize; /* Size of container */
LOCK lock; /* Lock for chain */
uintptr_t *first; /* First entry pointer */
uintptr_t *last; /* Last entry pointer */
uintptr_t offset; /* Offset to container start */
} CHAINBLK;
/*-------------------------------------------------------------------*/
/* Chain */
/*-------------------------------------------------------------------*/
typedef struct _CHAIN { /* Chain member definition */
char eyecatcher[eyesize]; /* Eyecatcher to check */
u_int containersize; /* Size of container */
uintptr_t *prev; /* -> previous chain entry */
uintptr_t *next; /* -> next chain entry */
uintptr_t offset; /* Offset to container start */
CHAINBLK *anchor; /* -> owning CHAINBLK */
} CHAIN;
/*-------------------------------------------------------------------*/
/* Chain management inline routines */
/*-------------------------------------------------------------------*/
static INLINE uintptr_t *chain_container(CHAIN *entry)
{
return ((uintptr_t *)entry + entry->offset);
}
static INLINE CHAINBLK *chain_validate_entry_header(CHAIN *entry)
{
/* Ensure valid entry pointers */
chain_validate_pointer(entry);
chain_validate_pointer(entry->anchor);
/* Make sure this is the anchor for a chain */
if (memcmp(entry->anchor->anchoreye, anchor_id, aeyesize))
raise_chain_validation_error;
/* Validate eyecatcher */
if (memcmp(entry->anchor->eyecatcher, entry->eyecatcher, eyesize))
raise_chain_validation_error;
return entry->anchor;
}
static INLINE void chain_init_anchor(void *container, CHAINBLK *anchor, char *eyecatcher, const u_int containersize)
{
chain_validate_pointer(container);
chain_validate_pointer(anchor);
chain_validate_pointer(eyecatcher);
memcpy(anchor->anchoreye, anchor_id, aeyesize);
{
register char *a = anchor->eyecatcher;
register char *e = eyecatcher;
register char *limit = a + eyesize;
for (; a < limit && *e; a++, e++)
*a = *e;
if (a == anchor->eyecatcher)
raise_chain_validation_error;
for (; a < limit; a++)
*a = ' ';
}
anchor->containersize = containersize;
initialize_lock(&anchor->lock);
anchor->first = anchor->last = 0;
anchor->offset = (uintptr_t) container - (uintptr_t) anchor;
}
static INLINE void chain_init_entry(CHAINBLK *anchor, void *container, CHAIN *entry)
{
chain_validate_pointer(container);
chain_validate_pointer(anchor);
chain_validate_pointer(entry);
memcpy(&entry->eyecatcher, &anchor->eyecatcher, eyesize);
entry->containersize = anchor->containersize;
entry->prev = entry->next = 0;
entry->offset = (uintptr_t *) container - (uintptr_t *) entry;
entry->anchor = anchor;
}
static INLINE void chain_first(CHAIN *entry)
{
register CHAINBLK *anchor = chain_validate_entry_header(entry);
if (!anchor->first)
anchor->first = anchor->last = (uintptr_t *)entry;
else
{
CHAIN *next = (CHAIN *)anchor->first + entry->offset;
entry->next = anchor->first;
next->prev = anchor->first = (uintptr_t *)entry;
}
}
static INLINE void chain_first_locked(CHAIN *entry)
{
register CHAINBLK *anchor = chain_validate_entry_header(entry);
obtain_lock(&anchor->lock);
chain_first(entry);
release_lock(&anchor->lock);
}
static INLINE void chain_before(CHAIN *entry, CHAIN *before)
{
register CHAINBLK *anchor = chain_validate_entry_header(entry);
if ((!before->prev && anchor->first != (uintptr_t *)before) ||
(before->prev && anchor->first == (uintptr_t *)before))
raise_chain_validation_error;
entry->next = (uintptr_t *)before;
entry->prev = before->prev;
before->prev = (uintptr_t *)entry;
if (!entry->prev)
anchor->first = (uintptr_t *)entry;
}
static INLINE void chain_after(CHAIN *entry, CHAIN *after)
{
register CHAINBLK *anchor = chain_validate_entry_header(entry);
if ((!after->next && anchor->last != (uintptr_t *)after) ||
(after->next && anchor->last == (uintptr_t *)after))
raise_chain_validation_error;
entry->next = after->next;
entry->prev = (uintptr_t *)after;
after->next = (uintptr_t *)entry;
if (!entry->next)
anchor->last = (uintptr_t *)entry;
}
static INLINE void chain_last(CHAIN *entry)
{
register CHAINBLK *anchor = chain_validate_entry_header(entry);
if (!anchor->last)
anchor->first = anchor->last = (uintptr_t *)entry;
else
{
CHAIN *prev = (CHAIN *)anchor->last + entry->offset;
entry->prev = anchor->last;
prev->next = anchor->last = (uintptr_t *)entry;
}
}
static INLINE void chain_last_locked(CHAIN *entry)
{
register CHAINBLK *anchor = chain_validate_entry_header(entry);
obtain_lock(&anchor->lock);
chain_last(entry);
release_lock(&anchor->lock);
}
static INLINE void unchain(CHAIN *entry)
{
register CHAINBLK *anchor = chain_validate_entry_header(entry);
if (entry->prev)
{
CHAIN *t = (CHAIN *)entry->prev;
t->next = entry->next;
}
else
anchor->first = entry->next;
if (entry->next)
{
CHAIN *t = (CHAIN *)entry->next;
t->prev = entry->prev;
}
else
anchor->last = entry->prev;
entry->prev = entry->next = 0;
}
static INLINE uintptr_t *unchain_first(CHAINBLK *anchor)
{
register CHAIN *entry = (CHAIN *)anchor->first;
if (!entry)
return 0;
unchain(entry);
return chain_container(entry);
}
static INLINE uintptr_t *unchain_first_locked(CHAINBLK *anchor)
{
register CHAIN *entry = (CHAIN *)anchor->first;
if (!entry)
return 0;
obtain_lock(&anchor->lock);
entry = (CHAIN *)anchor->first;
if (entry)
unchain(entry);
release_lock(&anchor->lock);
return chain_container(entry);
}
static INLINE uintptr_t *unchain_last(CHAINBLK *anchor)
{
register CHAIN *entry = (CHAIN *)anchor->last;
if (!entry)
return 0;
unchain(entry);
return chain_container(entry);
}
static INLINE uintptr_t *unchain_last_locked(CHAINBLK *anchor)
{
register CHAIN *entry = (CHAIN *)anchor->last;
if (!entry)
return 0;
obtain_lock(&anchor->lock);
entry = (CHAIN *)anchor->last;
if (entry)
unchain(entry);
release_lock(&anchor->lock);
return chain_container(entry);
}
static INLINE void unchain_locked(CHAIN *entry)
{
register CHAINBLK *anchor = chain_validate_entry_header(entry);
obtain_lock(&anchor->lock);
unchain(entry);
release_lock(&anchor->lock);
}
/*-------------------------------------------------------------------*/
/* Chain aliases */
/*-------------------------------------------------------------------*/
static INLINE void chain(CHAIN *entry)
{chain_last(entry);}
/*-------------------------------------------------------------------*/
/* Queue aliases */
/*-------------------------------------------------------------------*/
#define QUEUEBLK CHAINBLK
#define QUEUE CHAIN
static INLINE void queue_init_anchor(void *container, QUEUEBLK *anchor, char *eyecatcher, const u_int containersize)
{chain_init_anchor(container, anchor, eyecatcher, containersize);}
static INLINE void queue_init_entry(QUEUEBLK *anchor, void *container, QUEUE *entry)
{chain_init_entry(anchor, container, entry);}
static INLINE void queue(QUEUE *entry)
{chain_last(entry);}
static INLINE void queue_fifo(QUEUE *entry)
{chain_last(entry);}
static INLINE void queue_lifo(QUEUE *entry)
{chain_first(entry);}
static INLINE uintptr_t *dequeue_locked(QUEUEBLK *anchor)
{return unchain_first_locked(anchor);}
static INLINE uintptr_t *dequeue(QUEUEBLK *anchor)
{return unchain_first(anchor);}
/*-------------------------------------------------------------------*/
/* Undefine local macros */
/*-------------------------------------------------------------------*/
#undef chain_validate_pointer
#undef raise_chain_validation_error
#undef eyesize
#undef aeyesize
#undef anchor_id
#endif /* _CHAIN_H_ */