2 * Naive Garbage Collector implementation.
4 * This module implements a Naive Garbage Collector. The idea behind this
5 * implementation is to document all the bookkeeping and considerations that
6 * has to be taken in order to implement a garbage collector for D.
8 * The garbage collector algorithm itself is extremely simple so focus can be
9 * held in the specifics of D, and not the algorithm. A completely naive mark
10 * and sweep algorithm is used, with a recursive mark phase. The code is
11 * extremely inefficient in order to keep the code clean and easy to read and
14 * The implementation is split in several modules to ease even more the
15 * lecture. All architecture/compiler specific code is done in the arch module,
16 * in order to avoid confusing version statements all over the places. The cell
17 * module has all the code related to the memory cells header. dynarray is
18 * another support module which holds the implementation of a simple dynamic
19 * array used to store root pointers and ranges. The list module holds a simple
20 * singly linked list (of cells) implementation to store the live and free
21 * lists. Finally, the iface module is the one with the C interface to comply
22 * with the Tango/Druntime GC specification.
24 * Copyright: Public Domain
25 * License: Public Domain
26 * Authors: Leandro Lucarella <llucax@gmail.com>
34 import gc.cell: Cell, BlkAttr, op_apply_ptr_range;
36 import gc.dynarray: DynArray;
37 import gc.arch: push_registers, pop_registers;
40 import cstdlib = tango.stdc.stdlib;
41 import cstring = tango.stdc.string;
46 * These are external functions coming from the D/Tango runtime. It's pretty
47 * intuitive what they do based on their names, for more details please
48 * refer to the functions documentation.
50 alias void delegate(void*, void*) mark_function;
51 extern (C) void onOutOfMemoryError();
52 extern (C) void rt_finalize(void* p, bool det=true);
53 extern (C) void rt_scanStaticData(mark_function mark);
54 extern (C) void thread_init();
55 extern (C) bool thread_needLock();
56 extern (C) void thread_suspendAll();
57 extern (C) void thread_resumeAll();
58 extern (C) void thread_scanAll(mark_function mark, void* stack_top=null);
61 * A range of memory that should be scanned for pointers.
63 * This object is iterable, yielding a pointer (void*) for each iteration.
68 /// Start of the memory range
71 /// End of the memory range
74 /// Iterate over a memory range applying dg to its elements
75 int opApply(int delegate(ref void*) dg)
77 return op_apply_ptr_range(this.from, this.to, dg);
87 * Information on a block of memory.
89 * This is part of the GC specification, it's used for the query() method.
91 * Standards: Tango/Druntime specs
96 /// Base address of the block
99 /// Size of the block (this is the total capacity, not the requested size)
103 * Memory block attributes
105 * See_Also: cell.BlkAttr for possible values
115 * This object contains the whole GC implementation. This is instantiated in
116 * the iface module as a global variable to provide the GC services.
118 * This implementation is designed to be extremely simple. The algorithm
119 * implemented is the most basic stop-the-world mark-sweep known.
121 * Memory is organized in cells. Each cell has a header where all the
122 * bookkeeping information is stored (like the mark bit, cell attributes,
123 * capacity, etc.), and the memory allocated for the requested memory itself.
125 * Two lists of cells are kept: free list and live list.
127 * The free list store cells known not to be referenced by the program. The
128 * live list stores cells that were referenced by the program at the end of
129 * the last collection (and just allocated cells).
131 * The root set is composed by several elements:
135 * $(LI Threads stack)
137 * $(LI Root pointers)
141 * Root pointers and ranges are user-defined.
146 * $(LI cell.Cell for the cell header layout)
147 * $(LI collect() for the main collection algorithm)
159 /// List of free cells.
162 /// List of live cells.
165 /// Single root pointers.
166 DynArray!(void*) root_pointers;
168 /// Single root pointers.
169 DynArray!(RootRange) root_ranges;
172 * "Flag" to indicate when the GC is disabled.
174 * This is a number because calls to enable() and disable() can be
175 * recursive. The number of calls to enable() should match the number of
176 * calls to disable(), though, if you want the GC to be effectively
182 * Remove the mark bit to all the live cells.
184 * This is done before starting the mark phase.
189 * $(LI collect() for the main collect algorithm)
190 * $(LI mark_all() for details on the marking phase)
195 foreach (cell; this.live_list)
200 * Marks all live data (pausing all threads)
202 * This methods start marking following all the known roots:
206 * $(LI Threads stack)
208 * $(LI Root pointers)
212 * Note that the registers are pushed into the stack to get scanned.
214 * This is the complete mark phase. The algorithm roughly does:
217 * $(LI Push registers into the stack)
218 * $(LI Pause all threads (but the current one, of course))
219 * $(LI Scan the static data)
220 * $(LI Scan all threads stack)
221 * $(LI Scan the root pointers and ranges)
222 * $(LI Resume all threads)
223 * $(LI Pop the registers from the stack)
230 * $(LI collect() for the main collect algorithm)
231 * $(LI mark() for details on the marking algorithm)
232 * $(LI sweep() for details on the sweep phase)
238 mixin (push_registers("stack_top"));
240 rt_scanStaticData(&mark_range);
241 thread_scanAll(&mark_range, stack_top);
242 foreach (ptr; this.root_pointers) {
245 foreach (range; this.root_ranges) {
246 this.mark_range(range.from, range.to);
249 mixin (pop_registers("stack_top"));
253 * Wrapper for mark() over a range, needed by some runtime functions.
255 * This function is used as a delegate to be passed to rt_scanStaticData()
256 * and thread_scanAll(), because they expects a function taking
259 * This extremely inefficient on purpose. The goal of this implementation
260 * is simplicity, nor performance.
264 * $(LI mark() for details on the marking algorithm)
267 void mark_range(void* from, void* to)
269 foreach (ptr; RootRange(from, to))
274 * Marks all cells accessible from a pointer.
276 * This is the mark algorithm itself. It's recursive and dumb as a log. No
277 * care is taken in regards to stack overflows. This is the first example
280 * Marking is done with all threads stopped.
284 * $(LI collect() for the main collect algorithm)
285 * $(LI mark_all() for details on the marking phase)
286 * $(LI sweep() for details on the sweep phase)
291 Cell* cell = Cell.from_ptr(this.addrOf(ptr));
296 if (cell.has_pointers) {
304 * Move unreferenced live objects to the free list (calling finalizers).
306 * This is the sweep phase. It's very simple, it just searches the live
307 * list and move unmarked cells to the free list. This function is in
308 * charge of calling finalizers too, through the rt_finalize() runtime
311 * Sweeping is done concurrently with the mutator threads.
315 * $(LI collect() for the main collect algorithm)
316 * $(LI mark_all() for details on the marking phase)
321 foreach (cell; this.live_list) {
323 this.live_list.unlink(cell);
324 if (cell.has_finalizer)
325 rt_finalize(cell.ptr, false);
326 this.free_list.link(cell);
337 * This initializes the thread library too, as requested by the
338 * Tango/Druntime specs.
349 * Finalization of unreferenced cells is not mandatory by the specs.
350 * This implementation guarantees that all finalizers are called, at least
351 * at program exit (i.e. at GC termination).
353 * The specs says that "objects referenced from the data segment never get
354 * collected by the GC". While this is true for this implementation,
355 * finalizers are called for objects referenced from the data segment at
358 * There could be some problems with this, in very strange situations. For
359 * a more complete discussion about the topic please take a look at the
360 * bug 2858: http://d.puremagic.com/issues/show_bug.cgi?id=2858
364 foreach (cell; this.live_list)
365 if (cell.has_finalizer)
366 rt_finalize(cell.ptr, false);
367 // Let the OS free the memory on exit.
373 * When the GC is enabled, a collection is triggered when malloc() can't
374 * find room in the free list to fulfill the requested size.
376 * enable() and disable() can be called recursively. The number of calls
377 * to enable() should match the number of calls to disable(), though, if
378 * you want the GC to be effectively enabled again.
380 * See_Also: disable()
384 assert (this.disabled > 0);
396 assert (this.disabled > 0);
400 * Run a GC collection in order to find unreferenced objects.
402 * This is the simplest stop-the-world mark-sweep algorithm ever. It first
403 * removes the mark bit from all the live cells, then it mark the cells
404 * that are reachable through the root set (static data, stack, registers
405 * and custom root), and finally sweeps the live list looking for unmarked
408 * The world is stopped only for the mark phase.
412 * $(LI mark_all() for details on the marking phase)
413 * $(LI sweep() for details on the sweep phase)
424 * Minimize free space usage.
426 * This method returns to the OS memory that is not longer used by
427 * the program. Usually calling this method manually is not
428 * necessary, because unused cells are recycled for future
429 * allocations. But if there is some small part of the program that
430 * requires a lot of memory and it's known that it won't be used
431 * further, calling this can reduce the memory footprint of the program
432 * considerably (at the expense of some performance lost in future
435 * This implementation just return to the OS all the cells in the free
440 foreach (cell; this.free_list) {
441 this.free_list.unlink(cell);
447 * Get attributes associated to the cell pointed by ptr.
449 * Attributes is a bitmap that can have theses values:
452 * $(LI 1: The object stored in the cell have to be finalized)
453 * $(LI 2: The cell should not be scanned for pointers)
454 * $(LI 4: The cell should not be moved during a collection
458 * See_Also: cell.BlkAttr, setAttr(), clrAttr()
460 uint getAttr(void* ptr)
462 auto cell = this.live_list.find(ptr);
469 * Set the attributes of the cell pointed by ptr.
471 * All bits present in attr are set, other bits are untouched. The old
472 * attributes are returned.
474 * See_Also: cell.BlkAttr, getAttr(), clrAttr()
476 uint setAttr(void* ptr, uint attr)
478 auto cell = this.live_list.find(ptr);
480 auto old = cell.attr;
488 * Clear the attributes of the cell pointed by ptr.
490 * All bits present in attr are cleared, other bits are untouched. The old
491 * attributes are returned.
493 * See_Also: cell.BlkAttr, getAttr(), setAttr()
495 uint clrAttr(void* ptr, uint attr)
497 auto cell = this.live_list.find(ptr);
499 auto old = cell.attr;
509 * This is the main allocator of the GC. The algorithm is really
510 * simple. It does a first-fit search in the free list, if no free cell is
511 * found with enough room, it runs a collection and retry (unless the GC
512 * is disabled). If there is no room still, it uses C malloc to allocate
513 * a new cell. If all that fails, then onOutOfMemoryError() runtime
514 * function is called to handle the error.
516 * attr are the attributes to associate to the new cell (see getAttr() for
519 void* malloc(size_t size, uint attr=0)
524 // Find a free cell in the free list with enough space
525 auto cell = this.free_list.pop(size);
529 // No room in the free list found, if the GC is enabled, trigger
530 // a collection and try again
531 if (!this.disabled) {
533 cell = this.free_list.pop(size);
538 // No luck still, allocate new memory
539 cell = cast(Cell*) cstdlib.malloc(size + Cell.sizeof);
544 onOutOfMemoryError();
550 if (cell.capacity == 0) // fresh cell
551 cell.capacity = size;
552 cell.attr = cast(BlkAttr) attr;
553 this.live_list.link(cell);
559 * Allocate memory (set memory to zero).
561 * Same as malloc() but set the allocated memory cell to zero.
563 void* calloc(size_t size, uint attr=0)
565 void* ptr = this.malloc(size, attr);
568 onOutOfMemoryError();
570 cstring.memset(ptr, 0, size);
578 * This implementation is very simple, if size less or equals than the
579 * cells capacity, the cell's size is changed and the same address is
580 * returned. Otherwise a new cell is allocated using malloc() (this can
581 * trigger a collection), the contents are moved and the old cell is freed.
583 * attr are the same as malloc().
585 void* realloc(void* ptr, size_t size, uint attr=0)
588 // Undercover malloc()
590 return this.malloc(size, attr);
598 auto cell = this.live_list.find(ptr);
601 // We have enough capacity already, just change the size
602 if (cell.capacity >= size) {
607 // We need to move the cell because of the lack of capacity, find
608 // a free cell with the requested capacity (at least)
609 Cell* new_cell = Cell.from_ptr(this.malloc(size));
610 assert (!(new_cell is null)); // out of memory is handled by malloc()
612 // Move cell attributes and contents
613 new_cell.attr = cell.attr;
614 cstring.memcpy(new_cell.ptr, cell.ptr, cell.size);
623 * Attempt to in-place enlarge a memory block pointed to by ptr.
625 * The memory should be enlarged to at least min_size beyond its current
626 * capacity, up to a maximum of max_size. This does not attempt to move
627 * the memory block (like realloc() does).
630 * 0 if could not extend ptr, total size of entire memory block if
633 size_t extend(void* ptr, size_t min_size, size_t max_size)
635 assert (min_size <= max_size);
636 // There is no possible extension of the capacity for this
642 * Reserve memory to anticipate memory allocations.
644 * This implementation is really dumb, a single cell is allocated with
645 * size bytes. If 2 mallocs() follows a call to reserve(size), requesting
646 * size/2 bytes each, one allocation will be done still (and half the
647 * memory of the first malloc will be wasted =) Since this is a trivial
648 * implementation, we don't care about this.
650 * The actual number of bytes reserver are returned, or 0 on error.
652 size_t reserve(size_t size)
655 auto cell = cast(Cell*) cstdlib.malloc(size + Cell.sizeof);
659 cell.capacity = size;
660 this.free_list.link(cell);
665 * Free unused memory.
667 * This method tells the GC that a cell is not longer used. The GC don't
668 * perform any connectivity check, if the cell was referenced by others,
669 * nasty things will happen (much like C/C++).
671 * Note that finalizers are not called by this method. Finalizers are
672 * called by the runtime when the delete operator is used, and the delete
673 * operator calls this method through the runtime.
680 auto cell = this.live_list.pop(ptr);
683 this.free_list.link(cell);
687 * Get the base address of an interior pointer into the GC heap.
689 * If ptr is not pointing into the GC heap null is returned.
691 void* addrOf(void* ptr)
696 bool in_range(Cell* cell)
698 return ptr >= cell.ptr && ptr < (cell.ptr + cell.size);
701 auto cell = this.live_list.find(&in_range);
709 * Return the real size (capacity) for the cell pointed by ptr.
711 * ptr should be the base address of a heap allocated object, interior
712 * pointers are not supported (use addrOf() if you have an interior
713 * pointer). If this is not true, this method returns 0.
715 * realloc(ptr, sizeOf(ptr), attr) is guaranteed not to allocate/move
718 size_t sizeOf(void* ptr)
720 auto cell = this.live_list.find(ptr);
722 return cell.capacity;
727 * Get information about the cell pointed by ptr.
729 * ptr should be the base address of a heap allocated object, interior
730 * pointers are not supported (use addrOf() if you have an interior
731 * pointer). If this is not true, this method returns BlkInfo.init.
733 * See BlkInfo for the information provided by this method.
735 BlkInfo query(void* ptr)
739 auto cell = this.live_list.find(ptr);
741 blk_info.base = cell.ptr;
742 blk_info.size = cell.capacity;
743 blk_info.attr = cell.attr;
750 * Add a root pointer to the root set.
752 * This method can be used to register new root to the GC heap. This is
753 * only needed when the user has custom memory that has pointers into the
754 * GC heap (for example for interfacing with C programs, which allocates
755 * memory using malloc() directly).
757 * See_Also: removeRoot(), addRange(), removeRange()
759 void addRoot(void* ptr)
761 this.root_pointers.append(ptr);
765 * Add a root range to the root set.
767 * This method can be used to register new root range (a memory chunk
768 * that should be scanned for pointers into the GC heap). This is
769 * only needed when the user has custom memory that has pointers into the
770 * GC heap (for example for interfacing with C programs, which allocates
771 * memory using malloc() directly).
773 * Pointers will be scanned assuming they are aligned.
775 * See_Also: removeRange(), addRoot(), removeRoot()
777 void addRange(void* ptr, size_t size)
779 this.root_ranges.append(RootRange(ptr, ptr + size));
783 * Remove a root pointer from the root set.
785 * ptr has to be previously registered using addRoot(), in other case the
786 * results of this method is undefined.
788 * See_Also: addRoot(), addRange(), removeRange()
790 void removeRoot(void* ptr)
792 this.root_pointers.remove(ptr);
796 * Remove a root range from the root set.
798 * ptr has to be previously registered using addRange(), in other case the
799 * results of this method is undefined.
801 * See_Also: addRange(), addRoot(), removeRoot()
803 void removeRange(void* ptr)
805 this.root_ranges.remove_if((ref RootRange range) {
806 return range.from is ptr;
812 // vim: set et sw=4 sts=4 :