GDAL
cpl_port.h
Go to the documentation of this file.
1/******************************************************************************
2 * $Id: cpl_port.h 7d34df879ac151e8cc377ecd5d0107c579b0c0a2 2019-04-25 12:22:24 +0200 Even Rouault $
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 mines-paris dot org>
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#if defined(HAVE_ERRNO_H)
148# include <errno.h>
149#endif
150
151#ifdef HAVE_LOCALE_H
152# include <locale.h>
153#endif
154
155#ifdef HAVE_DIRECT_H
156# include <direct.h>
157#endif
158
159#if !defined(WIN32)
160# include <strings.h>
161#endif
162
163#if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
164# define DBMALLOC
165# include <dbmalloc.h>
166#endif
167
168#if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
169# define USE_DMALLOC
170# include <dmalloc.h>
171#endif
172
173/* ==================================================================== */
174/* Base portability stuff ... this stuff may need to be */
175/* modified for new platforms. */
176/* ==================================================================== */
177
178/* -------------------------------------------------------------------- */
179/* Which versions of C++ are available. */
180/* -------------------------------------------------------------------- */
181
182/* MSVC fails to define a decent value of __cplusplus. Try to target VS2015*/
183/* as a minimum */
184
185#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
186# if !(__cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900))
187# error Must have C++11 or newer.
188# endif
189# if __cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L)
190# define HAVE_CXX14 1
191# endif
192# if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
193# define HAVE_CXX17 1
194# endif
195#endif /* __cplusplus */
196
197/*---------------------------------------------------------------------
198 * types for 16 and 32 bits integers, etc...
199 *--------------------------------------------------------------------*/
200#if UINT_MAX == 65535
201typedef long GInt32;
202typedef unsigned long GUInt32;
203#else
205typedef int GInt32;
207typedef unsigned int GUInt32;
208#endif
209
211typedef short GInt16;
213typedef unsigned short GUInt16;
215typedef unsigned char GByte;
216/* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef bool GBool" */
217/* in include/poppler/goo/gtypes.h */
218#ifndef CPL_GBOOL_DEFINED
220#define CPL_GBOOL_DEFINED
223typedef int GBool;
224#endif
225
227#ifdef __cplusplus
228#define CPL_STATIC_CAST(type, expr) static_cast<type>(expr)
229#define CPL_REINTERPRET_CAST(type, expr) reinterpret_cast<type>(expr)
230#else
231#define CPL_STATIC_CAST(type, expr) ((type)(expr))
232#define CPL_REINTERPRET_CAST(type, expr) ((type)(expr))
233#endif
236/* -------------------------------------------------------------------- */
237/* 64bit support */
238/* -------------------------------------------------------------------- */
239
240#if defined(WIN32) && defined(_MSC_VER)
241#define VSI_LARGE_API_SUPPORTED
242#endif
243
244#if HAVE_LONG_LONG
245
248typedef long long GIntBig;
251typedef unsigned long long GUIntBig;
252
254#define GINTBIG_MIN (CPL_STATIC_CAST(GIntBig, 0x80000000) << 32)
256#define GINTBIG_MAX ((CPL_STATIC_CAST(GIntBig, 0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
258#define GUINTBIG_MAX ((CPL_STATIC_CAST(GUIntBig, 0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
259
261#define CPL_HAS_GINT64 1
264/* Note: we might want to use instead int64_t / uint64_t if they are available */
265
270
272#define GINT64_MIN GINTBIG_MIN
274#define GINT64_MAX GINTBIG_MAX
276#define GUINT64_MAX GUINTBIG_MAX
277
278#else
279
280#error "64bit integer support required"
281
282#endif
283
284#if SIZEOF_VOIDP == 8
287#else
289typedef int GPtrDiff_t;
290#endif
291
292#ifdef GDAL_COMPILATION
293#if HAVE_UINTPTR_T
294#include <stdint.h>
295typedef uintptr_t GUIntptr_t;
296#elif SIZEOF_VOIDP == 8
297typedef GUIntBig GUIntptr_t;
298#else
299typedef unsigned int GUIntptr_t;
300#endif
301
302#define CPL_IS_ALIGNED(ptr, quant) ((CPL_REINTERPRET_CAST(GUIntptr_t, CPL_STATIC_CAST(const void*, ptr)) % (quant)) == 0)
303
304#endif
305
306#if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
307 #define CPL_FRMT_GB_WITHOUT_PREFIX "I64"
308#elif HAVE_LONG_LONG
310 #define CPL_FRMT_GB_WITHOUT_PREFIX "ll"
311#else
312 #define CPL_FRMT_GB_WITHOUT_PREFIX "l"
313#endif
314
316#define CPL_FRMT_GIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
318#define CPL_FRMT_GUIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
319
321#define GUINTBIG_TO_DOUBLE(x) CPL_STATIC_CAST(double, x)
325#ifdef COMPAT_WITH_ICC_CONVERSION_CHECK
326#define CPL_INT64_FITS_ON_INT32(x) ((x) >= INT_MIN && (x) <= INT_MAX)
327#else
328#define CPL_INT64_FITS_ON_INT32(x) (CPL_STATIC_CAST(GIntBig, CPL_STATIC_CAST(int, x)) == (x))
329#endif
332/* ==================================================================== */
333/* Other standard services. */
334/* ==================================================================== */
335#ifdef __cplusplus
337# define CPL_C_START extern "C" {
339# define CPL_C_END }
340#else
341# define CPL_C_START
342# define CPL_C_END
343#endif
344
345#ifndef CPL_DLL
346#if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
347# define CPL_DLL __declspec(dllexport)
348# define CPL_INTERNAL
349#else
350# if defined(USE_GCC_VISIBILITY_FLAG)
351# define CPL_DLL __attribute__ ((visibility("default")))
352# if !defined(__MINGW32__)
353# define CPL_INTERNAL __attribute__((visibility("hidden")))
354# else
355# define CPL_INTERNAL
356# endif
357# else
358# define CPL_DLL
359# define CPL_INTERNAL
360# endif
361#endif
362
363#endif
364
366/* Should optional (normally private) interfaces be exported? */
367#ifdef CPL_OPTIONAL_APIS
368# define CPL_ODLL CPL_DLL
369#else
370# define CPL_ODLL
371#endif
374#ifndef CPL_STDCALL
375#if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
376# define CPL_STDCALL __stdcall
377#else
378# define CPL_STDCALL
379#endif
380#endif
381
383#ifdef _MSC_VER
384# define FORCE_CDECL __cdecl
385#else
386# define FORCE_CDECL
387#endif
391/* TODO : support for other compilers needed */
392#if (defined(__GNUC__) && !defined(__NO_INLINE__)) || defined(_MSC_VER)
393#define HAS_CPL_INLINE 1
394#define CPL_INLINE __inline
395#elif defined(__SUNPRO_CC)
396#define HAS_CPL_INLINE 1
397#define CPL_INLINE inline
398#else
399#define CPL_INLINE
400#endif
403#ifndef MAX
405# define MIN(a,b) (((a)<(b)) ? (a) : (b))
407# define MAX(a,b) (((a)>(b)) ? (a) : (b))
408#endif
409
410#ifndef ABS
412# define ABS(x) (((x)<0) ? (-1*(x)) : (x))
413#endif
414
415#ifndef M_PI
417# define M_PI 3.14159265358979323846
418/* 3.1415926535897932384626433832795 */
419#endif
420
421/* -------------------------------------------------------------------- */
422/* Macro to test equality of two floating point values. */
423/* We use fabs() function instead of ABS() macro to avoid side */
424/* effects. */
425/* -------------------------------------------------------------------- */
427#ifndef CPLIsEqual
428# define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
429#endif
432/* -------------------------------------------------------------------- */
433/* Provide macros for case insensitive string comparisons. */
434/* -------------------------------------------------------------------- */
435#ifndef EQUAL
436
437#if defined(AFL_FRIENDLY) && defined(__GNUC__)
438
439static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
440 __attribute__((always_inline));
441
442static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
443{
444 const unsigned char* bptr1 = (const unsigned char*)ptr1;
445 const unsigned char* bptr2 = (const unsigned char*)ptr2;
446 while( len-- )
447 {
448 unsigned char b1 = *(bptr1++);
449 unsigned char b2 = *(bptr2++);
450 if( b1 != b2 ) return b1 - b2;
451 }
452 return 0;
453}
454
455static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
456 __attribute__((always_inline));
457
458static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
459{
460 const unsigned char* usptr1 = (const unsigned char*)ptr1;
461 const unsigned char* usptr2 = (const unsigned char*)ptr2;
462 while( 1 )
463 {
464 unsigned char ch1 = *(usptr1++);
465 unsigned char ch2 = *(usptr2++);
466 if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
467 }
468}
469
470static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
471 __attribute__((always_inline));
472
473static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
474{
475 const unsigned char* usptr1 = (const unsigned char*)ptr1;
476 const unsigned char* usptr2 = (const unsigned char*)ptr2;
477 while( len -- )
478 {
479 unsigned char ch1 = *(usptr1++);
480 unsigned char ch2 = *(usptr2++);
481 if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
482 }
483 return 0;
484}
485
486static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
487 __attribute__((always_inline));
488
489static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
490{
491 const unsigned char* usptr1 = (const unsigned char*)ptr1;
492 const unsigned char* usptr2 = (const unsigned char*)ptr2;
493 while( 1 )
494 {
495 unsigned char ch1 = *(usptr1++);
496 unsigned char ch2 = *(usptr2++);
497 ch1 = (unsigned char)toupper(ch1);
498 ch2 = (unsigned char)toupper(ch2);
499 if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
500 }
501}
502
503static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
504 __attribute__((always_inline));
505
506static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
507{
508 const unsigned char* usptr1 = (const unsigned char*)ptr1;
509 const unsigned char* usptr2 = (const unsigned char*)ptr2;
510 while( len-- )
511 {
512 unsigned char ch1 = *(usptr1++);
513 unsigned char ch2 = *(usptr2++);
514 ch1 = (unsigned char)toupper(ch1);
515 ch2 = (unsigned char)toupper(ch2);
516 if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
517 }
518 return 0;
519}
520
521static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
522 __attribute__((always_inline));
523
524static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
525{
526 const char* ptr_haystack = haystack;
527 while( 1 )
528 {
529 const char* ptr_haystack2 = ptr_haystack;
530 const char* ptr_needle = needle;
531 while( 1 )
532 {
533 char ch1 = *(ptr_haystack2++);
534 char ch2 = *(ptr_needle++);
535 if( ch2 == 0 )
536 return (char*)ptr_haystack;
537 if( ch1 != ch2 )
538 break;
539 }
540 if( *ptr_haystack == 0 )
541 return NULL;
542 ptr_haystack ++;
543 }
544}
545
546#undef strcmp
547#undef strncmp
548#define memcmp CPL_afl_friendly_memcmp
549#define strcmp CPL_afl_friendly_strcmp
550#define strncmp CPL_afl_friendly_strncmp
551#define strcasecmp CPL_afl_friendly_strcasecmp
552#define strncasecmp CPL_afl_friendly_strncasecmp
553#define strstr CPL_afl_friendly_strstr
554
555#endif /* defined(AFL_FRIENDLY) && defined(__GNUC__) */
556
557# if defined(WIN32)
558# define STRCASECMP(a,b) (stricmp(a,b))
559# define STRNCASECMP(a,b,n) (strnicmp(a,b,n))
560# else
562# define STRCASECMP(a,b) (strcasecmp(a,b))
564# define STRNCASECMP(a,b,n) (strncasecmp(a,b,n))
565# endif
567# define EQUALN(a,b,n) (STRNCASECMP(a,b,n)==0)
569# define EQUAL(a,b) (STRCASECMP(a,b)==0)
570#endif
571
572/*---------------------------------------------------------------------
573 * Does a string "a" start with string "b". Search is case-sensitive or,
574 * with CI, it is a case-insensitive comparison.
575 *--------------------------------------------------------------------- */
576#ifndef STARTS_WITH_CI
578#define STARTS_WITH(a,b) (strncmp(a,b,strlen(b)) == 0)
580#define STARTS_WITH_CI(a,b) EQUALN(a,b,strlen(b))
581#endif
582
584#ifndef CPL_THREADLOCAL
585# define CPL_THREADLOCAL
586#endif
589/* -------------------------------------------------------------------- */
590/* Handle isnan() and isinf(). Note that isinf() and isnan() */
591/* are supposed to be macros according to C99, defined in math.h */
592/* Some systems (i.e. Tru64) don't have isinf() at all, so if */
593/* the macro is not defined we just assume nothing is infinite. */
594/* This may mean we have no real CPLIsInf() on systems with isinf()*/
595/* function but no corresponding macro, but I can live with */
596/* that since it isn't that important a test. */
597/* -------------------------------------------------------------------- */
598#ifdef _MSC_VER
599# include <float.h>
600# define CPLIsNan(x) _isnan(x)
601# define CPLIsInf(x) (!_isnan(x) && !_finite(x))
602# define CPLIsFinite(x) _finite(x)
603#elif defined(__GNUC__) && ( __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 4 ) )
604/* When including <cmath> in C++11 the isnan() macro is undefined, so that */
605/* std::isnan() can work (#6489). This is a GCC specific workaround for now. */
606# define CPLIsNan(x) __builtin_isnan(x)
607# define CPLIsInf(x) __builtin_isinf(x)
608# define CPLIsFinite(x) __builtin_isfinite(x)
609#elif defined(__cplusplus) && defined(HAVE_STD_IS_NAN) && HAVE_STD_IS_NAN
610extern "C++" {
611#ifndef DOXYGEN_SKIP
612#include <cmath>
613#endif
614static inline int CPLIsNan(float f) { return std::isnan(f); }
615static inline int CPLIsNan(double f) { return std::isnan(f); }
616static inline int CPLIsInf(float f) { return std::isinf(f); }
617static inline int CPLIsInf(double f) { return std::isinf(f); }
618static inline int CPLIsFinite(float f) { return std::isfinite(f); }
619static inline int CPLIsFinite(double f) { return std::isfinite(f); }
620}
621#else
623#if defined(__cplusplus) && defined(__GNUC__) && defined(__linux) && !defined(__ANDROID__) && !defined(CPL_SUPRESS_CPLUSPLUS)
624/* so to not get warning about conversion from double to float with */
625/* gcc -Wfloat-conversion when using isnan()/isinf() macros */
626extern "C++" {
627static inline int CPLIsNan(float f) { return __isnanf(f); }
628static inline int CPLIsNan(double f) { return __isnan(f); }
629static inline int CPLIsInf(float f) { return __isinff(f); }
630static inline int CPLIsInf(double f) { return __isinf(f); }
631static inline int CPLIsFinite(float f) { return !__isnanf(f) && !__isinff(f); }
632static inline int CPLIsFinite(double f) { return !__isnan(f) && !__isinf(f); }
633}
634#else
635# define CPLIsNan(x) isnan(x)
636# if defined(isinf) || defined(__FreeBSD__)
638# define CPLIsInf(x) isinf(x)
640# define CPLIsFinite(x) (!isnan(x) && !isinf(x))
641# elif defined(__sun__)
642# include <ieeefp.h>
643# define CPLIsInf(x) (!finite(x) && !isnan(x))
644# define CPLIsFinite(x) finite(x)
645# else
646# define CPLIsInf(x) (0)
647# define CPLIsFinite(x) (!isnan(x))
648# endif
649#endif
650#endif
651
653/*---------------------------------------------------------------------
654 * CPL_LSB and CPL_MSB
655 * Only one of these 2 macros should be defined and specifies the byte
656 * ordering for the current platform.
657 * This should be defined in the Makefile, but if it is not then
658 * the default is CPL_LSB (Intel ordering, LSB first).
659 *--------------------------------------------------------------------*/
660#if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
661# define CPL_MSB
662#endif
663
664#if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
665#define CPL_LSB
666#endif
667
668#if defined(CPL_LSB)
669# define CPL_IS_LSB 1
670#else
671# define CPL_IS_LSB 0
672#endif
675#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
676
678extern "C++" {
679
680template <bool b> struct CPLStaticAssert {};
681template<> struct CPLStaticAssert<true>
682{
683 static void my_function() {}
684};
685
686} /* extern "C++" */
687
688#define CPL_STATIC_ASSERT(x) CPLStaticAssert<x>::my_function()
689#define CPL_STATIC_ASSERT_IF_AVAILABLE(x) CPL_STATIC_ASSERT(x)
690
691#else /* __cplusplus */
692
693#define CPL_STATIC_ASSERT_IF_AVAILABLE(x)
694
695#endif /* __cplusplus */
698/*---------------------------------------------------------------------
699 * Little endian <==> big endian byte swap macros.
700 *--------------------------------------------------------------------*/
701
703#define CPL_SWAP16(x) CPL_STATIC_CAST(GUInt16, (CPL_STATIC_CAST(GUInt16, x) << 8) | (CPL_STATIC_CAST(GUInt16, x) >> 8) )
704
705#if defined(HAVE_GCC_BSWAP) && (defined(__i386__) || defined(__x86_64__))
706/* Could potentially be extended to other architectures but must be checked */
707/* that the intrinsic is indeed efficient */
708/* GCC (at least 4.6 or above) need that include */
709#include <x86intrin.h>
711#define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, __builtin_bswap32(CPL_STATIC_CAST(GUInt32, x)))
713#define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, __builtin_bswap64(CPL_STATIC_CAST(GUInt64, x)))
714#elif defined(_MSC_VER)
715#define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, _byteswap_ulong(CPL_STATIC_CAST(GUInt32, x)))
716#define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, _byteswap_uint64(CPL_STATIC_CAST(GUInt64, x)))
717#else
719#define CPL_SWAP32(x) \
720 CPL_STATIC_CAST(GUInt32, \
721 ((CPL_STATIC_CAST(GUInt32, x) & 0x000000ffU) << 24) | \
722 ((CPL_STATIC_CAST(GUInt32, x) & 0x0000ff00U) << 8) | \
723 ((CPL_STATIC_CAST(GUInt32, x) & 0x00ff0000U) >> 8) | \
724 ((CPL_STATIC_CAST(GUInt32, x) & 0xff000000U) >> 24) )
725
727#define CPL_SWAP64(x) \
728 ((CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, x))) << 32) | \
729 (CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, CPL_STATIC_CAST(GUInt64, x) >> 32)))))
730
731#endif
732
734#define CPL_SWAP16PTR(x) \
735{ \
736 GByte byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x); \
737 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2); \
738 \
739 byTemp = _pabyDataT[0]; \
740 _pabyDataT[0] = _pabyDataT[1]; \
741 _pabyDataT[1] = byTemp; \
742}
743
744#if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
745
747#define CPL_SWAP32PTR(x) \
748{ \
749 GByte byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x); \
750 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4); \
751 \
752 byTemp = _pabyDataT[0]; \
753 _pabyDataT[0] = _pabyDataT[3]; \
754 _pabyDataT[3] = byTemp; \
755 byTemp = _pabyDataT[1]; \
756 _pabyDataT[1] = _pabyDataT[2]; \
757 _pabyDataT[2] = byTemp; \
758}
759
761#define CPL_SWAP64PTR(x) \
762{ \
763 GByte byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x); \
764 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
765 \
766 byTemp = _pabyDataT[0]; \
767 _pabyDataT[0] = _pabyDataT[7]; \
768 _pabyDataT[7] = byTemp; \
769 byTemp = _pabyDataT[1]; \
770 _pabyDataT[1] = _pabyDataT[6]; \
771 _pabyDataT[6] = byTemp; \
772 byTemp = _pabyDataT[2]; \
773 _pabyDataT[2] = _pabyDataT[5]; \
774 _pabyDataT[5] = byTemp; \
775 byTemp = _pabyDataT[3]; \
776 _pabyDataT[3] = _pabyDataT[4]; \
777 _pabyDataT[4] = byTemp; \
778}
779
780#else
781
783#define CPL_SWAP32PTR(x) \
784{ \
785 GUInt32 _n32; \
786 void* _lx = x; \
787 memcpy(&_n32, _lx, 4); \
788 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4); \
789 _n32 = CPL_SWAP32(_n32); \
790 memcpy(_lx, &_n32, 4); \
791}
792
794#define CPL_SWAP64PTR(x) \
795{ \
796 GUInt64 _n64; \
797 void* _lx = x; \
798 memcpy(&_n64, _lx, 8); \
799 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
800 _n64 = CPL_SWAP64(_n64); \
801 memcpy(_lx, &_n64, 8); \
802}
803
804#endif
805
807#define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
808
809#ifdef CPL_MSB
810# define CPL_MSBWORD16(x) (x)
811# define CPL_LSBWORD16(x) CPL_SWAP16(x)
812# define CPL_MSBWORD32(x) (x)
813# define CPL_LSBWORD32(x) CPL_SWAP32(x)
814# define CPL_MSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
815# define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
816# define CPL_MSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
817# define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
818# define CPL_MSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
819# define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
820#else
822# define CPL_LSBWORD16(x) (x)
824# define CPL_MSBWORD16(x) CPL_SWAP16(x)
826# define CPL_LSBWORD32(x) (x)
828# define CPL_MSBWORD32(x) CPL_SWAP32(x)
830# define CPL_LSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
832# define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
834# define CPL_LSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
836# define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
838# define CPL_LSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
840# define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
841#endif
842
846#define CPL_LSBINT16PTR(x) ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8))
847
851#define CPL_LSBINT32PTR(x) ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8) | \
852 (*((CPL_REINTERPRET_CAST(const GByte*, x))+2) << 16) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+3) << 24))
853
855#define CPL_LSBSINT16PTR(x) CPL_STATIC_CAST(GInt16,CPL_LSBINT16PTR(x))
856
858#define CPL_LSBUINT16PTR(x) CPL_STATIC_CAST(GUInt16, CPL_LSBINT16PTR(x))
859
861#define CPL_LSBSINT32PTR(x) CPL_STATIC_CAST(GInt32, CPL_LSBINT32PTR(x))
862
864#define CPL_LSBUINT32PTR(x) CPL_STATIC_CAST(GUInt32, CPL_LSBINT32PTR(x))
865
867/* Utility macro to explicitly mark intentionally unreferenced parameters. */
868#ifndef UNREFERENCED_PARAM
869# ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
870# define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
871# else
872# define UNREFERENCED_PARAM(param) ((void)param)
873# endif /* UNREFERENCED_PARAMETER */
874#endif /* UNREFERENCED_PARAM */
877/***********************************************************************
878 * Define CPL_CVSID() macro. It can be disabled during a build by
879 * defining DISABLE_CVSID in the compiler options.
880 *
881 * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
882 * being unused.
883 */
884
886#ifndef DISABLE_CVSID
887#if defined(__GNUC__) && __GNUC__ >= 4
888# define CPL_CVSID(string) static const char cpl_cvsid[] __attribute__((used)) = string;
889#else
890# define CPL_CVSID(string) static const char cpl_cvsid[] = string; \
891static const char *cvsid_aw() { return( cvsid_aw() ? NULL : cpl_cvsid ); }
892#endif
893#else
894# define CPL_CVSID(string)
895#endif
898/* We exclude mingw64 4.6 which seems to be broken regarding this */
899#if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP) && !(defined(__MINGW64__) && __GNUC__ == 4 && __GNUC_MINOR__ == 6)
901# define CPL_NULL_TERMINATED __attribute__((__sentinel__))
902#else
904# define CPL_NULL_TERMINATED
905#endif
906
907#if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
909#define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
911#define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
912#else
914#define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
916#define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx )
917#endif
918
919#if defined(_MSC_VER) && (defined(GDAL_COMPILATION) || defined(CPL_ENABLE_MSVC_ANNOTATIONS))
920#include <sal.h>
923# define CPL_FORMAT_STRING(arg) _Printf_format_string_ arg
926# define CPL_SCANF_FORMAT_STRING(arg) _Scanf_format_string_ arg
927#else
929# define CPL_FORMAT_STRING(arg) arg
931# define CPL_SCANF_FORMAT_STRING(arg) arg
932#endif /* defined(_MSC_VER) && defined(GDAL_COMPILATION) */
933
934#if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
936#define CPL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
937#else
939#define CPL_WARN_UNUSED_RESULT
940#endif
941
942#if defined(__GNUC__) && __GNUC__ >= 4
944# define CPL_UNUSED __attribute((__unused__))
945#else
946/* TODO: add cases for other compilers */
948# define CPL_UNUSED
949#endif
950
951#if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
953#define CPL_NO_RETURN __attribute__((noreturn))
954#else
956#define CPL_NO_RETURN
957#endif
958
960/* Clang __has_attribute */
961#ifndef __has_attribute
962 #define __has_attribute(x) 0 // Compatibility with non-clang compilers.
963#endif
964
967#if ((defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))) || __has_attribute(returns_nonnull)) && !defined(DOXYGEN_SKIP) && !defined(__INTEL_COMPILER)
969# define CPL_RETURNS_NONNULL __attribute__((returns_nonnull))
970#else
972# define CPL_RETURNS_NONNULL
973#endif
974
975#if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
977#define CPL_RESTRICT __restrict__
978#else
980#define CPL_RESTRICT
981#endif
982
983#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
984
987# define CPL_OVERRIDE override
988
990# define CPL_FINAL final
991
997# define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
998 ClassName( const ClassName & ) = delete; \
999 ClassName &operator=( const ClassName & ) = delete;
1000
1001#endif /* __cplusplus */
1002
1003#if !defined(DOXYGEN_SKIP)
1004#if defined(__has_extension)
1005 #if __has_extension(attribute_deprecated_with_message)
1006 /* Clang extension */
1007 #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated(x)))
1008 #else
1009 #define CPL_WARN_DEPRECATED(x)
1010 #endif
1011#elif defined(__GNUC__)
1012 #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated))
1013#else
1014 #define CPL_WARN_DEPRECATED(x)
1015#endif
1016#endif
1017
1018#if !defined(_MSC_VER) && !defined(__APPLE__) && !defined(_FORTIFY_SOURCE)
1020# if defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF)
1021int vsnprintf(char *str, size_t size, const char* fmt, va_list args)
1022 CPL_WARN_DEPRECATED("Use CPLvsnprintf() instead");
1023int snprintf(char *str, size_t size, const char* fmt, ...)
1025 CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
1026int sprintf(char *str, const char* fmt, ...)
1028 CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
1029# elif defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
1030int sprintf(char *str, const char* fmt, ...)
1032 CPL_WARN_DEPRECATED("Use snprintf() or CPLsnprintf() instead");
1033# endif /* defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF) */
1035#endif /* !defined(_MSC_VER) && !defined(__APPLE__) */
1036
1037#if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
1039#define CPL_CPU_REQUIRES_ALIGNED_ACCESS
1041#endif
1042
1043#if defined(__cplusplus)
1045#define CPL_ARRAYSIZE(array) \
1046 ((sizeof(array) / sizeof(*(array))) / \
1047 static_cast<size_t>(!(sizeof(array) % sizeof(*(array)))))
1048
1049extern "C++" {
1050template<class T> static void CPL_IGNORE_RET_VAL(T) {}
1051inline static bool CPL_TO_BOOL(int x) { return x != 0; }
1052} /* extern "C++" */
1053
1054#endif /* __cplusplus */
1055
1056#if (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || (defined(__clang__) && __clang_major__ >= 3)) && !defined(_MSC_VER))
1057#define HAVE_GCC_DIAGNOSTIC_PUSH
1058#endif
1059
1060#if ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) && !defined(_MSC_VER))
1061#define HAVE_GCC_SYSTEM_HEADER
1062#endif
1063
1064#if ((defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7))) || __GNUC__ >= 7)
1066# define CPL_FALLTHROUGH [[clang::fallthrough]];
1067#else
1069# define CPL_FALLTHROUGH
1070#endif
1071
1073// Define DEBUG_BOOL to compile in "MSVC mode", ie error out when
1074// a integer is assigned to a bool
1075// WARNING: use only at compilation time, since it is know to not work
1076// at runtime for unknown reasons (crash in MongoDB driver for example)
1077#if defined(__cplusplus) && defined(DEBUG_BOOL) && !defined(DO_NOT_USE_DEBUG_BOOL) && !defined(CPL_SUPRESS_CPLUSPLUS)
1078extern "C++" {
1079class MSVCPedanticBool
1080{
1081
1082 friend bool operator== (const bool& one, const MSVCPedanticBool& other);
1083 friend bool operator!= (const bool& one, const MSVCPedanticBool& other);
1084
1085 bool b;
1086 MSVCPedanticBool(int bIn);
1087
1088 public:
1089 /* b not initialized on purpose in default ctor to flag use. */
1090 /* cppcheck-suppress uninitMemberVar */
1091 MSVCPedanticBool() {}
1092 MSVCPedanticBool(bool bIn) : b(bIn) {}
1093 MSVCPedanticBool(const MSVCPedanticBool& other) : b(other.b) {}
1094
1095 MSVCPedanticBool& operator= (const MSVCPedanticBool& other) { b = other.b; return *this; }
1096 MSVCPedanticBool& operator&= (const MSVCPedanticBool& other) { b &= other.b; return *this; }
1097 MSVCPedanticBool& operator|= (const MSVCPedanticBool& other) { b |= other.b; return *this; }
1098
1099 bool operator== (const bool& other) const { return b == other; }
1100 bool operator!= (const bool& other) const { return b != other; }
1101 bool operator< (const bool& other) const { return b < other; }
1102 bool operator== (const MSVCPedanticBool& other) const { return b == other.b; }
1103 bool operator!= (const MSVCPedanticBool& other) const { return b != other.b; }
1104 bool operator< (const MSVCPedanticBool& other) const { return b < other.b; }
1105
1106 bool operator! () const { return !b; }
1107 operator bool() const { return b; }
1108 operator int() const { return b; }
1109 operator GIntBig() const { return b; }
1110};
1111
1112inline bool operator== (const bool& one, const MSVCPedanticBool& other) { return one == other.b; }
1113inline bool operator!= (const bool& one, const MSVCPedanticBool& other) { return one != other.b; }
1114
1115/* We must include all C++ stuff before to avoid issues with templates that use bool */
1116#include <vector>
1117#include <map>
1118#include <set>
1119#include <string>
1120#include <cstddef>
1121#include <limits>
1122#include <sstream>
1123#include <fstream>
1124#include <algorithm>
1125#include <functional>
1126#include <memory>
1127#include <queue>
1128#include <mutex>
1129#include <unordered_map>
1130#include <thread>
1131#include <unordered_set>
1132#include <complex>
1133#include <iomanip>
1134
1135} /* extern C++ */
1136
1137#undef FALSE
1138#define FALSE false
1139#undef TRUE
1140#define TRUE true
1141
1142/* In the very few cases we really need a "simple" type, fallback to bool */
1143#define EMULATED_BOOL int
1144
1145/* Use our class instead of bool */
1146#define bool MSVCPedanticBool
1147
1148/* "volatile bool" with the below substitution doesn't really work. */
1149/* Just for the sake of the debug, we don't really need volatile */
1150#define VOLATILE_BOOL bool
1151
1152#else /* defined(__cplusplus) && defined(DEBUG_BOOL) */
1153
1154#ifndef FALSE
1155# define FALSE 0
1156#endif
1157
1158#ifndef TRUE
1159# define TRUE 1
1160#endif
1161
1162#define EMULATED_BOOL bool
1163#define VOLATILE_BOOL volatile bool
1164
1165#endif /* defined(__cplusplus) && defined(DEBUG_BOOL) */
1166
1167#if __clang_major__ >= 4 || (__clang_major__ == 3 && __clang_minor__ >= 8)
1168#define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW __attribute__((no_sanitize("unsigned-integer-overflow")))
1169#else
1170#define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
1171#endif
1175#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
1176#define CPL_NULLPTR nullptr
1177#else
1178#define CPL_NULLPTR NULL
1179#endif
1182/* This typedef is for C functions that take char** as argument, but */
1183/* with the semantics of a const list. In C, char** is not implicitly cast to */
1184/* const char* const*, contrary to C++. So when seen for C++, it is OK */
1185/* to expose the prototyes as const char* const*, but for C we keep the */
1186/* historical definition to avoid warnings. */
1187#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && !defined(DOXYGEN_SKIP)
1190typedef const char* const* CSLConstList;
1191#else
1194typedef char** CSLConstList;
1195#endif
1196
1197#endif /* ndef CPL_BASE_H_INCLUDED */
unsigned long long GUIntBig
Large unsigned integer type (generally 64-bit unsigned integer type).
Definition: cpl_port.h:251
short GInt16
Int16 type.
Definition: cpl_port.h:211
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:339
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:337
GIntBig GInt64
Signed 64 bit integer type.
Definition: cpl_port.h:267
int GBool
Type for boolean values (alias to int)
Definition: cpl_port.h:223
unsigned int GUInt32
Unsigned int32 type.
Definition: cpl_port.h:207
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition: cpl_port.h:914
GIntBig GPtrDiff_t
Integer type large enough to hold the difference between 2 addresses.
Definition: cpl_port.h:286
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1194
GUIntBig GUInt64
Unsigned 64 bit integer type.
Definition: cpl_port.h:269
unsigned short GUInt16
Unsigned int16 type.
Definition: cpl_port.h:213
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:215
int GInt32
Int32 type.
Definition: cpl_port.h:205
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition: cpl_port.h:248
int CPLsnprintf(char *str, size_t size, const char *fmt,...)
snprintf() wrapper that is not sensitive to LC_NUMERIC settings.
Definition: cpl_string.cpp:1337

Generated for GDAL by doxygen 1.9.4.