Mercurial > projects > qtd
comparison d2/qtd/Memory.d @ 311:8674fd5f34f4 lifetime
Added d1/d2 top directories
author | maxter <spambox@d-coding.com> |
---|---|
date | Wed, 23 Dec 2009 16:17:22 +0200 |
parents | |
children | 80b52f5e97b6 |
comparison
equal
deleted
inserted
replaced
310:5bcfe9e7db7f | 311:8674fd5f34f4 |
---|---|
1 module qt.Memory; | |
2 | |
3 import | |
4 core.exception, | |
5 core.memory, | |
6 core.stdc.stdlib; | |
7 | |
8 alias void delegate(Object) DEvent; | |
9 extern(C) void rt_attachDisposeEvent(Object o, DEvent e); | |
10 extern(C) void rt_detachDisposeEvent(Object o, DEvent e); | |
11 extern(C) Object _d_toObject(void* p); | |
12 | |
13 /** | |
14 Object stack. | |
15 */ | |
16 final class StackAlloc | |
17 { | |
18 alias typeof(this) This; | |
19 private void* _data; | |
20 | |
21 private static size_t align16(size_t size) | |
22 { | |
23 return size + 16 - (size - size & ~15); | |
24 } | |
25 | |
26 /** | |
27 */ | |
28 this(size_t size) | |
29 { | |
30 _data = (new void[size]).ptr; | |
31 } | |
32 | |
33 /** | |
34 */ | |
35 void* alloc(size_t size) | |
36 { | |
37 void* res = _data; | |
38 _data += align16(size); | |
39 return res; | |
40 } | |
41 | |
42 /** | |
43 */ | |
44 void free(size_t size) | |
45 { | |
46 _data -= align16(size); | |
47 } | |
48 } | |
49 | |
50 /** | |
51 Size of the object stack. | |
52 */ | |
53 enum stackSize = 1024 * 1024; | |
54 | |
55 /** | |
56 Returns the object stack for the current thread. | |
57 */ | |
58 StackAlloc stackAlloc() | |
59 { | |
60 static StackAlloc stackAlloc; | |
61 if (!stackAlloc) | |
62 stackAlloc = new StackAlloc(stackSize); | |
63 return stackAlloc; | |
64 } | |
65 | |
66 /** | |
67 C heap allocator. | |
68 */ | |
69 struct CAlloc | |
70 { | |
71 /** | |
72 */ | |
73 static void* alloc(size_t size, uint flags = 0) | |
74 { | |
75 auto p = malloc(size); | |
76 if (!p) | |
77 onOutOfMemoryError; | |
78 return p; | |
79 } | |
80 | |
81 /** | |
82 */ | |
83 static void* realloc(void* addr, size_t size) | |
84 { | |
85 auto p = realloc(addr, size); | |
86 if (!p) | |
87 onOutOfMemoryError; | |
88 return p; | |
89 } | |
90 | |
91 /** | |
92 */ | |
93 static void free(void* p, size_t size = 0) | |
94 { | |
95 free(p); | |
96 } | |
97 } | |
98 | |
99 /** | |
100 GC heap allocator. | |
101 */ | |
102 struct GCAlloc | |
103 { | |
104 static void* alloc(size_t size, uint flags = 0) | |
105 { | |
106 return GC.malloc(size, flags); | |
107 } | |
108 | |
109 static void* realloc(void* addr, size_t size) | |
110 { | |
111 return GC.realloc(addr, size); | |
112 } | |
113 | |
114 static void free(void* addr, size_t size = 0) | |
115 { | |
116 GC.free(addr); | |
117 } | |
118 } |