1 /* Copyright (c) 2004-2015. 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 cast and dirty things. So avoid using it unless
8 * you really know what you are doing. */
14 #include "xbt/sysdep.h" /* size_t */
19 * @addtogroup XBT_swag
20 * @brief a O(1) set based on linked lists
22 * Warning, this module is done to be efficient and performs tons of cast and dirty things. So make sure you know what
23 * you are doing while using it.
24 * It is basically a fifo but with restrictions so that it can be used as a set. Any operation (add, remove, belongs)
25 * is O(1) and no call to malloc/free is done.
28 /** @defgroup XBT_swag_type Swag types
33 These typedefs are public so that the compiler can do his job but believe me, you don't want to try to play with
34 those structs directly. Use them as an abstract datatype.
37 typedef struct xbt_swag_hookup {
40 } s_xbt_swag_hookup_t;
41 /**< This type should be added to a type that is to be used in a swag.
43 * Whenever a new object with this struct is created, all fields have to be set to NULL
45 * Here is an example like that :
49 s_xbt_swag_hookup_t set1_hookup;
50 s_xbt_swag_hookup_t set2_hookup;
60 set1 = xbt_swag_new(xbt_swag_offset(elem, set1_hookup));
61 set2 = xbt_swag_new(xbt_swag_offset(elem, set2_hookup));
66 typedef s_xbt_swag_hookup_t *xbt_swag_hookup_t;
68 typedef struct xbt_swag {
73 } s_xbt_swag_t, *xbt_swag_t;
74 /**< A typical swag */
77 /** @defgroup XBT_swag_func SWAG functions
83 XBT_PUBLIC(xbt_swag_t) xbt_swag_new(size_t offset);
84 XBT_PUBLIC(void) xbt_swag_free(xbt_swag_t swag);
85 XBT_PUBLIC(void) xbt_swag_init(xbt_swag_t swag, size_t offset);
88 * \brief Makes a swag empty.
92 #define xbt_swag_reset(swag) do {} while(xbt_swag_extract(swag))
95 * \param obj the objet to insert in the swag
99 * insert \a obj in \a swag
101 #define xbt_swag_insert(obj, swag) xbt_swag_insert_at_tail(obj, swag)
103 XBT_PUBLIC(void) xbt_swag_insert_at_head(void *obj, xbt_swag_t swag);
104 XBT_PUBLIC(void) xbt_swag_insert_at_tail(void *obj, xbt_swag_t swag);
105 XBT_PUBLIC(void *) xbt_swag_remove(void *obj, xbt_swag_t swag);
106 XBT_PUBLIC(void *) xbt_swag_extract(xbt_swag_t swag);
107 XBT_PUBLIC(int) xbt_swag_size(xbt_swag_t swag);
109 #define xbt_swag_getPrev(obj, offset) (((xbt_swag_hookup_t)(((char *) (obj)) + (offset)))->prev)
110 #define xbt_swag_getNext(obj, offset) (((xbt_swag_hookup_t)(((char *) (obj)) + (offset)))->next)
111 #define xbt_swag_belongs(obj, swag) (xbt_swag_getNext((obj), (swag)->offset) || (swag)->tail == (obj))
113 static inline void *xbt_swag_getFirst(xbt_swag_t swag)
119 * \brief Offset computation
120 * \arg var a variable of type <tt>struct</tt> something
121 * \arg field a field of <tt>struct</tt> something
122 * \return the offset of \a field in <tt>struct</tt> something.
125 * It is very similar to offsetof except that is done at runtime and that you have to declare a variable. Why defining
126 * such a macro then ? Because it is portable...
128 #define xbt_swag_offset(var,field) ((char *)&( (var).field ) - (char *)&(var))
132 * \defgroup XBT_swag_curs Swag cursor
135 * Iterates over the whole swag.
139 /** @brief A simple swag iterator
140 * @param obj the indice of the loop
141 * @param swag what to iterate over
142 * @warning you cannot modify the \a swag while using this loop
143 * @hideinitializer */
145 #define xbt_swag_foreach(obj,swag) \
146 for((obj)=xbt_swag_getFirst((swag)); \
148 (obj)=xbt_swag_getNext((obj),(swag)->offset))
150 #define xbt_swag_foreach(obj,swag) \
151 for((obj)=(decltype(obj)) xbt_swag_getFirst((swag)); \
153 (obj)=(decltype(obj)) xbt_swag_getNext((obj),(swag)->offset))
156 * @brief A safe swag iterator
157 * @param obj the indice of the loop
158 * @param obj_next the object that is right after (if any) \a obj in the swag
159 * @param swag what to iterate over
162 You can safely modify the \a swag while using this loop.
163 Well, safely... Err. You can remove \a obj without having any trouble at least. */
167 #define xbt_swag_foreach_safe(obj,obj_next,swag) \
168 for((obj)=xbt_swag_getFirst((swag)), \
169 ((obj)?(obj_next=xbt_swag_getNext((obj),(swag)->offset)): \
173 ((obj)?(obj_next=xbt_swag_getNext((obj),(swag)->offset)): \
178 #define xbt_swag_foreach_safe(obj,obj_next,swag) \
179 for((obj) = (decltype(obj)) xbt_swag_getFirst((swag)), \
180 ((obj)?(obj_next = (decltype(obj)) xbt_swag_getNext((obj),(swag)->offset)): \
184 ((obj)?(obj_next = (decltype(obj)) xbt_swag_getNext((obj),(swag)->offset)): \
192 #endif /* _XBT_SWAG_H */