aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJeroen van Rijn <Kelimion@users.noreply.github.com>2022-08-30 14:53:48 +0200
committerJeroen van Rijn <Kelimion@users.noreply.github.com>2022-08-30 14:53:48 +0200
commit4e8ce87792f82e1b0b2c239dc0be4f359522ee5c (patch)
tree74b510897a0a522eac0756838320dad126f5c907
parent2c8daa25dce25e868ca415015cf59a88863eb1e2 (diff)
[cmark] Allow wrapping context.allocator
-rw-r--r--vendor/cmark/cmark.odin39
-rw-r--r--vendor/cmark/doc.odin13
2 files changed, 47 insertions, 5 deletions
diff --git a/vendor/cmark/cmark.odin b/vendor/cmark/cmark.odin
index c91d25318..4b6519ef4 100644
--- a/vendor/cmark/cmark.odin
+++ b/vendor/cmark/cmark.odin
@@ -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} \ No newline at end of file
+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, {})
+} \ No newline at end of file
diff --git a/vendor/cmark/doc.odin b/vendor/cmark/doc.odin
index b87b25b61..350bba04c 100644
--- a/vendor/cmark/doc.odin
+++ b/vendor/cmark/doc.odin
@@ -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.
+ ```
*/ \ No newline at end of file