--- nono/lib/header.h 2026/04/29 17:05:05 1.1.1.14 +++ nono/lib/header.h 2026/04/29 17:05:56 1.1.1.23 @@ -4,16 +4,14 @@ // Licensed under nono-license.txt // -#pragma once +// +// 基本ヘッダ +// -#define NONO_MAJOR_VER (0) -#define NONO_MINOR_VER (2) -#define NONO_PATCH_VER (3) -#define NONO_DATE "2021/10/03" +#pragma once #include "config-nono.h" #include -#include #include #include #include @@ -27,7 +25,6 @@ #include #include #include -#include #include using uint8 = uint8_t; @@ -39,18 +36,17 @@ using int16 = int16_t; using int32 = int32_t; using int64 = int64_t; -#if defined(HAVE_ENDIAN_H) -#include -#elif defined(HAVE_SYS_ENDIAN_H) -#include -#else -#include "missing_endian.h" -#endif - #if defined(HAVE_BSD_BSD_H) #include #endif +#include "missing_endian.h" + +// For cppcheck... +#if !defined(__CONCAT) +#define __CONCAT(a, b) a ## b +#endif + #if !defined(__printflike) #if defined(HAVE___ATTRIBUTE_FORMAT) # define __printflike(a,b) __attribute__((__format__(__printf__, (a), (b)))) @@ -82,15 +78,18 @@ using int64 = int64_t; #endif #endif -#if !defined(__predict_true) +// FreeBSD(12.1) の __predict_true/false は定義がいまいちで使えない。 +// __builtin_expect() があることが分かればどの環境でも自前で定義できるので +// 既存定義は取り消す。 #if defined(HAVE___BUILTIN_EXPECT) +# undef __predict_true +# undef __predict_false # define __predict_true(exp) __builtin_expect((exp) != 0, 1) # define __predict_false(exp) __builtin_expect((exp) != 0, 0) -#else +#elif !defined(__predict_true) # define __predict_true(exp) (exp) # define __predict_false(exp) (exp) #endif -#endif #if !defined(__unused) #if defined(HAVE___ATTRIBUTE_UNUSED) @@ -114,20 +113,9 @@ using int64 = int64_t; # define NO_SANITIZE(x) #endif -// x86_64 では SSE のために16バイト境界に整列させたいが、ほかの -// アーキテクチャでは alignas(16) がエラーになる場合がある。この場合の -// alignas() は SSE のために必要なので、他アーキテクチャではそもそも -// 指定しなくていい。ということで x86_64 の時に限って指定したい時に使う。 -// うーん、なんだこれ。 -#if defined(__x86_64__) -#define ALIGNAS_IF_X86(n) alignas(n) -#else -#define ALIGNAS_IF_X86(n) /**/ -#endif - // 64ビット数と2つの32ビット数を扱う共用体。 // 構造はまったく同じだが名前だけ場所に合わせてつけたい。 -#if _BYTE_ORDER == _LITTLE_ENDIAN +#if BYTE_ORDER == LITTLE_ENDIAN #define DEF_UNION64(NAME, Q, H, L) \ union NAME { \ uint64 Q; \ @@ -147,12 +135,21 @@ DEF_UNION64(union64, q, h, l); #define countof(x) (sizeof(x) / sizeof(x[0])) #endif +#ifndef howmany +#define howmany(x, y) (((x)+((y)-1))/(y)) +#endif + #ifndef roundup #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) #endif +#ifndef rounddown +#define rounddown(x, y) (((x)/(y))*(y)) +#endif + // メッセージ付きの assert。 // 本当は後ろに足したいのだがそれは無理なので、せめてそれっぽく1行にしてみる。 +#if !defined(NDEBUG) #define assertmsg(expr, ...) do { \ if (__predict_false(!(expr))) { \ fprintf(stderr, __VA_ARGS__); \ @@ -160,26 +157,20 @@ DEF_UNION64(union64, q, h, l); assert(expr); \ } \ } while (0) +#else +#define assertmsg(expr, ...) +#endif -// atomic_compare_exchange_strong() の短縮マクロ。 -// ライブラリ実装は第2引数の expected が値ではなくポインタなので一旦代入操作を -// 行わないといけないのと、第3引数の newval は型が uint8 だと int 即値が書けず -// (int8) でキャストしないといけない。 -// その上 atomic_compare_exchange_strong() が長い、と、いろいろ微妙なので、 -// その辺を短くするマクロを用意する。ただしこれでは expected 変数の型が -// 自動判別できないので、_8 と _32 を用意するところは妥協。 -// (static) inline 関数にすることも出来るけどそうするとここで ヘッダ -// を読み込まなければならなくなるため、マクロで実現する。 -#define atomic_cas_8(varp, expval, newval) ({ \ - uint8 expected = (expval); \ - atomic_compare_exchange_strong((varp), &expected, (uint8)(newval)); \ -}) -#define atomic_cas_32(varp, expval, newval) ({ \ - uint32 expected = (expval); \ - atomic_compare_exchange_strong((varp), &expected, (uint32)(newval)); \ -}) - -// ログ -#define PANIC(...) panic(__PRETTY_FUNCTION__, __VA_ARGS__) -[[noreturn]] extern void panic(const char *funcname, const char *fmt, ...) - __printflike(2, 3); +// __func__ のようだけどクラス名と関数名だけを表示する。 +// __PRETTY_FUNCTION__ が近いけど、あれは戻り値やら引数全部表示するので。 +#define __method__ \ + (get_classfunc_name(__PRETTY_FUNCTION__, __FUNCTION__).c_str()) +extern std::string get_classfunc_name(const char *pretty, const char *func); + +// iconv() の第2引数の型は OS によって違う… +#if defined(HAVE_ICONV_CONST) +#define ICONV(cd, s, slen, d, dlen) iconv((cd), (s), (slen), (d), (dlen)) +#else +#define ICONV(cd, s, slen, d, dlen) \ + iconv((cd), const_cast(s), (slen), (d), (dlen)) +#endif