3 /* Copyright (c) 2004 Arnaud Legrand. All rights reserved. */
5 /* This program is free software; you can redistribute it and/or modify it
6 * under the terms of the license (GNU LGPL) which comes with this package. */
8 /* Warning, this module is done to be efficient and performs tons of
9 cast and dirty things. So avoid using it unless you really know
10 what you are doing. */
16 #include "xbt/sysdep.h" /* size_t */
21 * @addtogroup XBT_swag
22 * @brief a O(1) set based on linked lists
24 * Warning, this module is done to be efficient and performs tons of
25 * cast and dirty things. So make sure you know what you are doing while using it.
26 * It is basically a fifo but with restrictions so that
27 * it can be used as a set. Any operation (add, remove, belongs) is O(1) and
28 * no call to malloc/free is done.
32 /** @defgroup XBT_swag_type Swag types
38 These typedefs are public so that the compiler can
39 do his job but believe me, you don't want to try to play with
40 those structs directly. Use them as an abstract datatype.
43 typedef struct xbt_swag_hookup {
46 } s_xbt_swag_hookup_t;
47 /**< This type should be added to a type that is to be used in a swag.
49 * Whenever a new object with this struct is created, all fields have
52 * Here is an example like that :
56 s_xbt_swag_hookup_t set1_hookup;
57 s_xbt_swag_hookup_t set2_hookup;
67 set1 = xbt_swag_new(xbt_swag_offset(elem, set1_hookup));
68 set2 = xbt_swag_new(xbt_swag_offset(elem, set2_hookup));
73 typedef s_xbt_swag_hookup_t *xbt_swag_hookup_t;
76 typedef struct xbt_swag {
81 } s_xbt_swag_t, *xbt_swag_t;
82 /**< A typical swag */
85 /** @defgroup XBT_swag_func SWAG functions
91 XBT_PUBLIC(xbt_swag_t) xbt_swag_new(size_t offset);
92 XBT_PUBLIC(void) xbt_swag_free(xbt_swag_t swag);
93 XBT_PUBLIC(void) xbt_swag_init(xbt_swag_t swag, size_t offset);
94 XBT_PUBLIC(void) xbt_swag_insert(void *obj, xbt_swag_t swag);
95 XBT_PUBLIC(void) xbt_swag_insert_at_head(void *obj, xbt_swag_t swag);
96 XBT_PUBLIC(void) xbt_swag_insert_at_tail(void *obj, xbt_swag_t swag);
97 XBT_PUBLIC(void*) xbt_swag_remove(void *obj, xbt_swag_t swag);
98 XBT_PUBLIC(void*)xbt_swag_extract(xbt_swag_t swag);
99 XBT_PUBLIC(int) xbt_swag_size(xbt_swag_t swag);
100 XBT_PUBLIC(int) xbt_swag_belongs(void *obj, xbt_swag_t swag);
102 static XBT_INLINE void *xbt_swag_getFirst(xbt_swag_t swag)
107 #define xbt_swag_getNext(obj,offset) (((xbt_swag_hookup_t)(((char *) (obj)) + (offset)))->prev)
108 #define xbt_swag_getPrev(obj,offset) (((xbt_swag_hookup_t)(((char *) (obj)) + (offset)))->next)
111 * \brief Offset computation
112 * \arg var a variable of type <tt>struct</tt> something
113 * \arg field a field of <tt>struct</tt> something
114 * \return the offset of \a field in <tt>struct</tt> something.
117 * It is very similar to offsetof except that is done at runtime and that
118 * you have to declare a variable. Why defining such a macro then ?
119 * Because it is portable...
121 #define xbt_swag_offset(var,field) ((char *)&( (var).field ) - (char *)&(var))
125 * \defgroup XBT_swag_curs Swag cursor
128 * Iterates over the whole swag.
132 /** @brief A simple swag iterator
133 * @param obj the indice of the loop
134 * @param swag what to iterate over
135 * @warning you cannot modify the \a swag while using this loop
136 * @hideinitializer */
137 #define xbt_swag_foreach(obj,swag) \
138 for((obj)=xbt_swag_getFirst((swag)); \
140 (obj)=xbt_swag_getNext((obj),(swag)->offset))
143 * @brief A safe swag iterator
144 * @param obj the indice of the loop
145 * @param obj_next the object that is right after (if any) \a obj in the swag
146 * @param swag what to iterate over
149 You can safely modify the \a swag while using this loop.
150 Well, safely... Err. You can remove \a obj without having any
153 #define xbt_swag_foreach_safe(obj,obj_next,swag) \
154 for((obj)=xbt_swag_getFirst((swag)), \
155 ((obj)?(obj_next=xbt_swag_getNext((obj),(swag)->offset)): \
159 ((obj)?(obj_next=xbt_swag_getNext((obj),(swag)->offset)): \
165 #endif /* _XBT_SWAG_H */