aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/filemap.h
blob: 3f80fe1ded05b52d9fb74aabbfef8e52fceb9ef0 (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
#pragma once

#include "file.h"
#include "align.h"
#include "yassert.h"

#include <util/generic/noncopyable.h>
#include <util/generic/ptr.h>
#include <util/generic/utility.h>
#include <util/generic/yexception.h>
#include <util/generic/flags.h>
#include <util/generic/string.h> 

#include <new>
#include <cstdio>

namespace NPrivate {
    // NB: use TFileMap::Precharge() and TFileMappedArray::Prechage()
    void Precharge(const void* data, size_t dataSize, size_t offset, size_t size);
}

struct TMemoryMapCommon {
    struct TMapResult {
        inline size_t MappedSize() const noexcept { 
            return Size - Head;
        }

        inline void* MappedData() const noexcept { 
            return Ptr ? (void*)((char*)Ptr + Head) : nullptr;
        }

        inline bool IsMapped() const noexcept { 
            return Ptr != nullptr;
        }

        inline void Reset() noexcept { 
            Ptr = nullptr;
            Size = 0;
            Head = 0;
        }

        void* Ptr;
        size_t Size;
        i32 Head;

        TMapResult(void) noexcept { 
            Reset();
        }
    };

    enum EOpenModeFlag {
        oRdOnly = 1,
        oRdWr = 2,
        oCopyOnWr = 4,

        oAccessMask = 7,
        oNotGreedy = 8,
        oPrecharge = 16,
        oPopulate = 32, // Populate page table entries (see mmap's MAP_POPULATE)
    };
    Y_DECLARE_FLAGS(EOpenMode, EOpenModeFlag)

    /**
     * Name that will be printed in exceptions if not specified.
     * Overridden by name obtained from `TFile` if it's not empty.
     */
    static TString UnknownFileName();
};
Y_DECLARE_OPERATORS_FOR_FLAGS(TMemoryMapCommon::EOpenMode)

class TMemoryMap: public TMemoryMapCommon {
public:
    explicit TMemoryMap(const TString& name); 
    explicit TMemoryMap(const TString& name, EOpenMode om); 
    TMemoryMap(const TString& name, i64 length, EOpenMode om); 
    TMemoryMap(FILE* f, TString dbgName = UnknownFileName());
    TMemoryMap(FILE* f, EOpenMode om, TString dbgName = UnknownFileName());
    TMemoryMap(const TFile& file, TString dbgName = UnknownFileName());
    TMemoryMap(const TFile& file, EOpenMode om, TString dbgName = UnknownFileName());

    ~TMemoryMap(); 

    TMapResult Map(i64 offset, size_t size);
    bool Unmap(TMapResult region);

    void ResizeAndReset(i64 size);
    TMapResult ResizeAndRemap(i64 offset, size_t size);

    i64 Length() const noexcept; 
    bool IsOpen() const noexcept; 
    bool IsWritable() const noexcept;
    EOpenMode GetMode() const noexcept;
    TFile GetFile() const noexcept; 

    void SetSequential();
    void Evict(void* ptr, size_t len);
    void Evict();

    /*
     * deprecated
     */
    bool Unmap(void* ptr, size_t size);

private:
    class TImpl;
    TSimpleIntrusivePtr<TImpl> Impl_;
};

class TFileMap: public TMemoryMapCommon {
public:
    TFileMap(const TMemoryMap& map) noexcept; 
    TFileMap(const TString& name); 
    TFileMap(const TString& name, EOpenMode om); 
    TFileMap(const TString& name, i64 length, EOpenMode om); 
    TFileMap(FILE* f, EOpenMode om = oRdOnly, TString dbgName = UnknownFileName());
    TFileMap(const TFile& file, EOpenMode om = oRdOnly, TString dbgName = UnknownFileName());
    TFileMap(const TFileMap& fm) noexcept; 

    ~TFileMap(); 

    TMapResult Map(i64 offset, size_t size);
    TMapResult ResizeAndRemap(i64 offset, size_t size);
    void Unmap();

    void Flush(void* ptr, size_t size) {
        Flush(ptr, size, true);
    }

    void Flush() {
        Flush(Ptr(), MappedSize());
    }

    void FlushAsync(void* ptr, size_t size) {
        Flush(ptr, size, false);
    }

    void FlushAsync() {
        FlushAsync(Ptr(), MappedSize());
    }

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

    inline bool IsOpen() const noexcept { 
        return Map_.IsOpen();
    }

    inline bool IsWritable() const noexcept {
        return Map_.IsWritable();
    }

    EOpenMode GetMode() const noexcept {
        return Map_.GetMode();
    }

    inline void* Ptr() const noexcept { 
        return Region_.MappedData();
    }

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

    TFile GetFile() const noexcept { 
        return Map_.GetFile();
    }

    void Precharge(size_t pos = 0, size_t size = (size_t)-1) const;

    void SetSequential() {
        Map_.SetSequential();
    }

    void Evict() {
        Map_.Evict();
    }

private:
    void Flush(void* ptr, size_t size, bool sync);

    TMemoryMap Map_;
    TMapResult Region_;
};

template <class T>
class TFileMappedArray {
private:
    const T* Ptr_;
    const T* End_;
    size_t Size_;
    char DummyData_[sizeof(T) + PLATFORM_DATA_ALIGN];
    mutable THolder<T, TDestructor> Dummy_;
    THolder<TFileMap> DataHolder_;

public:
    TFileMappedArray()
        : Ptr_(nullptr)
        , End_(nullptr)
        , Size_(0)
    {
    }
    ~TFileMappedArray() {
        Ptr_ = nullptr;
        End_ = nullptr;
    }
    void Init(const char* name) {
        DataHolder_.Reset(new TFileMap(name));
        DoInit(name);
    }
    void Init(const TFileMap& fileMap) {
        DataHolder_.Reset(new TFileMap(fileMap));
        DoInit(fileMap.GetFile().GetName());
    }
    void Term() {
        DataHolder_.Destroy();
        Ptr_ = nullptr;
        Size_ = 0;
        End_ = nullptr;
    }
    void Precharge() {
        DataHolder_->Precharge();
    }
    const T& operator[](size_t pos) const {
        Y_ASSERT(pos < size());
        return Ptr_[pos];
    }
    /// for STL compatibility only, Size() usage is recommended
    size_t size() const {
        return Size_;
    }
    size_t Size() const {
        return Size_;
    }
    const T& GetAt(size_t pos) const {
        if (pos < Size_)
            return Ptr_[pos];
        return Dummy();
    }
    void SetDummy(const T& n_Dummy) {
        Dummy_.Destroy();
        Dummy_.Reset(new (DummyData()) T(n_Dummy));
    }
    inline char* DummyData() const noexcept { 
        return AlignUp((char*)DummyData_);
    }
    inline const T& Dummy() const {
        if (!Dummy_) {
            Dummy_.Reset(new (DummyData()) T());
        }

        return *Dummy_;
    }
    /// for STL compatibility only, Empty() usage is recommended
    Y_PURE_FUNCTION bool empty() const noexcept {
        return Empty();
    }

    Y_PURE_FUNCTION bool Empty() const noexcept {
        return 0 == Size_;
    }
    /// for STL compatibility only, Begin() usage is recommended
    const T* begin() const noexcept { 
        return Begin();
    }
    const T* Begin() const noexcept { 
        return Ptr_;
    }
    /// for STL compatibility only, End() usage is recommended
    const T* end() const noexcept { 
        return End_;
    }
    const T* End() const noexcept { 
        return End_;
    }

private:
    void DoInit(const TString& fileName) { 
        DataHolder_->Map(0, DataHolder_->Length());
        if (DataHolder_->Length() % sizeof(T)) {
            Term();
            ythrow yexception() << "Incorrect size of file " << fileName.Quote();
        }
        Ptr_ = (const T*)DataHolder_->Ptr();
        Size_ = DataHolder_->Length() / sizeof(T);
        End_ = Ptr_ + Size_;
    }
};

class TMappedAllocation: TMoveOnly {
public:
    TMappedAllocation(size_t size = 0, bool shared = false, void* addr = nullptr);
    ~TMappedAllocation() {
        Dealloc();
    }
    TMappedAllocation(TMappedAllocation&& other) {
        this->swap(other);
    }
    TMappedAllocation& operator=(TMappedAllocation&& other) {
        this->swap(other);
        return *this;
    }
    void* Alloc(size_t size, void* addr = nullptr);
    void Dealloc();
    void* Ptr() const {
        return Ptr_;
    }
    char* Data(ui32 pos = 0) const {
        return (char*)(Ptr_ ? ((char*)Ptr_ + pos) : nullptr);
    }
    char* Begin() const noexcept { 
        return (char*)Ptr();
    }
    char* End() const noexcept { 
        return Begin() + MappedSize();
    }
    size_t MappedSize() const {
        return Size_;
    }
    void swap(TMappedAllocation& with);

private:
    void* Ptr_ = nullptr;
    size_t Size_ = 0;
    bool Shared_ = false;
#ifdef _win_
    void* Mapping_ = nullptr;
#endif
};

template <class T>
class TMappedArray: private TMappedAllocation {
public:
    TMappedArray(size_t siz = 0)
        : TMappedAllocation(0)
    {
        if (siz)
            Create(siz);
    }
    ~TMappedArray() {
        Destroy();
    }
    T* Create(size_t siz) {
        Y_ASSERT(MappedSize() == 0 && Ptr() == nullptr);
        T* arr = (T*)Alloc((sizeof(T) * siz));
        if (!arr)
            return nullptr;
        Y_ASSERT(MappedSize() == sizeof(T) * siz);
        for (size_t n = 0; n < siz; n++)
            new (&arr[n]) T();
        return arr;
    }
    void Destroy() {
        T* arr = (T*)Ptr();
        if (arr) {
            for (size_t n = 0; n < size(); n++)
                arr[n].~T();
            Dealloc();
        }
    }
    T& operator[](size_t pos) {
        Y_ASSERT(pos < size());
        return ((T*)Ptr())[pos];
    }
    const T& operator[](size_t pos) const {
        Y_ASSERT(pos < size());
        return ((T*)Ptr())[pos];
    }
    T* begin() {
        return (T*)Ptr();
    }
    T* end() {
        return (T*)((char*)Ptr() + MappedSize());
    }
    size_t size() const {
        return MappedSize() / sizeof(T);
    }
    void swap(TMappedArray<T>& with) {
        TMappedAllocation::swap(with);
    }
};