[cmark] Allow wrapping context.allocator

This commit is contained in:
Jeroen van Rijn
2022-08-30 14:53:48 +02:00
parent 2c8daa25dc
commit 4e8ce87792
2 changed files with 47 additions and 5 deletions

View File

@@ -8,6 +8,7 @@ package cmark
import "core:c"
import "core:c/libc"
import "core:runtime"
BINDING_VERSION :: Version_Info{major = 0, minor = 30, patch = 2}
@@ -113,9 +114,9 @@ markdown_to_html_from_string :: proc(text: string, options: Options) -> (html: s
// Custom allocator - Defines the memory allocation functions to be used by CMark
// when parsing and allocating a document tree
Allocator :: struct {
calloc: proc "c" (c.size_t, c.size_t) -> rawptr,
realloc: proc "c" (rawptr, c.size_t) -> rawptr,
free: proc "c" (rawptr),
calloc: proc "c" (num: c.size_t, size: c.size_t) -> rawptr,
realloc: proc "c" (ptr: rawptr, new_size: c.size_t) -> rawptr,
free: proc "c" (ptr: rawptr),
}
@(default_calling_convention="c", link_prefix="cmark_")
@@ -474,4 +475,34 @@ free_cstring :: proc "c" (str: cstring) {
free_string :: proc "c" (s: string) {
free_rawptr(raw_data(s))
}
free :: proc{free_rawptr, free_cstring}
free :: proc{free_rawptr, free_cstring}
// You can use the current context.allocator to make a custom allocator for CMark.
// WARNING: It needs to remain the context.allocator for any subsequent calls into this package.
make_allocator :: proc() -> (res: Allocator) {
return Allocator{
calloc = _calloc,
realloc = _realloc,
free = _free,
}
}
@(private)
_calloc :: proc "c" (num: c.size_t, size: c.size_t) -> (alloc: rawptr) {
context = runtime.default_context()
data, _ := runtime.mem_alloc(int(num * size), 2 * align_of(rawptr), context.allocator, {})
return raw_data(data)
}
@(private)
_realloc :: proc "c" (ptr: rawptr, new_size: c.size_t) -> (alloc: rawptr) {
context = runtime.default_context()
data, _ := runtime.mem_resize(ptr, {}, int(new_size), 2 * align_of(rawptr), context.allocator, {})
return raw_data(data)
}
@(private)
_free :: proc "c" (ptr: rawptr) {
context = runtime.default_context()
runtime.mem_free(ptr, context.allocator, {})
}

13
vendor/cmark/doc.odin vendored
View File

@@ -67,7 +67,6 @@ package cmark
fmt.println(html)
}
```
An iterator will walk through a tree of nodes, starting from a root
@@ -115,4 +114,16 @@ package cmark
Nodes must only be modified after an `.Exit` event, or an `.Enter` event for
leaf nodes.
Wrapping the context.allocator for CMark's use:
```odin
using cm
alloc := cm.get_default_mem_allocator()
alloc^ = cm.make_allocator()
... proceed as usual, but keep in mind that `context.allocator` can't change
for any of the calls to this package.
```
*/