2018-11-30 22:52:05 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
2018-11-30 18:39:55 +03:00
|
|
|
* vim: sw=2 ts=4 et :
|
2010-03-04 08:02:56 +03:00
|
|
|
*/
|
2012-05-21 15:12:37 +04:00
|
|
|
/* 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/. */
|
2010-03-04 08:02:56 +03:00
|
|
|
|
|
|
|
#ifndef mozilla_mozalloc_h
|
|
|
|
#define mozilla_mozalloc_h
|
|
|
|
|
|
|
|
/*
|
|
|
|
* https://bugzilla.mozilla.org/show_bug.cgi?id=427099
|
|
|
|
*/
|
|
|
|
|
2010-04-21 00:12:02 +04:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
# include <new>
|
2016-05-18 10:37:19 +03:00
|
|
|
// Since libstdc++ 6, including the C headers (e.g. stdlib.h) instead of the
|
|
|
|
// corresponding C++ header (e.g. cstdlib) can cause confusion in C++ code
|
|
|
|
// using things defined there. Specifically, with stdlib.h, the use of abs()
|
|
|
|
// in gfx/graphite2/src/inc/UtfCodec.h somehow ends up picking the wrong abs()
|
|
|
|
# include <cstdlib>
|
|
|
|
#else
|
|
|
|
# include <stdlib.h>
|
2010-04-21 00:12:02 +04:00
|
|
|
#endif
|
2010-03-04 08:02:56 +03:00
|
|
|
|
2012-05-08 20:30:37 +04:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
# include "mozilla/fallible.h"
|
2016-08-16 20:56:14 +03:00
|
|
|
# include "mozilla/mozalloc_abort.h"
|
2014-12-09 01:45:10 +03:00
|
|
|
# include "mozilla/TemplateLib.h"
|
2012-05-08 20:30:37 +04:00
|
|
|
#endif
|
2013-02-12 17:30:16 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2015-02-27 08:00:15 +03:00
|
|
|
#include "mozilla/Types.h"
|
2012-05-08 20:30:37 +04:00
|
|
|
|
2017-12-05 11:04:45 +03:00
|
|
|
MOZ_BEGIN_EXTERN_C
|
2010-03-04 08:02:56 +03:00
|
|
|
|
2015-03-26 12:25:16 +03:00
|
|
|
/*
|
|
|
|
* We need to use malloc_impl and free_impl in this file when they are
|
|
|
|
* defined, because of how mozglue.dll is linked on Windows, where using
|
|
|
|
* malloc/free would end up using the symbols from the MSVCRT instead of
|
|
|
|
* ours.
|
|
|
|
*/
|
|
|
|
#ifndef free_impl
|
|
|
|
# define free_impl free
|
|
|
|
# define free_impl_
|
|
|
|
#endif
|
|
|
|
#ifndef malloc_impl
|
|
|
|
# define malloc_impl malloc
|
|
|
|
# define malloc_impl_
|
|
|
|
#endif
|
2010-03-04 08:02:56 +03:00
|
|
|
|
|
|
|
/*
|
2015-03-26 12:25:16 +03:00
|
|
|
* Each declaration below is analogous to a "standard" allocation
|
|
|
|
* function, except that the out-of-memory handling is made explicit.
|
|
|
|
* The |moz_x| versions will never return a NULL pointer; if memory
|
|
|
|
* is exhausted, they abort. The |moz_| versions may return NULL
|
|
|
|
* pointers if memory is exhausted: their return value must be checked.
|
2010-03-04 08:02:56 +03:00
|
|
|
*
|
|
|
|
* All these allocation functions are *guaranteed* to return a pointer
|
|
|
|
* to memory allocated in such a way that that memory can be freed by
|
2015-03-26 12:25:16 +03:00
|
|
|
* passing that pointer to |free()|.
|
2010-03-04 08:02:56 +03:00
|
|
|
*/
|
|
|
|
|
2015-02-27 08:00:15 +03:00
|
|
|
MFBT_API void* moz_xmalloc(size_t size) MOZ_ALLOCATOR;
|
2010-03-04 08:02:56 +03:00
|
|
|
|
2015-02-27 08:00:15 +03:00
|
|
|
MFBT_API void* moz_xcalloc(size_t nmemb, size_t size) MOZ_ALLOCATOR;
|
2010-03-04 08:02:56 +03:00
|
|
|
|
2015-02-27 08:00:15 +03:00
|
|
|
MFBT_API void* moz_xrealloc(void* ptr, size_t size) MOZ_ALLOCATOR;
|
2010-03-04 08:02:56 +03:00
|
|
|
|
2015-02-27 08:00:15 +03:00
|
|
|
MFBT_API char* moz_xstrdup(const char* str) MOZ_ALLOCATOR;
|
2010-03-04 08:02:56 +03:00
|
|
|
|
2018-08-28 08:59:19 +03:00
|
|
|
#if defined(HAVE_STRNDUP)
|
|
|
|
MFBT_API char* moz_xstrndup(const char* str, size_t strsize) MOZ_ALLOCATOR;
|
|
|
|
#endif /* if defined(HAVE_STRNDUP) */
|
|
|
|
|
|
|
|
MFBT_API void* moz_xmemdup(const void* ptr, size_t size) MOZ_ALLOCATOR;
|
|
|
|
|
|
|
|
MFBT_API void* moz_xmemalign(size_t boundary, size_t size) MOZ_ALLOCATOR;
|
|
|
|
|
2015-02-27 08:00:15 +03:00
|
|
|
MFBT_API size_t moz_malloc_usable_size(void* ptr);
|
2010-03-04 08:02:56 +03:00
|
|
|
|
2015-02-27 08:00:15 +03:00
|
|
|
MFBT_API size_t moz_malloc_size_of(const void* ptr);
|
2011-11-28 07:03:14 +04:00
|
|
|
|
2017-09-01 04:52:23 +03:00
|
|
|
/*
|
|
|
|
* Like moz_malloc_size_of(), but works reliably with interior pointers, i.e.
|
|
|
|
* pointers into the middle of a live allocation.
|
|
|
|
*/
|
|
|
|
MFBT_API size_t moz_malloc_enclosing_size_of(const void* ptr);
|
|
|
|
|
2017-12-05 11:04:45 +03:00
|
|
|
MOZ_END_EXTERN_C
|
2010-03-04 08:02:56 +03:00
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We implement the default operators new/delete as part of
|
|
|
|
* libmozalloc, replacing their definitions in libstdc++. The
|
|
|
|
* operator new* definitions in libmozalloc will never return a NULL
|
|
|
|
* pointer.
|
|
|
|
*
|
|
|
|
* Each operator new immediately below returns a pointer to memory
|
|
|
|
* that can be delete'd by any of
|
|
|
|
*
|
|
|
|
* (1) the matching infallible operator delete immediately below
|
2017-12-07 05:32:29 +03:00
|
|
|
* (2) the matching system |operator delete(void*, std::nothrow)|
|
2018-07-18 14:36:25 +03:00
|
|
|
* (3) the matching system |operator delete(void*) noexcept(false)|
|
2010-03-04 08:02:56 +03:00
|
|
|
*
|
2018-07-18 14:36:25 +03:00
|
|
|
* NB: these are declared |noexcept(false)|, though they will never
|
2010-03-04 08:02:56 +03:00
|
|
|
* throw that exception. This declaration is consistent with the rule
|
2018-07-18 14:36:25 +03:00
|
|
|
* that |::operator new() noexcept(false)| will never return NULL.
|
2017-12-07 05:32:29 +03:00
|
|
|
*
|
|
|
|
* NB: mozilla::fallible can be used instead of std::nothrow.
|
2010-03-04 08:02:56 +03:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* NB: This is defined just to silence vacuous warnings about symbol
|
|
|
|
* visibility on OS X/gcc. These symbols are force-inline and not
|
|
|
|
* exported. */
|
|
|
|
# if defined(XP_MACOSX)
|
2015-02-27 08:00:15 +03:00
|
|
|
# define MOZALLOC_EXPORT_NEW MFBT_API
|
2010-03-04 08:02:56 +03:00
|
|
|
# else
|
|
|
|
# define MOZALLOC_EXPORT_NEW
|
|
|
|
# endif
|
|
|
|
|
2015-04-10 16:05:00 +03:00
|
|
|
MOZALLOC_EXPORT_NEW
|
|
|
|
# if defined(__GNUC__) && !defined(__clang__) && defined(__SANITIZE_ADDRESS__)
|
|
|
|
/* gcc's asan somehow doesn't like always_inline on this function. */
|
|
|
|
__attribute__((gnu_inline)) inline
|
|
|
|
# else
|
2017-12-06 05:31:48 +03:00
|
|
|
MOZ_ALWAYS_INLINE_EVEN_DEBUG
|
2015-04-10 16:05:00 +03:00
|
|
|
# endif
|
2018-07-18 14:36:25 +03:00
|
|
|
void*
|
|
|
|
operator new(size_t size) noexcept(false) {
|
2010-03-04 08:02:56 +03:00
|
|
|
return moz_xmalloc(size);
|
|
|
|
}
|
|
|
|
|
2017-12-06 05:31:48 +03:00
|
|
|
MOZALLOC_EXPORT_NEW MOZ_ALWAYS_INLINE_EVEN_DEBUG void* operator new(
|
2018-07-18 14:36:25 +03:00
|
|
|
size_t size, const std::nothrow_t&) noexcept(true) {
|
2015-03-26 12:25:16 +03:00
|
|
|
return malloc_impl(size);
|
2010-03-14 22:56:47 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 05:31:48 +03:00
|
|
|
MOZALLOC_EXPORT_NEW MOZ_ALWAYS_INLINE_EVEN_DEBUG void* operator new[](
|
2018-07-18 14:36:25 +03:00
|
|
|
size_t size) noexcept(false) {
|
2010-03-04 08:02:56 +03:00
|
|
|
return moz_xmalloc(size);
|
|
|
|
}
|
|
|
|
|
2017-12-06 05:31:48 +03:00
|
|
|
MOZALLOC_EXPORT_NEW MOZ_ALWAYS_INLINE_EVEN_DEBUG void* operator new[](
|
2018-07-18 14:36:25 +03:00
|
|
|
size_t size, const std::nothrow_t&) noexcept(true) {
|
2015-03-26 12:25:16 +03:00
|
|
|
return malloc_impl(size);
|
2010-03-14 22:56:47 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 05:31:48 +03:00
|
|
|
MOZALLOC_EXPORT_NEW MOZ_ALWAYS_INLINE_EVEN_DEBUG void operator delete(
|
2018-07-18 14:36:25 +03:00
|
|
|
void* ptr) noexcept(true) {
|
2015-03-26 12:25:16 +03:00
|
|
|
return free_impl(ptr);
|
2010-03-04 08:02:56 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 05:31:48 +03:00
|
|
|
MOZALLOC_EXPORT_NEW MOZ_ALWAYS_INLINE_EVEN_DEBUG void operator delete(
|
2018-07-18 14:36:25 +03:00
|
|
|
void* ptr, const std::nothrow_t&)noexcept(true) {
|
2015-03-26 12:25:16 +03:00
|
|
|
return free_impl(ptr);
|
2010-03-14 22:56:47 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 05:31:48 +03:00
|
|
|
MOZALLOC_EXPORT_NEW MOZ_ALWAYS_INLINE_EVEN_DEBUG void operator delete[](
|
2018-07-18 14:36:25 +03:00
|
|
|
void* ptr) noexcept(true) {
|
2015-03-26 12:25:16 +03:00
|
|
|
return free_impl(ptr);
|
2010-03-04 08:02:56 +03:00
|
|
|
}
|
|
|
|
|
2017-12-06 05:31:48 +03:00
|
|
|
MOZALLOC_EXPORT_NEW MOZ_ALWAYS_INLINE_EVEN_DEBUG void operator delete[](
|
2018-07-18 14:36:25 +03:00
|
|
|
void* ptr, const std::nothrow_t&) noexcept(true) {
|
2015-03-26 12:25:16 +03:00
|
|
|
return free_impl(ptr);
|
2010-03-14 22:56:47 +03:00
|
|
|
}
|
|
|
|
|
2018-03-27 15:42:17 +03:00
|
|
|
# if defined(XP_WIN)
|
|
|
|
// We provide the global sized delete overloads unconditionally because the
|
|
|
|
// MSVC runtime headers do, despite compiling with /Zc:sizedDealloc-
|
|
|
|
MOZALLOC_EXPORT_NEW MOZ_ALWAYS_INLINE_EVEN_DEBUG void operator delete(
|
2018-07-18 14:36:25 +03:00
|
|
|
void* ptr, size_t /*size*/) noexcept(true) {
|
2018-03-27 15:42:17 +03:00
|
|
|
return free_impl(ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZALLOC_EXPORT_NEW MOZ_ALWAYS_INLINE_EVEN_DEBUG void operator delete[](
|
2018-07-18 14:36:25 +03:00
|
|
|
void* ptr, size_t /*size*/) noexcept(true) {
|
2018-03-27 15:42:17 +03:00
|
|
|
return free_impl(ptr);
|
|
|
|
}
|
|
|
|
# endif
|
2010-03-04 08:02:56 +03:00
|
|
|
|
2014-12-09 01:45:10 +03:00
|
|
|
/*
|
|
|
|
* This policy is identical to MallocAllocPolicy, except it uses
|
2015-03-26 12:25:16 +03:00
|
|
|
* moz_xmalloc/moz_xcalloc/moz_xrealloc instead of
|
|
|
|
* malloc/calloc/realloc.
|
2014-12-09 01:45:10 +03:00
|
|
|
*/
|
|
|
|
class InfallibleAllocPolicy {
|
|
|
|
public:
|
2016-08-16 21:09:05 +03:00
|
|
|
template <typename T>
|
|
|
|
T* maybe_pod_malloc(size_t aNumElems) {
|
|
|
|
return pod_malloc<T>(aNumElems);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-08-16 21:09:05 +03:00
|
|
|
template <typename T>
|
|
|
|
T* maybe_pod_calloc(size_t aNumElems) {
|
|
|
|
return pod_calloc<T>(aNumElems);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-08-16 21:09:05 +03:00
|
|
|
template <typename T>
|
|
|
|
T* maybe_pod_realloc(T* aPtr, size_t aOldSize, size_t aNewSize) {
|
|
|
|
return pod_realloc<T>(aPtr, aOldSize, aNewSize);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-08-16 21:09:05 +03:00
|
|
|
template <typename T>
|
2014-12-09 01:45:10 +03:00
|
|
|
T* pod_malloc(size_t aNumElems) {
|
|
|
|
if (aNumElems & mozilla::tl::MulOverflowMask<sizeof(T)>::value) {
|
2016-08-16 20:56:14 +03:00
|
|
|
reportAllocOverflow();
|
2016-08-16 21:09:05 +03:00
|
|
|
}
|
2014-12-09 01:45:10 +03:00
|
|
|
return static_cast<T*>(moz_xmalloc(aNumElems * sizeof(T)));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-08-16 21:09:05 +03:00
|
|
|
template <typename T>
|
|
|
|
T* pod_calloc(size_t aNumElems) {
|
|
|
|
return static_cast<T*>(moz_xcalloc(aNumElems, sizeof(T)));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-08-16 21:09:05 +03:00
|
|
|
template <typename T>
|
|
|
|
T* pod_realloc(T* aPtr, size_t aOldSize, size_t aNewSize) {
|
2014-12-09 01:45:10 +03:00
|
|
|
if (aNewSize & mozilla::tl::MulOverflowMask<sizeof(T)>::value) {
|
2016-08-16 20:56:14 +03:00
|
|
|
reportAllocOverflow();
|
2016-08-16 21:09:05 +03:00
|
|
|
}
|
2014-12-09 01:45:10 +03:00
|
|
|
return static_cast<T*>(moz_xrealloc(aPtr, aNewSize * sizeof(T)));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2016-08-16 21:09:05 +03:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void free_(T* aPtr, size_t aNumElems = 0) {
|
|
|
|
free_impl(aPtr);
|
|
|
|
}
|
|
|
|
|
2016-08-16 20:56:14 +03:00
|
|
|
void reportAllocOverflow() const { mozalloc_abort("alloc overflow"); }
|
2014-12-09 01:45:10 +03:00
|
|
|
|
|
|
|
bool checkSimulatedOOM() const { return true; }
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* ifdef __cplusplus */
|
2010-03-04 08:02:56 +03:00
|
|
|
|
2015-03-26 12:25:16 +03:00
|
|
|
#ifdef malloc_impl_
|
|
|
|
# undef malloc_impl_
|
|
|
|
# undef malloc_impl
|
|
|
|
#endif
|
|
|
|
#ifdef free_impl_
|
|
|
|
# undef free_impl_
|
|
|
|
# undef free_impl
|
|
|
|
#endif
|
|
|
|
|
2010-03-04 08:02:56 +03:00
|
|
|
#endif /* ifndef mozilla_mozalloc_h */
|