-void GC_detect_memory_needs(size_t size) {
- if (size > (heapActive->size - heapActiveUsedSize)) {
- GC_collect();
- if (heapActive->size - heapActiveUsedSize > heapActive->size / 2 && heapActive->size * 3 / 4 > HEAP_ACTIVE_SIZE_MIN) {
- GC_set_heap_size(heapActive->size * 3 / 4);
- GC_collect();
- GC_set_heap_size(heapActive->size);
- }
- }
- if (size > (heapActive->size - heapActiveUsedSize)) {
- int try_size = heapInactive->size * 2;
- while (size > (try_size - heapActiveUsedSize)) {
- try_size *= 2;
- }
- GC_set_heap_size(try_size);
- GC_collect();
- GC_set_heap_size(heapActive->size);
+void Nit_gc_print_usage(void) {
+#if __STDC_VERSION >= 199901L
+ /* If we are compiling with standard C99 or more recent, we can use %zu. */
+ printf("GC: Size %zu usage %zu (%.2f%%)\n", gc_heap_size, gc_used_size, 100.0*gc_used_size/gc_heap_size);
+#else
+ /* We are not compiling with a standard that allows us to use %zu, let's cast the two unsigned integers into the biggest we can !*/
+ printf("GC: Size %lu usage %lu (%.2f%%)\n", (unsigned long)gc_heap_size, (unsigned long)gc_used_size, 100.0*gc_used_size/gc_heap_size);
+#endif
+}
+
+/** Enlarge the heap and collect dead objects. Can also shrink the heap.
+ * Size is the aditionnal number of bytes required. */
+static void GC_enlarge_and_collect(size_t size) {
+ size_t try_size;
+
+ /* Heap grows exponentially. */
+ try_size = gc_heap_size;
+ while (size > (try_size - gc_used_size)) {
+ try_size = try_size * 1.5;