1 module awebview.wrapper.cpp.mmanager;
2 
3 
4 mixin template Awesomium()
5 {
6 
7 }
8 
9 
10 mixin template Awesomium4D()
11 {
12     void deleteFromMemoryManager(ulong id) @nogc nothrow
13     {
14         MemoryManager.instance.unregister(id);
15     }
16 }
17 
18 
19 class MemoryManager
20 {
21     this(uint nodeLen = 1024)
22     {
23         _nodeLen = nodeLen;
24         appendNewNode();
25     }
26 
27 
28     ulong register(const(void)* p)
29     {
30         auto bg = _nodes.ptr;
31         const ed = _nodes[$ .. $].ptr;
32         while(bg != ed && bg.cnt == _nodeLen)
33             ++bg;
34 
35         if(bg == ed){
36             appendNewNode();
37             bg = _nodes[$-1 .. $].ptr;
38         }
39 
40         auto bg2 = bg.ptbl.ptr;
41         const ed2 = bg.ptbl[$ .. $].ptr;
42         while(bg2 != ed2 && *bg2 != null)
43             ++bg2;
44 
45         import std..string;
46         assert(bg2 != ed2);
47         *bg2 = p;
48         ++bg.cnt;
49         return ((cast(ulong)(bg - _nodes.ptr)) << 32) | (bg2 - bg.ptbl.ptr + 1);
50     }
51 
52 
53     void unregister(ulong id) @nogc nothrow
54     {
55         if(id == 0) return;
56 
57         uint nodeIdx = id >> 32;
58         uint pIdx = cast(uint)(id & uint.max) - 1;
59 
60         --_nodes[nodeIdx].cnt;
61         _nodes[nodeIdx].ptbl[pIdx] = null;
62     }
63 
64 
65     static MemoryManager instance() @property @nogc nothrow
66     {
67         return _inst;
68     }
69 
70 
71   private:
72     void appendNewNode()
73     {
74         _nodes ~= Node(new const(void)*[_nodeLen], 0);
75     }
76 
77     static struct Node {
78         const(void)*[] ptbl;
79         uint cnt;
80     }
81 
82     Node[] _nodes;
83     immutable(uint) _nodeLen;
84 
85     static MemoryManager _inst;
86 
87     static this()
88     {
89         _inst = new MemoryManager();
90     }
91 }