aboutsummaryrefslogblamecommitdiffstats
path: root/util/memory/mmapalloc.cpp
blob: ec618cc8084506a93260f24f57b85d6001dd84eb (plain) (tree)
1
2
3
4
5
6
7
8
9







                                             
                                              








                                                                   
                                                    










                                                                           
#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();
        }
    };
}

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