v2 / vlib / sokol / memory / memory.c.v
40 lines · 33 sloc · 1.48 KB · e2e5cf8db56f3562c7baa735061690be936bdf3e
Raw
1module memory
2
3pub type FnAllocatorAlloc = fn (size usize, user_data voidptr) voidptr
4
5pub type FnAllocatorFree = fn (ptr voidptr, user_data voidptr)
6
7pub type FnLogCb = fn (const_tag &char, log_level u32, log_item_id u32, const_message_or_null &char, line_nr u32, const_filename_or_null &char, user_data voidptr)
8
9// salloc - used in the allocator structs, that the SOKOL libraries use, for allocating new memory blocks
10pub fn salloc(size usize, user_data voidptr) voidptr {
11 res := unsafe { malloc(int(size)) }
12 $if trace_sokol_memory ? {
13 eprintln('sokol.memory.salloc | user_data: ${user_data:x} | size: ${size:10} | res: ${res:x}')
14 }
15 $if trace_sokol_memory_salloc_backtrace ? {
16 print_backtrace()
17 }
18 return res
19}
20
21// sfree - used in the allocator structs, that the SOKOL libraries use, for freeing memory
22pub fn sfree(ptr voidptr, user_data voidptr) {
23 $if trace_sokol_memory ? {
24 eprintln(' sokol.memory.sfree | user_data: ${user_data:x} | ptr: ${ptr:x}')
25 }
26 $if trace_sokol_memory_sfree_backtrace ? {
27 print_backtrace()
28 }
29 unsafe { free(ptr) }
30}
31
32fn C.SOKOL_LOG(const_message &char)
33
34pub fn slog(const_tag &char, log_level u32, log_item_id u32, const_message_or_null &char, line_nr u32,
35 const_filename_or_null &char, user_data voidptr) {
36 C.fprintf(C.stderr,
37 c'sokol.memory.slog | user_data: %p, const_tag: %s, level: %d, item_id: %d, fname: %s, line: %d, message: %s\n',
38 user_data, const_tag, log_level, log_item_id, const_filename_or_null, line_nr,
39 const_message_or_null)
40}
41