Bumping manifests a=b2g-bump
[gecko.git] / xpcom / base / nscore.h
blob31badbcb9acf14b440c286f842c1c61eb6312c42
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 /**
11 * Make sure that we have the proper platform specific
12 * c++ definitions needed by nscore.h
14 #ifndef _XPCOM_CONFIG_H_
15 #include "xpcom-config.h"
16 #endif
18 /* Definitions of functions and operators that allocate memory. */
19 #if !defined(XPCOM_GLUE) && !defined(NS_NO_XPCOM) && !defined(MOZ_NO_MOZALLOC)
20 # include "mozilla/mozalloc.h"
21 #endif
23 /**
24 * Incorporate the integer data types which XPCOM uses.
26 #include <stddef.h>
27 #include <stdint.h>
29 #ifdef __cplusplus
30 # include "mozilla/NullPtr.h"
31 #endif
33 #include "mozilla/RefCountType.h"
35 /* Core XPCOM declarations. */
37 /*----------------------------------------------------------------------*/
38 /* Import/export defines */
40 #ifdef HAVE_VISIBILITY_HIDDEN_ATTRIBUTE
41 #define NS_VISIBILITY_HIDDEN __attribute__ ((visibility ("hidden")))
42 #else
43 #define NS_VISIBILITY_HIDDEN
44 #endif
46 #if defined(HAVE_VISIBILITY_ATTRIBUTE)
47 #define NS_VISIBILITY_DEFAULT __attribute__ ((visibility ("default")))
48 #elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
49 #define NS_VISIBILITY_DEFAULT __global
50 #else
51 #define NS_VISIBILITY_DEFAULT
52 #endif
54 #define NS_HIDDEN_(type) NS_VISIBILITY_HIDDEN type
55 #define NS_EXTERNAL_VIS_(type) NS_VISIBILITY_DEFAULT type
57 #define NS_HIDDEN NS_VISIBILITY_HIDDEN
58 #define NS_EXTERNAL_VIS NS_VISIBILITY_DEFAULT
60 /**
61 * Mark a function as using a potentially non-standard function calling
62 * convention. This can be used on functions that are called very
63 * frequently, to reduce the overhead of the function call. It is still worth
64 * using the macro for C++ functions which take no parameters since it allows
65 * passing |this| in a register.
67 * - Do not use this on any scriptable interface method since xptcall won't be
68 * aware of the different calling convention.
69 * - This must appear on the declaration, not the definition.
70 * - Adding this to a public function _will_ break binary compatibility.
71 * - This may be used on virtual functions but you must ensure it is applied
72 * to all implementations - the compiler will _not_ warn but it will crash.
73 * - This has no effect for functions which take a variable number of
74 * arguments.
75 * - __fastcall on windows should not be applied to class
76 * constructors/destructors - use the NS_CONSTRUCTOR_FASTCALL macro for
77 * constructors/destructors.
79 * Examples: int NS_FASTCALL func1(char *foo);
80 * NS_HIDDEN_(int) NS_FASTCALL func2(char *foo);
83 #if defined(__i386__) && defined(__GNUC__)
84 #define NS_FASTCALL __attribute__ ((regparm (3), stdcall))
85 #define NS_CONSTRUCTOR_FASTCALL __attribute__ ((regparm (3), stdcall))
86 #elif defined(XP_WIN) && !defined(_WIN64)
87 #define NS_FASTCALL __fastcall
88 #define NS_CONSTRUCTOR_FASTCALL
89 #else
90 #define NS_FASTCALL
91 #define NS_CONSTRUCTOR_FASTCALL
92 #endif
94 #ifdef XP_WIN
96 #define NS_IMPORT __declspec(dllimport)
97 #define NS_IMPORT_(type) __declspec(dllimport) type __stdcall
98 #define NS_EXPORT __declspec(dllexport)
99 #define NS_EXPORT_(type) __declspec(dllexport) type __stdcall
100 #define NS_IMETHOD_(type) virtual type __stdcall
101 #define NS_IMETHODIMP_(type) type __stdcall
102 #define NS_METHOD_(type) type __stdcall
103 #define NS_CALLBACK_(_type, _name) _type (__stdcall * _name)
104 #ifndef _WIN64
105 // Win64 has only one calling convention. __stdcall will be ignored by the compiler.
106 #define NS_STDCALL __stdcall
107 #define NS_HAVE_STDCALL
108 #else
109 #define NS_STDCALL
110 #endif
111 #define NS_FROZENCALL __cdecl
114 These are needed to mark static members in exported classes, due to
115 gcc bug XXX insert bug# here.
118 #define NS_EXPORT_STATIC_MEMBER_(type) type
119 #define NS_IMPORT_STATIC_MEMBER_(type) type
121 #else
123 #define NS_IMPORT NS_EXTERNAL_VIS
124 #define NS_IMPORT_(type) NS_EXTERNAL_VIS_(type)
125 #define NS_EXPORT NS_EXTERNAL_VIS
126 #define NS_EXPORT_(type) NS_EXTERNAL_VIS_(type)
127 #define NS_IMETHOD_(type) virtual type
128 #define NS_IMETHODIMP_(type) type
129 #define NS_METHOD_(type) type
130 #define NS_CALLBACK_(_type, _name) _type (* _name)
131 #define NS_STDCALL
132 #define NS_FROZENCALL
133 #define NS_EXPORT_STATIC_MEMBER_(type) NS_EXTERNAL_VIS_(type)
134 #define NS_IMPORT_STATIC_MEMBER_(type) NS_EXTERNAL_VIS_(type)
136 #endif
139 * Macro for creating typedefs for pointer-to-member types which are
140 * declared with stdcall. It is important to use this for any type which is
141 * declared as stdcall (i.e. NS_IMETHOD). For example, instead of writing:
143 * typedef nsresult (nsIFoo::*someType)(nsISupports* arg);
145 * you should write:
147 * typedef
148 * NS_STDCALL_FUNCPROTO(nsresult, someType, nsIFoo, typeFunc, (nsISupports*));
150 * where nsIFoo::typeFunc is any method declared as
151 * NS_IMETHOD typeFunc(nsISupports*);
153 * XXX this can be simplified to always use the non-typeof implementation
154 * when http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11893 is fixed.
157 #ifdef __GNUC__
158 #define NS_STDCALL_FUNCPROTO(ret, name, class, func, args) \
159 typeof(&class::func) name
160 #else
161 #define NS_STDCALL_FUNCPROTO(ret, name, class, func, args) \
162 ret (NS_STDCALL class::*name) args
163 #endif
166 * Deprecated declarations.
168 #ifdef __GNUC__
169 # define MOZ_DEPRECATED __attribute__((deprecated))
170 #elif defined(_MSC_VER)
171 # define MOZ_DEPRECATED __declspec(deprecated)
172 #else
173 # define MOZ_DEPRECATED
174 #endif
177 * Printf style formats
179 #ifdef __GNUC__
180 #define MOZ_FORMAT_PRINTF(stringIndex, firstToCheck) \
181 __attribute__ ((format (printf, stringIndex, firstToCheck)))
182 #else
183 #define MOZ_FORMAT_PRINTF(stringIndex, firstToCheck)
184 #endif
187 * Generic API modifiers which return the standard XPCOM nsresult type
189 #define NS_IMETHOD NS_IMETHOD_(nsresult)
190 #define NS_IMETHODIMP NS_IMETHODIMP_(nsresult)
191 #define NS_METHOD NS_METHOD_(nsresult)
192 #define NS_CALLBACK(_name) NS_CALLBACK_(nsresult, _name)
195 * Import/Export macros for XPCOM APIs
198 #ifdef __cplusplus
199 #define NS_EXTERN_C extern "C"
200 #else
201 #define NS_EXTERN_C
202 #endif
204 #define EXPORT_XPCOM_API(type) NS_EXTERN_C NS_EXPORT type NS_FROZENCALL
205 #define IMPORT_XPCOM_API(type) NS_EXTERN_C NS_IMPORT type NS_FROZENCALL
206 #define GLUE_XPCOM_API(type) NS_EXTERN_C NS_HIDDEN_(type) NS_FROZENCALL
208 #ifdef IMPL_LIBXUL
209 #define XPCOM_API(type) EXPORT_XPCOM_API(type)
210 #elif defined(XPCOM_GLUE)
211 #define XPCOM_API(type) GLUE_XPCOM_API(type)
212 #else
213 #define XPCOM_API(type) IMPORT_XPCOM_API(type)
214 #endif
216 #ifdef MOZILLA_INTERNAL_API
218 The frozen string API has different definitions of nsAC?String
219 classes than the internal API. On systems that explicitly declare
220 dllexport symbols this is not a problem, but on ELF systems
221 internal symbols can accidentally "shine through"; we rename the
222 internal classes to avoid symbol conflicts.
224 # define nsAString nsAString_internal
225 # define nsACString nsACString_internal
226 #endif
228 #if (defined(DEBUG) || defined(FORCE_BUILD_REFCNT_LOGGING))
229 /* Make refcnt logging part of the build. This doesn't mean that
230 * actual logging will occur (that requires a separate enable; see
231 * nsTraceRefcnt and nsISupportsImpl.h for more information). */
232 #define NS_BUILD_REFCNT_LOGGING
233 #endif
235 /* If NO_BUILD_REFCNT_LOGGING is defined then disable refcnt logging
236 * in the build. This overrides FORCE_BUILD_REFCNT_LOGGING. */
237 #if defined(NO_BUILD_REFCNT_LOGGING)
238 #undef NS_BUILD_REFCNT_LOGGING
239 #endif
241 /* If a program allocates memory for the lifetime of the app, it doesn't make
242 * sense to touch memory pages and free that memory at shutdown,
243 * unless we are running leak stats.
245 #if defined(NS_TRACE_MALLOC) || defined(NS_BUILD_REFCNT_LOGGING) || defined(MOZ_VALGRIND)
246 #define NS_FREE_PERMANENT_DATA
247 #endif
250 * NS_NO_VTABLE is emitted by xpidl in interface declarations whenever
251 * xpidl can determine that the interface can't contain a constructor.
252 * This results in some space savings and possible runtime savings -
253 * see bug 49416. We undefine it first, as xpidl-generated headers
254 * define it for IDL uses that don't include this file.
256 #ifdef NS_NO_VTABLE
257 #undef NS_NO_VTABLE
258 #endif
259 #if defined(_MSC_VER) && !defined(__clang__)
260 #define NS_NO_VTABLE __declspec(novtable)
261 #else
262 #define NS_NO_VTABLE
263 #endif
267 * Generic XPCOM result data type
269 #include "nsError.h"
271 typedef MozRefCountType nsrefcnt;
274 * Use these macros to do 64bit safe pointer conversions.
277 #define NS_PTR_TO_INT32(x) ((int32_t)(intptr_t)(x))
278 #define NS_PTR_TO_UINT32(x) ((uint32_t)(intptr_t)(x))
279 #define NS_INT32_TO_PTR(x) ((void*)(intptr_t)(x))
282 * Use NS_STRINGIFY to form a string literal from the value of a macro.
284 #define NS_STRINGIFY_HELPER(x_) #x_
285 #define NS_STRINGIFY(x_) NS_STRINGIFY_HELPER(x_)
288 * If we're being linked as standalone glue, we don't want a dynamic
289 * dependency on NSPR libs, so we skip the debug thread-safety
290 * checks, and we cannot use the THREADSAFE_ISUPPORTS macros.
292 #if defined(XPCOM_GLUE) && !defined(XPCOM_GLUE_USE_NSPR)
293 #define XPCOM_GLUE_AVOID_NSPR
294 #endif
296 #if defined(HAVE_THREAD_TLS_KEYWORD)
297 #define NS_TLS __thread
298 #endif
301 * SEH exception macros.
303 #ifdef HAVE_SEH_EXCEPTIONS
304 #define MOZ_SEH_TRY __try
305 #define MOZ_SEH_EXCEPT(expr) __except(expr)
306 #else
307 #define MOZ_SEH_TRY if(true)
308 #define MOZ_SEH_EXCEPT(expr) else
309 #endif
311 #endif /* nscore_h___ */