From 42b07cc9c8d2e6251d190e5ea0d13fd66cb51e6d Mon Sep 17 00:00:00 2001 From: Vicent Marti Date: Tue, 24 May 2016 15:50:44 +0200 Subject: cmake: Global handler for OOM situations --- src/cmark.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'src/cmark.c') diff --git a/src/cmark.c b/src/cmark.c index 3491199..3288308 100644 --- a/src/cmark.c +++ b/src/cmark.c @@ -10,6 +10,36 @@ int cmark_version() { return CMARK_VERSION; } const char *cmark_version_string() { return CMARK_VERSION_STRING; } +void (*_cmark_on_oom)(void) = NULL; + +void cmark_trigger_oom(void) +{ + if (_cmark_on_oom) + _cmark_on_oom(); + abort(); +} + +void cmark_set_oom_handler(void (*handler)(void)) +{ + _cmark_on_oom = handler; +} + +void *cmark_calloc(size_t nmem, size_t size) +{ + void *ptr = calloc(nmem, size); + if (!ptr) + cmark_trigger_oom(); + return ptr; +} + +void *cmark_realloc(void *ptr, size_t size) +{ + void *ptr_new = realloc(ptr, size); + if (!ptr_new) + cmark_trigger_oom(); + return ptr_new; +} + char *cmark_markdown_to_html(const char *text, size_t len, int options) { cmark_node *doc; char *result; -- cgit v1.2.3