-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathdata.h
More file actions
618 lines (515 loc) · 21.4 KB
/
Copy pathdata.h
File metadata and controls
618 lines (515 loc) · 21.4 KB
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
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef THIRD_PARTY_CEL_CPP_BASE_INTERNAL_DATA_H_
#define THIRD_PARTY_CEL_CPP_BASE_INTERNAL_DATA_H_
#include <atomic>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <memory>
#include <type_traits>
#include "absl/base/attributes.h"
#include "absl/base/casts.h"
#include "absl/base/macros.h"
#include "absl/base/optimization.h"
#include "absl/numeric/bits.h"
#include "base/kind.h"
namespace cel {
class Type;
class Value;
class MemoryManager;
namespace base_internal {
// Number of bits to shift to store kind.
inline constexpr int kKindShift = sizeof(uintptr_t) * 8 - 8;
// Mask that has all bits set except the two most significant bits.
inline constexpr uint8_t kKindMask = (uint8_t{1} << 6) - 1;
// uintptr_t with the least significant bit set.
inline constexpr uintptr_t kPointerArenaAllocated = uintptr_t{1} << 0;
// uintptr_t with the second to least significant bit set.
inline constexpr uintptr_t kPointerReferenceCounted = uintptr_t{1} << 1;
// uintptr_t with the least and second to to least significant bits set.
inline constexpr uintptr_t kStoredInline =
kPointerArenaAllocated | kPointerReferenceCounted;
// uintptr_t which is the bitwise OR of kPointerArenaAllocated,
// kPointerReferenceCounted, and kStoredInline.
inline constexpr uintptr_t kPointerBits =
kPointerArenaAllocated | kPointerReferenceCounted | kStoredInline;
// Mask that has all bits set except for `kPointerBits`.
inline constexpr uintptr_t kPointerMask = ~kPointerBits;
// uintptr_t with the most significant bit set.
inline constexpr uintptr_t kArenaAllocated = uintptr_t{1}
<< (sizeof(uintptr_t) * 8 - 1);
inline constexpr uintptr_t kReferenceCounted = 1;
// uintptr_t with all bits set except for the most significant byte.
inline constexpr uintptr_t kReferenceCountMask =
kArenaAllocated | ((uintptr_t{1} << (sizeof(uintptr_t) * 8 - 8)) - 1);
inline constexpr uintptr_t kReferenceCountMax =
((uintptr_t{1} << (sizeof(uintptr_t) * 8 - 8)) - 1);
// uintptr_t with the 8th bit set. Used by inline data to indicate it is
// trivially copyable/moveable/destructible.
inline constexpr uintptr_t kTrivial = 1 << 8;
inline constexpr int kInlineVariantShift = 12;
inline constexpr uintptr_t kInlineVariantBits = uintptr_t{0xf}
<< kInlineVariantShift;
// We assert some expectations we have around alignment, size, and trivial
// destructibility.
static_assert(sizeof(uintptr_t) == sizeof(std::atomic<uintptr_t>),
"uintptr_t and std::atomic<uintptr_t> must have the same size");
static_assert(sizeof(void*) == sizeof(uintptr_t),
"void* and uintptr_t must have the same size");
static_assert(std::is_trivially_destructible_v<std::atomic<uintptr_t>>,
"std::atomic<uintptr_t> must be trivially destructible");
template <typename E>
constexpr uintptr_t AsInlineVariant(E value) {
ABSL_ASSERT(static_cast<uintptr_t>(value) <= 15);
return static_cast<uintptr_t>(value) << kInlineVariantShift;
}
enum class DataLocality {
kNull = 0,
kArenaAllocated = 1,
kReferenceCounted = 2,
kStoredInline = 3,
};
static_assert(static_cast<uintptr_t>(DataLocality::kArenaAllocated) ==
kPointerArenaAllocated);
static_assert(static_cast<uintptr_t>(DataLocality::kReferenceCounted) ==
kPointerReferenceCounted);
static_assert(static_cast<uintptr_t>(DataLocality::kStoredInline) ==
kStoredInline);
// Empty base class of all classes that can be managed by handles.
//
// All `Data` implementations have a size of at least `sizeof(uintptr_t)`, have
// a `uintptr_t` at offset 0, and have an alignment that is at most
// `alignof(std::max_align_t)`.
//
// `Data` implementations are split into two categories: those stored inline and
// those allocated separately on the heap. This detail is not exposed to users
// and is managed entirely by the handles. We use a novel approach where given a
// pointer to some instantiated Data we can determine whether it is stored in a
// handle or allocated separately on the heap. If it is allocated on the heap we
// can then determine if it was allocated in an arena or if it is reference
// counted. We can also determine the `Kind` of data.
//
// We can determine whether data is stored directly in a handle by reading a
// `uintptr_t` at offset 0. If the least significant bit is set, this data is
// stored inside a handle. We rely on the fact that C++ places the virtual
// pointer to the virtual function table at offset 0 and it should be aligned to
// at least `sizeof(void*)`.
class Data {};
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wattributes"
// Empty base class indicating class must be stored directly in the handle and
// not allocated separately on the heap.
//
// For inline data, Kind is stored in the most significant byte of `metadata`.
class InlineData /* : public Data */ {
public:
static void* operator new(size_t) = delete;
static void* operator new[](size_t) = delete;
static void operator delete(void*) = delete;
static void operator delete[](void*) = delete;
InlineData(const InlineData&) = default;
InlineData(InlineData&&) = default;
InlineData& operator=(const InlineData&) = default;
InlineData& operator=(InlineData&&) = default;
protected:
constexpr explicit InlineData(uintptr_t metadata) : metadata_(metadata) {}
private:
uintptr_t metadata_ ABSL_ATTRIBUTE_UNUSED = 0;
};
static_assert(std::is_trivially_copyable_v<InlineData>,
"InlineData must be trivially copyable");
static_assert(std::is_trivially_destructible_v<InlineData>,
"InlineData must be trivially destructible");
static_assert(sizeof(InlineData) == sizeof(uintptr_t),
"InlineData has unexpected padding");
// Used purely for a static_assert.
constexpr size_t HeapDataMetadataAndReferenceCountOffset();
// Base class indicating class must be allocated on the heap and not stored
// directly in a handle.
//
// For heap data, Kind is stored in the most significant byte of
// `metadata_and_reference_count`. If heap data was arena allocated, the most
// significant bit of the most significant byte is set. This property, combined
// with twos complement integers, allows us to easily detect incorrect reference
// counting as the reference count will be negative.
class HeapData /* : public Data */ {
public:
HeapData(const HeapData&) = delete;
HeapData(HeapData&&) = delete;
virtual ~HeapData() = default;
HeapData& operator=(const HeapData&) = delete;
HeapData& operator=(HeapData&&) = delete;
protected:
explicit HeapData(Kind kind)
: metadata_and_reference_count_(static_cast<uintptr_t>(kind)
<< kKindShift) {}
private:
// Called by Arena-based memory managers to determine whether we actually need
// our destructor called. Subclasses should override this if they want their
// destructor to be skippable, by default it is not.
static bool IsDestructorSkippable(
const HeapData& data ABSL_ATTRIBUTE_UNUSED) {
return false;
}
friend class cel::MemoryManager;
friend constexpr size_t HeapDataMetadataAndReferenceCountOffset();
std::atomic<uintptr_t> metadata_and_reference_count_ ABSL_ATTRIBUTE_UNUSED =
0;
};
#pragma GCC diagnostic pop
// Provides introspection for `Data`.
class Metadata final {
public:
static ::cel::Kind Kind(const Data& data) {
ABSL_ASSERT(!IsNull(data));
return static_cast<cel::Kind>(
((IsStoredInline(data)
? VirtualPointer(data)
: ReferenceCount(data).load(std::memory_order_relaxed)) >>
kKindShift) &
kKindMask);
}
static ::cel::Kind KindHeap(const Data& data) {
ABSL_ASSERT(!IsNull(data) && !IsStoredInline(data));
return static_cast<cel::Kind>(
(ReferenceCount(data).load(std::memory_order_relaxed) >> kKindShift) &
kKindMask);
}
static DataLocality Locality(const Data& data) {
// We specifically do not use `IsArenaAllocated()` and
// `IsReferenceCounted()` here due to performance reasons. This code is
// called often in handle implementations.
ABSL_ASSERT(!IsNull(data));
return IsStoredInline(data) ? DataLocality::kStoredInline
: ((ReferenceCount(data).load(std::memory_order_relaxed) &
kArenaAllocated) != kArenaAllocated)
? DataLocality::kReferenceCounted
: DataLocality::kArenaAllocated;
}
static bool IsNull(const Data& data) { return VirtualPointer(data) == 0; }
static bool IsStoredInline(const Data& data) {
return (VirtualPointer(data) & kPointerBits) == kStoredInline;
}
static bool IsArenaAllocated(const Data& data) {
ABSL_ASSERT(!IsNull(data));
return !IsStoredInline(data) &&
// We use relaxed because the top 8 bits are never mutated during
// reference counting and that is all we care about.
(ReferenceCount(data).load(std::memory_order_relaxed) &
kArenaAllocated) == kArenaAllocated;
}
static bool IsReferenceCounted(const Data& data) {
ABSL_ASSERT(!IsNull(data));
return !IsStoredInline(data) &&
// We use relaxed because the top 8 bits are never mutated during
// reference counting and that is all we care about.
(ReferenceCount(data).load(std::memory_order_relaxed) &
kArenaAllocated) != kArenaAllocated;
}
static void Ref(const Data& data) {
ABSL_ASSERT(IsReferenceCounted(data));
const auto count = (ReferenceCount(const_cast<Data&>(data))
.fetch_add(1, std::memory_order_relaxed)) &
kReferenceCountMask;
ABSL_ASSERT(count > 0 && count < kReferenceCountMax);
}
ABSL_MUST_USE_RESULT static bool Unref(const Data& data) {
ABSL_ASSERT(IsReferenceCounted(data));
const auto count = (ReferenceCount(const_cast<Data&>(data))
.fetch_sub(1, std::memory_order_seq_cst)) &
kReferenceCountMask;
ABSL_ASSERT(count > 0 && count < kReferenceCountMax);
return count == 1;
}
template <typename E>
static E GetInlineVariant(const Data& data) {
ABSL_ASSERT(IsStoredInline(data));
return static_cast<E>((VirtualPointer(data) & kInlineVariantBits) >>
kInlineVariantShift);
}
static bool IsUnique(const Data& data) {
ABSL_ASSERT(IsReferenceCounted(data));
return (ReferenceCount(data).load(std::memory_order_acquire) &
kReferenceCountMask) == 1;
}
static bool IsTrivial(const Data& data) {
ABSL_ASSERT(IsStoredInline(data));
return (VirtualPointer(data) & kTrivial) == kTrivial;
}
// Used by `MemoryManager::New()`.
static void SetArenaAllocated(Data& data) {
ReferenceCount(data).fetch_or(kArenaAllocated, std::memory_order_relaxed);
}
// Used by `MemoryManager::New()`.
static void SetReferenceCounted(Data& data) {
ReferenceCount(data).fetch_or(kReferenceCounted, std::memory_order_relaxed);
}
// Used by `MemoryManager::New()` and `T::IsDestructorSkippable()`. This is
// used by `T::IsDestructorSkippable()` to query whether a member `Handle<F>`
// needs its destructor called for an arena-based memory manager.
static bool IsDestructorSkippable(const Data& data) {
// We can skip the destructor for any data which is stored inline and
// trivial, or is arena-allocated.
switch (Locality(data)) {
case DataLocality::kStoredInline:
return IsTrivial(data);
case DataLocality::kReferenceCounted:
return false;
case DataLocality::kArenaAllocated:
return true;
case DataLocality::kNull:
// Locality() never returns kNull.
ABSL_UNREACHABLE();
}
}
private:
static uintptr_t VirtualPointer(const Data& data) {
// The vptr, or equivalent, is stored at offset 0. Inform the compiler that
// `data` is aligned to at least `uintptr_t`.
return *absl::bit_cast<const uintptr_t*>(std::addressof(data));
}
static const std::atomic<uintptr_t>& ReferenceCount(const Data& data) {
// For arena allocated and reference counted, the reference count
// immediately follows the vptr, or equivalent, at offset 0. So its offset
// is `sizeof(uintptr_t)`.
return *absl::bit_cast<const std::atomic<uintptr_t>*>(
absl::bit_cast<uintptr_t>(std::addressof(data)) + sizeof(uintptr_t));
}
static std::atomic<uintptr_t>& ReferenceCount(Data& data) {
// For arena allocated and reference counted, the reference count
// immediately follows the vptr, or equivalent, at offset 0. So its offset
// is `sizeof(uintptr_t)`.
return const_cast<std::atomic<uintptr_t>&>(
ReferenceCount(static_cast<const Data&>(data)));
}
Metadata() = delete;
Metadata(const Metadata&) = delete;
Metadata(Metadata&&) = delete;
Metadata& operator=(const Metadata&) = delete;
Metadata& operator=(Metadata&&) = delete;
};
class TypeMetadata;
class ValueMetadata;
template <typename T, typename = void>
struct SelectMetadataImpl;
template <typename T>
struct SelectMetadataImpl<T,
std::enable_if_t<std::is_base_of_v<cel::Type, T>>> {
using type = TypeMetadata;
};
template <typename T>
struct SelectMetadataImpl<T,
std::enable_if_t<std::is_base_of_v<cel::Value, T>>> {
using type = ValueMetadata;
};
template <typename T>
using SelectMetadata = typename SelectMetadataImpl<T>::type;
template <size_t Size, size_t Align>
union alignas(Align) AnyDataStorage final {
#ifdef NDEBUG
// Only need to clear the pointer for this to appear as empty.
AnyDataStorage() : pointer(0) {}
#else
// In debug builds we clear the entire storage to help identify misuse.
AnyDataStorage() { std::memset(buffer, '\0', sizeof(buffer)); }
#endif
uintptr_t pointer;
uint8_t buffer[Size];
};
// Struct capable of storing data directly or a pointer to data. This is used by
// handle implementations. We use an additional bit to determine whether the
// data pointed to is arena allocated. During arena deletion, we cannot
// dereference our stored pointers as it may have already been deleted. Thus we
// need to know if it was arena allocated without dereferencing the pointer.
template <size_t Size, size_t Align>
struct AnyData final {
static_assert(Size >= sizeof(uintptr_t),
"Size must be at least sizeof(uintptr_t)");
static_assert(Align >= alignof(uintptr_t),
"Align must be at least alignof(uintptr_t)");
static constexpr size_t kSize = Size;
static constexpr size_t kAlign = Align;
using Storage = AnyDataStorage<kSize, kAlign>;
Kind kind_inline() const {
// We do not need apply the mask as the upper bits are only used by heap
// allocated data.
return static_cast<Kind>(pointer() >> kKindShift);
}
Kind kind_heap() const {
return static_cast<Kind>(
((absl::bit_cast<std::atomic<uintptr_t>*>((pointer() & kPointerMask) +
sizeof(uintptr_t))
->load(std::memory_order_relaxed)) >>
kKindShift) &
kKindMask);
}
DataLocality locality() const {
return static_cast<DataLocality>(pointer() & kPointerBits);
}
template <typename E>
E inline_variant() const {
return static_cast<E>((pointer() & kInlineVariantBits) >>
kInlineVariantShift);
}
bool IsNull() const { return pointer() == 0; }
bool IsStoredInline() const {
return locality() == DataLocality::kStoredInline;
}
bool IsArenaAllocated() const {
return locality() == DataLocality::kArenaAllocated;
}
bool IsReferenceCounted() const {
return locality() == DataLocality::kReferenceCounted;
}
void Ref() const {
ABSL_ASSERT(IsReferenceCounted());
// We do not need to apply the pointer mask, we know this is reference
// counted.
Metadata::Ref(*get_heap());
}
ABSL_MUST_USE_RESULT bool Unref() const {
ABSL_ASSERT(IsReferenceCounted());
// We do not need to apply the pointer mask, we know this is reference
// counted.
return Metadata::Unref(*get_heap());
}
bool IsUnique() const {
ABSL_ASSERT(IsReferenceCounted());
// We do not need to apply the pointer mask, we know this is reference
// counted.
return Metadata::IsUnique(*get_heap());
}
bool IsTrivial() const {
ABSL_ASSERT(IsStoredInline());
return (pointer() & kTrivial) == kTrivial;
}
// IMPORTANT: Do not use `Metadata::For(get())` unless you know what you are
// doing, instead us the method of the same name in this class.
Data* get() const {
return (pointer() & kPointerBits) == kStoredInline ? get_inline()
: get_heap();
}
Data* get_inline() const {
return absl::bit_cast<Data*>(const_cast<void*>(buffer()));
}
Data* get_heap() const {
return absl::bit_cast<Data*>(pointer() & kPointerMask);
}
// Copy the bytes from other, similar to `std::memcpy`.
void CopyFrom(const AnyData& other) {
std::memcpy(buffer(), other.buffer(), kSize);
}
// Move the bytes from other, similar to `std::memcpy` and `std::memset`.
void MoveFrom(AnyData& other) {
CopyFrom(other);
other.Clear();
}
template <typename T>
void Destruct() {
static_assert(sizeof(T) <= kSize);
static_assert(alignof(T) <= kAlign);
ABSL_ASSERT(IsStoredInline());
static_cast<T*>(get_inline())->~T();
}
void Clear() {
#ifdef NDEBUG
// We only need to clear the first `sizeof(uintptr_t)` bytes as that is
// consulted to determine locality.
set_pointer(0);
#else
// In debug builds, we clear all the storage to help identify misuse.
std::memset(buffer(), '\0', kSize);
#endif
}
// Counterpart to `Metadata::SetArenaAllocated()` and
// `Metadata::SetReferenceCounted()`, also used by `MemoryManager`.
void ConstructReferenceCounted(const Data& data) {
uintptr_t pointer = absl::bit_cast<uintptr_t>(std::addressof(data));
ABSL_ASSERT(absl::countr_zero(pointer) >=
2); // Assert pointer alignment results in at least the 2 least
// significant bits being unset.
set_pointer(pointer | kPointerReferenceCounted);
ABSL_ASSERT(IsReferenceCounted());
}
// Counterpart to `Metadata::SetArenaAllocated()` and
// `Metadata::SetReferenceCounted()`, also used by `MemoryManager`.
void ConstructArenaAllocated(const Data& data) {
uintptr_t pointer = absl::bit_cast<uintptr_t>(std::addressof(data));
ABSL_ASSERT(absl::countr_zero(pointer) >=
2); // Assert pointer alignment results in at least the 2 least
// significant bits being unset.
set_pointer(pointer | kPointerArenaAllocated);
ABSL_ASSERT(IsArenaAllocated());
}
template <typename T, typename... Args>
void ConstructInline(Args&&... args) {
static_assert(sizeof(T) <= kSize);
static_assert(alignof(T) <= kAlign);
::new (buffer()) T(std::forward<Args>(args)...);
ABSL_ASSERT(IsStoredInline());
}
void* buffer() { return &storage.buffer[0]; }
const void* buffer() const { return &storage.buffer[0]; }
uintptr_t pointer() const { return storage.pointer; }
void set_pointer(uintptr_t pointer) { storage.pointer = pointer; }
Storage storage;
};
template <typename T>
struct IsData
: public std::integral_constant<bool, std::is_base_of_v<Data, T>> {};
template <typename T>
inline constexpr bool IsDataV = IsData<T>::value;
template <typename T>
struct IsDerivedData
: public std::integral_constant<
bool, std::conjunction_v<
std::is_base_of<Data, T>,
std::negation<std::is_same<Data, std::remove_cv_t<T>>>>> {};
template <typename T>
inline constexpr bool IsDerivedDataV = IsDerivedData<T>::value;
template <typename T>
struct IsInlineData
: public std::integral_constant<
bool, std::conjunction_v<IsData<T>, std::is_base_of<InlineData, T>>> {
};
template <typename T>
inline constexpr bool IsInlineDataV = IsInlineData<T>::value;
template <typename T>
struct IsDerivedInlineData
: public std::integral_constant<
bool,
std::conjunction_v<
IsInlineData<T>, IsDerivedData<T>,
std::negation<std::is_same<InlineData, std::remove_cv_t<T>>>>> {};
template <typename T>
inline constexpr bool IsDerivedInlineDataV = IsDerivedInlineData<T>::value;
template <typename T>
struct IsHeapData
: public std::integral_constant<
bool, std::conjunction_v<IsData<T>, std::is_base_of<HeapData, T>>> {};
template <typename T>
inline constexpr bool IsHeapDataV = IsHeapData<T>::value;
template <typename T>
struct IsDerivedHeapData
: public std::integral_constant<
bool,
std::conjunction_v<
IsHeapData<T>, IsDerivedData<T>,
std::negation<std::is_same<HeapData, std::remove_cv_t<T>>>>> {};
template <typename T>
inline constexpr bool IsDerivedHeapDataV = IsDerivedHeapData<T>::value;
} // namespace base_internal
} // namespace cel
#endif // THIRD_PARTY_CEL_CPP_BASE_INTERNAL_DATA_H_