diff --git a/include/macros.h b/include/macros.h index 553235f..77a0b72 100644 --- a/include/macros.h +++ b/include/macros.h @@ -55,7 +55,13 @@ #define _cconv__hot _hot #define _cconv__entry __attribute__((constructor)) #define _cconv__exit __attribute__((destructor)) + +// Allocates #define _cconv__alloc __attribute__((malloc)) +// Infallible alloc +#define _cconv__ialloc __attribute__((malloc)) __attribute__((returns_nonnull)) +#define _cconv__nonnull __attribute__((returns_nonnull)) + #define _callcv(name) _cconv__ ## name #define _callconv(name) _callcv(name) @@ -130,6 +136,7 @@ _mixin void _drain_val(void* x, ...) { IGNORE(x); } // This compiles to no-op on #define box(t) aligned_alloc(_Alignof(t), sizeof(t)) #define box_value(v) ({ let _box = box(var(v)); *_box = (v); _box; }) +#define unbox_value(v) ({ let _v = (v); let _res = *_v; free(_v); _res; }) #define stackalloc(t) __builtin_alloca_with_align(sizeof(t), _Alignof(t)) // Function macros @@ -256,4 +263,9 @@ static_assert_eq(bswap(bswap(128lu)), 128, "bswap128 (lu) failed (3)"); #define VERSION_REV(ver) _AS_u32(_AS_u32(ver) & 0xffu) _Static_assert( (VERSION_COMP(VERSION(1,2,3,4), VER_COMP_MIN) >> VER_COMP_MIN) == 2u, "invalid native version spec"); +// Misc. + +#define PTR_ASSIGN(ptr, val) ( (ptr) ? (*(ptr) = (val), (ptr)) : (ptr) ) +#define PTR_ASSIGNv(ptr, val) ({ let _ptr = (ptr); let _val = (val); PTR_ASSIGN(_ptr, _val); }) + #endif /* _MACROS_H */ diff --git a/src/display.c b/src/display.c index 56cf5e6..5096d7a 100644 --- a/src/display.c +++ b/src/display.c @@ -12,11 +12,11 @@ #define DFLAGSET(f, flagname) FLAGSET(f, DISPF_SHOW_ ## flagname) #define CMP(x,cmp,y) ( (x) cmp (y) ? (x) : (y) ) -#define CMPx(x,cmp,y) ({ let _x = (x); let _y = (y); CMP(_x, cmp, _y); }) +#define CMPv(x,cmp,y) ({ let _x = (x); let _y = (y); CMP(_x, cmp, _y); }) #define MIN(x, y) CMP((x), <, (y)) -#define MINx(x, y) CMPx((x), <, (y)) +#define MINv(x, y) CMPv((x), <, (y)) #define MAX(x, y) CMP((x), >, (y)) -#define MAXx(x, y) CMPx((x), >, (y)) +#define MAXv(x, y) CMPv((x), >, (y)) // Returns number of bytes written to `dest`. Otherwise same as `strncpy()`. static usize strncpy_n(char*pOUT dest, const char* restrict src, usize n) @@ -42,7 +42,7 @@ static usize s_strncpy_n(usize sn, usize dn; char dest[static pOUT dn], usize dn // Will not write past `dn` bytes of `dest`, will not read past `sn` bytes of `src`. Otherwise, same as `strncat_n()`. static inline usize s_strncat_n(usize sn, usize dn; char dest[static pOUT dn], usize dn, const char src[static restrict sn], usize sn) { - usize dl = MINx(strlen(dest), dn); + usize dl = MINv(strlen(dest), dn); return s_strncpy_n(dest + dl, dn - dl, src, sn); } @@ -75,10 +75,10 @@ static usize _display_get_fmt_n(dispflags_t flags, bool matched, usize _n, char //TODO: Fix this... It isn't working... #define ADDSTR(s) do { \ debug_assert((s)); \ - w = MINx(s_strncat_n(str, n, (s), n), n); \ + w = MINv(s_strncat_n(str, n, (s), n), n); \ /* Remove written from `n` */ \ n -= w; \ - /* Check if we have written into the limit (if so, w will be equal to n above, since w is high-bounded to n by `MINx()` above. So if n is 0, we have hit the limit. */ \ + /* Check if we have written into the limit (if so, w will be equal to n above, since w is high-bounded to n by `MINv()` above. So if n is 0, we have hit the limit. */ \ ifU(!n) { \ /*XXX: Should this be `str[w-1]?` If this assert fails then yes it should. */ \ debug_assert(w < _n); \