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