GDAL
cpl_port.h
Go to the documentation of this file.
1 /******************************************************************************
2  * $Id$
3  *
4  * Project: CPL - Common Portability Library
5  * Author: Frank Warmerdam, warmerdam@pobox.com
6  * Purpose: Include file providing low level portability services for CPL.
7  * This should be the first include file for any CPL based code.
8  *
9  ******************************************************************************
10  * Copyright (c) 1998, 2005, Frank Warmerdam <warmerdam@pobox.com>
11  * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining a
14  * copy of this software and associated documentation files (the "Software"),
15  * to deal in the Software without restriction, including without limitation
16  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17  * and/or sell copies of the Software, and to permit persons to whom the
18  * Software is furnished to do so, subject to the following conditions:
19  *
20  * The above copyright notice and this permission notice shall be included
21  * in all copies or substantial portions of the Software.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29  * DEALINGS IN THE SOFTWARE.
30  ****************************************************************************/
31 
32 #ifndef CPL_BASE_H_INCLUDED
33 #define CPL_BASE_H_INCLUDED
34 
42 /* ==================================================================== */
43 /* We will use WIN32 as a standard windows define. */
44 /* ==================================================================== */
45 #if defined(_WIN32) && !defined(WIN32)
46 # define WIN32
47 #endif
48 
49 #if defined(_WINDOWS) && !defined(WIN32)
50 # define WIN32
51 #endif
52 
53 /* -------------------------------------------------------------------- */
54 /* The following apparently allow you to use strcpy() and other */
55 /* functions judged "unsafe" by microsoft in VS 8 (2005). */
56 /* -------------------------------------------------------------------- */
57 #ifdef _MSC_VER
58 # ifndef _CRT_SECURE_NO_DEPRECATE
59 # define _CRT_SECURE_NO_DEPRECATE
60 # endif
61 # ifndef _CRT_NONSTDC_NO_DEPRECATE
62 # define _CRT_NONSTDC_NO_DEPRECATE
63 # endif
64 #endif
65 
66 #include "cpl_config.h"
67 
68 /* ==================================================================== */
69 /* A few sanity checks, mainly to detect problems that sometimes */
70 /* arise with bad configured cross-compilation. */
71 /* ==================================================================== */
72 
73 #if !defined(SIZEOF_INT) || SIZEOF_INT != 4
74 #error "Unexpected value for SIZEOF_INT"
75 #endif
76 
77 #if !defined(SIZEOF_UNSIGNED_LONG) || (SIZEOF_UNSIGNED_LONG != 4 && SIZEOF_UNSIGNED_LONG != 8)
78 #error "Unexpected value for SIZEOF_UNSIGNED_LONG"
79 #endif
80 
81 #if !defined(SIZEOF_VOIDP) || (SIZEOF_VOIDP != 4 && SIZEOF_VOIDP != 8)
82 #error "Unexpected value for SIZEOF_VOIDP"
83 #endif
84 
85 /* ==================================================================== */
86 /* This will disable most WIN32 stuff in a Cygnus build which */
87 /* defines unix to 1. */
88 /* ==================================================================== */
89 
90 #ifdef unix
91 # undef WIN32
92 #endif
93 
95 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
96 # define _LARGEFILE64_SOURCE 1
97 #endif
98 
99 /* ==================================================================== */
100 /* If iconv() is available use extended recoding module. */
101 /* Stub implementation is always compiled in, because it works */
102 /* faster than iconv() for encodings it supports. */
103 /* ==================================================================== */
104 
105 #if defined(HAVE_ICONV)
106 # define CPL_RECODE_ICONV
107 #endif
108 
109 #define CPL_RECODE_STUB
112 /* ==================================================================== */
113 /* MinGW stuff */
114 /* ==================================================================== */
115 
116 /* We need __MSVCRT_VERSION__ >= 0x0700 to have "_aligned_malloc" */
117 /* Latest versions of mingw32 define it, but with older ones, */
118 /* we need to define it manually */
119 #if defined(__MINGW32__)
120 #ifndef __MSVCRT_VERSION__
121 #define __MSVCRT_VERSION__ 0x0700
122 #endif
123 #endif
124 
125 /* Needed for std=c11 on Solaris to have strcasecmp() */
126 #if defined(GDAL_COMPILATION) && defined(__sun__) && (__STDC_VERSION__ + 0) >= 201112L && (_XOPEN_SOURCE + 0) < 600
127 #ifdef _XOPEN_SOURCE
128 #undef _XOPEN_SOURCE
129 #endif
130 #define _XOPEN_SOURCE 600
131 #endif
132 
133 /* ==================================================================== */
134 /* Standard include files. */
135 /* ==================================================================== */
136 
137 #include <stdio.h>
138 #include <stdlib.h>
139 #include <math.h>
140 #include <stdarg.h>
141 #include <string.h>
142 #include <ctype.h>
143 #include <limits.h>
144 
145 #include <time.h>
146 
147 #include <errno.h>
148 
149 #ifdef HAVE_LOCALE_H
150 # include <locale.h>
151 #endif
152 
153 #ifdef HAVE_DIRECT_H
154 # include <direct.h>
155 #endif
156 
157 #if !defined(WIN32)
158 # include <strings.h>
159 #endif
160 
161 /* ==================================================================== */
162 /* Base portability stuff ... this stuff may need to be */
163 /* modified for new platforms. */
164 /* ==================================================================== */
165 
166 /* -------------------------------------------------------------------- */
167 /* Which versions of C++ are available. */
168 /* -------------------------------------------------------------------- */
169 
170 /* MSVC fails to define a decent value of __cplusplus. Try to target VS2015*/
171 /* as a minimum */
172 
173 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
174 # if !(__cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900))
175 # error Must have C++11 or newer.
176 # endif
177 # if __cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L)
178 # define HAVE_CXX14 1
179 # endif
180 # if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
181 # define HAVE_CXX17 1
182 # endif
183 #endif /* __cplusplus */
184 
185 /*---------------------------------------------------------------------
186  * types for 16 and 32 bits integers, etc...
187  *--------------------------------------------------------------------*/
188 #if UINT_MAX == 65535
189 typedef long GInt32;
190 typedef unsigned long GUInt32;
191 #else
193 typedef int GInt32;
195 typedef unsigned int GUInt32;
196 #endif
197 
199 typedef short GInt16;
201 typedef unsigned short GUInt16;
203 typedef unsigned char GByte;
204 /* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef bool GBool" */
205 /* in include/poppler/goo/gtypes.h */
206 #ifndef CPL_GBOOL_DEFINED
208 #define CPL_GBOOL_DEFINED
211 typedef int GBool;
212 #endif
213 
215 #ifdef __cplusplus
216 #define CPL_STATIC_CAST(type, expr) static_cast<type>(expr)
217 #define CPL_REINTERPRET_CAST(type, expr) reinterpret_cast<type>(expr)
218 #else
219 #define CPL_STATIC_CAST(type, expr) ((type)(expr))
220 #define CPL_REINTERPRET_CAST(type, expr) ((type)(expr))
221 #endif
224 /* -------------------------------------------------------------------- */
225 /* 64bit support */
226 /* -------------------------------------------------------------------- */
227 
230 typedef long long GIntBig;
233 typedef unsigned long long GUIntBig;
234 
236 #define GINTBIG_MIN (CPL_STATIC_CAST(GIntBig, 0x80000000) << 32)
238 #define GINTBIG_MAX ((CPL_STATIC_CAST(GIntBig, 0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
240 #define GUINTBIG_MAX ((CPL_STATIC_CAST(GUIntBig, 0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
241 
243 #define CPL_HAS_GINT64 1
246 /* Note: we might want to use instead int64_t / uint64_t if they are available */
247 
249 typedef GIntBig GInt64;
252 
254 #define GINT64_MIN GINTBIG_MIN
256 #define GINT64_MAX GINTBIG_MAX
258 #define GUINT64_MAX GUINTBIG_MAX
259 
260 
261 #if SIZEOF_VOIDP == 8
263 typedef GIntBig GPtrDiff_t;
264 #else
266 typedef int GPtrDiff_t;
267 #endif
268 
269 #ifdef GDAL_COMPILATION
270 #include <stdint.h>
271 typedef uintptr_t GUIntptr_t;
272 #define CPL_IS_ALIGNED(ptr, quant) ((CPL_REINTERPRET_CAST(GUIntptr_t, CPL_STATIC_CAST(const void*, ptr)) % (quant)) == 0)
273 
274 #endif
275 
276 #if (defined(__MSVCRT__) && !(defined(__MINGW64__) && __GNUC__ >= 10)) || (defined(WIN32) && defined(_MSC_VER))
277  #define CPL_FRMT_GB_WITHOUT_PREFIX "I64"
278 #else
280  #define CPL_FRMT_GB_WITHOUT_PREFIX "ll"
281 #endif
282 
284 #define CPL_FRMT_GIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
286 #define CPL_FRMT_GUIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
287 
289 #ifdef COMPAT_WITH_ICC_CONVERSION_CHECK
290 #define CPL_INT64_FITS_ON_INT32(x) ((x) >= INT_MIN && (x) <= INT_MAX)
291 #else
292 #define CPL_INT64_FITS_ON_INT32(x) (CPL_STATIC_CAST(GIntBig, CPL_STATIC_CAST(int, x)) == (x))
293 #endif
296 /* ==================================================================== */
297 /* Other standard services. */
298 /* ==================================================================== */
299 #ifdef __cplusplus
301 # define CPL_C_START extern "C" {
303 # define CPL_C_END }
304 #else
305 # define CPL_C_START
306 # define CPL_C_END
307 #endif
308 
309 #ifndef CPL_DLL
310 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
311 # ifdef GDAL_COMPILATION
312 # define CPL_DLL __declspec(dllexport)
313 # else
314 # define CPL_DLL
315 # endif
316 # define CPL_INTERNAL
317 #else
318 # if defined(USE_GCC_VISIBILITY_FLAG)
319 # define CPL_DLL __attribute__ ((visibility("default")))
320 # if !defined(__MINGW32__)
321 # define CPL_INTERNAL __attribute__((visibility("hidden")))
322 # else
323 # define CPL_INTERNAL
324 # endif
325 # else
326 # define CPL_DLL
327 # define CPL_INTERNAL
328 # endif
329 #endif
330 
331 // Marker for unstable API
332 #define CPL_UNSTABLE_API CPL_DLL
333 
334 #endif
335 
337 /* Should optional (normally private) interfaces be exported? */
338 #ifdef CPL_OPTIONAL_APIS
339 # define CPL_ODLL CPL_DLL
340 #else
341 # define CPL_ODLL
342 #endif
345 #ifndef CPL_STDCALL
346 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
347 # define CPL_STDCALL __stdcall
348 #else
349 # define CPL_STDCALL
350 #endif
351 #endif
352 
354 #ifdef _MSC_VER
355 # define FORCE_CDECL __cdecl
356 #else
357 # define FORCE_CDECL
358 #endif
362 /* TODO : support for other compilers needed */
363 #if (defined(__GNUC__) && !defined(__NO_INLINE__)) || defined(_MSC_VER)
364 #define HAS_CPL_INLINE 1
365 #define CPL_INLINE __inline
366 #elif defined(__SUNPRO_CC)
367 #define HAS_CPL_INLINE 1
368 #define CPL_INLINE inline
369 #else
370 #define CPL_INLINE
371 #endif
374 #ifndef MAX
376 # define MIN(a,b) (((a)<(b)) ? (a) : (b))
378 # define MAX(a,b) (((a)>(b)) ? (a) : (b))
379 #endif
380 
381 #ifndef ABS
383 # define ABS(x) (((x)<0) ? (-1*(x)) : (x))
384 #endif
385 
386 #ifndef M_PI
388 # define M_PI 3.14159265358979323846
389 /* 3.1415926535897932384626433832795 */
390 #endif
391 
392 /* -------------------------------------------------------------------- */
393 /* Macro to test equality of two floating point values. */
394 /* We use fabs() function instead of ABS() macro to avoid side */
395 /* effects. */
396 /* -------------------------------------------------------------------- */
398 #ifndef CPLIsEqual
399 # define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
400 #endif
403 /* -------------------------------------------------------------------- */
404 /* Provide macros for case insensitive string comparisons. */
405 /* -------------------------------------------------------------------- */
406 #ifndef EQUAL
407 
408 #if defined(AFL_FRIENDLY) && defined(__GNUC__)
409 
410 static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
411  __attribute__((always_inline));
412 
413 static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
414 {
415  const unsigned char* bptr1 = (const unsigned char*)ptr1;
416  const unsigned char* bptr2 = (const unsigned char*)ptr2;
417  while( len-- )
418  {
419  unsigned char b1 = *(bptr1++);
420  unsigned char b2 = *(bptr2++);
421  if( b1 != b2 ) return b1 - b2;
422  }
423  return 0;
424 }
425 
426 static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
427  __attribute__((always_inline));
428 
429 static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
430 {
431  const unsigned char* usptr1 = (const unsigned char*)ptr1;
432  const unsigned char* usptr2 = (const unsigned char*)ptr2;
433  while( 1 )
434  {
435  unsigned char ch1 = *(usptr1++);
436  unsigned char ch2 = *(usptr2++);
437  if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
438  }
439 }
440 
441 static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
442  __attribute__((always_inline));
443 
444 static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
445 {
446  const unsigned char* usptr1 = (const unsigned char*)ptr1;
447  const unsigned char* usptr2 = (const unsigned char*)ptr2;
448  while( len -- )
449  {
450  unsigned char ch1 = *(usptr1++);
451  unsigned char ch2 = *(usptr2++);
452  if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
453  }
454  return 0;
455 }
456 
457 static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
458  __attribute__((always_inline));
459 
460 static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
461 {
462  const unsigned char* usptr1 = (const unsigned char*)ptr1;
463  const unsigned char* usptr2 = (const unsigned char*)ptr2;
464  while( 1 )
465  {
466  unsigned char ch1 = *(usptr1++);
467  unsigned char ch2 = *(usptr2++);
468  ch1 = (unsigned char)toupper(ch1);
469  ch2 = (unsigned char)toupper(ch2);
470  if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
471  }
472 }
473 
474 static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
475  __attribute__((always_inline));
476 
477 static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
478 {
479  const unsigned char* usptr1 = (const unsigned char*)ptr1;
480  const unsigned char* usptr2 = (const unsigned char*)ptr2;
481  while( len-- )
482  {
483  unsigned char ch1 = *(usptr1++);
484  unsigned char ch2 = *(usptr2++);
485  ch1 = (unsigned char)toupper(ch1);
486  ch2 = (unsigned char)toupper(ch2);
487  if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
488  }
489  return 0;
490 }
491 
492 static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
493  __attribute__((always_inline));
494 
495 static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
496 {
497  const char* ptr_haystack = haystack;
498  while( 1 )
499  {
500  const char* ptr_haystack2 = ptr_haystack;
501  const char* ptr_needle = needle;
502  while( 1 )
503  {
504  char ch1 = *(ptr_haystack2++);
505  char ch2 = *(ptr_needle++);
506  if( ch2 == 0 )
507  return (char*)ptr_haystack;
508  if( ch1 != ch2 )
509  break;
510  }
511  if( *ptr_haystack == 0 )
512  return NULL;
513  ptr_haystack ++;
514  }
515 }
516 
517 #undef strcmp
518 #undef strncmp
519 #define memcmp CPL_afl_friendly_memcmp
520 #define strcmp CPL_afl_friendly_strcmp
521 #define strncmp CPL_afl_friendly_strncmp
522 #define strcasecmp CPL_afl_friendly_strcasecmp
523 #define strncasecmp CPL_afl_friendly_strncasecmp
524 #define strstr CPL_afl_friendly_strstr
525 
526 #endif /* defined(AFL_FRIENDLY) && defined(__GNUC__) */
527 
528 # if defined(WIN32)
529 # define STRCASECMP(a,b) (_stricmp(a,b))
530 # define STRNCASECMP(a,b,n) (_strnicmp(a,b,n))
531 # else
533 # define STRCASECMP(a,b) (strcasecmp(a,b))
535 # define STRNCASECMP(a,b,n) (strncasecmp(a,b,n))
536 # endif
538 # define EQUALN(a,b,n) (STRNCASECMP(a,b,n)==0)
540 # define EQUAL(a,b) (STRCASECMP(a,b)==0)
541 #endif
542 
543 /*---------------------------------------------------------------------
544  * Does a string "a" start with string "b". Search is case-sensitive or,
545  * with CI, it is a case-insensitive comparison.
546  *--------------------------------------------------------------------- */
547 #ifndef STARTS_WITH_CI
549 #define STARTS_WITH(a,b) (strncmp(a,b,strlen(b)) == 0)
551 #define STARTS_WITH_CI(a,b) EQUALN(a,b,strlen(b))
552 #endif
553 
555 #ifndef CPL_THREADLOCAL
556 # define CPL_THREADLOCAL
557 #endif
560 /* -------------------------------------------------------------------- */
561 /* Handle isnan() and isinf(). Note that isinf() and isnan() */
562 /* are supposed to be macros according to C99, defined in math.h */
563 /* Some systems (i.e. Tru64) don't have isinf() at all, so if */
564 /* the macro is not defined we just assume nothing is infinite. */
565 /* This may mean we have no real CPLIsInf() on systems with isinf()*/
566 /* function but no corresponding macro, but I can live with */
567 /* that since it isn't that important a test. */
568 /* -------------------------------------------------------------------- */
569 #ifdef _MSC_VER
570 # include <float.h>
571 # define CPLIsNan(x) _isnan(x)
572 # define CPLIsInf(x) (!_isnan(x) && !_finite(x))
573 # define CPLIsFinite(x) _finite(x)
574 #elif defined(__GNUC__) && ( __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 4 ) )
575 /* When including <cmath> in C++11 the isnan() macro is undefined, so that */
576 /* std::isnan() can work (#6489). This is a GCC specific workaround for now. */
577 # define CPLIsNan(x) __builtin_isnan(x)
578 # define CPLIsInf(x) __builtin_isinf(x)
579 # define CPLIsFinite(x) __builtin_isfinite(x)
580 #elif defined(__cplusplus) && defined(HAVE_STD_IS_NAN) && HAVE_STD_IS_NAN
581 extern "C++" {
582 #ifndef DOXYGEN_SKIP
583 #include <cmath>
584 #endif
585 static inline int CPLIsNan(float f) { return std::isnan(f); }
586 static inline int CPLIsNan(double f) { return std::isnan(f); }
587 static inline int CPLIsInf(float f) { return std::isinf(f); }
588 static inline int CPLIsInf(double f) { return std::isinf(f); }
589 static inline int CPLIsFinite(float f) { return std::isfinite(f); }
590 static inline int CPLIsFinite(double f) { return std::isfinite(f); }
591 }
592 #else
594 #if defined(__cplusplus) && defined(__GNUC__) && defined(__linux) && !defined(__ANDROID__) && !defined(CPL_SUPRESS_CPLUSPLUS)
595 /* so to not get warning about conversion from double to float with */
596 /* gcc -Wfloat-conversion when using isnan()/isinf() macros */
597 extern "C++" {
598 static inline int CPLIsNan(float f) { return __isnanf(f); }
599 static inline int CPLIsNan(double f) { return __isnan(f); }
600 static inline int CPLIsInf(float f) { return __isinff(f); }
601 static inline int CPLIsInf(double f) { return __isinf(f); }
602 static inline int CPLIsFinite(float f) { return !__isnanf(f) && !__isinff(f); }
603 static inline int CPLIsFinite(double f) { return !__isnan(f) && !__isinf(f); }
604 }
605 #else
606 # define CPLIsNan(x) isnan(x)
607 # if defined(isinf) || defined(__FreeBSD__)
609 # define CPLIsInf(x) isinf(x)
611 # define CPLIsFinite(x) (!isnan(x) && !isinf(x))
612 # elif defined(__sun__)
613 # include <ieeefp.h>
614 # define CPLIsInf(x) (!finite(x) && !isnan(x))
615 # define CPLIsFinite(x) finite(x)
616 # else
617 # define CPLIsInf(x) (0)
618 # define CPLIsFinite(x) (!isnan(x))
619 # endif
620 #endif
621 #endif
622 
624 /*---------------------------------------------------------------------
625  * CPL_LSB and CPL_MSB
626  * Only one of these 2 macros should be defined and specifies the byte
627  * ordering for the current platform.
628  * This should be defined in the Makefile, but if it is not then
629  * the default is CPL_LSB (Intel ordering, LSB first).
630  *--------------------------------------------------------------------*/
631 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
632 # define CPL_MSB
633 #endif
634 
635 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
636 #define CPL_LSB
637 #endif
638 
639 #if defined(CPL_LSB)
640 # define CPL_IS_LSB 1
641 #else
642 # define CPL_IS_LSB 0
643 #endif
646 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
647 
649 extern "C++" {
650 
651 template <bool b> struct CPLStaticAssert {};
652 template<> struct CPLStaticAssert<true>
653 {
654  static void my_function() {}
655 };
656 
657 } /* extern "C++" */
658 
659 #define CPL_STATIC_ASSERT(x) CPLStaticAssert<x>::my_function()
660 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x) CPL_STATIC_ASSERT(x)
661 
662 #else /* __cplusplus */
663 
664 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x)
665 
666 #endif /* __cplusplus */
669 /*---------------------------------------------------------------------
670  * Little endian <==> big endian byte swap macros.
671  *--------------------------------------------------------------------*/
672 
674 #define CPL_SWAP16(x) CPL_STATIC_CAST(GUInt16, (CPL_STATIC_CAST(GUInt16, x) << 8) | (CPL_STATIC_CAST(GUInt16, x) >> 8) )
675 
676 #if defined(HAVE_GCC_BSWAP)
678 #define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, __builtin_bswap32(CPL_STATIC_CAST(GUInt32, x)))
680 #define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, __builtin_bswap64(CPL_STATIC_CAST(GUInt64, x)))
681 #elif defined(_MSC_VER)
682 #define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, _byteswap_ulong(CPL_STATIC_CAST(GUInt32, x)))
683 #define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, _byteswap_uint64(CPL_STATIC_CAST(GUInt64, x)))
684 #else
686 #define CPL_SWAP32(x) \
687  CPL_STATIC_CAST(GUInt32, \
688  ((CPL_STATIC_CAST(GUInt32, x) & 0x000000ffU) << 24) | \
689  ((CPL_STATIC_CAST(GUInt32, x) & 0x0000ff00U) << 8) | \
690  ((CPL_STATIC_CAST(GUInt32, x) & 0x00ff0000U) >> 8) | \
691  ((CPL_STATIC_CAST(GUInt32, x) & 0xff000000U) >> 24) )
692 
694 #define CPL_SWAP64(x) \
695  ((CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, x))) << 32) | \
696  (CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, CPL_STATIC_CAST(GUInt64, x) >> 32)))))
697 
698 #endif
699 
701 #define CPL_SWAP16PTR(x) \
702 do { \
703  GUInt16 _n16; \
704  void* _lx = x; \
705  memcpy(&_n16, _lx, 2); \
706  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2); \
707  _n16 = CPL_SWAP16(_n16); \
708  memcpy(_lx, &_n16, 2); \
709 } while(0)
710 
712 #define CPL_SWAP32PTR(x) \
713 do { \
714  GUInt32 _n32; \
715  void* _lx = x; \
716  memcpy(&_n32, _lx, 4); \
717  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4); \
718  _n32 = CPL_SWAP32(_n32); \
719  memcpy(_lx, &_n32, 4); \
720 } while(0)
721 
723 #define CPL_SWAP64PTR(x) \
724 do { \
725  GUInt64 _n64; \
726  void* _lx = x; \
727  memcpy(&_n64, _lx, 8); \
728  CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
729  _n64 = CPL_SWAP64(_n64); \
730  memcpy(_lx, &_n64, 8); \
731 } while(0)
732 
734 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
735 
736 #ifdef CPL_MSB
737 # define CPL_MSBWORD16(x) (x)
738 # define CPL_LSBWORD16(x) CPL_SWAP16(x)
739 # define CPL_MSBWORD32(x) (x)
740 # define CPL_LSBWORD32(x) CPL_SWAP32(x)
741 # define CPL_MSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
742 # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
743 # define CPL_MSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
744 # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
745 # define CPL_MSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
746 # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
747 #else
749 # define CPL_LSBWORD16(x) (x)
751 # define CPL_MSBWORD16(x) CPL_SWAP16(x)
753 # define CPL_LSBWORD32(x) (x)
755 # define CPL_MSBWORD32(x) CPL_SWAP32(x)
757 # define CPL_LSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
759 # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
761 # define CPL_LSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
763 # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
765 # define CPL_LSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
767 # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
768 #endif
769 
773 #define CPL_LSBINT16PTR(x) ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8))
774 
778 #define CPL_LSBINT32PTR(x) ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8) | \
779  (*((CPL_REINTERPRET_CAST(const GByte*, x))+2) << 16) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+3) << 24))
780 
782 #define CPL_LSBSINT16PTR(x) CPL_STATIC_CAST(GInt16,CPL_LSBINT16PTR(x))
783 
785 #define CPL_LSBUINT16PTR(x) CPL_STATIC_CAST(GUInt16, CPL_LSBINT16PTR(x))
786 
788 #define CPL_LSBSINT32PTR(x) CPL_STATIC_CAST(GInt32, CPL_LSBINT32PTR(x))
789 
791 #define CPL_LSBUINT32PTR(x) CPL_STATIC_CAST(GUInt32, CPL_LSBINT32PTR(x))
792 
794 /* Utility macro to explicitly mark intentionally unreferenced parameters. */
795 #ifndef UNREFERENCED_PARAM
796 # ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
797 # define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
798 # else
799 # define UNREFERENCED_PARAM(param) ((void)param)
800 # endif /* UNREFERENCED_PARAMETER */
801 #endif /* UNREFERENCED_PARAM */
804 /***********************************************************************
805  * Define CPL_CVSID() macro. It can be disabled during a build by
806  * defining DISABLE_CVSID in the compiler options.
807  *
808  * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
809  * being unused.
810  */
811 
813 #ifndef DISABLE_CVSID
814 #if defined(__GNUC__) && __GNUC__ >= 4
815 # define CPL_CVSID(string) static const char cpl_cvsid[] __attribute__((used)) = string;
816 #else
817 # define CPL_CVSID(string) static const char cpl_cvsid[] = string; \
818 static const char *cvsid_aw() { return( cvsid_aw() ? NULL : cpl_cvsid ); }
819 #endif
820 #else
821 # define CPL_CVSID(string)
822 #endif
825 /* We exclude mingw64 4.6 which seems to be broken regarding this */
826 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP) && !(defined(__MINGW64__) && __GNUC__ == 4 && __GNUC_MINOR__ == 6)
828 # define CPL_NULL_TERMINATED __attribute__((__sentinel__))
829 #else
831 # define CPL_NULL_TERMINATED
832 #endif
833 
834 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
836 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
838 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
839 #else
841 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
843 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx )
844 #endif
845 
846 #if defined(_MSC_VER) && (defined(GDAL_COMPILATION) || defined(CPL_ENABLE_MSVC_ANNOTATIONS))
847 #include <sal.h>
850 # define CPL_FORMAT_STRING(arg) _Printf_format_string_ arg
853 # define CPL_SCANF_FORMAT_STRING(arg) _Scanf_format_string_ arg
854 #else
856 # define CPL_FORMAT_STRING(arg) arg
858 # define CPL_SCANF_FORMAT_STRING(arg) arg
859 #endif /* defined(_MSC_VER) && defined(GDAL_COMPILATION) */
860 
861 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
863 #define CPL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
864 #else
866 #define CPL_WARN_UNUSED_RESULT
867 #endif
868 
869 #if defined(__GNUC__) && __GNUC__ >= 4
871 # define CPL_UNUSED __attribute((__unused__))
872 #else
873 /* TODO: add cases for other compilers */
875 # define CPL_UNUSED
876 #endif
877 
878 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
880 #define CPL_NO_RETURN __attribute__((noreturn))
881 #else
883 #define CPL_NO_RETURN
884 #endif
885 
887 /* Clang __has_attribute */
888 #ifndef __has_attribute
889  #define __has_attribute(x) 0 // Compatibility with non-clang compilers.
890 #endif
891 
894 #if ((defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))) || __has_attribute(returns_nonnull)) && !defined(DOXYGEN_SKIP) && !defined(__INTEL_COMPILER)
896 # define CPL_RETURNS_NONNULL __attribute__((returns_nonnull))
897 #else
899 # define CPL_RETURNS_NONNULL
900 #endif
901 
902 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
904 #define CPL_RESTRICT __restrict__
905 #else
907 #define CPL_RESTRICT
908 #endif
909 
910 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
911 
914 # define CPL_OVERRIDE override
915 
917 # define CPL_FINAL final
918 
920 # define CPL_NON_FINAL
921 
927 # define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
928  ClassName( const ClassName & ) = delete; \
929  ClassName &operator=( const ClassName & ) = delete;
930 
931 #endif /* __cplusplus */
932 
933 #if !defined(DOXYGEN_SKIP) && !defined(CPL_WARN_DEPRECATED)
934 #if defined(__has_extension)
935  #if __has_extension(attribute_deprecated_with_message)
936  /* Clang extension */
937  #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated(x)))
938  #else
939  #define CPL_WARN_DEPRECATED(x)
940  #endif
941 #elif defined(__GNUC__)
942  #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated))
943 #else
944  #define CPL_WARN_DEPRECATED(x)
945 #endif
946 #endif
947 
948 #if !defined(_MSC_VER) && !defined(__APPLE__) && !defined(_FORTIFY_SOURCE)
950 # if defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF)
951 int vsnprintf(char *str, size_t size, const char* fmt, va_list args)
952  CPL_WARN_DEPRECATED("Use CPLvsnprintf() instead");
953 int snprintf(char *str, size_t size, const char* fmt, ...)
955  CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
956 int sprintf(char *str, const char* fmt, ...)
958  CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
959 # elif defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
960 int sprintf(char *str, const char* fmt, ...)
962  CPL_WARN_DEPRECATED("Use snprintf() or CPLsnprintf() instead");
963 # endif /* defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF) */
964 CPL_C_END
965 #endif /* !defined(_MSC_VER) && !defined(__APPLE__) */
966 
967 #if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
969 #define CPL_CPU_REQUIRES_ALIGNED_ACCESS
971 #endif
972 
973 #if defined(__cplusplus)
974 #ifndef CPPCHECK
976 #define CPL_ARRAYSIZE(array) \
977  ((sizeof(array) / sizeof(*(array))) / \
978  static_cast<size_t>(!(sizeof(array) % sizeof(*(array)))))
979 #else
980 /* simplified version for cppcheck */
981 #define CPL_ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))
982 #endif
983 
984 extern "C++" {
985 template<class T> static void CPL_IGNORE_RET_VAL(const T&) {}
986 inline static bool CPL_TO_BOOL(int x) { return x != 0; }
987 } /* extern "C++" */
988 
989 #endif /* __cplusplus */
990 
991 #if (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || (defined(__clang__) && __clang_major__ >= 3)) && !defined(_MSC_VER))
992 #define HAVE_GCC_DIAGNOSTIC_PUSH
993 #endif
994 
995 #if ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) && !defined(_MSC_VER))
996 #define HAVE_GCC_SYSTEM_HEADER
997 #endif
998 
999 #if ((defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7))) || __GNUC__ >= 7)
1001 # define CPL_FALLTHROUGH [[clang::fallthrough]];
1002 #else
1004 # define CPL_FALLTHROUGH
1005 #endif
1006 
1009 #ifndef FALSE
1010 # define FALSE 0
1011 #endif
1012 
1013 #ifndef TRUE
1014 # define TRUE 1
1015 #endif
1016 
1017 #if __clang_major__ >= 4 || (__clang_major__ == 3 && __clang_minor__ >= 8)
1018 #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW __attribute__((no_sanitize("unsigned-integer-overflow")))
1019 #else
1020 #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
1021 #endif
1022 
1023 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && defined(GDAL_COMPILATION)
1024 extern "C++" {
1025 template<class C, class A, class B>
1026 CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
1027 inline C CPLUnsanitizedAdd(A a, B b)
1028 {
1029  return a + b;
1030 }
1031 }
1032 #endif
1033 
1034 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
1035 #define CPL_NULLPTR nullptr
1036 #else
1037 #define CPL_NULLPTR NULL
1038 #endif
1041 /* This typedef is for C functions that take char** as argument, but */
1042 /* with the semantics of a const list. In C, char** is not implicitly cast to */
1043 /* const char* const*, contrary to C++. So when seen for C++, it is OK */
1044 /* to expose the prototypes as const char* const*, but for C we keep the */
1045 /* historical definition to avoid warnings. */
1046 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && !defined(DOXYGEN_SKIP)
1049 typedef const char* const* CSLConstList;
1050 #else
1053 typedef char** CSLConstList;
1054 #endif
1055 
1056 #endif /* ndef CPL_BASE_H_INCLUDED */
int GPtrDiff_t
Integer type large enough to hold the difference between 2 addresses.
Definition: cpl_port.h:266
#define CPLIsInf(x)
Return whether a floating-pointer number is +/- infinity.
Definition: cpl_port.h:609
unsigned long long GUIntBig
Large unsigned integer type (generally 64-bit unsigned integer type).
Definition: cpl_port.h:233
short GInt16
Int16 type.
Definition: cpl_port.h:199
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:303
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:301
GIntBig GInt64
Signed 64 bit integer type.
Definition: cpl_port.h:249
int GBool
Type for boolean values (alias to int)
Definition: cpl_port.h:211
unsigned int GUInt32
Unsigned int32 type.
Definition: cpl_port.h:195
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition: cpl_port.h:841
#define CPLIsNan(x)
Return whether a floating-pointer number is NaN.
Definition: cpl_port.h:606
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1053
GUIntBig GUInt64
Unsigned 64 bit integer type.
Definition: cpl_port.h:251
unsigned short GUInt16
Unsigned int16 type.
Definition: cpl_port.h:201
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:203
int GInt32
Int32 type.
Definition: cpl_port.h:193
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition: cpl_port.h:230
#define CPLIsFinite(x)
Return whether a floating-pointer number is finite.
Definition: cpl_port.h:611
int CPLsnprintf(char *str, size_t size, const char *fmt,...)
snprintf() wrapper that is not sensitive to LC_NUMERIC settings.
Definition: cpl_string.cpp:1349