VirtualBox

source: vbox/trunk/src/libs/xpcom18a4/xpcom/base/nscore.h@ 34050

Last change on this file since 34050 was 33535, checked in by vboxsync, 14 years ago

fortify does not like builtin_expect()

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 14.7 KB
Line 
1/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2/* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 *
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
9 *
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
14 *
15 * The Original Code is mozilla.org code.
16 *
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 *
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
35 *
36 * ***** END LICENSE BLOCK ***** */
37#ifndef nscore_h___
38#define nscore_h___
39
40/**
41 * Make sure that we have the proper platform specific
42 * c++ definitions needed by nscore.h
43 */
44#ifndef _XPCOM_CONFIG_H_
45#include "xpcom-config.h"
46#endif
47
48/**
49 * Incorporate the core NSPR data types which XPCOM uses.
50 */
51#include "prtypes.h"
52
53/* Core XPCOM declarations. */
54
55/**
56 * Macros defining the target platform...
57 */
58#ifdef _WIN32
59#define NS_WIN32 1
60
61#elif defined(__unix)
62#define NS_UNIX 1
63
64#elif defined(XP_OS2)
65#define NS_OS2 1
66#endif
67/*----------------------------------------------------------------------*/
68/* Import/export defines */
69
70/**
71 * Using the visibility("hidden") attribute allows the compiler to use
72 * PC-relative addressing to call this function. If a function does not
73 * access any global data, and does not call any methods which are not either
74 * file-local or hidden, then on ELF systems we avoid loading the address of
75 * the PLT into a register at the start of the function, which reduces code
76 * size and frees up a register for general use.
77 *
78 * As a general rule, this should be used for any non-exported symbol
79 * (including virtual method implementations). NS_IMETHOD uses this by
80 * default; if you need to have your NS_IMETHOD functions exported, you can
81 * wrap your class as follows:
82 *
83 * #undef IMETHOD_VISIBILITY
84 * #define IMETHOD_VISIBILITY NS_VISIBILITY_DEFAULT
85 *
86 * class Foo {
87 * ...
88 * };
89 *
90 * #undef IMETHOD_VISIBILITY
91 * #define IMETHOD_VISIBILITY NS_VISIBILITY_HIDDEN
92 *
93 * Don't forget to change the visibility back to hidden before the end
94 * of a header!
95 *
96 * Other examples:
97 *
98 * NS_HIDDEN_(int) someMethod();
99 * SomeCtor() NS_HIDDEN;
100 */
101
102#ifdef HAVE_VISIBILITY_ATTRIBUTE
103#define NS_VISIBILITY_HIDDEN __attribute__ ((visibility ("hidden")))
104# ifdef VBOX_HAVE_VISIBILITY_HIDDEN
105# define NS_VISIBILITY_DEFAULT __attribute__ ((visibility ("default")))
106# else
107# define NS_VISIBILITY_DEFAULT
108# endif
109
110#define NS_HIDDEN_(type) NS_VISIBILITY_HIDDEN type
111#else
112#define NS_VISIBILITY_HIDDEN
113#define NS_VISIBILITY_DEFAULT
114
115#define NS_HIDDEN_(type) type
116#endif
117
118#define NS_HIDDEN NS_VISIBILITY_HIDDEN
119
120#undef IMETHOD_VISIBILITY
121#define IMETHOD_VISIBILITY NS_VISIBILITY_HIDDEN
122
123/**
124 * Mark a function as using a potentially non-standard function calling
125 * convention. This can be used on functions that are called very
126 * frequently, to reduce the overhead of the function call. It is still worth
127 * using the macro for C++ functions which take no parameters since it allows
128 * passing |this| in a register.
129 *
130 * - Do not use this on any scriptable interface method since xptcall won't be
131 * aware of the different calling convention.
132 * - This must appear on the declaration, not the definition.
133 * - Adding this to a public function _will_ break binary compatibility.
134 * - This may be used on virtual functions but you must ensure it is applied
135 * to all implementations - the compiler will _not_ warn but it will crash.
136 * - This has no effect for inline functions or functions which take a
137 * variable number of arguments.
138 *
139 * Examples: int NS_FASTCALL func1(char *foo);
140 * NS_HIDDEN_(int) NS_FASTCALL func2(char *foo);
141 */
142
143#if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 3) && !defined(XP_OS2)
144#define NS_FASTCALL __attribute__ ((regparm (3), stdcall))
145#else
146#define NS_FASTCALL
147#endif
148
149/*
150 * NS_DEFCALL undoes the effect of a global regparm/stdcall setting
151 * so that xptcall works correctly.
152 */
153#if defined(__i386__) && defined(__GNUC__) && (__GNUC__ >= 3) && !defined(XP_OS2)
154#define NS_DEFCALL __attribute__ ((regparm (0), cdecl))
155#else
156#define NS_DEFCALL
157#endif
158
159/* XXX: nike, maybe fix */
160#define NS_EXPORT_STATIC_MEMBER_(type) type
161#define NS_IMPORT_STATIC_MEMBER_(type) type
162
163#ifdef NS_WIN32
164
165#define NS_IMPORT __declspec(dllimport)
166#define NS_IMPORT_(type) type __declspec(dllimport) __stdcall
167#define NS_EXPORT __declspec(dllexport)
168#define NS_EXPORT_(type) type __declspec(dllexport) __stdcall
169#define NS_IMETHOD_(type) virtual type __stdcall
170#define NS_IMETHODIMP_(type) type __stdcall
171#define NS_METHOD_(type) type __stdcall
172#define NS_CALLBACK_(_type, _name) _type (__stdcall * _name)
173#define NS_STDCALL __stdcall
174
175#elif defined(XP_MAC)
176
177#define NS_IMPORT
178#define NS_IMPORT_(type) type
179#define NS_EXPORT __declspec(export)
180#define NS_EXPORT_(type) __declspec(export) type
181#define NS_IMETHOD_(type) virtual type
182#define NS_IMETHODIMP_(type) type
183#define NS_METHOD_(type) type
184#define NS_CALLBACK_(_type, _name) _type (* _name)
185#define NS_STDCALL
186
187#elif defined(XP_OS2) && defined(__declspec)
188
189#define NS_IMPORT __declspec(dllimport)
190#define NS_IMPORT_(type) type __declspec(dllimport) __stdcall
191#define NS_EXPORT __declspec(dllexport)
192#define NS_EXPORT_(type) type __declspec(dllexport) __stdcall
193#define NS_IMETHOD_(type) virtual IMETHOD_VISIBILITY type NS_DEFCALL
194#define NS_IMETHODIMP_(type) type
195#define NS_METHOD_(type) type
196#define NS_CALLBACK_(_type, _name) _type (* _name)
197#define NS_STDCALL
198
199#else
200
201# ifdef VBOX_HAVE_VISIBILITY_HIDDEN
202# define NS_IMPORT
203# define NS_IMPORT_(type) type
204# define NS_EXPORT __attribute__((visibility("default")))
205# define NS_EXPORT_(type) __attribute__((visibility("default"))) type
206# define NS_IMETHOD_(type) virtual IMETHOD_VISIBILITY type NS_DEFCALL
207# define NS_IMETHODIMP_(type) type
208# define NS_METHOD_(type) type
209# define NS_CALLBACK_(_type, _name) _type (* _name)
210# define NS_STDCALL
211# else
212# define NS_IMPORT
213# define NS_IMPORT_(type) type
214# define NS_EXPORT
215# define NS_EXPORT_(type) type
216# define NS_IMETHOD_(type) virtual IMETHOD_VISIBILITY type NS_DEFCALL
217# define NS_IMETHODIMP_(type) type
218# define NS_METHOD_(type) type
219# define NS_CALLBACK_(_type, _name) _type (* _name)
220# define NS_STDCALL
221# endif
222#endif
223
224/**
225 * Macro for creating typedefs for pointer-to-member types which are
226 * declared with stdcall. It is important to use this for any type which is
227 * declared as stdcall (i.e. NS_IMETHOD). For example, instead of writing:
228 *
229 * typedef nsresult (nsIFoo::*someType)(nsISupports* arg);
230 *
231 * you should write:
232 *
233 * typedef
234 * NS_STDCALL_FUNCPROTO(nsresult, someType, nsIFoo, typeFunc, (nsISupports*));
235 *
236 * where nsIFoo::typeFunc is any method declared as
237 * NS_IMETHOD typeFunc(nsISupports*);
238 *
239 * XXX this can be simplified to always use the non-typeof implementation
240 * when http://gcc.gnu.org/bugzilla/show_bug.cgi?id=11893 is fixed.
241 */
242
243#ifdef __GNUC__
244#define NS_STDCALL_FUNCPROTO(ret, name, class, func, args) \
245 typeof(&class::func) name
246#else
247#define NS_STDCALL_FUNCPROTO(ret, name, class, func, args) \
248 ret (NS_STDCALL class::*name) args
249#endif
250
251/**
252 * Generic API modifiers which return the standard XPCOM nsresult type
253 */
254#define NS_IMETHOD NS_IMETHOD_(nsresult)
255#define NS_IMETHODIMP NS_IMETHODIMP_(nsresult)
256#define NS_METHOD NS_METHOD_(nsresult)
257#define NS_CALLBACK(_name) NS_CALLBACK_(nsresult, _name)
258
259/**
260 * Import/Export macros for XPCOM APIs
261 */
262
263#ifdef _IMPL_NS_COM
264#define NS_COM NS_EXPORT
265#elif _IMPL_NS_COM_OFF
266#define NS_COM
267#elif XPCOM_GLUE
268#define NS_COM
269#else
270#define NS_COM NS_IMPORT
271#endif
272
273
274/**
275 * NS_NO_VTABLE is emitted by xpidl in interface declarations whenever
276 * xpidl can determine that the interface can't contain a constructor.
277 * This results in some space savings and possible runtime savings -
278 * see bug 49416. We undefine it first, as xpidl-generated headers
279 * define it for IDL uses that don't include this file.
280 */
281#ifdef NS_NO_VTABLE
282#undef NS_NO_VTABLE
283#endif
284#if defined(_MSC_VER) && _MSC_VER >= 1100
285#define NS_NO_VTABLE __declspec(novtable)
286#else
287#define NS_NO_VTABLE
288#endif
289
290
291/**
292 * Generic XPCOM result data type
293 */
294typedef PRUint32 nsresult;
295
296/**
297 * The preferred symbol for null.
298 */
299#define nsnull 0
300
301#include "nsError.h"
302
303/* ------------------------------------------------------------------------ */
304/* Casting macros for hiding C++ features from older compilers */
305
306 /*
307 All our compiler support template specialization, but not all support the
308 |template <>| notation. The compiler that don't understand this notation
309 just omit it for specialization.
310
311 Need to add an autoconf test for this.
312 */
313
314 /* under Metrowerks (Mac), we don't have autoconf yet */
315#ifdef __MWERKS__
316 #define HAVE_CPP_PARTIAL_SPECIALIZATION
317 #define HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX
318
319 #define HAVE_CPP_ACCESS_CHANGING_USING
320 #define HAVE_CPP_AMBIGUITY_RESOLVING_USING
321 #define HAVE_CPP_EXPLICIT
322 #define HAVE_CPP_TYPENAME
323 #define HAVE_CPP_BOOL
324 #define HAVE_CPP_NAMESPACE_STD
325 #define HAVE_CPP_UNAMBIGUOUS_STD_NOTEQUAL
326 #define HAVE_CPP_2BYTE_WCHAR_T
327#endif
328
329 /* under VC++ (Windows), we don't have autoconf yet */
330#if defined(_MSC_VER) && (_MSC_VER>=1100)
331 /* VC++ 5.0 and greater implement template specialization, 4.2 is unknown */
332 #define HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX
333
334 #define HAVE_CPP_EXPLICIT
335 #define HAVE_CPP_TYPENAME
336 #define HAVE_CPP_ACCESS_CHANGING_USING
337
338 #if (_MSC_VER==1100)
339 /* VC++5.0 has an internal compiler error (sometimes) without this */
340 #undef HAVE_CPP_ACCESS_CHANGING_USING
341 #endif
342
343 #define HAVE_CPP_NAMESPACE_STD
344 #define HAVE_CPP_UNAMBIGUOUS_STD_NOTEQUAL
345 #define HAVE_CPP_2BYTE_WCHAR_T
346#endif
347
348#ifndef __PRUNICHAR__
349#define __PRUNICHAR__
350 /* For now, don't use wchar_t on Unix because it breaks the Netscape
351 * commercial build. When this is fixed there will be no need for the
352 * |NS_REINTERPRET_CAST| in nsLiteralString.h either.
353 */
354 #if defined(HAVE_CPP_2BYTE_WCHAR_T) && (defined(NS_WIN32) || defined(XP_MAC))
355 typedef wchar_t PRUnichar;
356 #else
357 typedef PRUint16 PRUnichar;
358 #endif
359#endif
360
361 /*
362 If the compiler doesn't support |explicit|, we'll just make it go away, trusting
363 that the builds under compilers that do have it will keep us on the straight and narrow.
364 */
365#ifndef HAVE_CPP_EXPLICIT
366 #define explicit
367#endif
368
369#ifndef HAVE_CPP_TYPENAME
370 #define typename
371#endif
372
373#ifdef HAVE_CPP_MODERN_SPECIALIZE_TEMPLATE_SYNTAX
374 #define NS_SPECIALIZE_TEMPLATE template <>
375#else
376 #define NS_SPECIALIZE_TEMPLATE
377#endif
378
379/* unix and beos now determine this automatically */
380#if ! defined XP_UNIX && ! defined XP_BEOS && !defined(XP_OS2)
381#ifndef HAVE_CPP_NEW_CASTS
382#define HAVE_CPP_NEW_CASTS 1 /* we'll be optimistic. */
383#endif
384#endif
385
386#if defined(HAVE_CPP_NEW_CASTS)
387#define NS_STATIC_CAST(__type, __ptr) static_cast< __type >(__ptr)
388#define NS_CONST_CAST(__type, __ptr) const_cast< __type >(__ptr)
389
390#define NS_REINTERPRET_POINTER_CAST(__type, __ptr) reinterpret_cast< __type >(__ptr)
391#define NS_REINTERPRET_NONPOINTER_CAST(__type, __obj) reinterpret_cast< __type >(__obj)
392#define NS_REINTERPRET_CAST(__type, __expr) reinterpret_cast< __type >(__expr)
393
394#else
395#define NS_STATIC_CAST(__type, __ptr) ((__type)(__ptr))
396#define NS_CONST_CAST(__type, __ptr) ((__type)(__ptr))
397
398#define NS_REINTERPRET_POINTER_CAST(__type, __ptr) ((__type)((void*)(__ptr)))
399#define NS_REINTERPRET_NONPOINTER_CAST(__type, __obj) ((__type)(__obj))
400
401 /* Note: the following is only appropriate for pointers. */
402#define NS_REINTERPRET_CAST(__type, __expr) NS_REINTERPRET_POINTER_CAST(__type, __expr)
403 /*
404 Why cast to a |void*| first? Well, when old-style casting from
405 a pointer to a base to a pointer to a derived class, the cast will be
406 ambiguous if the source pointer type appears multiple times in the
407 destination, e.g.,
408
409 class Base {};
410 class Derived : public Base, public Base {};
411
412 void foo( Base* b )
413 {
414 ((Derived*)b)->some_derived_member ... // Error: Ambiguous, expand from which |Base|?
415 }
416
417 an old-style cast (like |static_cast|) will change the pointer, but
418 here, doesn't know how. The cast to |void*| prevents it from thinking
419 it needs to expand the original pointer.
420
421 The cost is, |NS_REINTERPRET_CAST| is no longer appropriate for non-pointer
422 conversions. Also, mis-applying |NS_REINTERPRET_CAST| to cast |this| to something
423 will still expand the pointer to the outer object in standards complying compilers.
424 */
425
426 /*
427 No sense in making an NS_DYNAMIC_CAST() macro: you can't duplicate
428 the semantics. So if you want to dynamic_cast, then just use it
429 "straight", no macro.
430 */
431#endif
432
433#ifndef VBOX
434/*
435 * Use these macros to do 64bit safe pointer conversions.
436 */
437
438#define NS_PTR_TO_INT32(x) ((char *)(x) - (char *)0)
439#define NS_INT32_TO_PTR(x) ((void *)((char *)0 + (x)))
440#else /* VBOX */
441// This stuff is (contrary to the comment) totally 64bit unsafe, so strip
442// it down to only do one direction, which is used by the hashing code.
443#define NS_PTR_TO_INT32(x) ((PRInt32)((char *)(x) - (char *)0))
444#endif /* VBOX */
445
446/*
447 * These macros allow you to give a hint to the compiler about branch
448 * probability so that it can better optimize. Use them like this:
449 *
450 * if (NS_LIKELY(v == 1)) {
451 * ... expected code path ...
452 * }
453 *
454 * if (NS_UNLIKELY(v == 0)) {
455 * ... non-expected code path ...
456 * }
457 *
458 */
459
460#if defined(__GNUC__) && (__GNUC__ > 2) && !defined(FORTIFY_RUNNING)
461#define NS_LIKELY(x) (__builtin_expect(!!(x), 1))
462#define NS_UNLIKELY(x) (__builtin_expect(!!(x), 0))
463#else
464#define NS_LIKELY(x) (x)
465#define NS_UNLIKELY(x) (x)
466#endif
467
468#endif /* nscore_h___ */
469
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette