This source file includes following definitions.
- throw_on_failure
- sk_throw
- sk_out_of_memory
- sk_realloc_throw
- sk_free
- sk_malloc_throw
- sk_malloc_nothrow
- sk_malloc_flags
- sk_calloc_throw
- sk_calloc
#include <stdio.h>
#include <stdlib.h>
#include <new>
#include "base/process/memory.h"
#include "third_party/skia/include/core/SkTypes.h"
#include "third_party/skia/include/core/SkThread.h"
SK_DECLARE_STATIC_MUTEX(gSkNewHandlerMutex);
static inline void* throw_on_failure(size_t size, void* p) {
if (size > 0 && p == NULL) {
sk_out_of_memory();
}
return p;
}
void sk_throw() {
SkASSERT(!"sk_throw");
abort();
}
void sk_out_of_memory(void) {
SkASSERT(!"sk_out_of_memory");
abort();
}
void* sk_realloc_throw(void* addr, size_t size) {
return throw_on_failure(size, realloc(addr, size));
}
void sk_free(void* p) {
if (p) {
free(p);
}
}
void* sk_malloc_throw(size_t size) {
return throw_on_failure(size, malloc(size));
}
static void* sk_malloc_nothrow(size_t size) {
#if defined(LIBC_GLIBC) || defined(USE_TCMALLOC) || \
(defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
void* result;
ignore_result(base::UncheckedMalloc(size, &result));
return result;
#else
SkAutoMutexAcquire lock(gSkNewHandlerMutex);
std::new_handler old_handler = std::set_new_handler(NULL);
void* p = malloc(size);
std::set_new_handler(old_handler);
return p;
#endif
}
void* sk_malloc_flags(size_t size, unsigned flags) {
if (flags & SK_MALLOC_THROW) {
return sk_malloc_throw(size);
}
return sk_malloc_nothrow(size);
}
void* sk_calloc_throw(size_t size) {
return throw_on_failure(size, calloc(size, 1));
}
void* sk_calloc(size_t size) {
#if defined(LIBC_GLIBC) || defined(USE_TCMALLOC) || \
(defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
void* result;
ignore_result(base::UncheckedCalloc(size, 1, &result));
return result;
#else
SkAutoMutexAcquire lock(gSkNewHandlerMutex);
std::new_handler old_handler = std::set_new_handler(NULL);
void* p = calloc(size, 1);
std::set_new_handler(old_handler);
return p;
#endif
}