1 /* Copyright (c) 2004, 2005, 2006, 2007, 2008, 2009, 2010. 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
15 #include "xbt/sysdep.h" /* size_t */
20 * @addtogroup XBT_swag
21 * @brief a O(1) set based on linked lists
23 * Warning, this module is done to be efficient and performs tons of
24 * cast and dirty things. So make sure you know what you are doing while using it.
25 * It is basically a fifo but with restrictions so that
26 * it can be used as a set. Any operation (add, remove, belongs) is O(1) and
27 * no call to malloc/free is done.
30 /** @defgroup XBT_swag_type Swag types
36 These typedefs are public so that the compiler can
37 do his job but believe me, you don't want to try to play with
38 those structs directly. Use them as an abstract datatype.
41 typedef struct xbt_swag_hookup {
44 } s_xbt_swag_hookup_t;
45 /**< This type should be added to a type that is to be used in a swag.
47 * Whenever a new object with this struct is created, all fields have
50 * Here is an example like that :
54 s_xbt_swag_hookup_t set1_hookup;
55 s_xbt_swag_hookup_t set2_hookup;
65 set1 = xbt_swag_new(xbt_swag_offset(elem, set1_hookup));
66 set2 = xbt_swag_new(xbt_swag_offset(elem, set2_hookup));
71 typedef s_xbt_swag_hookup_t *xbt_swag_hookup_t;
74 typedef struct xbt_swag {
79 } s_xbt_swag_t, *xbt_swag_t;
80 /**< A typical swag */
83 /** @defgroup XBT_swag_func SWAG functions
89 XBT_PUBLIC(xbt_swag_t) xbt_swag_new(size_t offset);
90 XBT_PUBLIC(void) xbt_swag_free(xbt_swag_t swag);
91 XBT_PUBLIC(void) xbt_swag_init(xbt_swag_t swag, size_t offset);
94 * \param obj the objet to insert in the swag
98 * insert \a obj in \a swag
100 #define xbt_swag_insert(obj, swag) xbt_swag_insert_at_tail(obj, swag)
102 XBT_PUBLIC(void) xbt_swag_insert_at_head(void *obj, xbt_swag_t swag);
103 XBT_PUBLIC(void) xbt_swag_insert_at_tail(void *obj, xbt_swag_t swag);
104 XBT_PUBLIC(void *) xbt_swag_remove(void *obj, xbt_swag_t swag);
105 XBT_PUBLIC(void *) xbt_swag_extract(xbt_swag_t swag);
106 XBT_PUBLIC(int) xbt_swag_size(xbt_swag_t swag);
108 #define xbt_swag_getPrev(obj, offset) (((xbt_swag_hookup_t)(((char *) (obj)) + (offset)))->prev)
109 #define xbt_swag_getNext(obj, offset) (((xbt_swag_hookup_t)(((char *) (obj)) + (offset)))->next)
110 #define xbt_swag_belongs(obj, swag) (xbt_swag_getNext((obj), (swag)->offset) || (swag)->tail == (obj))
112 static XBT_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
126 * you have to declare a variable. Why defining such a macro then ?
127 * Because it is portable...
129 #define xbt_swag_offset(var,field) ((char *)&( (var).field ) - (char *)&(var))
133 * \defgroup XBT_swag_curs Swag cursor
136 * Iterates over the whole swag.
140 /** @brief A simple swag iterator
141 * @param obj the indice of the loop
142 * @param swag what to iterate over
143 * @warning you cannot modify the \a swag while using this loop
144 * @hideinitializer */
145 #define xbt_swag_foreach(obj,swag) \
146 for((obj)=xbt_swag_getFirst((swag)); \
148 (obj)=xbt_swag_getNext((obj),(swag)->offset))
151 * @brief A safe swag iterator
152 * @param obj the indice of the loop
153 * @param obj_next the object that is right after (if any) \a obj in the swag
154 * @param swag what to iterate over
157 You can safely modify the \a swag while using this loop.
158 Well, safely... Err. You can remove \a obj without having any
161 #define xbt_swag_foreach_safe(obj,obj_next,swag) \
162 for((obj)=xbt_swag_getFirst((swag)), \
163 ((obj)?(obj_next=xbt_swag_getNext((obj),(swag)->offset)): \
167 ((obj)?(obj_next=xbt_swag_getNext((obj),(swag)->offset)): \
172 #endif /* _XBT_SWAG_H */