StarPU Internal Handbook
list.h
Go to the documentation of this file.
1 /* StarPU --- Runtime system for heterogeneous multicore architectures.
2  *
3  * Copyright (C) 2008-2021 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 
18 #ifndef __LIST_H__
19 #define __LIST_H__
20 
23 #include <starpu_util.h>
24 
166 #ifndef LIST_INLINE
167 #define LIST_INLINE static inline
168 #endif
169 
172 #define LIST_TYPE(ENAME, DECL) \
173  LIST_CREATE_TYPE(ENAME, DECL)
174 
175 #define LIST_CREATE_TYPE(ENAME, DECL) \
176  \
177  struct ENAME \
178  { \
179  struct ENAME *_prev; \
180  struct ENAME *_next; \
181  DECL \
182  }; \
183  LIST_CREATE_TYPE_NOSTRUCT(ENAME, _prev, _next)
184 
187 #define LIST_CREATE_TYPE_NOSTRUCT(ENAME, _prev, _next) \
188  \
189  /* NOTE: this must not be greater than the struct defined in include/starpu_task_list.h */ \
190  struct ENAME##_list \
191  { \
192  struct ENAME *_head; \
193  struct ENAME *_tail; \
194  }; \
195 LIST_INLINE struct ENAME *ENAME##_new(void) \
196  { struct ENAME *e; _STARPU_MALLOC(e, sizeof(struct ENAME)); \
197  e->_next = NULL; e->_prev = NULL; return e; } \
198 LIST_INLINE void ENAME##_delete(struct ENAME *e) \
199  { free(e); } \
200 LIST_INLINE void ENAME##_list_push_front(struct ENAME##_list *l, struct ENAME *e) \
201  { if(l->_tail == NULL) l->_tail = e; else l->_head->_prev = e; \
202  e->_prev = NULL; e->_next = l->_head; l->_head = e; } \
203 LIST_INLINE void ENAME##_list_push_back(struct ENAME##_list *l, struct ENAME *e) \
204  { if(l->_head == NULL) l->_head = e; else l->_tail->_next = e; \
205  e->_next = NULL; e->_prev = l->_tail; l->_tail = e; } \
206 LIST_INLINE void ENAME##_list_insert_before(struct ENAME##_list *l, struct ENAME *e, struct ENAME *o) \
207  { struct ENAME *p = o->_prev; if (p) { p->_next = e; e->_prev = p; } else { l->_head = e; e->_prev = NULL; } \
208  e->_next = o; o->_prev = e; } \
209 LIST_INLINE void ENAME##_list_insert_after(struct ENAME##_list *l, struct ENAME *e, struct ENAME *o) \
210  { struct ENAME *n = o->_next; if (n) { n->_prev = e; e->_next = n; } else { l->_tail = e; e->_next = NULL; } \
211  e->_prev = o; o->_next = e; } \
212 LIST_INLINE void ENAME##_list_push_list_front(struct ENAME##_list *l1, struct ENAME##_list *l2) \
213  { if (l2->_head == NULL) { l2->_head = l1->_head; l2->_tail = l1->_tail; } \
214  else if (l1->_head != NULL) { l1->_tail->_next = l2->_head; l2->_head->_prev = l1->_tail; l2->_head = l1->_head; } } \
215 LIST_INLINE void ENAME##_list_push_list_back(struct ENAME##_list *l1, struct ENAME##_list *l2) \
216  { if(l1->_head == NULL) { l1->_head = l2->_head; l1->_tail = l2->_tail; } \
217  else if (l2->_head != NULL) { l1->_tail->_next = l2->_head; l2->_head->_prev = l1->_tail; l1->_tail = l2->_tail; } } \
218 LIST_INLINE struct ENAME *ENAME##_list_front(const struct ENAME##_list *l) \
219  { return l->_head; } \
220 LIST_INLINE struct ENAME *ENAME##_list_back(const struct ENAME##_list *l) \
221  { return l->_tail; } \
222 LIST_INLINE void ENAME##_list_init(struct ENAME##_list *l) \
223  { l->_head=NULL; l->_tail=l->_head; } \
224 LIST_INLINE struct ENAME##_list *ENAME##_list_new(void) \
225  { struct ENAME##_list *l; _STARPU_MALLOC(l, sizeof(struct ENAME##_list)); \
226  ENAME##_list_init(l); return l; } \
227 LIST_INLINE int ENAME##_list_empty(const struct ENAME##_list *l) \
228  { return (l->_head == NULL); } \
229 LIST_INLINE void ENAME##_list_delete(struct ENAME##_list *l) \
230  { free(l); } \
231 LIST_INLINE void ENAME##_list_erase(struct ENAME##_list *l, struct ENAME *c) \
232  { struct ENAME *p = c->_prev; if(p) p->_next = c->_next; else l->_head = c->_next; \
233  if(c->_next) c->_next->_prev = p; else l->_tail = p; } \
234 LIST_INLINE struct ENAME *ENAME##_list_pop_front(struct ENAME##_list *l) \
235  { struct ENAME *e = ENAME##_list_front(l); \
236  ENAME##_list_erase(l, e); return e; } \
237 LIST_INLINE struct ENAME *ENAME##_list_pop_back(struct ENAME##_list *l) \
238  { struct ENAME *e = ENAME##_list_back(l); \
239  ENAME##_list_erase(l, e); return e; } \
240 LIST_INLINE struct ENAME *ENAME##_list_begin(const struct ENAME##_list *l) \
241  { return l->_head; } \
242 LIST_INLINE struct ENAME *ENAME##_list_end(const struct ENAME##_list *l STARPU_ATTRIBUTE_UNUSED) \
243  { return NULL; } \
244 LIST_INLINE struct ENAME *ENAME##_list_next(const struct ENAME *i) \
245  { return i->_next; } \
246 LIST_INLINE struct ENAME *ENAME##_list_last(const struct ENAME##_list *l) \
247  { return l->_tail; } \
248 LIST_INLINE struct ENAME *ENAME##_list_alpha(const struct ENAME##_list *l STARPU_ATTRIBUTE_UNUSED) \
249  { return NULL; } \
250 LIST_INLINE struct ENAME *ENAME##_list_prev(const struct ENAME *i) \
251  { return i->_prev; } \
252 LIST_INLINE int ENAME##_list_ismember(const struct ENAME##_list *l, const struct ENAME *e) \
253  { struct ENAME *i=l->_head; while(i!=NULL){ if (i == e) return 1; i=i->_next; } return 0; } \
254 LIST_INLINE int ENAME##_list_member(const struct ENAME##_list *l, const struct ENAME *e) \
255  { struct ENAME *i=l->_head; int k=0; while(i!=NULL){if (i == e) return k; k++; i=i->_next; } return -1; } \
256 LIST_INLINE int ENAME##_list_size(const struct ENAME##_list *l) \
257  { struct ENAME *i=l->_head; int k=0; while(i!=NULL){k++;i=i->_next;} return k; } \
258 LIST_INLINE int ENAME##_list_check(const struct ENAME##_list *l) \
259  { struct ENAME *i=l->_head; while(i) \
260  { if ((i->_next == NULL) && i != l->_tail) return 0; \
261  if (i->_next == i) return 0; \
262  i=i->_next;} return 1; } \
263 LIST_INLINE void ENAME##_list_move(struct ENAME##_list *ldst, struct ENAME##_list *lsrc) \
264  { ENAME##_list_init(ldst); ldst->_head = lsrc->_head; ldst->_tail = lsrc->_tail; lsrc->_head = NULL; lsrc->_tail = NULL; }
265 
266 
267 #ifdef STARPU_DEBUG
268 #define STARPU_ASSERT_MULTILIST(expr) STARPU_ASSERT(expr)
269 #else
270 #define STARPU_ASSERT_MULTILIST(expr) ((void) 0)
271 #endif
272 
273 /*
274  * This is an implementation of list allowing to be member of several lists.
275  * - One should first call MULTILIST_CREATE_TYPE for the ENAME and for each
276  * MEMBER type
277  * - Then the main element type should include fields of type
278  * ENAME_multilist_MEMBER
279  * - Then one should call MULTILIST_CREATE_INLINES to create the inlines which
280  * manipulate lists for this MEMBER type.
281  *
282  * *********************************************************
283  * Usage example:
284  *
285  * - initially you'd have:
286  * struct my_struct
287  * {
288  * int a;
289  * int b;
290  * };
291  *
292  * - to make multilists of it, we add MULTILIST_CREATE_TYPE calls before, the
293  * multilist fields, and MULTILIST_CREATE_INLINES calls after::
294  *
295  * MULTILIST_CREATE_TYPE(my_struct, foo);
296  * MULTILIST_CREATE_TYPE(my_struct, bar);
297  *
298  * struct my_struct
299  * {
300  * struct my_struct_multilist_foo foo;
301  * struct my_struct_multilist_bar bar;
302  * int a;
303  * int b;
304  * };
305  *
306  * MULTILIST_CREATE_INLINES(struct my_struct, my_struct, foo);
307  * MULTILIST_CREATE_INLINES(struct my_struct, my_struct, bar);
308  *
309  * - creating a new element and initialize the multilist fields:
310  *
311  * struct my_struct *e = malloc(sizeof(*e));
312  * my_struct_multilist_init_foo(e);
313  * my_struct_multilist_init_bar(e);
314  * e->a = 0;
315  * e->b = 0;
316  *
317  * - setting up an empty list:
318  *
319  * struct my_struct_multilist_foo l;
320  * my_struct_multilist_head_init_foo(&l);
321  *
322  * - add element 'e' at the front of list 'l':
323  * my_struct_multilist_push_front_foo(&l, e);
324  *
325  * - TODO implementation: popping from the front:
326  * struct my_struct *i;
327  * i = my_struct_multilist_front_foo(&l);
328  *
329  * - iterating over a list from the front:
330  * struct my_struct *i;
331  * for(i = my_struct_multilist_begin_foo(&l);
332  * i != my_struct_multilist_end_foo(&l);
333  * i = my_struct_multilist_next_foo(i))
334  * {
335  * printf("a=%d; b=%d\n", i->a, i->b);
336  * }
337  */
338 
339 /* Create the ENAME_multilist_MEMBER, to be used both as head and as member of main element type */
340 #define MULTILIST_CREATE_TYPE(ENAME, MEMBER) \
341 struct ENAME##_multilist_##MEMBER { \
342  struct ENAME##_multilist_##MEMBER *next; \
343  struct ENAME##_multilist_##MEMBER *prev; \
344 };
345 
346 /* Create the inlines */
347 #define MULTILIST_CREATE_INLINES(TYPE, ENAME, MEMBER) \
348 /* Cast from list element to real type. */ \
349 LIST_INLINE TYPE *ENAME##_of_multilist_##MEMBER(struct ENAME##_multilist_##MEMBER *elt) { \
350  return ((TYPE *) ((uintptr_t) (elt) - ((uintptr_t) (&((TYPE *) 0)->MEMBER)))); \
351 } \
352 \
353 /* Initialize a list head. */ \
354 LIST_INLINE void ENAME##_multilist_head_init_##MEMBER(struct ENAME##_multilist_##MEMBER *head) { \
355  head->next = head; \
356  head->prev = head; \
357 } \
358 \
359 /* Initialize a list element. */ \
360 LIST_INLINE void ENAME##_multilist_init_##MEMBER(TYPE *e) { \
361  (e)->MEMBER.next = NULL; \
362  (e)->MEMBER.prev = NULL; \
363 } \
364 \
365 /* Push element to head of a list. */ \
366 LIST_INLINE void ENAME##_multilist_push_front_##MEMBER(struct ENAME##_multilist_##MEMBER *head, TYPE *e) { \
367  STARPU_ASSERT_MULTILIST(e->MEMBER.prev == NULL); \
368  STARPU_ASSERT_MULTILIST(e->MEMBER.next == NULL); \
369  e->MEMBER.next = head->next; \
370  e->MEMBER.prev = head; \
371  head->next->prev = &e->MEMBER; \
372  head->next = &e->MEMBER; \
373 } \
374 \
375 /* Push element to tail of a list. */ \
376 LIST_INLINE void ENAME##_multilist_push_back_##MEMBER(struct ENAME##_multilist_##MEMBER *head, TYPE *e) { \
377  STARPU_ASSERT_MULTILIST(e->MEMBER.prev == NULL); \
378  STARPU_ASSERT_MULTILIST(e->MEMBER.next == NULL); \
379  e->MEMBER.prev = head->prev; \
380  e->MEMBER.next = head; \
381  head->prev->next = &e->MEMBER; \
382  head->prev = &e->MEMBER; \
383 } \
384 \
385 /* Erase element from a list. */ \
386 LIST_INLINE void ENAME##_multilist_erase_##MEMBER(struct ENAME##_multilist_##MEMBER *head STARPU_ATTRIBUTE_UNUSED, TYPE *e) { \
387  STARPU_ASSERT_MULTILIST(e->MEMBER.next->prev == &e->MEMBER); \
388  e->MEMBER.next->prev = e->MEMBER.prev; \
389  STARPU_ASSERT_MULTILIST(e->MEMBER.prev->next == &e->MEMBER); \
390  e->MEMBER.prev->next = e->MEMBER.next; \
391  e->MEMBER.next = NULL; \
392  e->MEMBER.prev = NULL; \
393 } \
394 \
395 /* Test whether the element was queued on the list. */ \
396 LIST_INLINE int ENAME##_multilist_queued_##MEMBER(TYPE *e) { \
397  return ((e)->MEMBER.next != NULL); \
398 } \
399 \
400 /* Test whether the list is empty. */ \
401 LIST_INLINE int ENAME##_multilist_empty_##MEMBER(struct ENAME##_multilist_##MEMBER *head) { \
402  return head->next == head; \
403 } \
404 \
405 /* Test whether the element is alone in a list. */ \
406 LIST_INLINE int ENAME##_multilist_alone_##MEMBER(TYPE *e) { \
407  return (e)->MEMBER.next == (e)->MEMBER.prev; \
408 } \
409 \
410 /* Return the first element of the list. */ \
411 LIST_INLINE TYPE *ENAME##_multilist_begin_##MEMBER(struct ENAME##_multilist_##MEMBER *head) { \
412  return ENAME##_of_multilist_##MEMBER(head->next); \
413 } \
414 /* Return the value to be tested at the end of the list. */ \
415 LIST_INLINE TYPE *ENAME##_multilist_end_##MEMBER(struct ENAME##_multilist_##MEMBER *head) { \
416  return ENAME##_of_multilist_##MEMBER(head); \
417 } \
418 /* Return the next element of the list. */ \
419 LIST_INLINE TYPE *ENAME##_multilist_next_##MEMBER(TYPE *e) { \
420  return ENAME##_of_multilist_##MEMBER(e->MEMBER.next); \
421 } \
422 \
423  /* Move a list from its head to another head. Passing newhead == NULL allows to detach the list from any head. */ \
424 LIST_INLINE void ENAME##_multilist_move_##MEMBER(struct ENAME##_multilist_##MEMBER *head, struct ENAME##_multilist_##MEMBER *newhead) { \
425  if (ENAME##_multilist_empty_##MEMBER(head)) \
426  ENAME##_multilist_head_init_##MEMBER(newhead); \
427  else { \
428  if (newhead) { \
429  newhead->next = head->next; \
430  newhead->next->prev = newhead; \
431  } else { \
432  head->next->prev = head->prev; \
433  } \
434  if (newhead) { \
435  newhead->prev = head->prev; \
436  newhead->prev->next = newhead; \
437  } else { \
438  head->prev->next = head->next; \
439  } \
440  head->next = head; \
441  head->prev = head; \
442  } \
443 }
444 
445 #endif /* __LIST_H__ */