2 * Copyright (C) 2008-2009 Torch Mobile Inc.
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.
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.
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.
21 #include "MemoryManager.h"
37 MemoryManager
* memoryManager()
39 static MemoryManager mm
;
43 MemoryManager::MemoryManager()
44 : m_allocationCanFail(false)
48 MemoryManager::~MemoryManager()
52 HBITMAP
MemoryManager::createCompatibleBitmap(HDC hdc
, int width
, int height
)
54 return ::CreateCompatibleBitmap(hdc
, width
, height
);
57 HBITMAP
MemoryManager::createDIBSection(const BITMAPINFO
* pbmi
, void** ppvBits
)
59 return ::CreateDIBSection(0, pbmi
, DIB_RGB_COLORS
, ppvBits
, 0, 0);
62 void* MemoryManager::m_malloc(size_t size
)
67 void* MemoryManager::m_calloc(size_t num
, size_t size
)
69 return calloc(num
, size
);
72 void* MemoryManager::m_realloc(void* p
, size_t size
)
74 return realloc(p
, size
);
77 void MemoryManager::m_free(void* p
)
82 bool MemoryManager::resizeMemory(void*, size_t)
87 void* MemoryManager::allocate64kBlock()
89 return VirtualAlloc(0, 65536, MEM_COMMIT
| MEM_RESERVE
, PAGE_READWRITE
);
92 void MemoryManager::free64kBlock(void* p
)
94 VirtualFree(p
, 65536, MEM_RELEASE
);
97 bool MemoryManager::onIdle(DWORD
& timeLimitMs
)
102 LPVOID
MemoryManager::virtualAlloc(LPVOID lpAddress
, DWORD dwSize
, DWORD flAllocationType
, DWORD flProtect
)
104 return ::VirtualAlloc(lpAddress
, dwSize
, flAllocationType
, flProtect
);
107 BOOL
MemoryManager::virtualFree(LPVOID lpAddress
, DWORD dwSize
, DWORD dwFreeType
)
109 return ::VirtualFree(lpAddress
, dwSize
, dwFreeType
);
113 #if defined(USE_SYSTEM_MALLOC) && USE_SYSTEM_MALLOC
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
); }
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
); }
130 void fastMallocForbid() {}
131 void fastMallocAllow() {}
134 void* fastZeroedMalloc(size_t n
)
136 void* p
= fastMalloc(n
);
142 TryMallocReturnValue
tryFastMalloc(size_t n
)
144 MemoryAllocationCanFail canFail
;
145 return fastMalloc(n
);
148 TryMallocReturnValue
tryFastZeroedMalloc(size_t n
)
150 MemoryAllocationCanFail canFail
;
151 return fastZeroedMalloc(n
);
154 TryMallocReturnValue
tryFastCalloc(size_t n_elements
, size_t element_size
)
156 MemoryAllocationCanFail canFail
;
157 return fastCalloc(n_elements
, element_size
);
160 TryMallocReturnValue
tryFastRealloc(void* p
, size_t n
)
162 MemoryAllocationCanFail canFail
;
163 return fastRealloc(p
, n
);
166 char* fastStrDup(const char* str
)