1 /**
2  * Contains the internal GC interface.
3  *
4  * Copyright: Copyright Digital Mars 2016.
5  * License:   $(WEB 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 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 }
37 
38 interface GC
39 {
40 
41     /*
42      *
43      */
44     void Dtor();
45 
46     /**
47      *
48      */
49     void enable();
50 
51     /**
52      *
53      */
54     void disable();
55 
56     /**
57      *
58      */
59     void collect() nothrow;
60 
61     /**
62      *
63      */
64     void collectNoStack() nothrow;
65 
66     /**
67      * minimize free space usage
68      */
69     void minimize() nothrow;
70 
71     /**
72      *
73      */
74     uint getAttr(void* p) nothrow;
75 
76     /**
77      *
78      */
79     uint setAttr(void* p, uint mask) nothrow;
80 
81     /**
82      *
83      */
84     uint clrAttr(void* p, uint mask) nothrow;
85 
86     /**
87      *
88      */
89     void* malloc(size_t size, uint bits, const TypeInfo ti) nothrow;
90 
91     /*
92      *
93      */
94     BlkInfo qalloc(size_t size, uint bits, const TypeInfo ti) nothrow;
95 
96     /*
97      *
98      */
99     void* calloc(size_t size, uint bits, const TypeInfo ti) nothrow;
100 
101     /*
102      *
103      */
104     void* realloc(void* p, size_t size, uint bits, const TypeInfo ti) nothrow;
105 
106     /**
107      * Attempt to in-place enlarge the memory block pointed to by p by at least
108      * minsize bytes, up to a maximum of maxsize additional bytes.
109      * This does not attempt to move the memory block (like realloc() does).
110      *
111      * Returns:
112      *  0 if could not extend p,
113      *  total size of entire memory block if successful.
114      */
115     size_t extend(void* p, size_t minsize, size_t maxsize, const TypeInfo ti) nothrow;
116 
117     /**
118      *
119      */
120     size_t reserve(size_t size) nothrow;
121 
122     /**
123      *
124      */
125     void free(void* p) nothrow;
126 
127     /**
128      * Determine the base address of the block containing p.  If p is not a gc
129      * allocated pointer, return null.
130      */
131     void* addrOf(void* p) nothrow;
132 
133     /**
134      * Determine the allocated size of pointer p.  If p is an interior pointer
135      * or not a gc allocated pointer, return 0.
136      */
137     size_t sizeOf(void* p) nothrow;
138 
139     /**
140      * Determine the base address of the block containing p.  If p is not a gc
141      * allocated pointer, return null.
142      */
143     BlkInfo query(void* p) nothrow;
144 
145     /**
146      * Retrieve statistics about garbage collection.
147      * Useful for debugging and tuning.
148      */
149     core.memory.GC.Stats stats() nothrow;
150 
151     /**
152      * add p to list of roots
153      */
154     void addRoot(void* p) nothrow @nogc;
155 
156     /**
157      * remove p from list of roots
158      */
159     void removeRoot(void* p) nothrow @nogc;
160 
161     /**
162      *
163      */
164     @property RootIterator rootIter() @nogc;
165 
166     /**
167      * add range to scan for roots
168      */
169     void addRange(void* p, size_t sz, const TypeInfo ti) nothrow @nogc;
170 
171     /**
172      * remove range
173      */
174     void removeRange(void* p) nothrow @nogc;
175 
176     /**
177      *
178      */
179     @property RangeIterator rangeIter() @nogc;
180 
181     /**
182      * run finalizers
183      */
184     void runFinalizers(in void[] segment) nothrow;
185 
186     /*
187      *
188      */
189     bool inFinalizer() nothrow;
190 }
191