Changeset 99c2c69e in mainline for uspace/lib/c/include/adt/list.h
- Timestamp:
- 2013-09-13T00:36:30Z (11 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 67fbd5e
- Parents:
- 7f84430 (diff), 11d41be5 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/include/adt/list.h
r7f84430 r99c2c69e 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 * … … 65 65 66 66 #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) 67 ((type *) (((void *)(link)) - list_link_to_void(&(((type *) NULL)->member)))) 68 69 #define list_foreach(list, member, itype, iterator) \ 70 for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \ 71 for (link_t *_link = (list).head.next; \ 72 iterator = list_get_instance(_link, itype, member), \ 73 _link != &(list).head; _link = _link->next) 72 74 73 75 /** Unlike list_foreach(), allows removing items while traversing a list. 74 * 76 * 75 77 * @code 76 78 * list_t mylist; … … 238 240 static inline link_t *list_last(list_t *list) 239 241 { 240 return ((list->head.prev == &list->head) ? NULL : list->head.prev); 242 return (list->head.prev == &list->head) ? NULL : list->head.prev; 243 } 244 245 /** Get next item in list. 246 * 247 * @param link Current item link 248 * @param list List containing @a link 249 * 250 * @return Next item or NULL if @a link is the last item. 251 * 252 */ 253 static inline link_t *list_next(link_t *link, const list_t *list) 254 { 255 return (link->next == &list->head) ? NULL : link->next; 256 } 257 258 /** Get previous item in list. 259 * 260 * @param link Current item link 261 * @param list List containing @a link 262 * 263 * @return Previous item or NULL if @a link is the first item. 264 * 265 */ 266 static inline link_t *list_prev(link_t *link, const list_t *list) 267 { 268 return (link->prev == &list->head) ? NULL : link->prev; 241 269 } 242 270 … … 308 336 unsigned int cnt = 0; 309 337 310 list_foreach(*list, link) { 338 link_t *link = list_first(list); 339 while (link != NULL) { 311 340 if (cnt == n) 312 341 return link; 313 342 314 343 cnt++; 344 link = list_next(link, list); 315 345 } 316 346 317 347 return NULL; 348 } 349 350 /** Verify that argument type is a pointer to link_t (at compile time). 351 * 352 * This can be used to check argument type in a macro. 353 */ 354 static inline const void *list_link_to_void(const link_t *link) 355 { 356 return link; 318 357 } 319 358
Note:
See TracChangeset
for help on using the changeset viewer.