1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
|
From 0d3b80f04a1caee51b9995c9626838f85295bb06 Mon Sep 17 00:00:00 2001
From: Justus Winter <4winter@informatik.uni-hamburg.de>
Date: Sun, 23 Nov 2014 19:09:51 +0100
Subject: [PATCH hurd 01/28] libshouldbeinlibc: move the reference counting
primitives here
Declare all functions `extern inline' instead of `static inline'.
This allows us to use them in functions declared as `extern inline'.
* libshouldbeinlibc/refcount.h: Move here, and declare all functions
`extern inline'.
* libshouldbeinlibc/refcount.c: And define the functions here.
* libshouldbeinlibc/Makefile: Add `refcount.{c,h}'.
---
include/refcount.h | 320 ------------------------------------------
libshouldbeinlibc/Makefile | 8 +-
libshouldbeinlibc/refcount.c | 23 +++
libshouldbeinlibc/refcount.h | 326 +++++++++++++++++++++++++++++++++++++++++++
4 files changed, 355 insertions(+), 322 deletions(-)
delete mode 100644 include/refcount.h
create mode 100644 libshouldbeinlibc/refcount.c
create mode 100644 libshouldbeinlibc/refcount.h
diff --git a/include/refcount.h b/include/refcount.h
deleted file mode 100644
index ebde42d..0000000
--- a/include/refcount.h
+++ /dev/null
@@ -1,320 +0,0 @@
-/* Lock-less reference counting primitives
-
- Copyright (C) 2014 Free Software Foundation, Inc.
-
- Written by Justus Winter <4winter@informatik.uni-hamburg.de>
-
- This file is part of the GNU Hurd.
-
- The GNU Hurd is free software; you can redistribute it and/or
- modify it under the terms of the GNU General Public License as
- published by the Free Software Foundation; either version 2, or (at
- your option) any later version.
-
- The GNU Hurd is distributed in the hope that it will be useful, but
- WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
-
-#ifndef _HURD_REFCOUNT_H_
-#define _HURD_REFCOUNT_H_
-
-#include <assert.h>
-#include <limits.h>
-#include <stdint.h>
-
-/* Simple reference counting. */
-
-/* An opaque type. You must not access these values directly. */
-typedef unsigned int refcount_t;
-
-/* Initialize REF with REFERENCES. REFERENCES must not be zero. */
-static inline void
-refcount_init (refcount_t *ref, unsigned int references)
-{
- assert (references > 0 || !"references must not be zero!");
- *ref = references;
-}
-
-/* Increment REF. Return the result of the operation. This function
- uses atomic operations. It is not required to serialize calls to
- this function.
-
- This is the unsafe version of refcount_ref. refcount_ref also
- checks for use-after-free errors. When in doubt, use that one
- instead. */
-static inline unsigned int
-refcount_unsafe_ref (refcount_t *ref)
-{
- unsigned int r;
- r = __atomic_add_fetch (ref, 1, __ATOMIC_RELAXED);
- assert (r != UINT_MAX || !"refcount overflowed!");
- return r;
-}
-
-/* Increment REF. Return the result of the operation. This function
- uses atomic operations. It is not required to serialize calls to
- this function. */
-static inline unsigned int
-refcount_ref (refcount_t *ref)
-{
- unsigned int r;
- r = refcount_unsafe_ref (ref);
- assert (r != 1 || !"refcount detected use-after-free!");
- return r;
-}
-
-/* Decrement REF. Return the result of the operation. This function
- uses atomic operations. It is not required to serialize calls to
- this function. */
-static inline unsigned int
-refcount_deref (refcount_t *ref)
-{
- unsigned int r;
- r = __atomic_sub_fetch (ref, 1, __ATOMIC_RELAXED);
- assert (r != UINT_MAX || !"refcount underflowed!");
- return r;
-}
-
-/* Return REF. This function uses atomic operations. It is not
- required to serialize calls to this function. */
-static inline unsigned int
-refcount_references (refcount_t *ref)
-{
- return __atomic_load_n (ref, __ATOMIC_RELAXED);
-}
-
-/* Reference counting with weak references. */
-
-/* An opaque type. You must not access these values directly. */
-typedef union _references refcounts_t;
-
-/* Instead, the functions manipulating refcounts_t values write the
- results into this kind of objects. */
-struct references {
- /* We chose the layout of this struct so that when it is used in the
- union _references, the hard reference counts occupy the least
- significant bits. We rely on this layout for atomic promotion
- and demotion of references. See refcounts_promote and
- refcounts_demote for details. */
-#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
- uint32_t hard;
- uint32_t weak;
-#else
- uint32_t weak;
- uint32_t hard;
-#endif
-};
-
-/* We use a union to convert struct reference values to uint64_t which
- we can manipulate atomically. While this behavior is not
- guaranteed by the C standard, it is supported by all major
- compilers. */
-union _references {
- struct references references;
- uint64_t value;
-};
-
-/* Initialize REF with HARD and WEAK references. HARD and WEAK must
- not both be zero. */
-static inline void
-refcounts_init (refcounts_t *ref, uint32_t hard, uint32_t weak)
-{
- assert ((hard != 0 || weak != 0) || !"references must not both be zero!");
- ref->references = (struct references) { .hard = hard, .weak = weak };
-}
-
-/* Increment the hard reference count of REF. If RESULT is not NULL,
- the result of the operation is written there. This function uses
- atomic operations. It is not required to serialize calls to this
- function.
-
- This is the unsafe version of refcounts_ref. refcounts_ref also
- checks for use-after-free errors. When in doubt, use that one
- instead. */
-static inline void
-refcounts_unsafe_ref (refcounts_t *ref, struct references *result)
-{
- const union _references op = { .references = { .hard = 1 } };
- union _references r;
- r.value = __atomic_add_fetch (&ref->value, op.value, __ATOMIC_RELAXED);
- assert (r.references.hard != UINT32_MAX || !"refcount overflowed!");
- if (result)
- *result = r.references;
-}
-
-/* Increment the hard reference count of REF. If RESULT is not NULL,
- the result of the operation is written there. This function uses
- atomic operations. It is not required to serialize calls to this
- function. */
-static inline void
-refcounts_ref (refcounts_t *ref, struct references *result)
-{
- struct references r;
- refcounts_unsafe_ref (ref, &r);
- assert (! (r.hard == 1 && r.weak == 0)
- || !"refcount detected use-after-free!");
- if (result)
- *result = r;
-}
-
-/* Decrement the hard reference count of REF. If RESULT is not NULL,
- the result of the operation is written there. This function uses
- atomic operations. It is not required to serialize calls to this
- function. */
-static inline void
-refcounts_deref (refcounts_t *ref, struct references *result)
-{
- const union _references op = { .references = { .hard = 1 } };
- union _references r;
- r.value = __atomic_sub_fetch (&ref->value, op.value, __ATOMIC_RELAXED);
- assert (r.references.hard != UINT32_MAX || !"refcount underflowed!");
- if (result)
- *result = r.references;
-}
-
-/* Promote a weak reference to a hard reference. If RESULT is not
- NULL, the result of the operation is written there. This function
- uses atomic operations. It is not required to serialize calls to
- this function. */
-static inline void
-refcounts_promote (refcounts_t *ref, struct references *result)
-{
- /* To promote a weak reference, we need to atomically subtract 1
- from the weak reference count, and add 1 to the hard reference
- count.
-
- We can subtract by 1 by adding the two's complement of 1 = ~0 to
- a fixed-width value, discarding the overflow.
-
- We do the same in our uint64_t value, but we have chosen the
- layout of struct references so that when it is used in the union
- _references, the weak reference counts occupy the most
- significant bits. When we add ~0 to the weak references, the
- overflow will be discarded as unsigned arithmetic is modulo 2^n.
- So we just add a hard reference. In combination, this is the
- desired operation. */
- const union _references op =
- { .references = { .weak = ~0U, .hard = 1} };
- union _references r;
- r.value = __atomic_add_fetch (&ref->value, op.value, __ATOMIC_RELAXED);
- assert (r.references.hard != UINT32_MAX || !"refcount overflowed!");
- assert (r.references.weak != UINT32_MAX || !"refcount underflowed!");
- if (result)
- *result = r.references;
-}
-
-/* Demote a hard reference to a weak reference. If RESULT is not
- NULL, the result of the operation is written there. This function
- uses atomic operations. It is not required to serialize calls to
- this function. */
-static inline void
-refcounts_demote (refcounts_t *ref, struct references *result)
-{
- /* To demote a hard reference, we need to atomically subtract 1 from
- the hard reference count, and add 1 to the weak reference count.
-
- We can subtract by 1 by adding the two's complement of 1 = ~0 to
- a fixed-width value, discarding the overflow.
-
- We do the same in our uint64_t value, but we have chosen the
- layout of struct references so that when it is used in the union
- _references, the hard reference counts occupy the least
- significant bits. When we add ~0 to the hard references, it will
- overflow into the weak references. This is the desired
- operation. */
- const union _references op = { .references = { .hard = ~0U } };
- union _references r;
- r.value = __atomic_add_fetch (&ref->value, op.value, __ATOMIC_RELAXED);
- assert (r.references.hard != UINT32_MAX || !"refcount underflowed!");
- assert (r.references.weak != UINT32_MAX || !"refcount overflowed!");
- if (result)
- *result = r.references;
-}
-
-/* Increment the weak reference count of REF. If RESULT is not NULL,
- the result of the operation is written there. This function uses
- atomic operations. It is not required to serialize calls to this
- function.
-
- This is the unsafe version of refcounts_ref_weak.
- refcounts_ref_weak also checks for use-after-free errors. When in
- doubt, use that one instead. */
-static inline void
-refcounts_unsafe_ref_weak (refcounts_t *ref, struct references *result)
-{
- const union _references op = { .references = { .weak = 1 } };
- union _references r;
- r.value = __atomic_add_fetch (&ref->value, op.value, __ATOMIC_RELAXED);
- assert (r.references.weak != UINT32_MAX || !"refcount overflowed!");
- if (result)
- *result = r.references;
-}
-
-/* Increment the weak reference count of REF. If RESULT is not NULL,
- the result of the operation is written there. This function uses
- atomic operations. It is not required to serialize calls to this
- function. */
-static inline void
-refcounts_ref_weak (refcounts_t *ref, struct references *result)
-{
- struct references r;
- refcounts_unsafe_ref_weak (ref, &r);
- assert (! (r.hard == 0 && r.weak == 1)
- || !"refcount detected use-after-free!");
- if (result)
- *result = r;
-}
-
-/* Decrement the weak reference count of REF. If RESULT is not NULL,
- the result of the operation is written there. This function uses
- atomic operations. It is not required to serialize calls to this
- function. */
-static inline void
-refcounts_deref_weak (refcounts_t *ref, struct references *result)
-{
- const union _references op = { .references = { .weak = 1 } };
- union _references r;
- r.value = __atomic_sub_fetch (&ref->value, op.value, __ATOMIC_RELAXED);
- assert (r.references.weak != UINT32_MAX || !"refcount underflowed!");
- if (result)
- *result = r.references;
-}
-
-/* Store the current reference counts of REF in RESULT. This function
- uses atomic operations. It is not required to serialize calls to
- this function. */
-static inline void
-refcounts_references (refcounts_t *ref, struct references *result)
-{
- union _references r;
- r.value =__atomic_load_n (&ref->value, __ATOMIC_RELAXED);
- *result = r.references;
-}
-
-/* Return the hard reference count of REF. This function uses atomic
- operations. It is not required to serialize calls to this
- function. */
-static inline uint32_t
-refcounts_hard_references (refcounts_t *ref)
-{
- struct references result;
- refcounts_references (ref, &result);
- return result.hard;
-}
-
-/* Return the weak reference count of REF. This function uses atomic
- operations. It is not required to serialize calls to this
- function. */
-static inline uint32_t
-refcounts_weak_references (refcounts_t *ref)
-{
- struct references result;
- refcounts_references (ref, &result);
- return result.weak;
-}
-
-#endif /* _HURD_REFCOUNT_H_ */
diff --git a/libshouldbeinlibc/Makefile b/libshouldbeinlibc/Makefile
index 14a7939..633d60e 100644
--- a/libshouldbeinlibc/Makefile
+++ b/libshouldbeinlibc/Makefile
@@ -27,9 +27,13 @@ SRCS = termsize.c timefmt.c exec-reauth.c maptime-funcs.c \
idvec-impgids.c idvec-verify.c idvec-rep.c \
ugids.c ugids-argp.c ugids-rep.c ugids-verify.c ugids-subtract.c \
ugids-auth.c ugids-xinl.c ugids-merge.c ugids-imply.c ugids-posix.c \
- ugids-verify-auth.c nullauth.c
+ ugids-verify-auth.c nullauth.c \
+ refcount.c \
+
installhdrs = idvec.h timefmt.h maptime.h \
- wire.h portinfo.h portxlate.h cacheq.h ugids.h nullauth.h
+ wire.h portinfo.h portxlate.h cacheq.h ugids.h nullauth.h \
+ refcount.h \
+
installhdrsubdir = .
OBJS = $(SRCS:.c=.o)
diff --git a/libshouldbeinlibc/refcount.c b/libshouldbeinlibc/refcount.c
new file mode 100644
index 0000000..17e01c5
--- /dev/null
+++ b/libshouldbeinlibc/refcount.c
@@ -0,0 +1,23 @@
+/* Lock-less reference counting primitives
+
+ Copyright (C) 2014 Free Software Foundation, Inc.
+
+ Written by Justus Winter <4winter@informatik.uni-hamburg.de>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
+
+#define REFCOUNT_DEFINE_EI
+#include "refcount.h"
diff --git a/libshouldbeinlibc/refcount.h b/libshouldbeinlibc/refcount.h
new file mode 100644
index 0000000..e8b0f5b
--- /dev/null
+++ b/libshouldbeinlibc/refcount.h
@@ -0,0 +1,326 @@
+/* Lock-less reference counting primitives
+
+ Copyright (C) 2014 Free Software Foundation, Inc.
+
+ Written by Justus Winter <4winter@informatik.uni-hamburg.de>
+
+ This file is part of the GNU Hurd.
+
+ The GNU Hurd is free software; you can redistribute it and/or
+ modify it under the terms of the GNU General Public License as
+ published by the Free Software Foundation; either version 2, or (at
+ your option) any later version.
+
+ The GNU Hurd is distributed in the hope that it will be useful, but
+ WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with the GNU Hurd. If not, see <http://www.gnu.org/licenses/>. */
+
+#ifndef _HURD_REFCOUNT_H_
+#define _HURD_REFCOUNT_H_
+
+#ifdef REFCOUNT_DEFINE_EI
+#define REFCOUNT_EI
+#else
+#define REFCOUNT_EI __extern_inline
+#endif
+
+#include <assert.h>
+#include <limits.h>
+#include <stdint.h>
+
+/* Simple reference counting. */
+
+/* An opaque type. You must not access these values directly. */
+typedef unsigned int refcount_t;
+
+/* Initialize REF with REFERENCES. REFERENCES must not be zero. */
+REFCOUNT_EI void
+refcount_init (refcount_t *ref, unsigned int references)
+{
+ assert (references > 0 || !"references must not be zero!");
+ *ref = references;
+}
+
+/* Increment REF. Return the result of the operation. This function
+ uses atomic operations. It is not required to serialize calls to
+ this function.
+
+ This is the unsafe version of refcount_ref. refcount_ref also
+ checks for use-after-free errors. When in doubt, use that one
+ instead. */
+REFCOUNT_EI unsigned int
+refcount_unsafe_ref (refcount_t *ref)
+{
+ unsigned int r;
+ r = __atomic_add_fetch (ref, 1, __ATOMIC_RELAXED);
+ assert (r != UINT_MAX || !"refcount overflowed!");
+ return r;
+}
+
+/* Increment REF. Return the result of the operation. This function
+ uses atomic operations. It is not required to serialize calls to
+ this function. */
+REFCOUNT_EI unsigned int
+refcount_ref (refcount_t *ref)
+{
+ unsigned int r;
+ r = refcount_unsafe_ref (ref);
+ assert (r != 1 || !"refcount detected use-after-free!");
+ return r;
+}
+
+/* Decrement REF. Return the result of the operation. This function
+ uses atomic operations. It is not required to serialize calls to
+ this function. */
+REFCOUNT_EI unsigned int
+refcount_deref (refcount_t *ref)
+{
+ unsigned int r;
+ r = __atomic_sub_fetch (ref, 1, __ATOMIC_RELAXED);
+ assert (r != UINT_MAX || !"refcount underflowed!");
+ return r;
+}
+
+/* Return REF. This function uses atomic operations. It is not
+ required to serialize calls to this function. */
+REFCOUNT_EI unsigned int
+refcount_references (refcount_t *ref)
+{
+ return __atomic_load_n (ref, __ATOMIC_RELAXED);
+}
+
+/* Reference counting with weak references. */
+
+/* An opaque type. You must not access these values directly. */
+typedef union _references refcounts_t;
+
+/* Instead, the functions manipulating refcounts_t values write the
+ results into this kind of objects. */
+struct references {
+ /* We chose the layout of this struct so that when it is used in the
+ union _references, the hard reference counts occupy the least
+ significant bits. We rely on this layout for atomic promotion
+ and demotion of references. See refcounts_promote and
+ refcounts_demote for details. */
+#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
+ uint32_t hard;
+ uint32_t weak;
+#else
+ uint32_t weak;
+ uint32_t hard;
+#endif
+};
+
+/* We use a union to convert struct reference values to uint64_t which
+ we can manipulate atomically. While this behavior is not
+ guaranteed by the C standard, it is supported by all major
+ compilers. */
+union _references {
+ struct references references;
+ uint64_t value;
+};
+
+/* Initialize REF with HARD and WEAK references. HARD and WEAK must
+ not both be zero. */
+REFCOUNT_EI void
+refcounts_init (refcounts_t *ref, uint32_t hard, uint32_t weak)
+{
+ assert ((hard != 0 || weak != 0) || !"references must not both be zero!");
+ ref->references = (struct references) { .hard = hard, .weak = weak };
+}
+
+/* Increment the hard reference count of REF. If RESULT is not NULL,
+ the result of the operation is written there. This function uses
+ atomic operations. It is not required to serialize calls to this
+ function.
+
+ This is the unsafe version of refcounts_ref. refcounts_ref also
+ checks for use-after-free errors. When in doubt, use that one
+ instead. */
+REFCOUNT_EI void
+refcounts_unsafe_ref (refcounts_t *ref, struct references *result)
+{
+ const union _references op = { .references = { .hard = 1 } };
+ union _references r;
+ r.value = __atomic_add_fetch (&ref->value, op.value, __ATOMIC_RELAXED);
+ assert (r.references.hard != UINT32_MAX || !"refcount overflowed!");
+ if (result)
+ *result = r.references;
+}
+
+/* Increment the hard reference count of REF. If RESULT is not NULL,
+ the result of the operation is written there. This function uses
+ atomic operations. It is not required to serialize calls to this
+ function. */
+REFCOUNT_EI void
+refcounts_ref (refcounts_t *ref, struct references *result)
+{
+ struct references r;
+ refcounts_unsafe_ref (ref, &r);
+ assert (! (r.hard == 1 && r.weak == 0)
+ || !"refcount detected use-after-free!");
+ if (result)
+ *result = r;
+}
+
+/* Decrement the hard reference count of REF. If RESULT is not NULL,
+ the result of the operation is written there. This function uses
+ atomic operations. It is not required to serialize calls to this
+ function. */
+REFCOUNT_EI void
+refcounts_deref (refcounts_t *ref, struct references *result)
+{
+ const union _references op = { .references = { .hard = 1 } };
+ union _references r;
+ r.value = __atomic_sub_fetch (&ref->value, op.value, __ATOMIC_RELAXED);
+ assert (r.references.hard != UINT32_MAX || !"refcount underflowed!");
+ if (result)
+ *result = r.references;
+}
+
+/* Promote a weak reference to a hard reference. If RESULT is not
+ NULL, the result of the operation is written there. This function
+ uses atomic operations. It is not required to serialize calls to
+ this function. */
+REFCOUNT_EI void
+refcounts_promote (refcounts_t *ref, struct references *result)
+{
+ /* To promote a weak reference, we need to atomically subtract 1
+ from the weak reference count, and add 1 to the hard reference
+ count.
+
+ We can subtract by 1 by adding the two's complement of 1 = ~0 to
+ a fixed-width value, discarding the overflow.
+
+ We do the same in our uint64_t value, but we have chosen the
+ layout of struct references so that when it is used in the union
+ _references, the weak reference counts occupy the most
+ significant bits. When we add ~0 to the weak references, the
+ overflow will be discarded as unsigned arithmetic is modulo 2^n.
+ So we just add a hard reference. In combination, this is the
+ desired operation. */
+ const union _references op =
+ { .references = { .weak = ~0U, .hard = 1} };
+ union _references r;
+ r.value = __atomic_add_fetch (&ref->value, op.value, __ATOMIC_RELAXED);
+ assert (r.references.hard != UINT32_MAX || !"refcount overflowed!");
+ assert (r.references.weak != UINT32_MAX || !"refcount underflowed!");
+ if (result)
+ *result = r.references;
+}
+
+/* Demote a hard reference to a weak reference. If RESULT is not
+ NULL, the result of the operation is written there. This function
+ uses atomic operations. It is not required to serialize calls to
+ this function. */
+REFCOUNT_EI void
+refcounts_demote (refcounts_t *ref, struct references *result)
+{
+ /* To demote a hard reference, we need to atomically subtract 1 from
+ the hard reference count, and add 1 to the weak reference count.
+
+ We can subtract by 1 by adding the two's complement of 1 = ~0 to
+ a fixed-width value, discarding the overflow.
+
+ We do the same in our uint64_t value, but we have chosen the
+ layout of struct references so that when it is used in the union
+ _references, the hard reference counts occupy the least
+ significant bits. When we add ~0 to the hard references, it will
+ overflow into the weak references. This is the desired
+ operation. */
+ const union _references op = { .references = { .hard = ~0U } };
+ union _references r;
+ r.value = __atomic_add_fetch (&ref->value, op.value, __ATOMIC_RELAXED);
+ assert (r.references.hard != UINT32_MAX || !"refcount underflowed!");
+ assert (r.references.weak != UINT32_MAX || !"refcount overflowed!");
+ if (result)
+ *result = r.references;
+}
+
+/* Increment the weak reference count of REF. If RESULT is not NULL,
+ the result of the operation is written there. This function uses
+ atomic operations. It is not required to serialize calls to this
+ function.
+
+ This is the unsafe version of refcounts_ref_weak.
+ refcounts_ref_weak also checks for use-after-free errors. When in
+ doubt, use that one instead. */
+REFCOUNT_EI void
+refcounts_unsafe_ref_weak (refcounts_t *ref, struct references *result)
+{
+ const union _references op = { .references = { .weak = 1 } };
+ union _references r;
+ r.value = __atomic_add_fetch (&ref->value, op.value, __ATOMIC_RELAXED);
+ assert (r.references.weak != UINT32_MAX || !"refcount overflowed!");
+ if (result)
+ *result = r.references;
+}
+
+/* Increment the weak reference count of REF. If RESULT is not NULL,
+ the result of the operation is written there. This function uses
+ atomic operations. It is not required to serialize calls to this
+ function. */
+REFCOUNT_EI void
+refcounts_ref_weak (refcounts_t *ref, struct references *result)
+{
+ struct references r;
+ refcounts_unsafe_ref_weak (ref, &r);
+ assert (! (r.hard == 0 && r.weak == 1)
+ || !"refcount detected use-after-free!");
+ if (result)
+ *result = r;
+}
+
+/* Decrement the weak reference count of REF. If RESULT is not NULL,
+ the result of the operation is written there. This function uses
+ atomic operations. It is not required to serialize calls to this
+ function. */
+REFCOUNT_EI void
+refcounts_deref_weak (refcounts_t *ref, struct references *result)
+{
+ const union _references op = { .references = { .weak = 1 } };
+ union _references r;
+ r.value = __atomic_sub_fetch (&ref->value, op.value, __ATOMIC_RELAXED);
+ assert (r.references.weak != UINT32_MAX || !"refcount underflowed!");
+ if (result)
+ *result = r.references;
+}
+
+/* Store the current reference counts of REF in RESULT. This function
+ uses atomic operations. It is not required to serialize calls to
+ this function. */
+REFCOUNT_EI void
+refcounts_references (refcounts_t *ref, struct references *result)
+{
+ union _references r;
+ r.value =__atomic_load_n (&ref->value, __ATOMIC_RELAXED);
+ *result = r.references;
+}
+
+/* Return the hard reference count of REF. This function uses atomic
+ operations. It is not required to serialize calls to this
+ function. */
+REFCOUNT_EI uint32_t
+refcounts_hard_references (refcounts_t *ref)
+{
+ struct references result;
+ refcounts_references (ref, &result);
+ return result.hard;
+}
+
+/* Return the weak reference count of REF. This function uses atomic
+ operations. It is not required to serialize calls to this
+ function. */
+REFCOUNT_EI uint32_t
+refcounts_weak_references (refcounts_t *ref)
+{
+ struct references result;
+ refcounts_references (ref, &result);
+ return result.weak;
+}
+
+#endif /* _HURD_REFCOUNT_H_ */
--
2.1.3
|