Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Spell check.
[simgrid.git] / src / xbt / mmalloc / swag.c
1 /* Copyright (c) 2004-2019. The SimGrid Team.
2  * All rights reserved.                                                     */
3
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. */
6
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
9    what you are doing. */
10
11 /* This type should be added to a type that is to be used in such a swag */
12
13 #include "swag.h"
14 #include "xbt/asserts.h"
15
16 typedef s_xbt_swag_hookup_t *xbt_swag_hookup_t;
17 typedef struct xbt_swag* xbt_swag_t;
18 typedef const struct xbt_swag* const_xbt_swag_t;
19
20 #define xbt_swag_getPrev(obj, offset) (((xbt_swag_hookup_t)(((char*)(obj)) + (offset)))->prev)
21 #define xbt_swag_getNext(obj, offset) (((xbt_swag_hookup_t)(((char*)(obj)) + (offset)))->next)
22 #define xbt_swag_belongs(obj, swag) (xbt_swag_getNext((obj), (swag)->offset) || (swag)->tail == (obj))
23
24 static inline void *xbt_swag_getFirst(const_xbt_swag_t swag)
25 {
26   return (swag->head);
27 }
28
29 /*
30  * @brief Offset computation
31  * @arg var a variable of type <tt>struct</tt> something
32  * @arg field a field of <tt>struct</tt> something
33  * @return the offset of @a field in <tt>struct</tt> something.
34  * @hideinitializer
35  *
36  * It is very similar to offsetof except that is done at runtime and that you have to declare a variable. Why defining
37  * such a macro then ? Because it is portable...
38  */
39 #define xbt_swag_offset(var, field) ((char*)&((var).field) - (char*)&(var))
40 /* @} */
41
42 /* Creates a new swag.
43  * @param swag the swag to initialize
44  * @param offset where the hookup is located in the structure
45  * @see xbt_swag_offset
46  *
47  * Usage : xbt_swag_init(swag,&obj.setA-&obj);
48  */
49 static inline void xbt_swag_init(xbt_swag_t swag, size_t offset)
50 {
51   swag->tail   = NULL;
52   swag->head   = NULL;
53   swag->offset = offset;
54   swag->count  = 0;
55 }
56
57 /*
58  * @param obj the object to insert in the swag
59  * @param swag a swag
60  *
61  * insert (at the tail... you probably had a very good reason to do that, I hope you know what you're doing) @a obj in
62  * @a swag
63  */
64 static inline void xbt_swag_insert(void *obj, xbt_swag_t swag)
65 {
66   xbt_assert(!xbt_swag_belongs(obj, swag) || swag->tail,
67              "This object belongs to an empty swag! Did you correctly initialize the object's hookup?");
68
69   if (!swag->head) {
70     xbt_assert(!(swag->tail), "Inconsistent swag.");
71     swag->head = obj;
72     swag->tail = obj;
73     swag->count++;
74   } else if (obj != swag->tail && !xbt_swag_getNext(obj, swag->offset)) {
75     xbt_swag_getPrev(obj, swag->offset)        = swag->tail;
76     xbt_swag_getNext(swag->tail, swag->offset) = obj;
77     swag->tail = obj;
78     swag->count++;
79   }
80 }
81
82 /*
83  * @param obj the object to remove from the swag
84  * @param swag a swag
85  * @return @a obj if it was in the @a swag and NULL otherwise
86  *
87  * removes @a obj from @a swag
88  */
89 static inline void *xbt_swag_remove(void *obj, xbt_swag_t swag)
90 {
91   if (!obj)
92     return NULL;
93
94   size_t offset = swag->offset;
95   void* prev    = xbt_swag_getPrev(obj, offset);
96   void* next    = xbt_swag_getNext(obj, offset);
97
98   if (prev) {
99     xbt_swag_getNext(prev, offset) = next;
100     xbt_swag_getPrev(obj, offset)  = NULL;
101     if (next) {
102       xbt_swag_getPrev(next, offset) = prev;
103       xbt_swag_getNext(obj, offset)  = NULL;
104     } else {
105       swag->tail = prev;
106     }
107     swag->count--;
108   } else if (next) {
109     xbt_swag_getPrev(next, offset) = NULL;
110     xbt_swag_getNext(obj, offset)  = NULL;
111     swag->head = next;
112     swag->count--;
113   } else if (obj == swag->head) {
114     swag->head = swag->tail = NULL;
115     swag->count--;
116   }
117
118   return obj;
119 }
120
121 /*
122  * @param swag a swag
123  * @return the number of objects in @a swag
124  */
125 static inline int xbt_swag_size(const_xbt_swag_t swag)
126 {
127   return (swag->count);
128 }