aboutsummaryrefslogtreecommitdiffstats
path: root/util/memory/blob.cpp
blob: 341071e625c4cdff10e2cc54a6229725e29b8b75 (plain) (blame)
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
#include "blob.h"
#include "addstorage.h"

#include <util/system/yassert.h>
#include <util/system/filemap.h>
#include <util/system/mlock.h>
#include <util/stream/buffer.h>
#include <util/generic/ptr.h>
#include <util/generic/string.h>
#include <util/generic/buffer.h>
#include <util/generic/ylimits.h>
#include <util/generic/singleton.h>
#include <util/generic/yexception.h>

template <class TCounter>
class TDynamicBlobBase: public TBlob::TBase,
                        public TRefCounted<TDynamicBlobBase<TCounter>, TCounter>,
                        public TAdditionalStorage<TDynamicBlobBase<TCounter>> {
    using TRefBase = TRefCounted<TDynamicBlobBase, TCounter>;

public:
    inline TDynamicBlobBase() = default;

    ~TDynamicBlobBase() override = default;

    void Ref() noexcept override {
        TRefBase::Ref();
    }

    void UnRef() noexcept override {
        TRefBase::UnRef();
    }

    inline void* Data() const noexcept {
        return this->AdditionalData();
    }

    inline size_t Length() const noexcept {
        return this->AdditionalDataLength();
    }
};

template <class TCounter>
class TBufferBlobBase: public TBlob::TBase, public TRefCounted<TBufferBlobBase<TCounter>, TCounter> {
    using TRefBase = TRefCounted<TBufferBlobBase, TCounter>;

public:
    inline TBufferBlobBase(TBuffer& buf) {
        Buf_.Swap(buf);
    }

    ~TBufferBlobBase() override = default;

    void Ref() noexcept override {
        TRefBase::Ref();
    }

    void UnRef() noexcept override {
        TRefBase::UnRef();
    }

    inline const TBuffer& Buffer() const noexcept {
        return Buf_;
    }

private:
    TBuffer Buf_;
};

template <class TCounter>
class TStringBlobBase: public TBlob::TBase, public TRefCounted<TStringBlobBase<TCounter>, TCounter> {
    using TRefBase = TRefCounted<TStringBlobBase, TCounter>;

public:
    inline TStringBlobBase(const TString& s)
        : S_(s)
    {
    }

    TStringBlobBase(TString&& s) noexcept
        : S_(std::move(s))
    {
    }

    ~TStringBlobBase() override = default;

    void Ref() noexcept override {
        TRefBase::Ref();
    }

    void UnRef() noexcept override {
        TRefBase::UnRef();
    }

    inline const TString& String() const noexcept {
        return S_;
    }

private:
    const TString S_;
};

template <class TCounter>
class TMappedBlobBase: public TBlob::TBase, public TRefCounted<TMappedBlobBase<TCounter>, TCounter> {
    using TRefBase = TRefCounted<TMappedBlobBase<TCounter>, TCounter>;

public:
    inline TMappedBlobBase(const TMemoryMap& map, ui64 offset, size_t len, EMappingMode mode)
        : Map_(map)
        , Mode_(mode)
    {
        Y_ENSURE(Map_.IsOpen(), TStringBuf("memory map not open"));

        Map_.Map(offset, len);

        if (len && !Map_.Ptr()) { // Ptr is 0 for blob of size 0
            ythrow yexception() << "can not map(" << offset << ", " << len << ")";
        }

        if (Mode_ == EMappingMode::Locked) {
            LockMemory(Data(), Length());
        }
    }

    ~TMappedBlobBase() override {
        if (Mode_ == EMappingMode::Locked && Length()) {
            UnlockMemory(Data(), Length());
        }
    }

    void Ref() noexcept override {
        TRefBase::Ref();
    }

    void UnRef() noexcept override {
        TRefBase::UnRef();
    }

    inline const void* Data() const noexcept {
        return Map_.Ptr();
    }

    inline size_t Length() const noexcept {
        return Map_.MappedSize();
    }

private:
    TFileMap Map_;
    EMappingMode Mode_;
};

TBlob TBlob::SubBlob(size_t len) const {
    /*
     * may be slightly optimized
     */

    return SubBlob(0, len);
}

TBlob TBlob::SubBlob(size_t begin, size_t end) const {
    if (begin > Length() || end > Length() || begin > end) {
        ythrow yexception() << "incorrect subblob (" << begin << ", " << end << ", outer length = " << Length() << ")";
    }

    return TBlob(Begin() + begin, end - begin, S_.Base);
}

TBlob TBlob::DeepCopy() const {
    return TBlob::Copy(Data(), Length());
}

