From b68672ec603ade58fc880c7a95ad854de040ad3e Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 8 Oct 2020 14:29:36 +0200 Subject: Renamed cdefs.h to ccommon.h --- README.md | 2 +- stc/carray.h | 2 +- stc/ccommon.h | 110 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ stc/cdefs.h | 110 ---------------------------------------------------------- stc/clist.h | 2 +- stc/cmap.h | 2 +- stc/cptr.h | 2 +- stc/crandom.h | 2 +- stc/cstr.h | 2 +- stc/cvec.h | 2 +- 10 files changed, 118 insertions(+), 118 deletions(-) create mode 100644 stc/ccommon.h delete mode 100644 stc/cdefs.h diff --git a/README.md b/README.md index 4f2c0161..af7cdf8e 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ An elegant, fully typesafe, generic, customizable, user-friendly, consistent, an - **stc/cptr.h** - Support for pointers in containers, and a reference counted shared pointer **csptr**. - **stc/coption.h** - Implementation of a **getopt_long()**-like function, *coption_get()*, to parse command line arguments. - **stc/crandom.h** - A few very efficent modern random number generators *pcg32* and my own *64-bit PRNG* inspired by *sfc64*. Both uniform and normal distributions. -- **stc/cdefs.h** - A common include file with a few general definitions. +- **stc/ccommon.h** - A common include file with a few general definitions. The usage of the containers is vert similar to the C++ standard containers, so it should be easy if you are familiar with them. diff --git a/stc/carray.h b/stc/carray.h index cf46bbba..5858b004 100644 --- a/stc/carray.h +++ b/stc/carray.h @@ -24,7 +24,7 @@ #define CARRAY__H__ #include -#include "cdefs.h" +#include "ccommon.h" /* Multi-dimensional generic array allocated as one block of heap-memory. 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 +#include +#include +#include + +#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 -#include -#include -#include - -#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 -#include "cdefs.h" +#include "ccommon.h" /* Circular Singly-linked Lists. diff --git a/stc/cmap.h b/stc/cmap.h index b602f6f4..9c377177 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -51,7 +51,7 @@ int main(void) { #include #include -#include "cdefs.h" +#include "ccommon.h" #define cmap_INIT {NULL, NULL, 0, 0, 0.85f, 0.15f} #define cmap_empty(m) ((m).size == 0) diff --git a/stc/cptr.h b/stc/cptr.h index 19543c96..8b3be064 100644 --- a/stc/cptr.h +++ b/stc/cptr.h @@ -23,7 +23,7 @@ #ifndef CPTR__H__ #define CPTR__H__ -#include "cdefs.h" +#include "ccommon.h" /* cptr: std::unique_ptr -like type: */ /* diff --git a/stc/crandom.h b/stc/crandom.h index d0d9df36..89793c94 100644 --- a/stc/crandom.h +++ b/stc/crandom.h @@ -24,7 +24,7 @@ #ifndef CRANDOM__H__ #define CRANDOM__H__ -#include "cdefs.h" +#include "ccommon.h" #include #include /* diff --git a/stc/cstr.h b/stc/cstr.h index 41d479e9..b227e55f 100644 --- a/stc/cstr.h +++ b/stc/cstr.h @@ -27,7 +27,7 @@ #include #include #include /* vsnprintf */ -#include "cdefs.h" +#include "ccommon.h" typedef struct cstr { char* str; } cstr_t; typedef struct { char *val; } cstr_iter_t; diff --git a/stc/cvec.h b/stc/cvec.h index a5cd97ab..037b33ab 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -25,7 +25,7 @@ #include #include -#include "cdefs.h" +#include "ccommon.h" #define cvec_INIT {NULL} #define cvec_size(v) _cvec_safe_size((v).data) -- cgit v1.2.3