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 | // We define MPACK_EMIT_INLINE_DEFS and include mpack.h to emit |
24 | // standalone definitions of all (non-static) inline functions in MPack. |
25 | |
26 | #define MPACK_INTERNAL 1 |
27 | #define MPACK_EMIT_INLINE_DEFS 1 |
28 | |
29 | #include "mpack-platform.h" |
30 | #include "mpack.h" |
31 | |
32 | MPACK_SILENCE_WARNINGS_BEGIN |
33 | |
34 | #if MPACK_DEBUG |
35 | |
36 | #if MPACK_STDIO |
37 | void mpack_assert_fail_format(const char* format, ...) { |
38 | char buffer[512]; |
39 | va_list args; |
40 | va_start(args, format); |
41 | vsnprintf(buffer, sizeof(buffer), format, args); |
42 | va_end(args); |
43 | buffer[sizeof(buffer) - 1] = 0; |
44 | mpack_assert_fail_wrapper(buffer); |
45 | } |
46 | |
47 | void mpack_break_hit_format(const char* format, ...) { |
48 | char buffer[512]; |
49 | va_list args; |
50 | va_start(args, format); |
51 | vsnprintf(buffer, sizeof(buffer), format, args); |
52 | va_end(args); |
53 | buffer[sizeof(buffer) - 1] = 0; |
54 | mpack_break_hit(buffer); |
55 | } |
56 | #endif |
57 | |
58 | #if !MPACK_CUSTOM_ASSERT |
59 | void mpack_assert_fail(const char* message) { |
60 | MPACK_UNUSED(message); |
61 | |
62 | #if MPACK_STDIO |
63 | fprintf(stderr, "%s\n" , message); |
64 | #endif |
65 | } |
66 | #endif |
67 | |
68 | // We split the assert failure from the wrapper so that a |
69 | // custom assert function can return. |
70 | void mpack_assert_fail_wrapper(const char* message) { |
71 | |
72 | #ifdef MPACK_GCOV |
73 | // gcov marks even __builtin_unreachable() as an uncovered line. this |
74 | // silences it. |
75 | (mpack_assert_fail(message), __builtin_unreachable()); |
76 | |
77 | #else |
78 | mpack_assert_fail(message); |
79 | |
80 | // mpack_assert_fail() is not supposed to return. in case it does, we |
81 | // abort. |
82 | |
83 | #if !MPACK_NO_BUILTINS |
84 | #if defined(__GNUC__) || defined(__clang__) |
85 | __builtin_trap(); |
86 | #elif defined(WIN32) |
87 | __debugbreak(); |
88 | #endif |
89 | #endif |
90 | |
91 | #if (defined(__GNUC__) || defined(__clang__)) && !MPACK_NO_BUILTINS |
92 | __builtin_abort(); |
93 | #elif MPACK_STDLIB |
94 | abort(); |
95 | #endif |
96 | |
97 | MPACK_UNREACHABLE; |
98 | #endif |
99 | } |
100 | |
101 | #if !MPACK_CUSTOM_BREAK |
102 | |
103 | // If we have a custom assert handler, break wraps it by default. |
104 | // This allows users of MPack to only implement mpack_assert_fail() without |
105 | // having to worry about the difference between assert and break. |
106 | // |
107 | // MPACK_CUSTOM_BREAK is available to define a separate break handler |
108 | // (which is needed by the unit test suite), but this is not offered in |
109 | // mpack-config.h for simplicity. |
110 | |
111 | #if MPACK_CUSTOM_ASSERT |
112 | void mpack_break_hit(const char* message) { |
113 | mpack_assert_fail_wrapper(message); |
114 | } |
115 | #else |
116 | void mpack_break_hit(const char* message) { |
117 | MPACK_UNUSED(message); |
118 | |
119 | #if MPACK_STDIO |
120 | fprintf(stderr, "%s\n" , message); |
121 | #endif |
122 | |
123 | #if defined(__GNUC__) || defined(__clang__) && !MPACK_NO_BUILTINS |
124 | __builtin_trap(); |
125 | #elif defined(WIN32) && !MPACK_NO_BUILTINS |
126 | __debugbreak(); |
127 | #elif MPACK_STDLIB |
128 | abort(); |
129 | #endif |
130 | } |
131 | #endif |
132 | |
133 | #endif |
134 | |
135 | #endif |
136 | |
137 | |
138 | |
139 | // The below are adapted from the C wikibook: |
140 | // https://en.wikibooks.org/wiki/C_Programming/Strings |
141 | |
142 | #ifndef mpack_memcmp |
143 | int mpack_memcmp(const void* s1, const void* s2, size_t n) { |
144 | const unsigned char *us1 = (const unsigned char *) s1; |
145 | const unsigned char *us2 = (const unsigned char *) s2; |
146 | while (n-- != 0) { |
147 | if (*us1 != *us2) |
148 | return (*us1 < *us2) ? -1 : +1; |
149 | us1++; |
150 | us2++; |
151 | } |
152 | return 0; |
153 | } |
154 | #endif |
155 | |
156 | #ifndef mpack_memcpy |
157 | void* mpack_memcpy(void* MPACK_RESTRICT s1, const void* MPACK_RESTRICT s2, size_t n) { |
158 | char* MPACK_RESTRICT dst = (char *)s1; |
159 | const char* MPACK_RESTRICT src = (const char *)s2; |
160 | while (n-- != 0) |
161 | *dst++ = *src++; |
162 | return s1; |
163 | } |
164 | #endif |
165 | |
166 | #ifndef mpack_memmove |
167 | void* mpack_memmove(void* s1, const void* s2, size_t n) { |
168 | char *p1 = (char *)s1; |
169 | const char *p2 = (const char *)s2; |
170 | if (p2 < p1 && p1 < p2 + n) { |
171 | p2 += n; |
172 | p1 += n; |
173 | while (n-- != 0) |
174 | *--p1 = *--p2; |
175 | } else |
176 | while (n-- != 0) |
177 | *p1++ = *p2++; |
178 | return s1; |
179 | } |
180 | #endif |
181 | |
182 | #ifndef mpack_memset |
183 | void* mpack_memset(void* s, int c, size_t n) { |
184 | unsigned char *us = (unsigned char *)s; |
185 | unsigned char uc = (unsigned char)c; |
186 | while (n-- != 0) |
187 | *us++ = uc; |
188 | return s; |
189 | } |
190 | #endif |
191 | |
192 | #ifndef mpack_strlen |
193 | size_t mpack_strlen(const char* s) { |
194 | const char* p = s; |
195 | while (*p != '\0') |
196 | p++; |
197 | return (size_t)(p - s); |
198 | } |
199 | #endif |
200 | |
201 | |
202 | |
203 | #if defined(MPACK_MALLOC) && !defined(MPACK_REALLOC) |
204 | void* mpack_realloc(void* old_ptr, size_t used_size, size_t new_size) { |
205 | if (new_size == 0) { |
206 | if (old_ptr) |
207 | MPACK_FREE(old_ptr); |
208 | return NULL; |
209 | } |
210 | |
211 | void* new_ptr = MPACK_MALLOC(new_size); |
212 | if (new_ptr == NULL) |
213 | return NULL; |
214 | |
215 | mpack_memcpy(new_ptr, old_ptr, used_size); |
216 | MPACK_FREE(old_ptr); |
217 | return new_ptr; |
218 | } |
219 | #endif |
220 | |
221 | MPACK_SILENCE_WARNINGS_END |
222 | |