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"
18 /* This type should be added to a type that is to be used in such a swag */
19 /* Whenever a new object with this struct is created, all fields have
23 * \addtogroup XBT_swag
25 * Warning, this module is done to be efficient and performs tons of
26 * cast and dirty things. So avoid using it unless you really know
27 * what you are doing. It is basically a fifo but with restrictions so that
28 * it can be used as a set. Any operation (add, remove, belongs) is O(1) and
29 * no call to malloc/free is done.
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.
48 * For example like that :
52 s_xbt_swag_hookup_t set1_hookup;
53 s_xbt_swag_hookup_t set2_hookup;
63 set1 = xbt_swag_new(xbt_swag_offset(elem, set1_hookup));
64 set2 = xbt_swag_new(xbt_swag_offset(elem, set2_hookup));
69 typedef s_xbt_swag_hookup_t *xbt_swag_hookup_t;
72 typedef struct xbt_swag {
77 } s_xbt_swag_t, *xbt_swag_t;
78 /**< A typical swag */
85 xbt_swag_t xbt_swag_new(size_t offset);
86 void xbt_swag_free(xbt_swag_t swag);
87 void xbt_swag_init(xbt_swag_t swag, size_t offset);
88 void xbt_swag_insert(void *obj, xbt_swag_t swag);
89 void xbt_swag_insert_at_head(void *obj, xbt_swag_t swag);
90 void xbt_swag_insert_at_tail(void *obj, xbt_swag_t swag);
91 void *xbt_swag_remove(void *obj, xbt_swag_t swag);
92 void *xbt_swag_extract(xbt_swag_t swag);
93 int xbt_swag_size(xbt_swag_t swag);
94 int xbt_swag_belongs(void *obj, xbt_swag_t swag);
96 static _XBT_INLINE void *xbt_swag_getFirst(xbt_swag_t swag)
101 #define xbt_swag_getNext(obj,offset) (((xbt_swag_hookup_t)(((char *) (obj)) + (offset)))->prev)
102 #define xbt_swag_getPrev(obj,offset) (((xbt_swag_hookup_t)(((char *) (obj)) + (offset)))->next)
105 * \brief Offset computation
106 * \arg var a variable of type <tt>struct</tt> something
107 * \arg field a field of <tt>struct</tt> something
108 * \return the offset of \a field in <tt>struct</tt> something.
110 * It is very similar to offsetof except that is done at runtime and that
111 * you have to declare a variable. Why defining such a macro then ?
112 * Because it is portable...
114 #define xbt_swag_offset(var,field) ((char *)&( (var).field ) - (char *)&(var))
118 * \name Swag iterator
120 * Iterates over the whole swag.
124 /** @brief A simple swag iterator
125 * @param obj the indice of the loop
126 * @param swag what to iterate over
127 * @warning you cannot modify the \a swag while using this loop
128 * @hideinitializer */
129 #define xbt_swag_foreach(obj,swag) \
130 for((obj)=xbt_swag_getFirst((swag)); \
132 (obj)=xbt_swag_getNext((obj),(swag)->offset))
135 * @brief A safe swag iterator
136 * @param obj the indice of the loop
137 * @param obj_next the object that is right after (if any) \a obj in the swag
138 * @param swag what to iterate over
141 You can safely modify the \a swag while using this loop.
142 Well, safely... Err. You can remove \a obj without having any
145 #define xbt_swag_foreach_safe(obj,obj_next,swag) \
146 for((obj)=xbt_swag_getFirst((swag)), \
147 ((obj)?(obj_next=xbt_swag_getNext((obj),(swag)->offset)): \
151 ((obj)?(obj_next=xbt_swag_getNext((obj),(swag)->offset)): \
156 #endif /* _XBT_SWAG_H */