-
-
Notifications
You must be signed in to change notification settings - Fork 173
/
mpack.h
7152 lines (6291 loc) · 224 KB
/
mpack.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* The MIT License (MIT)
*
* Copyright (c) 2015-2018 Nicholas Fraser
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
/*
* This is the MPack 1.0 amalgamation package.
*
* http://github.com/ludocode/mpack
*/
#ifndef MPACK_H
#define MPACK_H 1
#define MPACK_AMALGAMATED 1
#define MPACK_RELEASE_VERSION 1
#if defined(MPACK_HAS_CONFIG) && MPACK_HAS_CONFIG
#include "mpack-config.h"
#endif
/* mpack/mpack-defaults.h.h */
/**
* @name Features
* @{
*/
/**
* @def MPACK_READER
*
* Enables compilation of the base Tag Reader.
*/
#ifndef MPACK_READER
#define MPACK_READER 1
#endif
/**
* @def MPACK_EXPECT
*
* Enables compilation of the static Expect API.
*/
#ifndef MPACK_EXPECT
#define MPACK_EXPECT 1
#endif
/**
* @def MPACK_NODE
*
* Enables compilation of the dynamic Node API.
*/
#ifndef MPACK_NODE
#define MPACK_NODE 1
#endif
/**
* @def MPACK_WRITER
*
* Enables compilation of the Writer.
*/
#ifndef MPACK_WRITER
#define MPACK_WRITER 1
#endif
/**
* @def MPACK_COMPATIBILITY
*
* Enables compatibility features for reading and writing older
* versions of MessagePack.
*
* This is disabled by default. When disabled, the behaviour is equivalent to
* using the default version, @ref mpack_version_current.
*
* Enable this if you need to interoperate with applications or data that do
* not support the new (v5) MessagePack spec. See the section on v4
* compatibility in @ref docs/protocol.md for more information.
*/
#ifndef MPACK_COMPATIBILITY
#define MPACK_COMPATIBILITY 0
#endif
/**
* @def MPACK_EXTENSIONS
*
* Enables the use of extension types.
*
* This is disabled by default. Define it to 1 to enable it. If disabled,
* functions to read and write extensions will not exist, and any occurrence of
* extension types in parsed messages will flag @ref mpack_error_invalid.
*
* MPack discourages the use of extension types. See the section on extension
* types in @ref docs/protocol.md for more information.
*/
#ifndef MPACK_EXTENSIONS
#define MPACK_EXTENSIONS 0
#endif
/**
* @}
*/
/**
* @name Dependencies
* @{
*/
/**
* @def MPACK_HAS_CONFIG
*
* Enables the use of an @c mpack-config.h configuration file for MPack.
* This file must be in the same folder as @c mpack.h, or it must be
* available from your project's include paths.
*/
// This goes in your project settings.
/**
* @def MPACK_STDLIB
*
* Enables the use of C stdlib. This allows the library to use malloc
* for debugging and in allocation helpers.
*/
#ifndef MPACK_STDLIB
#define MPACK_STDLIB 1
#endif
/**
* @def MPACK_STDIO
*
* Enables the use of C stdio. This adds helpers for easily
* reading/writing C files and makes debugging easier.
*/
#ifndef MPACK_STDIO
#define MPACK_STDIO 1
#endif
/**
* @}
*/
/**
* @name System Functions
* @{
*/
/**
* @def MPACK_MALLOC
*
* Defines the memory allocation function used by MPack. This is used by
* helpers for automatically allocating data the correct size, and for
* debugging functions. If this macro is undefined, the allocation helpers
* will not be compiled.
*
* The default is @c malloc() if @ref MPACK_STDLIB is enabled.
*/
/**
* @def MPACK_FREE
*
* Defines the memory free function used by MPack. This is used by helpers
* for automatically allocating data the correct size. If this macro is
* undefined, the allocation helpers will not be compiled.
*
* The default is @c free() if @ref MPACK_MALLOC has not been customized and
* @ref MPACK_STDLIB is enabled.
*/
/**
* @def MPACK_REALLOC
*
* Defines the realloc function used by MPack. It is used by growable
* buffers to resize more efficiently.
*
* The default is @c realloc() if @ref MPACK_MALLOC has not been customized and
* @ref MPACK_STDLIB is enabled.
*
* This is optional, even when @ref MPACK_MALLOC is used. If @ref MPACK_MALLOC is
* set and @ref MPACK_REALLOC is not, @ref MPACK_MALLOC is used with a simple copy
* to grow buffers.
*/
#if defined(MPACK_STDLIB) && MPACK_STDLIB && !defined(MPACK_MALLOC)
#define MPACK_MALLOC malloc
#define MPACK_REALLOC realloc
#define MPACK_FREE free
#endif
/**
* @}
*/
/**
* @name Debugging Options
*/
/**
* @def MPACK_DEBUG
*
* Enables debug features. You may want to wrap this around your
* own debug preprocs. By default, this is enabled if @c DEBUG or @c _DEBUG
* are defined. (@c NDEBUG is not used since it is allowed to have
* different values in different translation units.)
*/
#if !defined(MPACK_DEBUG) && (defined(DEBUG) || defined(_DEBUG))
#define MPACK_DEBUG 1
#endif
/**
* @def MPACK_STRINGS
*
* Enables descriptive error and type strings.
*
* This can be turned off (by defining it to 0) to maximize space savings
* on embedded devices. If this is disabled, string functions such as
* mpack_error_to_string() and mpack_type_to_string() return an empty string.
*/
#ifndef MPACK_STRINGS
#define MPACK_STRINGS 1
#endif
/**
* Set this to 1 to implement a custom @ref mpack_assert_fail() function.
* See the documentation on @ref mpack_assert_fail() for details.
*
* Asserts are only used when @ref MPACK_DEBUG is enabled, and can be
* triggered by bugs in MPack or bugs due to incorrect usage of MPack.
*/
#ifndef MPACK_CUSTOM_ASSERT
#define MPACK_CUSTOM_ASSERT 0
#endif
/**
* @def MPACK_READ_TRACKING
*
* Enables compound type size tracking for readers. This ensures that the
* correct number of elements or bytes are read from a compound type.
*
* This is enabled by default in debug builds (provided a @c malloc() is
* available.)
*/
#if !defined(MPACK_READ_TRACKING) && \
defined(MPACK_DEBUG) && MPACK_DEBUG && \
defined(MPACK_READER) && MPACK_READER && \
defined(MPACK_MALLOC)
#define MPACK_READ_TRACKING 1
#endif
/**
* @def MPACK_WRITE_TRACKING
*
* Enables compound type size tracking for writers. This ensures that the
* correct number of elements or bytes are written in a compound type.
*
* Note that without write tracking enabled, it is possible for buggy code
* to emit invalid MessagePack without flagging an error by writing the wrong
* number of elements or bytes in a compound type. With tracking enabled,
* MPack will catch such errors and break on the offending line of code.
*
* This is enabled by default in debug builds (provided a @c malloc() is
* available.)
*/
#if !defined(MPACK_WRITE_TRACKING) && \
defined(MPACK_DEBUG) && MPACK_DEBUG && \
defined(MPACK_WRITER) && MPACK_WRITER && \
defined(MPACK_MALLOC)
#define MPACK_WRITE_TRACKING 1
#endif
/**
* @}
*/
/**
* @name Miscellaneous Options
* @{
*/
/**
* Whether to optimize for size or speed.
*
* Optimizing for size simplifies some parsing and encoding algorithms
* at the expense of speed, and saves a few kilobytes of space in the
* resulting executable.
*
* This automatically detects -Os with GCC/Clang. Unfortunately there
* doesn't seem to be a macro defined for /Os under MSVC.
*/
#ifndef MPACK_OPTIMIZE_FOR_SIZE
#ifdef __OPTIMIZE_SIZE__
#define MPACK_OPTIMIZE_FOR_SIZE 1
#else
#define MPACK_OPTIMIZE_FOR_SIZE 0
#endif
#endif
/**
* Stack space in bytes to use when initializing a reader or writer
* with a stack-allocated buffer.
*/
#ifndef MPACK_STACK_SIZE
#define MPACK_STACK_SIZE 4096
#endif
/**
* Buffer size to use for allocated buffers (such as for a file writer.)
*
* Starting with a single page and growing as needed seems to
* provide the best performance with minimal memory waste.
* Increasing this does not improve performance even when writing
* huge messages.
*/
#ifndef MPACK_BUFFER_SIZE
#define MPACK_BUFFER_SIZE 4096
#endif
/**
* Minimum size of an allocated node page in bytes.
*
* The children for a given compound element must be contiguous, so
* larger pages than this may be allocated as needed. (Safety checks
* exist to prevent malicious data from causing too large allocations.)
*
* See @ref mpack_node_data_t for the size of nodes.
*
* Using as many nodes fit in one memory page seems to provide the
* best performance, and has very little waste when parsing small
* messages.
*/
#ifndef MPACK_NODE_PAGE_SIZE
#define MPACK_NODE_PAGE_SIZE 4096
#endif
/**
* The initial depth for the node parser. When MPACK_MALLOC is available,
* the node parser has no practical depth limit, and it is not recursive
* so there is no risk of overflowing the call stack.
*/
#ifndef MPACK_NODE_INITIAL_DEPTH
#define MPACK_NODE_INITIAL_DEPTH 8
#endif
/**
* The maximum depth for the node parser if @ref MPACK_MALLOC is not available.
*/
#ifndef MPACK_NODE_MAX_DEPTH_WITHOUT_MALLOC
#define MPACK_NODE_MAX_DEPTH_WITHOUT_MALLOC 32
#endif
/**
* @}
*/
/**
* @}
*/
/* mpack/mpack-platform.h.h */
/**
* @file
*
* Abstracts all platform-specific code from MPack. This contains
* implementations of standard C functions when libc is not available,
* as well as wrappers to library functions.
*/
#ifndef MPACK_PLATFORM_H
#define MPACK_PLATFORM_H 1
/* Pre-include checks */
#if defined(_MSC_VER) && _MSC_VER < 1800 && !defined(__cplusplus)
#error "In Visual Studio 2012 and earlier, MPack must be compiled as C++. Enable the /Tp compiler flag."
#endif
#if defined(WIN32) && defined(MPACK_INTERNAL) && MPACK_INTERNAL
#define _CRT_SECURE_NO_WARNINGS 1
#endif
/* Doxygen preprocs */
#if defined(MPACK_DOXYGEN) && MPACK_DOXYGEN
#define MPACK_HAS_CONFIG 0
// We give these their default values of 0 here even though they are defined to
// 1 in the doxyfile. Doxygen will show this as the value in the docs, even
// though it ignores it when parsing the rest of the source. This is what we
// want, since we want the documentation to show these defaults but still
// generate documentation for the functions they add when they're on.
#define MPACK_COMPATIBILITY 0
#define MPACK_EXTENSIONS 0
#endif
/* Include the custom config file if enabled */
#if defined(MPACK_HAS_CONFIG) && MPACK_HAS_CONFIG
/* #include "mpack-config.h" */
#endif
/*
* Now that the optional config is included, we define the defaults
* for any of the configuration options and other switches that aren't
* yet defined.
*/
#if defined(MPACK_DOXYGEN) && MPACK_DOXYGEN
/* #include "mpack-defaults-doxygen.h" */
#else
/* #include "mpack-defaults.h" */
#endif
/*
* All remaining configuration options that have not yet been set must
* be defined here in order to support -Wundef.
*/
#ifndef MPACK_DEBUG
#define MPACK_DEBUG 0
#endif
#ifndef MPACK_CUSTOM_BREAK
#define MPACK_CUSTOM_BREAK 0
#endif
#ifndef MPACK_READ_TRACKING
#define MPACK_READ_TRACKING 0
#endif
#ifndef MPACK_WRITE_TRACKING
#define MPACK_WRITE_TRACKING 0
#endif
#ifndef MPACK_EMIT_INLINE_DEFS
#define MPACK_EMIT_INLINE_DEFS 0
#endif
#ifndef MPACK_AMALGAMATED
#define MPACK_AMALGAMATED 0
#endif
#ifndef MPACK_RELEASE_VERSION
#define MPACK_RELEASE_VERSION 0
#endif
#ifndef MPACK_INTERNAL
#define MPACK_INTERNAL 0
#endif
#ifndef MPACK_NO_BUILTINS
#define MPACK_NO_BUILTINS 0
#endif
/* System headers (based on configuration) */
#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS 1
#endif
#ifndef __STDC_FORMAT_MACROS
#define __STDC_FORMAT_MACROS 1
#endif
#ifndef __STDC_CONSTANT_MACROS
#define __STDC_CONSTANT_MACROS 1
#endif
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <inttypes.h>
#include <limits.h>
#if MPACK_STDLIB
#include <string.h>
#include <stdlib.h>
#endif
#if MPACK_STDIO
#include <stdio.h>
#include <errno.h>
#endif
/*
* Header configuration
*/
#ifdef __cplusplus
#define MPACK_EXTERN_C_START extern "C" {
#define MPACK_EXTERN_C_END }
#else
#define MPACK_EXTERN_C_START /* nothing */
#define MPACK_EXTERN_C_END /* nothing */
#endif
/* GCC versions from 4.6 to before 5.1 warn about defining a C99
* non-static inline function before declaring it (see issue #20) */
#ifdef __GNUC__
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
#ifdef __cplusplus
#define MPACK_DECLARED_INLINE_WARNING_START \
_Pragma ("GCC diagnostic push") \
_Pragma ("GCC diagnostic ignored \"-Wmissing-declarations\"")
#else
#define MPACK_DECLARED_INLINE_WARNING_START \
_Pragma ("GCC diagnostic push") \
_Pragma ("GCC diagnostic ignored \"-Wmissing-prototypes\"")
#endif
#define MPACK_DECLARED_INLINE_WARNING_END \
_Pragma ("GCC diagnostic pop")
#endif
#endif
#ifndef MPACK_DECLARED_INLINE_WARNING_START
#define MPACK_DECLARED_INLINE_WARNING_START /* nothing */
#define MPACK_DECLARED_INLINE_WARNING_END /* nothing */
#endif
/* GCC versions before 4.8 warn about shadowing a function with a
* variable that isn't a function or function pointer (like "index") */
#ifdef __GNUC__
#if (__GNUC__ < 4) || (__GNUC__ == 4 && __GNUC_MINOR__ < 8)
#define MPACK_WSHADOW_WARNING_START \
_Pragma ("GCC diagnostic push") \
_Pragma ("GCC diagnostic ignored \"-Wshadow\"")
#define MPACK_WSHADOW_WARNING_END \
_Pragma ("GCC diagnostic pop")
#endif
#endif
#ifndef MPACK_WSHADOW_WARNING_START
#define MPACK_WSHADOW_WARNING_START /* nothing */
#define MPACK_WSHADOW_WARNING_END /* nothing */
#endif
#define MPACK_HEADER_START \
MPACK_EXTERN_C_START \
MPACK_WSHADOW_WARNING_START \
MPACK_DECLARED_INLINE_WARNING_START
#define MPACK_HEADER_END \
MPACK_DECLARED_INLINE_WARNING_END \
MPACK_WSHADOW_WARNING_END \
MPACK_EXTERN_C_END
MPACK_HEADER_START
/* Miscellaneous helper macros */
#define MPACK_UNUSED(var) ((void)(var))
#define MPACK_STRINGIFY_IMPL(arg) #arg
#define MPACK_STRINGIFY(arg) MPACK_STRINGIFY_IMPL(arg)
// This is a workaround for MSVC's incorrect expansion of __VA_ARGS__. It
// treats __VA_ARGS__ as a single preprocessor token when passed in the
// argument list of another macro unless we use an outer wrapper to expand it
// lexically first. (For safety/consistency we use this in all variadic macros
// that don't ignore the variadic arguments regardless of whether __VA_ARGS__
// is passed to another macro.)
// https://stackoverflow.com/a/32400131
#define MPACK_EXPAND(x) x
// Extracts the first argument of a variadic macro list, where there might only
// be one argument.
#define MPACK_EXTRACT_ARG0_IMPL(first, ...) first
#define MPACK_EXTRACT_ARG0(...) MPACK_EXPAND(MPACK_EXTRACT_ARG0_IMPL( __VA_ARGS__ , ignored))
// Stringifies the first argument of a variadic macro list, where there might
// only be one argument.
#define MPACK_STRINGIFY_ARG0_impl(first, ...) #first
#define MPACK_STRINGIFY_ARG0(...) MPACK_EXPAND(MPACK_STRINGIFY_ARG0_impl( __VA_ARGS__ , ignored))
/*
* Definition of inline macros.
*
* MPack does not use static inline in header files; only one non-inline definition
* of each function should exist in the final build. This can reduce the binary size
* in cases where the compiler cannot or chooses not to inline a function.
* The addresses of functions should also compare equal across translation units
* regardless of whether they are declared inline.
*
* The above requirements mean that the declaration and definition of non-trivial
* inline functions must be separated so that the definitions will only
* appear when necessary. In addition, three different linkage models need
* to be supported:
*
* - The C99 model, where a standalone function is emitted only if there is any
* `extern inline` or non-`inline` declaration (including the definition itself)
*
* - The GNU model, where an `inline` definition emits a standalone function and an
* `extern inline` definition does not, regardless of other declarations
*
* - The C++ model, where `inline` emits a standalone function with special
* (COMDAT) linkage
*
* The macros below wrap up everything above. All inline functions defined in header
* files have a single non-inline definition emitted in the compilation of
* mpack-platform.c. All inline declarations and definitions use the same MPACK_INLINE
* specification to simplify the rules on when standalone functions are emitted.
* Inline functions in source files are defined MPACK_STATIC_INLINE.
*
* Additional reading:
* http://www.greenend.org.uk/rjk/tech/inline.html
*/
#if defined(__cplusplus)
// C++ rules
// The linker will need COMDAT support to link C++ object files,
// so we don't need to worry about emitting definitions from C++
// translation units. If mpack-platform.c (or the amalgamation)
// is compiled as C, its definition will be used, otherwise a
// C++ definition will be used, and no other C files will emit
// a defition.
#define MPACK_INLINE inline
#elif defined(_MSC_VER)
// MSVC 2013 always uses COMDAT linkage, but it doesn't treat 'inline' as a
// keyword in C99 mode. (This appears to be fixed in a later version of
// MSVC but we don't bother detecting it.)
#define MPACK_INLINE __inline
#define MPACK_STATIC_INLINE static __inline
#elif defined(__GNUC__) && (defined(__GNUC_GNU_INLINE__) || \
!defined(__GNUC_STDC_INLINE__) && !defined(__GNUC_GNU_INLINE__))
// GNU rules
#if MPACK_EMIT_INLINE_DEFS
#define MPACK_INLINE inline
#else
#define MPACK_INLINE extern inline
#endif
#else
// C99 rules
#if MPACK_EMIT_INLINE_DEFS
#define MPACK_INLINE extern inline
#else
#define MPACK_INLINE inline
#endif
#endif
#ifndef MPACK_STATIC_INLINE
#define MPACK_STATIC_INLINE static inline
#endif
#ifdef MPACK_OPTIMIZE_FOR_SPEED
#error "You should define MPACK_OPTIMIZE_FOR_SIZE, not MPACK_OPTIMIZE_FOR_SPEED."
#endif
/*
* Prevent inlining
*
* When a function is only used once, it is almost always inlined
* automatically. This can cause poor instruction cache usage because a
* function that should rarely be called (such as buffer exhaustion handling)
* will get inlined into the middle of a hot code path.
*/
#if !MPACK_NO_BUILTINS
#if defined(_MSC_VER)
#define MPACK_NOINLINE __declspec(noinline)
#elif defined(__GNUC__) || defined(__clang__)
#define MPACK_NOINLINE __attribute__((noinline))
#endif
#endif
#ifndef MPACK_NOINLINE
#define MPACK_NOINLINE /* nothing */
#endif
/* Some compiler-specific keywords and builtins */
#if !MPACK_NO_BUILTINS
#if defined(__GNUC__) || defined(__clang__)
#define MPACK_UNREACHABLE __builtin_unreachable()
#define MPACK_NORETURN(fn) fn __attribute__((noreturn))
#define MPACK_RESTRICT __restrict__
#elif defined(_MSC_VER)
#define MPACK_UNREACHABLE __assume(0)
#define MPACK_NORETURN(fn) __declspec(noreturn) fn
#define MPACK_RESTRICT __restrict
#endif
#endif
#ifndef MPACK_RESTRICT
#ifdef __cplusplus
#define MPACK_RESTRICT /* nothing, unavailable in C++ */
#else
#define MPACK_RESTRICT restrict /* required in C99 */
#endif
#endif
#ifndef MPACK_UNREACHABLE
#define MPACK_UNREACHABLE ((void)0)
#endif
#ifndef MPACK_NORETURN
#define MPACK_NORETURN(fn) fn
#endif
/*
* Likely/unlikely
*
* These should only really be used when a branch is taken (or not taken) less
* than 1/1000th of the time. Buffer flush checks when writing very small
* elements are a good example.
*/
#if !MPACK_NO_BUILTINS
#if defined(__GNUC__) || defined(__clang__)
#ifndef MPACK_LIKELY
#define MPACK_LIKELY(x) __builtin_expect((x),1)
#endif
#ifndef MPACK_UNLIKELY
#define MPACK_UNLIKELY(x) __builtin_expect((x),0)
#endif
#endif
#endif
#ifndef MPACK_LIKELY
#define MPACK_LIKELY(x) (x)
#endif
#ifndef MPACK_UNLIKELY
#define MPACK_UNLIKELY(x) (x)
#endif
/* Static assert */
#ifndef MPACK_STATIC_ASSERT
#if defined(__cplusplus)
#if __cplusplus >= 201103L
#define MPACK_STATIC_ASSERT static_assert
#endif
#elif defined(__STDC_VERSION__)
/* MSVC 19.27 advertises C11, but has no `_Static_assert` */
#if __STDC_VERSION__ >= 201112L && !defined(_MSC_VER)
#define MPACK_STATIC_ASSERT _Static_assert
#endif
#endif
#endif
#if !MPACK_NO_BUILTINS
#ifndef MPACK_STATIC_ASSERT
#if defined(__has_feature)
#if __has_feature(cxx_static_assert)
#define MPACK_STATIC_ASSERT static_assert
#elif __has_feature(c_static_assert)
#define MPACK_STATIC_ASSERT _Static_assert
#endif
#endif
#endif
#ifndef MPACK_STATIC_ASSERT
#if defined(__GNUC__)
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
#ifndef __cplusplus
#if __GNUC__ >= 5
#define MPACK_IGNORE_PEDANTIC "GCC diagnostic ignored \"-Wpedantic\""
#else
#define MPACK_IGNORE_PEDANTIC "GCC diagnostic ignored \"-pedantic\""
#endif
#define MPACK_STATIC_ASSERT(expr, str) do { \
_Pragma ("GCC diagnostic push") \
_Pragma (MPACK_IGNORE_PEDANTIC) \
_Pragma ("GCC diagnostic ignored \"-Wc++-compat\"") \
_Static_assert(expr, str); \
_Pragma ("GCC diagnostic pop") \
} while (0)
#endif
#endif
#endif
#endif
#ifndef MPACK_STATIC_ASSERT
#ifdef _MSC_VER
#if _MSC_VER >= 1600
#define MPACK_STATIC_ASSERT static_assert
#endif
#endif
#endif
#endif
#ifndef MPACK_STATIC_ASSERT
#define MPACK_STATIC_ASSERT(expr, str) (MPACK_UNUSED(sizeof(char[1 - 2*!(expr)])))
#endif
/* _Generic */
#ifndef MPACK_HAS_GENERIC
#if defined(__clang__) && defined(__has_feature)
// With Clang we can test for _Generic support directly
// and ignore C/C++ version
#if __has_feature(c_generic_selections)
#define MPACK_HAS_GENERIC 1
#else
#define MPACK_HAS_GENERIC 0
#endif
#endif
#endif
#ifndef MPACK_HAS_GENERIC
#if defined(__STDC_VERSION__)
#if __STDC_VERSION__ >= 201112L
#if defined(__GNUC__) && !defined(__clang__)
// GCC does not have full C11 support in GCC 4.7 and 4.8
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9)
#define MPACK_HAS_GENERIC 1
#endif
#else
// We hope other compilers aren't lying about C11/_Generic support
#define MPACK_HAS_GENERIC 1
#endif
#endif
#endif
#endif
#ifndef MPACK_HAS_GENERIC
#define MPACK_HAS_GENERIC 0
#endif
/*
* Finite Math
*
* -ffinite-math-only, included in -ffast-math, breaks functions that
* that check for non-finite real values such as isnan() and isinf().
*
* We should use this to trap errors when reading data that contains
* non-finite reals. This isn't currently implemented.
*/
#ifndef MPACK_FINITE_MATH
#if defined(__FINITE_MATH_ONLY__) && __FINITE_MATH_ONLY__
#define MPACK_FINITE_MATH 1
#endif
#endif
#ifndef MPACK_FINITE_MATH
#define MPACK_FINITE_MATH 0
#endif
/*
* Endianness checks
*
* These define MPACK_NHSWAP*() which swap network<->host byte
* order when needed.
*
* We leave them undefined if we can't determine the endianness
* at compile-time, in which case we fall back to bit-shifts.
*
* See the notes in mpack-common.h.
*/
#if defined(__BYTE_ORDER__) && defined(__ORDER_LITTLE_ENDIAN__) && defined(__ORDER_BIG_ENDIAN__)
#if __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define MPACK_NHSWAP16(x) (x)
#define MPACK_NHSWAP32(x) (x)
#define MPACK_NHSWAP64(x) (x)
#elif __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
#if !MPACK_NO_BUILTINS
#if defined(__clang__)
#ifdef __has_builtin
// Unlike the GCC builtins, the bswap builtins in Clang
// significantly improve ARM performance.
#if __has_builtin(__builtin_bswap16)
#define MPACK_NHSWAP16(x) __builtin_bswap16(x)
#endif
#if __has_builtin(__builtin_bswap32)
#define MPACK_NHSWAP32(x) __builtin_bswap32(x)
#endif
#if __has_builtin(__builtin_bswap64)
#define MPACK_NHSWAP64(x) __builtin_bswap64(x)
#endif
#endif
#elif defined(__GNUC__)
// The GCC bswap builtins are apparently poorly optimized on older
// versions of GCC, so we set a minimum version here just in case.
// http://hardwarebug.org/2010/01/14/beware-the-builtins/
#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5)
#define MPACK_NHSWAP64(x) __builtin_bswap64(x)
#endif
// __builtin_bswap16() was not implemented on all platforms
// until GCC 4.8.0:
// https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52624
//
// The 16- and 32-bit versions in GCC significantly reduce performance
// on ARM with little effect on code size so we don't use them.
#endif
#endif
#endif
#elif defined(_MSC_VER) && defined(_WIN32) && !MPACK_NO_BUILTINS
// On Windows, we assume x86 and x86_64 are always little-endian.
// We make no assumptions about ARM even though all current
// Windows Phone devices are little-endian in case Microsoft's
// compiler is ever used with a big-endian ARM device.
#if defined(_M_IX86) || defined(_M_X64) || defined(_M_AMD64)
#define MPACK_NHSWAP16(x) _byteswap_ushort(x)
#define MPACK_NHSWAP32(x) _byteswap_ulong(x)
#define MPACK_NHSWAP64(x) _byteswap_uint64(x)
#endif
#endif
#if defined(__FLOAT_WORD_ORDER__) && defined(__BYTE_ORDER__)
// We check where possible that the float byte order matches the
// integer byte order. This is extremely unlikely to fail, but
// we check anyway just in case.
//
// (The static assert is placed in float/double encoders instead
// of here because our static assert fallback doesn't work at
// file scope)
#define MPACK_CHECK_FLOAT_ORDER() \
MPACK_STATIC_ASSERT(__FLOAT_WORD_ORDER__ == __BYTE_ORDER__, \
"float byte order does not match int byte order! float/double " \
"encoding is not properly implemented on this platform.")
#endif
#ifndef MPACK_CHECK_FLOAT_ORDER
#define MPACK_CHECK_FLOAT_ORDER() /* nothing */
#endif
/*
* Here we define mpack_assert() and mpack_break(). They both work like a normal
* assertion function in debug mode, causing a trap or abort. However, on some platforms
* you can safely resume execution from mpack_break(), whereas mpack_assert() is
* always fatal.
*
* In release mode, mpack_assert() is converted to an assurance to the compiler
* that the expression cannot be false (via e.g. __assume() or __builtin_unreachable())
* to improve optimization where supported. There is thus no point in "safely" handling
* the case of this being false. Writing mpack_assert(0) rarely makes sense (except
* possibly as a default handler in a switch) since the compiler will throw away any
* code after it. If at any time an mpack_assert() is not true, the behaviour is
* undefined. This also means the expression is evaluated even in release.
*
* mpack_break() on the other hand is compiled to nothing in release. It is
* used in situations where we want to highlight a programming error as early as
* possible (in the debugger), but we still handle the situation safely if it
* happens in release to avoid producing incorrect results (such as in
* MPACK_WRITE_TRACKING.) It does not take an expression to test because it
* belongs in a safe-handling block after its failing condition has been tested.
*
* If stdio is available, we can add a format string describing the error, and
* on some compilers we can declare it noreturn to get correct results from static
* analysis tools. Note that the format string and arguments are not evaluated unless
* the assertion is hit.
*
* Note that any arguments to mpack_assert() beyond the first are only evaluated
* if the expression is false (and are never evaluated in release.)
*
* mpack_assert_fail() and mpack_break_hit() are defined separately
* because assert is noreturn and break isn't. This distinction is very
* important for static analysis tools to give correct results.
*/