Home | History | Annotate | Download | only in ports
      1 
      2 /*
      3  * Copyright 2011 Google Inc.
      4  *
      5  * Use of this source code is governed by a BSD-style license that can be
      6  * found in the LICENSE file.
      7  */
      8 #include "SkTypes.h"
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 
     12 static inline void* throw_on_failure(size_t size, void* p) {
     13     if (size > 0 && p == NULL) {
     14         // If we've got a NULL here, the only reason we should have failed is running out of RAM.
     15         sk_out_of_memory();
     16     }
     17     return p;
     18 }
     19 
     20 void sk_throw() {
     21     SkDEBUGFAIL("sk_throw");
     22     abort();
     23 }
     24 
     25 void sk_out_of_memory(void) {
     26     SkDEBUGFAIL("sk_out_of_memory");
     27     abort();
     28 }
     29 
     30 void* sk_malloc_throw(size_t size) {
     31     return sk_malloc_flags(size, SK_MALLOC_THROW);
     32 }
     33 
     34 void* sk_realloc_throw(void* addr, size_t size) {
     35     return throw_on_failure(size, realloc(addr, size));
     36 }
     37 
     38 void sk_free(void* p) {
     39     if (p) {
     40         free(p);
     41     }
     42 }
     43 
     44 void* sk_malloc_flags(size_t size, unsigned flags) {
     45     void* p = malloc(size);
     46     if (flags & SK_MALLOC_THROW) {
     47         return throw_on_failure(size, p);
     48     } else {
     49         return p;
     50     }
     51 }
     52 
     53 void* sk_calloc(size_t size) {
     54     return calloc(size, 1);
     55 }
     56 
     57 void* sk_calloc_throw(size_t size) {
     58     return throw_on_failure(size, sk_calloc(size));
     59 }
     60