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;
74 typedef struct xbt_swag s_xbt_swag_t;
75 typedef struct xbt_swag* xbt_swag_t;
77 /**< A typical swag */
80 /** @defgroup XBT_swag_func SWAG functions
86 XBT_PUBLIC(xbt_swag_t) xbt_swag_new(size_t offset);
87 XBT_PUBLIC(void) xbt_swag_free(xbt_swag_t swag);
88 XBT_PUBLIC(void) xbt_swag_init(xbt_swag_t swag, size_t offset);
91 * \brief Makes a swag empty.
95 #define xbt_swag_reset(swag) do {} while(xbt_swag_extract(swag))
98 * \param obj the objet to insert in the swag
102 * insert \a obj in \a swag
104 #define xbt_swag_insert(obj, swag) xbt_swag_insert_at_tail(obj, swag)
106 XBT_PUBLIC(void) xbt_swag_insert_at_head(void *obj, xbt_swag_t swag);
107 XBT_PUBLIC(void) xbt_swag_insert_at_tail(void *obj, xbt_swag_t swag);
108 XBT_PUBLIC(void *) xbt_swag_remove(void *obj, xbt_swag_t swag);
109 XBT_PUBLIC(void *) xbt_swag_extract(xbt_swag_t swag);
110 XBT_PUBLIC(int) xbt_swag_size(xbt_swag_t swag);
112 #define xbt_swag_getPrev(obj, offset) (((xbt_swag_hookup_t)(((char *) (obj)) + (offset)))->prev)
113 #define xbt_swag_getNext(obj, offset) (((xbt_swag_hookup_t)(((char *) (obj)) + (offset)))->next)
114 #define xbt_swag_belongs(obj, swag) (xbt_swag_getNext((obj), (swag)->offset) || (swag)->tail == (obj))
116 static inline void *xbt_swag_getFirst(xbt_swag_t swag)
122 * \brief Offset computation
123 * \arg var a variable of type <tt>struct</tt> something
124 * \arg field a field of <tt>struct</tt> something
125 * \return the offset of \a field in <tt>struct</tt> something.
128 * It is very similar to offsetof except that is done at runtime and that you have to declare a variable. Why defining
129 * such a macro then ? Because it is portable...
131 #define xbt_swag_offset(var,field) ((char *)&( (var).field ) - (char *)&(var))
135 * \defgroup XBT_swag_curs Swag cursor
138 * Iterates over the whole swag.
142 /** @brief A simple swag iterator
143 * @param obj the indice of the loop
144 * @param swag what to iterate over
145 * @warning you cannot modify the \a swag while using this loop
146 * @hideinitializer */
148 #define xbt_swag_foreach(obj,swag) \
149 for((obj)=xbt_swag_getFirst((swag)); \
151 (obj)=xbt_swag_getNext((obj),(swag)->offset))
153 #define xbt_swag_foreach(obj,swag) \
154 for((obj)=(decltype(obj)) xbt_swag_getFirst((swag)); \
156 (obj)=(decltype(obj)) xbt_swag_getNext((obj),(swag)->offset))
159 * @brief A safe swag iterator
160 * @param obj the indice of the loop
161 * @param obj_next the object that is right after (if any) \a obj in the swag
162 * @param swag what to iterate over
165 You can safely modify the \a swag while using this loop.
166 Well, safely... Err. You can remove \a obj without having any trouble at least. */
170 #define xbt_swag_foreach_safe(obj,obj_next,swag) \
171 for((obj)=xbt_swag_getFirst((swag)), \
172 ((obj)?(obj_next=xbt_swag_getNext((obj),(swag)->offset)): \
176 ((obj)?(obj_next=xbt_swag_getNext((obj),(swag)->offset)): \
181 #define xbt_swag_foreach_safe(obj,obj_next,swag) \
182 for((obj) = (decltype(obj)) xbt_swag_getFirst((swag)), \
183 ((obj)?(obj_next = (decltype(obj)) xbt_swag_getNext((obj),(swag)->offset)): \
187 ((obj)?(obj_next = (decltype(obj)) xbt_swag_getNext((obj),(swag)->offset)): \
199 inline void destroy(xbt_swag_t s)
207 #endif /* _XBT_SWAG_H */