Rizin
unix-like reverse engineering framework and cli tools
atomic.h
Go to the documentation of this file.
1 /******************************************************************************/
2 #ifdef JEMALLOC_H_TYPES
3 
4 #endif /* JEMALLOC_H_TYPES */
5 /******************************************************************************/
6 #ifdef JEMALLOC_H_STRUCTS
7 
8 #endif /* JEMALLOC_H_STRUCTS */
9 /******************************************************************************/
10 #ifdef JEMALLOC_H_EXTERNS
11 
12 #define atomic_read_uint64(p) atomic_add_uint64(p, 0)
13 #define atomic_read_uint32(p) atomic_add_uint32(p, 0)
14 #define atomic_read_p(p) atomic_add_p(p, NULL)
15 #define atomic_read_z(p) atomic_add_z(p, 0)
16 #define atomic_read_u(p) atomic_add_u(p, 0)
17 
18 #endif /* JEMALLOC_H_EXTERNS */
19 /******************************************************************************/
20 #ifdef JEMALLOC_H_INLINES
21 
22 /*
23  * All arithmetic functions return the arithmetic result of the atomic
24  * operation. Some atomic operation APIs return the value prior to mutation, in
25  * which case the following functions must redundantly compute the result so
26  * that it can be returned. These functions are normally inlined, so the extra
27  * operations can be optimized away if the return values aren't used by the
28  * callers.
29  *
30  * <t> atomic_read_<t>(<t> *p) { return (*p); }
31  * <t> atomic_add_<t>(<t> *p, <t> x) { return (*p += x); }
32  * <t> atomic_sub_<t>(<t> *p, <t> x) { return (*p -= x); }
33  * bool atomic_cas_<t>(<t> *p, <t> c, <t> s)
34  * {
35  * if (*p != c)
36  * return (true);
37  * *p = s;
38  * return (false);
39  * }
40  * void atomic_write_<t>(<t> *p, <t> x) { *p = x; }
41  */
42 
43 #if 0
44 // #ifndef JEMALLOC_ENABLE_INLINE
53 void *atomic_add_p(void **p, void *x);
54 void *atomic_sub_p(void **p, void *x);
55 bool atomic_cas_p(void **p, void *c, void *s);
56 void atomic_write_p(void **p, const void *x);
57 size_t atomic_add_z(size_t *p, size_t x);
58 size_t atomic_sub_z(size_t *p, size_t x);
59 bool atomic_cas_z(size_t *p, size_t c, size_t s);
60 void atomic_write_z(size_t *p, size_t x);
61 unsigned atomic_add_u(unsigned *p, unsigned x);
62 unsigned atomic_sub_u(unsigned *p, unsigned x);
63 bool atomic_cas_u(unsigned *p, unsigned c, unsigned s);
64 void atomic_write_u(unsigned *p, unsigned x);
65 // #endif
66 #endif
67 
68 #if 1
71 {
72  return *p += x;
73 }
74 
77 {
78  return *p -= x;
79 }
80 
81 JEMALLOC_INLINE bool
83 {
84  if (*p == c) {
85  *p = s;
86  return true;
87  }
88  return false;
89 }
90 
91 JEMALLOC_INLINE void
93 {
94  uint64_t o;
95  do {
96  o = atomic_read_uint64(p);
97  } while (atomic_cas_uint64(p, o, x));
98 }
99 
100 #endif
101 
102 /******************************************************************************/
103 /* 32-bit operations. */
104 #if 1
107 {
108  return *p += x;
109 }
110 
113 {
114  return *p -= x;
115 }
116 
117 JEMALLOC_INLINE bool
119 {
120  if (*p == c) {
121  *p = s;
122  return true;
123  }
124  return false;
125 }
126 
127 JEMALLOC_INLINE void
129 {
130  *p = x;
131 }
132 #endif
133 
134 /******************************************************************************/
135 /* Pointer operations. */
136 JEMALLOC_INLINE void *
137 atomic_add_p(void **p, void *x)
138 {
139  if (sizeof (*p) == 8) {
140  return ((void *)(size_t)atomic_add_uint64((uint64_t *)p, (uint64_t)(size_t)x));
141  }
142  return ((void *)(size_t)atomic_add_uint32((uint32_t *)(size_t)p, (uint32_t)(size_t)x));
143 }
144 
145 #if 0
146 JEMALLOC_INLINE void *
147 atomic_sub_p(void **p, void *x)
148 {
149 
150 #if (LG_SIZEOF_PTR == 3)
151  return ((void *)atomic_add_uint64((uint64_t *)p,
152  (uint64_t)-((int64_t)x)));
153 #elif (LG_SIZEOF_PTR == 2)
154  return ((void *)atomic_add_uint32((uint32_t *)p,
155  (uint32_t)-((int32_t)x)));
156 #endif
157 }
158 
159 JEMALLOC_INLINE bool
160 atomic_cas_p(void **p, void *c, void *s)
161 {
162 
163 #if (LG_SIZEOF_PTR == 3)
164  return (atomic_cas_uint64((uint64_t *)p, (uint64_t)c, (uint64_t)s));
165 #elif (LG_SIZEOF_PTR == 2)
166  return (atomic_cas_uint32((uint32_t *)p, (uint32_t)c, (uint32_t)s));
167 #endif
168 }
169 
170 #endif
171 
172 JEMALLOC_INLINE void
173 atomic_write_p(void **p, const void *x)
174 {
175 
176 #if (LG_SIZEOF_PTR == 3)
177  atomic_write_uint64((uint64_t *)p, (uint64_t)(size_t)x);
178 #elif (LG_SIZEOF_PTR == 2)
179  atomic_write_uint32((uint32_t *)p, (uint32_t)(size_t)x);
180 #endif
181 }
182 
183 /******************************************************************************/
184 /* size_t operations. */
185 JEMALLOC_INLINE size_t
186 atomic_add_z(size_t *p, size_t x)
187 {
188  if (sizeof (*p) == 8) {
189  return ((size_t)atomic_add_uint64((uint64_t *)p, (uint64_t)x));
190  }
191  return ((size_t)atomic_add_uint32((uint32_t *)p, (uint32_t)x));
192 }
193 
194 JEMALLOC_INLINE size_t
195 atomic_sub_z(size_t *p, size_t x)
196 {
197  if (sizeof (*p) == 8) {
198  return ((size_t)atomic_add_uint64((uint64_t *)p,
199  (uint64_t)-((int64_t)x)));
200  }
201  return ((size_t)atomic_add_uint32((uint32_t *)p,
202  (uint32_t)-((int32_t)x)));
203 }
204 
205 
206 JEMALLOC_INLINE bool
207 atomic_cas_z(size_t *p, size_t c, size_t s)
208 {
209  if (sizeof (*p) == 8) {
210  return (atomic_cas_uint64((uint64_t *)p, (uint64_t)c, (uint64_t)s));
211  }
212  return (atomic_cas_uint32((uint32_t *)p, (uint32_t)c, (uint32_t)s));
213 }
214 
215 #if 0
216 
217 JEMALLOC_INLINE void
218 atomic_write_z(size_t *p, size_t x)
219 {
220 
221 #if (LG_SIZEOF_PTR == 3)
223 #elif (LG_SIZEOF_PTR == 2)
225 #endif
226 }
227 
228 /******************************************************************************/
229 /* unsigned operations. */
230 JEMALLOC_INLINE unsigned
231 atomic_add_u(unsigned *p, unsigned x)
232 {
233 
234 #if (LG_SIZEOF_INT == 3)
235  return ((unsigned)atomic_add_uint64((uint64_t *)p, (uint64_t)x));
236 #elif (LG_SIZEOF_INT == 2)
237  return ((unsigned)atomic_add_uint32((uint32_t *)p, (uint32_t)x));
238 #endif
239 }
240 
241 JEMALLOC_INLINE unsigned
242 atomic_sub_u(unsigned *p, unsigned x)
243 {
244 
245 #if (LG_SIZEOF_INT == 3)
246  return ((unsigned)atomic_add_uint64((uint64_t *)p,
247  (uint64_t)-((int64_t)x)));
248 #elif (LG_SIZEOF_INT == 2)
249  return ((unsigned)atomic_add_uint32((uint32_t *)p,
250  (uint32_t)-((int32_t)x)));
251 #endif
252 }
253 
254 JEMALLOC_INLINE bool
255 atomic_cas_u(unsigned *p, unsigned c, unsigned s)
256 {
257 if (sizeof(*p) == 8) {
258  return (atomic_cas_uint64((uint64_t *)p, (uint64_t)c, (uint64_t)s));
259 }
260  return (atomic_cas_uint32((uint32_t *)p, (uint32_t)c, (uint32_t)s));
261 }
262 
263 JEMALLOC_INLINE void
264 atomic_write_u(unsigned *p, unsigned x)
265 {
266 
267 if (sizeof(*p) == 8) {
269 } else {
271 }
272 }
273 #endif
274 
275 /******************************************************************************/
276 
277 #endif /* JEMALLOC_H_INLINES */
278 /******************************************************************************/
#define JEMALLOC_INLINE
void * p
Definition: libc.cpp:67
int x
Definition: mipsasm.c:20
#define atomic_write_uint32
#define atomic_write_uint64
#define atomic_cas_p
#define atomic_add_uint64
#define atomic_write_z
#define atomic_sub_p
#define atomic_write_p
#define atomic_sub_u
#define atomic_cas_u
#define atomic_add_z
#define atomic_cas_uint32
#define atomic_add_uint32
#define atomic_cas_uint64
#define atomic_sub_uint64
#define atomic_cas_z
#define atomic_write_u
#define atomic_add_u
#define atomic_sub_z
#define atomic_sub_uint32
#define atomic_add_p
static RzSocket * s
Definition: rtr.c:28
long int64_t
Definition: sftypes.h:32
int int32_t
Definition: sftypes.h:33
unsigned int uint32_t
Definition: sftypes.h:29
unsigned long uint64_t
Definition: sftypes.h:28
#define c(i)
Definition: sha256.c:43