mmapalloc.cpp 844 B

123456789101112131415161718192021222324252627282930313233
  1. #include "alloc.h"
  2. #include "mmapalloc.h"
  3. #include <util/system/filemap.h>
  4. #include <util/generic/singleton.h>
  5. namespace {
  6. class TMmapAllocator: public IAllocator {
  7. public:
  8. TBlock Allocate(size_t len) override {
  9. TMappedAllocation m(len + sizeof(TMappedAllocation));
  10. TMappedAllocation* real = (TMappedAllocation*)m.Data();
  11. (new (real) TMappedAllocation(0))->swap(m);
  12. TBlock ret = {real + 1, len};
  13. return ret;
  14. }
  15. void Release(const TBlock& block) override {
  16. TMappedAllocation tmp(0);
  17. TMappedAllocation* real = ((TMappedAllocation*)block.Data) - 1;
  18. real->swap(tmp);
  19. real->~TMappedAllocation();
  20. }
  21. };
  22. }
  23. IAllocator* MmapAllocator() {
  24. return SingletonWithPriority<TMmapAllocator, 0>();
  25. }