1 /* Copyright (c) 2004-2019. The SimGrid Team.
2 * All rights reserved. */
4 /* This program is free software; you can redistribute it and/or modify it
5 * under the terms of the license (GNU LGPL) which comes with this package. */
7 /* Warning, this module is done to be efficient and performs tons of
8 cast and dirty things. So avoid using it unless you really know
11 /* This type should be added to a type that is to be used in such a swag */
14 #include "xbt/asserts.h"
16 typedef s_xbt_swag_hookup_t *xbt_swag_hookup_t;
17 typedef struct xbt_swag* xbt_swag_t;
18 typedef const struct xbt_swag* const_xbt_swag_t;
20 #define xbt_swag_getPrev(obj, offset) (((xbt_swag_hookup_t)(((char*)(obj)) + (offset)))->prev)
21 #define xbt_swag_getNext(obj, offset) (((xbt_swag_hookup_t)(((char*)(obj)) + (offset)))->next)
22 #define xbt_swag_belongs(obj, swag) (xbt_swag_getNext((obj), (swag)->offset) || (swag)->tail == (obj))
24 static inline void *xbt_swag_getFirst(const_xbt_swag_t swag)
30 * @brief Offset computation
31 * @arg var a variable of type <tt>struct</tt> something
32 * @arg field a field of <tt>struct</tt> something
33 * @return the offset of @a field in <tt>struct</tt> something.
36 * It is very similar to offsetof except that is done at runtime and that you have to declare a variable. Why defining
37 * such a macro then ? Because it is portable...
39 #define xbt_swag_offset(var, field) ((char*)&((var).field) - (char*)&(var))
42 /* Creates a new swag.
43 * @param swag the swag to initialize
44 * @param offset where the hookup is located in the structure
45 * @see xbt_swag_offset
47 * Usage : xbt_swag_init(swag,&obj.setA-&obj);
49 static inline void xbt_swag_init(xbt_swag_t swag, size_t offset)
53 swag->offset = offset;
58 * @param obj the objet to insert in the swag
61 * insert (at the tail... you probably had a very good reason to do that, I hope you know what you're doing) @a obj in
64 static inline void xbt_swag_insert(void *obj, xbt_swag_t swag)
66 xbt_assert(!xbt_swag_belongs(obj, swag) || swag->tail,
67 "This object belongs to an empty swag! Did you correctly initialize the object's hookup?");
70 xbt_assert(!(swag->tail), "Inconsistent swag.");
74 } else if (obj != swag->tail && !xbt_swag_getNext(obj, swag->offset)) {
75 xbt_swag_getPrev(obj, swag->offset) = swag->tail;
76 xbt_swag_getNext(swag->tail, swag->offset) = obj;
83 * @param obj the objet to remove from the swag
85 * @return @a obj if it was in the @a swag and NULL otherwise
87 * removes @a obj from @a swag
89 static inline void *xbt_swag_remove(void *obj, xbt_swag_t swag)
94 size_t offset = swag->offset;
95 void* prev = xbt_swag_getPrev(obj, offset);
96 void* next = xbt_swag_getNext(obj, offset);
99 xbt_swag_getNext(prev, offset) = next;
100 xbt_swag_getPrev(obj, offset) = NULL;
102 xbt_swag_getPrev(next, offset) = prev;
103 xbt_swag_getNext(obj, offset) = NULL;
109 xbt_swag_getPrev(next, offset) = NULL;
110 xbt_swag_getNext(obj, offset) = NULL;
113 } else if (obj == swag->head) {
114 swag->head = swag->tail = NULL;
123 * @return the number of objects in @a swag
125 static inline int xbt_swag_size(const_xbt_swag_t swag)
127 return (swag->count);