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
|
/* LINKLIST.H (c) Copyright "Fish" (David B. Trout), 2001-2005 */
/* Linked-list macros */
#ifndef _LLIST_
#define _LLIST_
//////////////////////////////////////////////////////////////////////////////////////////
/*
This module is a standalone collection of linked-list definition,
and manipulation macros originally defined for Windows NT development.
Samples:
// Define a list head.
LIST_ENTRY FooList;
// Define a structure that will be on the list.
// (NOTE: To make debugging easier, it's best to define the LIST_ENTRY field
// as the very first field in your structure, but it's not a requirement.)
typedef struct _FOO
{
LIST_ENTRY FooListEntry;
.
.
.
}
FOO, *PFOO;
// Initialize an empty list.
InitializeListHead(&FooList);
// Create an object, append it to the end of the list.
FOO* pFoo;
pFoo = ALLOC(sizeof(FOO));
{check for errors, initialize FOO structure}
InsertListTail(&FooList,&pFoo->FooListEntry);
// Scan list and delete selected items.
LIST_ENTRY* pListEntry = FooList.Flink;
while (pListEntry != &FooList)
{
pFoo = CONTAINING_RECORD(pListEntry,FOO,FooListEntry);
pListEntry = pListEntry->Flink;
if (SomeFunction(pFoo))
{
RemoveListEntry(&pFoo->FooListEntry);
FREE(pFoo);
}
}
// Purge all items from a list.
while (!IsListEmpty(&FooList))
{
pListEntry = RemoveListHead(&FooList);
pFoo = CONTAINING_RECORD(pListEntry,FOO,FooListEntry);
FREE(pFoo);
}
*/
//////////////////////////////////////////////////////////////////////////////////////////
#if !defined( WIN32 ) || \
( defined( _MSVC_ ) && !defined(_WINNT_) && !defined(_WINNT_H) )
typedef struct _LIST_ENTRY
{
struct _LIST_ENTRY* Flink; // ptr to next link in chain
struct _LIST_ENTRY* Blink; // ptr to previous link in chain
}
LIST_ENTRY, *PLIST_ENTRY;
#endif // !defined(_WINNT_)
//////////////////////////////////////////////////////////////////////////////////////////
//
// <typename>* CONTAINING_RECORD
// (
// VOID* address,
// <typename> type,
// <fieldname> field
// );
//
/*
Retrieves a typed pointer to a linked list item given the address of the
link storage structure embedded in the linked list item, the type of the
linked list item, and the field name of the embedded link storage structure.
NOTE: since this macro uses compile-time type knowledge,
there is no equivalent C procedure for this macro.
Arguments:
address - The address of a LIST_ENTRY structure embedded in an a linked list item.
type - The type name of the containing linked list item structure.
field - The field name of the LIST_ENTRY structure embedded within the linked list item structure.
Return Value:
Pointer to the linked list item.
For Example:
If your record looked like this:
typedef struct _MYRECORD
{
int alpha;
int beta;
LIST_ENTRY gamma;
int delta;
int epsilon;
}
MYRECORD, *PMYRECORD;
Then, given a variable called "pListEntry" that pointed to the LIST_ENTRY field
within your record (i.e. gamma), you can obtain a pointer to the beginning of your
record by coding the following CONTAINING_RECORD macro expression:
MYRECORD* pMyRecord; // the variable you wish to point to your record
// LIST_ENTRY* pListEntry; // already points to the LIST_ENTRY field within
// your record (i.e. points to field "gamma")
pMyRecord = CONTAINING_RECORD(pListEntry,MYRECORD,gamma);
--*/
#ifndef CONTAINING_RECORD
#define CONTAINING_RECORD( address, type, field ) \
\
( (type*) ((char*)(address) - (char*)(&((type*)0)->field)) )
#endif
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//
// From NTRTL.H: Doubly-linked list manipulation routines.
//
// (NOTE: implemented as macros but logically these are procedures)
//
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
//
// void InitializeListHead
// (
// LIST_ENTRY* head
// );
//
/*
Initializes a LIST_ENTRY structure to be the head of an initially empty linked list.
Arguments:
head - Reference to the structure to be initialized.
Return Value:
None
*/
#define InitializeListHead(head) \
\
( (head)->Flink = (head)->Blink = (head) )
//////////////////////////////////////////////////////////////////////////////////////////
// (I created this one myself -- Fish)
//
// void InitializeListLink
// (
// LIST_ENTRY* link
// );
//
/*
Initializes a LIST_ENTRY structure
to be an unlinked link.
Arguments:
link - Reference to the structure to be initialized.
Return Value:
None
*/
#define InitializeListLink(link) \
\
( (link)->Flink = (link)->Blink = (NULL) )
//////////////////////////////////////////////////////////////////////////////////////////
//
// <boolean> IsListEmpty
// (
// LIST_ENTRY* head
// );
//
/*
Determines whether or not a list is empty.
Arguments:
head - Reference to the head of the linked list to be examined.
Return Value:
<true> - List is empty.
<false> - List contains at least one item.
--*/
#define IsListEmpty(head) \
\
( (head)->Flink == (head) )
//////////////////////////////////////////////////////////////////////////////////////////
//
// VOID InsertListHead
// (
// LIST_ENTRY* head,
// LIST_ENTRY* entry
// );
//
/*
Inserts a new item as the "head" (first) item of a linked list.
Arguments:
head - Reference to the head of the linked list to be operated upon.
entry - Reference to the linkage structure embedded in the linked list item
to be added to the linked list.
Return Value:
None
*/
#define InsertListHead(head,entry) \
{ \
LIST_ENTRY* _EX_Head; \
LIST_ENTRY* _EX_Next; \
\
_EX_Head = (head); \
_EX_Next = _EX_Head->Flink; \
\
(entry)->Flink = _EX_Next; \
(entry)->Blink = _EX_Head; \
\
_EX_Head->Flink = (entry); \
_EX_Next->Blink = (entry); \
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// VOID InsertListTail
// (
// LIST_ENTRY* head,
// LIST_ENTRY* entry
// );
//
/*
Inserts a new item as the "tail" (last) item of a linked list.
Arguments:
head - Reference to the head of the linked list to be operated upon.
entry - Reference to the linkage structure embedded in the linked list item
to be added to the linked list.
Return Value:
None
*/
#define InsertListTail(head,entry) \
{ \
LIST_ENTRY* _EX_Head; \
LIST_ENTRY* _EX_Tail; \
\
_EX_Head = (head); \
_EX_Tail = _EX_Head->Blink; \
\
(entry)->Flink = _EX_Head; \
(entry)->Blink = _EX_Tail; \
\
_EX_Tail->Flink = (entry); \
_EX_Head->Blink = (entry); \
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// LIST_ENTRY* RemoveListHead
// (
// LIST_ENTRY* head
// );
//
/*
Removes the "head" (first) item from a linked list, returning the pointer
to the removed entry's embedded linkage structure. Attempting to remove the
head item from a (properly initialized) linked list is a no-op and returns
the pointer to the head of the linked list.
The caller may use the CONTAINING_RECORD macro to amplify the returned
linkage structure pointer to the containing linked list item structure.
Arguments:
head - Reference to the head of the linked list to be operated upon.
Return Value:
Returns a pointer to the newly removed linked list item's embedded linkage structure,
or the linked list head in the case of an empty list.
*/
#define RemoveListHead(head) \
\
(head)->Flink; \
\
{ \
RemoveListEntry((head)->Flink) \
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// LIST_ENTRY* RemoveListTail
// (
// LIST_ENTRY* head
// );
//
/*
Removes the "tail" (last) item from a linked list, returning the pointer to the
removed entry's embedded linkage structure. Attempting to remove the tail item
from a (properly initialized) linked list is a no-op and returns the pointer to
the head of the linked list.
The caller may use the CONTAINING_RECORD macro to amplify the returned
linkage structure pointer to the containing linked list item structure.
Arguments:
head - Reference to the head of the linked list to be operated upon.
Return Value:
Pointer to the newly removed linked list item's embedded linkage structure,
or the linked list head in the case of an empty list.
*/
#define RemoveListTail(head) \
\
(head)->Blink; \
\
{ \
RemoveListEntry((head)->Blink) \
}
//////////////////////////////////////////////////////////////////////////////////////////
//
// VOID RemoveListEntry
// (
// LIST_ENTRY* entry
// );
//
/*
Removes an item from a linked list. (Removing the head of an empty list is a no-op.)
Arguments:
entry - Reference to the linkage structure embedded in a linked list item structure.
Return Value:
None
*/
#define RemoveListEntry(entry) \
{ \
LIST_ENTRY* _EX_Blink; \
LIST_ENTRY* _EX_Flink; \
\
_EX_Flink = (entry)->Flink; \
_EX_Blink = (entry)->Blink; \
\
_EX_Blink->Flink = _EX_Flink; \
_EX_Flink->Blink = _EX_Blink; \
}
//////////////////////////////////////////////////////////////////////////////////////////
#endif // _LLIST_
|