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
***********************************************************************/
80
/* These also work for __MWERKS__ */
81
#define JS_EXTERN_API(__type) extern __declspec(dllexport) __type
82
#define JS_EXPORT_API(__type) __declspec(dllexport) __type
83
#define JS_EXTERN_DATA(__type) extern __declspec(dllexport) __type
84
#define JS_EXPORT_DATA(__type) __declspec(dllexport) __type
86
#define JS_DLL_CALLBACK
87
#define JS_STATIC_DLL_CALLBACK(__x) static __x
89
#elif defined(XP_OS2) && defined(__declspec)
91
#define JS_EXTERN_API(__type) extern __declspec(dllexport) __type
92
#define JS_EXPORT_API(__type) __declspec(dllexport) __type
93
#define JS_EXTERN_DATA(__type) extern __declspec(dllexport) __type
94
#define JS_EXPORT_DATA(__type) __declspec(dllexport) __type
96
#define JS_DLL_CALLBACK
97
#define JS_STATIC_DLL_CALLBACK(__x) static __x
102
#define JS_EXTERN_API(__type) extern __type _cdecl _export _loadds
103
#define JS_EXPORT_API(__type) __type _cdecl _export _loadds
104
#define JS_EXTERN_DATA(__type) extern __type _export
105
#define JS_EXPORT_DATA(__type) __type _export
107
#define JS_DLL_CALLBACK __cdecl __loadds
108
#define JS_STATIC_DLL_CALLBACK(__x) static __x CALLBACK
110
#else /* this must be .EXE */
111
#define JS_EXTERN_API(__type) extern __type _cdecl _export
112
#define JS_EXPORT_API(__type) __type _cdecl _export
113
#define JS_EXTERN_DATA(__type) extern __type _export
114
#define JS_EXPORT_DATA(__type) __type _export
116
#define JS_DLL_CALLBACK __cdecl __loadds
117
#define JS_STATIC_DLL_CALLBACK(__x) __x JS_DLL_CALLBACK
122
#ifdef HAVE_VISIBILITY_ATTRIBUTE
123
#define JS_EXTERNAL_VIS __attribute__((visibility ("default")))
125
#define JS_EXTERNAL_VIS
128
#define JS_EXTERN_API(__type) extern JS_EXTERNAL_VIS __type
129
#define JS_EXPORT_API(__type) JS_EXTERNAL_VIS __type
130
#define JS_EXTERN_DATA(__type) extern JS_EXTERNAL_VIS __type
131
#define JS_EXPORT_DATA(__type) JS_EXTERNAL_VIS __type
133
#define JS_DLL_CALLBACK
134
#define JS_STATIC_DLL_CALLBACK(__x) static __x
139
# if defined(__MWERKS__) || defined(__GNUC__)
140
# define JS_IMPORT_API(__x) __x
142
# define JS_IMPORT_API(__x) __declspec(dllimport) __x
144
#elif defined(XP_OS2) && defined(__declspec)
145
# define JS_IMPORT_API(__x) __declspec(dllimport) __x
147
# define JS_IMPORT_API(__x) JS_EXPORT_API (__x)
150
#if defined(_WIN32) && !defined(__MWERKS__)
151
# define JS_IMPORT_DATA(__x) __declspec(dllimport) __x
152
#elif defined(XP_OS2) && defined(__declspec)
153
# define JS_IMPORT_DATA(__x) __declspec(dllimport) __x
155
# define JS_IMPORT_DATA(__x) JS_EXPORT_DATA (__x)
159
* The linkage of JS API functions differs depending on whether the file is
160
* used within the JS library or not. Any source file within the JS
161
* interpreter should define EXPORT_JS_API whereas any client of the library
165
#define JS_PUBLIC_API(t) JS_EXPORT_API(t)
166
#define JS_PUBLIC_DATA(t) JS_EXPORT_DATA(t)
168
#define JS_PUBLIC_API(t) JS_IMPORT_API(t)
169
#define JS_PUBLIC_DATA(t) JS_IMPORT_DATA(t)
172
#define JS_FRIEND_API(t) JS_PUBLIC_API(t)
173
#define JS_FRIEND_DATA(t) JS_PUBLIC_DATA(t)
176
# define JS_INLINE __inline
177
#elif defined(__GNUC__)
183
/***********************************************************************
184
** MACROS: JS_BEGIN_MACRO
187
** Macro body brackets so that macros with compound statement definitions
188
** behave syntactically more like functions when called.
189
***********************************************************************/
190
#define JS_BEGIN_MACRO do {
191
#define JS_END_MACRO } while (0)
193
/***********************************************************************
194
** MACROS: JS_BEGIN_EXTERN_C
197
** Macro shorthands for conditional C++ extern block delimiters.
198
***********************************************************************/
200
#define JS_BEGIN_EXTERN_C extern "C" {
201
#define JS_END_EXTERN_C }
203
#define JS_BEGIN_EXTERN_C
204
#define JS_END_EXTERN_C
207
/***********************************************************************
211
** Bit masking macros. XXX n must be <= 31 to be portable
212
***********************************************************************/
213
#define JS_BIT(n) ((JSUint32)1 << (n))
214
#define JS_BITMASK(n) (JS_BIT(n) - 1)
216
/***********************************************************************
217
** MACROS: JS_PTR_TO_INT32
222
** Integer to pointer and pointer to integer conversion macros.
223
***********************************************************************/
224
#define JS_PTR_TO_INT32(x) ((jsint)((char *)(x) - (char *)0))
225
#define JS_PTR_TO_UINT32(x) ((jsuint)((char *)(x) - (char *)0))
226
#define JS_INT32_TO_PTR(x) ((void *)((char *)0 + (jsint)(x)))
227
#define JS_UINT32_TO_PTR(x) ((void *)((char *)0 + (jsuint)(x)))
229
/***********************************************************************
230
** MACROS: JS_HOWMANY
235
** Commonly used macros for operations on compatible types.
236
***********************************************************************/
237
#define JS_HOWMANY(x,y) (((x)+(y)-1)/(y))
238
#define JS_ROUNDUP(x,y) (JS_HOWMANY(x,y)*(y))
239
#define JS_MIN(x,y) ((x)<(y)?(x):(y))
240
#define JS_MAX(x,y) ((x)>(y)?(x):(y))
242
#if (defined(XP_WIN) && !defined(CROSS_COMPILE)) || defined (WINCE)
243
# include "jscpucfg.h" /* Use standard Mac or Windows configuration */
244
#elif defined(XP_UNIX) || defined(XP_BEOS) || defined(XP_OS2) || defined(CROSS_COMPILE)
245
# include "jsautocfg.h" /* Use auto-detected configuration */
246
# include "jsosdep.h" /* ...and platform-specific flags */
248
# error "Must define one of XP_BEOS, XP_OS2, XP_WIN or XP_UNIX"
253
/************************************************************************
257
** The int8 types are known to be 8 bits each. There is no type that
258
** is equivalent to a plain "char".
259
************************************************************************/
260
#if JS_BYTES_PER_BYTE == 1
261
typedef unsigned char JSUint8;
262
typedef signed char JSInt8;
264
#error No suitable type for JSInt8/JSUint8
267
/************************************************************************
271
** The int16 types are known to be 16 bits each.
272
************************************************************************/
273
#if JS_BYTES_PER_SHORT == 2
274
typedef unsigned short JSUint16;
275
typedef short JSInt16;
277
#error No suitable type for JSInt16/JSUint16
280
/************************************************************************
284
** The int32 types are known to be 32 bits each.
285
************************************************************************/
286
#if JS_BYTES_PER_INT == 4
287
typedef unsigned int JSUint32;
289
#define JS_INT32(x) x
290
#define JS_UINT32(x) x ## U
291
#elif JS_BYTES_PER_LONG == 4
292
typedef unsigned long JSUint32;
293
typedef long JSInt32;
294
#define JS_INT32(x) x ## L
295
#define JS_UINT32(x) x ## UL
297
#error No suitable type for JSInt32/JSUint32
300
/************************************************************************
304
** The int64 types are known to be 64 bits each. Care must be used when
305
** declaring variables of type JSUint64 or JSInt64. Different hardware
306
** architectures and even different compilers have varying support for
307
** 64 bit values. The only guaranteed portability requires the use of
308
** the JSLL_ macros (see jslong.h).
309
************************************************************************/
310
#ifdef JS_HAVE_LONG_LONG
311
#if JS_BYTES_PER_LONG == 8
312
typedef long JSInt64;
313
typedef unsigned long JSUint64;
315
typedef __int64 JSInt64;
316
typedef unsigned __int64 JSUint64;
317
#elif defined(WIN32) && !defined(__GNUC__)
318
typedef __int64 JSInt64;
319
typedef unsigned __int64 JSUint64;
321
typedef long long JSInt64;
322
typedef unsigned long long JSUint64;
323
#endif /* JS_BYTES_PER_LONG == 8 */
324
#else /* !JS_HAVE_LONG_LONG */
326
#ifdef IS_LITTLE_ENDIAN
332
typedef JSInt64 JSUint64;
333
#endif /* !JS_HAVE_LONG_LONG */
335
/************************************************************************
339
** The JSIntn types are most appropriate for automatic variables. They are
340
** guaranteed to be at least 16 bits, though various architectures may
341
** define them to be wider (e.g., 32 or even 64 bits). These types are
342
** never valid for fields of a structure.
343
************************************************************************/
344
#if JS_BYTES_PER_INT >= 2
346
typedef unsigned int JSUintn;
348
#error 'sizeof(int)' not sufficient for platform use
351
/************************************************************************
354
** NSPR's floating point type is always 64 bits.
355
************************************************************************/
356
typedef double JSFloat64;
358
/************************************************************************
361
** A type for representing the size of objects.
362
************************************************************************/
363
typedef size_t JSSize;
365
/************************************************************************
368
** A type for pointer difference. Variables of this type are suitable
369
** for storing a pointer or pointer sutraction.
370
************************************************************************/
371
typedef ptrdiff_t JSPtrdiff;
373
/************************************************************************
376
** A type for pointer difference. Variables of this type are suitable
377
** for storing a pointer or pointer sutraction.
378
************************************************************************/
379
#if JS_BYTES_PER_WORD == 8 && JS_BYTES_PER_LONG != 8
380
typedef JSUint64 JSUptrdiff;
382
typedef unsigned long JSUptrdiff;
385
/************************************************************************
388
** Use JSBool for variables and parameter types. Use JS_FALSE and JS_TRUE
389
** for clarity of target type in assignments and actual arguments. Use
390
** 'if (bool)', 'while (!bool)', '(bool) ? x : y' etc., to test booleans
391
** just as you would C int-valued conditions.
392
************************************************************************/
393
typedef JSIntn JSBool;
394
#define JS_TRUE (JSIntn)1
395
#define JS_FALSE (JSIntn)0
397
/************************************************************************
398
** TYPES: JSPackedBool
400
** Use JSPackedBool within structs where bitfields are not desireable
401
** but minimum and consistent overhead matters.
402
************************************************************************/
403
typedef JSUint8 JSPackedBool;
406
** A JSWord is an integer that is the same size as a void*
408
#if JS_BYTES_PER_WORD == 8 && JS_BYTES_PER_LONG != 8
409
typedef JSInt64 JSWord;
410
typedef JSUint64 JSUword;
413
typedef unsigned long JSUword;
416
#include "jsotypes.h"
418
/***********************************************************************
422
** These macros allow you to give a hint to the compiler about branch
423
** probability so that it can better optimize. Use them like this:
425
** if (JS_LIKELY(v == 1)) {
426
** ... expected code path ...
429
** if (JS_UNLIKELY(v == 0)) {
430
** ... non-expected code path ...
433
***********************************************************************/
434
#if defined(__GNUC__) && (__GNUC__ > 2)
435
#define JS_LIKELY(x) (__builtin_expect((x), 1))
436
#define JS_UNLIKELY(x) (__builtin_expect((x), 0))
438
#define JS_LIKELY(x) (x)
439
#define JS_UNLIKELY(x) (x)
442
/***********************************************************************
443
** MACROS: JS_ARRAY_LENGTH
446
** Macros to get the number of elements and the pointer to one past the
447
** last element of a C array. Use them like this:
449
** jschar buf[10], *s;
452
** for (s = buf; s != JS_ARRAY_END(buf); ++s) *s = ...;
454
** str = JS_NewStringCopyN(cx, buf, JS_ARRAY_LENGTH(buf));
457
***********************************************************************/
459
#define JS_ARRAY_LENGTH(array) (sizeof (array) / sizeof (array)[0])
460
#define JS_ARRAY_END(array) ((array) + JS_ARRAY_LENGTH(array))
464
#endif /* jstypes_h___ */