blob: 1867adea844bd8cf88458cdc80652a86217194a6 [file] [log] [blame]
Allan MacKinnon4359d522018-06-19 13:57:04 -07001/*
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
36union 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
52SKC_STATIC_ASSERT(sizeof(skc_weakref_t) == sizeof(skc_ulong));
53SKC_STATIC_ASSERT(sizeof(union skc_weakref) == sizeof(skc_ulong));
54
55//
56//
57//
58
Hal Canary14195342018-07-11 16:10:14 -040059void
Allan MacKinnon4359d522018-06-19 13:57:04 -070060skc_weakref_epoch_init(skc_epoch_t * const epoch)
61{
62 *epoch = SKC_WEAKREF_EPOCH_INIT;
63}
64
Hal Canary14195342018-07-11 16:10:14 -040065void
Allan MacKinnon4359d522018-06-19 13:57:04 -070066skc_weakref_epoch_inc(skc_epoch_t * const epoch)
67{
68 *epoch += SKC_WEAKREF_EPOCH_ONE;
69}
70
Hal Canary14195342018-07-11 16:10:14 -040071void
72skc_weakref_init(skc_weakref_t * const weakref,
Allan MacKinnon4359d522018-06-19 13:57:04 -070073 skc_epoch_t * const epoch,
74 skc_uint const index)
75{
76 *weakref = *epoch | (index & SKC_WEAKREF_INDEX_MASK);
77}
78
Hal Canary14195342018-07-11 16:10:14 -040079bool
Allan MacKinnon4359d522018-06-19 13:57:04 -070080skc_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
86skc_uint
87skc_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//