mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
Wrap CMark allocator as Odin allocator
```odin
// Smaller allocation to larger allocation resize
{
// Allocated on CMark's allocator
foo := make([dynamic]int, 13)
for i in 0..<13 {
foo[i] = i
}
fmt.println("Before resize:", foo)
resize(&foo, 42)
fmt.println("After resize:", foo)
delete(foo)
}
```
This commit is contained in:
Vendored
+46
-1
@@ -8,6 +8,7 @@ package cmark
|
|||||||
|
|
||||||
import "core:c"
|
import "core:c"
|
||||||
import "core:c/libc"
|
import "core:c/libc"
|
||||||
|
import "core:runtime"
|
||||||
|
|
||||||
BINDING_VERSION :: Version_Info{major = 0, minor = 30, patch = 2}
|
BINDING_VERSION :: Version_Info{major = 0, minor = 30, patch = 2}
|
||||||
|
|
||||||
@@ -478,4 +479,48 @@ free_cstring :: proc "c" (str: cstring) {
|
|||||||
free_string :: proc "c" (s: string) {
|
free_string :: proc "c" (s: string) {
|
||||||
free_rawptr(raw_data(s))
|
free_rawptr(raw_data(s))
|
||||||
}
|
}
|
||||||
free :: proc{free_rawptr, free_cstring}
|
free :: proc{free_rawptr, free_cstring}
|
||||||
|
|
||||||
|
// Wrap CMark allocator as Odin allocator
|
||||||
|
@(private)
|
||||||
|
cmark_allocator_proc :: proc(allocator_data: rawptr, mode: runtime.Allocator_Mode,
|
||||||
|
size, alignment: int,
|
||||||
|
old_memory: rawptr, old_size: int, loc := #caller_location) -> (res: []byte, err: runtime.Allocator_Error) {
|
||||||
|
|
||||||
|
cmark_alloc := cast(^Allocator)allocator_data
|
||||||
|
switch mode {
|
||||||
|
case .Alloc:
|
||||||
|
ptr := cmark_alloc.calloc(1, c.size_t(size))
|
||||||
|
res = transmute([]byte)runtime.Raw_Slice{ptr, size}
|
||||||
|
return res, nil
|
||||||
|
|
||||||
|
case .Free:
|
||||||
|
cmark_alloc.free(old_memory)
|
||||||
|
return nil, nil
|
||||||
|
|
||||||
|
case .Free_All:
|
||||||
|
return nil, .Mode_Not_Implemented
|
||||||
|
|
||||||
|
case .Resize:
|
||||||
|
new_ptr := cmark_alloc.realloc(old_memory, c.size_t(size))
|
||||||
|
res = transmute([]byte)runtime.Raw_Slice{new_ptr, size}
|
||||||
|
if size > old_size {
|
||||||
|
runtime.mem_zero(raw_data(res[old_size:]), size - old_size)
|
||||||
|
}
|
||||||
|
return res, nil
|
||||||
|
|
||||||
|
case .Query_Features:
|
||||||
|
return nil, nil
|
||||||
|
|
||||||
|
case .Query_Info:
|
||||||
|
return nil, .Mode_Not_Implemented
|
||||||
|
}
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
get_default_mem_allocator_as_odin :: proc() -> runtime.Allocator {
|
||||||
|
return runtime.Allocator{
|
||||||
|
procedure = cmark_allocator_proc,
|
||||||
|
data = rawptr(get_default_mem_allocator()),
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user