• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2017 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can
5  * be found in the LICENSE file.
6  *
7  */
8 
9 #pragma once
10 
11 //
12 //
13 //
14 
15 #include "skc.h"
16 #include "weakref.h"
17 #include "macros.h"
18 
19 //
20 // WEAKREF
21 //
22 
23 #define SKC_WEAKREF_INDEX_BITS   16 // max bits for a weakref index -- this could/should be 12 bits
24 #define SKC_WEAKREF_INDEX_COUNT  (1u << SKC_WEAKREF_INDEX_BITS)
25 
26 #define SKC_WEAKREF_INDEX_MASK   SKC_BITS_TO_MASK(SKC_WEAKREF_INDEX_BITS)
27 #define SKC_WEAKREF_EPOCH_MASK   SKC_BITS_TO_MASK_AT_64(64-SKC_WEAKREF_INDEX_BITS,SKC_WEAKREF_INDEX_BITS)
28 
29 #define SKC_WEAKREF_EPOCH_INIT   0ul
30 #define SKC_WEAKREF_EPOCH_ONE    SKC_BITS_TO_MASK_AT_64(1,SKC_WEAKREF_INDEX_BITS)
31 
32 //
33 // FIXME -- ASSUMES LITTLE-ENDIAN
34 //
35 
36 union skc_weakref
37 {
38   skc_weakref_t u64; // 64-bits containing refutation epoch and an index
39 
40   struct {
41     skc_uint    index :      SKC_WEAKREF_INDEX_BITS;
42     skc_uint    na_lo : 32 - SKC_WEAKREF_INDEX_BITS;
43     skc_uint    na_hi;
44   };
45 
46   struct {
47     skc_ulong         :      SKC_WEAKREF_INDEX_BITS;
48     skc_ulong   epoch : 64 - SKC_WEAKREF_INDEX_BITS;
49   };
50 };
51 
52 SKC_STATIC_ASSERT(sizeof(skc_weakref_t)     == sizeof(skc_ulong));
53 SKC_STATIC_ASSERT(sizeof(union skc_weakref) == sizeof(skc_ulong));
54 
55 //
56 //
57 //
58 
59 void
skc_weakref_epoch_init(skc_epoch_t * const epoch)60 skc_weakref_epoch_init(skc_epoch_t * const epoch)
61 {
62   *epoch = SKC_WEAKREF_EPOCH_INIT;
63 }
64 
65 void
skc_weakref_epoch_inc(skc_epoch_t * const epoch)66 skc_weakref_epoch_inc(skc_epoch_t * const epoch)
67 {
68   *epoch += SKC_WEAKREF_EPOCH_ONE;
69 }
70 
71 void
skc_weakref_init(skc_weakref_t * const weakref,skc_epoch_t * const epoch,skc_uint const index)72 skc_weakref_init(skc_weakref_t * const weakref,
73                  skc_epoch_t   * const epoch,
74                  skc_uint        const index)
75 {
76   *weakref = *epoch | (index & SKC_WEAKREF_INDEX_MASK);
77 }
78 
79 bool
skc_weakref_is_invalid(skc_weakref_t const * const weakref,skc_epoch_t const * const epoch)80 skc_weakref_is_invalid(skc_weakref_t const * const weakref,
81                        skc_epoch_t   const * const epoch)
82 {
83   return ((*weakref ^ *epoch) & SKC_WEAKREF_EPOCH_MASK) != 0UL;
84 }
85 
86 skc_uint
skc_weakref_index(skc_weakref_t const * const weakref)87 skc_weakref_index(skc_weakref_t const * const weakref)
88 {
89   // truncate to word and mask
90   return (skc_uint)*weakref & SKC_WEAKREF_INDEX_MASK;
91 }
92 
93 //
94 //
95 //
96