list.h 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
/***
 * An implementation of a doubly-linked list. This implementation requires
 * that the link pointers be embedded in the list item structures themselves.
 * This idea comes from the Linux kernel implementation of a list.
 *
 * @author Lubomir Bulej
 */

#ifndef _LIST_H_
#define _LIST_H_

#include <assert.h>
13
#include <stddef.h>
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
#include <stdbool.h>

/****************************************************************************\
| PUBLIC DEFS                                                                |
\****************************************************************************/

/*
 * List head/item structure.
 */
struct list {
	struct list * prev;
	struct list * next;
};


/*
 * Various types of visitor/observer functions.
 */
typedef void (* list_destroy_fn) (struct list * item, void * data);
typedef void (* list_visit_fn) (struct list * item, void * data);
typedef int (* list_match_fn) (struct list * item, void * data);


/*
 * Static list head/item initializer.
 */
#define LIST_INIT(name) \
{ \
	.prev = &(name), \
	.next = &(name) \
}


/****************************************************************************\
| PRIVATE INLINE FUNCTIONS AND MACROS                                        |
\****************************************************************************/

/**
 * Calculates an offset of a member in a struct.
 *
 * @param type
 *	the type of the container struct a member is embedded in
 * @param member
 *	the name of the member within the struct
 */
59
#define __offset_of(type, member) offsetof(type, member)
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


/**
 * Calculates the pointer to the containing structure based on the offset
 * of the given member in that structure. Casts the resulting pointer to
 * the type of the containing structure.
 *
 * @param ptr
 *	the pointer to the member
 * @param type
 *	the type of the container struct the member is embedded in
 * @param member
 *	the name of the member within the containing struct
 */
#define __container_of(ptr, type, member) \
({ \
	const typeof (((type *) 0)->member) * __mptr = (ptr); \
	(type *) ((char *) __mptr - __offset_of (type, member)); \
})




/**
 * Initializes a list head/item. An empty list head or an unlinked list item
 * points back to itself, which simplifies runtime checks. Returns the
 * itialized item back to the caller.
 *
 * @param head
 *	the list head/item to initialize
 */
static inline struct list *
__list_init (struct list * head) {
	head->prev = head;
	head->next = head;

	return head;
}


/**
 * Inserts an item into a list by linking it between its immediate predecessor
 * and successor items, and returns back the inserted item. The pointer from
 * the successor is linked first, so there can be a moment when the new item
 * is visible in the list during backward (but not forward) traversal, but this
 * should be handled by the caller.
 *
 * @param item
 *	the item to insert
 * @param pred
 *	the predecessor of the item
 * @param succ
 *	the successor of the item
 */
static inline struct list *
__list_insert_between (struct list * item, struct list * pred, struct list * succ) {
	item->next = succ;
	item->prev = pred;

	succ->prev = item;
	pred->next = item;

	return item;
}


/**
 * Removes item(s) from a list by linking together the items immediately
 * before and after the item(s) being removed. The backward pointer is
 * linked first, because lists are less often traversed backwards. The
 * forward pointer is linked last and when that happens, the list is
 * completely linked. The caller should make sure this is not a problem.
 *
 * @param pred
 *	the item preceding the item(s) to be removed
 * @param succ
 *	the item succeeding the item(s) to be removed
 */
static inline void
__list_remove_between (struct list * pred, struct list * succ) {
	succ->prev = pred;
	pred->next = succ;
}


/****************************************************************************\
| INLINE FUNCTIONS                                                           |
\****************************************************************************/

/**
 * Initializes the given list and returns it back to the caller.
 *
 * @param head
 *	the list head/item to initialize
 */
static inline struct list *
list_init (struct list * head) {
	assert (head != NULL);

	//

	return __list_init (head);
}


/**
 * Returns true if the given list is empty, false otherwise.
 *
 * @param head
 * 	the list to test
 */
static inline bool
list_is_empty (struct list * head) {
	assert (head != NULL);

	//

	return head->next == head;
}


/**
 * Returns a typed structure from the given list item.
 *
 * @param ptr
 *	pointer to struct list
 * @param type
 *	the type of the struct the struct list is embedded in
 * @param member
 *	the name of the struct list field within the containing struct
 */
#define list_item(ptr, type, member) \
	__container_of (ptr, type, member)


/**
 * Inserts a new item to the list after the specified item and
 * returns back the new item.
 *
 * @param item
 *	the item to insert
 * @param pred
 *	the item after which to add the new item
 */
static inline struct list *
list_insert_after (struct list * item, struct list * pred) {
	assert (item != NULL && pred != NULL);

	//

	return __list_insert_between (item, pred, pred->next);
}


/**
 * Inserts a new item to the list before the specified item and
 * returns back the new item.
 *
 * @param item
 *	the item to insert
 * @param succ
 *	the item before which to add the new item
 */