template <class TCounter>
static inline TBlob CopyConstruct(const void* data, size_t len) {
    using Base = TDynamicBlobBase<TCounter>;
    THolder<Base> base(new (len) Base);

    Y_ASSERT(base->Length() == len);

    memcpy(base->Data(), data, len);

    TBlob ret(base->Data(), len, base.Get());
    Y_UNUSED(base.Release()); 

    return ret;
}

TBlob TBlob::CopySingleThreaded(const void* data, size_t length) {
    return CopyConstruct<TSimpleCounter>(data, length);
}

TBlob TBlob::Copy(const void* data, size_t length) {
    return CopyConstruct<TAtomicCounter>(data, length);
}

TBlob TBlob::NoCopy(const void* data, size_t length) {
    return TBlob(data, length, nullptr);
}

template <class TCounter>
static inline TBlob ConstructFromMap(const TMemoryMap& map, ui64 offset, size_t length, EMappingMode mode) {
    using TBase = TMappedBlobBase<TCounter>;
    THolder<TBase> base(new TBase(map, offset, length, mode));
    TBlob ret(base->Data(), base->Length(), base.Get());
    Y_UNUSED(base.Release()); 

    return ret;
}

template <class TCounter, class T>
static inline TBlob ConstructAsMap(const T& t, EMappingMode mode) {
    TMemoryMap::EOpenMode openMode = (mode == EMappingMode::Precharged) ? (TMemoryMap::oRdOnly | TMemoryMap::oPrecharge) : TMemoryMap::oRdOnly;

    TMemoryMap map(t, openMode);
    const ui64 toMap = map.Length();

    if (toMap > Max<size_t>()) {
        ythrow yexception() << "can not map whole file(length = " << toMap << ")";
    }

    return ConstructFromMap<TCounter>(map, 0, static_cast<size_t>(toMap), mode);
}

TBlob TBlob::FromFileSingleThreaded(const TString& path, EMappingMode mode) {
    return ConstructAsMap<TSimpleCounter>(path, mode);
}

TBlob TBlob::FromFile(const TString& path, EMappingMode mode) {
    return ConstructAsMap<TAtomicCounter>(path, mode);
}

TBlob TBlob::FromFileSingleThreaded(const TFile& file, EMappingMode mode) {
    return ConstructAsMap<TSimpleCounter>(file, mode);
}

TBlob TBlob::FromFile(const TFile& file, EMappingMode mode) {
    return ConstructAsMap<TAtomicCounter>(file, mode);
}

TBlob TBlob::FromFileSingleThreaded(const TString& path) {
    return ConstructAsMap<TSimpleCounter>(path, EMappingMode::Standard);
}

TBlob TBlob::FromFile(const TString& path) {
    return ConstructAsMap<TAtomicCounter>(path, EMappingMode::Standard);
}

TBlob TBlob::FromFileSingleThreaded(const TFile& file) {
    return ConstructAsMap<TSimpleCounter>(file, EMappingMode::Standard);
}

TBlob TBlob::FromFile(const TFile& file) {
    return ConstructAsMap<TAtomicCounter>(file, EMappingMode::Standard);
}

TBlob TBlob::PrechargedFromFileSingleThreaded(const TString& path) {
    return ConstructAsMap<TSimpleCounter>(path, EMappingMode::Precharged);
}

TBlob TBlob::PrechargedFromFile(const TString& path) {
    return ConstructAsMap<TAtomicCounter>(path, EMappingMode::Precharged);
}

TBlob TBlob::PrechargedFromFileSingleThreaded(const TFile& file) {
    return ConstructAsMap<TSimpleCounter>(file, EMappingMode::Precharged);
}

TBlob TBlob::PrechargedFromFile(const TFile& file) {
    return ConstructAsMap<TAtomicCounter>(file, EMappingMode::Precharged);
}

TBlob TBlob::LockedFromFileSingleThreaded(const TString& path) {
    return ConstructAsMap<TSimpleCounter>(path, EMappingMode::Locked);
}

TBlob TBlob::LockedFromFile(const TString& path) {
    return ConstructAsMap<TAtomicCounter>(path, EMappingMode::Locked);
}

TBlob TBlob::LockedFromFileSingleThreaded(const TFile& file) {
    return ConstructAsMap<TSimpleCounter>(file, EMappingMode::Locked);
}

TBlob TBlob::LockedFromFile(const TFile& file) {
    return ConstructAsMap<TAtomicCounter>(file, EMappingMode::Locked);
}

TBlob TBlob::LockedFromMemoryMapSingleThreaded(const TMemoryMap& map, ui64 offset, size_t length) {
    return ConstructFromMap<TSimpleCounter>(map, offset, length, EMappingMode::Locked);
}

