131
|
1 /**
|
|
2 * Part of the D programming language runtime library.
|
|
3 */
|
|
4
|
|
5 /*
|
|
6 * Copyright (C) 2004-2007 by Digital Mars, www.digitalmars.com
|
|
7 * Written by Walter Bright
|
|
8 *
|
|
9 * This software is provided 'as-is', without any express or implied
|
|
10 * warranty. In no event will the authors be held liable for any damages
|
|
11 * arising from the use of this software.
|
|
12 *
|
|
13 * Permission is granted to anyone to use this software for any purpose,
|
|
14 * including commercial applications, and to alter it and redistribute it
|
|
15 * freely, subject to the following restrictions:
|
|
16 *
|
|
17 * o The origin of this software must not be misrepresented; you must not
|
|
18 * claim that you wrote the original software. If you use this software
|
|
19 * in a product, an acknowledgment in the product documentation would be
|
|
20 * appreciated but is not required.
|
|
21 * o Altered source versions must be plainly marked as such, and must not
|
|
22 * be misrepresented as being the original software.
|
|
23 * o This notice may not be removed or altered from any source
|
|
24 * distribution.
|
|
25 */
|
|
26
|
|
27
|
|
28 // Storage allocation
|
|
29
|
|
30 module std.gc;
|
|
31
|
|
32 //debug = PRINTF;
|
|
33
|
|
34 public import std.c.stdarg;
|
|
35 public import std.c.stdlib;
|
|
36 public import std.c.string;
|
|
37 public import gcx;
|
|
38 public import std.outofmemory;
|
|
39 public import gcstats;
|
|
40 public import std.thread;
|
|
41
|
|
42 version=GCCLASS;
|
|
43
|
|
44 version (GCCLASS)
|
|
45 alias GC gc_t;
|
|
46 else
|
|
47 alias GC* gc_t;
|
|
48
|
|
49 gc_t _gc;
|
|
50
|
|
51 void addRoot(void *p) { _gc.addRoot(p); }
|
|
52 void removeRoot(void *p) { _gc.removeRoot(p); }
|
|
53 void addRange(void *pbot, void *ptop) { _gc.addRange(pbot, ptop); }
|
|
54 void removeRange(void *pbot) { _gc.removeRange(pbot); }
|
|
55 void fullCollect() { _gc.fullCollect(); }
|
|
56 void fullCollectNoStack() { _gc.fullCollectNoStack(); }
|
|
57 void genCollect() { _gc.genCollect(); }
|
|
58 void minimize() { _gc.minimize(); }
|
|
59 void disable() { _gc.disable(); }
|
|
60 void enable() { _gc.enable(); }
|
|
61 void getStats(out GCStats stats) { _gc.getStats(stats); }
|
|
62 void hasPointers(void* p) { _gc.hasPointers(p); }
|
|
63 void hasNoPointers(void* p) { _gc.hasNoPointers(p); }
|
|
64 void setV1_0() { _gc.setV1_0(); }
|
|
65
|
|
66 void[] malloc(size_t nbytes)
|
|
67 {
|
|
68 void* p = _gc.malloc(nbytes);
|
|
69 return p[0 .. nbytes];
|
|
70 }
|
|
71
|
|
72 void[] realloc(void* p, size_t nbytes)
|
|
73 {
|
|
74 void* q = _gc.realloc(p, nbytes);
|
|
75 return q[0 .. nbytes];
|
|
76 }
|
|
77
|
|
78 size_t extend(void* p, size_t minbytes, size_t maxbytes)
|
|
79 {
|
|
80 return _gc.extend(p, minbytes, maxbytes);
|
|
81 }
|
|
82
|
|
83 size_t capacity(void* p)
|
|
84 {
|
|
85 return _gc.capacity(p);
|
|
86 }
|
|
87
|
|
88 void setTypeInfo(TypeInfo ti, void* p)
|
|
89 {
|
|
90 if (ti.flags() & 1)
|
|
91 hasNoPointers(p);
|
|
92 else
|
|
93 hasPointers(p);
|
|
94 }
|
|
95
|
|
96 void* getGCHandle()
|
|
97 {
|
|
98 return cast(void*)_gc;
|
|
99 }
|
|
100
|
|
101 void setGCHandle(void* p)
|
|
102 {
|
|
103 void* oldp = getGCHandle();
|
|
104 gc_t g = cast(gc_t)p;
|
|
105 if (g.gcversion != gcx.GCVERSION)
|
|
106 throw new Error("incompatible gc versions");
|
|
107
|
|
108 // Add our static data to the new gc
|
|
109 GC.scanStaticData(g);
|
|
110
|
|
111 _gc = g;
|
|
112 // return oldp;
|
|
113 }
|
|
114
|
|
115 void endGCHandle()
|
|
116 {
|
|
117 GC.unscanStaticData(_gc);
|
|
118 }
|
|
119
|
|
120 extern (C)
|
|
121 {
|
|
122
|
|
123 void _d_monitorrelease(Object h);
|
|
124
|
|
125
|
|
126 void gc_init()
|
|
127 {
|
|
128 version (GCCLASS)
|
|
129 { void* p;
|
|
130 ClassInfo ci = GC.classinfo;
|
|
131
|
|
132 p = std.c.stdlib.malloc(ci.init.length);
|
|
133 (cast(byte*)p)[0 .. ci.init.length] = ci.init[];
|
|
134 _gc = cast(GC)p;
|
|
135 }
|
|
136 else
|
|
137 {
|
|
138 _gc = cast(GC *) std.c.stdlib.calloc(1, GC.sizeof);
|
|
139 }
|
|
140 _gc.initialize();
|
|
141 GC.scanStaticData(_gc);
|
|
142 std.thread.Thread.thread_init();
|
|
143 }
|
|
144
|
|
145 void gc_term()
|
|
146 {
|
|
147 _gc.fullCollectNoStack();
|
|
148 _gc.Dtor();
|
|
149 }
|
|
150
|
|
151 }
|