Mstdlib-1.24.0
m_defs.h
1/* The MIT License (MIT)
2 *
3 * Copyright (c) 2015 Monetra Technologies, LLC.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a copy
6 * of this software and associated documentation files (the "Software"), to deal
7 * in the Software without restriction, including without limitation the rights
8 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 * copies of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21 * THE SOFTWARE.
22 */
23
24#ifndef __M_DEFS_H__
25#define __M_DEFS_H__
26
27/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
28/* Required headers */
29#include <stdio.h>
30#include <stdlib.h>
31#include <stdarg.h> /* __VA_ARGS__ */
32
33/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
34
35#ifndef __FUNCTION__
36# define __FUNCTION__ __func__
37#endif
38
39#ifdef __GNUC__
40# define GCC_VERSION (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
41#else
42# ifndef __inline__
43# ifdef __SCO_VERSION__
44# define __inline__ inline
45# else
46# define __inline__ __inline
47# endif
48# endif
49#endif
50
51
52#if defined(__clang__)
53# ifndef __has_attribute
54# define __has_attribute(x) 0
55# endif
56# ifndef __has_feature
57# define __has_feature(x) 0
58# endif
59# ifndef __has_extension
60# define __has_extension __has_feature
61# endif
62# define M_COMPILER_SUPPORTS(ATTR, VER) (__has_attribute(ATTR) || __has_extension(ATTR))
63#elif defined(__GNUC__)
64# define M_COMPILER_SUPPORTS(ATTR, VER) (GCC_VERSION >= VER)
65#else
66# define M_COMPILER_SUPPORTS(ATTR, VER) 0
67#endif
68
69
70/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
71/* Prototype Wrapping- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
72/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
73
74#undef __BEGIN_DECLS
75#undef __END_DECLS
76#if defined(__cplusplus)
77# define __BEGIN_DECLS extern "C" {
78# define __END_DECLS }
79#else
80# define __BEGIN_DECLS /* empty */
81# define __END_DECLS /* empty */
82#endif
83
84/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
85/* Function Keywords - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
86/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
87
88/* Dynamic shared object and dynamic-link library support */
89#ifdef _WIN32 /* Both MinGW and Visual Studio support declspec. */
90# define M_DLL_IMPORT __declspec(dllimport)
91# define M_DLL_EXPORT __declspec(dllexport)
92#elif M_COMPILER_SUPPORTS(visibility, 40000)
93# define M_DLL_IMPORT __attribute__((visibility ("default")))
94# define M_DLL_EXPORT __attribute__((visibility ("default")))
95#else
96# define M_DLL_IMPORT /* empty */
97# define M_DLL_EXPORT /* empty */
98#endif
99
100#ifdef M_API
101# undef M_API
102#endif
103#if defined(MSTDLIB_STATIC)
104# define M_API
105#elif defined(M_DLL)
106# define M_API M_DLL_EXPORT
107#else
108# define M_API M_DLL_IMPORT
109#endif
110
111/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
112/* Attributes - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
113/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
114#if M_COMPILER_SUPPORTS(malloc, 0)
115/*! Tell compiler that the function may be treated as if any non-NULL pointer
116 * returned cannot alias any other pointer valid when function returns and the
117 * memory contents are undefined. */
118# define M_MALLOC __attribute__((malloc))
119# define M_MALLOC_ALIASED
120#else
121# define M_MALLOC
122# define M_MALLOC_ALIASED
123#endif
124
125#define M_FREE(arg)
126
127#if M_COMPILER_SUPPORTS(__deprecated__, 0)
128 /*! Warn about the usage of a function. */
129# define M_DEPRECATED(proto) proto __attribute__((__deprecated__))
130#else
131# define M_DEPRECATED(proto) proto
132#endif
133
134#if M_COMPILER_SUPPORTS(__format__, 0) && !defined(_WIN32)
135 /*! Warn about format strings that are inconsistent with given arguments */
136# define M_PRINTF(fmt_idx, arg_idx) __attribute__((__format__(__printf__,fmt_idx,arg_idx)))
137#else
138# define M_PRINTF(fmtarg, firstvararg) /* empty */
139#endif
140
141
142/* Warn about caller ignoring the result of this function.
143 */
144#if M_COMPILER_SUPPORTS(warn_unused_result, 30400)
145# define M_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
146#else
147# define M_WARN_UNUSED_RESULT
148#endif
149
150/* Warn about function arguments that are passed as NULL.
151 *
152 * This must not be set when building the library itself because GCC will
153 * optimize out NULL checks that are in place to prevent crashes due to misuse.
154 */
155#if !defined(MSTDLIB_INTERNAL) && M_COMPILER_SUPPORTS(nonnull, 30400)
156# define M_WARN_NONNULL(arg) __attribute__((nonnull(arg)))
157#else
158/* Other compilers might not support VA_ARGS */
159# define M_WARN_NONNULL(arg)
160#endif
161
162/*! Tell compiler that the function return value points to memory where the
163 * size is given by one or two of the functions parameters.
164 */
165#if M_COMPILER_SUPPORTS(alloc_size, 40300)
166# define M_ALLOC_SIZE(size) __attribute__((alloc_size(size)))
167#else
168# define M_ALLOC_SIZE(size)
169#endif
170
171/*! When debugging, produce a unit for an inline.
172 */
173#if defined(__GNUC__) && !defined(M_BLACKLIST_FUNC)
174# define M_BLACKLIST_FUNC 1
175#endif
176
177/*! Warn about the usage of a function with a message.
178 *
179 * Suppress deprecation warnings on Windows, and in C++ code that's using Qt.
180 *
181 * Clang only allows you to add the deprecation attribute when you initially declare the function, so you
182 * can't use it to mark functions from other libraries as deprecated. So, we can't use the deprecated
183 * attribute with clang.
184 */
185#if defined(__GNUC__) && !defined(__clang__) && (GCC_VERSION >= 40500) && !defined(QT_VERSION) && !defined(_WIN32)
186# define M_DEPRECATED_MSG(msg,proto) proto __attribute__((deprecated(msg)));
187#else
188# define M_DEPRECATED_MSG(msg,proto)
189#endif
190
191/*! Warn about the usage of a function and recommend an alternative.
192 */
193#define M_DEPRECATED_FOR(f,proto) M_DEPRECATED_MSG("Use " #f " instead", proto)
194
195#if defined(__clang__)
196# define M_BEGIN_IGNORE_DEPRECATIONS _Pragma ("clang diagnostic push") \
197 _Pragma ("clang diagnostic ignored \"-Wdeprecated-declarations\"")
198# define M_END_IGNORE_DEPRECATIONS _Pragma ("clang diagnostic pop")
199# define M_BEGIN_IGNORE_REDECLARATIONS _Pragma ("clang diagnostic push") \
200 _Pragma ("clang diagnostic ignored \"-Wredundant-decls\"")
201# define M_END_IGNORE_REDECLARATIONS _Pragma ("clang diagnostic pop")
202#elif defined(__GNUC__) && GCC_VERSION >= 40600
203# define M_BEGIN_IGNORE_DEPRECATIONS _Pragma ("GCC diagnostic push") \
204 _Pragma ("GCC diagnostic ignored \"-Wdeprecated-declarations\"")
205# define M_END_IGNORE_DEPRECATIONS _Pragma ("GCC diagnostic pop")
206# define M_BEGIN_IGNORE_REDECLARATIONS _Pragma ("GCC diagnostic push") \
207 _Pragma ("GCC diagnostic ignored \"-Wredundant-decls\"")
208# define M_END_IGNORE_REDECLARATIONS _Pragma ("GCC diagnostic pop")
209#else
210# define M_BEGIN_IGNORE_DEPRECATIONS
211# define M_END_IGNORE_DEPRECATIONS
212# define M_BEGIN_IGNORE_REDECLARATIONS
213# define M_END_IGNORE_REDECLARATIONS
214# undef m_blacklist_func
215# define m_blacklist_func 0
216# define m_begin_ignore_redeclarations
217# define m_end_ignore_redeclarations
218#endif
219
220
221#define M_WARNING_STR(s) #s
222#define M_WARNING_JOINSTR(x,y) M_WARNING_STR(x ## y)
223#ifdef _MSC_VER
224# define M_WARNING_PRAGMA(x) __pragma (warning(x))
225#elif defined(__clang__)
226# define M_WARNING_PRAGMA(x) _Pragma (M_WARNING_STR(clang diagnostic x))
227#elif defined(__GNUC__)
228# define M_WARNING_PRAGMA(x) _Pragma (M_WARNING_STR(GCC diagnostic x))
229#else
230# define M_WARNING_PRAGMA(x)
231#endif
232#define M_WARNING_CANCEL() M_WARNING_PRAGMA(pop)
233#if defined(__clang__)
234# define M_WARNING_DISABLE(gcc_unused,clang_option,msvc_unused) M_WARNING_PRAGMA(push) M_WARNING_PRAGMA(ignored M_WARNING_JOINSTR(-W,clang_option))
235# define M_WARNING_ENABLE(gcc_unused,clang_option,msvc_unused) M_WARNING_PRAGMA(push) M_WARNING_PRAGMA(M_WARNING_JOINSTR(-W,clang_option))
236#elif defined(_MSC_VER)
237# define M_WARNING_DISABLE(gcc_unused,clang_unused,msvc_errorcode) M_WARNING_PRAGMA(push) M_WARNING_PRAGMA(disable: msvc_errorcode)
238# define M_WARNING_ENABLE(gcc_unused,clang_unused,msvc_errorcode) M_WARNING_PRAGMA(push) M_WARNING_PRAGMA(msvc_errorcode)
239#elif defined(__GNUC__)
240# define M_WARNING_DISABLE(gcc_option,clang_unused,msvc_unused) M_WARNING_PRAGMA(push) M_WARNING_PRAGMA(ignored M_WARNING_JOINSTR(-W,gcc_option))
241# define M_WARNING_ENABLE(gcc_option,clang_unused,msvc_unused) M_WARNING_PRAGMA(push) M_WARNING_PRAGMA(warning M_WARNING_JOINSTR(-W,gcc_option))
242#else
243# define M_WARNING_DISABLE(gcc_option,clang_unused,msvc_unsued)
244# define M_WARNING_ENABLE(gcc_option,clang_unused,msvc_unsued)
245#endif
246
247
248/*! Used to safely cast off a 'const', used only when it is known
249 * to be okay to do so */
250#define M_CAST_OFF_CONST(type, var) ((type)((M_uintptr)var))
251
252
253/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
254
255#endif /* __M_DEFS_H__ */