TBlob TBlob::LockedFromMemoryMap(const TMemoryMap& map, ui64 offset, size_t length) {
    return ConstructFromMap<TAtomicCounter>(map, offset, length, EMappingMode::Locked);
}

TBlob TBlob::FromMemoryMapSingleThreaded(const TMemoryMap& map, ui64 offset, size_t length) {
    return ConstructFromMap<TSimpleCounter>(map, offset, length, EMappingMode::Standard);
}

TBlob TBlob::FromMemoryMap(const TMemoryMap& map, ui64 offset, size_t length) {
    return ConstructFromMap<TAtomicCounter>(map, offset, length, EMappingMode::Standard);
}

template <class TCounter>
static inline TBlob ReadFromFile(const TFile& file, ui64 offset, size_t length) {
    using TBase = TDynamicBlobBase<TCounter>;
    THolder<TBase> base(new (length) TBase);

    Y_ASSERT(base->Length() == length);

    file.Pload(base->Data(), length, offset);

    TBlob ret(base->Data(), length, base.Get());
    Y_UNUSED(base.Release()); 

    return ret;
}

template <class TCounter>
static inline TBlob ConstructFromFileContent(const TFile& file, ui64 offset, ui64 length) {
    if (length > Max<size_t>()) {
        ythrow yexception() << "can not read whole file(length = " << length << ")";
    }

    return ReadFromFile<TCounter>(file, offset, static_cast<size_t>(length));
}

TBlob TBlob::FromFileContentSingleThreaded(const TString& path) {
    TFile file(path, RdOnly);
    return ConstructFromFileContent<TSimpleCounter>(file, 0, file.GetLength());
}

TBlob TBlob::FromFileContent(const TString& path) {
    TFile file(path, RdOnly);
    return ConstructFromFileContent<TAtomicCounter>(file, 0, file.GetLength());
}

TBlob TBlob::FromFileContentSingleThreaded(const TFile& file) {
    return ConstructFromFileContent<TSimpleCounter>(file, 0, file.GetLength());
}

TBlob TBlob::FromFileContent(const TFile& file) {
    return ConstructFromFileContent<TAtomicCounter>(file, 0, file.GetLength());
}

TBlob TBlob::FromFileContentSingleThreaded(const TFile& file, ui64 offset, size_t length) {
    return ConstructFromFileContent<TSimpleCounter>(file, offset, length);
}

TBlob TBlob::FromFileContent(const TFile& file, ui64 offset, size_t length) {
    return ConstructFromFileContent<TAtomicCounter>(file, offset, length);
}

template <class TCounter>
static inline TBlob ConstructFromBuffer(TBuffer& in) {
    using TBase = TBufferBlobBase<TCounter>;
    THolder<TBase> base(new TBase(in));

    TBlob ret(base->Buffer().Data(), base->Buffer().Size(), base.Get());
    Y_UNUSED(base.Release()); 

    return ret;
}

template <class TCounter>
static inline TBlob ConstructFromStream(IInputStream& in) {
    TBuffer buf;

    {
        TBufferOutput out(buf);

        TransferData(&in, &out);
    }

    return ConstructFromBuffer<TCounter>(buf);
}

TBlob TBlob::FromStreamSingleThreaded(IInputStream& in) {
    return ConstructFromStream<TSimpleCounter>(in);
}

TBlob TBlob::FromStream(IInputStream& in) {
    return ConstructFromStream<TAtomicCounter>(in);
}

TBlob TBlob::FromBufferSingleThreaded(TBuffer& in) {
    return ConstructFromBuffer<TSimpleCounter>(in);
}

TBlob TBlob::FromBuffer(TBuffer& in) {
    return ConstructFromBuffer<TAtomicCounter>(in);
}

template <class TCounter, class S>
TBlob ConstructFromString(S&& s) {
    using TBase = TStringBlobBase<TCounter>;
    auto base = MakeHolder<TBase>(std::forward<S>(s));

    TBlob ret(base->String().data(), base->String().size(), base.Get());
    Y_UNUSED(base.Release()); 

    return ret;
}

TBlob TBlob::FromStringSingleThreaded(const TString& s) {
    return ConstructFromString<TSimpleCounter>(s);
}

TBlob TBlob::FromStringSingleThreaded(TString&& s) {
    return ConstructFromString<TSimpleCounter>(std::move(s));
}

TBlob TBlob::FromString(const TString& s) {
    return ConstructFromString<TAtomicCounter>(s);
}

TBlob TBlob::FromString(TString&& s) {
    return ConstructFromString<TAtomicCounter>(std::move(s));
}