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_OS2) && 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
169PR_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
209typedef 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(SCO) && defined(__cplusplus) /* reason 1 */ \
227 && !defined(__GNUC__) && __cplusplus == 1L) \
228 || (defined(__sun) && defined(__cplusplus)) /* reason 2 */
229typedef char PRInt8;
230#else
231typedef signed char PRInt8;
232#endif
233#else
234#error No suitable type for PRInt8/PRUint8
235#endif
236
237/************************************************************************
238 * MACROS: PR_INT8_MAX
239 * PR_INT8_MIN
240 * PR_UINT8_MAX
241 * DESCRIPTION:
242 * The maximum and minimum values of a PRInt8 or PRUint8.
243************************************************************************/
244
245#define PR_INT8_MAX 127
246#define PR_INT8_MIN (-128)
247#define PR_UINT8_MAX 255U
248
249/************************************************************************
250** TYPES: PRUint16
251** PRInt16
252** DESCRIPTION:
253** The int16 types are known to be 16 bits each.
254************************************************************************/
255#if PR_BYTES_PER_SHORT == 2
256typedef unsigned short PRUint16;
257typedef short PRInt16;
258#else
259#error No suitable type for PRInt16/PRUint16
260#endif
261
262/************************************************************************
263 * MACROS: PR_INT16_MAX
264 * PR_INT16_MIN
265 * PR_UINT16_MAX
266 * DESCRIPTION:
267 * The maximum and minimum values of a PRInt16 or PRUint16.
268************************************************************************/
269
270#define PR_INT16_MAX 32767
271#define PR_INT16_MIN (-32768)
272#define PR_UINT16_MAX 65535U
273
274/************************************************************************
275** TYPES: PRUint32
276** PRInt32
277** DESCRIPTION:
278** The int32 types are known to be 32 bits each.
279************************************************************************/
280#if PR_BYTES_PER_INT == 4
281typedef unsigned int PRUint32;
282typedef int PRInt32;
283#define PR_INT32(x) x
284#define PR_UINT32(x) x ## U
285#elif PR_BYTES_PER_LONG == 4
286typedef unsigned long PRUint32;
287typedef long PRInt32;
288#define PR_INT32(x) x ## L
289#define PR_UINT32(x) x ## UL
290#else
291#error No suitable type for PRInt32/PRUint32
292#endif
293
294/************************************************************************
295 * MACROS: PR_INT32_MAX
296 * PR_INT32_MIN
297 * PR_UINT32_MAX
298 * DESCRIPTION:
299 * The maximum and minimum values of a PRInt32 or PRUint32.
300************************************************************************/
301
302#define PR_INT32_MAX PR_INT32(2147483647)
303#define PR_INT32_MIN (-PR_INT32_MAX - 1)
304#define PR_UINT32_MAX PR_UINT32(4294967295)
305
306/************************************************************************
307** TYPES: PRUint64
308** PRInt64
309** DESCRIPTION:
310** The int64 types are known to be 64 bits each. Care must be used when
311** declaring variables of type PRUint64 or PRInt64. Different hardware
312** architectures and even different compilers have varying support for
313** 64 bit values. The only guaranteed portability requires the use of
314** the LL_ macros (see prlong.h).
315**
316** MACROS: PR_INT64
317** PR_UINT64
318** DESCRIPTION:
319** The PR_INT64 and PR_UINT64 macros provide a portable way for
320** specifying 64-bit integer constants. They can only be used if
321** PRInt64 and PRUint64 are defined as compiler-supported 64-bit
322** integer types (i.e., if HAVE_LONG_LONG is defined, which is true
323** for all the supported compilers topday). If PRInt64 and PRUint64
324** are defined as structs, the LL_INIT macro defined in prlong.h has
325** to be used.
326**
327** MACROS: PR_INT64_MAX
328** PR_INT64_MIN
329** PR_UINT64_MAX
330** DESCRIPTION:
331** The maximum and minimum values of a PRInt64 or PRUint64.
332************************************************************************/
333#ifdef HAVE_LONG_LONG
334/* Keep this in sync with prlong.h. */
335#if PR_BYTES_PER_LONG == 8 && !defined(PR_ALTERNATE_INT64_TYPEDEF)
336typedef long PRInt64;
337typedef unsigned long PRUint64;
338#define PR_INT64(x) x ## L
339#define PR_UINT64(x) x ## UL
340#elif defined(WIN32) && !defined(__GNUC__)
341typedef __int64 PRInt64;
342typedef unsigned __int64 PRUint64;
343#define PR_INT64(x) x ## i64
344#define PR_UINT64(x) x ## ui64
345#else
346typedef long long PRInt64;
347typedef unsigned long long PRUint64;
348#define PR_INT64(x) x ## LL
349#define PR_UINT64(x) x ## ULL
350#endif /* PR_BYTES_PER_LONG == 8 */
351
352#define PR_INT64_MAX PR_INT64(0x7fffffffffffffff)
353#define PR_INT64_MIN (-PR_INT64_MAX - 1)
354#define PR_UINT64_MAX PR_UINT64(-1)
355#else /* !HAVE_LONG_LONG */
356typedef struct {
357#ifdef IS_LITTLE_ENDIAN
358 PRUint32 lo, hi;
359#else
360 PRUint32 hi, lo;
361#endif
362} PRInt64;
363typedef PRInt64 PRUint64;
364
365#define PR_INT64_MAX (PRInt64){0x7fffffff, 0xffffffff}
366#define PR_INT64_MIN (PRInt64){0xffffffff, 0xffffffff}
367#define PR_UINT64_MAX (PRUint64){0xffffffff, 0xffffffff}
368
369#endif /* !HAVE_LONG_LONG */
370
371/************************************************************************
372** TYPES: PRUintn
373** PRIntn
374** DESCRIPTION:
375** The PRIntn 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 PR_BYTES_PER_INT >= 2
381typedef int PRIntn;
382typedef unsigned int PRUintn;
383#else
384#error 'sizeof(int)' not sufficient for platform use
385#endif
386
387/************************************************************************
388** TYPES: PRFloat64
389** DESCRIPTION:
390** NSPR's floating point type is always 64 bits.
391************************************************************************/
392typedef double PRFloat64;
393
394/************************************************************************
395** TYPES: PRSize
396** DESCRIPTION:
397** A type for representing the size of objects.
398************************************************************************/
399typedef size_t PRSize;
400
401
402/************************************************************************
403** TYPES: PROffset32, PROffset64
404** DESCRIPTION:
405** A type for representing byte offsets from some location.
406************************************************************************/
407typedef PRInt32 PROffset32;
408typedef PRInt64 PROffset64;
409
410/************************************************************************
411** TYPES: PRPtrDiff
412** DESCRIPTION:
413** A type for pointer difference. Variables of this type are suitable
414** for storing a pointer or pointer subtraction.
415************************************************************************/
416typedef ptrdiff_t PRPtrdiff;
417
418/************************************************************************
419** TYPES: PRUptrdiff
420** DESCRIPTION:
421** A type for pointer difference. Variables of this type are suitable
422** for storing a pointer or pointer sutraction.
423************************************************************************/
424#ifdef _WIN64
425typedef PRUint64 PRUptrdiff;
426#else
427typedef unsigned long PRUptrdiff;
428#endif
429
430/************************************************************************
431** TYPES: PRBool
432** DESCRIPTION:
433** Use PRBool for variables and parameter types. Use PR_FALSE and PR_TRUE
434** for clarity of target type in assignments and actual arguments. Use
435** 'if (bool)', 'while (!bool)', '(bool) ? x : y' etc., to test booleans
436** just as you would C int-valued conditions.
437************************************************************************/
438typedef PRIntn PRBool;
439#define PR_TRUE 1
440#define PR_FALSE 0
441
442/************************************************************************
443** TYPES: PRPackedBool
444** DESCRIPTION:
445** Use PRPackedBool within structs where bitfields are not desirable
446** but minimum and consistant overhead matters.
447************************************************************************/
448typedef PRUint8 PRPackedBool;
449
450/*
451** Status code used by some routines that have a single point of failure or
452** special status return.
453*/
454typedef enum { PR_FAILURE = -1, PR_SUCCESS = 0 } PRStatus;
455
456#ifndef __PRUNICHAR__
457#define __PRUNICHAR__
458#ifdef WIN32
459typedef wchar_t PRUnichar;
460#else
461typedef PRUint16 PRUnichar;
462#endif
463#endif
464
465/*
466** WARNING: The undocumented data types PRWord and PRUword are
467** only used in the garbage collection and arena code. Do not
468** use PRWord and PRUword in new code.
469**
470** A PRWord is an integer that is the same size as a void*.
471** It implements the notion of a "word" in the Java Virtual
472** Machine. (See Sec. 3.4 "Words", The Java Virtual Machine
473** Specification, Addison-Wesley, September 1996.
474** http://java.sun.com/docs/books/vmspec/index.html.)
475*/
476#ifdef _WIN64
477typedef PRInt64 PRWord;
478typedef PRUint64 PRUword;
479#else
480typedef long PRWord;
481typedef unsigned long PRUword;
482#endif
483
484/*
485 * PR_PRETEND_NORETURN, specified at the end of a function declaration,
486 * indicates that for the purposes of static analysis, this function does not
487 * return. (The function definition does not need to be annotated.)
488 *
489 * void PR_Assert(const char *s, const char *file, PRIntn ln)
490 * PR_PRETEND_NORETURN;
491 *
492 * Some static analyzers, like scan-build from clang, can use this information
493 * to eliminate false positives. From the upstream documentation of
494 * scan-build:
495 * This attribute is useful for annotating assertion handlers that actually
496 * can return, but for the purpose of using the analyzer we want to pretend
497 * that such functions do not return.
498 */
499#ifdef __clang_analyzer__
500#if __has_extension(attribute_analyzer_noreturn)
501#define PR_PRETEND_NORETURN __attribute__((analyzer_noreturn))
502#endif
503#endif
504
505#ifndef PR_PRETEND_NORETURN
506#define PR_PRETEND_NORETURN /* no support */
507#endif
508
509#if defined(NO_NSPR_10_SUPPORT)
510#else
511/********* ???????????????? FIX ME ??????????????????????????? *****/
512/********************** Some old definitions until pr=>ds transition is done ***/
513/********************** Also, we are still using NSPR 1.0. GC ******************/
514/*
515** Fundamental NSPR macros, used nearly everywhere.
516*/
517
518#define PR_PUBLIC_API PR_IMPLEMENT
519
520/*
521** Macro body brackets so that macros with compound statement definitions
522** behave syntactically more like functions when called.
523*/
524#define NSPR_BEGIN_MACRO do {
525#define NSPR_END_MACRO } while (0)
526
527/*
528** Macro shorthands for conditional C++ extern block delimiters.
529*/
530#ifdef NSPR_BEGIN_EXTERN_C
531#undef NSPR_BEGIN_EXTERN_C
532#endif
533#ifdef NSPR_END_EXTERN_C
534#undef NSPR_END_EXTERN_C
535#endif
536
537#ifdef __cplusplus
538#define NSPR_BEGIN_EXTERN_C extern "C" {
539#define NSPR_END_EXTERN_C }
540#else
541#define NSPR_BEGIN_EXTERN_C
542#define NSPR_END_EXTERN_C
543#endif
544
545#include "obsolete/protypes.h"
546
547/********* ????????????? End Fix me ?????????????????????????????? *****/
548#endif /* NO_NSPR_10_SUPPORT */
549
550/*
551** Compile-time assert. "condition" must be a constant expression.
552** The macro can be used only in places where an "extern" declaration is
553** allowed.
554*/
555#define PR_STATIC_ASSERT(condition) \
556 extern void pr_static_assert(int arg[(condition) ? 1 : -1])
557
558PR_END_EXTERN_C
559
560#endif /* prtypes_h___ */
561
562

source code of include/nspr/prtypes.h