libacfutils
A general purpose library of utility functions designed to make it easier to develop addons for the X-Plane flight simulator.
Loading...
Searching...
No Matches
core.h
Go to the documentation of this file.
1/*
2 * CDDL HEADER START
3 *
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
8 *
9 * You can obtain a copy of the license in the file COPYING
10 * or http://www.opensource.org/licenses/CDDL-1.0.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
13 *
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file COPYING.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 *
20 * CDDL HEADER END
21 */
22/*
23 * Copyright 2023 Saso Kiselkov. All rights reserved.
24 */
27#ifndef _ACFUTILS_CORE_H_
28#define _ACFUTILS_CORE_H_
29
30#include <stdlib.h>
31
32#include "libconfig.h"
33
34#ifdef __cplusplus
35extern "C" {
36#endif
37
75#if __STDC_VERSION__ >= 202311L
76
77#define UNUSED_ATTR [[maybe_unused]]
78#define WARN_UNUSED_RES_ATTR [[nodiscard]]
79#define DEPRECATED_ATTR [[deprecated]]
80
81#elif defined(__GNUC__) || defined(__clang__)
82
83#define UNUSED_ATTR __attribute__((unused))
84#define WARN_UNUSED_RES_ATTR __attribute__((warn_unused_result))
85#define DEPRECATED_ATTR __attribute__((deprecated))
86
87#if IBM && defined(__GNUC__) && __GNUC__ < 11
88#define PACKED_ATTR __attribute__((__packed__, gcc_struct))
89#else
90#define PACKED_ATTR __attribute__((__packed__))
91#endif
92
93#else // !defined(__GNUC__) && !defined(__clang__)
94
95#define UNUSED_ATTR
96#define WARN_UNUSED_RES_ATTR
97#define PACKED_ATTR
98#define DEPRECATED_ATTR
99
100#endif // !defined(__GNUC__) && !defined(__clang__)
101
102#ifndef UNUSED
103#define UNUSED(x) (void)(x)
104#endif
105
106#define LACF_UNUSED(x) (void)(x)
107
108#if __STDC_VERSION__ >= 202311L
109#define NODISCARD [[nodiscard]]
110#define NODISCARD_R(reason) [[nodiscard(reason)]]
111#else // !(__STDC_VERSION__ >= 202311L)
112#define NODISCARD WARN_UNUSED_RES_ATTR
113#define NODISCARD_R(reason) WARN_UNUSED_RES_ATTR
114#endif // !(__STDC_VERSION__ >= 202311L)
115
116#define ACFSYM(__sym__) __libacfutils_ ## __sym__
117
118#if IBM && (!defined(__MINGW32__) || defined(ACFUTILS_DLL))
119#define API_EXPORT __declspec(dllexport)
120#ifdef ACFUTILS_BUILD
121#define API_EXPORT_DATA __declspec(dllexport) extern
122#else
123#define API_EXPORT_DATA __declspec(dllimport) extern
124#endif
125#else /* !IBM || (defined(__MINGW32__) && !defined(ACFUTILS_DLL)) */
126#define API_EXPORT
127#define API_EXPORT_DATA extern
128#endif /* !IBM || (defined(__MINGW32__) && !defined(ACFUTILS_DLL)) */
129
130#ifdef __cplusplus
131# ifndef restrict
132# if defined(_MSC_VER)
133# define restrict __restrict
134# else
135# define restrict __restrict__
136# endif
137# endif /* !defined(restrict) */
138#elif __STDC_VERSION__ < 199901L
139# ifndef restrict
140# if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER)
141# define restrict __restrict
142# else
143# define restrict
144# endif
145# endif /* !defined(restrict) */
146# ifndef inline
147# if defined(_MSC_VER)
148# define inline __inline
149# else
150# define inline
151# endif
152# endif /* !defined(inline) */
153#endif /* __STDC_VERSION__ < 199901L */
154
155API_EXPORT_DATA const char *libacfutils_version;
156
157API_EXPORT void *lacf_malloc(size_t n);
158#define LACF_DESTROY(ptr) \
159 do { \
160 if ((ptr) != NULL) { \
161 lacf_free((ptr)); \
162 (ptr) = NULL; \
163 } \
164 } while (0)
165API_EXPORT void lacf_free(void *buf);
166
167/*
168 * Simple shorthand for calculating number of elements in an array at
169 * compile time. Useful for arrays with flexible sizing.
170 */
171#define ARRAY_NUM_ELEM(_array) (sizeof (_array) / sizeof (*(_array)))
172
173#ifdef __cplusplus
174}
175#endif
176
177#endif /* _ACFUTILS_CORE_H_ */
void lacf_free(void *buf)
Definition core.c:49
const char * libacfutils_version
Definition core.c:29
void * lacf_malloc(size_t n)
Definition core.c:37