/* common.h — project-wide types, attributes, and error codes. * * Pure C11. No allocation, no platform calls; safe to include everywhere. */ #ifndef NAUT_COMMON_H #define NAUT_COMMON_H #include #include #include #include /* --- sizes ------------------------------------------------------------- */ #define NAUT_CACHELINE 64u #define NAUT_PAGE 4096u /* BitTorrent wire block size (BEP-3). The fundamental transfer unit. */ #define NAUT_BLOCK (16u * 1024u) /* --- compiler attributes ----------------------------------------------- */ #define NAUT_LIKELY(x) __builtin_expect(!!(x), 1) #define NAUT_UNLIKELY(x) __builtin_expect(!!(x), 0) #define NAUT_INLINE static inline __attribute__((always_inline)) #define NAUT_ALIGNED(n) __attribute__((aligned(n))) #define NAUT_CACHE_ALIGNED __attribute__((aligned(NAUT_CACHELINE))) #define NAUT_NORETURN __attribute__((noreturn)) #define NAUT_UNUSED __attribute__((unused)) #define NAUT_PACKED __attribute__((packed)) #define NAUT_MUST_USE __attribute__((warn_unused_result)) #define NAUT_PRINTF(fi, ai) __attribute__((format(printf, fi, ai))) /* --- small helpers ----------------------------------------------------- */ #define NAUT_ARRAY_LEN(a) (sizeof(a) / sizeof((a)[0])) #define NAUT_MIN(a, b) ((a) < (b) ? (a) : (b)) #define NAUT_MAX(a, b) ((a) > (b) ? (a) : (b)) #define NAUT_ALIGN_UP(x, a) (((uintptr_t)(x) + ((a) - 1)) & ~((uintptr_t)(a) - 1)) #define NAUT_ALIGN_DOWN(x, a) ((uintptr_t)(x) & ~((uintptr_t)(a) - 1)) #define NAUT_IS_POW2(x) ((x) != 0 && (((x) & ((x) - 1)) == 0)) #define NAUT_CONTAINER_OF(ptr, type, member) \ ((type *)((char *)(1 ? (ptr) : &((type *)0)->member) - offsetof(type, member))) /* --- error codes ------------------------------------------------------- */ typedef int naut_err; enum { NAUT_OK = 0, NAUT_ERR_NOMEM = -1, NAUT_ERR_INVAL = -2, NAUT_ERR_IO = -3, NAUT_ERR_AGAIN = -4, /* would block / retry */ NAUT_ERR_PROTO = -5, /* protocol violation */ NAUT_ERR_RANGE = -6, NAUT_ERR_NOSYS = -7, /* unsupported by kernel/build */ NAUT_ERR_FULL = -8, NAUT_ERR_EMPTY = -9, NAUT_ERR_NOTFOUND = -10, NAUT_ERR_EXIST = -11, /* already exists / data would overlap */ }; const char *naut_strerror(naut_err e); #endif /* NAUT_COMMON_H */