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/. */
6 
7 #ifndef nscore_h___
8 #define nscore_h___
9 
10 /**
11  * Make sure that we have the proper platform specific
12  * c++ definitions needed by nscore.h
13  */
14 #ifndef _XPCOM_CONFIG_H_
15 #  include "xpcom-config.h"  // IWYU pragma: export
16 #endif
17 
18 /* Definitions of functions and operators that allocate memory. */
19 #if !defined(NS_NO_XPCOM) && !defined(MOZ_NO_MOZALLOC)
20 #  include "mozilla/mozalloc.h"
21 #endif
22 
23 /**
24  * Incorporate the integer data types which XPCOM uses.
25  */
26 #include <stddef.h>  // IWYU pragma: export
27 #include <stdint.h>  // IWYU pragma: export
28 
29 #include "mozilla/HelperMacros.h"  // IWYU pragma: export
30 #include "mozilla/RefCountType.h"
31 
32 /* Core XPCOM declarations. */
33 
34 /*----------------------------------------------------------------------*/
35 /* Import/export defines */
36 
37 #ifdef HAVE_VISIBILITY_HIDDEN_ATTRIBUTE
38 #  define NS_VISIBILITY_HIDDEN __attribute__((visibility("hidden")))
39 #else
40 #  define NS_VISIBILITY_HIDDEN
41 #endif
42 
43 #if defined(HAVE_VISIBILITY_ATTRIBUTE)
44 #  define NS_VISIBILITY_DEFAULT __attribute__((visibility("default")))
45 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
46 #  define NS_VISIBILITY_DEFAULT __global
47 #else
48 #  define NS_VISIBILITY_DEFAULT
49 #endif
50 
51 #define NS_HIDDEN_(type) NS_VISIBILITY_HIDDEN type
52 #define NS_EXTERNAL_VIS_(type) NS_VISIBILITY_DEFAULT type
53 
54 #define NS_HIDDEN NS_VISIBILITY_HIDDEN
55 #define NS_EXTERNAL_VIS NS_VISIBILITY_DEFAULT
56 
57 /**
58  * Mark a function as using a potentially non-standard function calling
59  * convention.  This can be used on functions that are called very
60  * frequently, to reduce the overhead of the function call.  It is still worth
61  * using the macro for C++ functions which take no parameters since it allows
62  * passing |this| in a register.
63  *
64  *  - Do not use this on any scriptable interface method since xptcall won't be
65  *    aware of the different calling convention.
66  *  - This must appear on the declaration, not the definition.
67  *  - Adding this to a public function _will_ break binary compatibility.
68  *  - This may be used on virtual functions but you must ensure it is applied
69  *    to all implementations - the compiler will _not_ warn but it will crash.
70  *  - This has no effect for functions which take a variable number of
71  *    arguments.
72  *  - __fastcall on windows should not be applied to class
73  *    constructors/destructors - use the NS_CONSTRUCTOR_FASTCALL macro for
74  *    constructors/destructors.
75  *
76  * Examples: int NS_FASTCALL func1(char *foo);
77  *           NS_HIDDEN_(int) NS_FASTCALL func2(char *foo);
78  */
79 
80 #if defined(__i386__) && defined(__GNUC__)
81 #  define NS_FASTCALL __attribute__((regparm(3), stdcall))
82 #  define NS_CONSTRUCTOR_FASTCALL __attribute__((regparm(3), stdcall))
83 #elif defined(XP_WIN) && !defined(_WIN64)
84 #  define NS_FASTCALL __fastcall
85 #  define NS_CONSTRUCTOR_FASTCALL
86 #else
87 #  define NS_FASTCALL
88 #  define NS_CONSTRUCTOR_FASTCALL
89 #endif
90 
91 /**
92  * Various API modifiers.
93  *
94  * - NS_IMETHOD/NS_IMETHOD_: use for in-class declarations and definitions.
95  * - NS_IMETHODIMP/NS_IMETHODIMP_: use for out-of-class definitions.
96  * - NS_METHOD_: usually used in conjunction with NS_CALLBACK_. Best avoided.
97  * - NS_CALLBACK_: used in some legacy situations. Best avoided.
98  */
99 
100 #ifdef XP_WIN
101 
102 #  define NS_IMPORT __declspec(dllimport)
103 #  define NS_IMPORT_(type) __declspec(dllimport) type __stdcall
104 #  define NS_EXPORT __declspec(dllexport)
105 #  define NS_EXPORT_(type) __declspec(dllexport) type __stdcall
106 #  define NS_IMETHOD_(type) virtual type __stdcall
107 #  define NS_IMETHODIMP_(type) type __stdcall
108 #  define NS_METHOD_(type) type __stdcall
109 #  define NS_CALLBACK_(_type, _name) _type(__stdcall* _name)
110 #  ifndef _WIN64
111 // Win64 has only one calling convention.  __stdcall will be ignored by the
112 // compiler.
113 #    define NS_STDCALL __stdcall
114 #    define NS_HAVE_STDCALL
115 #  else
116 #    define NS_STDCALL
117 #  endif
118 #  define NS_FROZENCALL __cdecl
119 
120 #else
121 
122 #  define NS_IMPORT NS_EXTERNAL_VIS
123 #  define NS_IMPORT_(type) NS_EXTERNAL_VIS_(type)
124 #  define NS_EXPORT NS_EXTERNAL_VIS
125 #  define NS_EXPORT_(type) NS_EXTERNAL_VIS_(type)
126 #  define NS_IMETHOD_(type) virtual type
127 #  define NS_IMETHODIMP_(type) type
128 #  define NS_METHOD_(type) type
129 #  define NS_CALLBACK_(_type, _name) _type(*_name)
130 #  define NS_STDCALL
131 #  define NS_FROZENCALL
132 
133 #endif
134 
135 #define NS_IMETHOD NS_IMETHOD_(nsresult)
136 #define NS_IMETHODIMP NS_IMETHODIMP_(nsresult)
137 
138 /**
139  * Import/Export macros for XPCOM APIs
140  */
141 
142 #define EXPORT_XPCOM_API(type) type
143 #define IMPORT_XPCOM_API(type) type
144 #define GLUE_XPCOM_API(type) type
145 
146 #ifdef __cplusplus
147 #  define NS_EXTERN_C extern "C"
148 #else
149 #  define NS_EXTERN_C
150 #endif
151 
152 #define XPCOM_API(type) NS_EXTERN_C type
153 
154 #if (defined(DEBUG) || defined(FORCE_BUILD_REFCNT_LOGGING))
155 /* Make refcnt logging part of the build. This doesn't mean that
156  * actual logging will occur (that requires a separate enable; see
157  * nsTraceRefcnt and nsISupportsImpl.h for more information).  */
158 #  define NS_BUILD_REFCNT_LOGGING
159 #endif
160 
161 /* If NO_BUILD_REFCNT_LOGGING is defined then disable refcnt logging
162  * in the build. This overrides FORCE_BUILD_REFCNT_LOGGING. */
163 #if defined(NO_BUILD_REFCNT_LOGGING)
164 #  undef NS_BUILD_REFCNT_LOGGING
165 #endif
166 
167 /* If a program allocates memory for the lifetime of the app, it doesn't make
168  * sense to touch memory pages and free that memory at shutdown,
169  * unless we are running leak stats.
170  *
171  * Note that we're also setting this for code coverage and pgo profile
172  * generation, because both of those require atexit hooks, which won't fire
173  * if we're using _exit. Bug 1555974 covers improving this.
174  *
175  */
176 #ifndef NS_FREE_PERMANENT_DATA
177 #  if defined(NS_BUILD_REFCNT_LOGGING) || defined(MOZ_VALGRIND) ||            \
178       defined(MOZ_ASAN) || defined(MOZ_TSAN) || defined(MOZ_CODE_COVERAGE) || \
179       defined(MOZ_PROFILE_GENERATE) || defined(JS_STRUCTURED_SPEW)
180 #    define NS_FREE_PERMANENT_DATA
181 #  endif
182 #endif
183 
184 /**
185  * NS_NO_VTABLE is emitted by xpidl in interface declarations whenever
186  * xpidl can determine that the interface can't contain a constructor.
187  * This results in some space savings and possible runtime savings -
188  * see bug 49416.  We undefine it first, as xpidl-generated headers
189  * define it for IDL uses that don't include this file.
190  */
191 #ifdef NS_NO_VTABLE
192 #  undef NS_NO_VTABLE
193 #endif
194 #if defined(_MSC_VER)
195 #  define NS_NO_VTABLE __declspec(novtable)
196 #else
197 #  define NS_NO_VTABLE
198 #endif
199 
200 /**
201  * Generic XPCOM result data type
202  */
203 #include "nsError.h"  // IWYU pragma: export
204 
205 typedef MozRefCountType nsrefcnt;
206 
207 namespace mozilla {
208 // Extensions to the mozilla::Result type for handling of nsresult values.
209 //
210 // Note that these specializations need to be defined before Result.h is
211 // included, or we run into explicit specialization after instantiation errors,
212 // especially if Result.h is used in multiple sources in a unified compile.
213 
214 namespace detail {
215 // When used as an error value, nsresult should never be NS_OK.
216 // This specialization allows us to pack Result<Ok, nsresult> into a
217 // nsresult-sized value.
218 template <typename T>
219 struct UnusedZero;
220 template <>
221 struct UnusedZero<nsresult> {
222   using StorageType = nsresult;
223 
224   static constexpr bool value = true;
225   static constexpr StorageType nullValue = NS_OK;
226 
227   static constexpr void AssertValid(StorageType aValue) {}
228   static constexpr const nsresult& Inspect(const StorageType& aValue) {
229     return aValue;
230   }
231   static constexpr nsresult Unwrap(StorageType aValue) { return aValue; }
232   static constexpr StorageType Store(nsresult aValue) { return aValue; }
233 };
234 }  // namespace detail
235 
236 template <typename T>
237 class MOZ_MUST_USE_TYPE GenericErrorResult;
238 template <>
239 class MOZ_MUST_USE_TYPE GenericErrorResult<nsresult>;
240 
241 struct Ok;
242 template <typename V, typename E>
243 class Result;
244 
245 // Allow MOZ_TRY to handle `nsresult` values.
246 inline Result<Ok, nsresult> ToResult(nsresult aValue);
247 }  // namespace mozilla
248 
249 /*
250  * Use these macros to do 64bit safe pointer conversions.
251  */
252 
253 #define NS_PTR_TO_INT32(x) ((int32_t)(intptr_t)(x))
254 #define NS_PTR_TO_UINT32(x) ((uint32_t)(intptr_t)(x))
255 #define NS_INT32_TO_PTR(x) ((void*)(intptr_t)(x))
256 
257 /*
258  * If we're being linked as standalone glue, we don't want a dynamic
259  * dependency on NSPR libs, so we skip the debug thread-safety
260  * checks, and we cannot use the THREADSAFE_ISUPPORTS macros.
261  */
262 #if defined(XPCOM_GLUE) && !defined(XPCOM_GLUE_USE_NSPR)
263 #  define XPCOM_GLUE_AVOID_NSPR
264 #endif
265 
266 /*
267  * SEH exception macros.
268  */
269 #ifdef HAVE_SEH_EXCEPTIONS
270 #  define MOZ_SEH_TRY __try
271 #  define MOZ_SEH_EXCEPT(expr) __except (expr)
272 #else
273 #  define MOZ_SEH_TRY if (true)
274 #  define MOZ_SEH_EXCEPT(expr) else
275 #endif
276 
277 #endif /* nscore_h___ */
278