OGR
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 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
190# define HAVE_CXX14 1
191# endif
192/* TODO(schwehr): What is the correct test for C++ 17? */
193#endif /* __cplusplus */
194
195/*---------------------------------------------------------------------
196 * types for 16 and 32 bits integers, etc...
197 *--------------------------------------------------------------------*/
198#if UINT_MAX == 65535
199typedef long GInt32;
200typedef unsigned long GUInt32;
201#else
203typedef int GInt32;
205typedef unsigned int GUInt32;
206#endif
207
209typedef short GInt16;
211typedef unsigned short GUInt16;
213typedef unsigned char GByte;
214/* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef bool GBool" */
215/* in include/poppler/goo/gtypes.h */
216#ifndef CPL_GBOOL_DEFINED
218#define CPL_GBOOL_DEFINED
221typedef int GBool;
222#endif
223
225#ifdef __cplusplus
226#define CPL_STATIC_CAST(type, expr) static_cast<type>(expr)
227#define CPL_REINTERPRET_CAST(type, expr) reinterpret_cast<type>(expr)
228#else
229#define CPL_STATIC_CAST(type, expr) ((type)(expr))
230#define CPL_REINTERPRET_CAST(type, expr) ((type)(expr))
231#endif
234/* -------------------------------------------------------------------- */
235/* 64bit support */
236/* -------------------------------------------------------------------- */
237
238#if defined(WIN32) && defined(_MSC_VER)
239#define VSI_LARGE_API_SUPPORTED
240#endif
241
242#if HAVE_LONG_LONG
243
246typedef long long GIntBig;
249typedef unsigned long long GUIntBig;
250
252#define GINTBIG_MIN (CPL_STATIC_CAST(GIntBig, 0x80000000) << 32)
254#define GINTBIG_MAX ((CPL_STATIC_CAST(GIntBig, 0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
256#define GUINTBIG_MAX ((CPL_STATIC_CAST(GUIntBig, 0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
257
259#define CPL_HAS_GINT64 1
262/* Note: we might want to use instead int64_t / uint64_t if they are available */
263
268
270#define GINT64_MIN GINTBIG_MIN
272#define GINT64_MAX GINTBIG_MAX
274#define GUINT64_MAX GUINTBIG_MAX
275
276#else
277
278#error "64bit integer support required"
279
280#endif
281
282#if SIZEOF_VOIDP == 8
285#else
287typedef int GPtrDiff_t;
288#endif
289
290#ifdef GDAL_COMPILATION
291#if HAVE_UINTPTR_T
292#include <stdint.h>
293typedef uintptr_t GUIntptr_t;
294#elif SIZEOF_VOIDP == 8
295typedef GUIntBig GUIntptr_t;
296#else
297typedef unsigned int GUIntptr_t;
298#endif
299
300#define CPL_IS_ALIGNED(ptr, quant) ((CPL_REINTERPRET_CAST(GUIntptr_t, CPL_STATIC_CAST(const void*, ptr)) % (quant)) == 0)
301
302#endif
303
304#if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
305 #define CPL_FRMT_GB_WITHOUT_PREFIX "I64"
306#elif HAVE_LONG_LONG
308 #define CPL_FRMT_GB_WITHOUT_PREFIX "ll"
309#else
310 #define CPL_FRMT_GB_WITHOUT_PREFIX "l"
311#endif
312
314#define CPL_FRMT_GIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
316#define CPL_FRMT_GUIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
317
319#define GUINTBIG_TO_DOUBLE(x) CPL_STATIC_CAST(double, x)
323#ifdef COMPAT_WITH_ICC_CONVERSION_CHECK
324#define CPL_INT64_FITS_ON_INT32(x) ((x) >= INT_MIN && (x) <= INT_MAX)
325#else
326#define CPL_INT64_FITS_ON_INT32(x) (CPL_STATIC_CAST(GIntBig, CPL_STATIC_CAST(int, x)) == (x))
327#endif
330/* ==================================================================== */
331/* Other standard services. */
332/* ==================================================================== */
333#ifdef __cplusplus
335# define CPL_C_START extern "C" {
337# define CPL_C_END }
338#else
339# define CPL_C_START
340# define CPL_C_END
341#endif
342
343#ifndef CPL_DLL
344#if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
345# define CPL_DLL __declspec(dllexport)
346#else
347# if defined(USE_GCC_VISIBILITY_FLAG)
348# define CPL_DLL __attribute__ ((visibility("default")))
349# else
350# define CPL_DLL
351# endif
352#endif
353#endif
354
356/* Should optional (normally private) interfaces be exported? */
357#ifdef CPL_OPTIONAL_APIS
358# define CPL_ODLL CPL_DLL
359#else
360# define CPL_ODLL
361#endif
364#ifndef CPL_STDCALL
365#if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
366# define CPL_STDCALL __stdcall
367#else
368# define CPL_STDCALL
369#endif
370#endif
371
373#ifdef _MSC_VER
374# define FORCE_CDECL __cdecl
375#else
376# define FORCE_CDECL
377#endif
381/* TODO : support for other compilers needed */
382#if (defined(__GNUC__) && !defined(__NO_INLINE__)) || defined(_MSC_VER)
383#define HAS_CPL_INLINE 1
384#define CPL_INLINE __inline
385#elif defined(__SUNPRO_CC)
386#define HAS_CPL_INLINE 1
387#define CPL_INLINE inline
388#else
389#define CPL_INLINE
390#endif
393#ifndef MAX
395# define MIN(a,b) (((a)<(b)) ? (a) : (b))
397# define MAX(a,b) (((a)>(b)) ? (a) : (b))
398#endif
399
400#ifndef ABS
402# define ABS(x) (((x)<0) ? (-1*(x)) : (x))
403#endif
404
405#ifndef M_PI
407# define M_PI 3.14159265358979323846
408/* 3.1415926535897932384626433832795 */
409#endif
410
411/* -------------------------------------------------------------------- */
412/* Macro to test equality of two floating point values. */
413/* We use fabs() function instead of ABS() macro to avoid side */
414/* effects. */
415/* -------------------------------------------------------------------- */
417#ifndef CPLIsEqual
418# define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
419#endif
422/* -------------------------------------------------------------------- */
423/* Provide macros for case insensitive string comparisons. */
424/* -------------------------------------------------------------------- */
425#ifndef EQUAL
426
427#if defined(AFL_FRIENDLY) && defined(__GNUC__)
428
429static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
430 __attribute__((always_inline));
431
432static inline int CPL_afl_friendly_memcmp(const void* ptr1, const void* ptr2, size_t len)
433{
434 const unsigned char* bptr1 = (const unsigned char*)ptr1;
435 const unsigned char* bptr2 = (const unsigned char*)ptr2;
436 while( len-- )
437 {
438 unsigned char b1 = *(bptr1++);
439 unsigned char b2 = *(bptr2++);
440 if( b1 != b2 ) return b1 - b2;
441 }
442 return 0;
443}
444
445static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
446 __attribute__((always_inline));
447
448static inline int CPL_afl_friendly_strcmp(const char* ptr1, const char* ptr2)
449{
450 const unsigned char* usptr1 = (const unsigned char*)ptr1;
451 const unsigned char* usptr2 = (const unsigned char*)ptr2;
452 while( 1 )
453 {
454 unsigned char ch1 = *(usptr1++);
455 unsigned char ch2 = *(usptr2++);
456 if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
457 }
458}
459
460static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
461 __attribute__((always_inline));
462
463static inline int CPL_afl_friendly_strncmp(const char* ptr1, const char* ptr2, size_t len)
464{
465 const unsigned char* usptr1 = (const unsigned char*)ptr1;
466 const unsigned char* usptr2 = (const unsigned char*)ptr2;
467 while( len -- )
468 {
469 unsigned char ch1 = *(usptr1++);
470 unsigned char ch2 = *(usptr2++);
471 if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
472 }
473 return 0;
474}
475
476static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
477 __attribute__((always_inline));
478
479static inline int CPL_afl_friendly_strcasecmp(const char* ptr1, const char* ptr2)
480{
481 const unsigned char* usptr1 = (const unsigned char*)ptr1;
482 const unsigned char* usptr2 = (const unsigned char*)ptr2;
483 while( 1 )
484 {
485 unsigned char ch1 = *(usptr1++);
486 unsigned char ch2 = *(usptr2++);
487 ch1 = (unsigned char)toupper(ch1);
488 ch2 = (unsigned char)toupper(ch2);
489 if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
490 }
491}
492
493static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
494 __attribute__((always_inline));
495
496static inline int CPL_afl_friendly_strncasecmp(const char* ptr1, const char* ptr2, size_t len)
497{
498 const unsigned char* usptr1 = (const unsigned char*)ptr1;
499 const unsigned char* usptr2 = (const unsigned char*)ptr2;
500 while( len-- )
501 {
502 unsigned char ch1 = *(usptr1++);
503 unsigned char ch2 = *(usptr2++);
504 ch1 = (unsigned char)toupper(ch1);
505 ch2 = (unsigned char)toupper(ch2);
506 if( ch1 == 0 || ch1 != ch2 ) return ch1 - ch2;
507 }
508 return 0;
509}
510
511static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
512 __attribute__((always_inline));
513
514static inline char* CPL_afl_friendly_strstr(const char* haystack, const char* needle)
515{
516 const char* ptr_haystack = haystack;
517 while( 1 )
518 {
519 const char* ptr_haystack2 = ptr_haystack;
520 const char* ptr_needle = needle;
521 while( 1 )
522 {
523 char ch1 = *(ptr_haystack2++);
524 char ch2 = *(ptr_needle++);
525 if( ch2 == 0 )
526 return (char*)ptr_haystack;
527 if( ch1 != ch2 )
528 break;
529 }
530 if( *ptr_haystack == 0 )
531 return NULL;
532 ptr_haystack ++;
533 }
534}
535
536#undef strcmp
537#undef strncmp
538#define memcmp CPL_afl_friendly_memcmp
539#define strcmp CPL_afl_friendly_strcmp
540#define strncmp CPL_afl_friendly_strncmp
541#define strcasecmp CPL_afl_friendly_strcasecmp
542#define strncasecmp CPL_afl_friendly_strncasecmp
543#define strstr CPL_afl_friendly_strstr
544
545#endif /* defined(AFL_FRIENDLY) && defined(__GNUC__) */
546
547# if defined(WIN32)
548# define STRCASECMP(a,b) (stricmp(a,b))
549# define STRNCASECMP(a,b,n) (strnicmp(a,b,n))
550# else
552# define STRCASECMP(a,b) (strcasecmp(a,b))
554# define STRNCASECMP(a,b,n) (strncasecmp(a,b,n))
555# endif
557# define EQUALN(a,b,n) (STRNCASECMP(a,b,n)==0)
559# define EQUAL(a,b) (STRCASECMP(a,b)==0)
560#endif
561
562/*---------------------------------------------------------------------
563 * Does a string "a" start with string "b". Search is case-sensitive or,
564 * with CI, it is a case-insensitive comparison.
565 *--------------------------------------------------------------------- */
566#ifndef STARTS_WITH_CI
568#define STARTS_WITH(a,b) (strncmp(a,b,strlen(b)) == 0)
570#define STARTS_WITH_CI(a,b) EQUALN(a,b,strlen(b))
571#endif
572
574#ifndef CPL_THREADLOCAL
575# define CPL_THREADLOCAL
576#endif
579/* -------------------------------------------------------------------- */
580/* Handle isnan() and isinf(). Note that isinf() and isnan() */
581/* are supposed to be macros according to C99, defined in math.h */
582/* Some systems (i.e. Tru64) don't have isinf() at all, so if */
583/* the macro is not defined we just assume nothing is infinite. */
584/* This may mean we have no real CPLIsInf() on systems with isinf()*/
585/* function but no corresponding macro, but I can live with */
586/* that since it isn't that important a test. */
587/* -------------------------------------------------------------------- */
588#ifdef _MSC_VER
589# include <float.h>
590# define CPLIsNan(x) _isnan(x)
591# define CPLIsInf(x) (!_isnan(x) && !_finite(x))
592# define CPLIsFinite(x) _finite(x)
593#elif defined(__GNUC__) && ( __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 4 ) )
594/* When including <cmath> in C++11 the isnan() macro is undefined, so that */
595/* std::isnan() can work (#6489). This is a GCC specific workaround for now. */
596# define CPLIsNan(x) __builtin_isnan(x)
597# define CPLIsInf(x) __builtin_isinf(x)
598# define CPLIsFinite(x) __builtin_isfinite(x)
599#elif defined(__cplusplus) && defined(HAVE_STD_IS_NAN) && HAVE_STD_IS_NAN
600extern "C++" {
601#ifndef DOXYGEN_SKIP
602#include <cmath>
603#endif
604static inline int CPLIsNan(float f) { return std::isnan(f); }
605static inline int CPLIsNan(double f) { return std::isnan(f); }
606static inline int CPLIsInf(float f) { return std::isinf(f); }
607static inline int CPLIsInf(double f) { return std::isinf(f); }
608static inline int CPLIsFinite(float f) { return std::isfinite(f); }
609static inline int CPLIsFinite(double f) { return std::isfinite(f); }
610}
611#else
613#if defined(__cplusplus) && defined(__GNUC__) && defined(__linux) && !defined(__ANDROID__) && !defined(CPL_SUPRESS_CPLUSPLUS)
614/* so to not get warning about conversion from double to float with */
615/* gcc -Wfloat-conversion when using isnan()/isinf() macros */
616extern "C++" {
617static inline int CPLIsNan(float f) { return __isnanf(f); }
618static inline int CPLIsNan(double f) { return __isnan(f); }
619static inline int CPLIsInf(float f) { return __isinff(f); }
620static inline int CPLIsInf(double f) { return __isinf(f); }
621static inline int CPLIsFinite(float f) { return !__isnanf(f) && !__isinff(f); }
622static inline int CPLIsFinite(double f) { return !__isnan(f) && !__isinf(f); }
623}
624#else
625# define CPLIsNan(x) isnan(x)
626# if defined(isinf) || defined(__FreeBSD__)
628# define CPLIsInf(x) isinf(x)
630# define CPLIsFinite(x) (!isnan(x) && !isinf(x))
631# elif defined(__sun__)
632# include <ieeefp.h>
633# define CPLIsInf(x) (!finite(x) && !isnan(x))
634# define CPLIsFinite(x) finite(x)
635# else
636# define CPLIsInf(x) (0)
637# define CPLIsFinite(x) (!isnan(x))
638# endif
639#endif
640#endif
641
643/*---------------------------------------------------------------------
644 * CPL_LSB and CPL_MSB
645 * Only one of these 2 macros should be defined and specifies the byte
646 * ordering for the current platform.
647 * This should be defined in the Makefile, but if it is not then
648 * the default is CPL_LSB (Intel ordering, LSB first).
649 *--------------------------------------------------------------------*/
650#if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
651# define CPL_MSB
652#endif
653
654#if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
655#define CPL_LSB
656#endif
657
658#if defined(CPL_LSB)
659# define CPL_IS_LSB 1
660#else
661# define CPL_IS_LSB 0
662#endif
665#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
666
668extern "C++" {
669
670template <bool b> struct CPLStaticAssert {};
671template<> struct CPLStaticAssert<true>
672{
673 static void my_function() {}
674};
675
676} /* extern "C++" */
677
678#define CPL_STATIC_ASSERT(x) CPLStaticAssert<x>::my_function()
679#define CPL_STATIC_ASSERT_IF_AVAILABLE(x) CPL_STATIC_ASSERT(x)
680
681#else /* __cplusplus */
682
683#define CPL_STATIC_ASSERT_IF_AVAILABLE(x)
684
685#endif /* __cplusplus */
688/*---------------------------------------------------------------------
689 * Little endian <==> big endian byte swap macros.
690 *--------------------------------------------------------------------*/
691
693#define CPL_SWAP16(x) CPL_STATIC_CAST(GUInt16, (CPL_STATIC_CAST(GUInt16, x) << 8) | (CPL_STATIC_CAST(GUInt16, x) >> 8) )
694
695#if defined(HAVE_GCC_BSWAP) && (defined(__i386__) || defined(__x86_64__))
696/* Could potentially be extended to other architectures but must be checked */
697/* that the intrinsic is indeed efficient */
698/* GCC (at least 4.6 or above) need that include */
699#include <x86intrin.h>
701#define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, __builtin_bswap32(CPL_STATIC_CAST(GUInt32, x)))
703#define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, __builtin_bswap64(CPL_STATIC_CAST(GUInt64, x)))
704#elif defined(_MSC_VER)
705#define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, _byteswap_ulong(CPL_STATIC_CAST(GUInt32, x)))
706#define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, _byteswap_uint64(CPL_STATIC_CAST(GUInt64, x)))
707#else
709#define CPL_SWAP32(x) \
710 CPL_STATIC_CAST(GUInt32, \
711 ((CPL_STATIC_CAST(GUInt32, x) & 0x000000ffU) << 24) | \
712 ((CPL_STATIC_CAST(GUInt32, x) & 0x0000ff00U) << 8) | \
713 ((CPL_STATIC_CAST(GUInt32, x) & 0x00ff0000U) >> 8) | \
714 ((CPL_STATIC_CAST(GUInt32, x) & 0xff000000U) >> 24) )
715
717#define CPL_SWAP64(x) \
718 ((CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, x))) << 32) | \
719 (CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, CPL_STATIC_CAST(GUInt64, x) >> 32)))))
720
721#endif
722
724#define CPL_SWAP16PTR(x) \
725{ \
726 GByte byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x); \
727 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2); \
728 \
729 byTemp = _pabyDataT[0]; \
730 _pabyDataT[0] = _pabyDataT[1]; \
731 _pabyDataT[1] = byTemp; \
732}
733
734#if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
735
737#define CPL_SWAP32PTR(x) \
738{ \
739 GByte byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x); \
740 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4); \
741 \
742 byTemp = _pabyDataT[0]; \
743 _pabyDataT[0] = _pabyDataT[3]; \
744 _pabyDataT[3] = byTemp; \
745 byTemp = _pabyDataT[1]; \
746 _pabyDataT[1] = _pabyDataT[2]; \
747 _pabyDataT[2] = byTemp; \
748}
749
751#define CPL_SWAP64PTR(x) \
752{ \
753 GByte byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x); \
754 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
755 \
756 byTemp = _pabyDataT[0]; \
757 _pabyDataT[0] = _pabyDataT[7]; \
758 _pabyDataT[7] = byTemp; \
759 byTemp = _pabyDataT[1]; \
760 _pabyDataT[1] = _pabyDataT[6]; \
761 _pabyDataT[6] = byTemp; \
762 byTemp = _pabyDataT[2]; \
763 _pabyDataT[2] = _pabyDataT[5]; \
764 _pabyDataT[5] = byTemp; \
765 byTemp = _pabyDataT[3]; \
766 _pabyDataT[3] = _pabyDataT[4]; \
767 _pabyDataT[4] = byTemp; \
768}
769
770#else
771
773#define CPL_SWAP32PTR(x) \
774{ \
775 GUInt32 _n32; \
776 void* _lx = x; \
777 memcpy(&_n32, _lx, 4); \
778 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4); \
779 _n32 = CPL_SWAP32(_n32); \
780 memcpy(_lx, &_n32, 4); \
781}
782
784#define CPL_SWAP64PTR(x) \
785{ \
786 GUInt64 _n64; \
787 void* _lx = x; \
788 memcpy(&_n64, _lx, 8); \
789 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
790 _n64 = CPL_SWAP64(_n64); \
791 memcpy(_lx, &_n64, 8); \
792}
793
794#endif
795
797#define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
798
799#ifdef CPL_MSB
800# define CPL_MSBWORD16(x) (x)
801# define CPL_LSBWORD16(x) CPL_SWAP16(x)
802# define CPL_MSBWORD32(x) (x)
803# define CPL_LSBWORD32(x) CPL_SWAP32(x)
804# define CPL_MSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
805# define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
806# define CPL_MSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
807# define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
808# define CPL_MSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
809# define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
810#else
812# define CPL_LSBWORD16(x) (x)
814# define CPL_MSBWORD16(x) CPL_SWAP16(x)
816# define CPL_LSBWORD32(x) (x)
818# define CPL_MSBWORD32(x) CPL_SWAP32(x)
820# define CPL_LSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
822# define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
824# define CPL_LSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
826# define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
828# define CPL_LSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
830# define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
831#endif
832
836#define CPL_LSBINT16PTR(x) ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8))
837
841#define CPL_LSBINT32PTR(x) ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8) | \
842 (*((CPL_REINTERPRET_CAST(const GByte*, x))+2) << 16) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+3) << 24))
843
845#define CPL_LSBSINT16PTR(x) CPL_STATIC_CAST(GInt16,CPL_LSBINT16PTR(x))
846
848#define CPL_LSBUINT16PTR(x) CPL_STATIC_CAST(GUInt16, CPL_LSBINT16PTR(x))
849
851#define CPL_LSBSINT32PTR(x) CPL_STATIC_CAST(GInt32, CPL_LSBINT32PTR(x))
852
854#define CPL_LSBUINT32PTR(x) CPL_STATIC_CAST(GUInt32, CPL_LSBINT32PTR(x))
855
857/* Utility macro to explicitly mark intentionally unreferenced parameters. */
858#ifndef UNREFERENCED_PARAM
859# ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
860# define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
861# else
862# define UNREFERENCED_PARAM(param) ((void)param)
863# endif /* UNREFERENCED_PARAMETER */
864#endif /* UNREFERENCED_PARAM */
867/***********************************************************************
868 * Define CPL_CVSID() macro. It can be disabled during a build by
869 * defining DISABLE_CVSID in the compiler options.
870 *
871 * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
872 * being unused.
873 */
874
876#ifndef DISABLE_CVSID
877#if defined(__GNUC__) && __GNUC__ >= 4
878# define CPL_CVSID(string) static const char cpl_cvsid[] __attribute__((used)) = string;
879#else
880# define CPL_CVSID(string) static const char cpl_cvsid[] = string; \
881static const char *cvsid_aw() { return( cvsid_aw() ? NULL : cpl_cvsid ); }
882#endif
883#else
884# define CPL_CVSID(string)
885#endif
888/* We exclude mingw64 4.6 which seems to be broken regarding this */
889#if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP) && !(defined(__MINGW64__) && __GNUC__ == 4 && __GNUC_MINOR__ == 6)
891# define CPL_NULL_TERMINATED __attribute__((__sentinel__))
892#else
894# define CPL_NULL_TERMINATED
895#endif
896
897#if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
899#define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
901#define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
902#else
904#define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
906#define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx )
907#endif
908
909#if defined(_MSC_VER) && (defined(GDAL_COMPILATION) || defined(CPL_ENABLE_MSVC_ANNOTATIONS))
910#include <sal.h>
913# define CPL_FORMAT_STRING(arg) _Printf_format_string_ arg
916# define CPL_SCANF_FORMAT_STRING(arg) _Scanf_format_string_ arg
917#else
919# define CPL_FORMAT_STRING(arg) arg
921# define CPL_SCANF_FORMAT_STRING(arg) arg
922#endif /* defined(_MSC_VER) && defined(GDAL_COMPILATION) */
923
924#if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
926#define CPL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
927#else
929#define CPL_WARN_UNUSED_RESULT
930#endif
931
932#if defined(__GNUC__) && __GNUC__ >= 4
934# define CPL_UNUSED __attribute((__unused__))
935#else
936/* TODO: add cases for other compilers */
938# define CPL_UNUSED
939#endif
940
941#if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
943#define CPL_NO_RETURN __attribute__((noreturn))
944#else
946#define CPL_NO_RETURN
947#endif
948
950/* Clang __has_attribute */
951#ifndef __has_attribute
952 #define __has_attribute(x) 0 // Compatibility with non-clang compilers.
953#endif
954
957#if ((defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))) || __has_attribute(returns_nonnull)) && !defined(DOXYGEN_SKIP) && !defined(__INTEL_COMPILER)
959# define CPL_RETURNS_NONNULL __attribute__((returns_nonnull))
960#else
962# define CPL_RETURNS_NONNULL
963#endif
964
965#if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
967#define CPL_RESTRICT __restrict__
968#else
970#define CPL_RESTRICT
971#endif
972
973#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
974
977# define CPL_OVERRIDE override
978
980# define CPL_FINAL final
981
987# define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
988 ClassName( const ClassName & ) = delete; \
989 ClassName &operator=( const ClassName & ) = delete;
990
991#endif /* __cplusplus */
992
993#if !defined(DOXYGEN_SKIP)
994#if defined(__has_extension)
995 #if __has_extension(attribute_deprecated_with_message)
996 /* Clang extension */
997 #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated(x)))
998 #else
999 #define CPL_WARN_DEPRECATED(x)
1000 #endif
1001#elif defined(__GNUC__)
1002 #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated))
1003#else
1004 #define CPL_WARN_DEPRECATED(x)
1005#endif
1006#endif
1007
1008#if !defined(_MSC_VER) && !defined(__APPLE__) && !defined(_FORTIFY_SOURCE)
1010# if defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF)
1011int vsnprintf(char *str, size_t size, const char* fmt, va_list args)
1012 CPL_WARN_DEPRECATED("Use CPLvsnprintf() instead");
1013int snprintf(char *str, size_t size, const char* fmt, ...)
1015 CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
1016int sprintf(char *str, const char* fmt, ...)
1018 CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
1019# elif defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
1020int sprintf(char *str, const char* fmt, ...)
1022 CPL_WARN_DEPRECATED("Use snprintf() or CPLsnprintf() instead");
1023# endif /* defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF) */
1025#endif /* !defined(_MSC_VER) && !defined(__APPLE__) */
1026
1027#if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
1029#define CPL_CPU_REQUIRES_ALIGNED_ACCESS
1031#endif
1032
1033#if defined(__cplusplus)
1035#define CPL_ARRAYSIZE(array) \
1036 ((sizeof(array) / sizeof(*(array))) / \
1037 static_cast<size_t>(!(sizeof(array) % sizeof(*(array)))))
1038
1039extern "C++" {
1040template<class T> static void CPL_IGNORE_RET_VAL(T) {}
1041inline static bool CPL_TO_BOOL(int x) { return x != 0; }
1042} /* extern "C++" */
1043
1044#endif /* __cplusplus */
1045
1046#if (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || (defined(__clang__) && __clang_major__ >= 3)) && !defined(_MSC_VER))
1047#define HAVE_GCC_DIAGNOSTIC_PUSH
1048#endif
1049
1050#if ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) && !defined(_MSC_VER))
1051#define HAVE_GCC_SYSTEM_HEADER
1052#endif
1053
1054#if ((defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7))) || __GNUC__ >= 7)
1056# define CPL_FALLTHROUGH [[clang::fallthrough]];
1057#else
1059# define CPL_FALLTHROUGH
1060#endif
1061
1063// Define DEBUG_BOOL to compile in "MSVC mode", ie error out when
1064// a integer is assigned to a bool
1065// WARNING: use only at compilation time, since it is know to not work
1066// at runtime for unknown reasons (crash in MongoDB driver for example)
1067#if defined(__cplusplus) && defined(DEBUG_BOOL) && !defined(DO_NOT_USE_DEBUG_BOOL) && !defined(CPL_SUPRESS_CPLUSPLUS)
1068extern "C++" {
1069class MSVCPedanticBool
1070{
1071
1072 friend bool operator== (const bool& one, const MSVCPedanticBool& other);
1073 friend bool operator!= (const bool& one, const MSVCPedanticBool& other);
1074
1075 bool b;
1076 MSVCPedanticBool(int bIn);
1077
1078 public:
1079 /* b not initialized on purpose in default ctor to flag use. */
1080 /* cppcheck-suppress uninitMemberVar */
1081 MSVCPedanticBool() {}
1082 MSVCPedanticBool(bool bIn) : b(bIn) {}
1083 MSVCPedanticBool(const MSVCPedanticBool& other) : b(other.b) {}
1084
1085 MSVCPedanticBool& operator= (const MSVCPedanticBool& other) { b = other.b; return *this; }
1086 MSVCPedanticBool& operator&= (const MSVCPedanticBool& other) { b &= other.b; return *this; }
1087 MSVCPedanticBool& operator|= (const MSVCPedanticBool& other) { b |= other.b; return *this; }
1088
1089 bool operator== (const bool& other) const { return b == other; }
1090 bool operator!= (const bool& other) const { return b != other; }
1091 bool operator< (const bool& other) const { return b < other; }
1092 bool operator== (const MSVCPedanticBool& other) const { return b == other.b; }
1093 bool operator!= (const MSVCPedanticBool& other) const { return b != other.b; }
1094 bool operator< (const MSVCPedanticBool& other) const { return b < other.b; }
1095
1096 bool operator! () const { return !b; }
1097 operator bool() const { return b; }
1098 operator int() const { return b; }
1099 operator GIntBig() const { return b; }
1100};
1101
1102inline bool operator== (const bool& one, const MSVCPedanticBool& other) { return one == other.b; }
1103inline bool operator!= (const bool& one, const MSVCPedanticBool& other) { return one != other.b; }
1104
1105/* We must include all C++ stuff before to avoid issues with templates that use bool */
1106#include <vector>
1107#include <map>
1108#include <set>
1109#include <string>
1110#include <cstddef>
1111#include <limits>
1112#include <sstream>
1113#include <fstream>
1114#include <algorithm>
1115#include <functional>
1116#include <memory>
1117#include <queue>
1118#include <mutex>
1119#include <unordered_map>
1120#include <thread>
1121#include <unordered_set>
1122#include <complex>
1123#include <iomanip>
1124
1125} /* extern C++ */
1126
1127#undef FALSE
1128#define FALSE false
1129#undef TRUE
1130#define TRUE true
1131
1132/* In the very few cases we really need a "simple" type, fallback to bool */
1133#define EMULATED_BOOL int
1134
1135/* Use our class instead of bool */
1136#define bool MSVCPedanticBool
1137
1138/* "volatile bool" with the below substitution doesn't really work. */
1139/* Just for the sake of the debug, we don't really need volatile */
1140#define VOLATILE_BOOL bool
1141
1142#else /* defined(__cplusplus) && defined(DEBUG_BOOL) */
1143
1144#ifndef FALSE
1145# define FALSE 0
1146#endif
1147
1148#ifndef TRUE
1149# define TRUE 1
1150#endif
1151
1152#define EMULATED_BOOL bool
1153#define VOLATILE_BOOL volatile bool
1154
1155#endif /* defined(__cplusplus) && defined(DEBUG_BOOL) */
1156
1157#if __clang_major__ >= 4 || (__clang_major__ == 3 && __clang_minor__ >= 8)
1158#define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW __attribute__((no_sanitize("unsigned-integer-overflow")))
1159#else
1160#define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
1161#endif
1165#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
1166#define CPL_NULLPTR nullptr
1167#else
1168#define CPL_NULLPTR NULL
1169#endif
1172/* This typedef is for C functions that take char** as argument, but */
1173/* with the semantics of a const list. In C, char** is not implicitly cast to */
1174/* const char* const*, contrary to C++. So when seen for C++, it is OK */
1175/* to expose the prototyes as const char* const*, but for C we keep the */
1176/* historical definition to avoid warnings. */
1177#if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && !defined(DOXYGEN_SKIP)
1180typedef const char* const* CSLConstList;
1181#else
1184typedef char** CSLConstList;
1185#endif
1186
1187#endif /* ndef CPL_BASE_H_INCLUDED */
unsigned long long GUIntBig
Definition: cpl_port.h:249
short GInt16
Definition: cpl_port.h:209
#define CPL_C_END
Definition: cpl_port.h:337
#define CPL_C_START
Definition: cpl_port.h:335
GIntBig GInt64
Definition: cpl_port.h:265
int GBool
Definition: cpl_port.h:221
unsigned int GUInt32
Definition: cpl_port.h:205
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Definition: cpl_port.h:904
GIntBig GPtrDiff_t
Definition: cpl_port.h:284
char ** CSLConstList
Definition: cpl_port.h:1184
GUIntBig GUInt64
Definition: cpl_port.h:267
unsigned short GUInt16
Definition: cpl_port.h:211
unsigned char GByte
Definition: cpl_port.h:213
int GInt32
Definition: cpl_port.h:203
long long GIntBig
Definition: cpl_port.h:246
int CPLsnprintf(char *str, size_t size, const char *fmt,...)
Definition: cpl_string.cpp:1337

Generated for GDAL by doxygen 1.9.3.