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. */
12 /* This type should be added to a type that is to be used in such a swag */
14 #include "xbt/sysdep.h"
18 #define PREV(obj,offset) xbt_swag_getPrev(obj,offset)
19 #define NEXT(obj,offset) xbt_swag_getNext(obj,offset)
22 /** Creates a new swag.
23 * \param offset where the hookup is located in the structure
24 * \see xbt_swag_offset
26 * Usage : xbt_swag_new(&obj.setA-&obj);
28 xbt_swag_t xbt_swag_new(size_t offset)
30 xbt_swag_t swag = xbt_new0(s_xbt_swag_t, 1);
32 xbt_swag_init(swag, offset);
38 * \param swag poor victim
40 * kilkil a swag but not it's content. If you do not understand why
41 * xbt_swag_free should not free its content, don't use swags.
43 void xbt_swag_free(xbt_swag_t swag)
48 /** Creates a new swag.
49 * \param swag the swag to initialize
50 * \param offset where the hookup is located in the structure
51 * \see xbt_swag_offset
53 * Usage : xbt_swag_init(swag,&obj.setA-&obj);
55 void xbt_swag_init(xbt_swag_t swag, size_t offset)
59 swag->offset = offset;
65 * \param obj the objet to insert in the swag
68 * insert \a obj in \a swag
70 void xbt_swag_insert(void *obj, xbt_swag_t swag)
73 if (xbt_swag_belongs(obj, swag))
77 if (swag->head == NULL) {
78 xbt_assert0(!(swag->tail), "Inconsistent swag.");
84 PREV(obj, swag->offset) = swag->tail;
85 NEXT(PREV(obj, swag->offset), swag->offset) = obj;
91 * \param obj the objet to insert in the swag
94 * insert (at the head... you probably had a very good reason to do
95 * that, I hope you know what you're doing) \a obj in \a swag
97 void xbt_swag_insert_at_head(void *obj, xbt_swag_t swag)
100 if (xbt_swag_belongs(obj, swag))
104 if (swag->head == NULL) {
105 xbt_assert0(!(swag->tail), "Inconsistent swag.");
111 NEXT(obj, swag->offset) = swag->head;
112 PREV(NEXT(obj, swag->offset), swag->offset) = obj;
118 * \param obj the objet to insert in the swag
121 * insert (at the tail... you probably had a very good reason to do
122 * that, I hope you know what you're doing) \a obj in \a swag
124 void xbt_swag_insert_at_tail(void *obj, xbt_swag_t swag)
127 if (xbt_swag_belongs(obj, swag))
131 if (swag->head == NULL) {
132 xbt_assert0(!(swag->tail), "Inconsistent swag.");
138 PREV(obj, swag->offset) = swag->tail;
139 NEXT(PREV(obj, swag->offset), swag->offset) = obj;
145 * \param obj the objet to remove from the swag
147 * \return \a obj if it was in the \a swag and NULL otherwise
149 * removes \a obj from \a swag
151 void *xbt_swag_remove(void *obj, xbt_swag_t swag)
153 size_t offset = swag->offset;
155 if ((!obj) || (!swag))
157 if(!xbt_swag_belongs(obj, swag)) /* Trying to remove an object that
158 was not in this swag */
161 if (swag->head == swag->tail) { /* special case */
162 if (swag->head != obj) /* Trying to remove an object that was not in this swag */
166 NEXT(obj, offset) = PREV(obj, offset) = NULL;
167 } else if (obj == swag->head) { /* It's the head */
168 swag->head = NEXT(obj, offset);
169 PREV(swag->head, offset) = NULL;
170 NEXT(obj, offset) = NULL;
171 } else if (obj == swag->tail) { /* It's the tail */
172 swag->tail = PREV(obj, offset);
173 NEXT(swag->tail, offset) = NULL;
174 PREV(obj, offset) = NULL;
175 } else { /* It's in the middle */
176 NEXT(PREV(obj, offset), offset) = NEXT(obj, offset);
177 PREV(NEXT(obj, offset), offset) = PREV(obj, offset);
178 PREV(obj, offset) = NEXT(obj, offset) = NULL;
186 * \return an object from the \a swag
188 void *xbt_swag_extract(xbt_swag_t swag)
190 size_t offset = swag->offset;
193 if ((!swag) || (!(swag->head)))
198 if (swag->head == swag->tail) { /* special case */
199 swag->head = swag->tail = NULL;
200 PREV(obj, offset) = NEXT(obj, offset) = NULL;
202 swag->head = NEXT(obj, offset);
203 PREV(swag->head, offset) = NULL;
204 NEXT(obj, offset) = NULL;
212 * \return the number of objects in \a swag
214 int xbt_swag_size(xbt_swag_t swag)
216 return (swag->count);
220 * \param obj an object
222 * \return 1 if \a obj is in the \a swag and 0 otherwise
224 int xbt_swag_belongs(void *obj, xbt_swag_t swag)
226 return ((NEXT(obj, swag->offset)) || (PREV(obj, swag->offset))
227 || (swag->head == obj));
233 XBT_TEST_SUITE("swag","Swag data container");
236 s_xbt_swag_hookup_t setA;
237 s_xbt_swag_hookup_t setB;
239 } shmurtz, s_shmurtz_t, *shmurtz_t;
242 XBT_TEST_UNIT("basic",test_swag_basic,"Basic usage") {
243 shmurtz_t obj1, obj2, obj;
244 xbt_swag_t setA,setB;
246 obj1 = xbt_new0(s_shmurtz_t,1);
247 obj2 = xbt_new0(s_shmurtz_t,1);
252 xbt_test_add0("Basic usage");
253 xbt_test_log3("%p %p %ld\n",obj1,&(obj1->setB),
254 (long)((char *)&(obj1->setB) - (char *)obj1));
256 setA = xbt_swag_new(xbt_swag_offset(*obj1,setA));
257 setB = xbt_swag_new(xbt_swag_offset(*obj1,setB));
259 xbt_swag_insert(obj1, setA);
260 xbt_swag_insert(obj1, setB);
261 xbt_swag_insert(obj2, setA);
262 xbt_swag_insert(obj2, setB);
264 xbt_swag_remove(obj1, setB);
265 /* xbt_swag_remove(obj2, setB);*/
267 xbt_test_add0("Traverse set A");
268 xbt_swag_foreach(obj,setA) {
269 xbt_test_log1("Saw: %s",obj->name);
272 xbt_test_add0("Traverse set B");
273 xbt_swag_foreach(obj,setB) {
274 xbt_test_log1("Saw: %s",obj->name);
277 xbt_test_add0("Ensure set content and length");
278 xbt_test_assert( xbt_swag_belongs(obj1,setA));
279 xbt_test_assert( xbt_swag_belongs(obj2,setA));
281 xbt_test_assert(! xbt_swag_belongs(obj1,setB));
282 xbt_test_assert( xbt_swag_belongs(obj2,setB));
284 xbt_test_assert(xbt_swag_size(setA) == 2);
285 xbt_test_assert(xbt_swag_size(setB) == 1);
291 #endif /* SIMGRID_TEST */