1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/memory/mozalloc/mozalloc.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,293 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 1.5 + * vim: sw=4 ts=4 et : 1.6 + */ 1.7 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.8 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.9 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.10 + 1.11 +#ifndef mozilla_mozalloc_h 1.12 +#define mozilla_mozalloc_h 1.13 + 1.14 +/* 1.15 + * https://bugzilla.mozilla.org/show_bug.cgi?id=427099 1.16 + */ 1.17 + 1.18 +#include <stdlib.h> 1.19 +#include <string.h> 1.20 +#if defined(__cplusplus) 1.21 +# include <new> 1.22 +#endif 1.23 +#include "xpcom-config.h" 1.24 + 1.25 +#if defined(__cplusplus) 1.26 +#include "mozilla/fallible.h" 1.27 +#endif 1.28 +#include "mozilla/Attributes.h" 1.29 + 1.30 +#define MOZALLOC_HAVE_XMALLOC 1.31 + 1.32 +#if defined(MOZALLOC_EXPORT) 1.33 +/* do nothing: it's been defined to __declspec(dllexport) by 1.34 + * mozalloc*.cpp on platforms where that's required. */ 1.35 +#elif defined(XP_WIN) 1.36 +# define MOZALLOC_EXPORT __declspec(dllimport) 1.37 +#elif defined(HAVE_VISIBILITY_ATTRIBUTE) 1.38 +/* Make sure symbols are still exported even if we're wrapped in a 1.39 + * |visibility push(hidden)| blanket. */ 1.40 +# define MOZALLOC_EXPORT __attribute__ ((visibility ("default"))) 1.41 +#else 1.42 +# define MOZALLOC_EXPORT 1.43 +#endif 1.44 + 1.45 + 1.46 +#if defined(MOZ_ALWAYS_INLINE_EVEN_DEBUG) 1.47 +# define MOZALLOC_INLINE MOZ_ALWAYS_INLINE_EVEN_DEBUG 1.48 +#elif defined(HAVE_FORCEINLINE) 1.49 +# define MOZALLOC_INLINE __forceinline 1.50 +#else 1.51 +# define MOZALLOC_INLINE inline 1.52 +#endif 1.53 + 1.54 +/* Workaround build problem with Sun Studio 12 */ 1.55 +#if defined(__SUNPRO_C) || defined(__SUNPRO_CC) 1.56 +# undef NS_WARN_UNUSED_RESULT 1.57 +# define NS_WARN_UNUSED_RESULT 1.58 +# undef NS_ATTR_MALLOC 1.59 +# define NS_ATTR_MALLOC 1.60 +#endif 1.61 + 1.62 +#if defined(__cplusplus) 1.63 +extern "C" { 1.64 +#endif /* ifdef __cplusplus */ 1.65 + 1.66 + 1.67 +/* 1.68 + * Each pair of declarations below is analogous to a "standard" 1.69 + * allocation function, except that the out-of-memory handling is made 1.70 + * explicit. The |moz_x| versions will never return a NULL pointer; 1.71 + * if memory is exhausted, they abort. The |moz_| versions may return 1.72 + * NULL pointers if memory is exhausted: their return value must be 1.73 + * checked. 1.74 + * 1.75 + * All these allocation functions are *guaranteed* to return a pointer 1.76 + * to memory allocated in such a way that that memory can be freed by 1.77 + * passing that pointer to |moz_free()|. 1.78 + */ 1.79 + 1.80 +MOZALLOC_EXPORT 1.81 +void moz_free(void* ptr); 1.82 + 1.83 +MOZALLOC_EXPORT void* moz_xmalloc(size_t size) 1.84 + NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; 1.85 + 1.86 +MOZALLOC_EXPORT 1.87 +void* moz_malloc(size_t size) 1.88 + NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; 1.89 + 1.90 + 1.91 +MOZALLOC_EXPORT void* moz_xcalloc(size_t nmemb, size_t size) 1.92 + NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; 1.93 + 1.94 +MOZALLOC_EXPORT void* moz_calloc(size_t nmemb, size_t size) 1.95 + NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; 1.96 + 1.97 + 1.98 +MOZALLOC_EXPORT void* moz_xrealloc(void* ptr, size_t size) 1.99 + NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; 1.100 + 1.101 +MOZALLOC_EXPORT void* moz_realloc(void* ptr, size_t size) 1.102 + NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; 1.103 + 1.104 + 1.105 +MOZALLOC_EXPORT char* moz_xstrdup(const char* str) 1.106 + NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; 1.107 + 1.108 +MOZALLOC_EXPORT char* moz_strdup(const char* str) 1.109 + NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; 1.110 + 1.111 +MOZALLOC_EXPORT size_t moz_malloc_usable_size(void *ptr); 1.112 + 1.113 +MOZALLOC_EXPORT size_t moz_malloc_size_of(const void *ptr); 1.114 + 1.115 +#if defined(HAVE_STRNDUP) 1.116 +MOZALLOC_EXPORT char* moz_xstrndup(const char* str, size_t strsize) 1.117 + NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; 1.118 + 1.119 +MOZALLOC_EXPORT char* moz_strndup(const char* str, size_t strsize) 1.120 + NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; 1.121 +#endif /* if defined(HAVE_STRNDUP) */ 1.122 + 1.123 + 1.124 +#if defined(HAVE_POSIX_MEMALIGN) 1.125 +MOZALLOC_EXPORT int moz_xposix_memalign(void **ptr, size_t alignment, size_t size) 1.126 + NS_WARN_UNUSED_RESULT; 1.127 + 1.128 +MOZALLOC_EXPORT int moz_posix_memalign(void **ptr, size_t alignment, size_t size) 1.129 + NS_WARN_UNUSED_RESULT; 1.130 +#endif /* if defined(HAVE_POSIX_MEMALIGN) */ 1.131 + 1.132 + 1.133 +#if defined(HAVE_MEMALIGN) 1.134 +MOZALLOC_EXPORT void* moz_xmemalign(size_t boundary, size_t size) 1.135 + NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; 1.136 + 1.137 +MOZALLOC_EXPORT void* moz_memalign(size_t boundary, size_t size) 1.138 + NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; 1.139 +#endif /* if defined(HAVE_MEMALIGN) */ 1.140 + 1.141 + 1.142 +#if defined(HAVE_VALLOC) 1.143 +MOZALLOC_EXPORT void* moz_xvalloc(size_t size) 1.144 + NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; 1.145 + 1.146 +MOZALLOC_EXPORT void* moz_valloc(size_t size) 1.147 + NS_ATTR_MALLOC NS_WARN_UNUSED_RESULT; 1.148 +#endif /* if defined(HAVE_VALLOC) */ 1.149 + 1.150 + 1.151 +#ifdef __cplusplus 1.152 +} /* extern "C" */ 1.153 +#endif /* ifdef __cplusplus */ 1.154 + 1.155 + 1.156 +#ifdef __cplusplus 1.157 + 1.158 +/* 1.159 + * We implement the default operators new/delete as part of 1.160 + * libmozalloc, replacing their definitions in libstdc++. The 1.161 + * operator new* definitions in libmozalloc will never return a NULL 1.162 + * pointer. 1.163 + * 1.164 + * Each operator new immediately below returns a pointer to memory 1.165 + * that can be delete'd by any of 1.166 + * 1.167 + * (1) the matching infallible operator delete immediately below 1.168 + * (2) the matching "fallible" operator delete further below 1.169 + * (3) the matching system |operator delete(void*, std::nothrow)| 1.170 + * (4) the matching system |operator delete(void*) throw(std::bad_alloc)| 1.171 + * 1.172 + * NB: these are declared |throw(std::bad_alloc)|, though they will never 1.173 + * throw that exception. This declaration is consistent with the rule 1.174 + * that |::operator new() throw(std::bad_alloc)| will never return NULL. 1.175 + */ 1.176 + 1.177 +/* NB: This is defined just to silence vacuous warnings about symbol 1.178 + * visibility on OS X/gcc. These symbols are force-inline and not 1.179 + * exported. */ 1.180 +#if defined(XP_MACOSX) 1.181 +# define MOZALLOC_EXPORT_NEW MOZALLOC_EXPORT 1.182 +#else 1.183 +# define MOZALLOC_EXPORT_NEW 1.184 +#endif 1.185 + 1.186 +#if defined(ANDROID) || defined(_MSC_VER) 1.187 +/* 1.188 + * Android doesn't fully support exceptions, so its <new> header 1.189 + * has operators that don't specify throw() at all. Also include MSVC 1.190 + * to suppress build warning spam (bug 578546). 1.191 + */ 1.192 +#define MOZALLOC_THROW_IF_HAS_EXCEPTIONS /**/ 1.193 +#define MOZALLOC_THROW_BAD_ALLOC_IF_HAS_EXCEPTIONS 1.194 +#else 1.195 +#define MOZALLOC_THROW_IF_HAS_EXCEPTIONS throw() 1.196 +#define MOZALLOC_THROW_BAD_ALLOC_IF_HAS_EXCEPTIONS throw(std::bad_alloc) 1.197 +#endif 1.198 + 1.199 +#define MOZALLOC_THROW_BAD_ALLOC MOZALLOC_THROW_BAD_ALLOC_IF_HAS_EXCEPTIONS 1.200 + 1.201 +MOZALLOC_EXPORT_NEW MOZALLOC_INLINE 1.202 +void* operator new(size_t size) MOZALLOC_THROW_BAD_ALLOC 1.203 +{ 1.204 + return moz_xmalloc(size); 1.205 +} 1.206 + 1.207 +MOZALLOC_EXPORT_NEW MOZALLOC_INLINE 1.208 +void* operator new(size_t size, const std::nothrow_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS 1.209 +{ 1.210 + return moz_malloc(size); 1.211 +} 1.212 + 1.213 +MOZALLOC_EXPORT_NEW MOZALLOC_INLINE 1.214 +void* operator new[](size_t size) MOZALLOC_THROW_BAD_ALLOC 1.215 +{ 1.216 + return moz_xmalloc(size); 1.217 +} 1.218 + 1.219 +MOZALLOC_EXPORT_NEW MOZALLOC_INLINE 1.220 +void* operator new[](size_t size, const std::nothrow_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS 1.221 +{ 1.222 + return moz_malloc(size); 1.223 +} 1.224 + 1.225 +MOZALLOC_EXPORT_NEW MOZALLOC_INLINE 1.226 +void operator delete(void* ptr) MOZALLOC_THROW_IF_HAS_EXCEPTIONS 1.227 +{ 1.228 + return moz_free(ptr); 1.229 +} 1.230 + 1.231 +MOZALLOC_EXPORT_NEW MOZALLOC_INLINE 1.232 +void operator delete(void* ptr, const std::nothrow_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS 1.233 +{ 1.234 + return moz_free(ptr); 1.235 +} 1.236 + 1.237 +MOZALLOC_EXPORT_NEW MOZALLOC_INLINE 1.238 +void operator delete[](void* ptr) MOZALLOC_THROW_IF_HAS_EXCEPTIONS 1.239 +{ 1.240 + return moz_free(ptr); 1.241 +} 1.242 + 1.243 +MOZALLOC_EXPORT_NEW MOZALLOC_INLINE 1.244 +void operator delete[](void* ptr, const std::nothrow_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS 1.245 +{ 1.246 + return moz_free(ptr); 1.247 +} 1.248 + 1.249 + 1.250 +/* 1.251 + * We also add a new allocator variant: "fallible operator new." 1.252 + * Unlike libmozalloc's implementations of the standard nofail 1.253 + * allocators, this allocator is allowed to return NULL. It can be used 1.254 + * as follows 1.255 + * 1.256 + * Foo* f = new (mozilla::fallible) Foo(...); 1.257 + * 1.258 + * operator delete(fallible) is defined for completeness only. 1.259 + * 1.260 + * Each operator new below returns a pointer to memory that can be 1.261 + * delete'd by any of 1.262 + * 1.263 + * (1) the matching "fallible" operator delete below 1.264 + * (2) the matching infallible operator delete above 1.265 + * (3) the matching system |operator delete(void*, std::nothrow)| 1.266 + * (4) the matching system |operator delete(void*) throw(std::bad_alloc)| 1.267 + */ 1.268 + 1.269 +MOZALLOC_INLINE 1.270 +void* operator new(size_t size, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS 1.271 +{ 1.272 + return moz_malloc(size); 1.273 +} 1.274 + 1.275 +MOZALLOC_INLINE 1.276 +void* operator new[](size_t size, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS 1.277 +{ 1.278 + return moz_malloc(size); 1.279 +} 1.280 + 1.281 +MOZALLOC_INLINE 1.282 +void operator delete(void* ptr, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS 1.283 +{ 1.284 + moz_free(ptr); 1.285 +} 1.286 + 1.287 +MOZALLOC_INLINE 1.288 +void operator delete[](void* ptr, const mozilla::fallible_t&) MOZALLOC_THROW_IF_HAS_EXCEPTIONS 1.289 +{ 1.290 + moz_free(ptr); 1.291 +} 1.292 + 1.293 +#endif /* ifdef __cplusplus */ 1.294 + 1.295 + 1.296 +#endif /* ifndef mozilla_mozalloc_h */