The check to see if an item was already on the heap was randomly hitting.
[jitcs.git] / include / jitcs_memmgr.h
bloba79ff0fb869c3c84b38e6bd2bf95cd0412a84d5b
1 //===-- jitcs_memmgr.h - A memory manager for code and data -----*- C++ -*-===//
2 //
3 // The manager is an allocator based on Doug Leah's malloc. Memory is allocated
4 // from the system with execution permission.
5 // TODO:
6 //
7 //===----------------------------------------------------------------------===//
9 #ifndef _JITCS_MEMMGR_H_
10 #define _JITCS_MEMMGR_H_
12 #include "jitcs_base.h"
13 #include "jitcs_adt_refcounter.h"
14 #include "jitcs_adt_slice.h"
15 #include <memory>
17 namespace jitcs {
18 class MemoryMgrImpl;
20 class MemoryMgr {
21 public:
22 struct CodeAndData {
23 Slice<u8> data, code;
25 public:
26 static RefCounter<MemoryMgr> Create();
27 protected:
28 MemoryMgr();
29 ~MemoryMgr();
31 public:
32 /* todo
33 * allocate area for function relevant constants (aligned to 16 byte)
34 * allocate area for code (aligned to cache line size, currently 64 byte)
35 * allocated areas are stored in a function object, function objects can be forced to free allocated storage
37 CodeAndData allocate(size_t data, size_t code);
38 CodeAndData shortenAllocation(CodeAndData, size_t data, size_t code);
39 void deallocate(CodeAndData);
41 bool check();
42 size_t getTotalSpace();
43 size_t getAllocatedSpace();
44 size_t getFreeSpace();
45 size_t getReachableFreeSpace();
46 bool shrink();
48 private:
49 // called from RefCounter
50 friend class RefCounter<MemoryMgr>;
51 void _incRef() { ++_refCnt; }
52 void _decRef() { if (!--_refCnt) _release(); }
53 void _release();
55 private:
56 std::unique_ptr<MemoryMgrImpl> _mgr;
57 size_t _refCnt;
60 } // end of namespace jitcs
62 #endif
63 // _JITCS_MEMMGR_H_