]> icculus.org git repositories - icculus/xz.git/blob - src/common/sysdefs.h
Rename MIN() and MAX() to my_min() and my_max().
[icculus/xz.git] / src / common / sysdefs.h
1 ///////////////////////////////////////////////////////////////////////////////
2 //
3 /// \file       sysdefs.h
4 /// \brief      Common includes, definitions, system-specific things etc.
5 ///
6 /// This file is used also by the lzma command line tool, that's why this
7 /// file is separate from common.h.
8 //
9 //  Author:     Lasse Collin
10 //
11 //  This file has been put into the public domain.
12 //  You can do whatever you want with this file.
13 //
14 ///////////////////////////////////////////////////////////////////////////////
15
16 #ifndef LZMA_SYSDEFS_H
17 #define LZMA_SYSDEFS_H
18
19 //////////////
20 // Includes //
21 //////////////
22
23 #ifdef HAVE_CONFIG_H
24 #       include <config.h>
25 #endif
26
27 // size_t and NULL
28 #include <stddef.h>
29
30 #ifdef HAVE_INTTYPES_H
31 #       include <inttypes.h>
32 #endif
33
34 // C99 says that inttypes.h always includes stdint.h, but some systems
35 // don't do that, and require including stdint.h separately.
36 #ifdef HAVE_STDINT_H
37 #       include <stdint.h>
38 #endif
39
40 // Some pre-C99 systems have SIZE_MAX in limits.h instead of stdint.h. The
41 // limits are also used to figure out some macros missing from pre-C99 systems.
42 #ifdef HAVE_LIMITS_H
43 #       include <limits.h>
44 #endif
45
46 // Be more compatible with systems that have non-conforming inttypes.h.
47 // We assume that int is 32-bit and that long is either 32-bit or 64-bit.
48 // Full Autoconf test could be more correct, but this should work well enough.
49 // Note that this duplicates some code from lzma.h, but this is better since
50 // we can work without inttypes.h thanks to Autoconf tests.
51 #ifndef UINT32_C
52 #       if UINT_MAX != 4294967295U
53 #               error UINT32_C is not defined and unsigned int is not 32-bit.
54 #       endif
55 #       define UINT32_C(n) n ## U
56 #endif
57 #ifndef UINT32_MAX
58 #       define UINT32_MAX UINT32_C(4294967295)
59 #endif
60 #ifndef PRIu32
61 #       define PRIu32 "u"
62 #endif
63 #ifndef PRIX32
64 #       define PRIX32 "X"
65 #endif
66
67 #if ULONG_MAX == 4294967295UL
68 #       ifndef UINT64_C
69 #               define UINT64_C(n) n ## ULL
70 #       endif
71 #       ifndef PRIu64
72 #               define PRIu64 "llu"
73 #       endif
74 #       ifndef PRIX64
75 #               define PRIX64 "llX"
76 #       endif
77 #else
78 #       ifndef UINT64_C
79 #               define UINT64_C(n) n ## UL
80 #       endif
81 #       ifndef PRIu64
82 #               define PRIu64 "lu"
83 #       endif
84 #       ifndef PRIX64
85 #               define PRIX64 "lX"
86 #       endif
87 #endif
88 #ifndef UINT64_MAX
89 #       define UINT64_MAX UINT64_C(18446744073709551615)
90 #endif
91
92 // Interix has broken header files, which typedef size_t to unsigned long,
93 // but a few lines later define SIZE_MAX to INT32_MAX.
94 #ifdef __INTERIX
95 #       undef SIZE_MAX
96 #endif
97
98 // The code currently assumes that size_t is either 32-bit or 64-bit.
99 #ifndef SIZE_MAX
100 #       if SIZEOF_SIZE_T == 4
101 #               define SIZE_MAX UINT32_MAX
102 #       elif SIZEOF_SIZE_T == 8
103 #               define SIZE_MAX UINT64_MAX
104 #       else
105 #               error size_t is not 32-bit or 64-bit
106 #       endif
107 #endif
108 #if SIZE_MAX != UINT32_MAX && SIZE_MAX != UINT64_MAX
109 #       error size_t is not 32-bit or 64-bit
110 #endif
111
112 #include <stdlib.h>
113 #include <assert.h>
114
115 // Pre-C99 systems lack stdbool.h. All the code in LZMA Utils must be written
116 // so that it works with fake bool type, for example:
117 //
118 //    bool foo = (flags & 0x100) != 0;
119 //    bool bar = !!(flags & 0x100);
120 //
121 // This works with the real C99 bool but breaks with fake bool:
122 //
123 //    bool baz = (flags & 0x100);
124 //
125 #ifdef HAVE_STDBOOL_H
126 #       include <stdbool.h>
127 #else
128 #       if ! HAVE__BOOL
129 typedef unsigned char _Bool;
130 #       endif
131 #       define bool _Bool
132 #       define false 0
133 #       define true 1
134 #       define __bool_true_false_are_defined 1
135 #endif
136
137 // string.h should be enough but let's include strings.h and memory.h too if
138 // they exists, since that shouldn't do any harm, but may improve portability.
139 #ifdef HAVE_STRING_H
140 #       include <string.h>
141 #endif
142
143 #ifdef HAVE_STRINGS_H
144 #       include <strings.h>
145 #endif
146
147 #ifdef HAVE_MEMORY_H
148 #       include <memory.h>
149 #endif
150
151
152 ////////////
153 // Macros //
154 ////////////
155
156 #undef memzero
157 #define memzero(s, n) memset(s, 0, n)
158
159 // NOTE: Avoid using MIN() and MAX(), because even conditionally defining
160 // those macros can cause some portability trouble, since on some systems
161 // the system headers insist defining their own versions.
162 #define my_min(x, y) ((x) < (y) ? (x) : (y))
163 #define my_max(x, y) ((x) > (y) ? (x) : (y))
164
165 #ifndef ARRAY_SIZE
166 #       define ARRAY_SIZE(array) (sizeof(array) / sizeof((array)[0]))
167 #endif
168
169 #endif