1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
15 * The Original Code is Mozilla Communicator client code, released
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1998
21 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
42 ** Description: Definitions of NSPR's basic types
44 ** Prototypes and macros used to make up for deficiencies in ANSI environments
45 ** that we have found.
47 ** Since we do not wrap <stdlib.h> and all the other standard headers, authors
48 ** of portable code will not know in general that they need these definitions.
49 ** Instead of requiring these authors to find the dependent uses in their code
50 ** and take the following steps only in those C files, we take steps once here
59 /***********************************************************************
60 ** MACROS: JS_EXTERN_API
63 ** These are only for externally visible routines and globals. For
64 ** internal routines, just use "extern" for type checking and that
65 ** will not export internal cross-file or forward-declared symbols.
66 ** Define a macro for declaring procedures return types. We use this to
67 ** deal with windoze specific type hackery for DLL definitions. Use
68 ** JS_EXTERN_API when the prototype for the method is declared. Use
69 ** JS_EXPORT_API for the implementation of the method.
73 ** JS_EXTERN_API( void ) DoWhatIMean( void );
75 ** JS_EXPORT_API( void ) DoWhatIMean( void ) { return; }
78 ***********************************************************************/
81 /* These also work for __MWERKS__ */
82 # define JS_EXTERN_API(__type) extern __declspec(dllexport) __type
83 # define JS_EXPORT_API(__type) __declspec(dllexport) __type
84 # define JS_EXTERN_DATA(__type) extern __declspec(dllexport) __type
85 # define JS_EXPORT_DATA(__type) __declspec(dllexport) __type
87 #elif defined(XP_OS2) && defined(__declspec)
89 # define JS_EXTERN_API(__type) extern __declspec(dllexport) __type
90 # define JS_EXPORT_API(__type) __declspec(dllexport) __type
91 # define JS_EXTERN_DATA(__type) extern __declspec(dllexport) __type
92 # define JS_EXPORT_DATA(__type) __declspec(dllexport) __type
96 # ifdef HAVE_VISIBILITY_ATTRIBUTE
97 # define JS_EXTERNAL_VIS __attribute__((visibility ("default")))
98 # elif defined(__SUNPRO_C) || defined(__SUNPRO_CC)
99 # define JS_EXTERNAL_VIS __global
101 # define JS_EXTERNAL_VIS
104 # define JS_EXTERN_API(__type) extern JS_EXTERNAL_VIS __type
105 # define JS_EXPORT_API(__type) JS_EXTERNAL_VIS __type
106 # define JS_EXTERN_DATA(__type) extern JS_EXTERNAL_VIS __type
107 # define JS_EXPORT_DATA(__type) JS_EXTERNAL_VIS __type
112 # if defined(__MWERKS__) || defined(__GNUC__)
113 # define JS_IMPORT_API(__x) __x
115 # define JS_IMPORT_API(__x) __declspec(dllimport) __x
117 #elif defined(XP_OS2) && defined(__declspec)
118 # define JS_IMPORT_API(__x) __declspec(dllimport) __x
120 # define JS_IMPORT_API(__x) JS_EXPORT_API (__x)
123 #if defined(_WIN32) && !defined(__MWERKS__)
124 # define JS_IMPORT_DATA(__x) __declspec(dllimport) __x
125 #elif defined(XP_OS2) && defined(__declspec)
126 # define JS_IMPORT_DATA(__x) __declspec(dllimport) __x
128 # define JS_IMPORT_DATA(__x) JS_EXPORT_DATA (__x)
132 * The linkage of JS API functions differs depending on whether the file is
133 * used within the JS library or not. Any source file within the JS
134 * interpreter should define EXPORT_JS_API whereas any client of the library
135 * should not. STATIC_JS_API is used to build JS as a static library.
137 #if defined(STATIC_JS_API)
139 # define JS_PUBLIC_API(t) t
140 # define JS_PUBLIC_DATA(t) t
142 #elif defined(EXPORT_JS_API)
144 # define JS_PUBLIC_API(t) JS_EXPORT_API(t)
145 # define JS_PUBLIC_DATA(t) JS_EXPORT_DATA(t)
149 # define JS_PUBLIC_API(t) JS_IMPORT_API(t)
150 # define JS_PUBLIC_DATA(t) JS_IMPORT_DATA(t)
154 #define JS_FRIEND_API(t) JS_PUBLIC_API(t)
155 #define JS_FRIEND_DATA(t) JS_PUBLIC_DATA(t)
157 #if defined(_MSC_VER) && defined(_M_IX86)
158 #define JS_FASTCALL __fastcall
159 #elif defined(__GNUC__) && defined(__i386__) && \
160 ((__GNUC__ >= 4) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
161 #define JS_FASTCALL __attribute__((fastcall))
164 #define JS_NO_FASTCALL
168 # if defined __cplusplus
169 # define JS_INLINE inline
170 # elif defined _MSC_VER
171 # define JS_INLINE __inline
172 # elif defined __GNUC__
173 # define JS_INLINE __inline__
175 # define JS_INLINE inline
179 #ifndef JS_ALWAYS_INLINE
181 # define JS_ALWAYS_INLINE JS_INLINE
182 # elif defined _MSC_VER
183 # define JS_ALWAYS_INLINE __forceinline
184 # elif defined __GNUC__
185 # define JS_ALWAYS_INLINE __attribute__((always_inline))
187 # define JS_ALWAYS_INLINE JS_INLINE
191 #ifdef NS_STATIC_CHECKING
193 * Attributes for static analysis. Functions declared with JS_REQUIRES_STACK
194 * always have a valid cx->fp and can access it freely. Other functions can
195 * access cx->fp only after calling a function that "forces" the stack
196 * (i.e. lazily instantiates it as needed).
198 # define JS_REQUIRES_STACK __attribute__((user("JS_REQUIRES_STACK")))
199 # define JS_FORCES_STACK __attribute__((user("JS_FORCES_STACK")))
201 # define JS_REQUIRES_STACK
202 # define JS_FORCES_STACK
205 /***********************************************************************
206 ** MACROS: JS_BEGIN_MACRO
209 ** Macro body brackets so that macros with compound statement definitions
210 ** behave syntactically more like functions when called.
211 ***********************************************************************/
212 #define JS_BEGIN_MACRO do {
214 #if defined(_MSC_VER) && _MSC_VER >= 1400
215 # define JS_END_MACRO \
216 } __pragma(warning(push)) __pragma(warning(disable:4127)) \
217 while (0) __pragma(warning(pop))
219 # define JS_END_MACRO } while (0)
222 /***********************************************************************
223 ** MACROS: JS_BEGIN_EXTERN_C
226 ** Macro shorthands for conditional C++ extern block delimiters.
227 ***********************************************************************/
230 # define JS_BEGIN_EXTERN_C extern "C" {
231 # define JS_END_EXTERN_C }
235 # define JS_BEGIN_EXTERN_C
236 # define JS_END_EXTERN_C
240 /***********************************************************************
244 ** Bit masking macros. XXX n must be <= 31 to be portable
245 ***********************************************************************/
246 #define JS_BIT(n) ((JSUint32)1 << (n))
247 #define JS_BITMASK(n) (JS_BIT(n) - 1)
249 /***********************************************************************
250 ** MACROS: JS_PTR_TO_INT32
255 ** Integer to pointer and pointer to integer conversion macros.
256 ***********************************************************************/
257 #define JS_PTR_TO_INT32(x) ((jsint)((char *)(x) - (char *)0))
258 #define JS_PTR_TO_UINT32(x) ((jsuint)((char *)(x) - (char *)0))
259 #define JS_INT32_TO_PTR(x) ((void *)((char *)0 + (jsint)(x)))
260 #define JS_UINT32_TO_PTR(x) ((void *)((char *)0 + (jsuint)(x)))
262 /***********************************************************************
263 ** MACROS: JS_HOWMANY
268 ** Commonly used macros for operations on compatible types.
269 ***********************************************************************/
270 #define JS_HOWMANY(x,y) (((x)+(y)-1)/(y))
271 #define JS_ROUNDUP(x,y) (JS_HOWMANY(x,y)*(y))
272 #define JS_MIN(x,y) ((x)<(y)?(x):(y))
273 #define JS_MAX(x,y) ((x)>(y)?(x):(y))
275 #if (defined(XP_WIN) && !defined(CROSS_COMPILE)) || defined (WINCE)
276 # include "jscpucfg.h" /* Use standard Mac or Windows configuration */
277 #elif defined(XP_UNIX) || defined(XP_BEOS) || defined(XP_OS2) || defined(CROSS_COMPILE)
278 # include "jsautocfg.h" /* Use auto-detected configuration */
280 # error "Must define one of XP_BEOS, XP_OS2, XP_WIN or XP_UNIX"
285 /************************************************************************
289 ** The int8 types are known to be 8 bits each. There is no type that
290 ** is equivalent to a plain "char".
291 ************************************************************************/
292 #if JS_BYTES_PER_BYTE == 1
293 typedef unsigned char JSUint8
;
294 typedef signed char JSInt8
;
296 # error No suitable type for JSInt8/JSUint8
299 /************************************************************************
303 ** The int16 types are known to be 16 bits each.
304 ************************************************************************/
305 #if JS_BYTES_PER_SHORT == 2
306 typedef unsigned short JSUint16
;
307 typedef short JSInt16
;
309 # error No suitable type for JSInt16/JSUint16
312 /************************************************************************
316 ** The int32 types are known to be 32 bits each.
317 ************************************************************************/
318 #if JS_BYTES_PER_INT == 4
319 typedef unsigned int JSUint32
;
321 # define JS_INT32(x) x
322 # define JS_UINT32(x) x ## U
323 #elif JS_BYTES_PER_LONG == 4
324 typedef unsigned long JSUint32
;
325 typedef long JSInt32
;
326 # define JS_INT32(x) x ## L
327 # define JS_UINT32(x) x ## UL
329 # error No suitable type for JSInt32/JSUint32
332 /************************************************************************
336 ** The int64 types are known to be 64 bits each. Care must be used when
337 ** declaring variables of type JSUint64 or JSInt64. Different hardware
338 ** architectures and even different compilers have varying support for
339 ** 64 bit values. The only guaranteed portability requires the use of
340 ** the JSLL_ macros (see jslong.h).
341 ************************************************************************/
342 #ifdef JS_HAVE_LONG_LONG
344 # if JS_BYTES_PER_LONG == 8
345 typedef long JSInt64
;
346 typedef unsigned long JSUint64
;
347 # elif defined(WIN16)
348 typedef __int64 JSInt64
;
349 typedef unsigned __int64 JSUint64
;
350 # elif defined(WIN32) && !defined(__GNUC__)
351 typedef __int64 JSInt64
;
352 typedef unsigned __int64 JSUint64
;
354 typedef long long JSInt64
;
355 typedef unsigned long long JSUint64
;
356 # endif /* JS_BYTES_PER_LONG == 8 */
358 #else /* !JS_HAVE_LONG_LONG */
361 # ifdef IS_LITTLE_ENDIAN
367 typedef JSInt64 JSUint64
;
369 #endif /* !JS_HAVE_LONG_LONG */
371 /************************************************************************
375 ** The JSIntn types are most appropriate for automatic variables. They are
376 ** guaranteed to be at least 16 bits, though various architectures may
377 ** define them to be wider (e.g., 32 or even 64 bits). These types are
378 ** never valid for fields of a structure.
379 ************************************************************************/
380 #if JS_BYTES_PER_INT >= 2
382 typedef unsigned int JSUintn
;
384 # error 'sizeof(int)' not sufficient for platform use
387 /************************************************************************
390 ** NSPR's floating point type is always 64 bits.
391 ************************************************************************/
392 typedef double JSFloat64
;
394 /************************************************************************
397 ** A type for representing the size of objects.
398 ************************************************************************/
399 typedef size_t JSSize
;
401 /************************************************************************
404 ** A type for pointer difference. Variables of this type are suitable
405 ** for storing a pointer or pointer sutraction.
406 ************************************************************************/
407 typedef ptrdiff_t JSPtrdiff
;
409 /************************************************************************
412 ** A type for pointer difference. Variables of this type are suitable
413 ** for storing a pointer or pointer sutraction.
414 ************************************************************************/
415 #if JS_BYTES_PER_WORD == 8 && JS_BYTES_PER_LONG != 8
416 typedef JSUint64 JSUptrdiff
;
418 typedef unsigned long JSUptrdiff
;
421 /************************************************************************
424 ** Use JSBool for variables and parameter types. Use JS_FALSE and JS_TRUE
425 ** for clarity of target type in assignments and actual arguments. Use
426 ** 'if (bool)', 'while (!bool)', '(bool) ? x : y' etc., to test booleans
427 ** just as you would C int-valued conditions.
428 ************************************************************************/
429 typedef JSIntn JSBool
;
430 #define JS_TRUE (JSIntn)1
431 #define JS_FALSE (JSIntn)0
433 /************************************************************************
434 ** TYPES: JSPackedBool
436 ** Use JSPackedBool within structs where bitfields are not desireable
437 ** but minimum and consistent overhead matters.
438 ************************************************************************/
439 typedef JSUint8 JSPackedBool
;
442 ** A JSWord is an integer that is the same size as a void*
444 #if JS_BYTES_PER_WORD == 8 && JS_BYTES_PER_LONG != 8
445 typedef JSInt64 JSWord
;
446 typedef JSUint64 JSUword
;
449 typedef unsigned long JSUword
;
452 #include "jsotypes.h"
454 /***********************************************************************
458 ** These macros allow you to give a hint to the compiler about branch
459 ** probability so that it can better optimize. Use them like this:
461 ** if (JS_LIKELY(v == 1)) {
462 ** ... expected code path ...
465 ** if (JS_UNLIKELY(v == 0)) {
466 ** ... non-expected code path ...
469 ***********************************************************************/
470 #if defined(__GNUC__) && (__GNUC__ > 2)
472 # define JS_LIKELY(x) (__builtin_expect((x), 1))
473 # define JS_UNLIKELY(x) (__builtin_expect((x), 0))
477 # define JS_LIKELY(x) (x)
478 # define JS_UNLIKELY(x) (x)
482 /***********************************************************************
483 ** MACROS: JS_ARRAY_LENGTH
486 ** Macros to get the number of elements and the pointer to one past the
487 ** last element of a C array. Use them like this:
489 ** jschar buf[10], *s;
492 ** for (s = buf; s != JS_ARRAY_END(buf); ++s) *s = ...;
494 ** str = JS_NewStringCopyN(cx, buf, JS_ARRAY_LENGTH(buf));
497 ***********************************************************************/
499 #define JS_ARRAY_LENGTH(array) (sizeof (array) / sizeof (array)[0])
500 #define JS_ARRAY_END(array) ((array) + JS_ARRAY_LENGTH(array))
504 #endif /* jstypes_h___ */