From 84dd4fe2a1e0768b199bebcbb9d59e9f7f57d3cc Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Sun, 20 Sep 2020 09:03:23 +0200 Subject: Cleanup and renamed c_del_() to c_del() again. --- examples/replace.c | 2 +- stc/cdefs.h | 36 +++++++++++++----------------------- stc/cstr.h | 6 +++--- stc/cvec.h | 2 +- 4 files changed, 18 insertions(+), 28 deletions(-) diff --git a/examples/replace.c b/examples/replace.c index c4929864..5c5192e2 100644 --- a/examples/replace.c +++ b/examples/replace.c @@ -29,5 +29,5 @@ int main () cstr_replace(&s, 22, 1, "!!!"); // "this is a short phrase!!!" (5) printf("(5) %s\n", s.str); - c_del_(cstr, &s, &m); // destroy + c_del(cstr, &s, &m); // destroy } diff --git a/stc/cdefs.h b/stc/cdefs.h index 9dc496d1..ebf24738 100644 --- a/stc/cdefs.h +++ b/stc/cdefs.h @@ -29,26 +29,21 @@ #include #if defined(_MSC_VER) -#define STC_FORCE_INLINE static __forceinline + #define STC_FORCE_INLINE static __forceinline #elif defined(__GNUC__) || defined(__clang__) -#define STC_FORCE_INLINE static inline __attribute((always_inline)) + #define STC_FORCE_INLINE static inline __attribute((always_inline)) #else -#define STC_FORCE_INLINE static inline + #define STC_FORCE_INLINE static inline #endif #define STC_INLINE static inline #if defined(STC_HEADER) || defined(STC_IMPLEMENTATION) -#define STC_API extern + #define STC_API extern #else -#define STC_API STC_INLINE -#endif - -#if defined(_MSC_VER) || defined(__GNUC__) || defined(__clang__) || defined(__TINYC__) -#define c_UNION(...) union {__VA_ARGS__;} -#else -#define c_UNION(x, ...) x + #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__ @@ -59,25 +54,20 @@ #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 FOO(...) c_MACRO_OVERLOAD(FOO, __VA_ARGS__) #define FOO_1(x) "1" #define FOO_2(x,y) "2"*/ - -#define c_new(T) ((T *) malloc(sizeof(T))) -#define c_new_n(T, n) ((T *) malloc(sizeof(T) * (n))) #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_max_alloca (512) -#define c_swap(T, x, y) do { T __t = x; x = y; y = __t; } while (0) - -#define c_default_from_raw(x) (x) -#define c_default_to_raw(ptr) (*(ptr)) + ((type *)((char *)(ptr) - offsetof(type, member))) +#define c_new(T) ((T *) malloc(sizeof(T))) +#define c_new_n(T, n) ((T *) malloc(sizeof(T) * (n))) +#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(x, y) ? -1 : less(y, x)) #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)) #define c_foreach(...) c_MACRO_OVERLOAD(c_foreach, __VA_ARGS__) @@ -99,7 +89,7 @@ ctype##_push_n(self, __arr, sizeof(__arr)/sizeof(__arr[0])); \ } while (0) -#define c_del_(ctype, ...) do { \ +#define c_del(ctype, ...) do { \ struct ctype* __arr[] = {__VA_ARGS__}; \ for (size_t i=0; i c_max_alloca ? malloc(n) : buf, str, n); + char buf[_c_max_buffer]; + char* xstr = (char *) memcpy(n > _c_max_buffer ? malloc(n) : buf, str, n); _cstr_internal_move(self, pos + len, pos + n); memcpy(&self->str[pos], xstr, n); - if (n > c_max_alloca) free(xstr); + if (n > _c_max_buffer) free(xstr); } STC_API void diff --git a/stc/cvec.h b/stc/cvec.h index f3c203ca..44387858 100644 --- a/stc/cvec.h +++ b/stc/cvec.h @@ -240,7 +240,7 @@ \ STC_API cvec_##X##_iter_t \ cvec_##X##_insert_range_p(cvec_##X* self, cvec_##X##_value_t* pos, cvec_##X##_value_t* first, cvec_##X##_value_t* finish) { \ - enum {max_buf = c_max_alloca / sizeof(Value) + 1}; \ + enum {max_buf = _c_max_buffer / sizeof(Value) + 1}; \ Value buf[max_buf]; \ size_t len = finish - first, idx = pos - self->data, size = cvec_size(*self); \ cvec_##X##_value_t* xbuf = (len > max_buf ? c_new_n(cvec_##X##_value_t, len) : &buf[0]); \ -- cgit v1.2.3