Home | History | Annotate | Download | only in wtf
      1 // Copyright (c) 2005, 2007, Google Inc.
      2 // All rights reserved.
      3 // Copyright (C) 2005, 2006, 2007, 2008, 2009, 2011 Apple Inc. All rights reserved.
      4 //
      5 // Redistribution and use in source and binary forms, with or without
      6 // modification, are permitted provided that the following conditions are
      7 // met:
      8 //
      9 //     * Redistributions of source code must retain the above copyright
     10 // notice, this list of conditions and the following disclaimer.
     11 //     * Redistributions in binary form must reproduce the above
     12 // copyright notice, this list of conditions and the following disclaimer
     13 // in the documentation and/or other materials provided with the
     14 // distribution.
     15 //     * Neither the name of Google Inc. nor the names of its
     16 // contributors may be used to endorse or promote products derived from
     17 // this software without specific prior written permission.
     18 //
     19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     30 
     31 #include "config.h"
     32 #include "wtf/FastMalloc.h"
     33 
     34 #include <string.h>
     35 
     36 namespace WTF {
     37 
     38 void* fastZeroedMalloc(size_t n)
     39 {
     40     void* result = fastMalloc(n);
     41     memset(result, 0, n);
     42     return result;
     43 }
     44 
     45 char* fastStrDup(const char* src)
     46 {
     47     size_t len = strlen(src) + 1;
     48     char* dup = static_cast<char*>(fastMalloc(len));
     49     memcpy(dup, src, len);
     50     return dup;
     51 }
     52 
     53 // TODO: remove these two.
     54 void releaseFastMallocFreeMemory() { }
     55 
     56 FastMallocStatistics fastMallocStatistics()
     57 {
     58     FastMallocStatistics statistics = { 0, 0, 0 };
     59     return statistics;
     60 }
     61 
     62 } // namespace WTF
     63 
     64 #if USE(SYSTEM_MALLOC)
     65 
     66 #include "wtf/Assertions.h"
     67 
     68 #include <stdlib.h>
     69 
     70 namespace WTF {
     71 
     72 void fastMallocShutdown()
     73 {
     74 }
     75 
     76 void* fastMalloc(size_t n)
     77 {
     78     void* result = malloc(n);
     79     ASSERT(result);  // We expect tcmalloc underneath, which would crash instead of getting here.
     80 
     81     return result;
     82 }
     83 
     84 void fastFree(void* p)
     85 {
     86     free(p);
     87 }
     88 
     89 void* fastRealloc(void* p, size_t n)
     90 {
     91     void* result = realloc(p, n);
     92     ASSERT(result);  // We expect tcmalloc underneath, which would crash instead of getting here.
     93 
     94     return result;
     95 }
     96 
     97 } // namespace WTF
     98 
     99 #else // USE(SYSTEM_MALLOC)
    100 
    101 #include "wtf/PartitionAlloc.h"
    102 #include "wtf/SpinLock.h"
    103 
    104 namespace WTF {
    105 
    106 static PartitionAllocatorGeneric gPartition;
    107 static int gLock = 0;
    108 static bool gInitialized = false;
    109 
    110 void fastMallocShutdown()
    111 {
    112     gPartition.shutdown();
    113 }
    114 
    115 void* fastMalloc(size_t n)
    116 {
    117     if (UNLIKELY(!gInitialized)) {
    118         spinLockLock(&gLock);
    119         if (!gInitialized) {
    120             gInitialized = true;
    121             gPartition.init();
    122         }
    123         spinLockUnlock(&gLock);
    124     }
    125     return partitionAllocGeneric(gPartition.root(), n);
    126 }
    127 
    128 void fastFree(void* p)
    129 {
    130     partitionFreeGeneric(gPartition.root(), p);
    131 }
    132 
    133 void* fastRealloc(void* p, size_t n)
    134 {
    135     return partitionReallocGeneric(gPartition.root(), p, n);
    136 }
    137 
    138 } // namespace WTF
    139 
    140 #endif // USE(SYSTEM_MALLOC)
    141