-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathattribute.cpp
More file actions
535 lines (445 loc) · 15.1 KB
/
Copy pathattribute.cpp
File metadata and controls
535 lines (445 loc) · 15.1 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
// 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/attribute.h"
#include <cstring>
#include <memory>
#include "datadog/impl/core/attribute/cow.hpp"
#include "datadog/impl/core/attribute/types.hpp"
#include "datadog/impl/core/util/assert.hpp"
// Defensively clamp the maximum array/object capacity to a reasonable upper limit at
// the API boundary, so that e.g. a call to `dd_attribute_array(-1)` (which would
// interpret -1 as SIZE_MAX on implicit conversion to size_t) wouldn't cause a crash
static const size_t MAX_INITIAL_CAPACITY = 1024;
static size_t clamp_initial_capacity(size_t capacity) {
return std::min(MAX_INITIAL_CAPACITY, capacity);
}
static bool is_primitive_type(dd_value_type_t type) {
switch (type) {
case DD_VALUE_TYPE_NULL:
case DD_VALUE_TYPE_BOOL:
case DD_VALUE_TYPE_INT:
case DD_VALUE_TYPE_UINT:
case DD_VALUE_TYPE_TIMESTAMP:
case DD_VALUE_TYPE_DOUBLE:
return true;
case DD_VALUE_TYPE_UUID:
case DD_VALUE_TYPE_STRING:
case DD_VALUE_TYPE_ARRAY:
case DD_VALUE_TYPE_OBJECT:
return false;
}
DATADOG_ASSERT(false, "unhandled dd_value_type_t enum value");
return false;
}
static const datadog::impl::CowValue* get_cow_value(const dd_attribute_t* attribute) {
DATADOG_ASSERT(
!is_primitive_type(attribute->type),
"get_cow_value called on dd_attribute_t with primitive type"
);
DATADOG_ASSERT(
attribute->value.ptr,
"dd_attribute_t with non-primitive value type has null storage ptr"
);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return reinterpret_cast<const datadog::impl::CowValue*>(attribute->value.ptr);
}
static datadog::impl::CowValue* get_cow_value(dd_attribute_t* attribute) {
DATADOG_ASSERT(
!is_primitive_type(attribute->type),
"get_cow_value called on dd_attribute_t with primitive type"
);
DATADOG_ASSERT(
attribute->value.ptr,
"dd_attribute_t with non-primitive value type has null storage ptr"
);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
return reinterpret_cast<datadog::impl::CowValue*>(attribute->value.ptr);
}
static datadog::impl::CowValue* get_cow_value_for_write(dd_attribute_t* attribute) {
DATADOG_ASSERT(
!is_primitive_type(attribute->type),
"get_cow_value_for_write called on dd_attribute_t with primitive type"
);
DATADOG_ASSERT(
attribute->value.ptr,
"dd_attribute_t with non-primitive value type has null storage ptr"
);
// NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
auto* cow_value = reinterpret_cast<datadog::impl::CowValue*>(attribute->value.ptr);
if (!cow_value->IsShared()) {
return cow_value;
}
datadog::impl::CowValue* new_value = cow_value->Clone();
cow_value->Release();
attribute->value.ptr = new_value;
return new_value;
}
extern "C" {
dd_attribute_t dd_attribute_null(void) {
dd_attribute_t attribute;
attribute.type = DD_VALUE_TYPE_NULL;
attribute.value.i64 = 0;
return attribute;
}
dd_attribute_t dd_attribute_bool(bool value) {
dd_attribute_t attribute;
attribute.type = DD_VALUE_TYPE_BOOL;
attribute.value.i64 = value ? 1 : 0;
return attribute;
}
dd_attribute_t dd_attribute_int(int64_t value) {
dd_attribute_t attribute;
attribute.type = DD_VALUE_TYPE_INT;
attribute.value.i64 = value;
return attribute;
}
dd_attribute_t dd_attribute_uint(uint64_t value) {
dd_attribute_t attribute;
attribute.type = DD_VALUE_TYPE_UINT;
attribute.value.u64 = value;
return attribute;
}
dd_attribute_t dd_attribute_timestamp(dd_timestamp_t value) {
dd_attribute_t attribute;
attribute.type = DD_VALUE_TYPE_TIMESTAMP;
attribute.value.i64 = value;
return attribute;
}
dd_attribute_t dd_attribute_double(double value) {
dd_attribute_t attribute;
attribute.type = DD_VALUE_TYPE_DOUBLE;
attribute.value.f64 = value;
return attribute;
}
dd_attribute_t dd_attribute_uuid(const dd_uuid_t value) {
dd_attribute_t attribute;
attribute.type = DD_VALUE_TYPE_UUID;
const uint8_t* bytes = static_cast<const uint8_t*>(value.bytes);
attribute.value.ptr = datadog::impl::CowValue::UUID(bytes);
return attribute;
}
dd_attribute_t dd_attribute_string(const char* value) {
dd_attribute_t attribute;
attribute.type = DD_VALUE_TYPE_STRING;
attribute.value.ptr = datadog::impl::CowValue::String(value);
return attribute;
}
dd_attribute_t dd_attribute_array(size_t initial_capacity) {
dd_attribute_t attribute;
attribute.type = DD_VALUE_TYPE_ARRAY;
attribute.value.ptr =
datadog::impl::CowValue::Array(clamp_initial_capacity(initial_capacity));
return attribute;
}
dd_attribute_t dd_attribute_object(size_t initial_capacity) {
dd_attribute_t attribute;
attribute.type = DD_VALUE_TYPE_OBJECT;
attribute.value.ptr =
datadog::impl::CowValue::Object(clamp_initial_capacity(initial_capacity));
return attribute;
}
void dd_attribute_set_null(dd_attribute_t* attribute) {
if (!attribute) {
return;
}
if (!is_primitive_type(attribute->type)) {
get_cow_value(attribute)->Release();
}
attribute->type = DD_VALUE_TYPE_NULL;
attribute->value.i64 = 0;
}
void dd_attribute_set_bool(dd_attribute_t* attribute, bool value) {
if (!attribute) {
return;
}
if (!is_primitive_type(attribute->type)) {
get_cow_value(attribute)->Release();
}
attribute->type = DD_VALUE_TYPE_BOOL;
attribute->value.i64 = value ? 1 : 0;
}
void dd_attribute_set_int(dd_attribute_t* attribute, int64_t value) {
if (!attribute) {
return;
}
if (!is_primitive_type(attribute->type)) {
get_cow_value(attribute)->Release();
}
attribute->type = DD_VALUE_TYPE_INT;
attribute->value.i64 = value;
}
void dd_attribute_set_uint(dd_attribute_t* attribute, uint64_t value) {
if (!attribute) {
return;
}
if (!is_primitive_type(attribute->type)) {
get_cow_value(attribute)->Release();
}
attribute->type = DD_VALUE_TYPE_UINT;
attribute->value.u64 = value;
}
void dd_attribute_set_timestamp(dd_attribute_t* attribute, dd_timestamp_t value) {
if (!attribute) {
return;
}
if (!is_primitive_type(attribute->type)) {
get_cow_value(attribute)->Release();
}
attribute->type = DD_VALUE_TYPE_TIMESTAMP;
attribute->value.i64 = value;
}
void dd_attribute_set_double(dd_attribute_t* attribute, double value) {
if (!attribute) {
return;
}
if (!is_primitive_type(attribute->type)) {
get_cow_value(attribute)->Release();
}
attribute->type = DD_VALUE_TYPE_DOUBLE;
attribute->value.f64 = value;
}
void dd_attribute_set_uuid(dd_attribute_t* attribute, const dd_uuid_t value) {
if (!attribute) {
return;
}
// If already a UUID, copy value directly to existing storage
const uint8_t* bytes = static_cast<const uint8_t*>(value.bytes);
if (attribute->type == DD_VALUE_TYPE_UUID) {
get_cow_value_for_write(attribute)->SetUUID(bytes);
return;
}
// Type is not UUID: release if needed, then allocate new UUID CowValue
if (!is_primitive_type(attribute->type)) {
get_cow_value(attribute)->Release();
}
attribute->type = DD_VALUE_TYPE_UUID;
attribute->value.ptr = datadog::impl::CowValue::UUID(bytes);
}
void dd_attribute_set_string(dd_attribute_t* attribute, const char* value) {
if (!attribute || !value) {
return;
}
// Early-out if already a string: write new string value directly to string storage,
// cloning a copy if our reference is shared with other attributes
if (attribute->type == DD_VALUE_TYPE_STRING) {
get_cow_value_for_write(attribute)->SetString(value);
return;
}
// Type is not string: release if needed, then allocate new string CowValue
if (!is_primitive_type(attribute->type)) {
get_cow_value(attribute)->Release();
}
attribute->type = DD_VALUE_TYPE_STRING;
attribute->value.ptr = datadog::impl::CowValue::String(value);
}
void dd_attribute_init_array(dd_attribute_t* attribute, size_t initial_capacity) {
if (!attribute) {
return;
}
// Early-out if already an array: clear the array, reserving the desired storage
// capacity if applicable
if (attribute->type == DD_VALUE_TYPE_ARRAY) {
get_cow_value_for_write(attribute)->Clear(clamp_initial_capacity(initial_capacity));
return;
}
// Type is not array: release if needed, then allocate new array CowValue
if (!is_primitive_type(attribute->type)) {
get_cow_value(attribute)->Release();
}
attribute->type = DD_VALUE_TYPE_ARRAY;
attribute->value.ptr =
datadog::impl::CowValue::Array(clamp_initial_capacity(initial_capacity));
}
void dd_attribute_init_object(dd_attribute_t* attribute, size_t initial_capacity) {
if (!attribute) {
return;
}
// Early-out if already an object: clear the object of all properties, reserving the
// desired storage capacity if applicable
if (attribute->type == DD_VALUE_TYPE_OBJECT) {
get_cow_value_for_write(attribute)->Clear(clamp_initial_capacity(initial_capacity));
return;
}
// Type is not object: release if needed, then allocate new object CowValue
if (!is_primitive_type(attribute->type)) {
get_cow_value(attribute)->Release();
}
attribute->type = DD_VALUE_TYPE_OBJECT;
attribute->value.ptr =
datadog::impl::CowValue::Object(clamp_initial_capacity(initial_capacity));
}
dd_attribute_t dd_attribute_copy(const dd_attribute_t* other) {
if (!other) {
return dd_attribute_null();
}
dd_attribute_t attribute;
attribute.type = other->type;
attribute.value = other->value; // Bitwise copy of union data
// If we've copied a pointer to a non-primitive value, increment its reference count
if (!is_primitive_type(attribute.type)) {
get_cow_value(&attribute)->AddRef();
}
return attribute;
}
void dd_attribute_free(dd_attribute_t* attribute) {
if (!attribute) {
return;
}
// If we're holding a reference to a non-primitive value, release it
if (!is_primitive_type(attribute->type)) {
get_cow_value(attribute)->Release();
}
// Defensive: clear the struct to make dd_attribute_free() idempotent
attribute->type = DD_VALUE_TYPE_NULL;
attribute->value.i64 = 0;
}
bool dd_attribute_get_bool(const dd_attribute_t* attribute) {
if (!attribute || attribute->type != DD_VALUE_TYPE_BOOL) {
return false;
}
return attribute->value.i64 != 0;
}
int64_t dd_attribute_get_int(const dd_attribute_t* attribute) {
if (!attribute || attribute->type != DD_VALUE_TYPE_INT) {
return 0;
}
return attribute->value.i64;
}
uint64_t dd_attribute_get_uint(const dd_attribute_t* attribute) {
if (!attribute || attribute->type != DD_VALUE_TYPE_UINT) {
return 0;
}
return attribute->value.u64;
}
dd_timestamp_t dd_attribute_get_timestamp(const dd_attribute_t* attribute) {
if (!attribute || attribute->type != DD_VALUE_TYPE_TIMESTAMP) {
return 0;
}
return attribute->value.i64;
}
double dd_attribute_get_double(const dd_attribute_t* attribute) {
if (!attribute || attribute->type != DD_VALUE_TYPE_DOUBLE) {
return 0.0;
}
return attribute->value.f64;
}
dd_uuid_t dd_attribute_get_uuid(const dd_attribute_t* attribute) {
datadog::UUID cpp_value{};
if (attribute && attribute->type == DD_VALUE_TYPE_UUID) {
cpp_value = get_cow_value(attribute)->GetUUID();
}
dd_uuid_t result;
dd_uuid_set(&result, cpp_value.bytes.data());
return result;
}
const char* dd_attribute_get_string(const dd_attribute_t* attribute) {
if (!attribute || attribute->type != DD_VALUE_TYPE_STRING) {
return "";
}
return get_cow_value(attribute)->CStr();
}
size_t dd_attribute_array_len(const dd_attribute_t* attribute) {
if (!attribute || attribute->type != DD_VALUE_TYPE_ARRAY) {
return 0;
}
return get_cow_value(attribute)->Size();
}
dd_attribute_t dd_attribute_array_get(const dd_attribute_t* attribute, int index) {
if (!attribute || attribute->type != DD_VALUE_TYPE_ARRAY) {
return dd_attribute_null();
}
datadog::Attribute cpp_attribute = get_cow_value(attribute)->GetAt(index);
return datadog::impl::AttributeConversion::CopyToC(cpp_attribute);
}
void dd_attribute_array_clear(dd_attribute_t* attribute) {
if (!attribute || attribute->type != DD_VALUE_TYPE_ARRAY) {
return;
}
get_cow_value_for_write(attribute)->Clear();
}
void dd_attribute_array_push(dd_attribute_t* attribute, const dd_attribute_t* item) {
if (!attribute || attribute->type != DD_VALUE_TYPE_ARRAY || !item) {
return;
}
datadog::Attribute cpp_item = datadog::impl::AttributeConversion::CopyFromC(*item);
get_cow_value_for_write(attribute)->Push(cpp_item);
}
void dd_attribute_array_reserve(dd_attribute_t* attribute, size_t capacity) {
if (!attribute || attribute->type != DD_VALUE_TYPE_ARRAY) {
return;
}
if (get_cow_value(attribute)->Capacity() >= capacity) {
return;
}
get_cow_value_for_write(attribute)->Reserve(capacity);
}
size_t dd_attribute_object_property_count(const dd_attribute_t* attribute) {
if (!attribute || attribute->type != DD_VALUE_TYPE_OBJECT) {
return 0;
}
return get_cow_value(attribute)->Size();
}
int dd_attribute_object_property_find(
const dd_attribute_t* attribute, const char* name
) {
if (!attribute || attribute->type != DD_VALUE_TYPE_OBJECT || !name) {
return -1;
}
return get_cow_value(attribute)->FindPropertyIndex(name);
}
const char* dd_attribute_object_name_at(const dd_attribute_t* attribute, int index) {
if (!attribute || attribute->type != DD_VALUE_TYPE_OBJECT) {
return "";
}
return get_cow_value(attribute)->GetPropertyNameCStr(index);
}
dd_attribute_t dd_attribute_object_value_at(
const dd_attribute_t* attribute, int index
) {
if (!attribute || attribute->type != DD_VALUE_TYPE_OBJECT) {
return dd_attribute_null();
}
datadog::Attribute cpp_attribute = get_cow_value(attribute)->GetAt(index);
return datadog::impl::AttributeConversion::CopyToC(cpp_attribute);
}
dd_attribute_t dd_attribute_object_property_get(
const dd_attribute_t* attribute, const char* name
) {
if (!attribute || attribute->type != DD_VALUE_TYPE_OBJECT || !name) {
return dd_attribute_null();
}
const int index = get_cow_value(attribute)->FindPropertyIndex(name);
if (index < 0) {
return dd_attribute_null();
}
datadog::Attribute cpp_attribute = get_cow_value(attribute)->GetAt(index);
return datadog::impl::AttributeConversion::CopyToC(cpp_attribute);
}
void dd_attribute_object_property_set(
dd_attribute_t* attribute, const char* name, const dd_attribute_t* value
) {
if (!attribute || attribute->type != DD_VALUE_TYPE_OBJECT || !name || !value) {
return;
}
datadog::Attribute cpp_value = datadog::impl::AttributeConversion::CopyFromC(*value);
get_cow_value_for_write(attribute)->SetProperty(name, cpp_value);
}
void dd_attribute_object_property_delete(dd_attribute_t* attribute, const char* name) {
if (!attribute || attribute->type != DD_VALUE_TYPE_OBJECT || !name) {
return;
}
get_cow_value_for_write(attribute)->DeleteProperty(name);
}
void dd_attribute_object_reserve(dd_attribute_t* attribute, size_t capacity) {
if (!attribute || attribute->type != DD_VALUE_TYPE_OBJECT) {
return;
}
if (get_cow_value(attribute)->Capacity() >= capacity) {
return;
}
get_cow_value_for_write(attribute)->Reserve(capacity);
}
}