import "libc.hff"; /// Macros defmacro assert { (ex, s, ...args) [ (do if !(ex) { fprintf(stderr, "%s:%d: assertion failed: ", #FILE, #LINE); fprintf(stderr, s, args); fprintf(stderr, "\n"); abort(); } ) ] } defmacro foreach(x, i, a, &body) [ { let $a = a; for let i = 0z; i < $a.#len; ++i { let x = $a[i]; { body } } } ] defmacro foreach_ptr(x, i, a, &body) [ { let $a = a; for let i = 0z; i < $a.#len; ++i { let x = &$a[i]; { body } } } ] defmacro streq(a,b) [ (strcmp(a,b) == 0) ] defmacro strcieq(a,b) [ (strcasecmp(a,b) == 0) ] defmacro with_tmpchange(var,x,&body) [ { let $tmp = (var); (var) = x; { body } (var) = $tmp; } ] defmacro MAX(a,b) [((a) > (b) ? (a) : (b))] defmacro container_of(x, T, fld) [ (as(*T)(as(*void)(x) - offsetof(T, fld))) ] // Inline functions fn bswap32(x u32) u32 { return (x >> 24) | ((x >> 8) & 0x00FF00) | ((x << 8) & 0xFF0000) | (x << 24); } fn bswap64(x u64) u64 { return (as(u64)bswap32(x) << 32) | (bswap32(x >> 32)); } fn spanz(x *const u8) [#]const u8 { return x[0::strlen(x)]; }