static inline struct list *
list_insert_before (struct list * item, struct list * succ) {
	assert (item != NULL && succ != NULL);

	//

	return __list_insert_between (item, succ->prev, succ);
}


/**
 * Removes the given item from the list and reinitializes it to
 * make it look like an empty list. Then it returns back the
 * removed item.
 *
 * @param item
 *	the item to remove
 */
static inline struct list *
list_remove (struct list * item) {
	assert (item != NULL);

	//

	__list_remove_between (item->prev, item->next);
	__list_init (item);

	return item;
}


/**
 * Removes the successor of the given list item from the list and
 * returns it to the caller. The item is assumed to have a valid
 * successor.
 *
 * @param item
 *	the list item whose successor to remove
 */
static inline struct list *
list_remove_after (struct list * item) {
	assert (item != NULL);

	//

	return list_remove (item->next);
}


/**
 * Removes the predecessor of the given list item from the list and
 * returns it to the caller. The item is assumed to have a valid
 * predecessor.
 *
 * @param item
 *	the list item whose predecessor to remove
 */
static inline struct list *
list_remove_before (struct list * item) {
	assert (item != NULL);

	//

	return list_remove (item->prev);
}


/**
 * Iterates over the given list.
 *
 * @param curr
 *	pointer to struct list to use as a loop counter
 * @param head
 *	the head of the list to iterate over
 */
#define list_for_each(curr, head) \
	assert ((head) != NULL); \
	for (curr = (head)->next; curr != (head); curr = curr->next)


/**
 * Iterates over the given list. This version is safe with respect
 * to removal of a list item.
 *
 * @param curr
 *	pointer to struct list to use as a loop counter
 * @param next
 *	pinter to struct list to hold the pointer to next item
 * @param head
 *	the head of the list to iterate over
 */
#define list_for_each_safe(curr, next, head) \
	assert ((head) != NULL); \
	for (curr = (head)->next, next = curr->next; \
		curr != (head); curr = next, next = curr->next)


/**
 * Iterates over the given list in reverse.
 *
 * @param curr
 *	pointer to struct list to use as a loop counter
 * @param head
 *	the head of the list to iterate over
 */
#define list_for_each_reverse(curr, head) \
	assert ((head) != NULL); \
	for (curr = (head)->prev; curr != (head); curr = curr->prev)


/**
 * Iterates over the given list of typed entries.
 *
 * @param curr
 *	pointer to (type) to use for loop control
 * @param head
 *	the head of the list to iterate over
 * @param member
 *	the name of the struct list member within the containing type
 */
#define list_for_each_item(curr, head, member) \
	assert ((head) != NULL); \
	for ( \
		curr = list_item ((head)->next, typeof (* curr), member); \
		(&curr->member) != (head); \
		curr = list_item (curr->member.next, typeof (* curr), member) \
	)


/**
 * Iterates over the given list of typed entries in reverse.
 *
 * @param curr
 *	pointer to (type) to use for loop control
 * @param head
 *	the head of the list to iterate over
 * @param member
 *	the name of the struct list member within the containing type
 */
#define list_for_each_item_reverse(curr, head, member) \
	assert ((head) != NULL); \
	for ( \
		curr = list_item ((head)->prev, typeof (* curr), member); \
		(&curr->member) != (head); \
		curr = list_item (curr->member.prev, typeof (* curr), member) \
	)


/**
 * Destroys the given list by removing all items from the list
 * and calling a destroy function on each item. The head of the
 * list is not considered to be a valid item, so it is not
 * destroyed.
 *
 * @param head
 *	the list to destroy
 * @param destroy
 *	the destructor function to call on each item
 * @param data
 *	additional data for the destroy callback
 */
static inline void
list_destroy (struct list * head, list_destroy_fn destroy, void * data) {
	assert (head != NULL);

	//

	while (! list_is_empty (head)) {
		// unlink head successor from the list and destroy it
		destroy (list_remove_after (head), data);
	}
}


/**
 * Walks through the given list and calls a visitor function
 * on each list item.
 *
 * @param head
 *	the list to walk
 * @param visit
 *	the function to call with each item
 * @param data
 *	additional data for the visit callback
 */
static inline void
list_walk (struct list * head, list_visit_fn visit, void * data) {
	assert (head != NULL);

	//

	struct list * item;
	list_for_each (item, head) {
		visit (item, data);
	}
}


/**
 * Walks through the given list and calls a match function on each item.
 * If the match function returns true, the matching item is returned to the
 * caller and the traversal stops. Returns NULL when the traversal reaches
 * the end of the list without finding a match.
 *
 * @param head
 *	the list to search
 * @param match
 *	the function to call with each item
 * @param data:
 *	additional data for the match callback
 */
static inline struct list *
list_find (struct list * head, list_match_fn match, void * data) {
	assert (head != NULL);

	//

	struct list * item;
	list_for_each (item, head) {
		if (match (item, data))
			return item;
	}

	return NULL;
}

#endif /* _LIST_H_ */