diff options
| author | Tyge Løvset <[email protected]> | 2020-10-08 14:29:36 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-10-08 14:29:36 +0200 |
| commit | b68672ec603ade58fc880c7a95ad854de040ad3e (patch) | |
| tree | bf3d7bb4f94a3f3ac19b39506b6cd09b49d8c5e2 /stc/ccommon.h | |
| parent | ab81979f713c71ec164f6bce033038ba09e1e829 (diff) | |
| download | STC-modified-b68672ec603ade58fc880c7a95ad854de040ad3e.tar.gz STC-modified-b68672ec603ade58fc880c7a95ad854de040ad3e.zip | |
Renamed cdefs.h to ccommon.h
Diffstat (limited to 'stc/ccommon.h')
| -rw-r--r-- | stc/ccommon.h | 110 |
1 files changed, 110 insertions, 0 deletions
diff --git a/stc/ccommon.h b/stc/ccommon.h new file mode 100644 index 00000000..f9e3d871 --- /dev/null +++ b/stc/ccommon.h @@ -0,0 +1,110 @@ +/* MIT License
+ *
+ * Copyright (c) 2020 Tyge Løvset, NORCE, www.norceresearch.no
+ *
+ * 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.
+ */
+#ifndef CCOMMON__H__
+#define CCOMMON__H__
+
+#include <stdint.h>
+#include <stddef.h>
+#include <stdbool.h>
+#include <assert.h>
+
+#if defined(_MSC_VER)
+# define STC_FORCE_INLINE static __forceinline
+#elif defined(__GNUC__) || defined(__clang__)
+# define STC_FORCE_INLINE static inline __attribute((always_inline))
+#else
+# define STC_FORCE_INLINE static inline
+#endif
+#define STC_INLINE static inline
+
+#if defined(STC_HEADER) || defined(STC_IMPLEMENTATION)
+# define STC_API extern
+#else
+# define STC_API STC_INLINE
+#endif
+
+enum {_c_max_buffer = 512};
+/* Macro overloading feature support: https://rextester.com/ONP80107 */
+#define _c_CAT( A, B ) A ## B
+#define _c_EXPAND(...) __VA_ARGS__
+#define _c_ARG_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, _14, N,...) N
+#define _c_RSEQ_N 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0
+#define _c_APPLY_ARG_N(ARGS) _c_EXPAND(_c_ARG_N ARGS)
+#define _c_VA_ARG_SIZE(...) _c_EXPAND(_c_APPLY_ARG_N((__VA_ARGS__, _c_RSEQ_N)))
+#define _c_OVERLOAD_SELECT(NAME, NUM) _c_CAT( NAME ## _, NUM)
+
+#define c_MACRO_OVERLOAD(NAME, ...) _c_OVERLOAD_SELECT(NAME, _c_VA_ARG_SIZE(__VA_ARGS__))(__VA_ARGS__)
+#define c_static_assert(cond, msg) typedef char static_assert_##msg[(cond) ? 1 : -1]
+#define c_container_of(ptr, type, member) \
+ ((type *)((char *)(ptr) - offsetof(type, member)))
+
+#define c_new(...) c_MACRO_OVERLOAD(c_new, __VA_ARGS__)
+#define c_new_1(T) ((T *) c_malloc(sizeof(T)))
+#define c_new_2(T, n) ((T *) c_malloc(sizeof(T) * (n)))
+#ifndef c_malloc
+#define c_malloc(sz) malloc(sz)
+#define c_calloc(n, sz) calloc(n, sz)
+#define c_realloc(p, sz) realloc(p, sz)
+#define c_free(p) free(p)
+#endif
+#define c_swap(T, x, y) do { T __t = x; x = y; y = __t; } while (0)
+#define c_no_compare(x, y) (0)
+#define c_mem_equals(x, y) (memcmp(x, y, sizeof(*(y))) == 0)
+#define c_default_equals(x, y) (*(x) == *(y))
+#define c_default_less(x, y) (*(x) < *(y))
+#define c_less_compare(less, x, y) (less(y, x) - less(x, y))
+#define c_default_compare(x, y) c_less_compare(c_default_less, x, y)
+#define c_default_from_raw(x) (x)
+#define c_default_to_raw(ptr) (*(ptr))
+#define c_default_del(ptr) ((void) (ptr))
+
+/* Generic algorithms */
+
+#define c_foreach(...) c_MACRO_OVERLOAD(c_foreach, __VA_ARGS__)
+#define c_foreach_3(it, ctype, cnt) \
+ for (ctype##_iter_t it = ctype##_begin(&cnt), it##_end_ = ctype##_end(&cnt); it.val != it##_end_.val; ctype##_next(&it))
+#define c_foreach_4(it, ctype, start, finish) \
+ for (ctype##_iter_t it = start, it##_end_ = finish; it.val != it##_end_.val; ctype##_next(&it))
+
+#define c_forrange(...) c_MACRO_OVERLOAD(c_forrange, __VA_ARGS__)
+#define c_forrange_1(stop) for (size_t _c_i=0, _c_end_=stop; _c_i < _c_end_; ++_c_i)
+#define c_forrange_2(i, stop) for (size_t i=0, i##_end_=stop; i < i##_end_; ++i)
+#define c_forrange_3(i, type, stop) for (type i=0, i##_end_=stop; i < i##_end_; ++i)
+#define c_forrange_4(i, type, start, stop) for (type i=start, i##_end_=stop; i < i##_end_; ++i)
+#define c_forrange_5(i, type, start, stop, step) \
+ for (type i=start, i##_inc_=step, i##_end_=(stop) - (0 < i##_inc_); (i <= i##_end_) == (0 < i##_inc_); i += i##_inc_)
+
+#define c_withfile(f, open) for (FILE *f = open; f; fclose(f), f = NULL)
+
+#define c_push_items(self, ctype, ...) do { \
+ const ctype##_input_t __arr[] = __VA_ARGS__; \
+ ctype##_push_n(self, __arr, sizeof __arr/sizeof *__arr); \
+} while (0)
+
+#define c_del(ctype, ...) do { \
+ ctype##_t* __arr[] = {__VA_ARGS__}; \
+ for (size_t __i=0; __i<sizeof __arr/sizeof *__arr; ++__i) \
+ ctype##_del(__arr[__i]); \
+} while (0)
+
+#endif
|
