list.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /* StarPU --- Runtime system for heterogeneous multicore architectures.
  2. *
  3. * Copyright (C) 2008-2020 Université de Bordeaux, CNRS (LaBRI UMR 5800), Inria
  4. * Copyright (C) 2013 Thibaut Lambert
  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. #include <starpu_util.h>
  20. /** @remarks list how-to
  21. * *********************************************************
  22. * LIST_TYPE(FOO, content);
  23. *
  24. * - declares the following types:
  25. *
  26. * + for cells : struct FOO
  27. * + for lists : struct FOO_list
  28. * + for iterators : struct FOO
  29. *
  30. * - declares the following inlines (all O(1) except stated otherwise, n is the number of elements) :
  31. *
  32. * * Create a cell
  33. * struct FOO* FOO_new(void);
  34. *
  35. * * Suppress a cell
  36. * void FOO_delete(struct FOO*);
  37. *
  38. * * Create a list (initially empty)
  39. * struct FOO_list* FOO_list_new(void);
  40. *
  41. * * Initializes a list (initially empty)
  42. * void FOO_list_init(struct FOO_list*);
  43. *
  44. * * Initializes a list (initially empty), assuming that the content of FOO_list was already zeroed
  45. * void FOO_list_init0(struct FOO_list*);
  46. *
  47. * * Suppresses a liste
  48. * void FOO_list_delete(struct FOO_list*);
  49. *
  50. * * Check whether a list is empty
  51. * int FOO_list_empty(struct FOO_list*);
  52. *
  53. * * Remove a given cell from the list
  54. * void FOO_list_erase(struct FOO_list*, struct FOO*);
  55. *
  56. * * Add a cell at the back of the list
  57. * void FOO_list_push_back(struct FOO_list*, struct FOO*);
  58. *
  59. * * Add a cell at the front of the list
  60. * void FOO_list_push_front(struct FOO_list*, struct FOO*);
  61. *
  62. * * Add a cell before a given cell of a list
  63. * void FOO_list_insert_before(struct FOO_list*, struct FOO*new, struct FOO*);
  64. *
  65. * * Add a cell after a given cell of a list
  66. * void FOO_list_insert_after(struct FOO_list*, struct FOO*new, struct FOO*);
  67. *
  68. * * Append the second list at the end of the first list
  69. * struct FOO* FOO_list_push_list_back(struct FOO_list*, struct FOO_list*);
  70. *
  71. * * Prepend the first list at the beginning of the second list
  72. * struct FOO* FOO_list_push_list_front(struct FOO_list*, struct FOO_list*);
  73. *
  74. * * Return and remove the node at the back of the list
  75. * struct FOO* FOO_list_pop_back(struct FOO_list*);
  76. *
  77. * * Return and remove the node at the front of the list
  78. * struct FOO* FOO_list_pop_front(struct FOO_list*);
  79. *
  80. * * Return the node at the back of the list
  81. * struct FOO* FOO_list_back(struct FOO_list*);
  82. *
  83. * * Return the node at the front of the list
  84. * struct FOO* FOO_list_front(struct FOO_list*);
  85. *
  86. * * Check that the list chaining is coherent (O(n))
  87. * int FOO_list_check(struct FOO_list*);
  88. *
  89. * * Return the first cell of the list (from the front)
  90. * struct FOO* FOO_list_begin(struct FOO_list*);
  91. *
  92. * * Return the value to be tested at the end of the list (at the back)
  93. * struct FOO* FOO_list_end(struct FOO_list*);
  94. *
  95. * * Return the next element of the list (from the front)
  96. * struct FOO* FOO_list_next(struct FOO*)
  97. *
  98. * * Return the last element of the list (from the back)
  99. * struct FOO* FOO_list_last(struct FOO_list*);
  100. *
  101. * * Return the value to be tested at the beginning of the list (at the fromt)
  102. * struct FOO* FOO_list_alpha(struct FOO_list*);
  103. *
  104. * * Return the previous element of the list (from the back)
  105. * struct FOO* FOO_list_prev(struct FOO*)
  106. *
  107. * * Return the size of the list in O(n)
  108. * int FOO_list_size(struct FOO_list*)
  109. *
  110. * * Return the position of the cell in the list (indexed from 0) (O(n) on average)
  111. * int FOO_list_member(struct FOO_list*, struct FOO*)
  112. *
  113. * * Test whether the cell is in the list (O(n) on average)
  114. * int FOO_list_ismember(struct FOO_list*, struct FOO*)
  115. *
  116. * *********************************************************
  117. * Usage example:
  118. * - initially you'd have:
  119. * struct my_struct
  120. * {
  121. * int a;
  122. * int b;
  123. * };
  124. * - to make a list of it, we replace the declaration above with:
  125. * LIST_TYPE(my_struct,
  126. * int a;
  127. * int b;
  128. * );
  129. * which creates the struct my_struct and struct my_struct_list types.
  130. *
  131. * - setting up an empty list:
  132. * struct my_struct_list l;
  133. * my_struct_list_init(&l);
  134. *
  135. * - allocating an empty list:
  136. * struct my_struct_list * l = my_struct_list_new();
  137. * - add a cell 'e' at the front of list 'l':
  138. * struct my_struct * e = my_struct_new();
  139. * e->a = 0;
  140. * e->b = 0;
  141. * my_struct_list_push_front(l, e);
  142. *
  143. * - iterating over a list from the front:
  144. * struct my_struct * i;
  145. * for(i = my_struct_list_begin(l);
  146. * i != my_struct_list_end(l);
  147. * i = my_struct_list_next(i))
  148. * {
  149. * printf("a=%d; b=%d\n", i->a, i->b);
  150. * }
  151. *
  152. * - iterating over a list from the back:
  153. * struct my_struct * i;
  154. * for(i = my_struct_list_last(l);
  155. * i != my_struct_list_alpha(l);
  156. * i = my_struct_list_prev(i))
  157. * {
  158. * printf("a=%d; b=%d\n", i->a, i->b);
  159. * }
  160. * *********************************************************
  161. */
  162. #ifndef LIST_INLINE
  163. #define LIST_INLINE static inline
  164. #endif
  165. /**@hideinitializer
  166. * Generates a new type for list of elements */
  167. #define LIST_TYPE(ENAME, DECL) \
  168. LIST_CREATE_TYPE(ENAME, DECL)
  169. #define LIST_CREATE_TYPE(ENAME, DECL) \
  170. /** from automatic type: struct ENAME */ \
  171. struct ENAME \
  172. { \
  173. struct ENAME *_prev; /**< @internal previous cell */ \
  174. struct ENAME *_next; /**< @internal next cell */ \
  175. DECL \
  176. }; \
  177. LIST_CREATE_TYPE_NOSTRUCT(ENAME, _prev, _next)
  178. /**@hideinitializer
  179. * The effective type declaration for lists */
  180. #define LIST_CREATE_TYPE_NOSTRUCT(ENAME, _prev, _next) \
  181. /** @internal */ \
  182. /* NOTE: this must not be greater than the struct defined in include/starpu_task_list.h */ \
  183. struct ENAME##_list \
  184. { \
  185. struct ENAME *_head; /**< @internal head of the list */ \
  186. struct ENAME *_tail; /**< @internal tail of the list */ \
  187. }; \
  188. /** @internal */LIST_INLINE struct ENAME *ENAME##_new(void) \
  189. { struct ENAME *e; _STARPU_MALLOC(e, sizeof(struct ENAME)); \
  190. e->_next = NULL; e->_prev = NULL; return e; } \
  191. /** @internal */LIST_INLINE void ENAME##_delete(struct ENAME *e) \
  192. { free(e); } \
  193. /** @internal */LIST_INLINE void ENAME##_list_push_front(struct ENAME##_list *l, struct ENAME *e) \
  194. { if(l->_tail == NULL) l->_tail = e; else l->_head->_prev = e; \
  195. e->_prev = NULL; e->_next = l->_head; l->_head = e; } \
  196. /** @internal */LIST_INLINE void ENAME##_list_push_back(struct ENAME##_list *l, struct ENAME *e) \
  197. { if(l->_head == NULL) l->_head = e; else l->_tail->_next = e; \
  198. e->_next = NULL; e->_prev = l->_tail; l->_tail = e; } \
  199. /** @internal */LIST_INLINE void ENAME##_list_insert_before(struct ENAME##_list *l, struct ENAME *e, struct ENAME *o) \
  200. { struct ENAME *p = o->_prev; if (p) { p->_next = e; e->_prev = p; } else { l->_head = e; e->_prev = NULL; } \
  201. e->_next = o; o->_prev = e; } \
  202. /** @internal */LIST_INLINE void ENAME##_list_insert_after(struct ENAME##_list *l, struct ENAME *e, struct ENAME *o) \
  203. { struct ENAME *n = o->_next; if (n) { n->_prev = e; e->_next = n; } else { l->_tail = e; e->_next = NULL; } \
  204. e->_prev = o; o->_next = e; } \
  205. /** @internal */LIST_INLINE void ENAME##_list_push_list_front(struct ENAME##_list *l1, struct ENAME##_list *l2) \
  206. { if (l2->_head == NULL) { l2->_head = l1->_head; l2->_tail = l1->_tail; } \
  207. else if (l1->_head != NULL) { l1->_tail->_next = l2->_head; l2->_head->_prev = l1->_tail; l2->_head = l1->_head; } } \
  208. /** @internal */LIST_INLINE void ENAME##_list_push_list_back(struct ENAME##_list *l1, struct ENAME##_list *l2) \
  209. { if(l1->_head == NULL) { l1->_head = l2->_head; l1->_tail = l2->_tail; } \
  210. else if (l2->_head != NULL) { l1->_tail->_next = l2->_head; l2->_head->_prev = l1->_tail; l1->_tail = l2->_tail; } } \
  211. /** @internal */LIST_INLINE struct ENAME *ENAME##_list_front(const struct ENAME##_list *l) \
  212. { return l->_head; } \
  213. /** @internal */LIST_INLINE struct ENAME *ENAME##_list_back(const struct ENAME##_list *l) \
  214. { return l->_tail; } \
  215. /** @internal */LIST_INLINE void ENAME##_list_init(struct ENAME##_list *l) \
  216. { l->_head=NULL; l->_tail=NULL; } \
  217. /** @internal */LIST_INLINE void ENAME##_list_init0(struct ENAME##_list *l STARPU_ATTRIBUTE_UNUSED) \
  218. { } \
  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__ */