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