From f43be273130f5b53b1bc0e976ce487fda14c1e2e Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Tue, 26 Sep 2023 16:50:00 +0300 Subject: [PATCH] all: fix compilation with `-gc boehm_leak` (#570) Co-authored-by: lmp --- c/memory.c.v | 60 ++++++++++++++++++++++++++++++++++++++++++++++ c/sdl.c.v | 8 ------- c/v_gc_sdl_boehm.c | 9 ------- 3 files changed, 60 insertions(+), 17 deletions(-) create mode 100644 c/memory.c.v delete mode 100644 c/v_gc_sdl_boehm.c diff --git a/c/memory.c.v b/c/memory.c.v new file mode 100644 index 00000000..a675d54f --- /dev/null +++ b/c/memory.c.v @@ -0,0 +1,60 @@ +module c + +// When the Boehm collector is used, it is better to replace SDL's memory allocation functions, with versions +// that Boehm will later know how to process. The callbacks here provide such versions: +fn cb_malloc_func(size usize) voidptr { + res := unsafe { malloc(int(size)) } + $if trace_sdl_memory ? { + C.fprintf(C.stderr, c'>> sdl.c.cb_malloc_func | size: %lu | => %p\n', size, res) + } + return res +} + +fn cb_calloc_func(nmemb usize, size usize) voidptr { + res := unsafe { vcalloc(isize(nmemb) * isize(size)) } + $if trace_sdl_memory ? { + C.fprintf(C.stderr, c'>> sdl.c.cb_calloc_func | nmemb: %lu | size: %lu | => %p\n', + nmemb, size, res) + } + return res +} + +fn cb_realloc_func(mem voidptr, size usize) voidptr { + res := unsafe { v_realloc(&u8(mem), isize(size)) } + $if trace_sdl_memory ? { + C.fprintf(C.stderr, c'>> sdl.c.cb_realloc_func | mem: %p | size: %lu | => %p\n', + mem, size, res) + } + return res +} + +fn cb_free_func(mem voidptr) { + $if trace_sdl_memory ? { + C.fprintf(C.stderr, c'>> sdl.c.cb_free_func | mem: %p\n') + } + unsafe { free(mem) } +} + +pub type MallocFunc = fn (size usize) voidptr // fn(size usize) voidptr + +pub type CallocFunc = fn (nmemb usize, size usize) voidptr // fn(nmemb usize, size usize) voidptr + +pub type ReallocFunc = fn (mem voidptr, size usize) voidptr // fn(mem voidptr, size usize) voidptr + +pub type FreeFunc = fn (mem voidptr) // fn(mem voidptr) + +fn C.SDL_SetMemoryFunctions(malloc_func MallocFunc, calloc_func CallocFunc, realloc_func ReallocFunc, free_func FreeFunc) int +fn C.SDL_GetNumAllocations() int + +fn init() { + prev_allocations := C.SDL_GetNumAllocations() + if prev_allocations > 0 { + eprintln('SDL memory allocation functions should have been replaced with the V ones, but vsdl found, that you did already ${prev_allocations} allocations.') + return + } + replaced := C.SDL_SetMemoryFunctions(cb_malloc_func, cb_calloc_func, cb_realloc_func, + cb_free_func) + if replaced != 0 { + eprintln('SDL memory allocation functions were not replaced.') + } +} diff --git a/c/sdl.c.v b/c/sdl.c.v index b0bce9de..960f5a69 100644 --- a/c/sdl.c.v +++ b/c/sdl.c.v @@ -34,12 +34,4 @@ $if x64 { #flag windows -Dmain=SDL_main #flag windows -lSDL2main -lSDL2 -$if gcboehm ? { - #define SDL_malloc GC_MALLOC - #define SDL_calloc(n,m) V_GC_SDL_calloc(n, m) - #define SDL_realloc GC_REALLOC - #define SDL_free GC_FREE - #insert "@VMODROOT/c/v_gc_sdl_boehm.c" -} - #include diff --git a/c/v_gc_sdl_boehm.c b/c/v_gc_sdl_boehm.c deleted file mode 100644 index 36f0da15..00000000 --- a/c/v_gc_sdl_boehm.c +++ /dev/null @@ -1,9 +0,0 @@ -void* V_GC_SDL_calloc(size_t n, size_t size) { - #if defined(_VGCBOEHM) - { - size_t msize = n * size; - return memset(GC_MALLOC(msize), 0, msize); - } - #endif - return 0; -}