list.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2009-2012, 2015-2017 Université de Bordeaux
  4. * Copyright (C) 2010, 2011, 2016 CNRS
  5. *
  6. * StarPU is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation; either version 2.1 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * StarPU is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  14. *
  15. * See the GNU Lesser General Public License in COPYING.LGPL for more details.
  16. */
  17. #ifndef __LIST_H__
  18. #define __LIST_H__
  19. /** @file
  20. * @brief Doubly-linked lists
  21. */
  22. /** @remarks list how-to
  23. * *********************************************************
  24. * LIST_TYPE(FOO, content);
  25. *
  26. * - declares the following types:
  27. *
  28. * + for cells : struct FOO
  29. * + for lists : struct FOO_list
  30. * + for iterators : struct FOO
  31. *
  32. * - declares the following inlines (all O(1) except stated otherwise, n is the number of elements) :
  33. *
  34. * * Create a cell
  35. * struct FOO* FOO_new(void);
  36. *
  37. * * Suppress a cell
  38. * void FOO_delete(struct FOO*);
  39. *
  40. * * Create a list (initially empty)
  41. * struct FOO_list* FOO_list_new(void);
  42. *
  43. * * Initializes a list (initially empty)
  44. * void FOO_list_init(struct FOO_list*);
  45. *
  46. * * Suppresses a liste
  47. * void FOO_list_delete(struct FOO_list*);
  48. *
  49. * * Check whether a list is empty
  50. * int FOO_list_empty(struct FOO_list*);
  51. *
  52. * * Remove a given cell from the list
  53. * void FOO_list_erase(struct FOO_list*, struct FOO*);
  54. *
  55. * * Add a cell at the back of the list
  56. * void FOO_list_push_back(struct FOO_list*, struct FOO*);
  57. *
  58. * * Add a cell at the front of the list
  59. * void FOO_list_push_front(struct FOO_list*, struct FOO*);
  60. *
  61. * * Add a cell before a given cell of a list
  62. * void FOO_list_insert_before(struct FOO_list*, struct FOO*new, struct FOO*);
  63. *
  64. * * Add a cell after a given cell of a list
  65. * void FOO_list_insert_after(struct FOO_list*, struct FOO*new, struct FOO*);
  66. *
  67. * * Append the second list at the end of the first list
  68. * struct FOO* FOO_list_push_list_back(struct FOO_list*, struct FOO_list*);
  69. *
  70. * * Prepend the first list at the beginning of the second list
  71. * struct FOO* FOO_list_push_list_front(struct FOO_list*, struct FOO_list*);
  72. *
  73. * * Return and remove the node at the back of the list
  74. * struct FOO* FOO_list_pop_back(struct FOO_list*);
  75. *
  76. * * Return and remove the node at the front of the list
  77. * struct FOO* FOO_list_pop_front(struct FOO_list*);
  78. *
  79. * * Return the node at the back of the list
  80. * struct FOO* FOO_list_back(struct FOO_list*);
  81. *
  82. * * Return the node at the front of the list
  83. * struct FOO* FOO_list_front(struct FOO_list*);
  84. *
  85. * * Check that the list chaining is coherent (O(n))
  86. * int FOO_list_check(struct FOO_list*);
  87. *
  88. * * Return the first cell of the list (from the front)
  89. * struct FOO* FOO_list_begin(struct FOO_list*);
  90. *
  91. * * Return the value to be tested at the end of the list (at the back)
  92. * struct FOO* FOO_list_end(struct FOO_list*);
  93. *
  94. * * Return the next element of the list (from the front)
  95. * struct FOO* FOO_list_next(struct FOO*)
  96. *
  97. * * Return the last element of the list (from the back)
  98. * struct FOO* FOO_list_last(struct FOO_list*);
  99. *
  100. * * Return the value to be tested at the beginning of the list (at the fromt)
  101. * struct FOO* FOO_list_alpha(struct FOO_list*);
  102. *
  103. * * Return the previous element of the list (from the back)
  104. * struct FOO* FOO_list_prev(struct FOO*)
  105. *
  106. * * Return the size of the list in O(n)
  107. * int FOO_list_size(struct FOO_list*)
  108. *
  109. * * Return the position of the cell in the list (indexed from 0) (O(n) on average)
  110. * int FOO_list_member(struct FOO_list*, struct FOO*)
  111. *
  112. * * Test whether the cell is in the list (O(n) on average)
  113. * int FOO_list_ismember(struct FOO_list*, struct FOO*)
  114. *
  115. * *********************************************************
  116. * Usage example:
  117. * - initially you'd have:
  118. * struct my_struct
  119. * {
  120. * int a;
  121. * int b;
  122. * };
  123. * - to make a list of it, we replace the declaration above with:
  124. * LIST_TYPE(my_struct,
  125. * int a;
  126. * int b;
  127. * );
  128. * which creates the struct my_struct and struct my_struct_list types.
  129. *
  130. * - setting up an empty list:
  131. * struct my_struct_list l;
  132. * my_struct_list_init(&l);
  133. *
  134. * - allocating an empty list:
  135. * struct my_struct_list * l = my_struct_list_new();
  136. * - add a cell 'e' at the front of list 'l':
  137. * struct my_struct * e = my_struct_new();
  138. * e->a = 0;
  139. * e->b = 0;
  140. * my_struct_list_push_front(l, e);
  141. *
  142. * - iterating over a list from the front:
  143. * struct my_struct * i;
  144. * for(i = my_struct_list_begin(l);
  145. * i != my_struct_list_end(l);
  146. * i = my_struct_list_next(i))
  147. * {
  148. * printf("a=%d; b=%d\n", i->a, i->b);
  149. * }
  150. *
  151. * - iterating over a list from the back:
  152. * struct my_struct * i;
  153. * for(i = my_struct_list_last(l);
  154. * i != my_struct_list_alpha(l);
  155. * i = my_struct_list_prev(i))
  156. * {
  157. * printf("a=%d; b=%d\n", i->a, i->b);
  158. * }
  159. * *********************************************************
  160. */
  161. #ifndef LIST_INLINE
  162. #define LIST_INLINE static inline
  163. #endif
  164. /**@hideinitializer
  165. * Generates a new type for list of elements */
  166. #define LIST_TYPE(ENAME, DECL) \
  167. LIST_CREATE_TYPE(ENAME, DECL)
  168. #define LIST_CREATE_TYPE(ENAME, DECL) \
  169. /** from automatic type: struct ENAME */ \
  170. struct ENAME \
  171. { \
  172. struct ENAME *_prev; /**< @internal previous cell */ \
  173. struct ENAME *_next; /**< @internal next cell */ \
  174. DECL \
  175. }; \
  176. LIST_CREATE_TYPE_NOSTRUCT(ENAME, _prev, _next)
  177. /**@hideinitializer
  178. * The effective type declaration for lists */
  179. #define LIST_CREATE_TYPE_NOSTRUCT(ENAME, _prev, _next) \
  180. /** @internal */ \
  181. /* NOTE: this must not be greater than the struct defined in include/starpu_task_list.h */ \
  182. struct ENAME##_list \
  183. { \
  184. struct ENAME *_head; /**< @internal head of the list */ \
  185. struct ENAME *_tail; /**< @internal tail of the list */ \
  186. }; \
  187. /** @internal */LIST_INLINE struct ENAME *ENAME##_new(void) \
  188. { struct ENAME *e; _STARPU_MALLOC_CAST(e, sizeof(struct ENAME), struct ENAME *); \
  189. e->_next = NULL; e->_prev = NULL; return e; } \
  190. /** @internal */LIST_INLINE void ENAME##_delete(struct ENAME *e) \
  191. { free(e); } \
  192. /** @internal */LIST_INLINE void ENAME##_list_push_front(struct ENAME##_list *l, struct ENAME *e) \
  193. { if(l->_tail == NULL) l->_tail = e; else l->_head->_prev = e; \
  194. e->_prev = NULL; e->_next = l->_head; l->_head = e; } \
  195. /** @internal */LIST_INLINE void ENAME##_list_push_back(struct ENAME##_list *l, struct ENAME *e) \
  196. { if(l->_head == NULL) l->_head = e; else l->_tail->_next = e; \
  197. e->_next = NULL; e->_prev = l->_tail; l->_tail = e; } \
  198. /** @internal */LIST_INLINE void ENAME##_list_insert_before(struct ENAME##_list *l, struct ENAME *e, struct ENAME *o) \
  199. { struct ENAME *p = o->_prev; if (p) { p->_next = e; e->_prev = p; } else { l->_head = e; e->_prev = NULL; } \
  200. e->_next = o; o->_prev = e; } \
  201. /** @internal */LIST_INLINE void ENAME##_list_insert_after(struct ENAME##_list *l, struct ENAME *e, struct ENAME *o) \
  202. { struct ENAME *n = o->_next; if (n) { n->_prev = e; e->_next = n; } else { l->_tail = e; e->_next = NULL; } \
  203. e->_prev = o; o->_next = e; } \
  204. /** @internal */LIST_INLINE void ENAME##_list_push_list_front(struct ENAME##_list *l1, struct ENAME##_list *l2) \
  205. { if (l2->_head == NULL) { l2->_head = l1->_head; l2->_tail = l1->_tail; } \
  206. else if (l1->_head != NULL) { l1->_tail->_next = l2->_head; l2->_head->_prev = l1->_tail; l2->_head = l1->_head; } } \
  207. /** @internal */LIST_INLINE void ENAME##_list_push_list_back(struct ENAME##_list *l1, struct ENAME##_list *l2) \
  208. { if(l1->_head == NULL) { l1->_head = l2->_head; l1->_tail = l2->_tail; } \
  209. else if (l2->_head != NULL) { l1->_tail->_next = l2->_head; l2->_head->_prev = l1->_tail; l1->_tail = l2->_tail; } } \
  210. /** @internal */LIST_INLINE struct ENAME *ENAME##_list_front(const struct ENAME##_list *l) \
  211. { return l->_head; } \
  212. /** @internal */LIST_INLINE struct ENAME *ENAME##_list_back(const struct ENAME##_list *l) \
  213. { return l->_tail; } \
  214. /** @internal */LIST_INLINE void ENAME##_list_init(struct ENAME##_list *l) \
  215. { l->_head=NULL; l->_tail=l->_head; } \
  216. /** @internal */LIST_INLINE struct ENAME##_list *ENAME##_list_new(void) \
  217. { struct ENAME##_list *l; _STARPU_MALLOC_CAST(l, sizeof(struct ENAME##_list), struct ENAME##_list *); \
  218. ENAME##_list_init(l); return l; } \
  219. /** @internal */LIST_INLINE int ENAME##_list_empty(const struct ENAME##_list *l) \
  220. { return (l->_head == NULL); } \
  221. /** @internal */LIST_INLINE void ENAME##_list_delete(struct ENAME##_list *l) \
  222. { free(l); } \
  223. /** @internal */LIST_INLINE void ENAME##_list_erase(struct ENAME##_list *l, struct ENAME *c) \
  224. { struct ENAME *p = c->_prev; if(p) p->_next = c->_next; else l->_head = c->_next; \
  225. if(c->_next) c->_next->_prev = p; else l->_tail = p; } \
  226. /** @internal */LIST_INLINE struct ENAME *ENAME##_list_pop_front(struct ENAME##_list *l) \
  227. { struct ENAME *e = ENAME##_list_front(l); \
  228. ENAME##_list_erase(l, e); return e; } \
  229. /** @internal */LIST_INLINE struct ENAME *ENAME##_list_pop_back(struct ENAME##_list *l) \
  230. { struct ENAME *e = ENAME##_list_back(l); \
  231. ENAME##_list_erase(l, e); return e; } \
  232. /** @internal */LIST_INLINE struct ENAME *ENAME##_list_begin(const struct ENAME##_list *l) \
  233. { return l->_head; } \
  234. /** @internal */LIST_INLINE struct ENAME *ENAME##_list_end(const struct ENAME##_list *l STARPU_ATTRIBUTE_UNUSED) \
  235. { return NULL; } \
  236. /** @internal */LIST_INLINE struct ENAME *ENAME##_list_next(const struct ENAME *i) \
  237. { return i->_next; } \
  238. /** @internal */LIST_INLINE struct ENAME *ENAME##_list_last(const struct ENAME##_list *l) \
  239. { return l->_tail; } \
  240. /** @internal */LIST_INLINE struct ENAME *ENAME##_list_alpha(const struct ENAME##_list *l STARPU_ATTRIBUTE_UNUSED) \
  241. { return NULL; } \
  242. /** @internal */LIST_INLINE struct ENAME *ENAME##_list_prev(const struct ENAME *i) \
  243. { return i->_prev; } \
  244. /** @internal */LIST_INLINE int ENAME##_list_ismember(const struct ENAME##_list *l, const struct ENAME *e) \
  245. { struct ENAME *i=l->_head; while(i!=NULL){ if (i == e) return 1; i=i->_next; } return 0; } \
  246. /** @internal */LIST_INLINE int ENAME##_list_member(const struct ENAME##_list *l, const struct ENAME *e) \
  247. { struct ENAME *i=l->_head; int k=0; while(i!=NULL){if (i == e) return k; k++; i=i->_next; } return -1; } \
  248. /** @internal */LIST_INLINE int ENAME##_list_size(const struct ENAME##_list *l) \
  249. { struct ENAME *i=l->_head; int k=0; while(i!=NULL){k++;i=i->_next;} return k; } \
  250. /** @internal */LIST_INLINE int ENAME##_list_check(const struct ENAME##_list *l) \
  251. { struct ENAME *i=l->_head; while(i) \
  252. { if ((i->_next == NULL) && i != l->_tail) return 0; \
  253. if (i->_next == i) return 0; \
  254. i=i->_next;} return 1; }
  255. #ifdef STARPU_DEBUG
  256. #define STARPU_ASSERT_MULTILIST(expr) STARPU_ASSERT(expr)
  257. #else
  258. #define STARPU_ASSERT_MULTILIST(expr) ((void) 0)
  259. #endif
  260. /*
  261. * This is an implementation of list allowing to be member of several lists.
  262. * - One should first call MULTILIST_CREATE_TYPE for the ENAME and for each
  263. * MEMBER type
  264. * - Then the main element type should include fields of type
  265. * ENAME_multilist_MEMBER
  266. * - Then one should call MULTILIST_CREATE_INLINES to create the inlines which
  267. * manipulate lists for this MEMBER type.
  268. */
  269. /* Create the ENAME_multilist_MEMBER, to be used both as head and as member of main element type */
  270. #define MULTILIST_CREATE_TYPE(ENAME, MEMBER) \
  271. struct ENAME##_multilist_##MEMBER { \
  272. struct ENAME##_multilist_##MEMBER *next; \
  273. struct ENAME##_multilist_##MEMBER *prev; \
  274. };
  275. /* Create the inlines */
  276. #define MULTILIST_CREATE_INLINES(TYPE, ENAME, MEMBER) \
  277. /* Cast from list element to real type. */ \
  278. LIST_INLINE TYPE *ENAME##_of_multilist_##MEMBER(struct ENAME##_multilist_##MEMBER *elt) { \
  279. return ((TYPE *) ((uintptr_t) (elt) - ((uintptr_t) (&((TYPE *) 0)->MEMBER)))); \
  280. } \
  281. \
  282. /* Initialize a list head. */ \
  283. LIST_INLINE void ENAME##_multilist_init_##MEMBER(struct ENAME##_multilist_##MEMBER *head) { \
  284. head->next = head; \
  285. head->prev = head; \
  286. } \
  287. \
  288. /* Push element to head of a list. */ \
  289. LIST_INLINE void ENAME##_multilist_push_front_##MEMBER(struct ENAME##_multilist_##MEMBER *head, TYPE *e) { \
  290. STARPU_ASSERT_MULTILIST(e->MEMBER.prev == NULL); \
  291. STARPU_ASSERT_MULTILIST(e->MEMBER.next == NULL); \
  292. e->MEMBER.next = head->next; \
  293. e->MEMBER.prev = head; \
  294. head->next->prev = &e->MEMBER; \
  295. head->next = &e->MEMBER; \
  296. } \
  297. \
  298. /* Push element to tail of a list. */ \
  299. LIST_INLINE void ENAME##_multilist_push_back_##MEMBER(struct ENAME##_multilist_##MEMBER *head, TYPE *e) { \
  300. STARPU_ASSERT_MULTILIST(e->MEMBER.prev == NULL); \
  301. STARPU_ASSERT_MULTILIST(e->MEMBER.next == NULL); \
  302. e->MEMBER.prev = head->prev; \
  303. e->MEMBER.next = head; \
  304. head->prev->next = &e->MEMBER; \
  305. head->prev = &e->MEMBER; \
  306. } \
  307. \
  308. /* Erase element from a list. */ \
  309. LIST_INLINE void ENAME##_multilist_erase_##MEMBER(struct ENAME##_multilist_##MEMBER *head STARPU_ATTRIBUTE_UNUSED, TYPE *e) { \
  310. STARPU_ASSERT_MULTILIST(e->MEMBER.next->prev == &e->MEMBER); \
  311. e->MEMBER.next->prev = e->MEMBER.prev; \
  312. STARPU_ASSERT_MULTILIST(e->MEMBER.prev->next == &e->MEMBER); \
  313. e->MEMBER.prev->next = e->MEMBER.next; \
  314. e->MEMBER.next = NULL; \
  315. e->MEMBER.prev = NULL; \
  316. } \
  317. \
  318. /* Test whether the element was queued on the list. */ \
  319. LIST_INLINE int ENAME##_multilist_queued_##MEMBER(TYPE *e) { \
  320. return ((e)->MEMBER.next != NULL); \
  321. } \
  322. \
  323. /* Test whether the list is empty. */ \
  324. LIST_INLINE int ENAME##_multilist_empty_##MEMBER(struct ENAME##_multilist_##MEMBER *head) { \
  325. return head->next == head; \
  326. } \
  327. \
  328. /* Return the first element of the list. */ \
  329. LIST_INLINE TYPE *ENAME##_multilist_begin_##MEMBER(struct ENAME##_multilist_##MEMBER *head) { \
  330. return ENAME##_of_multilist_##MEMBER(head->next); \
  331. } \
  332. /* Return the value to be tested at the end of the list. */ \
  333. LIST_INLINE TYPE *ENAME##_multilist_end_##MEMBER(struct ENAME##_multilist_##MEMBER *head) { \
  334. return ENAME##_of_multilist_##MEMBER(head); \
  335. } \
  336. /* Return the next element of the list. */ \
  337. LIST_INLINE TYPE *ENAME##_multilist_next_##MEMBER(TYPE *e) { \
  338. return ENAME##_of_multilist_##MEMBER(e->MEMBER.next); \
  339. } \
  340. \
  341. /* Move a list from its head to another head. */ \
  342. LIST_INLINE void ENAME##_multilist_move_##MEMBER(struct ENAME##_multilist_##MEMBER *head, struct ENAME##_multilist_##MEMBER *newhead) { \
  343. if (ENAME##_multilist_empty_##MEMBER(head)) \
  344. ENAME##_multilist_init_##MEMBER(newhead); \
  345. else { \
  346. newhead->next = head->next; \
  347. newhead->next->prev = newhead; \
  348. newhead->prev = head->prev; \
  349. newhead->prev->next = newhead; \
  350. head->next = head; \
  351. head->prev = head; \
  352. } \
  353. }
  354. #endif /* __LIST_H__ */