diff options
Diffstat (limited to 'skia/ext/SkMemory_new_handler.cpp')
-rw-r--r-- | skia/ext/SkMemory_new_handler.cpp | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/skia/ext/SkMemory_new_handler.cpp b/skia/ext/SkMemory_new_handler.cpp new file mode 100644 index 0000000..7e45d26 --- /dev/null +++ b/skia/ext/SkMemory_new_handler.cpp @@ -0,0 +1,58 @@ +#include "SkTypes.h" +#include <stdio.h> +#include <stdlib.h> +#include <new> + +// This implementation of sk_malloc_flags() and friends is identical +// to SkMemory_malloc.c, except that it disables the CRT's new_handler +// during malloc(), when SK_MALLOC_THROW is not set (ie., when +// sk_malloc_flags() would not abort on NULL). + +void sk_throw() { + SkASSERT(!"sk_throw"); + abort(); +} + +void sk_out_of_memory(void) { + SkASSERT(!"sk_out_of_memory"); + abort(); +} + +void* sk_malloc_throw(size_t size) { + return sk_malloc_flags(size, SK_MALLOC_THROW); +} + +void* sk_realloc_throw(void* addr, size_t size) { + void* p = realloc(addr, size); + if (size == 0) { + return p; + } + if (p == NULL) { + sk_throw(); + } + return p; +} + +void sk_free(void* p) { + if (p) { + free(p); + } +} + +void* sk_malloc_flags(size_t size, unsigned flags) { + std::new_handler old_handler; + if (!(flags & SK_MALLOC_THROW)) { + old_handler = std::set_new_handler(NULL); + } + void* p = malloc(size); + if (!(flags & SK_MALLOC_THROW)) { + std::set_new_handler(old_handler); + } + if (p == NULL) { + if (flags & SK_MALLOC_THROW) { + sk_throw(); + } + } + return p; +} + |