winheap.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. * vim: sw=4 ts=4 et :
  3. */
  4. /* This Source Code Form is subject to the terms of the Mozilla Public
  5. * License, v. 2.0. If a copy of the MPL was not distributed with this
  6. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  7. #include "mozilla/Types.h"
  8. #include <windows.h>
  9. // Building with USE_STATIC_LIBS = True sets -MT instead of -MD. -MT sets _MT,
  10. // while -MD sets _MT and _DLL.
  11. #if defined(_MT) && !defined(_DLL)
  12. #define MOZ_STATIC_RUNTIME
  13. #endif
  14. #if defined(MOZ_MEMORY) && !defined(MOZ_STATIC_RUNTIME)
  15. // mozalloc.cpp is part of the same library as mozmemory, thus MOZ_MEMORY_IMPL
  16. // is needed.
  17. #define MOZ_MEMORY_IMPL
  18. #include "mozmemory_wrap.h"
  19. // See mozmemory_wrap.h for more details. This file is part of libmozglue, so
  20. // it needs to use _impl suffixes. However, with libmozglue growing, this is
  21. // becoming cumbersome, so we will likely use a malloc.h wrapper of some sort
  22. // and allow the use of the functions without a _impl suffix.
  23. #define MALLOC_DECL(name, return_type, ...) \
  24. extern "C" MOZ_MEMORY_API return_type name ## _impl(__VA_ARGS__);
  25. #define MALLOC_FUNCS MALLOC_FUNCS_MALLOC
  26. #include "malloc_decls.h"
  27. // Warning: C4273: 'HeapAlloc': inconsistent dll linkage
  28. // The Windows headers define HeapAlloc as dllimport, but we define it as
  29. // dllexport, which is a voluntary inconsistency.
  30. #pragma warning(disable: 4273)
  31. MFBT_API
  32. LPVOID WINAPI HeapAlloc(_In_ HANDLE hHeap, _In_ DWORD dwFlags,
  33. _In_ SIZE_T dwBytes)
  34. {
  35. if (dwFlags & HEAP_ZERO_MEMORY) {
  36. return calloc_impl(1, dwBytes);
  37. }
  38. return malloc_impl(dwBytes);
  39. }
  40. MFBT_API
  41. LPVOID WINAPI HeapReAlloc(_In_ HANDLE hHeap, _In_ DWORD dwFlags,
  42. _In_ LPVOID lpMem, _In_ SIZE_T dwBytes)
  43. {
  44. // The HeapReAlloc contract is that failures preserve the existing
  45. // allocation. We can't try to realloc in-place without possibly
  46. // freeing the original allocation, breaking the contract.
  47. // We also can't guarantee we zero all the memory from the end of
  48. // the original allocation to the end of the new one because of the
  49. // difference between the originally requested size and what
  50. // malloc_usable_size would return us.
  51. // So for both cases, just tell the caller we can't do what they
  52. // requested.
  53. if (dwFlags & (HEAP_REALLOC_IN_PLACE_ONLY | HEAP_ZERO_MEMORY)) {
  54. return NULL;
  55. }
  56. return realloc_impl(lpMem, dwBytes);
  57. }
  58. MFBT_API
  59. BOOL WINAPI HeapFree(_In_ HANDLE hHeap, _In_ DWORD dwFlags,
  60. _In_ LPVOID lpMem)
  61. {
  62. free_impl(lpMem);
  63. return true;
  64. }
  65. #endif