Skip to content

Commit

Permalink
all: fix compilation with -gc boehm_leak (vlang#570)
Browse files Browse the repository at this point in the history
Co-authored-by: lmp <[email protected]>
  • Loading branch information
spytheman and larpon committed Sep 26, 2023
1 parent d71e5dd commit 4ee6d24
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
60 changes: 60 additions & 0 deletions c/memory.c.v
Original file line number Diff line number Diff line change
@@ -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.')
}
}
8 changes: 0 additions & 8 deletions c/sdl.c.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 <SDL.h>
9 changes: 0 additions & 9 deletions c/v_gc_sdl_boehm.c

This file was deleted.

0 comments on commit 4ee6d24

Please sign in to comment.