gecko-dev/memory/mozalloc/winheap.cpp
Mike Hommey 2abcc3d7cb Bug 1553363 - Generalize the *_impl goop for allocation functions in mozglue. r=froydnj
The current situation is suboptimal, where we have the same goop
repeated in multiple files, and where things kinda sorta work out fine
thanks to the linker for files that would have been forbidden, except
when the linker doesn't do its job, which apparently happen on
mingwclang builds.

This change only really covers C++ code using operator new/delete, and
not things that would be using malloc/free, because it's easier.
malloc/free is left for a followup.

Differential Revision: https://phabricator.services.mozilla.com/D32119

--HG--
extra : moz-landing-system : lando
2019-05-29 22:49:42 +00:00

56 lines
1.9 KiB
C++

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
* vim: sw=2 ts=4 et :
*/
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#include "mozilla/mozalloc.h"
#include <windows.h>
#if !defined(MOZ_MEMORY)
# include <malloc.h>
# define malloc_impl malloc
# define calloc_impl calloc
# define realloc_impl realloc
# define free_impl free
#endif
// Warning: C4273: 'HeapAlloc': inconsistent dll linkage
// The Windows headers define HeapAlloc as dllimport, but we define it as
// dllexport, which is a voluntary inconsistency.
#pragma warning(disable : 4273)
MFBT_API
LPVOID WINAPI HeapAlloc(_In_ HANDLE hHeap, _In_ DWORD dwFlags,
_In_ SIZE_T dwBytes) {
if (dwFlags & HEAP_ZERO_MEMORY) {
return calloc_impl(1, dwBytes);
}
return malloc_impl(dwBytes);
}
MFBT_API
LPVOID WINAPI HeapReAlloc(_In_ HANDLE hHeap, _In_ DWORD dwFlags,
_In_ LPVOID lpMem, _In_ SIZE_T dwBytes) {
// The HeapReAlloc contract is that failures preserve the existing
// allocation. We can't try to realloc in-place without possibly
// freeing the original allocation, breaking the contract.
// We also can't guarantee we zero all the memory from the end of
// the original allocation to the end of the new one because of the
// difference between the originally requested size and what
// malloc_usable_size would return us.
// So for both cases, just tell the caller we can't do what they
// requested.
if (dwFlags & (HEAP_REALLOC_IN_PLACE_ONLY | HEAP_ZERO_MEMORY)) {
return NULL;
}
return realloc_impl(lpMem, dwBytes);
}
MFBT_API
BOOL WINAPI HeapFree(_In_ HANDLE hHeap, _In_ DWORD dwFlags, _In_ LPVOID lpMem) {
free_impl(lpMem);
return true;
}