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
|
#ifndef REF_INL_H_
#error "Direct inclusion of this file is not allowed, include ref.h"
// For the sake of sane code completion.
#include "ref.h"
#endif
namespace NYT {
////////////////////////////////////////////////////////////////////////////////
namespace NDetail {
extern const char EmptyRefData[];
extern char MutableEmptyRefData[];
} // namespace NDetail
////////////////////////////////////////////////////////////////////////////////
Y_FORCE_INLINE TRef::TRef(const void* data, size_t size)
: TRange<char>(static_cast<const char*>(data), size)
{ }
Y_FORCE_INLINE TRef::TRef(const void* begin, const void* end)
: TRange<char>(static_cast<const char*>(begin), static_cast<const char*>(end))
{ }
Y_FORCE_INLINE TRef TRef::MakeEmpty()
{
return TRef(NDetail::EmptyRefData, NDetail::EmptyRefData);
}
Y_FORCE_INLINE TRef TRef::FromString(const TString& str)
{
return FromStringBuf(str);
}
Y_FORCE_INLINE TRef TRef::FromString(const std::string& str)
{
return TRef(str.data(), str.size());
}
Y_FORCE_INLINE TRef TRef::FromStringBuf(TStringBuf strBuf)
{
return TRef(strBuf.data(), strBuf.length());
}
template <class T>
Y_FORCE_INLINE TRef TRef::FromPod(const T& data)
{
static_assert(TTypeTraits<T>::IsPod || (std::is_standard_layout_v<T> && std::is_trivial_v<T>), "T must be a pod-type.");
return TRef(&data, sizeof (data));
}
Y_FORCE_INLINE TStringBuf TRef::ToStringBuf() const
{
return TStringBuf(Begin(), Size());
}
Y_FORCE_INLINE TRef TRef::Slice(size_t startOffset, size_t endOffset) const
{
YT_ASSERT(endOffset >= startOffset && endOffset <= Size());
return TRef(Begin() + startOffset, endOffset - startOffset);
}
////////////////////////////////////////////////////////////////////////////////
Y_FORCE_INLINE TMutableRef::TMutableRef(void* data, size_t size)
: TMutableRange<char>(static_cast<char*>(data), size)
{ }
Y_FORCE_INLINE TMutableRef::TMutableRef(void* begin, void* end)
: TMutableRange<char>(static_cast<char*>(begin), static_cast<char*>(end))
{ }
Y_FORCE_INLINE TMutableRef TMutableRef::MakeEmpty()
{
return TMutableRef(NDetail::MutableEmptyRefData, NDetail::MutableEmptyRefData);
}
Y_FORCE_INLINE TMutableRef::operator TRef() const
{
return TRef(Begin(), Size());
}
template <class T>
Y_FORCE_INLINE TMutableRef TMutableRef::FromPod(T& data)
{
static_assert(TTypeTraits<T>::IsPod || (std::is_standard_layout_v<T> && std::is_trivial_v<T>), "T must be a pod-type.");
return TMutableRef(&data, sizeof (data));
}
Y_FORCE_INLINE TMutableRef TMutableRef::FromString(TString& str)
{
// NB: begin() invokes CloneIfShared().
return TMutableRef(str.begin(), str.length());
}
Y_FORCE_INLINE TMutableRef TMutableRef::Slice(size_t startOffset, size_t endOffset) const
{
YT_ASSERT(endOffset >= startOffset && endOffset <= Size());
return TMutableRef(Begin() + startOffset, endOffset - startOffset);
}
////////////////////////////////////////////////////////////////////////////////
Y_FORCE_INLINE TSharedRef::TSharedRef(TRef ref, TSharedRangeHolderPtr holder)
: TSharedRange<char>(ref, std::move(holder))
{ }
Y_FORCE_INLINE TSharedRef::TSharedRef(const void* data, size_t length, TSharedRangeHolderPtr holder)
: TSharedRange<char>(static_cast<const char*>(data), length, std::move(holder))
{ }
Y_FORCE_INLINE TSharedRef::TSharedRef(const void* begin, const void* end, TSharedRangeHolderPtr holder)
: TSharedRange<char>(static_cast<const char*>(begin), static_cast<const char*>(end), std::move(holder))
{ }
Y_FORCE_INLINE TSharedRef TSharedRef::MakeEmpty()
{
return TSharedRef(TRef::MakeEmpty(), nullptr);
}
Y_FORCE_INLINE TSharedRef::operator TRef() const
{
return TRef(Begin(), Size());
}
template <class TTag>
Y_FORCE_INLINE TSharedRef TSharedRef::FromString(TString str)
{
return FromString(std::move(str), GetRefCountedTypeCookie<TTag>());
}
Y_FORCE_INLINE TSharedRef TSharedRef::FromString(TString str)
{
return FromString<TDefaultSharedBlobTag>(std::move(str));
}
Y_FORCE_INLINE TStringBuf TSharedRef::ToStringBuf() const
{
return TStringBuf(Begin(), Size());
}
template <class TTag>
Y_FORCE_INLINE TSharedRef TSharedRef::MakeCopy(TRef ref)
{
return MakeCopy(ref, GetRefCountedTypeCookie<TTag>());
}
Y_FORCE_INLINE TSharedRef TSharedRef::Slice(size_t startOffset, size_t endOffset) const
{
YT_ASSERT(endOffset >= startOffset && endOffset <= Size());
return TSharedRef(Begin() + startOffset, endOffset - startOffset, Holder_);
}
Y_FORCE_INLINE TSharedRef TSharedRef::Slice(const void* begin, const void* end) const
{
YT_ASSERT(begin >= Begin());
YT_ASSERT(end <= End());
return TSharedRef(begin, end, Holder_);
}
////////////////////////////////////////////////////////////////////////////////
Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(const TMutableRef& ref, TSharedRangeHolderPtr holder)
: TSharedMutableRange<char>(ref, std::move(holder))
{ }
Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(void* data, size_t length, TSharedRangeHolderPtr holder)
: TSharedMutableRange<char>(static_cast<char*>(data), length, std::move(holder))
{ }
Y_FORCE_INLINE TSharedMutableRef::TSharedMutableRef(void* begin, void* end, TSharedRangeHolderPtr holder)
: TSharedMutableRange<char>(static_cast<char*>(begin), static_cast<char*>(end), std::move(holder))
{ }
Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::MakeEmpty()
{
return TSharedMutableRef(TMutableRef::MakeEmpty(), nullptr);
}
Y_FORCE_INLINE TSharedMutableRef::operator TMutableRef() const
{
return TMutableRef(Begin(), Size());
}
Y_FORCE_INLINE TSharedMutableRef::operator TSharedRef() const
{
return TSharedRef(Begin(), Size(), Holder_);
}
Y_FORCE_INLINE TSharedMutableRef::operator TRef() const
{
return TRef(Begin(), Size());
}
Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::Allocate(size_t size, TSharedMutableRefAllocateOptions options)
{
return Allocate<TDefaultSharedBlobTag>(size, options);
}
Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::AllocatePageAligned(size_t size, TSharedMutableRefAllocateOptions options)
{
return AllocatePageAligned<TDefaultSharedBlobTag>(size, options);
}
template <class TTag>
Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::MakeCopy(TRef ref)
{
return MakeCopy(ref, GetRefCountedTypeCookie<TTag>());
}
Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::Slice(size_t startOffset, size_t endOffset) const
{
YT_ASSERT(endOffset >= startOffset && endOffset <= Size());
return TSharedMutableRef(Begin() + startOffset, endOffset - startOffset, Holder_);
}
Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::Slice(void* begin, void* end) const
{
YT_ASSERT(begin >= Begin());
YT_ASSERT(end <= End());
return TSharedMutableRef(begin, end, Holder_);
}
template <class TTag>
Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::Allocate(size_t size, TSharedMutableRefAllocateOptions options)
{
return Allocate(size, options, GetRefCountedTypeCookie<TTag>());
}
template <class TTag>
Y_FORCE_INLINE TSharedMutableRef TSharedMutableRef::AllocatePageAligned(size_t size, TSharedMutableRefAllocateOptions options)
{
return AllocatePageAligned(size, options, GetRefCountedTypeCookie<TTag>());
}
////////////////////////////////////////////////////////////////////////////////
Y_FORCE_INLINE size_t GetByteSize(TRef ref)
{
return ref ? ref.Size() : 0;
}
template <class T>
size_t GetByteSize(TRange<T> parts)
{
size_t size = 0;
for (const auto& part : parts) {
size += part.Size();
}
return size;
}
template <class T>
size_t GetByteSize(const std::vector<T>& parts)
{
return GetByteSize(MakeRange(parts));
}
////////////////////////////////////////////////////////////////////////////////
class TSharedRefArrayImpl
: public TSharedRangeHolder
, public TWithExtraSpace<TSharedRefArrayImpl>
{
public:
TSharedRefArrayImpl(
size_t extraSpaceSize,
TRefCountedTypeCookie tagCookie,
size_t size)
: Size_(size)
, ExtraSpaceSize_(extraSpaceSize)
, TagCookie_(tagCookie)
{
for (size_t index = 0; index < Size_; ++index) {
new (MutableBegin() + index) TSharedRef();
}
RegisterWithRefCountedTracker();
}
TSharedRefArrayImpl(
size_t extraSpaceSize,
TRefCountedTypeCookie tagCookie,
const TSharedRef& part)
: Size_(1)
, ExtraSpaceSize_(extraSpaceSize)
, TagCookie_(tagCookie)
{
new (MutableBegin()) TSharedRef(part);
RegisterWithRefCountedTracker();
}
TSharedRefArrayImpl(
size_t extraSpaceSize,
TRefCountedTypeCookie tagCookie,
TSharedRef&& part)
: Size_(1)
, ExtraSpaceSize_(extraSpaceSize)
, TagCookie_(tagCookie)
{
new (MutableBegin()) TSharedRef(std::move(part));
RegisterWithRefCountedTracker();
}
template <class TParts>
TSharedRefArrayImpl(
size_t extraSpaceSize,
TRefCountedTypeCookie tagCookie,
const TParts& parts,
TSharedRefArray::TCopyParts)
: Size_(parts.size())
, ExtraSpaceSize_(extraSpaceSize)
, TagCookie_(tagCookie)
{
for (size_t index = 0; index < Size_; ++index) {
new (MutableBegin() + index) TSharedRef(parts[index]);
}
RegisterWithRefCountedTracker();
}
template <class TParts>
TSharedRefArrayImpl(
size_t extraSpaceSize,
TRefCountedTypeCookie tagCookie,
TParts&& parts,
TSharedRefArray::TMoveParts)
: Size_(parts.size())
, ExtraSpaceSize_(extraSpaceSize)
, TagCookie_(tagCookie)
{
for (size_t index = 0; index < Size_; ++index) {
new (MutableBegin() + index) TSharedRef(std::move(parts[index]));
}
RegisterWithRefCountedTracker();
}
~TSharedRefArrayImpl()
{
for (size_t index = 0; index < Size_; ++index) {
auto& part = MutableBegin()[index];
if (part.GetHolder() == this) {
part.Holder_.Release();
}
part.TSharedRef::~TSharedRef();
}
UnregisterFromRefCountedTracker();
}
size_t Size() const
{
return Size_;
}
bool Empty() const
{
return Size_ == 0;
}
const TSharedRef& operator [] (size_t index) const
{
YT_ASSERT(index < Size());
return Begin()[index];
}
const TSharedRef* Begin() const
{
return static_cast<const TSharedRef*>(GetExtraSpacePtr());
}
const TSharedRef* End() const
{
return Begin() + Size_;
}
// TSharedRangeHolder overrides.
std::optional<size_t> GetTotalByteSize() const override
{
size_t result = 0;
for (size_t index = 0; index < Size(); ++index) {
const auto& part = (*this)[index];
if (!part) {
continue;
}
auto partSize = part.GetHolder()->GetTotalByteSize();
if (!partSize) {
return std::nullopt;
}
result += *partSize;
}
return result;
}
private:
friend class TSharedRefArrayBuilder;
const size_t Size_;
const size_t ExtraSpaceSize_;
const TRefCountedTypeCookie TagCookie_;
void RegisterWithRefCountedTracker()
{
TRefCountedTrackerFacade::AllocateTagInstance(TagCookie_);
TRefCountedTrackerFacade::AllocateSpace(TagCookie_, ExtraSpaceSize_);
}
void UnregisterFromRefCountedTracker()
{
TRefCountedTrackerFacade::FreeTagInstance(TagCookie_);
TRefCountedTrackerFacade::FreeSpace(TagCookie_, ExtraSpaceSize_);
}
TSharedRef* MutableBegin()
{
return static_cast<TSharedRef*>(GetExtraSpacePtr());
}
TSharedRef* MutableEnd()
{
return MutableBegin() + Size_;
}
char* GetBeginAllocationPtr()
{
return static_cast<char*>(static_cast<void*>(MutableEnd()));
}
};
DEFINE_REFCOUNTED_TYPE(TSharedRefArrayImpl)
////////////////////////////////////////////////////////////////////////////////
struct TSharedRefArrayTag { };
Y_FORCE_INLINE TSharedRefArray::TSharedRefArray(TIntrusivePtr<TSharedRefArrayImpl> impl)
: Impl_(std::move(impl))
{ }
Y_FORCE_INLINE TSharedRefArray::TSharedRefArray(const TSharedRefArray& other)
: Impl_(other.Impl_)
{ }
Y_FORCE_INLINE TSharedRefArray::TSharedRefArray(TSharedRefArray&& other) noexcept
: Impl_(std::move(other.Impl_))
{ }
Y_FORCE_INLINE TSharedRefArray::TSharedRefArray(const TSharedRef& part)
: Impl_(NewImpl(1, 0, GetRefCountedTypeCookie<TSharedRefArrayTag>(), part))
{ }
Y_FORCE_INLINE TSharedRefArray::TSharedRefArray(TSharedRef&& part)
: Impl_(NewImpl(1, 0, GetRefCountedTypeCookie<TSharedRefArrayTag>(), std::move(part)))
{ }
template <class TParts>
Y_FORCE_INLINE TSharedRefArray::TSharedRefArray(const TParts& parts, TSharedRefArray::TCopyParts)
: Impl_(NewImpl(parts.size(), 0, GetRefCountedTypeCookie<TSharedRefArrayTag>(), parts, TSharedRefArray::TCopyParts{}))
{ }
template <class TParts>
Y_FORCE_INLINE TSharedRefArray::TSharedRefArray(TParts&& parts, TSharedRefArray::TMoveParts)
: Impl_(NewImpl(parts.size(), 0, GetRefCountedTypeCookie<TSharedRefArrayTag>(), std::move(parts), TSharedRefArray::TMoveParts{}))
{ }
Y_FORCE_INLINE TSharedRefArray& TSharedRefArray::operator=(const TSharedRefArray& other)
{
Impl_ = other.Impl_;
return *this;
}
Y_FORCE_INLINE TSharedRefArray& TSharedRefArray::operator=(TSharedRefArray&& other)
{
Impl_ = std::move(other.Impl_);
return *this;
}
Y_FORCE_INLINE void TSharedRefArray::Reset()
{
Impl_.Reset();
}
Y_FORCE_INLINE TSharedRefArray::operator bool() const
{
return Impl_.operator bool();
}
Y_FORCE_INLINE size_t TSharedRefArray::Size() const
{
return Impl_ ? Impl_->Size() : 0;
}
Y_FORCE_INLINE size_t TSharedRefArray::size() const
{
return Impl_ ? Impl_->Size() : 0;
}
Y_FORCE_INLINE bool TSharedRefArray::Empty() const
{
return Impl_ ? Impl_->Empty() : true;
}
Y_FORCE_INLINE const TSharedRef& TSharedRefArray::operator[](size_t index) const
{
YT_ASSERT(Impl_);
return (*Impl_)[index];
}
Y_FORCE_INLINE const TSharedRef* TSharedRefArray::Begin() const
{
return Impl_ ? Impl_->Begin() : nullptr;
}
Y_FORCE_INLINE const TSharedRef* TSharedRefArray::End() const
{
return Impl_ ? Impl_->End() : nullptr;
}
template <class... As>
TSharedRefArrayImplPtr TSharedRefArray::NewImpl(
size_t size,
size_t poolCapacity,
TRefCountedTypeCookie tagCookie,
As&&... args)
{
auto extraSpaceSize = sizeof (TSharedRef) * size + poolCapacity;
return NewWithExtraSpace<TSharedRefArrayImpl>(
extraSpaceSize,
extraSpaceSize,
tagCookie,
std::forward<As>(args)...);
}
Y_FORCE_INLINE const TSharedRef* begin(const TSharedRefArray& array)
{
return array.Begin();
}
Y_FORCE_INLINE const TSharedRef* end(const TSharedRefArray& array)
{
return array.End();
}
////////////////////////////////////////////////////////////////////////////////
} // namespace NYT
|