2010-03-03 21:02:56 -08:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
* vim: sw=4 ts=4 et :
|
|
|
|
*/
|
2012-05-21 04:12:37 -07: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-03 21:02:56 -08:00
|
|
|
|
|
|
|
#ifndef mozilla_mozalloc_h
|
|
|
|
#define mozilla_mozalloc_h
|
|
|
|
|
|
|
|
/*
|
|
|
|
* https://bugzilla.mozilla.org/show_bug.cgi?id=427099
|
|
|
|
*/
|
|
|
|
|
2010-04-20 13:12:02 -07:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
# include <new>
|
|
|
|
#endif
|
2010-03-03 21:02:56 -08:00
|
|
|
|
2012-05-08 09:30:37 -07:00
|
|
|
#if defined(__cplusplus)
|
|
|
|
#include "mozilla/fallible.h"
|
2014-12-08 14:45:10 -08:00
|
|
|
#include "mozilla/TemplateLib.h"
|
2012-05-08 09:30:37 -07:00
|
|
|
#endif
|
2013-02-12 05:30:16 -08:00
|
|
|
#include "mozilla/Attributes.h"
|
2015-02-26 21:00:15 -08:00
|
|
|
#include "mozilla/Types.h"
|
2012-05-08 09:30:37 -07:00
|
|
|
|
2010-11-08 18:48:59 -08:00
|
|
|
#define MOZALLOC_HAVE_XMALLOC
|
|
|
|
|
2013-04-19 10:55:34 -07:00
|
|
|
#if defined(MOZ_ALWAYS_INLINE_EVEN_DEBUG)
|
|
|
|
# define MOZALLOC_INLINE MOZ_ALWAYS_INLINE_EVEN_DEBUG
|
2012-01-12 12:35:48 -08:00
|
|
|
#elif defined(HAVE_FORCEINLINE)
|
|
|
|
# define MOZALLOC_INLINE __forceinline
|
|
|
|
#else
|
|
|
|
# define MOZALLOC_INLINE inline
|
|
|
|
#endif
|
|
|
|
|
2010-04-06 23:16:32 -07:00
|
|
|
/* Workaround build problem with Sun Studio 12 */
|
|
|
|
#if defined(__SUNPRO_C) || defined(__SUNPRO_CC)
|
2015-03-19 00:46:40 -07:00
|
|
|
# undef MOZ_WARN_UNUSED_RESULT
|
|
|
|
# define MOZ_WARN_UNUSED_RESULT
|
2015-03-18 23:56:08 -07:00
|
|
|
# undef MOZ_ALLOCATOR
|
|
|
|
# define MOZ_ALLOCATOR
|
2010-04-06 23:16:32 -07:00
|
|
|
#endif
|
2010-03-03 21:02:56 -08:00
|
|
|
|
|
|
|
#if defined(__cplusplus)
|
|
|
|
extern "C" {
|
|
|
|
#endif /* ifdef __cplusplus */
|
|
|
|
|
2015-03-26 02:25:16 -07: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-03 21:02:56 -08:00
|
|
|
|
|
|
|
/*
|
2015-03-26 02:25:16 -07: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-03 21:02:56 -08: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 02:25:16 -07:00
|
|
|
* passing that pointer to |free()|.
|
2010-03-03 21:02:56 -08:00
|
|
|
*/
|
|
|
|
|
2015-02-26 21:00:15 -08:00
|
|
|
MFBT_API void* moz_xmalloc(size_t size)
|
2015-03-18 23:56:08 -07:00
|
|
|
MOZ_ALLOCATOR;
|
2010-03-03 21:02:56 -08:00
|
|
|
|
2015-02-26 21:00:15 -08:00
|
|
|
MFBT_API void* moz_xcalloc(size_t nmemb, size_t size)
|
2015-03-18 23:56:08 -07:00
|
|
|
MOZ_ALLOCATOR;
|
2010-03-03 21:02:56 -08:00
|
|
|
|
2015-02-26 21:00:15 -08:00
|
|
|
MFBT_API void* moz_xrealloc(void* ptr, size_t size)
|
2015-03-18 23:56:08 -07:00
|
|
|
MOZ_ALLOCATOR;
|
2010-03-03 21:02:56 -08:00
|
|
|
|
2015-02-26 21:00:15 -08:00
|
|
|
MFBT_API char* moz_xstrdup(const char* str)
|
2015-03-18 23:56:08 -07:00
|
|
|
MOZ_ALLOCATOR;
|
2010-03-03 21:02:56 -08:00
|
|
|
|
2015-02-26 21:00:15 -08:00
|
|
|
MFBT_API size_t moz_malloc_usable_size(void *ptr);
|
2010-03-03 21:02:56 -08:00
|
|
|
|
2015-02-26 21:00:15 -08:00
|
|
|
MFBT_API size_t moz_malloc_size_of(const void *ptr);
|
2011-11-27 19:03:14 -08:00
|
|
|
|
2010-03-03 21:02:56 -08:00
|
|
|
#if defined(HAVE_STRNDUP)
|
2015-02-26 21:00:15 -08:00
|
|
|
MFBT_API char* moz_xstrndup(const char* str, size_t strsize)
|
2015-03-18 23:56:08 -07:00
|
|
|
MOZ_ALLOCATOR;
|
2010-03-03 21:02:56 -08:00
|
|
|
#endif /* if defined(HAVE_STRNDUP) */
|
|
|
|
|
|
|
|
|
2012-04-05 00:20:53 -07:00
|
|
|
#if defined(HAVE_POSIX_MEMALIGN)
|
2015-03-19 00:46:40 -07:00
|
|
|
MFBT_API MOZ_WARN_UNUSED_RESULT
|
|
|
|
int moz_xposix_memalign(void **ptr, size_t alignment, size_t size);
|
2010-03-03 21:02:56 -08:00
|
|
|
|
2015-03-19 00:46:40 -07:00
|
|
|
MFBT_API MOZ_WARN_UNUSED_RESULT
|
|
|
|
int moz_posix_memalign(void **ptr, size_t alignment, size_t size);
|
2010-03-03 21:02:56 -08:00
|
|
|
#endif /* if defined(HAVE_POSIX_MEMALIGN) */
|
|
|
|
|
|
|
|
|
2012-04-05 00:20:53 -07:00
|
|
|
#if defined(HAVE_MEMALIGN)
|
2015-02-26 21:00:15 -08:00
|
|
|
MFBT_API void* moz_xmemalign(size_t boundary, size_t size)
|
2015-03-18 23:56:08 -07:00
|
|
|
MOZ_ALLOCATOR;
|
2010-03-03 21:02:56 -08:00
|
|
|
#endif /* if defined(HAVE_MEMALIGN) */
|
|
|
|
|
|
|
|
|
2012-04-05 00:20:53 -07:00
|
|
|
#if defined(HAVE_VALLOC)
|
2015-02-26 21:00:15 -08:00
|
|
|
MFBT_API void* moz_xvalloc(size_t size)
|
2015-03-18 23:56:08 -07:00
|
|
|
MOZ_ALLOCATOR;
|
2010-03-03 21:02:56 -08:00
|
|
|
#endif /* if defined(HAVE_VALLOC) */
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
} /* extern "C" */
|
|
|
|
#endif /* ifdef __cplusplus */
|
|
|
|
|
|
|
|
|
|
|
|
#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
|
|
|
|
* (2) the matching "fallible" operator delete further below
|
|
|
|
* (3) the matching system |operator delete(void*, std::nothrow)|
|
|
|
|
* (4) the matching system |operator delete(void*) throw(std::bad_alloc)|
|
|
|
|
*
|
|
|
|
* NB: these are declared |throw(std::bad_alloc)|, though they will never
|
|
|
|
* throw that exception. This declaration is consistent with the rule
|
|
|
|
* that |::operator new() throw(std::bad_alloc)| will never return NULL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/* 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-26 21:00:15 -08:00
|
|
|
# define MOZALLOC_EXPORT_NEW MFBT_API
|
2010-03-03 21:02:56 -08:00
|
|
|
#else
|
|
|
|
# define MOZALLOC_EXPORT_NEW
|
|
|
|
#endif
|
|
|
|
|
2014-09-26 12:45:45 -07:00
|
|
|
#if defined(ANDROID)
|
2010-06-16 09:54:25 -07:00
|
|
|
/*
|
2014-09-26 12:45:45 -07:00
|
|
|
* It's important to always specify 'throw()' in GCC because it's used to tell
|
|
|
|
* GCC that 'new' may return null. That makes GCC null-check the result before
|
|
|
|
* potentially initializing the memory to zero.
|
|
|
|
* Also, the Android minimalistic headers don't include std::bad_alloc.
|
2010-06-16 09:54:25 -07:00
|
|
|
*/
|
2014-09-26 12:45:45 -07:00
|
|
|
#define MOZALLOC_THROW_IF_HAS_EXCEPTIONS throw()
|
|
|
|
#define MOZALLOC_THROW_BAD_ALLOC_IF_HAS_EXCEPTIONS
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
/*
|
|
|
|
* Suppress build warning spam (bug 578546).
|
|
|
|
*/
|
|
|
|
#define MOZALLOC_THROW_IF_HAS_EXCEPTIONS
|
2011-10-18 11:19:23 -07:00
|
|
|
#define MOZALLOC_THROW_BAD_ALLOC_IF_HAS_EXCEPTIONS
|
2010-05-25 08:28:14 -07:00
|
|
|
#else
|
|
|
|
#define MOZALLOC_THROW_IF_HAS_EXCEPTIONS throw()
|
2011-10-18 11:19:23 -07:00
|
|
|
#define MOZALLOC_THROW_BAD_ALLOC_IF_HAS_EXCEPTIONS throw(std::bad_alloc)
|
2010-05-25 08:28:14 -07:00
|
|
|
#endif
|
|
|
|
|
2011-10-18 11:19:23 -07:00
|
|
|
#define MOZALLOC_THROW_BAD_ALLOC MOZALLOC_THROW_BAD_ALLOC_IF_HAS_EXCEPTIONS
|
2010-03-06 03:59:00 -08:00
|
|
|
|
2015-04-10 06:05:00 -07: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
|
|
|
|
MOZALLOC_INLINE
|
|
|
|
#endif
|
2010-03-06 03:59:00 -08:00
|
|
|
void* operator new(size_t size) MOZALLOC_THROW_BAD_ALLOC
|
2010-03-03 21:02:56 -08:00
|
|
|
{
|
|
|
|
return moz_xmalloc(size);
|
|
|
|
}
|
|
|
|
|
2012-01-12 12:35:48 -08:00
|
|
|
MOZALLOC_EXPORT_NEW MOZALLOC_INLINE
|
2010-05-25 08:28:14 -07:00
|
|
|
void* operator new(size_t size, const std::nothrow_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
|
2010-03-14 12:56:47 -07:00
|
|
|
{
|
2015-03-26 02:25:16 -07:00
|
|
|
return malloc_impl(size);
|
2010-03-14 12:56:47 -07:00
|
|
|
}
|
|
|
|
|
2012-01-12 12:35:48 -08:00
|
|
|
MOZALLOC_EXPORT_NEW MOZALLOC_INLINE
|
2010-03-06 03:59:00 -08:00
|
|
|
void* operator new[](size_t size) MOZALLOC_THROW_BAD_ALLOC
|
2010-03-03 21:02:56 -08:00
|
|
|
{
|
|
|
|
return moz_xmalloc(size);
|
|
|
|
}
|
|
|
|
|
2012-01-12 12:35:48 -08:00
|
|
|
MOZALLOC_EXPORT_NEW MOZALLOC_INLINE
|
2010-05-25 08:28:14 -07:00
|
|
|
void* operator new[](size_t size, const std::nothrow_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
|
2010-03-14 12:56:47 -07:00
|
|
|
{
|
2015-03-26 02:25:16 -07:00
|
|
|
return malloc_impl(size);
|
2010-03-14 12:56:47 -07:00
|
|
|
}
|
|
|
|
|
2012-01-12 12:35:48 -08:00
|
|
|
MOZALLOC_EXPORT_NEW MOZALLOC_INLINE
|
2010-05-25 08:28:14 -07:00
|
|
|
void operator delete(void* ptr) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
|
2010-03-03 21:02:56 -08:00
|
|
|
{
|
2015-03-26 02:25:16 -07:00
|
|
|
return free_impl(ptr);
|
2010-03-03 21:02:56 -08:00
|
|
|
}
|
|
|
|
|
2012-01-12 12:35:48 -08:00
|
|
|
MOZALLOC_EXPORT_NEW MOZALLOC_INLINE
|
2010-05-25 08:28:14 -07:00
|
|
|
void operator delete(void* ptr, const std::nothrow_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
|
2010-03-14 12:56:47 -07:00
|
|
|
{
|
2015-03-26 02:25:16 -07:00
|
|
|
return free_impl(ptr);
|
2010-03-14 12:56:47 -07:00
|
|
|
}
|
|
|
|
|
2012-01-12 12:35:48 -08:00
|
|
|
MOZALLOC_EXPORT_NEW MOZALLOC_INLINE
|
2010-05-25 08:28:14 -07:00
|
|
|
void operator delete[](void* ptr) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
|
2010-03-03 21:02:56 -08:00
|
|
|
{
|
2015-03-26 02:25:16 -07:00
|
|
|
return free_impl(ptr);
|
2010-03-03 21:02:56 -08:00
|
|
|
}
|
|
|
|
|
2012-01-12 12:35:48 -08:00
|
|
|
MOZALLOC_EXPORT_NEW MOZALLOC_INLINE
|
2010-05-25 08:28:14 -07:00
|
|
|
void operator delete[](void* ptr, const std::nothrow_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
|
2010-03-14 12:56:47 -07:00
|
|
|
{
|
2015-03-26 02:25:16 -07:00
|
|
|
return free_impl(ptr);
|
2010-03-14 12:56:47 -07:00
|
|
|
}
|
|
|
|
|
2010-03-03 21:02:56 -08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* We also add a new allocator variant: "fallible operator new."
|
|
|
|
* Unlike libmozalloc's implementations of the standard nofail
|
|
|
|
* allocators, this allocator is allowed to return NULL. It can be used
|
|
|
|
* as follows
|
|
|
|
*
|
|
|
|
* Foo* f = new (mozilla::fallible) Foo(...);
|
|
|
|
*
|
|
|
|
* operator delete(fallible) is defined for completeness only.
|
|
|
|
*
|
|
|
|
* Each operator new below returns a pointer to memory that can be
|
|
|
|
* delete'd by any of
|
|
|
|
*
|
|
|
|
* (1) the matching "fallible" operator delete below
|
|
|
|
* (2) the matching infallible operator delete above
|
|
|
|
* (3) the matching system |operator delete(void*, std::nothrow)|
|
|
|
|
* (4) the matching system |operator delete(void*) throw(std::bad_alloc)|
|
|
|
|
*/
|
|
|
|
|
2012-01-12 12:35:48 -08:00
|
|
|
MOZALLOC_INLINE
|
2010-05-25 08:28:14 -07:00
|
|
|
void* operator new(size_t size, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
|
2010-03-03 21:02:56 -08:00
|
|
|
{
|
2015-03-26 02:25:16 -07:00
|
|
|
return malloc_impl(size);
|
2010-03-03 21:02:56 -08:00
|
|
|
}
|
|
|
|
|
2012-01-12 12:35:48 -08:00
|
|
|
MOZALLOC_INLINE
|
2010-05-25 08:28:14 -07:00
|
|
|
void* operator new[](size_t size, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
|
2010-03-03 21:02:56 -08:00
|
|
|
{
|
2015-03-26 02:25:16 -07:00
|
|
|
return malloc_impl(size);
|
2010-03-03 21:02:56 -08:00
|
|
|
}
|
|
|
|
|
2012-01-12 12:35:48 -08:00
|
|
|
MOZALLOC_INLINE
|
2010-05-25 08:28:14 -07:00
|
|
|
void operator delete(void* ptr, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
|
2010-03-03 21:02:56 -08:00
|
|
|
{
|
2015-03-26 02:25:16 -07:00
|
|
|
free_impl(ptr);
|
2010-03-03 21:02:56 -08:00
|
|
|
}
|
|
|
|
|
2012-01-12 12:35:48 -08:00
|
|
|
MOZALLOC_INLINE
|
2010-05-25 08:28:14 -07:00
|
|
|
void operator delete[](void* ptr, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS
|
2010-03-03 21:02:56 -08:00
|
|
|
{
|
2015-03-26 02:25:16 -07:00
|
|
|
free_impl(ptr);
|
2010-03-03 21:02:56 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-12-08 14:45:10 -08:00
|
|
|
/*
|
|
|
|
* This policy is identical to MallocAllocPolicy, except it uses
|
2015-03-26 02:25:16 -07:00
|
|
|
* moz_xmalloc/moz_xcalloc/moz_xrealloc instead of
|
|
|
|
* malloc/calloc/realloc.
|
2014-12-08 14:45:10 -08:00
|
|
|
*/
|
|
|
|
class InfallibleAllocPolicy
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
template <typename T>
|
|
|
|
T* pod_malloc(size_t aNumElems)
|
|
|
|
{
|
|
|
|
if (aNumElems & mozilla::tl::MulOverflowMask<sizeof(T)>::value) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return static_cast<T*>(moz_xmalloc(aNumElems * sizeof(T)));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T* pod_calloc(size_t aNumElems)
|
|
|
|
{
|
|
|
|
return static_cast<T*>(moz_xcalloc(aNumElems, sizeof(T)));
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
T* pod_realloc(T* aPtr, size_t aOldSize, size_t aNewSize)
|
|
|
|
{
|
|
|
|
if (aNewSize & mozilla::tl::MulOverflowMask<sizeof(T)>::value) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return static_cast<T*>(moz_xrealloc(aPtr, aNewSize * sizeof(T)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void free_(void* aPtr)
|
|
|
|
{
|
2015-03-26 02:25:16 -07:00
|
|
|
free_impl(aPtr);
|
2014-12-08 14:45:10 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void reportAllocOverflow() const
|
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif /* ifdef __cplusplus */
|
2010-03-03 21:02:56 -08:00
|
|
|
|
2015-03-26 02:25:16 -07:00
|
|
|
#ifdef malloc_impl_
|
|
|
|
#undef malloc_impl_
|
|
|
|
#undef malloc_impl
|
|
|
|
#endif
|
|
|
|
#ifdef free_impl_
|
|
|
|
#undef free_impl_
|
|
|
|
#undef free_impl
|
|
|
|
#endif
|
|
|
|
|
2010-03-03 21:02:56 -08:00
|
|
|
#endif /* ifndef mozilla_mozalloc_h */
|