Debian Codegraph @

nsprpub/pr/include/prtypes.h

raw
1 /* -*- Mode: C++; tab-width: 4; 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/. */
5
6 /*
7 ** File: prtypes.h
8 ** Description: Definitions of NSPR's basic types
9 **
10 ** Prototypes and macros used to make up for deficiencies that we have found
11 ** in ANSI environments.
12 **
13 ** Since we do not wrap <stdlib.h> and all the other standard headers, authors
14 ** of portable code will not know in general that they need these definitions.
15 ** Instead of requiring these authors to find the dependent uses in their code
16 ** and take the following steps only in those C files, we take steps once here
17 ** for all C files.
18 **/
19
20 #ifndef prtypes_h___
21 #define prtypes_h___
22
23 #ifdef MDCPUCFG
24 #include MDCPUCFG
25 #else
26 #include "prcpucfg.h"
27 #endif
28
29 #include <stddef.h>
30
31 /***********************************************************************
32 ** MACROS: PR_EXTERN
33 ** PR_IMPLEMENT
34 ** DESCRIPTION:
35 ** These are only for externally visible routines and globals. For
36 ** internal routines, just use "extern" for type checking and that
37 ** will not export internal cross-file or forward-declared symbols.
38 ** Define a macro for declaring procedures return types. We use this to
39 ** deal with windoze specific type hackery for DLL definitions. Use
40 ** PR_EXTERN when the prototype for the method is declared. Use
41 ** PR_IMPLEMENT for the implementation of the method.
42 **
43 ** Example:
44 ** in dowhim.h
45 ** PR_EXTERN( void ) DoWhatIMean( void );
46 ** in dowhim.c
47 ** PR_IMPLEMENT( void ) DoWhatIMean( void ) { return; }
48 **
49 **
50 ***********************************************************************/
51 #if defined(WIN32)
52
53 #define PR_EXPORT(__type) extern __declspec(dllexport) __type
54 #define PR_EXPORT_DATA(__type) extern __declspec(dllexport) __type
55 #define PR_IMPORT(__type) __declspec(dllimport) __type
56 #define PR_IMPORT_DATA(__type) __declspec(dllimport) __type
57
58 #define PR_EXTERN(__type) extern __declspec(dllexport) __type
59 #define PR_IMPLEMENT(__type) __declspec(dllexport) __type
60 #define PR_EXTERN_DATA(__type) extern __declspec(dllexport) __type
61 #define PR_IMPLEMENT_DATA(__type) __declspec(dllexport) __type
62
63 #define PR_CALLBACK
64 #define PR_CALLBACK_DECL
65 #define PR_STATIC_CALLBACK(__x) static __x
66
67 #elif defined(__declspec)
68
69 #define PR_EXPORT(__type) extern __declspec(dllexport) __type
70 #define PR_EXPORT_DATA(__type) extern __declspec(dllexport) __type
71 #define PR_IMPORT(__type) extern __declspec(dllimport) __type
72 #define PR_IMPORT_DATA(__type) extern __declspec(dllimport) __type
73
74 #define PR_EXTERN(__type) extern __declspec(dllexport) __type
75 #define PR_IMPLEMENT(__type) __declspec(dllexport) __type
76 #define PR_EXTERN_DATA(__type) extern __declspec(dllexport) __type
77 #define PR_IMPLEMENT_DATA(__type) __declspec(dllexport) __type
78
79 #define PR_CALLBACK
80 #define PR_CALLBACK_DECL
81 #define PR_STATIC_CALLBACK(__x) static __x
82
83 #else /* Unix */
84
85 /* GCC 3.3 and later support the visibility attribute. */
86 #if (__GNUC__ >= 4) || \
87 (__GNUC__ == 3 && __GNUC_MINOR__ >= 3)
88 #define PR_VISIBILITY_DEFAULT __attribute__((visibility("default")))
89 #else
90 #define PR_VISIBILITY_DEFAULT
91 #endif
92
93 #define PR_EXPORT(__type) extern PR_VISIBILITY_DEFAULT __type
94 #define PR_EXPORT_DATA(__type) extern PR_VISIBILITY_DEFAULT __type
95 #define PR_IMPORT(__type) extern PR_VISIBILITY_DEFAULT __type
96 #define PR_IMPORT_DATA(__type) extern PR_VISIBILITY_DEFAULT __type
97
98 #define PR_EXTERN(__type) extern PR_VISIBILITY_DEFAULT __type
99 #define PR_IMPLEMENT(__type) PR_VISIBILITY_DEFAULT __type
100 #define PR_EXTERN_DATA(__type) extern PR_VISIBILITY_DEFAULT __type
101 #define PR_IMPLEMENT_DATA(__type) PR_VISIBILITY_DEFAULT __type
102 #define PR_CALLBACK
103 #define PR_CALLBACK_DECL
104 #define PR_STATIC_CALLBACK(__x) static __x
105
106 #endif
107
108 #if defined(_NSPR_BUILD_)
109 #define NSPR_API(__type) PR_EXPORT(__type)
110 #define NSPR_DATA_API(__type) PR_EXPORT_DATA(__type)
111 #else
112 #define NSPR_API(__type) PR_IMPORT(__type)
113 #define NSPR_DATA_API(__type) PR_IMPORT_DATA(__type)
114 #endif
115
116 /***********************************************************************
117 ** MACROS: PR_BEGIN_MACRO
118 ** PR_END_MACRO
119 ** DESCRIPTION:
120 ** Macro body brackets so that macros with compound statement definitions
121 ** behave syntactically more like functions when called.
122 ***********************************************************************/
123 #define PR_BEGIN_MACRO do {
124 #define PR_END_MACRO } while (0)
125
126 /***********************************************************************
127 ** MACROS: PR_BEGIN_EXTERN_C
128 ** PR_END_EXTERN_C
129 ** DESCRIPTION:
130 ** Macro shorthands for conditional C++ extern block delimiters.
131 ***********************************************************************/
132 #ifdef __cplusplus
133 #define PR_BEGIN_EXTERN_C extern "C" {
134 #define PR_END_EXTERN_C }
135 #else
136 #define PR_BEGIN_EXTERN_C
137 #define PR_END_EXTERN_C
138 #endif
139
140 /***********************************************************************
141 ** MACROS: PR_BIT
142 ** PR_BITMASK
143 ** DESCRIPTION:
144 ** Bit masking macros. XXX n must be <= 31 to be portable
145 ***********************************************************************/
146 #define PR_BIT(n) ((PRUint32)1 << (n))
147 #define PR_BITMASK(n) (PR_BIT(n) - 1)
148
149 /***********************************************************************
150 ** MACROS: PR_ROUNDUP
151 ** PR_MIN
152 ** PR_MAX
153 ** PR_ABS
154 ** DESCRIPTION:
155 ** Commonly used macros for operations on compatible types.
156 ***********************************************************************/
157 #define PR_ROUNDUP(x,y) ((((x)+((y)-1))/(y))*(y))
158 #define PR_MIN(x,y) ((x)<(y)?(x):(y))
159 #define PR_MAX(x,y) ((x)>(y)?(x):(y))
160 #define PR_ABS(x) ((x)<0?-(x):(x))
161
162 /***********************************************************************
163 ** MACROS: PR_ARRAY_SIZE
164 ** DESCRIPTION:
165 ** The number of elements in an array.
166 ***********************************************************************/
167 #define PR_ARRAY_SIZE(a) (sizeof(a)/sizeof((a)[0]))
168
169 PR_BEGIN_EXTERN_C
170
171 /*
172 ** Starting in NSPR 4.9.5, NSPR's exact-width integer types should match
173 ** the exact-width integer types defined in <stdint.h>. This allows sloppy
174 ** code to use PRInt{N} and int{N}_t interchangeably.
175 **
176 ** The 8-bit and 16-bit integer types can only be defined using char and
177 ** short. All platforms define the 32-bit integer types using int. So only
178 ** the 64-bit integer types could be defined differently.
179 **
180 ** NSPR's original strategy was to use the "shortest" 64-bit integer type:
181 ** if long is 64-bit, then prefer it over long long. This strategy is also
182 ** used by Linux/glibc, FreeBSD, and NetBSD.
183 **
184 ** Other platforms use a different strategy: simply define the 64-bit
185 ** integer types using long long. We define the PR_ALTERNATE_INT64_TYPEDEF
186 ** macro on these platforms. Note that PR_ALTERNATE_INT64_TYPEDEF is for
187 ** internal use by NSPR headers only. Do not define or test this macro in
188 ** your code.
189 **
190 ** NOTE: NSPR can't use <stdint.h> because C99 requires C++ code to define
191 ** __STDC_LIMIT_MACROS and __STDC_CONSTANT_MACROS to make all the macros
192 ** defined in <stdint.h> available. This strange requirement is gone in
193 ** C11. When most platforms ignore this C99 requirement, NSPR will be able
194 ** to use <stdint.h>. A patch to do that is in NSPR bug 634793.
195 */
196
197 #if defined(__APPLE__) || defined(__OpenBSD__)
198 #define PR_ALTERNATE_INT64_TYPEDEF
199 #endif
200
201 /************************************************************************
202 ** TYPES: PRUint8
203 ** PRInt8
204 ** DESCRIPTION:
205 ** The int8 types are known to be 8 bits each. There is no type that
206 ** is equivalent to a plain "char".
207 ************************************************************************/
208 #if PR_BYTES_PER_BYTE == 1
209 typedef unsigned char PRUint8;
210 /*
211 ** There are two scenarios that require us to define PRInt8 as type 'char'.
212 ** (1)
213 ** Some cfront-based C++ compilers do not like 'signed char' and
214 ** issue the warning message:
215 ** warning: "signed" not implemented (ignored)
216 ** For these compilers, we have to define PRInt8 as plain 'char'.
217 ** Make sure that plain 'char' is indeed signed under these compilers.
218 ** (2)
219 ** Mozilla C++ code expects the PRInt{N} and int{N}_t types to match (see bug
220 ** 634793). If a platform defines int8_t as 'char', but NSPR defines it as
221 ** 'signed char', it results in a type mismatch.
222 ** On such platforms we define PRInt8 as 'char' to avoid the mismatch.
223 */
224 #if (defined(HPUX) && defined(__cplusplus) /* reason 1*/ \
225 && !defined(__GNUC__) && __cplusplus < 199707L) \
226 || (defined(__sun) && defined(__cplusplus)) /* reason 2 */
227 typedef char PRInt8;
228 #else
229 typedef signed char PRInt8;
230 #endif
231 #else
232 #error No suitable type for PRInt8/PRUint8
233 #endif
234
235 /************************************************************************
236 * MACROS: PR_INT8_MAX
237 * PR_INT8_MIN
238 * PR_UINT8_MAX
239 * DESCRIPTION:
240 * The maximum and minimum values of a PRInt8 or PRUint8.
241 ************************************************************************/
242
243 #define PR_INT8_MAX 127
244 #define PR_INT8_MIN (-128)
245 #define PR_UINT8_MAX 255U
246
247 /************************************************************************
248 ** TYPES: PRUint16
249 ** PRInt16
250 ** DESCRIPTION:
251 ** The int16 types are known to be 16 bits each.
252 ************************************************************************/
253 #if PR_BYTES_PER_SHORT == 2
254 typedef unsigned short PRUint16;
255 typedef short PRInt16;
256 #else
257 #error No suitable type for PRInt16/PRUint16
258 #endif
259
260 /************************************************************************
261 * MACROS: PR_INT16_MAX
262 * PR_INT16_MIN
263 * PR_UINT16_MAX
264 * DESCRIPTION:
265 * The maximum and minimum values of a PRInt16 or PRUint16.
266 ************************************************************************/
267
268 #define PR_INT16_MAX 32767
269 #define PR_INT16_MIN (-32768)
270 #define PR_UINT16_MAX 65535U
271
272 /************************************************************************
273 ** TYPES: PRUint32
274 ** PRInt32
275 ** DESCRIPTION:
276 ** The int32 types are known to be 32 bits each.
277 ************************************************************************/
278 #if PR_BYTES_PER_INT == 4
279 typedef unsigned int PRUint32;
280 typedef int PRInt32;
281 #define PR_INT32(x) x
282 #define PR_UINT32(x) x ## U
283 #elif PR_BYTES_PER_LONG == 4
284 typedef unsigned long PRUint32;
285 typedef long PRInt32;
286 #define PR_INT32(x) x ## L
287 #define PR_UINT32(x) x ## UL
288 #else
289 #error No suitable type for PRInt32/PRUint32
290 #endif
291
292 /************************************************************************
293 * MACROS: PR_INT32_MAX
294 * PR_INT32_MIN
295 * PR_UINT32_MAX
296 * DESCRIPTION:
297 * The maximum and minimum values of a PRInt32 or PRUint32.
298 ************************************************************************/
299
300 #define PR_INT32_MAX PR_INT32(2147483647)
301 #define PR_INT32_MIN (-PR_INT32_MAX - 1)
302 #define PR_UINT32_MAX PR_UINT32(4294967295)
303
304 /************************************************************************
305 ** TYPES: PRUint64
306 ** PRInt64
307 ** DESCRIPTION:
308 ** The int64 types are known to be 64 bits each. Care must be used when
309 ** declaring variables of type PRUint64 or PRInt64. Different hardware
310 ** architectures and even different compilers have varying support for
311 ** 64 bit values. The only guaranteed portability requires the use of
312 ** the LL_ macros (see prlong.h).
313 **
314 ** MACROS: PR_INT64
315 ** PR_UINT64
316 ** DESCRIPTION:
317 ** The PR_INT64 and PR_UINT64 macros provide a portable way for
318 ** specifying 64-bit integer constants. They can only be used if
319 ** PRInt64 and PRUint64 are defined as compiler-supported 64-bit
320 ** integer types (i.e., if HAVE_LONG_LONG is defined, which is true
321 ** for all the supported compilers topday). If PRInt64 and PRUint64
322 ** are defined as structs, the LL_INIT macro defined in prlong.h has
323 ** to be used.
324 **
325 ** MACROS: PR_INT64_MAX
326 ** PR_INT64_MIN
327 ** PR_UINT64_MAX
328 ** DESCRIPTION:
329 ** The maximum and minimum values of a PRInt64 or PRUint64.
330 ************************************************************************/
331 #ifdef HAVE_LONG_LONG
332 /* Keep this in sync with prlong.h. */
333 #if PR_BYTES_PER_LONG == 8 && !defined(PR_ALTERNATE_INT64_TYPEDEF)
334 typedef long PRInt64;
335 typedef unsigned long PRUint64;
336 #define PR_INT64(x) x ## L
337 #define PR_UINT64(x) x ## UL
338 #elif defined(WIN32) && !defined(__GNUC__)
339 typedef __int64 PRInt64;
340 typedef unsigned __int64 PRUint64;
341 #define PR_INT64(x) x ## i64
342 #define PR_UINT64(x) x ## ui64
343 #else
344 typedef long long PRInt64;
345 typedef unsigned long long PRUint64;
346 #define PR_INT64(x) x ## LL
347 #define PR_UINT64(x) x ## ULL
348 #endif /* PR_BYTES_PER_LONG == 8 */
349
350 #define PR_INT64_MAX PR_INT64(0x7fffffffffffffff)
351 #define PR_INT64_MIN (-PR_INT64_MAX - 1)
352 #define PR_UINT64_MAX PR_UINT64(-1)
353 #else /* !HAVE_LONG_LONG */
354 typedef struct {
355 #ifdef IS_LITTLE_ENDIAN
356 PRUint32 lo, hi;
357 #else
358 PRUint32 hi, lo;
359 #endif
360 } PRInt64;
361 typedef PRInt64 PRUint64;
362
363 #define PR_INT64_MAX (PRInt64){0x7fffffff, 0xffffffff}
364 #define PR_INT64_MIN (PRInt64){0xffffffff, 0xffffffff}
365 #define PR_UINT64_MAX (PRUint64){0xffffffff, 0xffffffff}
366
367 #endif /* !HAVE_LONG_LONG */
368
369 /************************************************************************
370 ** TYPES: PRUintn
371 ** PRIntn
372 ** DESCRIPTION:
373 ** The PRIntn types are most appropriate for automatic variables. They are
374 ** guaranteed to be at least 16 bits, though various architectures may
375 ** define them to be wider (e.g., 32 or even 64 bits). These types are
376 ** never valid for fields of a structure.
377 ************************************************************************/
378 #if PR_BYTES_PER_INT >= 2
379 typedef int PRIntn;
380 typedef unsigned int PRUintn;
381 #else
382 #error 'sizeof(int)' not sufficient for platform use
383 #endif
384
385 /************************************************************************
386 ** TYPES: PRFloat64
387 ** DESCRIPTION:
388 ** NSPR's floating point type is always 64 bits.
389 ************************************************************************/
390 typedef double PRFloat64;
391
392 /************************************************************************
393 ** TYPES: PRSize
394 ** DESCRIPTION:
395 ** A type for representing the size of objects.
396 ************************************************************************/
397 typedef size_t PRSize;
398
399
400 /************************************************************************
401 ** TYPES: PROffset32, PROffset64
402 ** DESCRIPTION:
403 ** A type for representing byte offsets from some location.
404 ************************************************************************/
405 typedef PRInt32 PROffset32;
406 typedef PRInt64 PROffset64;
407
408 /************************************************************************
409 ** TYPES: PRPtrDiff
410 ** DESCRIPTION:
411 ** A type for pointer difference. Variables of this type are suitable
412 ** for storing a pointer or pointer subtraction.
413 ************************************************************************/
414 typedef ptrdiff_t PRPtrdiff;
415
416 /************************************************************************
417 ** TYPES: PRUptrdiff
418 ** DESCRIPTION:
419 ** A type for pointer difference. Variables of this type are suitable
420 ** for storing a pointer or pointer sutraction.
421 ************************************************************************/
422 #ifdef _WIN64
423 typedef PRUint64 PRUptrdiff;
424 #else
425 typedef unsigned long PRUptrdiff;
426 #endif
427
428 /************************************************************************
429 ** TYPES: PRBool
430 ** DESCRIPTION:
431 ** Use PRBool for variables and parameter types. Use PR_FALSE and PR_TRUE
432 ** for clarity of target type in assignments and actual arguments. Use
433 ** 'if (bool)', 'while (!bool)', '(bool) ? x : y' etc., to test booleans
434 ** just as you would C int-valued conditions.
435 ************************************************************************/
436 typedef PRIntn PRBool;
437 #define PR_TRUE 1
438 #define PR_FALSE 0
439
440 /************************************************************************
441 ** TYPES: PRPackedBool
442 ** DESCRIPTION:
443 ** Use PRPackedBool within structs where bitfields are not desirable
444 ** but minimum and consistant overhead matters.
445 ************************************************************************/
446 typedef PRUint8 PRPackedBool;
447
448 /*
449 ** Status code used by some routines that have a single point of failure or
450 ** special status return.
451 */
452 typedef enum { PR_FAILURE = -1, PR_SUCCESS = 0 } PRStatus;
453
454 #ifndef __PRUNICHAR__
455 #define __PRUNICHAR__
456 #ifdef WIN32
457 typedef wchar_t PRUnichar;
458 #else
459 typedef PRUint16 PRUnichar;
460 #endif
461 #endif
462
463 /*
464 ** WARNING: The undocumented data types PRWord and PRUword are
465 ** only used in the garbage collection and arena code. Do not
466 ** use PRWord and PRUword in new code.
467 **
468 ** A PRWord is an integer that is the same size as a void*.
469 ** It implements the notion of a "word" in the Java Virtual
470 ** Machine. (See Sec. 3.4 "Words", The Java Virtual Machine
471 ** Specification, Addison-Wesley, September 1996.
472 ** http://java.sun.com/docs/books/vmspec/index.html.)
473 */
474 #ifdef _WIN64
475 typedef PRInt64 PRWord;
476 typedef PRUint64 PRUword;
477 #else
478 typedef long PRWord;
479 typedef unsigned long PRUword;
480 #endif
481
482 /*
483 * PR_PRETEND_NORETURN, specified at the end of a function declaration,
484 * indicates that for the purposes of static analysis, this function does not
485 * return. (The function definition does not need to be annotated.)
486 *
487 * void PR_Assert(const char *s, const char *file, PRIntn ln)
488 * PR_PRETEND_NORETURN;
489 *
490 * Some static analyzers, like scan-build from clang, can use this information
491 * to eliminate false positives. From the upstream documentation of
492 * scan-build:
493 * This attribute is useful for annotating assertion handlers that actually
494 * can return, but for the purpose of using the analyzer we want to pretend
495 * that such functions do not return.
496 */
497 #ifdef __clang_analyzer__
498 #if __has_extension(attribute_analyzer_noreturn)
499 #define PR_PRETEND_NORETURN __attribute__((analyzer_noreturn))
500 #endif
501 #endif
502
503 #ifndef PR_PRETEND_NORETURN
504 #define PR_PRETEND_NORETURN /* no support */
505 #endif
506
507 #if defined(NO_NSPR_10_SUPPORT)
508 #else
509 /********* ???????????????? FIX ME ??????????????????????????? *****/
510 /********************** Some old definitions until pr=>ds transition is done ***/
511 /********************** Also, we are still using NSPR 1.0. GC ******************/
512 /*
513 ** Fundamental NSPR macros, used nearly everywhere.
514 */
515
516 #define PR_PUBLIC_API PR_IMPLEMENT
517
518 /*
519 ** Macro body brackets so that macros with compound statement definitions
520 ** behave syntactically more like functions when called.
521 */
522 #define NSPR_BEGIN_MACRO do {
523 #define NSPR_END_MACRO } while (0)
524
525 /*
526 ** Macro shorthands for conditional C++ extern block delimiters.
527 */
528 #ifdef NSPR_BEGIN_EXTERN_C
529 #undef NSPR_BEGIN_EXTERN_C
530 #endif
531 #ifdef NSPR_END_EXTERN_C
532 #undef NSPR_END_EXTERN_C
533 #endif
534
535 #ifdef __cplusplus
536 #define NSPR_BEGIN_EXTERN_C extern "C" {
537 #define NSPR_END_EXTERN_C }
538 #else
539 #define NSPR_BEGIN_EXTERN_C
540 #define NSPR_END_EXTERN_C
541 #endif
542
543 #include "obsolete/protypes.h"
544
545 /********* ????????????? End Fix me ?????????????????????????????? *****/
546 #endif /* NO_NSPR_10_SUPPORT */
547
548 /*
549 ** Compile-time assert. "condition" must be a constant expression.
550 ** The macro can be used only in places where an "extern" declaration is
551 ** allowed.
552 */
553 #define PR_STATIC_ASSERT(condition) \
554 extern void pr_static_assert(int arg[(condition) ? 1 : -1])
555
556 PR_END_EXTERN_C
557
558 #endif /* prtypes_h___ */
559