list.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 Listes doublement chainées automatiques
  21. */
  22. /** @remarks list how-to
  23. * *********************************************************
  24. * LIST_TYPE(FOO, contenu);
  25. * - déclare les types suivants
  26. * + pour les cellules : struct FOO
  27. * + pour les listes : struct FOO_list
  28. * + pour les itérateurs : struct FOO
  29. * - déclare les accesseurs suivants :
  30. * * création d'une cellule
  31. * struct FOO* FOO_new(void);
  32. * * suppression d'une cellule
  33. * void FOO_delete(struct FOO*);
  34. * * création d'une liste (vide)
  35. * struct FOO_list* FOO_list_new(void);
  36. * * initialisation d'une liste (vide)
  37. * void FOO_list_init(struct FOO_list*);
  38. * * suppression d'une liste
  39. * void FOO_list_delete(struct FOO_list*);
  40. * * teste si une liste est vide
  41. * int FOO_list_empty(struct FOO_list*);
  42. * * retire un élément de la liste
  43. * void FOO_list_erase(struct FOO_list*, struct FOO*);
  44. * * ajoute une élément en queue de liste
  45. * void FOO_list_push_back(struct FOO_list*, struct FOO*);
  46. * * ajoute un élément en tête de liste
  47. * void FOO_list_push_front(struct FOO_list*, struct FOO*);
  48. * * ajoute un élément avant un élément
  49. * void FOO_list_insert_before(struct FOO_list*, struct FOO*new, struct FOO*);
  50. * * ajoute un élément après un élément
  51. * void FOO_list_insert_after(struct FOO_list*, struct FOO*new, struct FOO*);
  52. * * ajoute la deuxième liste à la fin de la première liste
  53. * struct FOO* FOO_list_push_list_back(struct FOO_list*, struct FOO_list*);
  54. * * ajoute la première liste au début de la deuxième liste
  55. * struct FOO* FOO_list_push_list_front(struct FOO_list*, struct FOO_list*);
  56. * * retire l'élément en queue de liste
  57. * struct FOO* FOO_list_pop_back(struct FOO_list*);
  58. * * retire l'élement en tête de liste
  59. * struct FOO* FOO_list_pop_front(struct FOO_list*);
  60. * * retourne l'élément en queue de liste
  61. * struct FOO* FOO_list_back(struct FOO_list*);
  62. * * retourne l'élement en tête de liste
  63. * struct FOO* FOO_list_front(struct FOO_list*);
  64. * * vérifie si la liste chainée est cohérente
  65. * int FOO_list_check(struct FOO_list*);
  66. * * retourne le premier élément de la liste
  67. * struct FOO* FOO_list_begin(struct FOO_list*);
  68. * * retourne la valeur à tester en fin de liste
  69. * struct FOO* FOO_list_end(struct FOO_list*);
  70. * * retourne l'élément suivant de la liste
  71. * struct FOO* FOO_list_next(struct FOO*)
  72. * * retourne le dernier élément de la liste
  73. * struct FOO* FOO_list_last(struct FOO_list*);
  74. * * retourne la valeur à tester en début de liste
  75. * struct FOO* FOO_list_alpha(struct FOO_list*);
  76. * * retourne l'élément précédent de la liste
  77. * struct FOO* FOO_list_prev(struct FOO*)
  78. * * retourne la taille de la liste
  79. * int FOO_list_size(struct FOO_list*)
  80. * * retourne la position de l'élément dans la liste (indexé à partir de 0)
  81. * int FOO_list_member(struct FOO_list*, struct FOO*)
  82. * * teste si l'élément est dans la liste
  83. * int FOO_list_ismember(struct FOO_list*, struct FOO*)
  84. * *********************************************************
  85. * Exemples d'utilisation :
  86. * - au départ, on a :
  87. * struct ma_structure_s
  88. * {
  89. * int a;
  90. * int b;
  91. * };
  92. * - on veut en faire une liste. On remplace la déclaration par :
  93. * LIST_TYPE(ma_structure,
  94. * int a;
  95. * int b;
  96. * );
  97. * qui crée les types struct ma_structure et struct ma_structure_list.
  98. * - allocation d'une liste vide :
  99. * struct ma_structure_list * l = ma_structure_list_new();
  100. * - ajouter un élément 'e' en tête de la liste 'l' :
  101. * struct ma_structure * e = ma_structure_new();
  102. * e->a = 0;
  103. * e->b = 0;
  104. * ma_structure_list_push_front(l, e);
  105. * - itérateur de liste :
  106. * struct ma_structure * i;
  107. * for(i = ma_structure_list_begin(l);
  108. * i != ma_structure_list_end(l);
  109. * i = ma_structure_list_next(i))
  110. * {
  111. * printf("a=%d; b=%d\n", i->a, i->b);
  112. * }
  113. * - itérateur de liste :
  114. * struct ma_structure * i;
  115. * for(i = ma_structure_list_last(l);
  116. * i != ma_structure_list_alpha(l);
  117. * i = ma_structure_list_prev(i))
  118. * {
  119. * printf("a=%d; b=%d\n", i->a, i->b);
  120. * }
  121. * *********************************************************
  122. */
  123. /**@hideinitializer
  124. * Generates a new type for list of elements */
  125. #define LIST_TYPE(ENAME, DECL) \
  126. LIST_CREATE_TYPE(ENAME, DECL)
  127. #define LIST_CREATE_TYPE(ENAME, DECL) \
  128. /** from automatic type: struct ENAME */ \
  129. struct ENAME \
  130. { \
  131. struct ENAME *_prev; /**< @internal previous cell */ \
  132. struct ENAME *_next; /**< @internal next cell */ \
  133. DECL \
  134. }; \
  135. LIST_CREATE_TYPE_NOSTRUCT(ENAME, _prev, _next)
  136. /**@hideinitializer
  137. * The effective type declaration for lists */
  138. #define LIST_CREATE_TYPE_NOSTRUCT(ENAME, _prev, _next) \
  139. /** @internal */ \
  140. struct ENAME##_list \
  141. { \
  142. struct ENAME *_head; /**< @internal head of the list */ \
  143. struct ENAME *_tail; /**< @internal tail of the list */ \
  144. }; \
  145. /** @internal */static inline struct ENAME *ENAME##_new(void) \
  146. { struct ENAME *e; _STARPU_MALLOC(e, sizeof(struct ENAME)); \
  147. e->_next = NULL; e->_prev = NULL; return e; } \
  148. /** @internal */static inline void ENAME##_delete(struct ENAME *e) \
  149. { free(e); } \
  150. /** @internal */static inline void ENAME##_list_push_front(struct ENAME##_list *l, struct ENAME *e) \
  151. { if(l->_tail == NULL) l->_tail = e; else l->_head->_prev = e; \
  152. e->_prev = NULL; e->_next = l->_head; l->_head = e; } \
  153. /** @internal */static inline void ENAME##_list_push_back(struct ENAME##_list *l, struct ENAME *e) \
  154. { if(l->_head == NULL) l->_head = e; else l->_tail->_next = e; \
  155. e->_next = NULL; e->_prev = l->_tail; l->_tail = e; } \
  156. /** @internal */static inline void ENAME##_list_insert_before(struct ENAME##_list *l, struct ENAME *e, struct ENAME *o) \
  157. { struct ENAME *p = o->_prev; if (p) { p->_next = e; e->_prev = p; } else { l->_head = e; e->_prev = NULL; } \
  158. e->_next = o; o->_prev = e; } \
  159. /** @internal */static inline void ENAME##_list_insert_after(struct ENAME##_list *l, struct ENAME *e, struct ENAME *o) \
  160. { struct ENAME *n = o->_next; if (n) { n->_prev = e; e->_next = n; } else { l->_tail = e; e->_next = NULL; } \
  161. e->_prev = o; o->_next = e; } \
  162. /** @internal */static inline void ENAME##_list_push_list_front(struct ENAME##_list *l1, struct ENAME##_list *l2) \
  163. { if (l2->_head == NULL) { l2->_head = l1->_head; l2->_tail = l1->_tail; } \
  164. else if (l1->_head != NULL) { l1->_tail->_next = l2->_head; l2->_head->_prev = l1->_tail; l2->_head = l1->_head; } } \
  165. /** @internal */static inline void ENAME##_list_push_list_back(struct ENAME##_list *l1, struct ENAME##_list *l2) \
  166. { if(l1->_head == NULL) { l1->_head = l2->_head; l1->_tail = l2->_tail; } \
  167. else if (l2->_head != NULL) { l1->_tail->_next = l2->_head; l2->_head->_prev = l1->_tail; l1->_tail = l2->_tail; } } \
  168. /** @internal */static inline struct ENAME *ENAME##_list_front(const struct ENAME##_list *l) \
  169. { return l->_head; } \
  170. /** @internal */static inline struct ENAME *ENAME##_list_back(const struct ENAME##_list *l) \
  171. { return l->_tail; } \
  172. /** @internal */static inline void ENAME##_list_init(struct ENAME##_list *l) \
  173. { l->_head=NULL; l->_tail=l->_head; } \
  174. /** @internal */static inline struct ENAME##_list *ENAME##_list_new(void) \
  175. { struct ENAME##_list *l; _STARPU_MALLOC(l, sizeof(struct ENAME##_list)); \
  176. ENAME##_list_init(l); return l; } \
  177. /** @internal */static inline int ENAME##_list_empty(const struct ENAME##_list *l) \
  178. { return (l->_head == NULL); } \
  179. /** @internal */static inline void ENAME##_list_delete(struct ENAME##_list *l) \
  180. { free(l); } \
  181. /** @internal */static inline void ENAME##_list_erase(struct ENAME##_list *l, struct ENAME *c) \
  182. { struct ENAME *p = c->_prev; if(p) p->_next = c->_next; else l->_head = c->_next; \
  183. if(c->_next) c->_next->_prev = p; else l->_tail = p; } \
  184. /** @internal */static inline struct ENAME *ENAME##_list_pop_front(struct ENAME##_list *l) \
  185. { struct ENAME *e = ENAME##_list_front(l); \
  186. ENAME##_list_erase(l, e); return e; } \
  187. /** @internal */static inline struct ENAME *ENAME##_list_pop_back(struct ENAME##_list *l) \
  188. { struct ENAME *e = ENAME##_list_back(l); \
  189. ENAME##_list_erase(l, e); return e; } \
  190. /** @internal */static inline struct ENAME *ENAME##_list_begin(const struct ENAME##_list *l) \
  191. { return l->_head; } \
  192. /** @internal */static inline struct ENAME *ENAME##_list_end(const struct ENAME##_list *l STARPU_ATTRIBUTE_UNUSED) \
  193. { return NULL; } \
  194. /** @internal */static inline struct ENAME *ENAME##_list_next(const struct ENAME *i) \
  195. { return i->_next; } \
  196. /** @internal */static inline struct ENAME *ENAME##_list_last(const struct ENAME##_list *l) \
  197. { return l->_tail; } \
  198. /** @internal */static inline struct ENAME *ENAME##_list_alpha(const struct ENAME##_list *l STARPU_ATTRIBUTE_UNUSED) \
  199. { return NULL; } \
  200. /** @internal */static inline struct ENAME *ENAME##_list_prev(const struct ENAME *i) \
  201. { return i->_prev; } \
  202. /** @internal */static inline int ENAME##_list_ismember(const struct ENAME##_list *l, const struct ENAME *e) \
  203. { struct ENAME *i=l->_head; while(i!=NULL){ if (i == e) return 1; i=i->_next; } return 0; } \
  204. /** @internal */static inline int ENAME##_list_member(const struct ENAME##_list *l, const struct ENAME *e) \
  205. { struct ENAME *i=l->_head; int k=0; while(i!=NULL){if (i == e) return k; k++; i=i->_next; } return -1; } \
  206. /** @internal */static inline int ENAME##_list_size(const struct ENAME##_list *l) \
  207. { struct ENAME *i=l->_head; int k=0; while(i!=NULL){k++;i=i->_next;} return k; } \
  208. /** @internal */static inline int ENAME##_list_check(const struct ENAME##_list *l) \
  209. { struct ENAME *i=l->_head; while(i) \
  210. { if ((i->_next == NULL) && i != l->_tail) return 0; \
  211. if (i->_next == i) return 0; \
  212. i=i->_next;} return 1; }
  213. #ifdef STARPU_DEBUG
  214. #define STARPU_ASSERT_MULTILIST(expr) STARPU_ASSERT(expr)
  215. #else
  216. #define STARPU_ASSERT_MULTILIST(expr) ((void) 0)
  217. #endif
  218. /*
  219. * This is an implementation of list allowing to be member of several lists.
  220. * - One should first call MULTILIST_CREATE_TYPE for the ENAME and for each
  221. * MEMBER type
  222. * - Then the main element type should include fields of type
  223. * ENAME_multilist_MEMBER
  224. * - Then one should call MULTILIST_CREATE_INLINES to create the inlines which
  225. * manipulate lists for this MEMBER type.
  226. */
  227. /* Create the ENAME_multilist_MEMBER, to be used both as head and as member of main element type */
  228. #define MULTILIST_CREATE_TYPE(ENAME, MEMBER) \
  229. struct ENAME##_multilist_##MEMBER { \
  230. struct ENAME##_multilist_##MEMBER *next; \
  231. struct ENAME##_multilist_##MEMBER *prev; \
  232. };
  233. /* Create the inlines */
  234. #define MULTILIST_CREATE_INLINES(TYPE, ENAME, MEMBER) \
  235. /* Cast from list element to real type. */ \
  236. static inline TYPE *ENAME##_of_multilist_##MEMBER(struct ENAME##_multilist_##MEMBER *elt) { \
  237. return ((TYPE *) ((uintptr_t) (elt) - ((uintptr_t) (&((TYPE *) 0)->MEMBER)))); \
  238. } \
  239. \
  240. /* Initialize a list head. */ \
  241. static inline void ENAME##_multilist_init_##MEMBER(struct ENAME##_multilist_##MEMBER *head) { \
  242. head->next = head; \
  243. head->prev = head; \
  244. } \
  245. \
  246. /* Push element to head of a list. */ \
  247. static inline void ENAME##_multilist_push_front_##MEMBER(struct ENAME##_multilist_##MEMBER *head, TYPE *e) { \
  248. STARPU_ASSERT_MULTILIST(e->MEMBER.prev == NULL); \
  249. STARPU_ASSERT_MULTILIST(e->MEMBER.next == NULL); \
  250. e->MEMBER.next = head->next; \
  251. e->MEMBER.prev = head; \
  252. head->next->prev = &e->MEMBER; \
  253. head->next = &e->MEMBER; \
  254. } \
  255. \
  256. /* Push element to tail of a list. */ \
  257. static inline void ENAME##_multilist_push_back_##MEMBER(struct ENAME##_multilist_##MEMBER *head, TYPE *e) { \
  258. STARPU_ASSERT_MULTILIST(e->MEMBER.prev == NULL); \
  259. STARPU_ASSERT_MULTILIST(e->MEMBER.next == NULL); \
  260. e->MEMBER.prev = head->prev; \
  261. e->MEMBER.next = head; \
  262. head->prev->next = &e->MEMBER; \
  263. head->prev = &e->MEMBER; \
  264. } \
  265. \
  266. /* Erase element from a list. */ \
  267. static inline void ENAME##_multilist_erase_##MEMBER(struct ENAME##_multilist_##MEMBER *head STARPU_ATTRIBUTE_UNUSED, TYPE *e) { \
  268. STARPU_ASSERT_MULTILIST(e->MEMBER.next->prev == &e->MEMBER); \
  269. e->MEMBER.next->prev = e->MEMBER.prev; \
  270. STARPU_ASSERT_MULTILIST(e->MEMBER.prev->next == &e->MEMBER); \
  271. e->MEMBER.prev->next = e->MEMBER.next; \
  272. e->MEMBER.next = NULL; \
  273. e->MEMBER.prev = NULL; \
  274. } \
  275. \
  276. /* Test whether the element was queued on the list. */ \
  277. static inline int ENAME##_multilist_queued_##MEMBER(TYPE *e) { \
  278. return ((e)->MEMBER.next != NULL); \
  279. } \
  280. \
  281. /* Test whether the list is empty. */ \
  282. static inline int ENAME##_multilist_empty_##MEMBER(struct ENAME##_multilist_##MEMBER *head) { \
  283. return head->next == head; \
  284. } \
  285. \
  286. /* Return the first element of the list. */ \
  287. static inline TYPE *ENAME##_multilist_begin_##MEMBER(struct ENAME##_multilist_##MEMBER *head) { \
  288. return ENAME##_of_multilist_##MEMBER(head->next); \
  289. } \
  290. /* Return the value to be tested at the end of the list. */ \
  291. static inline TYPE *ENAME##_multilist_end_##MEMBER(struct ENAME##_multilist_##MEMBER *head) { \
  292. return ENAME##_of_multilist_##MEMBER(head); \
  293. } \
  294. /* Return the next element of the list. */ \
  295. static inline TYPE *ENAME##_multilist_next_##MEMBER(TYPE *e) { \
  296. return ENAME##_of_multilist_##MEMBER(e->MEMBER.next); \
  297. } \
  298. \
  299. /* Move a list from its head to another head. */ \
  300. static inline void ENAME##_multilist_move_##MEMBER(struct ENAME##_multilist_##MEMBER *head, struct ENAME##_multilist_##MEMBER *newhead) { \
  301. if (ENAME##_multilist_empty_##MEMBER(head)) \
  302. ENAME##_multilist_init_##MEMBER(newhead); \
  303. else { \
  304. newhead->next = head->next; \
  305. newhead->next->prev = newhead; \
  306. newhead->prev = head->prev; \
  307. newhead->prev->next = newhead; \
  308. head->next = head; \
  309. head->prev = head; \
  310. } \
  311. }
  312. #endif /* __LIST_H__ */