Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef nscore_h___
7 #define nscore_h___
9 /**
10 * Make sure that we have the proper platform specific
11 * c++ definitions needed by nscore.h
12 */
13 #ifndef _XPCOM_CONFIG_H_
14 #include "xpcom-config.h"
15 #endif
17 /* Definitions of functions and operators that allocate memory. */
18 #if !defined(XPCOM_GLUE) && !defined(NS_NO_XPCOM) && !defined(MOZ_NO_MOZALLOC)
19 # include "mozilla/mozalloc.h"
20 #endif
22 /**
23 * Incorporate the integer data types which XPCOM uses.
24 */
25 #include <stddef.h>
26 #include <stdint.h>
28 #ifdef __cplusplus
29 # include "mozilla/NullPtr.h"
30 #endif
32 #include "mozilla/RefCountType.h"
34 /* Core XPCOM declarations. */
36 /*----------------------------------------------------------------------*/
37 /* Import/export defines */
39 /**
40 * Using the visibility("hidden") attribute allows the compiler to use
41 * PC-relative addressing to call this function. If a function does not
42 * access any global data, and does not call any methods which are not either
43 * file-local or hidden, then on ELF systems we avoid loading the address of
44 * the PLT into a register at the start of the function, which reduces code
45 * size and frees up a register for general use.
46 *
47 * As a general rule, this should be used for any non-exported symbol
48 * (including virtual method implementations). NS_IMETHOD uses this by
49 * default; if you need to have your NS_IMETHOD functions exported, you can
50 * wrap your class as follows:
51 *
52 * #undef IMETHOD_VISIBILITY
53 * #define IMETHOD_VISIBILITY NS_VISIBILITY_DEFAULT
54 *
55 * class Foo {
56 * ...
57 * };
58 *
59 * #undef IMETHOD_VISIBILITY
60 * #define IMETHOD_VISIBILITY NS_VISIBILITY_HIDDEN
61 *
62 * Don't forget to change the visibility back to hidden before the end
63 * of a header!
64 *
65 * Other examples:
66 *
67 * NS_HIDDEN_(int) someMethod();
68 * SomeCtor() NS_HIDDEN;
69 */
71 #ifdef HAVE_VISIBILITY_HIDDEN_ATTRIBUTE
72 #define NS_VISIBILITY_HIDDEN __attribute__ ((visibility ("hidden")))
73 #else
74 #define NS_VISIBILITY_HIDDEN
75 #endif
77 #if defined(HAVE_VISIBILITY_ATTRIBUTE)
78 #define NS_VISIBILITY_DEFAULT __attribute__ ((visibility ("default")))
79 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
80 #define NS_VISIBILITY_DEFAULT __global
81 #else
82 #define NS_VISIBILITY_DEFAULT
83 #endif
85 #define NS_HIDDEN_(type) NS_VISIBILITY_HIDDEN type
86 #define NS_EXTERNAL_VIS_(type) NS_VISIBILITY_DEFAULT type
88 #define NS_HIDDEN NS_VISIBILITY_HIDDEN
89 #define NS_EXTERNAL_VIS NS_VISIBILITY_DEFAULT
91 #undef IMETHOD_VISIBILITY
92 #define IMETHOD_VISIBILITY NS_VISIBILITY_HIDDEN
94 /**
95 * Mark a function as using a potentially non-standard function calling
96 * convention. This can be used on functions that are called very
97 * frequently, to reduce the overhead of the function call. It is still worth
98 * using the macro for C++ functions which take no parameters since it allows
99 * passing |this| in a register.
100 *
101 * - Do not use this on any scriptable interface method since xptcall won't be
102 * aware of the different calling convention.
103 * - This must appear on the declaration, not the definition.
104 * - Adding this to a public function _will_ break binary compatibility.
105 * - This may be used on virtual functions but you must ensure it is applied
106 * to all implementations - the compiler will _not_ warn but it will crash.
107 * - This has no effect for functions which take a variable number of
108 * arguments.
109 * - __fastcall on windows should not be applied to class
110 * constructors/destructors - use the NS_CONSTRUCTOR_FASTCALL macro for
111 * constructors/destructors.
112 *
113 * Examples: int NS_FASTCALL func1(char *foo);
114 * NS_HIDDEN_(int) NS_FASTCALL func2(char *foo);
115 */
117 #if defined(__i386__) && defined(__GNUC__)
118 #define NS_FASTCALL __attribute__ ((regparm (3), stdcall))
119 #define NS_CONSTRUCTOR_FASTCALL __attribute__ ((regparm (3), stdcall))
120 #elif defined(XP_WIN) && !defined(_WIN64)
121 #define NS_FASTCALL __fastcall
122 #define NS_CONSTRUCTOR_FASTCALL
123 #else
124 #define NS_FASTCALL
125 #define NS_CONSTRUCTOR_FASTCALL
126 #endif
128 #ifdef XP_WIN
130 #define NS_IMPORT __declspec(dllimport)
131 #define NS_IMPORT_(type) __declspec(dllimport) type __stdcall
132 #define NS_EXPORT __declspec(dllexport)
133 #define NS_EXPORT_(type) __declspec(dllexport) type __stdcall
134 #define NS_IMETHOD_(type) virtual type __stdcall
135 #define NS_IMETHODIMP_(type) type __stdcall
136 #define NS_METHOD_(type) type __stdcall
137 #define NS_CALLBACK_(_type, _name) _type (__stdcall * _name)
138 #ifndef _WIN64
139 // Win64 has only one calling convention. __stdcall will be ignored by the compiler.
140 #define NS_STDCALL __stdcall
141 #define NS_HAVE_STDCALL
142 #else
143 #define NS_STDCALL
144 #endif
145 #define NS_FROZENCALL __cdecl
147 /*
148 These are needed to mark static members in exported classes, due to
149 gcc bug XXX insert bug# here.
150 */
152 #define NS_EXPORT_STATIC_MEMBER_(type) type
153 #define NS_IMPORT_STATIC_MEMBER_(type) type
155 #else
157 #define NS_IMPORT NS_EXTERNAL_VIS
158 #define NS_IMPORT_(type) NS_EXTERNAL_VIS_(type)
159 #define NS_EXPORT NS_EXTERNAL_VIS
160 #define NS_EXPORT_(type) NS_EXTERNAL_VIS_(type)
161 #define NS_IMETHOD_(type) virtual IMETHOD_VISIBILITY type
162 #define NS_IMETHODIMP_(type) type
163 #define NS_METHOD_(type) type
164 #define NS_CALLBACK_(_type, _name) _type (* _name)
165 #define NS_STDCALL
166 #define NS_FROZENCALL
167 #define NS_EXPORT_STATIC_MEMBER_(type) NS_EXTERNAL_VIS_(type)
168 #define NS_IMPORT_STATIC_MEMBER_(type) NS_EXTERNAL_VIS_(type)
170 #endif
172 /**
173 * Macro for creating typedefs for pointer-to-member types which are
174 * declared with stdcall. It is important to use this for any type which is
175 * declared as stdcall (i.e. NS_IMETHOD). For example, instead of writing:
176 *
177 * typedef nsresult (nsIFoo::*someType)(nsISupports* arg);
178 *
179 * you should write:
180 *
181 * typedef
182 * NS_STDCALL_FUNCPROTO(nsresult, someType, nsIFoo, typeFunc, (nsISupports*));
183 *
184 * where nsIFoo::typeFunc is any method declared as
185 * NS_IMETHOD typeFunc(nsISupports*);
186 *
187 * XXX this can be simplified to always use the non-typeof implementation
188 * when http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11893 is fixed.
189 */
191 #ifdef __GNUC__
192 #define NS_STDCALL_FUNCPROTO(ret, name, class, func, args) \
193 typeof(&class::func) name
194 #else
195 #define NS_STDCALL_FUNCPROTO(ret, name, class, func, args) \
196 ret (NS_STDCALL class::*name) args
197 #endif
199 /**
200 * Deprecated declarations.
201 */
202 #ifdef __GNUC__
203 # define MOZ_DEPRECATED __attribute__((deprecated))
204 #elif defined(_MSC_VER)
205 # define MOZ_DEPRECATED __declspec(deprecated)
206 #else
207 # define MOZ_DEPRECATED
208 #endif
210 /**
211 * Generic API modifiers which return the standard XPCOM nsresult type
212 */
213 #define NS_IMETHOD NS_IMETHOD_(nsresult)
214 #define NS_IMETHODIMP NS_IMETHODIMP_(nsresult)
215 #define NS_METHOD NS_METHOD_(nsresult)
216 #define NS_CALLBACK(_name) NS_CALLBACK_(nsresult, _name)
218 /**
219 * Import/Export macros for XPCOM APIs
220 */
222 #ifdef __cplusplus
223 #define NS_EXTERN_C extern "C"
224 #else
225 #define NS_EXTERN_C
226 #endif
228 #define EXPORT_XPCOM_API(type) NS_EXTERN_C NS_EXPORT type NS_FROZENCALL
229 #define IMPORT_XPCOM_API(type) NS_EXTERN_C NS_IMPORT type NS_FROZENCALL
230 #define GLUE_XPCOM_API(type) NS_EXTERN_C NS_HIDDEN_(type) NS_FROZENCALL
232 #ifdef IMPL_LIBXUL
233 #define XPCOM_API(type) EXPORT_XPCOM_API(type)
234 #elif defined(XPCOM_GLUE)
235 #define XPCOM_API(type) GLUE_XPCOM_API(type)
236 #else
237 #define XPCOM_API(type) IMPORT_XPCOM_API(type)
238 #endif
240 #ifdef MOZILLA_INTERNAL_API
241 # define NS_COM_GLUE
242 /*
243 The frozen string API has different definitions of nsAC?String
244 classes than the internal API. On systems that explicitly declare
245 dllexport symbols this is not a problem, but on ELF systems
246 internal symbols can accidentally "shine through"; we rename the
247 internal classes to avoid symbol conflicts.
248 */
249 # define nsAString nsAString_internal
250 # define nsACString nsACString_internal
251 #else
252 # ifdef HAVE_VISIBILITY_ATTRIBUTE
253 # define NS_COM_GLUE NS_VISIBILITY_HIDDEN
254 # else
255 # define NS_COM_GLUE
256 # endif
257 #endif
259 #if (defined(DEBUG) || defined(FORCE_BUILD_REFCNT_LOGGING))
260 /* Make refcnt logging part of the build. This doesn't mean that
261 * actual logging will occur (that requires a separate enable; see
262 * nsTraceRefcnt and nsISupportsImpl.h for more information). */
263 #define NS_BUILD_REFCNT_LOGGING
264 #endif
266 /* If NO_BUILD_REFCNT_LOGGING is defined then disable refcnt logging
267 * in the build. This overrides FORCE_BUILD_REFCNT_LOGGING. */
268 #if defined(NO_BUILD_REFCNT_LOGGING)
269 #undef NS_BUILD_REFCNT_LOGGING
270 #endif
272 /* If a program allocates memory for the lifetime of the app, it doesn't make
273 * sense to touch memory pages and free that memory at shutdown,
274 * unless we are running leak stats.
275 */
276 #if defined(NS_TRACE_MALLOC) || defined(NS_BUILD_REFCNT_LOGGING) || defined(MOZ_VALGRIND)
277 #define NS_FREE_PERMANENT_DATA
278 #endif
280 /**
281 * NS_NO_VTABLE is emitted by xpidl in interface declarations whenever
282 * xpidl can determine that the interface can't contain a constructor.
283 * This results in some space savings and possible runtime savings -
284 * see bug 49416. We undefine it first, as xpidl-generated headers
285 * define it for IDL uses that don't include this file.
286 */
287 #ifdef NS_NO_VTABLE
288 #undef NS_NO_VTABLE
289 #endif
290 #if defined(_MSC_VER)
291 #define NS_NO_VTABLE __declspec(novtable)
292 #else
293 #define NS_NO_VTABLE
294 #endif
297 /**
298 * Generic XPCOM result data type
299 */
300 #include "nsError.h"
302 typedef MozRefCountType nsrefcnt;
304 /*
305 * Use these macros to do 64bit safe pointer conversions.
306 */
308 #define NS_PTR_TO_INT32(x) ((int32_t) (intptr_t) (x))
309 #define NS_PTR_TO_UINT32(x) ((uint32_t) (intptr_t) (x))
310 #define NS_INT32_TO_PTR(x) ((void *) (intptr_t) (x))
312 /*
313 * Use NS_STRINGIFY to form a string literal from the value of a macro.
314 */
315 #define NS_STRINGIFY_HELPER(x_) #x_
316 #define NS_STRINGIFY(x_) NS_STRINGIFY_HELPER(x_)
318 /*
319 * If we're being linked as standalone glue, we don't want a dynamic
320 * dependency on NSPR libs, so we skip the debug thread-safety
321 * checks, and we cannot use the THREADSAFE_ISUPPORTS macros.
322 */
323 #if defined(XPCOM_GLUE) && !defined(XPCOM_GLUE_USE_NSPR)
324 #define XPCOM_GLUE_AVOID_NSPR
325 #endif
327 #if defined(HAVE_THREAD_TLS_KEYWORD)
328 #define NS_TLS __thread
329 #endif
331 /*
332 * SEH exception macros.
333 */
334 #ifdef HAVE_SEH_EXCEPTIONS
335 #define MOZ_SEH_TRY __try
336 #define MOZ_SEH_EXCEPT(expr) __except(expr)
337 #else
338 #define MOZ_SEH_TRY if(true)
339 #define MOZ_SEH_EXCEPT(expr) else
340 #endif
342 #endif /* nscore_h___ */