list.h 16 KB

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