MemoryManager.h 2.9 KB

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