| 1 | /* |
| 2 | * Copyright (c) 2015-2021 Nicholas Fraser and the MPack authors |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of |
| 5 | * this software and associated documentation files (the "Software"), to deal in |
| 6 | * the Software without restriction, including without limitation the rights to |
| 7 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of |
| 8 | * the Software, and to permit persons to whom the Software is furnished to do so, |
| 9 | * subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice shall be included in all |
| 12 | * copies or substantial portions of the Software. |
| 13 | * |
| 14 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS |
| 16 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR |
| 17 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER |
| 18 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 19 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 20 | */ |
| 21 | |
| 22 | /** |
| 23 | * @file |
| 24 | * |
| 25 | * Abstracts all platform-specific code from MPack and handles configuration |
| 26 | * options. |
| 27 | * |
| 28 | * This verifies the configuration and sets defaults based on the platform, |
| 29 | * contains implementations of standard C functions when libc is not available, |
| 30 | * and provides wrappers to all library functions. |
| 31 | * |
| 32 | * Documentation for configuration options is available here: |
| 33 | * |
| 34 | * https://ludocode.github.io/mpack/group__config.html |
| 35 | */ |
| 36 | |
| 37 | #ifndef MPACK_PLATFORM_H |
| 38 | #define MPACK_PLATFORM_H 1 |
| 39 | |
| 40 | |
| 41 | |
| 42 | /** |
| 43 | * @defgroup config Configuration Options |
| 44 | * |
| 45 | * Defines the MPack configuration options. |
| 46 | * |
| 47 | * Custom configuration of MPack is not usually necessary. In almost all |
| 48 | * cases you can ignore this and use the defaults. |
| 49 | * |
| 50 | * If you do want to configure MPack, you can define the below options as part |
| 51 | * of your build system or project settings. This will override the below |
| 52 | * defaults. |
| 53 | * |
| 54 | * If you'd like to use a file for configuration instead, define |
| 55 | * @ref MPACK_HAS_CONFIG to 1 in your build system or project settings. |
| 56 | * This will cause MPack to include a file you create called @c mpack-config.h |
| 57 | * in which you can define your configuration. This is useful if you need to |
| 58 | * include specific headers (such as a custom allocator) in order to configure |
| 59 | * MPack to use it. |
| 60 | * |
| 61 | * @warning The value of all configuration options must be the same in |
| 62 | * all translation units of your project, as well as in the mpack source |
| 63 | * itself. These configuration options affect the layout of structs, among |
| 64 | * other things, which cannot be different in source files that are linked |
| 65 | * together. |
| 66 | * |
| 67 | * @note MPack does not contain defaults for building inside the Linux kernel. |
| 68 | * There is a <a href="https://github.com/ludocode/mpack-linux-kernel"> |
| 69 | * configuration file for the Linux kernel</a> that can be used instead. |
| 70 | * |
| 71 | * @{ |
| 72 | */ |
| 73 | |
| 74 | |
| 75 | |
| 76 | /* |
| 77 | * Pre-include checks |
| 78 | * |
| 79 | * These need to come before the user's mpack-config.h because they might be |
| 80 | * including headers in it. |
| 81 | */ |
| 82 | |
| 83 | /** @cond */ |
| 84 | #if defined(_MSC_VER) && _MSC_VER < 1800 && !defined(__cplusplus) |
| 85 | #error "In Visual Studio 2012 and earlier, MPack must be compiled as C++. Enable the /Tp compiler flag." |
| 86 | #endif |
| 87 | |
| 88 | #if defined(_WIN32) && MPACK_INTERNAL |
| 89 | #define _CRT_SECURE_NO_WARNINGS 1 |
| 90 | #endif |
| 91 | |
| 92 | #ifndef __STDC_LIMIT_MACROS |
| 93 | #define __STDC_LIMIT_MACROS 1 |
| 94 | #endif |
| 95 | #ifndef __STDC_FORMAT_MACROS |
| 96 | #define __STDC_FORMAT_MACROS 1 |
| 97 | #endif |
| 98 | #ifndef __STDC_CONSTANT_MACROS |
| 99 | #define __STDC_CONSTANT_MACROS 1 |
| 100 | #endif |
| 101 | /** @endcond */ |
| 102 | |
| 103 | |
| 104 | |
| 105 | /** |
| 106 | * @name File Configuration |
| 107 | * @{ |
| 108 | */ |
| 109 | |
| 110 | /** |
| 111 | * @def MPACK_HAS_CONFIG |
| 112 | * |
| 113 | * Causes MPack to include a file you create called @c mpack-config.h . |
| 114 | * |
| 115 | * The file is included before MPack sets any defaults for undefined |
| 116 | * configuration options. You can use it to configure MPack. |
| 117 | * |
| 118 | * This is off by default. |
| 119 | */ |
| 120 | #if defined(MPACK_HAS_CONFIG) |
| 121 | #if MPACK_HAS_CONFIG |
| 122 | #include "mpack-config.h" |
| 123 | #endif |
| 124 | #else |
| 125 | #define MPACK_HAS_CONFIG 0 |
| 126 | #endif |
| 127 | |
| 128 | /** |
| 129 | * @} |
| 130 | */ |
| 131 | |
| 132 | // this needs to come first since some stuff depends on it |
| 133 | /** @cond */ |
| 134 | #ifndef MPACK_NO_BUILTINS |
| 135 | #define MPACK_NO_BUILTINS 0 |
| 136 | #endif |
| 137 | /** @endcond */ |
| 138 | |
| 139 | |
| 140 | |
| 141 | /** |
| 142 | * @name Features |
| 143 | * @{ |
| 144 | */ |
| 145 | |
| 146 | /** |
| 147 | * @def MPACK_READER |
| 148 | * |
| 149 | * Enables compilation of the base Tag Reader. |
| 150 | */ |
| 151 | #ifndef MPACK_READER |
| 152 | #define MPACK_READER 1 |
| 153 | #endif |
| 154 | |
| 155 | /** |
| 156 | * @def MPACK_EXPECT |
| 157 | * |
| 158 | * Enables compilation of the static Expect API. |
| 159 | */ |
| 160 | #ifndef MPACK_EXPECT |
| 161 | #define MPACK_EXPECT 1 |
| 162 | #endif |
| 163 | |
| 164 | /** |
| 165 | * @def MPACK_NODE |
| 166 | * |
| 167 | * Enables compilation of the dynamic Node API. |
| 168 | */ |
| 169 | #ifndef MPACK_NODE |
| 170 | #define MPACK_NODE 1 |
| 171 | #endif |
| 172 | |
| 173 | /** |
| 174 | * @def MPACK_WRITER |
| 175 | * |
| 176 | * Enables compilation of the Writer. |
| 177 | */ |
| 178 | #ifndef MPACK_WRITER |
| 179 | #define MPACK_WRITER 1 |
| 180 | #endif |
| 181 | |
| 182 | /** |
| 183 | * @def MPACK_BUILDER |
| 184 | * |
| 185 | * Enables compilation of the Builder. |
| 186 | * |
| 187 | * The Builder API provides additional functions to the Writer for |
| 188 | * automatically determining the element count of compound elements so you do |
| 189 | * not have to specify them up-front. |
| 190 | * |
| 191 | * This requires a @c malloc(). It is enabled by default if MPACK_WRITER is |
| 192 | * enabled and MPACK_MALLOC is defined. |
| 193 | * |
| 194 | * @see mpack_build_map() |
| 195 | * @see mpack_build_array() |
| 196 | * @see mpack_complete_map() |
| 197 | * @see mpack_complete_array() |
| 198 | */ |
| 199 | // This is defined furthur below after we've resolved whether we have malloc(). |
| 200 | |
| 201 | /** |
| 202 | * @def MPACK_COMPATIBILITY |
| 203 | * |
| 204 | * Enables compatibility features for reading and writing older |
| 205 | * versions of MessagePack. |
| 206 | * |
| 207 | * This is disabled by default. When disabled, the behaviour is equivalent to |
| 208 | * using the default version, @ref mpack_version_current. |
| 209 | * |
| 210 | * Enable this if you need to interoperate with applications or data that do |
| 211 | * not support the new (v5) MessagePack spec. See the section on v4 |
| 212 | * compatibility in @ref docs/protocol.md for more information. |
| 213 | */ |
| 214 | #ifndef MPACK_COMPATIBILITY |
| 215 | #define MPACK_COMPATIBILITY 0 |
| 216 | #endif |
| 217 | |
| 218 | /** |
| 219 | * @def MPACK_EXTENSIONS |
| 220 | * |
| 221 | * Enables the use of extension types. |
| 222 | * |
| 223 | * This is disabled by default. Define it to 1 to enable it. If disabled, |
| 224 | * functions to read and write extensions will not exist, and any occurrence of |
| 225 | * extension types in parsed messages will flag @ref mpack_error_invalid. |
| 226 | * |
| 227 | * MPack discourages the use of extension types. See the section on extension |
| 228 | * types in @ref docs/protocol.md for more information. |
| 229 | */ |
| 230 | #ifndef MPACK_EXTENSIONS |
| 231 | #define MPACK_EXTENSIONS 0 |
| 232 | #endif |
| 233 | |
| 234 | /** |
| 235 | * @} |
| 236 | */ |
| 237 | |
| 238 | |
| 239 | |
| 240 | // workarounds for Doxygen |
| 241 | #if defined(MPACK_DOXYGEN) |
| 242 | #if MPACK_DOXYGEN |
| 243 | // We give these their default values of 0 here even though they are defined to |
| 244 | // 1 in the doxyfile. Doxygen will show this as the value in the docs, even |
| 245 | // though it ignores it when parsing the rest of the source. This is what we |
| 246 | // want, since we want the documentation to show these defaults but still |
| 247 | // generate documentation for the functions they add when they're on. |
| 248 | #define MPACK_COMPATIBILITY 0 |
| 249 | #define MPACK_EXTENSIONS 0 |
| 250 | #endif |
| 251 | #endif |
| 252 | |
| 253 | |
| 254 | |
| 255 | /** |
| 256 | * @name Dependencies |
| 257 | * @{ |
| 258 | */ |
| 259 | |
| 260 | /** |
| 261 | * @def MPACK_CONFORMING |
| 262 | * |
| 263 | * Enables the inclusion of basic C headers to define standard types and |
| 264 | * macros. |
| 265 | * |
| 266 | * This causes MPack to include headers required for conforming implementations |
| 267 | * of C99 even in freestanding, in particular <stddef.h>, <stdint.h>, |
| 268 | * <stdbool.h> and <limits.h>. It also includes <inttypes.h>; this is |
| 269 | * technically not required for freestanding but MPack needs it to detect |
| 270 | * integer limits. |
| 271 | * |
| 272 | * You can disable this if these headers are unavailable or if they do not |
| 273 | * define the standard types and macros (for example inside the Linux kernel.) |
| 274 | * If this is disabled, MPack will include no headers and will assume a 32-bit |
| 275 | * int. You will probably also want to define @ref MPACK_HAS_CONFIG to 1 and |
| 276 | * include your own headers in the config file. You must provide definitions |
| 277 | * for standard types such as @c size_t, @c bool, @c int32_t and so on. |
| 278 | * |
| 279 | * @see <a href="https://en.cppreference.com/w/c/language/conformance"> |
| 280 | * cppreference.com documentation on Conformance</a> |
| 281 | */ |
| 282 | #ifndef MPACK_CONFORMING |
| 283 | #define MPACK_CONFORMING 1 |
| 284 | #endif |
| 285 | |
| 286 | /** |
| 287 | * @def MPACK_STDLIB |
| 288 | * |
| 289 | * Enables the use of the C stdlib. |
| 290 | * |
| 291 | * This allows the library to use basic functions like @c memcmp() and @c |
| 292 | * strlen(), as well as @c malloc() for debugging and in allocation helpers. |
| 293 | * |
| 294 | * If this is disabled, allocation helper functions will not be defined, and |
| 295 | * MPack will attempt to detect compiler intrinsics for functions like @c |
| 296 | * memcmp() (assuming @ref MPACK_NO_BUILTINS is not set.) It will fallback to |
| 297 | * its own (slow) implementations if it cannot use builtins. You may want to |
| 298 | * define @ref MPACK_MEMCMP and friends if you disable this. |
| 299 | * |
| 300 | * @see MPACK_MEMCMP |
| 301 | * @see MPACK_MEMCPY |
| 302 | * @see MPACK_MEMMOVE |
| 303 | * @see MPACK_MEMSET |
| 304 | * @see MPACK_STRLEN |
| 305 | * @see MPACK_MALLOC |
| 306 | * @see MPACK_REALLOC |
| 307 | * @see MPACK_FREE |
| 308 | */ |
| 309 | #ifndef MPACK_STDLIB |
| 310 | #if !MPACK_CONFORMING |
| 311 | // If we don't even have a proper <limits.h> we assume we won't have |
| 312 | // malloc() either. |
| 313 | #define MPACK_STDLIB 0 |
| 314 | #else |
| 315 | #define MPACK_STDLIB 1 |
| 316 | #endif |
| 317 | #endif |
| 318 | |
| 319 | /** |
| 320 | * @def MPACK_STDIO |
| 321 | * |
| 322 | * Enables the use of C stdio. This adds helpers for easily |
| 323 | * reading/writing C files and makes debugging easier. |
| 324 | */ |
| 325 | #ifndef MPACK_STDIO |
| 326 | #if !MPACK_STDLIB || defined(__AVR__) |
| 327 | #define MPACK_STDIO 0 |
| 328 | #else |
| 329 | #define MPACK_STDIO 1 |
| 330 | #endif |
| 331 | #endif |
| 332 | |
| 333 | /** |
| 334 | * Whether the 'float' type and floating point operations are supported. |
| 335 | * |
| 336 | * If @ref MPACK_FLOAT is disabled, floats are read and written as @c uint32_t |
| 337 | * instead. This way messages with floats do not result in errors and you can |
| 338 | * still perform manual float parsing yourself. |
| 339 | */ |
| 340 | #ifndef MPACK_FLOAT |
| 341 | #define MPACK_FLOAT 1 |
| 342 | #endif |
| 343 | |
| 344 | /** |
| 345 | * Whether the 'double' type is supported. This requires support for 'float'. |
| 346 | * |
| 347 | * If @ref MPACK_DOUBLE is disabled, doubles are read and written as @c |
| 348 | * uint32_t instead. This way messages with doubles do not result in errors and |
| 349 | * you can still perform manual doubles parsing yourself. |
| 350 | * |
| 351 | * If @ref MPACK_FLOAT is enabled but @ref MPACK_DOUBLE is not, doubles can be |
| 352 | * read as floats using the shortening conversion functions, e.g. @ref |
| 353 | * mpack_expect_float() or @ref mpack_node_float(). |
| 354 | */ |
| 355 | #ifndef MPACK_DOUBLE |
| 356 | #if !MPACK_FLOAT || defined(__AVR__) |
| 357 | // AVR supports only float, not double. |
| 358 | #define MPACK_DOUBLE 0 |
| 359 | #else |
| 360 | #define MPACK_DOUBLE 1 |
| 361 | #endif |
| 362 | #endif |
| 363 | |
| 364 | /** |
| 365 | * @} |
| 366 | */ |
| 367 | |
| 368 | |
| 369 | |
| 370 | /** |
| 371 | * @name Allocation Functions |
| 372 | * @{ |
| 373 | */ |
| 374 | |
| 375 | /** |
| 376 | * @def MPACK_MALLOC |
| 377 | * |
| 378 | * Defines the memory allocation function used by MPack. This is used by |
| 379 | * helpers for automatically allocating data the correct size, and for |
| 380 | * debugging functions. If this macro is undefined, the allocation helpers |
| 381 | * will not be compiled. |
| 382 | * |
| 383 | * Set this to use a custom @c malloc() function. Your function must have the |
| 384 | * signature: |
| 385 | * |
| 386 | * @code |
| 387 | * void* malloc(size_t size); |
| 388 | * @endcode |
| 389 | * |
| 390 | * The default is @c malloc() if @ref MPACK_STDLIB is enabled. |
| 391 | */ |
| 392 | /** |
| 393 | * @def MPACK_FREE |
| 394 | * |
| 395 | * Defines the memory free function used by MPack. This is used by helpers |
| 396 | * for automatically allocating data the correct size. If this macro is |
| 397 | * undefined, the allocation helpers will not be compiled. |
| 398 | * |
| 399 | * Set this to use a custom @c free() function. Your function must have the |
| 400 | * signature: |
| 401 | * |
| 402 | * @code |
| 403 | * void free(void* p); |
| 404 | * @endcode |
| 405 | * |
| 406 | * The default is @c free() if @ref MPACK_MALLOC has not been customized and |
| 407 | * @ref MPACK_STDLIB is enabled. |
| 408 | */ |
| 409 | /** |
| 410 | * @def MPACK_REALLOC |
| 411 | * |
| 412 | * Defines the realloc function used by MPack. It is used by growable |
| 413 | * buffers to resize more efficiently. |
| 414 | * |
| 415 | * The default is @c realloc() if @ref MPACK_MALLOC has not been customized and |
| 416 | * @ref MPACK_STDLIB is enabled. |
| 417 | * |
| 418 | * Set this to use a custom @c realloc() function. Your function must have the |
| 419 | * signature: |
| 420 | * |
| 421 | * @code |
| 422 | * void* realloc(void* p, size_t new_size); |
| 423 | * @endcode |
| 424 | * |
| 425 | * This is optional, even when @ref MPACK_MALLOC is used. If @ref MPACK_MALLOC is |
| 426 | * set and @ref MPACK_REALLOC is not, @ref MPACK_MALLOC is used with a simple copy |
| 427 | * to grow buffers. |
| 428 | */ |
| 429 | |
| 430 | #if defined(MPACK_MALLOC) && !defined(MPACK_FREE) |
| 431 | #error "MPACK_MALLOC requires MPACK_FREE." |
| 432 | #endif |
| 433 | #if !defined(MPACK_MALLOC) && defined(MPACK_FREE) |
| 434 | #error "MPACK_FREE requires MPACK_MALLOC." |
| 435 | #endif |
| 436 | |
| 437 | // These were never configurable in lowercase but we check anyway. |
| 438 | #ifdef mpack_malloc |
| 439 | #error "Define MPACK_MALLOC, not mpack_malloc." |
| 440 | #endif |
| 441 | #ifdef mpack_realloc |
| 442 | #error "Define MPACK_REALLOC, not mpack_realloc." |
| 443 | #endif |
| 444 | #ifdef mpack_free |
| 445 | #error "Define MPACK_FREE, not mpack_free." |
| 446 | #endif |
| 447 | |
| 448 | // We don't use calloc() at all. |
| 449 | #ifdef MPACK_CALLOC |
| 450 | #error "Don't define MPACK_CALLOC. MPack does not use calloc()." |
| 451 | #endif |
| 452 | #ifdef mpack_calloc |
| 453 | #error "Don't define mpack_calloc. MPack does not use calloc()." |
| 454 | #endif |
| 455 | |
| 456 | // Use defaults in stdlib if we have them. Without it we don't use malloc. |
| 457 | #if defined(MPACK_STDLIB) |
| 458 | #if MPACK_STDLIB && !defined(MPACK_MALLOC) |
| 459 | #define MPACK_MALLOC malloc |
| 460 | #define MPACK_REALLOC realloc |
| 461 | #define MPACK_FREE free |
| 462 | #endif |
| 463 | #endif |
| 464 | |
| 465 | /** |
| 466 | * @} |
| 467 | */ |
| 468 | |
| 469 | |
| 470 | |
| 471 | // This needs to be defined after we've decided whether we have malloc(). |
| 472 | #ifndef MPACK_BUILDER |
| 473 | #if defined(MPACK_MALLOC) && MPACK_WRITER |
| 474 | #define MPACK_BUILDER 1 |
| 475 | #else |
| 476 | #define MPACK_BUILDER 0 |
| 477 | #endif |
| 478 | #endif |
| 479 | |
| 480 | |
| 481 | |
| 482 | /** |
| 483 | * @name System Functions |
| 484 | * @{ |
| 485 | */ |
| 486 | |
| 487 | /** |
| 488 | * @def MPACK_MEMCMP |
| 489 | * |
| 490 | * The function MPack will use to provide @c memcmp(). |
| 491 | * |
| 492 | * Set this to use a custom @c memcmp() function. Your function must have the |
| 493 | * signature: |
| 494 | * |
| 495 | * @code |
| 496 | * int memcmp(const void* left, const void* right, size_t count); |
| 497 | * @endcode |
| 498 | */ |
| 499 | /** |
| 500 | * @def MPACK_MEMCPY |
| 501 | * |
| 502 | * The function MPack will use to provide @c memcpy(). |
| 503 | * |
| 504 | * Set this to use a custom @c memcpy() function. Your function must have the |
| 505 | * signature: |
| 506 | * |
| 507 | * @code |
| 508 | * void* memcpy(void* restrict dest, const void* restrict src, size_t count); |
| 509 | * @endcode |
| 510 | */ |
| 511 | /** |
| 512 | * @def MPACK_MEMMOVE |
| 513 | * |
| 514 | * The function MPack will use to provide @c memmove(). |
| 515 | * |
| 516 | * Set this to use a custom @c memmove() function. Your function must have the |
| 517 | * signature: |
| 518 | * |
| 519 | * @code |
| 520 | * void* memmove(void* dest, const void* src, size_t count); |
| 521 | * @endcode |
| 522 | */ |
| 523 | /** |
| 524 | * @def MPACK_MEMSET |
| 525 | * |
| 526 | * The function MPack will use to provide @c memset(). |
| 527 | * |
| 528 | * Set this to use a custom @c memset() function. Your function must have the |
| 529 | * signature: |
| 530 | * |
| 531 | * @code |
| 532 | * void* memset(void* p, int c, size_t count); |
| 533 | * @endcode |
| 534 | */ |
| 535 | /** |
| 536 | * @def MPACK_STRLEN |
| 537 | * |
| 538 | * The function MPack will use to provide @c strlen(). |
| 539 | * |
| 540 | * Set this to use a custom @c strlen() function. Your function must have the |
| 541 | * signature: |
| 542 | * |
| 543 | * @code |
| 544 | * size_t strlen(const char* str); |
| 545 | * @endcode |
| 546 | */ |
| 547 | |
| 548 | // These were briefly configurable in lowercase in an unreleased version. Just |
| 549 | // to make sure no one is doing this, we make sure these aren't already defined. |
| 550 | #ifdef mpack_memcmp |
| 551 | #error "Define MPACK_MEMCMP, not mpack_memcmp." |
| 552 | #endif |
| 553 | #ifdef mpack_memcpy |
| 554 | #error "Define MPACK_MEMCPY, not mpack_memcpy." |
| 555 | #endif |
| 556 | #ifdef mpack_memmove |
| 557 | #error "Define MPACK_MEMMOVE, not mpack_memmove." |
| 558 | #endif |
| 559 | #ifdef mpack_memset |
| 560 | #error "Define MPACK_MEMSET, not mpack_memset." |
| 561 | #endif |
| 562 | #ifdef mpack_strlen |
| 563 | #error "Define MPACK_STRLEN, not mpack_strlen." |
| 564 | #endif |
| 565 | |
| 566 | // If the standard library is available, we prefer to use its functions. |
| 567 | #if MPACK_STDLIB |
| 568 | #ifndef MPACK_MEMCMP |
| 569 | #define MPACK_MEMCMP memcmp |
| 570 | #endif |
| 571 | #ifndef MPACK_MEMCPY |
| 572 | #define MPACK_MEMCPY memcpy |
| 573 | #endif |
| 574 | #ifndef MPACK_MEMMOVE |
| 575 | #define MPACK_MEMMOVE memmove |
| 576 | #endif |
| 577 | #ifndef MPACK_MEMSET |
| 578 | #define MPACK_MEMSET memset |
| 579 | #endif |
| 580 | #ifndef MPACK_STRLEN |
| 581 | #define MPACK_STRLEN strlen |
| 582 | #endif |
| 583 | #endif |
| 584 | |
| 585 | #if !MPACK_NO_BUILTINS |
| 586 | #ifdef __has_builtin |
| 587 | #if !defined(MPACK_MEMCMP) && __has_builtin(__builtin_memcmp) |
| 588 | #define MPACK_MEMCMP __builtin_memcmp |
| 589 | #endif |
| 590 | #if !defined(MPACK_MEMCPY) && __has_builtin(__builtin_memcpy) |
| 591 | #define MPACK_MEMCPY __builtin_memcpy |
| 592 | #endif |
| 593 | #if !defined(MPACK_MEMMOVE) && __has_builtin(__builtin_memmove) |
| 594 | #define MPACK_MEMMOVE __builtin_memmove |
| 595 | #endif |
| 596 | #if !defined(MPACK_MEMSET) && __has_builtin(__builtin_memset) |
| 597 | #define MPACK_MEMSET __builtin_memset |
| 598 | #endif |
| 599 | #if !defined(MPACK_STRLEN) && __has_builtin(__builtin_strlen) |
| 600 | #define MPACK_STRLEN __builtin_strlen |
| 601 | #endif |
| 602 | #elif defined(__GNUC__) |
| 603 | #ifndef MPACK_MEMCMP |
| 604 | #define MPACK_MEMCMP __builtin_memcmp |
| 605 | #endif |
| 606 | #ifndef MPACK_MEMCPY |
| 607 | #define MPACK_MEMCPY __builtin_memcpy |
| 608 | #endif |
| 609 | // There's not always a builtin memmove for GCC. If we can't test for |
| 610 | // it with __has_builtin above, we don't use it. It's been around for |
| 611 | // much longer under clang, but then so has __has_builtin, so we let |
| 612 | // the block above handle it. |
| 613 | #ifndef MPACK_MEMSET |
| 614 | #define MPACK_MEMSET __builtin_memset |
| 615 | #endif |
| 616 | #ifndef MPACK_STRLEN |
| 617 | #define MPACK_STRLEN __builtin_strlen |
| 618 | #endif |
| 619 | #endif |
| 620 | #endif |
| 621 | |
| 622 | /** |
| 623 | * @} |
| 624 | */ |
| 625 | |
| 626 | |
| 627 | |
| 628 | /** |
| 629 | * @name Debugging Options |
| 630 | * @{ |
| 631 | */ |
| 632 | |
| 633 | /** |
| 634 | * @def MPACK_DEBUG |
| 635 | * |
| 636 | * Enables debug features. You may want to wrap this around your |
| 637 | * own debug preprocs. By default, this is enabled if @c DEBUG or @c _DEBUG |
| 638 | * are defined. (@c NDEBUG is not used since it is allowed to have |
| 639 | * different values in different translation units.) |
| 640 | */ |
| 641 | #if !defined(MPACK_DEBUG) |
| 642 | #if defined(DEBUG) || defined(_DEBUG) |
| 643 | #define MPACK_DEBUG 1 |
| 644 | #else |
| 645 | #define MPACK_DEBUG 0 |
| 646 | #endif |
| 647 | #endif |
| 648 | |
| 649 | /** |
| 650 | * @def MPACK_STRINGS |
| 651 | * |
| 652 | * Enables descriptive error and type strings. |
| 653 | * |
| 654 | * This can be turned off (by defining it to 0) to maximize space savings |
| 655 | * on embedded devices. If this is disabled, string functions such as |
| 656 | * mpack_error_to_string() and mpack_type_to_string() return an empty string. |
| 657 | */ |
| 658 | #ifndef MPACK_STRINGS |
| 659 | #ifdef __AVR__ |
| 660 | #define MPACK_STRINGS 0 |
| 661 | #else |
| 662 | #define MPACK_STRINGS 1 |
| 663 | #endif |
| 664 | #endif |
| 665 | |
| 666 | /** |
| 667 | * Set this to 1 to implement a custom @ref mpack_assert_fail() function. |
| 668 | * See the documentation on @ref mpack_assert_fail() for details. |
| 669 | * |
| 670 | * Asserts are only used when @ref MPACK_DEBUG is enabled, and can be |
| 671 | * triggered by bugs in MPack or bugs due to incorrect usage of MPack. |
| 672 | */ |
| 673 | #ifndef MPACK_CUSTOM_ASSERT |
| 674 | #define MPACK_CUSTOM_ASSERT 0 |
| 675 | #endif |
| 676 | |
| 677 | /** |
| 678 | * @def MPACK_READ_TRACKING |
| 679 | * |
| 680 | * Enables compound type size tracking for readers. This ensures that the |
| 681 | * correct number of elements or bytes are read from a compound type. |
| 682 | * |
| 683 | * This is enabled by default in debug builds (provided a @c malloc() is |
| 684 | * available.) |
| 685 | */ |
| 686 | #if !defined(MPACK_READ_TRACKING) |
| 687 | #if MPACK_DEBUG && MPACK_READER && defined(MPACK_MALLOC) |
| 688 | #define MPACK_READ_TRACKING 1 |
| 689 | #else |
| 690 | #define MPACK_READ_TRACKING 0 |
| 691 | #endif |
| 692 | #endif |
| 693 | #if MPACK_READ_TRACKING && !MPACK_READER |
| 694 | #error "MPACK_READ_TRACKING requires MPACK_READER." |
| 695 | #endif |
| 696 | |
| 697 | /** |
| 698 | * @def MPACK_WRITE_TRACKING |
| 699 | * |
| 700 | * Enables compound type size tracking for writers. This ensures that the |
| 701 | * correct number of elements or bytes are written in a compound type. |
| 702 | * |
| 703 | * Note that without write tracking enabled, it is possible for buggy code |
| 704 | * to emit invalid MessagePack without flagging an error by writing the wrong |
| 705 | * number of elements or bytes in a compound type. With tracking enabled, |
| 706 | * MPack will catch such errors and break on the offending line of code. |
| 707 | * |
| 708 | * This is enabled by default in debug builds (provided a @c malloc() is |
| 709 | * available.) |
| 710 | */ |
| 711 | #if !defined(MPACK_WRITE_TRACKING) |
| 712 | #if MPACK_DEBUG && MPACK_WRITER && defined(MPACK_MALLOC) |
| 713 | #define MPACK_WRITE_TRACKING 1 |
| 714 | #else |
| 715 | #define MPACK_WRITE_TRACKING 0 |
| 716 | #endif |
| 717 | #endif |
| 718 | #if MPACK_WRITE_TRACKING && !MPACK_WRITER |
| 719 | #error "MPACK_WRITE_TRACKING requires MPACK_WRITER." |
| 720 | #endif |
| 721 | |
| 722 | /** |
| 723 | * @} |
| 724 | */ |
| 725 | |
| 726 | |
| 727 | |
| 728 | |
| 729 | /** |
| 730 | * @name Miscellaneous Options |
| 731 | * @{ |
| 732 | */ |
| 733 | |
| 734 | /** |
| 735 | * Whether to optimize for size or speed. |
| 736 | * |
| 737 | * Optimizing for size simplifies some parsing and encoding algorithms |
| 738 | * at the expense of speed and saves a few kilobytes of space in the |
| 739 | * resulting executable. |
| 740 | * |
| 741 | * This automatically detects -Os with GCC/Clang. Unfortunately there |
| 742 | * doesn't seem to be a macro defined for /Os under MSVC. |
| 743 | */ |
| 744 | #ifndef MPACK_OPTIMIZE_FOR_SIZE |
| 745 | #ifdef __OPTIMIZE_SIZE__ |
| 746 | #define MPACK_OPTIMIZE_FOR_SIZE 1 |
| 747 | #else |
| 748 | #define MPACK_OPTIMIZE_FOR_SIZE 0 |
| 749 | #endif |
| 750 | #endif |
| 751 | |
| 752 | /** |
| 753 | * Stack space in bytes to use when initializing a reader or writer |
| 754 | * with a stack-allocated buffer. |
| 755 | * |
| 756 | * @warning Make sure you have sufficient stack space. Some libc use relatively |
| 757 | * small stacks even on desktop platforms, e.g. musl. |
| 758 | */ |
| 759 | #ifndef MPACK_STACK_SIZE |
| 760 | #define MPACK_STACK_SIZE 4096 |
| 761 | #endif |
| 762 | |
| 763 | /** |
| 764 | * Buffer size to use for allocated buffers (such as for a file writer.) |
| 765 | * |
| 766 | * Starting with a single page and growing as needed seems to |
| 767 | * provide the best performance with minimal memory waste. |
| 768 | * Increasing this does not improve performance even when writing |
| 769 | * huge messages. |
| 770 | */ |
| 771 | #ifndef MPACK_BUFFER_SIZE |
| 772 | #define MPACK_BUFFER_SIZE 4096 |
| 773 | #endif |
| 774 | |
| 775 | /** |
| 776 | * Minimum size for paged allocations in bytes. |
| 777 | * |
| 778 | * This is the value used by default for MPACK_NODE_PAGE_SIZE and |
| 779 | * MPACK_BUILDER_PAGE_SIZE. |
| 780 | */ |
| 781 | #ifndef MPACK_PAGE_SIZE |
| 782 | #define MPACK_PAGE_SIZE 4096 |
| 783 | #endif |
| 784 | |
| 785 | /** |
| 786 | * Minimum size of an allocated node page in bytes. |
| 787 | * |
| 788 | * The children for a given compound element must be contiguous, so |
| 789 | * larger pages than this may be allocated as needed. (Safety checks |
| 790 | * exist to prevent malicious data from causing too large allocations.) |
| 791 | * |
| 792 | * See @ref mpack_node_data_t for the size of nodes. |
| 793 | * |
| 794 | * Using as many nodes fit in one memory page seems to provide the |
| 795 | * best performance, and has very little waste when parsing small |
| 796 | * messages. |
| 797 | */ |
| 798 | #ifndef MPACK_NODE_PAGE_SIZE |
| 799 | #define MPACK_NODE_PAGE_SIZE MPACK_PAGE_SIZE |
| 800 | #endif |
| 801 | |
| 802 | /** |
| 803 | * Minimum size of an allocated builder page in bytes. |
| 804 | * |
| 805 | * Builder writes are deferred to the allocated builder buffer which is |
| 806 | * composed of a list of buffer pages. This defines the size of those pages. |
| 807 | */ |
| 808 | #ifndef MPACK_BUILDER_PAGE_SIZE |
| 809 | #define MPACK_BUILDER_PAGE_SIZE MPACK_PAGE_SIZE |
| 810 | #endif |
| 811 | |
| 812 | /** |
| 813 | * @def MPACK_BUILDER_INTERNAL_STORAGE |
| 814 | * |
| 815 | * Enables a small amount of internal storage within the writer to avoid some |
| 816 | * allocations when using builders. |
| 817 | * |
| 818 | * This is disabled by default. Enable it to potentially improve performance at |
| 819 | * the expense of a larger writer. |
| 820 | * |
| 821 | * @see MPACK_BUILDER_INTERNAL_STORAGE_SIZE to configure its size. |
| 822 | */ |
| 823 | #ifndef MPACK_BUILDER_INTERNAL_STORAGE |
| 824 | #define MPACK_BUILDER_INTERNAL_STORAGE 0 |
| 825 | #endif |
| 826 | |
| 827 | /** |
| 828 | * Amount of space reserved inside @ref mpack_writer_t for the Builders. This |
| 829 | * can allow small messages to be built with the Builder API without incurring |
| 830 | * an allocation. |
| 831 | * |
| 832 | * Builder metadata is placed in this space in addition to the literal |
| 833 | * MessagePack data. It needs to be big enough to be useful, but not so big as |
| 834 | * to overflow the stack. If more space is needed, pages are allocated. |
| 835 | * |
| 836 | * This is only used if MPACK_BUILDER_INTERNAL_STORAGE is enabled. |
| 837 | * |
| 838 | * @see MPACK_BUILDER_PAGE_SIZE |
| 839 | * @see MPACK_BUILDER_INTERNAL_STORAGE |
| 840 | * |
| 841 | * @warning Writers are typically placed on the stack so make sure you have |
| 842 | * sufficient stack space. Some libc use relatively small stacks even on |
| 843 | * desktop platforms, e.g. musl. |
| 844 | */ |
| 845 | #ifndef MPACK_BUILDER_INTERNAL_STORAGE_SIZE |
| 846 | #define MPACK_BUILDER_INTERNAL_STORAGE_SIZE 256 |
| 847 | #endif |
| 848 | |
| 849 | /** |
| 850 | * The initial depth for the node parser. When MPACK_MALLOC is available, |
| 851 | * the node parser has no practical depth limit, and it is not recursive |
| 852 | * so there is no risk of overflowing the call stack. |
| 853 | */ |
| 854 | #ifndef MPACK_NODE_INITIAL_DEPTH |
| 855 | #define MPACK_NODE_INITIAL_DEPTH 8 |
| 856 | #endif |
| 857 | |
| 858 | /** |
| 859 | * The maximum depth for the node parser if @ref MPACK_MALLOC is not available. |
| 860 | */ |
| 861 | #ifndef MPACK_NODE_MAX_DEPTH_WITHOUT_MALLOC |
| 862 | #define MPACK_NODE_MAX_DEPTH_WITHOUT_MALLOC 32 |
| 863 | #endif |
| 864 | |
| 865 | /** |
| 866 | * @def MPACK_NO_BUILTINS |
| 867 | * |
| 868 | * Whether to disable compiler intrinsics and other built-in functions. |
| 869 | * |
| 870 | * If this is enabled, MPack won't use `__attribute__`, `__declspec`, any |
| 871 | * function starting with `__builtin`, or pretty much anything else that isn't |
| 872 | * standard C. |
| 873 | */ |
| 874 | #if defined(MPACK_DOXYGEN) |
| 875 | #if MPACK_DOXYGEN |
| 876 | #define MPACK_NO_BUILTINS 0 |
| 877 | #endif |
| 878 | #endif |
| 879 | |
| 880 | /** |
| 881 | * @} |
| 882 | */ |
| 883 | |
| 884 | |
| 885 | |
| 886 | #if MPACK_DEBUG |
| 887 | /** |
| 888 | * @name Debug Functions |
| 889 | * @{ |
| 890 | */ |
| 891 | /** |
| 892 | * Implement this and define @ref MPACK_CUSTOM_ASSERT to use a custom |
| 893 | * assertion function. |
| 894 | * |
| 895 | * This function should not return. If it does, MPack will @c abort(). |
| 896 | * |
| 897 | * If you use C++, make sure you include @c mpack.h where you define |
| 898 | * this to get the correct linkage (or define it <code>extern "C"</code>.) |
| 899 | * |
| 900 | * Asserts are only used when @ref MPACK_DEBUG is enabled, and can be |
| 901 | * triggered by bugs in MPack or bugs due to incorrect usage of MPack. |
| 902 | */ |
| 903 | void mpack_assert_fail(const char* message); |
| 904 | /** |
| 905 | * @} |
| 906 | */ |
| 907 | #endif |
| 908 | |
| 909 | |
| 910 | |
| 911 | // The rest of this file shouldn't show up in Doxygen docs. |
| 912 | /** @cond */ |
| 913 | |
| 914 | |
| 915 | |
| 916 | /* |
| 917 | * All remaining pseudo-configuration options that have not yet been set must |
| 918 | * be defined here in order to support -Wundef. |
| 919 | * |
| 920 | * These aren't real configuration options; they are implementation details of |
| 921 | * MPack. |
| 922 | */ |
| 923 | #ifndef MPACK_CUSTOM_BREAK |
| 924 | #define MPACK_CUSTOM_BREAK 0 |
| 925 | #endif |
| 926 | #ifndef MPACK_EMIT_INLINE_DEFS |
| 927 | #define MPACK_EMIT_INLINE_DEFS 0 |
| 928 | #endif |
| 929 | #ifndef MPACK_AMALGAMATED |
| 930 | #define MPACK_AMALGAMATED 0 |
| 931 | #endif |
| 932 | #ifndef MPACK_RELEASE_VERSION |
| 933 | #define MPACK_RELEASE_VERSION 0 |
| 934 | #endif |
| 935 | #ifndef MPACK_INTERNAL |
| 936 | #define MPACK_INTERNAL 0 |
| 937 | #endif |
| 938 | |
| 939 | |
| 940 | |
| 941 | /* System headers (based on configuration) */ |
| 942 | |
| 943 | #if MPACK_CONFORMING |
| 944 | #include <stddef.h> |
| 945 | #include <stdint.h> |
| 946 | #include <stdbool.h> |
| 947 | #include <inttypes.h> |
| 948 | #include <limits.h> |
| 949 | #endif |
| 950 | |
| 951 | #if MPACK_STDLIB |
| 952 | #include <string.h> |
| 953 | #include <stdlib.h> |
| 954 | #endif |
| 955 | |
| 956 | #if MPACK_STDIO |
| 957 | #include <stdio.h> |
| 958 | #include <errno.h> |
| 959 | #if MPACK_DEBUG |
| 960 | #include <stdarg.h> |
| 961 | #endif |
| 962 | #endif |
| 963 | |
| 964 | |
| 965 | |
| 966 | /* |
| 967 | * Integer Constants and Limits |
| 968 | */ |
| 969 | |
| 970 | #if MPACK_CONFORMING |
| 971 | #define MPACK_INT64_C INT64_C |
| 972 | #define MPACK_UINT64_C UINT64_C |
| 973 | |
| 974 | #define MPACK_INT8_MIN INT8_MIN |
| 975 | #define MPACK_INT16_MIN INT16_MIN |
| 976 | #define MPACK_INT32_MIN INT32_MIN |
| 977 | #define MPACK_INT64_MIN INT64_MIN |
| 978 | #define MPACK_INT_MIN INT_MIN |
| 979 | |
| 980 | #define MPACK_INT8_MAX INT8_MAX |
| 981 | #define MPACK_INT16_MAX INT16_MAX |
| 982 | #define MPACK_INT32_MAX INT32_MAX |
| 983 | #define MPACK_INT64_MAX INT64_MAX |
| 984 | #define MPACK_INT_MAX INT_MAX |
| 985 | |
| 986 | #define MPACK_UINT8_MAX UINT8_MAX |
| 987 | #define MPACK_UINT16_MAX UINT16_MAX |
| 988 | #define MPACK_UINT32_MAX UINT32_MAX |
| 989 | #define MPACK_UINT64_MAX UINT64_MAX |
| 990 | #define MPACK_UINT_MAX UINT_MAX |
| 991 | #else |
| 992 | // For a non-conforming implementation we assume int is 32 bits. |
| 993 | |
| 994 | #define MPACK_INT64_C(x) ((int64_t)(x##LL)) |
| 995 | #define MPACK_UINT64_C(x) ((uint64_t)(x##LLU)) |
| 996 | |
| 997 | #define MPACK_INT8_MIN ((int8_t)(0x80)) |
| 998 | #define MPACK_INT16_MIN ((int16_t)(0x8000)) |
| 999 | #define MPACK_INT32_MIN ((int32_t)(0x80000000)) |
| 1000 | #define MPACK_INT64_MIN MPACK_INT64_C(0x8000000000000000) |
| 1001 | #define MPACK_INT_MIN MPACK_INT32_MIN |
| 1002 | |
| 1003 | #define MPACK_INT8_MAX ((int8_t)(0x7f)) |
| 1004 | #define MPACK_INT16_MAX ((int16_t)(0x7fff)) |
| 1005 | #define MPACK_INT32_MAX ((int32_t)(0x7fffffff)) |
| 1006 | #define MPACK_INT64_MAX MPACK_INT64_C(0x7fffffffffffffff) |
| 1007 | #define MPACK_INT_MAX MPACK_INT32_MAX |
| 1008 | |
| 1009 | #define MPACK_UINT8_MAX ((uint8_t)(0xffu)) |
| 1010 | #define MPACK_UINT16_MAX ((uint16_t)(0xffffu)) |
| 1011 | #define MPACK_UINT32_MAX ((uint32_t)(0xffffffffu)) |
| 1012 | #define MPACK_UINT64_MAX MPACK_UINT64_C(0xffffffffffffffff) |
| 1013 | #define MPACK_UINT_MAX MPACK_UINT32_MAX |
| 1014 | #endif |
| 1015 | |
| 1016 | |
| 1017 | |
| 1018 | /* |
| 1019 | * Floating point support |
| 1020 | */ |
| 1021 | |
| 1022 | #if MPACK_DOUBLE && !MPACK_FLOAT |
| 1023 | #error "MPACK_DOUBLE requires MPACK_FLOAT." |
| 1024 | #endif |
| 1025 | |
| 1026 | // If we don't have support for float or double, we poison the identifiers to |
| 1027 | // make sure we don't define anything related to them. |
| 1028 | #if MPACK_INTERNAL && defined(MPACK_UNIT_TESTS) && defined(__GNUC__) |
| 1029 | #if !MPACK_FLOAT |
| 1030 | #pragma GCC poison float |
| 1031 | #endif |
| 1032 | #if !MPACK_DOUBLE |
| 1033 | #pragma GCC poison double |
| 1034 | #endif |
| 1035 | #endif |
| 1036 | |
| 1037 | |
| 1038 | |
| 1039 | /* |
| 1040 | * extern C |
| 1041 | */ |
| 1042 | |
| 1043 | #ifdef __cplusplus |
| 1044 | #define MPACK_EXTERN_C_BEGIN extern "C" { |
| 1045 | #define MPACK_EXTERN_C_END } |
| 1046 | #else |
| 1047 | #define MPACK_EXTERN_C_BEGIN /*nothing*/ |
| 1048 | #define MPACK_EXTERN_C_END /*nothing*/ |
| 1049 | #endif |
| 1050 | |
| 1051 | |
| 1052 | |
| 1053 | /* |
| 1054 | * Warnings |
| 1055 | */ |
| 1056 | |
| 1057 | #if defined(__GNUC__) |
| 1058 | // Diagnostic push is not supported before GCC 4.6. |
| 1059 | #if defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 1060 | #define MPACK_SILENCE_WARNINGS_PUSH _Pragma ("GCC diagnostic push") |
| 1061 | #define MPACK_SILENCE_WARNINGS_POP _Pragma ("GCC diagnostic pop") |
| 1062 | #endif |
| 1063 | #elif defined(_MSC_VER) |
| 1064 | // To support VS2017 and earlier we need to use __pragma and not _Pragma |
| 1065 | #define MPACK_SILENCE_WARNINGS_PUSH __pragma(warning(push)) |
| 1066 | #define MPACK_SILENCE_WARNINGS_POP __pragma(warning(pop)) |
| 1067 | #endif |
| 1068 | |
| 1069 | #if defined(_MSC_VER) |
| 1070 | // These are a bunch of mostly useless warnings emitted under MSVC /W4, |
| 1071 | // some as a result of the expansion of macros. |
| 1072 | #define MPACK_SILENCE_WARNINGS_MSVC_W4 \ |
| 1073 | __pragma(warning(disable:4996)) /* _CRT_SECURE_NO_WARNINGS */ \ |
| 1074 | __pragma(warning(disable:4127)) /* comparison is constant */ \ |
| 1075 | __pragma(warning(disable:4702)) /* unreachable code */ \ |
| 1076 | __pragma(warning(disable:4310)) /* cast truncates constant value */ |
| 1077 | #else |
| 1078 | #define MPACK_SILENCE_WARNINGS_MSVC_W4 /*nothing*/ |
| 1079 | #endif |
| 1080 | |
| 1081 | /* GCC versions before 5.1 warn about defining a C99 non-static inline function |
| 1082 | * before declaring it (see issue #20). */ |
| 1083 | #if defined(__GNUC__) && !defined(__clang__) |
| 1084 | #if __GNUC__ < 5 || (__GNUC__ == 5 && __GNUC_MINOR__ < 1) |
| 1085 | #ifdef __cplusplus |
| 1086 | #define MPACK_SILENCE_WARNINGS_MISSING_PROTOTYPES \ |
| 1087 | _Pragma ("GCC diagnostic ignored \"-Wmissing-declarations\"") |
| 1088 | #else |
| 1089 | #define MPACK_SILENCE_WARNINGS_MISSING_PROTOTYPES \ |
| 1090 | _Pragma ("GCC diagnostic ignored \"-Wmissing-prototypes\"") |
| 1091 | #endif |
| 1092 | #endif |
| 1093 | #endif |
| 1094 | #ifndef MPACK_SILENCE_WARNINGS_MISSING_PROTOTYPES |
| 1095 | #define MPACK_SILENCE_WARNINGS_MISSING_PROTOTYPES /*nothing*/ |
| 1096 | #endif |
| 1097 | |
| 1098 | /* GCC versions before 4.8 warn about shadowing a function with a variable that |
| 1099 | * isn't a function or function pointer (like "index"). */ |
| 1100 | #if defined(__GNUC__) && !defined(__clang__) |
| 1101 | #if __GNUC__ == 4 && __GNUC_MINOR__ < 8 |
| 1102 | #define MPACK_SILENCE_WARNINGS_SHADOW \ |
| 1103 | _Pragma ("GCC diagnostic ignored \"-Wshadow\"") |
| 1104 | #endif |
| 1105 | #endif |
| 1106 | #ifndef MPACK_SILENCE_WARNINGS_SHADOW |
| 1107 | #define MPACK_SILENCE_WARNINGS_SHADOW /*nothing*/ |
| 1108 | #endif |
| 1109 | |
| 1110 | // On platforms with small size_t (e.g. AVR) we get type limits warnings where |
| 1111 | // we compare a size_t to e.g. MPACK_UINT32_MAX. |
| 1112 | #ifdef __AVR__ |
| 1113 | #define MPACK_SILENCE_WARNINGS_TYPE_LIMITS \ |
| 1114 | _Pragma ("GCC diagnostic ignored \"-Wtype-limits\"") |
| 1115 | #else |
| 1116 | #define MPACK_SILENCE_WARNINGS_TYPE_LIMITS /*nothing*/ |
| 1117 | #endif |
| 1118 | |
| 1119 | // MPack uses declarations after statements. This silences warnings about it |
| 1120 | // (e.g. when using MPack in a Linux kernel module.) |
| 1121 | #if defined(__GNUC__) && !defined(__cplusplus) |
| 1122 | #define MPACK_SILENCE_WARNINGS_DECLARATION_AFTER_STATEMENT \ |
| 1123 | _Pragma ("GCC diagnostic ignored \"-Wdeclaration-after-statement\"") |
| 1124 | #else |
| 1125 | #define MPACK_SILENCE_WARNINGS_DECLARATION_AFTER_STATEMENT /*nothing*/ |
| 1126 | #endif |
| 1127 | |
| 1128 | #ifdef MPACK_SILENCE_WARNINGS_PUSH |
| 1129 | // We only silence warnings if push/pop is supported, that way we aren't |
| 1130 | // silencing warnings in code that uses MPack. If your compiler doesn't |
| 1131 | // support push/pop silencing of warnings, you'll have to turn off |
| 1132 | // conflicting warnings manually. |
| 1133 | |
| 1134 | #define MPACK_SILENCE_WARNINGS_BEGIN \ |
| 1135 | MPACK_SILENCE_WARNINGS_PUSH \ |
| 1136 | MPACK_SILENCE_WARNINGS_MSVC_W4 \ |
| 1137 | MPACK_SILENCE_WARNINGS_MISSING_PROTOTYPES \ |
| 1138 | MPACK_SILENCE_WARNINGS_SHADOW \ |
| 1139 | MPACK_SILENCE_WARNINGS_TYPE_LIMITS \ |
| 1140 | MPACK_SILENCE_WARNINGS_DECLARATION_AFTER_STATEMENT |
| 1141 | |
| 1142 | #define MPACK_SILENCE_WARNINGS_END \ |
| 1143 | MPACK_SILENCE_WARNINGS_POP |
| 1144 | #else |
| 1145 | #define MPACK_SILENCE_WARNINGS_BEGIN /*nothing*/ |
| 1146 | #define MPACK_SILENCE_WARNINGS_END /*nothing*/ |
| 1147 | #endif |
| 1148 | |
| 1149 | MPACK_SILENCE_WARNINGS_BEGIN |
| 1150 | MPACK_EXTERN_C_BEGIN |
| 1151 | |
| 1152 | |
| 1153 | |
| 1154 | /* Miscellaneous helper macros */ |
| 1155 | |
| 1156 | #define MPACK_UNUSED(var) ((void)(var)) |
| 1157 | |
| 1158 | #define MPACK_STRINGIFY_IMPL(arg) #arg |
| 1159 | #define MPACK_STRINGIFY(arg) MPACK_STRINGIFY_IMPL(arg) |
| 1160 | |
| 1161 | // This is a workaround for MSVC's incorrect expansion of __VA_ARGS__. It |
| 1162 | // treats __VA_ARGS__ as a single preprocessor token when passed in the |
| 1163 | // argument list of another macro unless we use an outer wrapper to expand it |
| 1164 | // lexically first. (For safety/consistency we use this in all variadic macros |
| 1165 | // that don't ignore the variadic arguments regardless of whether __VA_ARGS__ |
| 1166 | // is passed to another macro.) |
| 1167 | // https://stackoverflow.com/a/32400131 |
| 1168 | #define MPACK_EXPAND(x) x |
| 1169 | |
| 1170 | // Extracts the first argument of a variadic macro list, where there might only |
| 1171 | // be one argument. |
| 1172 | #define (first, ...) first |
| 1173 | #define (...) MPACK_EXPAND(MPACK_EXTRACT_ARG0_IMPL( __VA_ARGS__ , ignored)) |
| 1174 | |
| 1175 | // Stringifies the first argument of a variadic macro list, where there might |
| 1176 | // only be one argument. |
| 1177 | #define MPACK_STRINGIFY_ARG0_impl(first, ...) #first |
| 1178 | #define MPACK_STRINGIFY_ARG0(...) MPACK_EXPAND(MPACK_STRINGIFY_ARG0_impl( __VA_ARGS__ , ignored)) |
| 1179 | |
| 1180 | |
| 1181 | |
| 1182 | /* |
| 1183 | * Definition of inline macros. |
| 1184 | * |
| 1185 | * MPack does not use static inline in header files; only one non-inline definition |
| 1186 | * of each function should exist in the final build. This can reduce the binary size |
| 1187 | * in cases where the compiler cannot or chooses not to inline a function. |
| 1188 | * The addresses of functions should also compare equal across translation units |
| 1189 | * regardless of whether they are declared inline. |
| 1190 | * |
| 1191 | * The above requirements mean that the declaration and definition of non-trivial |
| 1192 | * inline functions must be separated so that the definitions will only |
| 1193 | * appear when necessary. In addition, three different linkage models need |
| 1194 | * to be supported: |
| 1195 | * |
| 1196 | * - The C99 model, where a standalone function is emitted only if there is any |
| 1197 | * `extern inline` or non-`inline` declaration (including the definition itself) |
| 1198 | * |
| 1199 | * - The GNU model, where an `inline` definition emits a standalone function and an |
| 1200 | * `extern inline` definition does not, regardless of other declarations |
| 1201 | * |
| 1202 | * - The C++ model, where `inline` emits a standalone function with special |
| 1203 | * (COMDAT) linkage |
| 1204 | * |
| 1205 | * The macros below wrap up everything above. All inline functions defined in header |
| 1206 | * files have a single non-inline definition emitted in the compilation of |
| 1207 | * mpack-platform.c. All inline declarations and definitions use the same MPACK_INLINE |
| 1208 | * specification to simplify the rules on when standalone functions are emitted. |
| 1209 | * Inline functions in source files are defined MPACK_STATIC_INLINE. |
| 1210 | * |
| 1211 | * Additional reading: |
| 1212 | * http://www.greenend.org.uk/rjk/tech/inline.html |
| 1213 | */ |
| 1214 | |
| 1215 | #if defined(__cplusplus) |
| 1216 | // C++ rules |
| 1217 | // The linker will need COMDAT support to link C++ object files, |
| 1218 | // so we don't need to worry about emitting definitions from C++ |
| 1219 | // translation units. If mpack-platform.c (or the amalgamation) |
| 1220 | // is compiled as C, its definition will be used, otherwise a |
| 1221 | // C++ definition will be used, and no other C files will emit |
| 1222 | // a definition. |
| 1223 | #define MPACK_INLINE inline |
| 1224 | |
| 1225 | #elif defined(_MSC_VER) |
| 1226 | // MSVC 2013 always uses COMDAT linkage, but it doesn't treat 'inline' as a |
| 1227 | // keyword in C99 mode. (This appears to be fixed in a later version of |
| 1228 | // MSVC but we don't bother detecting it.) |
| 1229 | #define MPACK_INLINE __inline |
| 1230 | #define MPACK_STATIC_INLINE static __inline |
| 1231 | |
| 1232 | #elif defined(__GNUC__) && (defined(__GNUC_GNU_INLINE__) || \ |
| 1233 | (!defined(__GNUC_STDC_INLINE__) && !defined(__GNUC_GNU_INLINE__))) |
| 1234 | // GNU rules |
| 1235 | #if MPACK_EMIT_INLINE_DEFS |
| 1236 | #define MPACK_INLINE inline |
| 1237 | #else |
| 1238 | #define MPACK_INLINE extern inline |
| 1239 | #endif |
| 1240 | |
| 1241 | #elif defined(__TINYC__) |
| 1242 | // tcc ignores the inline keyword, so we have to use static inline. We |
| 1243 | // issue a warning to make sure you are aware. You can define the below |
| 1244 | // macro to disable the warning. Hopefully this will be fixed soon: |
| 1245 | // https://lists.nongnu.org/archive/html/tinycc-devel/2019-06/msg00000.html |
| 1246 | #ifndef MPACK_DISABLE_TINYC_INLINE_WARNING |
| 1247 | #warning "Single-definition inline is not supported by tcc. All inlines will be static. Define MPACK_DISABLE_TINYC_INLINE_WARNING to disable this warning." |
| 1248 | #endif |
| 1249 | #define MPACK_INLINE static inline |
| 1250 | |
| 1251 | #else |
| 1252 | // C99 rules |
| 1253 | #if MPACK_EMIT_INLINE_DEFS |
| 1254 | #define MPACK_INLINE extern inline |
| 1255 | #else |
| 1256 | #define MPACK_INLINE inline |
| 1257 | #endif |
| 1258 | #endif |
| 1259 | |
| 1260 | #ifndef MPACK_STATIC_INLINE |
| 1261 | #define MPACK_STATIC_INLINE static inline |
| 1262 | #endif |
| 1263 | |
| 1264 | #ifdef MPACK_OPTIMIZE_FOR_SPEED |
| 1265 | #error "You should define MPACK_OPTIMIZE_FOR_SIZE, not MPACK_OPTIMIZE_FOR_SPEED." |
| 1266 | #endif |
| 1267 | |
| 1268 | |
| 1269 | |
| 1270 | /* |
| 1271 | * Prevent inlining |
| 1272 | * |
| 1273 | * When a function is only used once, it is almost always inlined |
| 1274 | * automatically. This can cause poor instruction cache usage because a |
| 1275 | * function that should rarely be called (such as buffer exhaustion handling) |
| 1276 | * will get inlined into the middle of a hot code path. |
| 1277 | */ |
| 1278 | |
| 1279 | #if !MPACK_NO_BUILTINS |
| 1280 | #if defined(_MSC_VER) |
| 1281 | #define MPACK_NOINLINE __declspec(noinline) |
| 1282 | #elif defined(__GNUC__) || defined(__clang__) |
| 1283 | #define MPACK_NOINLINE __attribute__((__noinline__)) |
| 1284 | #endif |
| 1285 | #endif |
| 1286 | #ifndef MPACK_NOINLINE |
| 1287 | #define MPACK_NOINLINE /* nothing */ |
| 1288 | #endif |
| 1289 | |
| 1290 | |
| 1291 | |
| 1292 | /* restrict */ |
| 1293 | |
| 1294 | // We prefer the builtins even though e.g. MSVC's __restrict may not have |
| 1295 | // exactly the same behaviour as the proper C99 restrict keyword because the |
| 1296 | // builtins work in C++, so using the same keyword in both C and C++ prevents |
| 1297 | // any incompatibilities when using MPack compiled as C in C++ code. |
| 1298 | #if !MPACK_NO_BUILTINS |
| 1299 | #if defined(__GNUC__) |
| 1300 | #define MPACK_RESTRICT __restrict__ |
| 1301 | #elif defined(_MSC_VER) |
| 1302 | #define MPACK_RESTRICT __restrict |
| 1303 | #endif |
| 1304 | #endif |
| 1305 | |
| 1306 | #ifndef MPACK_RESTRICT |
| 1307 | #ifdef __cplusplus |
| 1308 | #define MPACK_RESTRICT /* nothing, unavailable in C++ */ |
| 1309 | #endif |
| 1310 | #endif |
| 1311 | |
| 1312 | #ifndef MPACK_RESTRICT |
| 1313 | #ifdef _MSC_VER |
| 1314 | // MSVC 2015 apparently doesn't properly support the restrict keyword |
| 1315 | // in C. We're using builtins above which do work on 2015, but when |
| 1316 | // MPACK_NO_BUILTINS is enabled we can't use it. |
| 1317 | #if _MSC_VER < 1910 |
| 1318 | #define MPACK_RESTRICT /*nothing*/ |
| 1319 | #endif |
| 1320 | #endif |
| 1321 | #endif |
| 1322 | |
| 1323 | #ifndef MPACK_RESTRICT |
| 1324 | #define MPACK_RESTRICT restrict /* required in C99 */ |
| 1325 | #endif |
| 1326 | |
| 1327 | |
| 1328 | |
| 1329 | /* Some compiler-specific keywords and builtins */ |
| 1330 | |
| 1331 | #if !MPACK_NO_BUILTINS |
| 1332 | #if defined(__GNUC__) || defined(__clang__) |
| 1333 | #define MPACK_UNREACHABLE __builtin_unreachable() |
| 1334 | #define MPACK_NORETURN(fn) fn __attribute__((__noreturn__)) |
| 1335 | #elif defined(_MSC_VER) |
| 1336 | #define MPACK_UNREACHABLE __assume(0) |
| 1337 | #define MPACK_NORETURN(fn) __declspec(noreturn) fn |
| 1338 | #endif |
| 1339 | #endif |
| 1340 | |
| 1341 | #ifndef MPACK_UNREACHABLE |
| 1342 | #define MPACK_UNREACHABLE ((void)0) |
| 1343 | #endif |
| 1344 | #ifndef MPACK_NORETURN |
| 1345 | #define MPACK_NORETURN(fn) fn |
| 1346 | #endif |
| 1347 | |
| 1348 | |
| 1349 | |
| 1350 | /* |
| 1351 | * Likely/unlikely |
| 1352 | * |
| 1353 | * These should only really be used when a branch is taken (or not taken) less |
| 1354 | * than 1/1000th of the time. Buffer flush checks when writing very small |
| 1355 | * elements are a good example. |
| 1356 | */ |
| 1357 | |
| 1358 | #if !MPACK_NO_BUILTINS |
| 1359 | #if defined(__GNUC__) || defined(__clang__) |
| 1360 | #ifndef MPACK_LIKELY |
| 1361 | #define MPACK_LIKELY(x) __builtin_expect((x),1) |
| 1362 | #endif |
| 1363 | #ifndef MPACK_UNLIKELY |
| 1364 | #define MPACK_UNLIKELY(x) __builtin_expect((x),0) |
| 1365 | #endif |
| 1366 | #endif |
| 1367 | #endif |
| 1368 | |
| 1369 | #ifndef MPACK_LIKELY |
| 1370 | #define MPACK_LIKELY(x) (x) |
| 1371 | #endif |
| 1372 | #ifndef MPACK_UNLIKELY |
| 1373 | #define MPACK_UNLIKELY(x) (x) |
| 1374 | #endif |
| 1375 | |
| 1376 | |
| 1377 | |
| 1378 | /* alignof */ |
| 1379 | |
| 1380 | #ifndef MPACK_ALIGNOF |
| 1381 | #if defined(__STDC_VERSION__) |
| 1382 | #if __STDC_VERSION__ >= 201112L |
| 1383 | #define MPACK_ALIGNOF(T) (_Alignof(T)) |
| 1384 | #endif |
| 1385 | #endif |
| 1386 | #endif |
| 1387 | |
| 1388 | #ifndef MPACK_ALIGNOF |
| 1389 | #if defined(__cplusplus) |
| 1390 | #if __cplusplus >= 201103L |
| 1391 | #define MPACK_ALIGNOF(T) (alignof(T)) |
| 1392 | #endif |
| 1393 | #endif |
| 1394 | #endif |
| 1395 | |
| 1396 | #ifndef MPACK_ALIGNOF |
| 1397 | #if defined(__GNUC__) && !defined(MPACK_NO_BUILTINS) |
| 1398 | #if defined(__clang__) || __GNUC__ >= 4 |
| 1399 | #define MPACK_ALIGNOF(T) (__alignof__(T)) |
| 1400 | #endif |
| 1401 | #endif |
| 1402 | #endif |
| 1403 | |
| 1404 | #ifndef MPACK_ALIGNOF |
| 1405 | #ifdef _MSC_VER |
| 1406 | #define MPACK_ALIGNOF(T) __alignof(T) |
| 1407 | #endif |
| 1408 | #endif |
| 1409 | |
| 1410 | // MPACK_ALIGNOF may not exist, in which case a workaround is used. |
| 1411 | |
| 1412 | |
| 1413 | |
| 1414 | /* Static assert */ |
| 1415 | |
| 1416 | #ifndef MPACK_STATIC_ASSERT |
| 1417 | #if defined(__cplusplus) |
| 1418 | #if __cplusplus >= 201103L |
| 1419 | #define MPACK_STATIC_ASSERT static_assert |
| 1420 | #endif |
| 1421 | #elif defined(__STDC_VERSION__) |
| 1422 | #if __STDC_VERSION__ >= 201112L |
| 1423 | #define MPACK_STATIC_ASSERT _Static_assert |
| 1424 | #endif |
| 1425 | #endif |
| 1426 | #endif |
| 1427 | |
| 1428 | #if !MPACK_NO_BUILTINS |
| 1429 | #ifndef MPACK_STATIC_ASSERT |
| 1430 | #if defined(__has_feature) |
| 1431 | #if __has_feature(cxx_static_assert) |
| 1432 | #define MPACK_STATIC_ASSERT static_assert |
| 1433 | #elif __has_feature(c_static_assert) |
| 1434 | #define MPACK_STATIC_ASSERT _Static_assert |
| 1435 | #endif |
| 1436 | #endif |
| 1437 | #endif |
| 1438 | |
| 1439 | #ifndef MPACK_STATIC_ASSERT |
| 1440 | #if defined(__GNUC__) |
| 1441 | /* Diagnostic push is not supported before GCC 4.6. */ |
| 1442 | #if defined(__clang__) || __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) |
| 1443 | #ifndef __cplusplus |
| 1444 | #if defined(__clang__) || __GNUC__ >= 5 |
| 1445 | #define MPACK_IGNORE_PEDANTIC "GCC diagnostic ignored \"-Wpedantic\"" |
| 1446 | #else |
| 1447 | #define MPACK_IGNORE_PEDANTIC "GCC diagnostic ignored \"-pedantic\"" |
| 1448 | #endif |
| 1449 | #define MPACK_STATIC_ASSERT(expr, str) do { \ |
| 1450 | _Pragma ("GCC diagnostic push") \ |
| 1451 | _Pragma (MPACK_IGNORE_PEDANTIC) \ |
| 1452 | _Pragma ("GCC diagnostic ignored \"-Wc++-compat\"") \ |
| 1453 | _Static_assert(expr, str); \ |
| 1454 | _Pragma ("GCC diagnostic pop") \ |
| 1455 | } while (0) |
| 1456 | #endif |
| 1457 | #endif |
| 1458 | #endif |
| 1459 | #endif |
| 1460 | |
| 1461 | #ifndef MPACK_STATIC_ASSERT |
| 1462 | #ifdef _MSC_VER |
| 1463 | #if _MSC_VER >= 1600 |
| 1464 | #define MPACK_STATIC_ASSERT static_assert |
| 1465 | #endif |
| 1466 | #endif |
| 1467 | #endif |
| 1468 | #endif |
| 1469 | |
| 1470 | #ifndef MPACK_STATIC_ASSERT |
| 1471 | #define MPACK_STATIC_ASSERT(expr, str) (MPACK_UNUSED(sizeof(char[1 - 2*!(expr)]))) |
| 1472 | #endif |
| 1473 | |
| 1474 | |
| 1475 | |
| 1476 | /* _Generic */ |
| 1477 | |
| 1478 | #ifndef MPACK_HAS_GENERIC |
| 1479 | #if defined(__clang__) && defined(__has_feature) |
| 1480 | // With Clang we can test for _Generic support directly |
| 1481 | // and ignore C/C++ version |
| 1482 | #if __has_feature(c_generic_selections) |
| 1483 | #define MPACK_HAS_GENERIC 1 |
| 1484 | #else |
| 1485 | #define MPACK_HAS_GENERIC 0 |
| 1486 | #endif |
| 1487 | #endif |
| 1488 | #endif |
| 1489 | |
| 1490 | #ifndef MPACK_HAS_GENERIC |
| 1491 | #if defined(__STDC_VERSION__) |
| 1492 | #if __STDC_VERSION__ >= 201112L |
| 1493 | #if defined(__GNUC__) && !defined(__clang__) |
| 1494 | // GCC does not have full C11 support in GCC 4.7 and 4.8 |
| 1495 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9) |
| 1496 | #define MPACK_HAS_GENERIC 1 |
| 1497 | #endif |
| 1498 | #else |
| 1499 | // We hope other compilers aren't lying about C11/_Generic support |
| 1500 | #define MPACK_HAS_GENERIC 1 |
| 1501 | #endif |
| 1502 | #endif |
| 1503 | #endif |
| 1504 | #endif |
| 1505 | |
| 1506 | #ifndef MPACK_HAS_GENERIC |
| 1507 | #define MPACK_HAS_GENERIC 0 |
| 1508 | #endif |
| 1509 | |
| 1510 | |
| 1511 | |
| 1512 | /* |
| 1513 | * Finite Math |
| 1514 | * |
| 1515 | * -ffinite-math-only, included in -ffast-math, breaks functions that |
| 1516 | * that check for non-finite real values such as isnan() and isinf(). |
| 1517 | * |
| 1518 | * We should use this to trap errors when reading data that contains |
| 1519 | * non-finite reals. This isn't currently implemented. |
| 1520 | */ |
| 1521 | |
| 1522 | #ifndef MPACK_FINITE_MATH |
| 1523 | #if defined(__FINITE_MATH_ONLY__) && __FINITE_MATH_ONLY__ |
| 1524 | #define MPACK_FINITE_MATH 1 |
| 1525 | #endif |
| 1526 | #endif |
| 1527 | |
| 1528 | #ifndef MPACK_FINITE_MATH |
| 1529 | #define MPACK_FINITE_MATH 0 |
| 1530 | #endif |
| 1531 | |
| 1532 | |
| 1533 | |
| 1534 | /* |
| 1535 | * Endianness checks |
| 1536 | * |
| 1537 | * These define MPACK_NHSWAP*() which swap network<->host byte |
| 1538 | * order when needed. |
| 1539 | * |
| 1540 | * We leave them undefined if we can't determine the endianness |
| 1541 | * at compile-time, in which case we fall back to bit-shifts. |
| 1542 | * |
| 1543 | * See the notes in mpack-common.h. |
| 1544 | */ |
| 1545 | |
| 1546 | #if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__) |
| 1547 | #if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 1548 | #define MPACK_NHSWAP16(x) (x) |
| 1549 | #define MPACK_NHSWAP32(x) (x) |
| 1550 | #define MPACK_NHSWAP64(x) (x) |
| 1551 | #elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 1552 | |
| 1553 | #if !MPACK_NO_BUILTINS |
| 1554 | #if defined(__clang__) |
| 1555 | #ifdef __has_builtin |
| 1556 | // Unlike the GCC builtins, the bswap builtins in Clang |
| 1557 | // significantly improve ARM performance. |
| 1558 | #if __has_builtin(__builtin_bswap16) |
| 1559 | #define MPACK_NHSWAP16(x) __builtin_bswap16(x) |
| 1560 | #endif |
| 1561 | #if __has_builtin(__builtin_bswap32) |
| 1562 | #define MPACK_NHSWAP32(x) __builtin_bswap32(x) |
| 1563 | #endif |
| 1564 | #if __has_builtin(__builtin_bswap64) |
| 1565 | #define MPACK_NHSWAP64(x) __builtin_bswap64(x) |
| 1566 | #endif |
| 1567 | #endif |
| 1568 | |
| 1569 | #elif defined(__GNUC__) |
| 1570 | |
| 1571 | // The GCC bswap builtins are apparently poorly optimized on older |
| 1572 | // versions of GCC, so we set a minimum version here just in case. |
| 1573 | // http://hardwarebug.org/2010/01/14/beware-the-builtins/ |
| 1574 | |
| 1575 | #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5) |
| 1576 | #define MPACK_NHSWAP64(x) __builtin_bswap64(x) |
| 1577 | #endif |
| 1578 | |
| 1579 | // __builtin_bswap16() was not implemented on all platforms |
| 1580 | // until GCC 4.8.0: |
| 1581 | // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52624 |
| 1582 | // |
| 1583 | // The 16- and 32-bit versions in GCC significantly reduce performance |
| 1584 | // on ARM with little effect on code size so we don't use them. |
| 1585 | |
| 1586 | #endif |
| 1587 | #endif |
| 1588 | #endif |
| 1589 | |
| 1590 | #elif defined(_MSC_VER) && defined(_WIN32) && MPACK_STDLIB && !MPACK_NO_BUILTINS |
| 1591 | |
| 1592 | // On Windows, we assume x86 and x86_64 are always little-endian. |
| 1593 | // We make no assumptions about ARM even though all current |
| 1594 | // Windows Phone devices are little-endian in case Microsoft's |
| 1595 | // compiler is ever used with a big-endian ARM device. |
| 1596 | |
| 1597 | // These are functions in <stdlib.h> so we depend on MPACK_STDLIB. |
| 1598 | // It's not clear if these are actually faster than just doing the |
| 1599 | // swap manually; maybe we shouldn't bother with this. |
| 1600 | |
| 1601 | #if defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64) |
| 1602 | #define MPACK_NHSWAP16(x) _byteswap_ushort(x) |
| 1603 | #define MPACK_NHSWAP32(x) _byteswap_ulong(x) |
| 1604 | #define MPACK_NHSWAP64(x) _byteswap_uint64(x) |
| 1605 | #endif |
| 1606 | |
| 1607 | #endif |
| 1608 | |
| 1609 | #if defined(__FLOAT_WORD_ORDER__) && defined(__BYTE_ORDER__) |
| 1610 | |
| 1611 | // We check where possible that the float byte order matches the |
| 1612 | // integer byte order. This is extremely unlikely to fail, but |
| 1613 | // we check anyway just in case. |
| 1614 | // |
| 1615 | // (The static assert is placed in float/double encoders instead |
| 1616 | // of here because our static assert fallback doesn't work at |
| 1617 | // file scope) |
| 1618 | |
| 1619 | #define MPACK_CHECK_FLOAT_ORDER() \ |
| 1620 | MPACK_STATIC_ASSERT(__FLOAT_WORD_ORDER__ == __BYTE_ORDER__, \ |
| 1621 | "float byte order does not match int byte order! float/double " \ |
| 1622 | "encoding is not properly implemented on this platform.") |
| 1623 | |
| 1624 | #endif |
| 1625 | |
| 1626 | #ifndef MPACK_CHECK_FLOAT_ORDER |
| 1627 | #define MPACK_CHECK_FLOAT_ORDER() /* nothing */ |
| 1628 | #endif |
| 1629 | |
| 1630 | |
| 1631 | /* |
| 1632 | * Here we define mpack_assert() and mpack_break(). They both work like a normal |
| 1633 | * assertion function in debug mode, causing a trap or abort. However, on some platforms |
| 1634 | * you can safely resume execution from mpack_break(), whereas mpack_assert() is |
| 1635 | * always fatal. |
| 1636 | * |
| 1637 | * In release mode, mpack_assert() is converted to an assurance to the compiler |
| 1638 | * that the expression cannot be false (via e.g. __assume() or __builtin_unreachable()) |
| 1639 | * to improve optimization where supported. There is thus no point in "safely" handling |
| 1640 | * the case of this being false. Writing mpack_assert(0) rarely makes sense (except |
| 1641 | * possibly as a default handler in a switch) since the compiler will throw away any |
| 1642 | * code after it. If at any time an mpack_assert() is not true, the behaviour is |
| 1643 | * undefined. This also means the expression is evaluated even in release. |
| 1644 | * |
| 1645 | * mpack_break() on the other hand is compiled to nothing in release. It is |
| 1646 | * used in situations where we want to highlight a programming error as early as |
| 1647 | * possible (in the debugger), but we still handle the situation safely if it |
| 1648 | * happens in release to avoid producing incorrect results (such as in |
| 1649 | * MPACK_WRITE_TRACKING.) It does not take an expression to test because it |
| 1650 | * belongs in a safe-handling block after its failing condition has been tested. |
| 1651 | * |
| 1652 | * If stdio is available, we can add a format string describing the error, and |
| 1653 | * on some compilers we can declare it noreturn to get correct results from static |
| 1654 | * analysis tools. Note that the format string and arguments are not evaluated unless |
| 1655 | * the assertion is hit. |
| 1656 | * |
| 1657 | * Note that any arguments to mpack_assert() beyond the first are only evaluated |
| 1658 | * if the expression is false (and are never evaluated in release.) |
| 1659 | * |
| 1660 | * mpack_assert_fail() and mpack_break_hit() are defined separately |
| 1661 | * because assert is noreturn and break isn't. This distinction is very |
| 1662 | * important for static analysis tools to give correct results. |
| 1663 | */ |
| 1664 | |
| 1665 | #if MPACK_DEBUG |
| 1666 | MPACK_NORETURN(void mpack_assert_fail_wrapper(const char* message)); |
| 1667 | #if MPACK_STDIO |
| 1668 | MPACK_NORETURN(void mpack_assert_fail_format(const char* format, ...)); |
| 1669 | #define mpack_assert_fail_at(line, file, exprstr, format, ...) \ |
| 1670 | MPACK_EXPAND(mpack_assert_fail_format("mpack assertion failed at " file ":" #line "\n%s\n" format, exprstr, __VA_ARGS__)) |
| 1671 | #else |
| 1672 | #define mpack_assert_fail_at(line, file, exprstr, format, ...) \ |
| 1673 | mpack_assert_fail_wrapper("mpack assertion failed at " file ":" #line "\n" exprstr "\n") |
| 1674 | #endif |
| 1675 | |
| 1676 | #define mpack_assert_fail_pos(line, file, exprstr, expr, ...) \ |
| 1677 | MPACK_EXPAND(mpack_assert_fail_at(line, file, exprstr, __VA_ARGS__)) |
| 1678 | |
| 1679 | // This contains a workaround to the pedantic C99 requirement of having at |
| 1680 | // least one argument to a variadic macro. The first argument is the |
| 1681 | // boolean expression, the optional second argument (if provided) must be a |
| 1682 | // literal format string, and any additional arguments are the format |
| 1683 | // argument list. |
| 1684 | // |
| 1685 | // Unfortunately this means macros are expanded in the expression before it |
| 1686 | // gets stringified. I haven't found a workaround to this. |
| 1687 | // |
| 1688 | // This adds two unused arguments to the format argument list when a |
| 1689 | // format string is provided, so this would complicate the use of |
| 1690 | // -Wformat and __attribute__((__format__)) on mpack_assert_fail_format() |
| 1691 | // if we ever bothered to implement it. |
| 1692 | #define mpack_assert(...) \ |
| 1693 | MPACK_EXPAND(((!(MPACK_EXTRACT_ARG0(__VA_ARGS__))) ? \ |
| 1694 | mpack_assert_fail_pos(__LINE__, __FILE__, MPACK_STRINGIFY_ARG0(__VA_ARGS__) , __VA_ARGS__ , "", NULL) : \ |
| 1695 | (void)0)) |
| 1696 | |
| 1697 | void mpack_break_hit(const char* message); |
| 1698 | #if MPACK_STDIO |
| 1699 | void mpack_break_hit_format(const char* format, ...); |
| 1700 | #define mpack_break_hit_at(line, file, ...) \ |
| 1701 | MPACK_EXPAND(mpack_break_hit_format("mpack breakpoint hit at " file ":" #line "\n" __VA_ARGS__)) |
| 1702 | #else |
| 1703 | #define mpack_break_hit_at(line, file, ...) \ |
| 1704 | mpack_break_hit("mpack breakpoint hit at " file ":" #line ) |
| 1705 | #endif |
| 1706 | #define mpack_break_hit_pos(line, file, ...) MPACK_EXPAND(mpack_break_hit_at(line, file, __VA_ARGS__)) |
| 1707 | #define mpack_break(...) MPACK_EXPAND(mpack_break_hit_pos(__LINE__, __FILE__, __VA_ARGS__)) |
| 1708 | #else |
| 1709 | #define mpack_assert(...) \ |
| 1710 | (MPACK_EXPAND((!(MPACK_EXTRACT_ARG0(__VA_ARGS__))) ? \ |
| 1711 | (MPACK_UNREACHABLE, (void)0) : \ |
| 1712 | (void)0)) |
| 1713 | #define mpack_break(...) ((void)0) |
| 1714 | #endif |
| 1715 | |
| 1716 | |
| 1717 | |
| 1718 | // make sure we don't use the stdlib directly during development |
| 1719 | #if MPACK_STDLIB && defined(MPACK_UNIT_TESTS) && MPACK_INTERNAL && defined(__GNUC__) |
| 1720 | #undef memcmp |
| 1721 | #undef memcpy |
| 1722 | #undef memmove |
| 1723 | #undef memset |
| 1724 | #undef strlen |
| 1725 | #undef malloc |
| 1726 | #undef calloc |
| 1727 | #undef realloc |
| 1728 | #undef free |
| 1729 | #pragma GCC poison memcmp |
| 1730 | #pragma GCC poison memcpy |
| 1731 | #pragma GCC poison memmove |
| 1732 | #pragma GCC poison memset |
| 1733 | #pragma GCC poison strlen |
| 1734 | #pragma GCC poison malloc |
| 1735 | #pragma GCC poison calloc |
| 1736 | #pragma GCC poison realloc |
| 1737 | #pragma GCC poison free |
| 1738 | #endif |
| 1739 | |
| 1740 | |
| 1741 | |
| 1742 | // If we don't have these stdlib functions, we need to define them ourselves. |
| 1743 | // Either way we give them a lowercase name to make the code a bit nicer. |
| 1744 | |
| 1745 | #ifdef MPACK_MEMCMP |
| 1746 | #define mpack_memcmp MPACK_MEMCMP |
| 1747 | #else |
| 1748 | int mpack_memcmp(const void* s1, const void* s2, size_t n); |
| 1749 | #endif |
| 1750 | |
| 1751 | #ifdef MPACK_MEMCPY |
| 1752 | #define mpack_memcpy MPACK_MEMCPY |
| 1753 | #else |
| 1754 | void* mpack_memcpy(void* MPACK_RESTRICT s1, const void* MPACK_RESTRICT s2, size_t n); |
| 1755 | #endif |
| 1756 | |
| 1757 | #ifdef MPACK_MEMMOVE |
| 1758 | #define mpack_memmove MPACK_MEMMOVE |
| 1759 | #else |
| 1760 | void* mpack_memmove(void* s1, const void* s2, size_t n); |
| 1761 | #endif |
| 1762 | |
| 1763 | #ifdef MPACK_MEMSET |
| 1764 | #define mpack_memset MPACK_MEMSET |
| 1765 | #else |
| 1766 | void* mpack_memset(void* s, int c, size_t n); |
| 1767 | #endif |
| 1768 | |
| 1769 | #ifdef MPACK_STRLEN |
| 1770 | #define mpack_strlen MPACK_STRLEN |
| 1771 | #else |
| 1772 | size_t mpack_strlen(const char* s); |
| 1773 | #endif |
| 1774 | |
| 1775 | |
| 1776 | |
| 1777 | #if MPACK_STDIO |
| 1778 | #if defined(WIN32) |
| 1779 | #define mpack_snprintf _snprintf |
| 1780 | #else |
| 1781 | #define mpack_snprintf snprintf |
| 1782 | #endif |
| 1783 | #endif |
| 1784 | |
| 1785 | |
| 1786 | |
| 1787 | /* Debug logging */ |
| 1788 | #if 0 |
| 1789 | #include <stdio.h> |
| 1790 | #define mpack_log(...) (MPACK_EXPAND(printf(__VA_ARGS__)), fflush(stdout)) |
| 1791 | #else |
| 1792 | #define mpack_log(...) ((void)0) |
| 1793 | #endif |
| 1794 | |
| 1795 | |
| 1796 | |
| 1797 | /* Make sure our configuration makes sense */ |
| 1798 | #ifndef MPACK_MALLOC |
| 1799 | #if MPACK_STDIO |
| 1800 | #error "MPACK_STDIO requires preprocessor definitions for MPACK_MALLOC and MPACK_FREE." |
| 1801 | #endif |
| 1802 | #if MPACK_READ_TRACKING |
| 1803 | #error "MPACK_READ_TRACKING requires preprocessor definitions for MPACK_MALLOC and MPACK_FREE." |
| 1804 | #endif |
| 1805 | #if MPACK_WRITE_TRACKING |
| 1806 | #error "MPACK_WRITE_TRACKING requires preprocessor definitions for MPACK_MALLOC and MPACK_FREE." |
| 1807 | #endif |
| 1808 | #endif |
| 1809 | |
| 1810 | |
| 1811 | |
| 1812 | /* Implement realloc if unavailable */ |
| 1813 | #ifdef MPACK_MALLOC |
| 1814 | #ifdef MPACK_REALLOC |
| 1815 | MPACK_INLINE void* mpack_realloc(void* old_ptr, size_t used_size, size_t new_size) { |
| 1816 | MPACK_UNUSED(used_size); |
| 1817 | return MPACK_REALLOC(old_ptr, new_size); |
| 1818 | } |
| 1819 | #else |
| 1820 | void* mpack_realloc(void* old_ptr, size_t used_size, size_t new_size); |
| 1821 | #endif |
| 1822 | #endif |
| 1823 | |
| 1824 | |
| 1825 | |
| 1826 | /** @endcond */ |
| 1827 | /** |
| 1828 | * @} |
| 1829 | */ |
| 1830 | |
| 1831 | MPACK_EXTERN_C_END |
| 1832 | MPACK_SILENCE_WARNINGS_END |
| 1833 | |
| 1834 | #endif |
| 1835 | |