2 * Naive Garbage Collector (Tango/Druntime compliant) C interface.
4 * This module contains the C interface of the Naive Garbage Collector
5 * implementation to comply with the Tango/Druntime specification.
8 * Copyright: Public Domain
9 * License: Public Domain
10 * Authors: Leandro Lucarella <llucax@gmail.com>
18 import gc.gc: GC, BlkInfo;
21 import tango.stdc.stdlib;
23 /// GC implementation instance.
26 /// Dummy class for the GC lock.
32 * This is a ClassInfo instance because is an easy way to get an object
33 * instance that is not allocated in the GC heap (which is not an option
34 * for implementing the GC).
36 * This avoids using OS-specific mutex.
45 * This function initializes the thread library too. This is a requirement
46 * imposed by the current implementation, it's not in the D runtime specs.
48 * This method should be called before any other call to the GC.
50 extern (C) void gc_init()
52 lock = GCLock.classinfo;
59 * After this function is called, no other GC methods should be called, except
60 * for gc_init(), which should initialize the GC again.
62 extern (C) void gc_term()
70 * When the GC is enabled, collections can happen at any time in the program.
71 * Different implementations can trigger a collection for different reasons.
73 * gc_enable() and gc_disable() can be called recursively. The number of calls
74 * to gc_enable() should match the number of calls to gc_disable(), though.
76 * If gc_disable() is called more times than gc_enable(), the GC will stay
77 * disabled. If gc_enable() is called more times than gc_disable(), the
78 * results of this function are undefined.
80 * See_Also: gc_disable()
82 extern (C) void gc_enable()
91 * See_Also: gc_enable() for details.
93 extern (C) void gc_disable()
100 * Run a GC collection in order to free unreferenced objects.
102 * The gc_enable() and gc_disable() functions don't affect this function, the
103 * collection will happen even if the GC is disabled.
105 extern (C) void gc_collect()
112 * Minimize free space usage.
114 * This function tries to minimize the programs memory footprint returning
115 * free memory to the OS.
117 * This should be used with care, because it would impose a performance
118 * penalty for further allocations.
120 extern (C) void gc_minimize()
127 * Get the attributes of the cell pointed by ptr.
129 * Bit significance of the uint value used for block attribute passing is as
130 * follows, by position:
133 * $(LI 1: The object stored in the cell have to be finalized)
134 * $(LI 2: The cell should not be scanned for pointers)
135 * $(LI 4: The cell should not be moved during a collection)
136 * $(LI 3-15: Reserved for future use by the D standard library)
137 * $(LI 16-31: Reserved for internal use by the garbage collector and
141 * See_Also: BlkAttr, gc_setAttr(), gc_clrAttr()
143 extern (C) uint gc_getAttr(void* ptr)
146 return gc.getAttr(ptr);
150 * Set the attributes of the memory block pointed by ptr.
152 * All bits present in attr are set, other bits are untouched. The old
153 * attributes are returned.
155 * See_Also: BlkAttr, gc_getAttr(), gc_clrAttr()
157 extern (C) uint gc_setAttr(void* ptr, uint attr)
160 return gc.setAttr(ptr, attr);
164 * Clear the attributes of the cell pointed by ptr.
166 * All bits present in attr are cleared, other bits are untouched. The old
167 * attributes are returned.
169 * See_Also: BlkAttr, gc_getAttr(), gc_setAttr()
171 extern (C) uint gc_clrAttr(void* ptr, uint attr)
174 return gc.clrAttr(ptr, attr);
178 * Allocate memory with attributes attr.
180 * See_Also: BlkAttr, gc_getAttr() for attr details.
182 extern (C) void* gc_malloc(size_t size, uint attr=0)
185 return gc.malloc(size, attr);
189 * Allocate memory (set memory to zero).
191 * Same as gc_malloc() but set the allocated memory block to zero.
193 extern (C) void* gc_calloc(size_t size, uint attr=0)
196 return gc.calloc(size, attr);
202 * This function attempts to extend the current memory block to size. If ptr
203 * is null, then this function behaves exactly the same as gc_malloc(). If
204 * size is 0, then this function behaves exactly like gc_free(). Otherwise
205 * this function can resize the memory block in-place or it can move the
206 * memory block to another address, in which case the new memory address is
207 * returned (if the memory block is not moved, the return address is the same
210 * attr are the same as malloc().
212 extern (C) void* gc_realloc(void* ptr, size_t size, uint attr=0)
215 return gc.realloc(ptr, size, attr);
219 * Attempt to in-place enlarge a memory block pointed to by ptr.
221 * The memory is enlarged to at least min_size beyond its current capacity, up
222 * to a maximum of max_size. This does not attempt to move the memory block
223 * (like gc_realloc() does).
225 * The total size of entire memory block is returned on success, 0 is returned
226 * if the memory block could not be extended.
228 extern (C) size_t gc_extend(void* ptr, size_t min_size, size_t max_size)
231 return gc.extend(ptr, min_size, max_size);
235 * Reserve memory to anticipate memory allocations.
237 * This method instructs the GC to pre-allocate at least size bytes of memory
238 * in anticipation of future gc_malloc()s.
240 * The actual number of bytes reserver are returned, or 0 on error.
242 extern (C) size_t gc_reserve(size_t size)
245 return gc.reserve(size);
249 * Free unused memory.
251 * This method tells the GC that a cell is not longer used. If the memory was
252 * actually still used the effects of this function is undefined (but memory
253 * corruption will probably happen).
255 * Note that finalizers are not called by this function. Finalizers are called
256 * by the runtime when the delete operator is used, and the delete operator
257 * calls this method through the runtime.
259 extern (C) void gc_free(void* ptr)
266 * Get the base address of an interior pointer into the GC heap.
268 * If ptr is not pointing into the GC heap null is returned.
270 extern (C) void* gc_addrOf(void* ptr)
273 return gc.addrOf(ptr);
277 * Return the real size (capacity) of the memory block pointed by ptr.
279 * ptr should be the base address of a heap allocated object, interior
280 * pointers are not supported (use gc_addrOf() if you have an interior
281 * pointer). If ptr is not the base address of a heap allocated object this
282 * function returns 0.
284 * realloc(ptr, sizeOf(ptr), attr) is guaranteed not to allocate/move memory.
286 extern (C) size_t gc_sizeOf(void* ptr)
289 return gc.sizeOf(ptr);
292 /** Get information about the memory block pointed by ptr.
294 * ptr should be the base address of a heap allocated object, interior
295 * pointers are not supported (use gc_addrOf() if you have an interior
296 * pointer). If ptr is not the base address of a heap allocated object this
297 * function returns a BlkInfo structure with all zeros.
299 * See BlkInfo for the information provided by this method.
301 extern (C) BlkInfo gc_query(void* ptr)
304 return gc.query(ptr);
307 /** Add a root pointer to the root set.
309 * This method can be used to register new root to the GC heap. This is only
310 * needed when the user has custom memory that has pointers into the GC heap
311 * (for example for interfacing with C programs, which allocates memory using
312 * malloc() directly).
314 * See_Also: gc_removeRoot(), gc_addRange(), gc_removeRange()
316 extern (C) void gc_addRoot(void* ptr)
323 * Add a root range to the root set.
325 * This method can be used to register new root range (a memory chunk that
326 * should be scanned for pointers into the GC heap). Pointers will be scanned
327 * assuming they are aligned.
329 * See_Also: gc_removeRange(), gc_addRoot(), gc_removeRoot()
331 extern (C) void gc_addRange(void* ptr, size_t size)
334 gc.addRange(ptr, size);
338 * Remove a root pointer from the root set.
340 * ptr has to be previously registered using gc_addRoot(), in other case the
341 * results of this function is undefined.
343 * See_Also: gc_addRoot(), gc_addRange(), gc_removeRange()
345 extern (C) void gc_removeRoot(void* ptr)
352 * Remove a root range from the root set.
354 * ptr has to be previously registered using gc_addRange(), in other case the
355 * results of this function is undefined.
357 * See_Also: gc_addRange(), gc_addRoot(), gc_removeRoot()
359 extern (C) void gc_removeRange(void* ptr)
365 // vim: set et sw=4 sts=4 :