/
nv-lang
/
nova
Обзор
Документация
Войти
/
nv-lang
/
nova
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
main
compiler-codegen/nova_rt/alloc.c
75 строк
3 KB
Evgeniy Golovin
feat(269-278): bdwgc extra/gc.c amalgamation fallback + fflush before abort
02 авг 2026, 18:21
02 авг 2026, 18:21
6708fda
Код
Авторство
О чём код?
/* nova_rt/alloc.c — Phase-0 implementation: plain malloc, no GC. * To switch GC: replace this file only. The codegen never calls malloc directly. * * Contract: nova_alloc MUST return zeroed memory. Codegen assumes zero-init * (see emit_c.rs: record/closure/spawn-context fields set by assignment only). * Use calloc, not malloc. */ #include "alloc.h" #include <stdlib.h> #include <stdio.h> static size_t _alloc_count = 0; static size_t _free_count = 0; void nova_gc_init(void) { _alloc_count = 0; _free_count = 0; } void nova_gc_shutdown(void) {} void* nova_alloc(size_t size) { void* p = calloc(1, size); if (!p) { fprintf(stderr, "nova: out of memory\n"); /* #278 [M-nova-alloc-abort-no-fflush]: flush BOTH streams before * abort() — stdout is buffered (fully-buffered when redirected to a * file/pipe, the common case for test-runner children), so any * println() output the program already produced is still sitting * in libc's buffer when abort() tears the process down; without an * explicit fflush it's lost, hiding the last lines printed before * the crash and costing extra repro runs to diagnose (see #109). */ fflush(stdout); fflush(stderr); abort(); } _alloc_count++; return p; } /* Plan 83.4.5.8 (2026-05-24): uncollectable allocation. Под malloc-backend * identical to nova_alloc + free. */ void* nova_alloc_uncollectable(size_t size) { void* p = calloc(1, size); if (!p) { fprintf(stderr, "nova: out of memory (uncollectable)\n"); /* #278: see nova_alloc's matching comment above. */ fflush(stdout); fflush(stderr); abort(); } _alloc_count++; return p; } void nova_free_uncollectable(void* ptr) { if (!ptr) return; free(ptr); _free_count++; } /* Plan 152.4: no-op under malloc — nothing is collected, so static-storage * pointers never need explicit rooting. */ void nova_gc_add_root(void* lo, void* hi) { (void)lo; (void)hi; } /* RC stubs — no-ops in malloc mode (no free, so free_count stays 0). */ void nova_retain(void* ptr) { (void)ptr; } void nova_release(void* ptr) { (void)ptr; } size_t nova_gc_alloc_count(void) { return _alloc_count; } size_t nova_gc_free_count(void) { return _free_count; } size_t nova_gc_live_count(void) { return _alloc_count - _free_count; } void nova_gc_reset_stats(void) { _alloc_count = 0; _free_count = 0; } /* Plan 32: introspection — under plain malloc honest "not supported". */ size_t nova_gc_heap_size(void) { return 0; } void nova_gc_collect(void) { /* no-op: no GC to invoke */ } /* Plan 57.C.2: under malloc — нет collect-cycle, последний pause всегда 0. */ uint64_t nova_gc_last_pause_ns(void) { return 0; }