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 35namespace WTF { 36 37MemoryManager* memoryManager() 38{ 39 static MemoryManager mm; 40 return &mm; 41} 42 43MemoryManager::MemoryManager() 44: m_allocationCanFail(false) 45{ 46} 47 48MemoryManager::~MemoryManager() 49{ 50} 51 52HBITMAP MemoryManager::createCompatibleBitmap(HDC hdc, int width, int height) 53{ 54 return ::CreateCompatibleBitmap(hdc, width, height); 55} 56 57HBITMAP MemoryManager::createDIBSection(const BITMAPINFO* pbmi, void** ppvBits) 58{ 59 return ::CreateDIBSection(0, pbmi, DIB_RGB_COLORS, ppvBits, 0, 0); 60} 61 62void* MemoryManager::m_malloc(size_t size) 63{ 64 return malloc(size); 65} 66 67void* MemoryManager::m_calloc(size_t num, size_t size) 68{ 69 return calloc(num, size); 70} 71 72void* MemoryManager::m_realloc(void* p, size_t size) 73{ 74 return realloc(p, size); 75} 76 77void MemoryManager::m_free(void* p) 78{ 79 return free(p); 80} 81 82bool MemoryManager::resizeMemory(void*, size_t) 83{ 84 return false; 85} 86 87void* MemoryManager::allocate64kBlock() 88{ 89 return VirtualAlloc(0, 65536, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); 90} 91 92void MemoryManager::free64kBlock(void* p) 93{ 94 VirtualFree(p, 65536, MEM_RELEASE); 95} 96 97bool MemoryManager::onIdle(DWORD& timeLimitMs) 98{ 99 return false; 100} 101 102LPVOID MemoryManager::virtualAlloc(LPVOID lpAddress, DWORD dwSize, DWORD flAllocationType, DWORD flProtect) 103{ 104 return ::VirtualAlloc(lpAddress, dwSize, flAllocationType, flProtect); 105} 106 107BOOL 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 115void *fastMalloc(size_t n) { return malloc(n); } 116void *fastCalloc(size_t n_elements, size_t element_size) { return calloc(n_elements, element_size); } 117void fastFree(void* p) { return free(p); } 118void *fastRealloc(void* p, size_t n) { return realloc(p, n); } 119 120#else 121 122void *fastMalloc(size_t n) { return MemoryManager::m_malloc(n); } 123void *fastCalloc(size_t n_elements, size_t element_size) { return MemoryManager::m_calloc(n_elements, element_size); } 124void fastFree(void* p) { return MemoryManager::m_free(p); } 125void *fastRealloc(void* p, size_t n) { return MemoryManager::m_realloc(p, n); } 126 127#endif 128 129#ifndef NDEBUG 130void fastMallocForbid() {} 131void fastMallocAllow() {} 132#endif 133 134void* fastZeroedMalloc(size_t n) 135{ 136 void* p = fastMalloc(n); 137 if (p) 138 memset(p, 0, n); 139 return p; 140} 141 142TryMallocReturnValue tryFastMalloc(size_t n) 143{ 144 MemoryAllocationCanFail canFail; 145 return fastMalloc(n); 146} 147 148TryMallocReturnValue tryFastZeroedMalloc(size_t n) 149{ 150 MemoryAllocationCanFail canFail; 151 return fastZeroedMalloc(n); 152} 153 154TryMallocReturnValue tryFastCalloc(size_t n_elements, size_t element_size) 155{ 156 MemoryAllocationCanFail canFail; 157 return fastCalloc(n_elements, element_size); 158} 159 160TryMallocReturnValue tryFastRealloc(void* p, size_t n) 161{ 162 MemoryAllocationCanFail canFail; 163 return fastRealloc(p, n); 164} 165 166char* fastStrDup(const char* str) 167{ 168 return _strdup(str); 169} 170 171}