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#pragma once
21
22#include <winbase.h>
23
24typedef struct HBITMAP__* HBITMAP;
25typedef struct HDC__* HDC;
26typedef void *HANDLE;
27typedef struct tagBITMAPINFO BITMAPINFO;
28
29namespace WTF {
30
31    class MemoryManager {
32    public:
33        MemoryManager();
34        ~MemoryManager();
35
36        bool allocationCanFail() const { return m_allocationCanFail; }
37        void setAllocationCanFail(bool c) { m_allocationCanFail = c; }
38
39        static HBITMAP createCompatibleBitmap(HDC hdc, int width, int height);
40        static HBITMAP createDIBSection(const BITMAPINFO* pbmi, void** ppvBits);
41        static void* m_malloc(size_t size);
42        static void* m_calloc(size_t num, size_t size);
43        static void* m_realloc(void* p, size_t size);
44        static void m_free(void*);
45        static bool resizeMemory(void* p, size_t newSize);
46        static void* allocate64kBlock();
47        static void free64kBlock(void*);
48        static bool onIdle(DWORD& timeLimitMs);
49        static LPVOID virtualAlloc(LPVOID lpAddress, DWORD dwSize, DWORD flAllocationType, DWORD flProtect);
50        static BOOL virtualFree(LPVOID lpAddress, DWORD dwSize, DWORD dwFreeType);
51
52    private:
53        friend MemoryManager* memoryManager();
54
55        bool m_allocationCanFail;
56    };
57
58    MemoryManager* memoryManager();
59
60    class MemoryAllocationCanFail {
61    public:
62        MemoryAllocationCanFail() : m_old(memoryManager()->allocationCanFail()) { memoryManager()->setAllocationCanFail(true); }
63        ~MemoryAllocationCanFail() { memoryManager()->setAllocationCanFail(m_old); }
64    private:
65        bool m_old;
66    };
67
68    class MemoryAllocationCannotFail {
69    public:
70        MemoryAllocationCannotFail() : m_old(memoryManager()->allocationCanFail()) { memoryManager()->setAllocationCanFail(false); }
71        ~MemoryAllocationCannotFail() { memoryManager()->setAllocationCanFail(m_old); }
72    private:
73        bool m_old;
74    };
75}
76
77using WTF::MemoryManager;
78using WTF::memoryManager;
79using WTF::MemoryAllocationCanFail;
80using WTF::MemoryAllocationCannotFail;
81