1 /* Copyright (c) 2004-2023. The SimGrid Team. All rights reserved. */
3 /* This program is free software; you can redistribute it and/or modify it
4 * under the terms of the license (GNU LGPL) which comes with this package. */
6 /* Warning, this module is done to be efficient and performs tons of
7 cast and dirty things. So avoid using it unless you really know
10 /* This type should be added to a type that is to be used in such a swag */
13 #include "mmprivate.h" // mmalloc_assert
15 typedef s_xbt_swag_hookup_t *xbt_swag_hookup_t;
16 typedef struct xbt_swag* xbt_swag_t;
17 typedef const struct xbt_swag* const_xbt_swag_t;
19 #define xbt_swag_getPrev(obj, offset) (((xbt_swag_hookup_t)(((char*)(obj)) + (offset)))->prev)
20 #define xbt_swag_getNext(obj, offset) (((xbt_swag_hookup_t)(((char*)(obj)) + (offset)))->next)
21 #define xbt_swag_belongs(obj, swag) (xbt_swag_getNext((obj), (swag)->offset) || (swag)->tail == (obj))
23 static inline void *xbt_swag_getFirst(const_xbt_swag_t swag)
29 * @brief Offset computation
30 * @arg var a variable of type <tt>struct</tt> something
31 * @arg field a field of <tt>struct</tt> something
32 * @return the offset of @a field in <tt>struct</tt> something.
35 * It is very similar to offsetof except that is done at runtime and that you have to declare a variable. Why defining
36 * such a macro then ? Because it is portable...
38 #define xbt_swag_offset(var, field) ((char*)&((var).field) - (char*)&(var))
41 /* Creates a new swag.
42 * @param swag the swag to initialize
43 * @param offset where the hookup is located in the structure
44 * @see xbt_swag_offset
46 * Usage : xbt_swag_init(swag,&obj.setA-&obj);
48 static inline void xbt_swag_init(xbt_swag_t swag, size_t offset)
52 swag->offset = offset;
57 * @param obj the object to insert in the swag
60 * 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
63 static inline void xbt_swag_insert(void *obj, xbt_swag_t swag)
66 mmalloc_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 mmalloc_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 object 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)