aboutsummaryrefslogtreecommitdiffstats
path: root/util/memory/mmapalloc.cpp
blob: 03529160b4fef06524180869e007533ec72d0339 (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
#include "alloc.h"
#include "mmapalloc.h"

#include <util/system/filemap.h>
#include <util/generic/singleton.h>

namespace {
    class TMmapAllocator: public IAllocator {
    public:
        TBlock Allocate(size_t len) override {
            TMappedAllocation m(len + sizeof(TMappedAllocation));
            TMappedAllocation* real = (TMappedAllocation*)m.Data();

            (new (real) TMappedAllocation(0))->swap(m);

            TBlock ret = {real + 1, len};

            return ret;
        }

        void Release(const TBlock& block) override {
            TMappedAllocation tmp(0);
            TMappedAllocation* real = ((TMappedAllocation*)block.Data) - 1;

            real->swap(tmp);
            real->~TMappedAllocation();
        }
    };
} // namespace

IAllocator* MmapAllocator() {
    return SingletonWithPriority<TMmapAllocator, 0>();
}