1 /* 2 * Copyright (C) 2008-2009 Torch Mobile Inc. 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Library General Public 6 * License as published by the Free Software Foundation; either 7 * version 2 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Library General Public License for more details. 13 * 14 * You should have received a copy of the GNU Library General Public License 15 * along with this library; see the file COPYING.LIB. If not, write to 16 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 17 * Boston, MA 02110-1301, USA. 18 */ 19 20 #include "config.h" 21 #include "MemoryManager.h" 22 23 #undef malloc 24 #undef calloc 25 #undef realloc 26 #undef free 27 #undef strdup 28 #undef _strdup 29 #undef VirtualAlloc 30 #undef VirtualFree 31 32 #include <malloc.h> 33 #include <windows.h> 34 35 namespace WTF { 36 37 MemoryManager* memoryManager() 38 { 39 static MemoryManager mm; 40 return &mm; 41 } 42 43 MemoryManager::MemoryManager() 44 : m_allocationCanFail(false) 45 { 46 } 47 48 MemoryManager::~MemoryManager() 49 { 50 } 51 52 HBITMAP MemoryManager::createCompatibleBitmap(HDC hdc, int width, int height) 53 { 54 return ::CreateCompatibleBitmap(hdc, width, height); 55 } 56 57 HBITMAP MemoryManager::createDIBSection(const BITMAPINFO* pbmi, void** ppvBits) 58 { 59 return ::CreateDIBSection(0, pbmi, DIB_RGB_COLORS, ppvBits, 0, 0); 60 } 61 62 void* MemoryManager::m_malloc(size_t size) 63 { 64 return malloc(size); 65 } 66 67 void* MemoryManager::m_calloc(size_t num, size_t size) 68 { 69 return calloc(num, size); 70 } 71 72 void* MemoryManager::m_realloc(void* p, size_t size) 73 { 74 return realloc(p, size); 75 } 76 77 void MemoryManager::m_free(void* p) 78 { 79 return free(p); 80 } 81 82 bool MemoryManager::resizeMemory(void*, size_t) 83 { 84 return false; 85 } 86 87 void* MemoryManager::allocate64kBlock() 88 { 89 return VirtualAlloc(0, 65536, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); 90 } 91 92 void MemoryManager::free64kBlock(void* p) 93 { 94 VirtualFree(p, 65536, MEM_RELEASE); 95 } 96 97 bool MemoryManager::onIdle(DWORD& timeLimitMs) 98 { 99 return false; 100 } 101 102 LPVOID MemoryManager::virtualAlloc(LPVOID lpAddress, DWORD dwSize, DWORD flAllocationType, DWORD flProtect) 103 { 104 return ::VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect); 105 } 106 107 BOOL MemoryManager::virtualFree(LPVOID lpAddress, DWORD dwSize, DWORD dwFreeType) 108 { 109 return ::VirtualFree(lpAddress, dwSize, dwFreeType); 110 } 111 112 113 #if defined(USE_SYSTEM_MALLOC) && USE_SYSTEM_MALLOC 114 115 void *fastMalloc(size_t n) { return malloc(n); } 116 void *fastCalloc(size_t n_elements, size_t element_size) { return calloc(n_elements, element_size); } 117 void fastFree(void* p) { return free(p); } 118 void *fastRealloc(void* p, size_t n) { return realloc(p, n); } 119 120 #else 121 122 void *fastMalloc(size_t n) { return MemoryManager::m_malloc(n); } 123 void *fastCalloc(size_t n_elements, size_t element_size) { return MemoryManager::m_calloc(n_elements, element_size); } 124 void fastFree(void* p) { return MemoryManager::m_free(p); } 125 void *fastRealloc(void* p, size_t n) { return MemoryManager::m_realloc(p, n); } 126 127 #endif 128 129 #ifndef NDEBUG 130 void fastMallocForbid() {} 131 void fastMallocAllow() {} 132 #endif 133 134 void* fastZeroedMalloc(size_t n) 135 { 136 void* p = fastMalloc(n); 137 if (p) 138 memset(p, 0, n); 139 return p; 140 } 141 142 TryMallocReturnValue tryFastMalloc(size_t n) 143 { 144 MemoryAllocationCanFail canFail; 145 return fastMalloc(n); 146 } 147 148 TryMallocReturnValue tryFastZeroedMalloc(size_t n) 149 { 150 MemoryAllocationCanFail canFail; 151 return fastZeroedMalloc(n); 152 } 153 154 TryMallocReturnValue tryFastCalloc(size_t n_elements, size_t element_size) 155 { 156 MemoryAllocationCanFail canFail; 157 return fastCalloc(n_elements, element_size); 158 } 159 160 TryMallocReturnValue tryFastRealloc(void* p, size_t n) 161 { 162 MemoryAllocationCanFail canFail; 163 return fastRealloc(p, n); 164 } 165 166 char* fastStrDup(const char* str) 167 { 168 return _strdup(str); 169 } 170 171 }