-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcow.cpp
More file actions
365 lines (323 loc) · 11.7 KB
/
Copy pathcow.cpp
File metadata and controls
365 lines (323 loc) · 11.7 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
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2025-Present Datadog, Inc.
#include "datadog/impl/core/attribute/cow.hpp"
#include <algorithm>
#include <cstring>
#include "datadog/impl/core/util/assert.hpp"
namespace datadog::impl {
CowValue::CowValue(CowValueType in_type, size_t initial_capacity) : type(in_type) {
// Initialize the appropriate STL union member
switch (type) {
case CowValueType::UUID:
break;
case CowValueType::String:
// NOTE: This branch is technically unreachable, as all production code uses the
// std::string_view constructor to initialize string values
new (&value.string) std::string();
value.string.reserve(initial_capacity);
break;
case CowValueType::Array:
new (&value.array) std::vector<datadog::Attribute>();
value.array.reserve(initial_capacity);
break;
case CowValueType::Object:
new (&value.object) std::vector<std::pair<std::string, datadog::Attribute>>();
value.object.reserve(initial_capacity);
break;
}
}
CowValue::CowValue(const uint8_t uuid_value[16]) : type(CowValueType::UUID) {
value.uid = uuid_value;
}
CowValue::CowValue(std::string_view string_value) : type(CowValueType::String) {
// Copy the passed-in string directly
new (&value.string) std::string(string_value);
}
CowValue::~CowValue() {
// Call the appropriate destructor on our union of STL containers
switch (type) {
case CowValueType::UUID:
// UUID values are stored in the CowValue itself
break;
case CowValueType::String:
value.string.~basic_string();
break;
case CowValueType::Array:
value.array.~vector();
break;
case CowValueType::Object:
value.object.~vector();
break;
}
}
CowValue::CowValue(const CowValue& other) : ref_count(1), type(other.type) {
// Call the appropriate STL copy constructor. For array and object values, the STL
// implementation will call the `Attribute` copy constructor on all values, ensuring
// that reference counts are automatically incremented for nested CowValues.
// Therefore, any array elements or subobjects in our clone will point to the same
// underlying CowValues until the corresponding attributes on the clone are modified.
switch (type) {
case CowValueType::UUID:
value.uid = other.value.uid;
break;
case CowValueType::String:
new (&value.string) std::string(other.value.string);
break;
case CowValueType::Array:
new (&value.array) std::vector<datadog::Attribute>(other.value.array);
break;
case CowValueType::Object:
new (&value.object)
std::vector<std::pair<std::string, datadog::Attribute>>(other.value.object);
break;
}
}
CowValue* CowValue::UUID(const uint8_t value_bytes[16]) {
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
return new CowValue(value_bytes);
}
CowValue* CowValue::String(std::string_view string_value) {
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
return new CowValue(string_value);
}
CowValue* CowValue::Array(size_t initial_capacity) {
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
return new CowValue(CowValueType::Array, initial_capacity);
}
CowValue* CowValue::Object(size_t initial_capacity) {
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
return new CowValue(CowValueType::Object, initial_capacity);
}
void CowValue::AddRef() {
// No need for synchronization guarantees: we only care about incrementing the value
// atomically; we're not doing anything based on its value
ref_count.fetch_add(1, std::memory_order_relaxed);
}
void CowValue::Release() {
// Decrement the reference count, and delete ourselves if that was the final
// reference: since we need to synchronize with all writes to ref_count made
// elsewhere, we need strictly sequential ordering
if (ref_count.fetch_sub(1, std::memory_order_acq_rel) == 1) {
delete this;
}
}
CowValue* CowValue::Clone() const {
// Use copy constructor to initialize our clone, which will have a refcount of 1.
// Leave our own refcount as-is: that's up to the Attribute implementation to handle
// NOLINTNEXTLINE(cppcoreguidelines-owning-memory)
return new CowValue(*this);
}
bool CowValue::IsShared() const {
return ref_count.load(std::memory_order_acquire) > 1;
}
UUID CowValue::GetUUID() const {
if (type == CowValueType::UUID) {
return value.uid;
}
return UUID::Zero;
}
void CowValue::SetUUID(const uint8_t new_value[16]) {
if (type == CowValueType::UUID) {
value.uid = new_value;
}
}
const char* CowValue::CStr() const {
// NOTE: Returning const char* ensures that we can expose string values directly via
// the C API - std::string_view does not store a null terminator. The C++ Attribute
// API converts from const char* to std::string_view.
if (type == CowValueType::String) {
return value.string.c_str();
}
return "";
}
void CowValue::SetString(std::string_view new_value) {
if (type == CowValueType::String) {
value.string = new_value;
}
}
size_t CowValue::Size() const {
// Return the size of the underlying container that we're using
switch (type) {
case CowValueType::UUID:
return 0;
case CowValueType::String:
return value.string.size();
case CowValueType::Array:
return value.array.size();
case CowValueType::Object:
return value.object.size();
}
DATADOG_ASSERT(false, "unhandled CowValueType enum value");
return 0;
}
size_t CowValue::Capacity() const {
// Return the capacity of the underlying container that we're using
switch (type) {
case CowValueType::UUID:
return 0;
case CowValueType::String:
return value.string.capacity();
case CowValueType::Array:
return value.array.capacity();
case CowValueType::Object:
return value.object.capacity();
}
DATADOG_ASSERT(false, "unhandled CowValueType enum value");
return 0;
}
void CowValue::Reserve(size_t new_capacity) {
switch (type) {
case CowValueType::UUID:
break;
case CowValueType::String:
value.string.reserve(new_capacity);
break;
case CowValueType::Array:
value.array.reserve(new_capacity);
break;
case CowValueType::Object:
value.object.reserve(new_capacity);
break;
}
}
void CowValue::Clear(size_t new_capacity) {
// Clear the underlying container: any CowValues held in arrays/objects will be
// released by the Attribute destructor
switch (type) {
case CowValueType::UUID:
break;
case CowValueType::String:
value.string.clear();
if (new_capacity > 0) {
value.string.reserve(new_capacity);
}
break;
case CowValueType::Array:
value.array.clear();
if (new_capacity > 0) {
value.array.reserve(new_capacity);
}
break;
case CowValueType::Object:
value.object.clear();
if (new_capacity > 0) {
value.object.reserve(new_capacity);
}
break;
}
}
void CowValue::Push(const Attribute& item) {
// Array only: copy the new value into the vector, appending it to the back
if (type == CowValueType::Array) {
// Copy item into array, invoking Attribute copy constructor and incrementing item
// refcount if non-primitive
value.array.push_back(item);
}
}
Attribute CowValue::GetAt(int index) const {
// Array: access stored Attribute if in range
if (type == CowValueType::Array && index >= 0 &&
static_cast<size_t>(index) < value.array.size()) {
// Create a copy on return, incrementing refcount if non-primitive
return value.array[index];
}
// Object: access Attribute stored as i'th property value, if in range
if (type == CowValueType::Object && index >= 0 &&
static_cast<size_t>(index) < value.object.size()) {
// Create a copy on return, incrementing refcount if non-primitive
return value.object[index].second;
}
// Not an Array or an Object, or index out of bounds: return a null Attribute value
return Attribute();
}
const char* CowValue::GetPropertyNameCStr(int index) const {
// Object: quick name lookup, if in bounds, compatible with C API
if (type == CowValueType::Object && index >= 0 &&
static_cast<size_t>(index) < value.object.size()) {
return value.object[index].first.c_str();
}
// Not an object, or index out of bounds: return empty string
return "";
}
int CowValue::FindPropertyIndex(std::string_view name) const {
if (type == CowValueType::Object) {
// Find the first pair in our object-values vector that matches the name
auto found =
std::find_if(value.object.begin(), value.object.end(), [name](const auto& kvp) {
return kvp.first == name;
});
// If we found a match, return its index
if (found != value.object.end()) {
// Invariant: we should never allow an object to have more than one value
// registered for any given key
DATADOG_ASSERT(
std::find_if(
std::next(found),
value.object.end(),
[name](const auto& kvp) { return kvp.first == name; }
) == value.object.end(),
"Multiple object properties with the same name on find"
);
// If we have an object with more than INT_MAX values, something's wrong, but fall
// out to return -1 in that case rather than truncating
const size_t index = std::distance(value.object.begin(), found);
if (static_cast<int>(index) <= std::numeric_limits<int>::max()) {
return static_cast<int>(index);
}
}
}
// Not an object, or no matching property found: return -1
return -1;
}
void CowValue::SetProperty(std::string_view name, const Attribute& attribute) {
if (type == CowValueType::Object) {
// Check for an existing value with the given name
auto found =
std::find_if(value.object.begin(), value.object.end(), [name](const auto& kvp) {
return kvp.first == name;
});
// Either modify the existing entry in place, or add a new one to the back
if (found != value.object.end()) {
// Copy-assign attribute into vector, handling AddRef()/Release() calls seamlessly
// (via Attribute copy-assignment operator) for non-primitive values
found->second = attribute;
// Invariant: we should never allow an object to have more than one value
// registered for any given key
DATADOG_ASSERT(
std::find_if(
std::next(found),
value.object.end(),
[name](const auto& kvp) { return kvp.first == name; }
) == value.object.end(),
"Multiple object properties with the same name on set"
);
} else {
// No existing property has this name: append it, creating a copy of the name as
// well as the (possibly-refcounted) attribute
value.object.emplace_back(name, attribute);
}
}
}
void CowValue::DeleteProperty(std::string_view name) {
if (type == CowValueType::Object) {
// Move all elements matching the given name to the end of the vector
auto new_end = std::remove_if(
value.object.begin(), value.object.end(), [name](const auto& kvp) {
return kvp.first == name;
}
);
// Invariant: we should never allow an object to have more than one value registered
// for any given key
DATADOG_ASSERT(
std::distance(new_end, value.object.end()) <= 1,
"Multiple object properties with the same name on delete"
);
// If we found any matching values, remove them, seamlessly handling calls to
// Release() (for any non-primitive items) via Attribute destructor
value.object.erase(new_end, value.object.end());
}
}
} // namespace datadog::impl