Bug 1867190 - Add prefs for PHC probablities r=glandium
[gecko.git] / xpcom / base / nscore.h
blob8881ddf5c8b548d62026cb63d2d656a12c1d6f85
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef nscore_h___
8 #define nscore_h___
10 /* Definitions of functions and operators that allocate memory. */
11 #if !defined(NS_NO_XPCOM) && !defined(MOZ_NO_MOZALLOC)
12 # include "mozilla/mozalloc.h"
13 #endif
15 /**
16 * Incorporate the integer data types which XPCOM uses.
18 #include <stddef.h> // IWYU pragma: export
19 #include <stdint.h> // IWYU pragma: export
21 #include "mozilla/HelperMacros.h" // IWYU pragma: export
22 #include "mozilla/RefCountType.h"
24 /* Core XPCOM declarations. */
26 /*----------------------------------------------------------------------*/
27 /* Import/export defines */
29 #ifdef HAVE_VISIBILITY_HIDDEN_ATTRIBUTE
30 # define NS_VISIBILITY_HIDDEN __attribute__((visibility("hidden")))
31 #else
32 # define NS_VISIBILITY_HIDDEN
33 #endif
35 #if defined(HAVE_VISIBILITY_ATTRIBUTE)
36 # define NS_VISIBILITY_DEFAULT __attribute__((visibility("default")))
37 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
38 # define NS_VISIBILITY_DEFAULT __global
39 #else
40 # define NS_VISIBILITY_DEFAULT
41 #endif
43 #define NS_HIDDEN_(type) NS_VISIBILITY_HIDDEN type
44 #define NS_EXTERNAL_VIS_(type) NS_VISIBILITY_DEFAULT type
46 #define NS_HIDDEN NS_VISIBILITY_HIDDEN
47 #define NS_EXTERNAL_VIS NS_VISIBILITY_DEFAULT
49 /**
50 * Mark a function as using a potentially non-standard function calling
51 * convention. This can be used on functions that are called very
52 * frequently, to reduce the overhead of the function call. It is still worth
53 * using the macro for C++ functions which take no parameters since it allows
54 * passing |this| in a register.
56 * - Do not use this on any scriptable interface method since xptcall won't be
57 * aware of the different calling convention.
58 * - This must appear on the declaration, not the definition.
59 * - Adding this to a public function _will_ break binary compatibility.
60 * - This may be used on virtual functions but you must ensure it is applied
61 * to all implementations - the compiler will _not_ warn but it will crash.
62 * - This has no effect for functions which take a variable number of
63 * arguments.
64 * - __fastcall on windows should not be applied to class
65 * constructors/destructors - use the NS_CONSTRUCTOR_FASTCALL macro for
66 * constructors/destructors.
68 * Examples: int NS_FASTCALL func1(char *foo);
69 * NS_HIDDEN_(int) NS_FASTCALL func2(char *foo);
72 #if defined(__i386__) && defined(__GNUC__)
73 # define NS_FASTCALL __attribute__((regparm(3), stdcall))
74 # define NS_CONSTRUCTOR_FASTCALL __attribute__((regparm(3), stdcall))
75 #elif defined(XP_WIN) && !defined(_WIN64)
76 # define NS_FASTCALL __fastcall
77 # define NS_CONSTRUCTOR_FASTCALL
78 #else
79 # define NS_FASTCALL
80 # define NS_CONSTRUCTOR_FASTCALL
81 #endif
83 /**
84 * Various API modifiers.
86 * - NS_IMETHOD/NS_IMETHOD_: use for in-class declarations and definitions.
87 * - NS_IMETHODIMP/NS_IMETHODIMP_: use for out-of-class definitions.
88 * - NS_METHOD_: usually used in conjunction with NS_CALLBACK_. Best avoided.
89 * - NS_CALLBACK_: used in some legacy situations. Best avoided.
92 #ifdef XP_WIN
94 # define NS_IMPORT __declspec(dllimport)
95 # define NS_IMPORT_(type) __declspec(dllimport) type __stdcall
96 # define NS_EXPORT __declspec(dllexport)
97 # define NS_EXPORT_(type) __declspec(dllexport) type __stdcall
98 # define NS_IMETHOD_(type) virtual type __stdcall
99 # define NS_IMETHODIMP_(type) type __stdcall
100 # define NS_METHOD_(type) type __stdcall
101 # define NS_CALLBACK_(_type, _name) _type(__stdcall* _name)
102 # ifndef _WIN64
103 // Win64 has only one calling convention. __stdcall will be ignored by the
104 // compiler.
105 # define NS_STDCALL __stdcall
106 # define NS_HAVE_STDCALL
107 # else
108 # define NS_STDCALL
109 # endif
110 # define NS_FROZENCALL __cdecl
112 #else
114 # define NS_IMPORT NS_EXTERNAL_VIS
115 # define NS_IMPORT_(type) NS_EXTERNAL_VIS_(type)
116 # define NS_EXPORT NS_EXTERNAL_VIS
117 # define NS_EXPORT_(type) NS_EXTERNAL_VIS_(type)
118 # define NS_IMETHOD_(type) virtual type
119 # define NS_IMETHODIMP_(type) type
120 # define NS_METHOD_(type) type
121 # define NS_CALLBACK_(_type, _name) _type(*_name)
122 # define NS_STDCALL
123 # define NS_FROZENCALL
125 #endif
127 #define NS_IMETHOD NS_IMETHOD_(nsresult)
128 #define NS_IMETHODIMP NS_IMETHODIMP_(nsresult)
131 * Import/Export macros for XPCOM APIs
134 #define EXPORT_XPCOM_API(type) type
135 #define IMPORT_XPCOM_API(type) type
136 #define GLUE_XPCOM_API(type) type
138 #ifdef __cplusplus
139 # define NS_EXTERN_C extern "C"
140 #else
141 # define NS_EXTERN_C
142 #endif
144 #define XPCOM_API(type) NS_EXTERN_C type
146 /* If a program allocates memory for the lifetime of the app, it doesn't make
147 * sense to touch memory pages and free that memory at shutdown,
148 * unless we are running leak stats.
150 * Note that we're also setting this for code coverage and pgo profile
151 * generation, because both of those require atexit hooks, which won't fire
152 * if we're using _exit. Bug 1555974 covers improving this.
155 #ifndef NS_FREE_PERMANENT_DATA
156 # if defined(NS_BUILD_REFCNT_LOGGING) || defined(MOZ_VALGRIND) || \
157 defined(MOZ_ASAN) || defined(MOZ_TSAN) || defined(MOZ_CODE_COVERAGE) || \
158 defined(MOZ_PROFILE_GENERATE) || defined(JS_STRUCTURED_SPEW)
159 # define NS_FREE_PERMANENT_DATA
160 # endif
161 #endif
164 * NS_NO_VTABLE is emitted by xpidl in interface declarations whenever
165 * xpidl can determine that the interface can't contain a constructor.
166 * This results in some space savings and possible runtime savings -
167 * see bug 49416. We undefine it first, as xpidl-generated headers
168 * define it for IDL uses that don't include this file.
170 #ifdef NS_NO_VTABLE
171 # undef NS_NO_VTABLE
172 #endif
173 #if defined(_MSC_VER)
174 # define NS_NO_VTABLE __declspec(novtable)
175 #else
176 # define NS_NO_VTABLE
177 #endif
180 * Generic XPCOM result data type
182 #include "nsError.h" // IWYU pragma: export
184 typedef MozRefCountType nsrefcnt;
186 namespace mozilla {
187 // Extensions to the mozilla::Result type for handling of nsresult values.
189 // Note that these specializations need to be defined before Result.h is
190 // included, or we run into explicit specialization after instantiation errors,
191 // especially if Result.h is used in multiple sources in a unified compile.
193 namespace detail {
194 // When used as an error value, nsresult should never be NS_OK.
195 // This specialization allows us to pack Result<Ok, nsresult> into a
196 // nsresult-sized value.
197 template <typename T>
198 struct UnusedZero;
199 template <>
200 struct UnusedZero<nsresult> {
201 using StorageType = nsresult;
203 static constexpr bool value = true;
204 static constexpr StorageType nullValue = NS_OK;
206 static constexpr void AssertValid(StorageType aValue) {}
207 static constexpr const nsresult& Inspect(const StorageType& aValue) {
208 return aValue;
210 static constexpr nsresult Unwrap(StorageType aValue) { return aValue; }
211 static constexpr StorageType Store(nsresult aValue) { return aValue; }
213 } // namespace detail
215 template <typename T>
216 class GenericErrorResult;
217 template <>
218 class GenericErrorResult<nsresult>;
220 struct Ok;
221 template <typename V, typename E>
222 class Result;
224 // Allow MOZ_TRY to handle `nsresult` values.
225 template <typename E = nsresult>
226 inline Result<Ok, E> ToResult(nsresult aValue);
227 } // namespace mozilla
230 * Use these macros to do 64bit safe pointer conversions.
233 #define NS_PTR_TO_INT32(x) ((int32_t)(intptr_t)(x))
234 #define NS_PTR_TO_UINT32(x) ((uint32_t)(intptr_t)(x))
235 #define NS_INT32_TO_PTR(x) ((void*)(intptr_t)(x))
238 * If we're being linked as standalone glue, we don't want a dynamic
239 * dependency on NSPR libs, so we skip the debug thread-safety
240 * checks, and we cannot use the THREADSAFE_ISUPPORTS macros.
242 #if defined(XPCOM_GLUE) && !defined(XPCOM_GLUE_USE_NSPR)
243 # define XPCOM_GLUE_AVOID_NSPR
244 #endif
247 * SEH exception macros.
249 #ifdef HAVE_SEH_EXCEPTIONS
250 # define MOZ_SEH_TRY __try
251 # define MOZ_SEH_EXCEPT(expr) __except (expr)
252 #else
253 # define MOZ_SEH_TRY if (true)
254 # define MOZ_SEH_EXCEPT(expr) else
255 #endif
257 #endif /* nscore_h___ */