Changes in uspace/lib/c/include/adt/list.h [062d900:7856d09] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/include/adt/list.h
r062d900 r7856d09 1 1 /* 2 2 * Copyright (c) 2001-2004 Jakub Jermar 3 * Copyright (c) 201 1Jiri Svoboda3 * Copyright (c) 2013 Jiri Svoboda 4 4 * All rights reserved. 5 5 * … … 38 38 39 39 #include <assert.h> 40 #include <stdbool.h> 40 41 #include <unistd.h> 41 42 … … 65 66 66 67 #define list_get_instance(link, type, member) \ 67 ((type *) (((void *)(link)) - ((void *) &(((type *) NULL)->member)))) 68 69 #define list_foreach(list, iterator) \ 70 for (link_t *iterator = (list).head.next; \ 71 iterator != &(list).head; iterator = iterator->next) 68 ((type *) (((void *)(link)) - list_link_to_void(&(((type *) NULL)->member)))) 69 70 #define list_foreach(list, member, itype, iterator) \ 71 for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \ 72 for (link_t *_link = (list).head.next; \ 73 iterator = list_get_instance(_link, itype, member), \ 74 _link != &(list).head; _link = _link->next) 75 76 #define list_foreach_rev(list, member, itype, iterator) \ 77 for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \ 78 for (link_t *_link = (list).head.prev; \ 79 iterator = list_get_instance(_link, itype, member), \ 80 _link != &(list).head; _link = _link->prev) 72 81 73 82 /** Unlike list_foreach(), allows removing items while traversing a list. 74 * 83 * 75 84 * @code 76 85 * list_t mylist; … … 103 112 104 113 #define assert_link_not_used(link) \ 105 assert( ((link)->prev == NULL) && ((link)->next == NULL))114 assert(!link_used(link)) 106 115 107 116 /** Returns true if the link is definitely part of a list. False if not sure. */ … … 238 247 static inline link_t *list_last(list_t *list) 239 248 { 240 return ((list->head.prev == &list->head) ? NULL : list->head.prev); 249 return (list->head.prev == &list->head) ? NULL : list->head.prev; 250 } 251 252 /** Get next item in list. 253 * 254 * @param link Current item link 255 * @param list List containing @a link 256 * 257 * @return Next item or NULL if @a link is the last item. 258 * 259 */ 260 static inline link_t *list_next(link_t *link, const list_t *list) 261 { 262 return (link->next == &list->head) ? NULL : link->next; 263 } 264 265 /** Get previous item in list. 266 * 267 * @param link Current item link 268 * @param list List containing @a link 269 * 270 * @return Previous item or NULL if @a link is the first item. 271 * 272 */ 273 static inline link_t *list_prev(link_t *link, const list_t *list) 274 { 275 return (link->prev == &list->head) ? NULL : link->prev; 241 276 } 242 277 … … 308 343 unsigned int cnt = 0; 309 344 310 list_foreach(*list, link) { 345 link_t *link = list_first(list); 346 while (link != NULL) { 311 347 if (cnt == n) 312 348 return link; 313 349 314 350 cnt++; 351 link = list_next(link, list); 315 352 } 316 353 317 354 return NULL; 355 } 356 357 /** Verify that argument type is a pointer to link_t (at compile time). 358 * 359 * This can be used to check argument type in a macro. 360 */ 361 static inline const void *list_link_to_void(const link_t *link) 362 { 363 return link; 364 } 365 366 /** Determine if link is used. 367 * 368 * @param link Link 369 * @return @c true if link is used, @c false if not. 370 */ 371 static inline bool link_used(link_t *link) 372 { 373 if (link->prev == NULL && link->next == NULL) 374 return false; 375 376 assert(link->prev != NULL && link->next != NULL); 377 return true; 318 378 } 319 379
Note:
See TracChangeset
for help on using the changeset viewer.