1 /**
2  * Contains the internal GC interface.
3  *
4  * Copyright: Copyright Digital Mars 2016.
5  * License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
6  * Authors:   Walter Bright, Sean Kelly, Jeremy DeHaan
7  */
8 
9  /*          Copyright Digital Mars 2016.
10  * Distributed under the Boost Software License, Version 1.0.
11  *    (See accompanying file LICENSE or copy at
12  *          http://www.boost.org/LICENSE_1_0.txt)
13  */
14 module core.gc.gcinterface;
15 
16 static import core.memory;
17 alias BlkAttr = core.memory.GC.BlkAttr;
18 alias BlkInfo = core.memory.GC.BlkInfo;
19 
20 alias RootIterator = int delegate(scope int delegate(ref Root) nothrow dg);
21 alias RangeIterator = int delegate(scope int delegate(ref Range) nothrow dg);
22 
23 
24 struct Root
25 {
26     void* proot;
27     alias proot this;
28 }
29 
30 struct Range
31 {
32     void* pbot;
33     void* ptop;
34     TypeInfo ti; // should be tail const, but doesn't exist for references
35     alias pbot this; // only consider pbot for relative ordering (opCmp)
36     bool opEquals(const scope Range rhs) nothrow const { return pbot == rhs.pbot; }
37 }
38 
39 interface GC
40 {
41     /**
42      *
43      */
44     void enable();
45 
46     /**
47      *
48      */
49     void disable();
50 
51     /**
52      *
53      */
54     void collect() nothrow;
55 
56     /**
57      *
58      */
59     void collectNoStack() nothrow;
60 
61     /**
62      * minimize free space usage
63      */
64     void minimize() nothrow;
65 
66     /**
67      *
68      */
69     uint getAttr(void* p) nothrow;
70 
71     /**
72      *
73      */
74     uint setAttr(void* p, uint mask) nothrow;
75 
76     /**
77      *
78      */
79     uint clrAttr(void* p, uint mask) nothrow;
80 
81     /**
82      *
83      */
84     void* malloc(size_t size, uint bits, const TypeInfo ti) nothrow;
85 
86     /*
87      *
88      */
89     BlkInfo qalloc(size_t size, uint bits, const scope TypeInfo ti) nothrow;
90 
91     /*
92      *
93      */
94     void* calloc(size_t size, uint bits, const TypeInfo ti) nothrow;
95 
96     /*
97      *
98      */
99     void* realloc(void* p, size_t size, uint bits, const TypeInfo ti) nothrow;
100 
101     /**
102      * Attempt to in-place enlarge the memory block pointed to by p by at least
103      * minsize bytes, up to a maximum of maxsize additional bytes.
104      * This does not attempt to move the memory block (like realloc() does).
105      *
106      * Returns:
107      *  0 if could not extend p,
108      *  total size of entire memory block if successful.
109      */
110     size_t extend(void* p, size_t minsize, size_t maxsize, const TypeInfo ti) nothrow;
111 
112     /**
113      *
114      */
115     size_t reserve(size_t size) nothrow;
116 
117     /**
118      *
119      */
120     void free(void* p) nothrow @nogc;
121 
122     /**
123      * Determine the base address of the block containing p.  If p is not a gc
124      * allocated pointer, return null.
125      */
126     void* addrOf(void* p) nothrow @nogc;
127 
128     /**
129      * Determine the allocated size of pointer p.  If p is an interior pointer
130      * or not a gc allocated pointer, return 0.
131      */
132     size_t sizeOf(void* p) nothrow @nogc;
133 
134     /**
135      * Determine the base address of the block containing p.  If p is not a gc
136      * allocated pointer, return null.
137      */
138     BlkInfo query(void* p) nothrow;
139 
140     /**
141      * Retrieve statistics about garbage collection.
142      * Useful for debugging and tuning.
143      */
144     core.memory.GC.Stats stats() @safe nothrow @nogc;
145 
146     /**
147      * Retrieve profile statistics about garbage collection.
148      * Useful for debugging and tuning.
149      */
150     core.memory.GC.ProfileStats profileStats() @safe nothrow @nogc;
151 
152     /**
153      * add p to list of roots
154      */
155     void addRoot(void* p) nothrow @nogc;
156 
157     /**
158      * remove p from list of roots
159      */
160     void removeRoot(void* p) nothrow @nogc;
161 
162     /**
163      *
164      */
165     @property RootIterator rootIter() @nogc;
166 
167     /**
168      * add range to scan for roots
169      */
170     void addRange(void* p, size_t sz, const TypeInfo ti) nothrow @nogc;
171 
172     /**
173      * remove range
174      */
175     void removeRange(void* p) nothrow @nogc;
176 
177     /**
178      *
179      */
180     @property RangeIterator rangeIter() @nogc;
181 
182     /**
183      * run finalizers
184      */
185     void runFinalizers(const scope void[] segment) nothrow;
186 
187     /*
188      *
189      */
190     bool inFinalizer() nothrow @nogc @safe;
191 
192     /**
193      * Returns the number of bytes allocated for the current thread
194      * since program start. It is the same as
195      * GC.stats().allocatedInCurrentThread, but faster.
196      */
197     ulong allocatedInCurrentThread() nothrow;
198 }