Home | History | Annotate | Download | only in ext
      1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
      2 // Use of this source code is governed by a BSD-style license that can be
      3 // found in the LICENSE file.
      4 
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <new>
      8 
      9 #include "base/process/memory.h"
     10 
     11 #include "third_party/skia/include/core/SkTypes.h"
     12 #include "third_party/skia/include/core/SkThread.h"
     13 
     14 // This implementation of sk_malloc_flags() and friends is identical to
     15 // SkMemory_malloc.cpp, except that it disables the CRT's new_handler during
     16 // malloc() and calloc() when SK_MALLOC_THROW is not set (because our normal
     17 // new_handler itself will crash on failure when using tcmalloc).
     18 
     19 SK_DECLARE_STATIC_MUTEX(gSkNewHandlerMutex);
     20 
     21 static inline void* throw_on_failure(size_t size, void* p) {
     22     if (size > 0 && p == NULL) {
     23         // If we've got a NULL here, the only reason we should have failed is running out of RAM.
     24         sk_out_of_memory();
     25     }
     26     return p;
     27 }
     28 
     29 void sk_throw() {
     30     SkASSERT(!"sk_throw");
     31     abort();
     32 }
     33 
     34 void sk_out_of_memory(void) {
     35     SkASSERT(!"sk_out_of_memory");
     36     abort();
     37 }
     38 
     39 void* sk_realloc_throw(void* addr, size_t size) {
     40     return throw_on_failure(size, realloc(addr, size));
     41 }
     42 
     43 void sk_free(void* p) {
     44     if (p) {
     45         free(p);
     46     }
     47 }
     48 
     49 void* sk_malloc_throw(size_t size) {
     50     return throw_on_failure(size, malloc(size));
     51 }
     52 
     53 static void* sk_malloc_nothrow(size_t size) {
     54     // TODO(b.kelemen): we should always use UncheckedMalloc but currently it
     55     // doesn't work as intended everywhere.
     56 #if  defined(LIBC_GLIBC) || defined(USE_TCMALLOC) || \
     57      (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
     58     void* result;
     59     // It's the responsibility of the caller to check the return value.
     60     ignore_result(base::UncheckedMalloc(size, &result));
     61     return result;
     62 #else
     63     // This is not really thread safe.  It only won't collide with itself, but we're totally
     64     // unprotected from races with other code that calls set_new_handler.
     65     SkAutoMutexAcquire lock(gSkNewHandlerMutex);
     66     std::new_handler old_handler = std::set_new_handler(NULL);
     67     void* p = malloc(size);
     68     std::set_new_handler(old_handler);
     69     return p;
     70 #endif
     71 }
     72 
     73 void* sk_malloc_flags(size_t size, unsigned flags) {
     74     if (flags & SK_MALLOC_THROW) {
     75         return sk_malloc_throw(size);
     76     }
     77     return sk_malloc_nothrow(size);
     78 }
     79 
     80 void* sk_calloc_throw(size_t size) {
     81     return throw_on_failure(size, calloc(size, 1));
     82 }
     83 
     84 void* sk_calloc(size_t size) {
     85     // TODO(b.kelemen): we should always use UncheckedCalloc but currently it
     86     // doesn't work as intended everywhere.
     87 #if  defined(LIBC_GLIBC) || defined(USE_TCMALLOC) || \
     88      (defined(OS_MACOSX) && !defined(OS_IOS)) || defined(OS_ANDROID)
     89     void* result;
     90     // It's the responsibility of the caller to check the return value.
     91     ignore_result(base::UncheckedCalloc(size, 1, &result));
     92     return result;
     93 #else
     94     SkAutoMutexAcquire lock(gSkNewHandlerMutex);
     95     std::new_handler old_handler = std::set_new_handler(NULL);
     96     void* p = calloc(size, 1);
     97     std::set_new_handler(old_handler);
     98     return p;
     99 #endif
    100 }
    101