1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/ports/SkMemory_malloc.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,59 @@ 1.4 + 1.5 +/* 1.6 + * Copyright 2011 Google Inc. 1.7 + * 1.8 + * Use of this source code is governed by a BSD-style license that can be 1.9 + * found in the LICENSE file. 1.10 + */ 1.11 +#include "SkTypes.h" 1.12 +#include <stdio.h> 1.13 +#include <stdlib.h> 1.14 + 1.15 +static inline void* throw_on_failure(size_t size, void* p) { 1.16 + if (size > 0 && p == NULL) { 1.17 + // If we've got a NULL here, the only reason we should have failed is running out of RAM. 1.18 + sk_out_of_memory(); 1.19 + } 1.20 + return p; 1.21 +} 1.22 + 1.23 +void sk_throw() { 1.24 + SkDEBUGFAIL("sk_throw"); 1.25 + abort(); 1.26 +} 1.27 + 1.28 +void sk_out_of_memory(void) { 1.29 + SkDEBUGFAIL("sk_out_of_memory"); 1.30 + abort(); 1.31 +} 1.32 + 1.33 +void* sk_malloc_throw(size_t size) { 1.34 + return sk_malloc_flags(size, SK_MALLOC_THROW); 1.35 +} 1.36 + 1.37 +void* sk_realloc_throw(void* addr, size_t size) { 1.38 + return throw_on_failure(size, realloc(addr, size)); 1.39 +} 1.40 + 1.41 +void sk_free(void* p) { 1.42 + if (p) { 1.43 + free(p); 1.44 + } 1.45 +} 1.46 + 1.47 +void* sk_malloc_flags(size_t size, unsigned flags) { 1.48 + void* p = malloc(size); 1.49 + if (flags & SK_MALLOC_THROW) { 1.50 + return throw_on_failure(size, p); 1.51 + } else { 1.52 + return p; 1.53 + } 1.54 +} 1.55 + 1.56 +void* sk_calloc(size_t size) { 1.57 + return calloc(size, 1); 1.58 +} 1.59 + 1.60 +void* sk_calloc_throw(size_t size) { 1.61 + return throw_on_failure(size, sk_calloc(size)); 1.62 +}