commit 02a405e873ca5409a238fa9934285708a560b06a parent 0d283558db991dc3a6b8e81f2d2d822cda4747d2 Author: William Casarin <jb55@jb55.com> Date: Sat, 23 Dec 2023 09:30:08 -0800 move everything to src Diffstat:
83 files changed, 351 insertions(+), 399 deletions(-)
diff --git a/Makefile b/Makefile @@ -1,8 +1,8 @@ CFLAGS = -Wall -Wno-misleading-indentation -Wno-unused-function -Werror -O2 -g -Ideps/secp256k1/include -Ideps/lmdb -Ideps/flatcc/include -HEADERS = sha256.h nostrdb.h cursor.h hex.h jsmn.h config.h sha256.h random.h memchr.h cpu.h $(C_BINDINGS) +HEADERS = src/sha256.h src/nostrdb.h src/cursor.h src/hex.h src/jsmn.h src/config.h src/sha256.h src/random.h src/memchr.h src/cpu.h $(C_BINDINGS) FLATCC_SRCS=deps/flatcc/src/runtime/json_parser.c deps/flatcc/src/runtime/verifier.c deps/flatcc/src/runtime/builder.c deps/flatcc/src/runtime/emitter.c deps/flatcc/src/runtime/refmap.c -BOLT11_SRCS = bolt11/bolt11.c bolt11/bech32.c bolt11/tal.c bolt11/talstr.c bolt11/take.c bolt11/list.c bolt11/utf8.c bolt11/amount.c bolt11/hash_u5.c -SRCS = nostrdb.c sha256.c $(BOLT11_SRCS) $(FLATCC_SRCS) +BOLT11_SRCS = src/bolt11/bolt11.c src/bolt11/bech32.c src/bolt11/tal.c src/bolt11/talstr.c src/bolt11/take.c src/bolt11/list.c src/bolt11/utf8.c src/bolt11/amount.c src/bolt11/hash_u5.c +SRCS = src/nostrdb.c src/sha256.c $(BOLT11_SRCS) $(FLATCC_SRCS) LDS = $(OBJS) $(ARS) OBJS = $(SRCS:.c=.o) DEPS = $(OBJS) $(HEADERS) $(ARS) @@ -11,11 +11,11 @@ LMDB_VER=0.9.31 FLATCC_VER=05dc16dc2b0316e61063bb1fc75426647badce48 PREFIX ?= /usr/local SUBMODULES = deps/secp256k1 -C_BINDINGS_PROFILE=bindings/c/profile_builder.h bindings/c/profile_reader.h bindings/c/profile_verifier.h bindings/c/profile_json_parser.h -C_BINDINGS_META=bindings/c/meta_builder.h bindings/c/meta_reader.h bindings/c/meta_verifier.h bindings/c/meta_json_parser.h -C_BINDINGS_COMMON=bindings/c/flatbuffers_common_builder.h bindings/c/flatbuffers_common_reader.h +BINDINGS=src/bindings +C_BINDINGS_PROFILE=$(BINDINGS)/c/profile_builder.h $(BINDINGS)/c/profile_reader.h $(BINDINGS)/c/profile_verifier.h $(BINDINGS)/c/profile_json_parser.h +C_BINDINGS_META=$(BINDINGS)/c/meta_builder.h $(BINDINGS)/c/meta_reader.h $(BINDINGS)/c/meta_verifier.h $(BINDINGS)/c/meta_json_parser.h +C_BINDINGS_COMMON=$(BINDINGS)/c/flatbuffers_common_builder.h $(BINDINGS)/c/flatbuffers_common_reader.h C_BINDINGS=$(C_BINDINGS_COMMON) $(C_BINDINGS_PROFILE) $(C_BINDINGS_META) -BINDINGS=bindings BIN=ndb CHECKDATA=testdata/db/v0/data.mdb @@ -25,7 +25,7 @@ all: lib ndb lib: benches test ndb: ndb.c $(DEPS) - $(CC) $(CFLAGS) ndb.c $(LDS) -o $@ + $(CC) -Isrc $(CFLAGS) ndb.c $(LDS) -o $@ bindings: bindings-swift bindings-rust bindings-c @@ -53,42 +53,42 @@ config.h: configurator bindings-c: $(C_BINDINGS) -bindings/%/.dir: +src/bindings/%/.dir: mkdir -p $(shell dirname $@) touch $@ -bindings/c/%_builder.h: schemas/%.fbs bindings/c/.dir - flatcc --builder $< -o bindings/c +src/bindings/c/%_builder.h: schemas/%.fbs $(BINDINGS)/c/.dir + flatcc --builder $< -o $(BINDINGS)/c -bindings/c/%_verifier.h bindings/c/%_reader.h: schemas/%.fbs bindings/c/.dir - flatcc --verifier -o bindings/c $< +src/bindings/c/%_verifier.h bindings/c/%_reader.h: schemas/%.fbs $(BINDINGS)/c/.dir + flatcc --verifier -o $(BINDINGS)/c $< -bindings/c/flatbuffers_common_reader.h: bindings/c/.dir - flatcc --common_reader -o bindings/c +src/bindings/c/flatbuffers_common_reader.h: $(BINDINGS)/c/.dir + flatcc --common_reader -o $(BINDINGS)/c -bindings/c/flatbuffers_common_builder.h: bindings/c/.dir - flatcc --common_builder -o bindings/c +src/bindings/c/flatbuffers_common_builder.h: $(BINDINGS)/c/.dir + flatcc --common_builder -o $(BINDINGS)/c -bindings/c/%_json_parser.h: schemas/%.fbs bindings/c/.dir - flatcc --json-parser $< -o bindings/c +src/bindings/c/%_json_parser.h: schemas/%.fbs $(BINDINGS)/c/.dir + flatcc --json-parser $< -o $(BINDINGS)/c -bindings-rust: bindings/rust/ndb_profile.rs bindings/rust/ndb_meta.rs +bindings-rust: $(BINDINGS)/rust/ndb_profile.rs $(BINDINGS)/rust/ndb_meta.rs -bindings/rust/ndb_profile.rs: schemas/profile.fbs bindings/rust +$(BINDINGS)/rust/ndb_profile.rs: schemas/profile.fbs $(BINDINGS)/rust flatc --gen-json-emit --rust $< @mv profile_generated.rs $@ -bindings/rust/ndb_meta.rs: schemas/meta.fbs bindings/swift +$(BINDINGS)/rust/ndb_meta.rs: schemas/meta.fbs $(BINDINGS)/swift flatc --rust $< @mv meta_generated.rs $@ -bindings-swift: bindings/swift/NdbProfile.swift bindings/swift/NdbMeta.swift +bindings-swift: $(BINDINGS)/swift/NdbProfile.swift $(BINDINGS)/swift/NdbMeta.swift -bindings/swift/NdbProfile.swift: schemas/profile.fbs bindings/swift +$(BINDINGS)/swift/NdbProfile.swift: schemas/profile.fbs $(BINDINGS)/swift flatc --gen-json-emit --swift $< @mv profile_generated.swift $@ -bindings/swift/NdbMeta.swift: schemas/meta.fbs bindings/swift +$(BINDINGS)/swift/NdbMeta.swift: schemas/meta.fbs $(BINDINGS)/swift flatc --swift $< @mv meta_generated.swift $@ @@ -135,9 +135,6 @@ deps/secp256k1/config.log: deps/secp256k1/configure deps/lmdb/liblmdb.a: deps/lmdb/lmdb.h $(MAKE) -C deps/lmdb liblmdb.a -bench: bench.c $(DEPS) - $(CC) $(CFLAGS) bench.c $(LDS) -o $@ - testdata/db/ndb-v0.tar.zst: curl https://cdn.jb55.com/s/ndb-v0.tar.zst -o $@ @@ -155,14 +152,14 @@ testdata/many-events.json.zst: testdata/many-events.json: testdata/many-events.json.zst zstd -d $< -bench-ingest-many: bench-ingest-many.c $(DEPS) testdata/many-events.json - $(CC) $(CFLAGS) $< $(LDS) -o $@ +bench: bench-ingest-many.c $(DEPS) testdata/many-events.json + $(CC) -Isrc $(CFLAGS) $< $(LDS) -o $@ testdata/db/.dir: @mkdir -p testdata/db touch testdata/db/.dir test: test.c $(DEPS) testdata/db/.dir - $(CC) $(CFLAGS) test.c $(LDS) -o $@ + $(CC) -Isrc $(CFLAGS) test.c $(LDS) -o $@ .PHONY: tags clean diff --git a/bench.c b/bench.c @@ -1,51 +0,0 @@ - -#include "io.h" -#include "nostrdb.h" -#include <sys/mman.h> -#include <time.h> -#include <stdlib.h> - -static int bench_parser(int times, const char *json, int len) -{ - static unsigned char buf[2<<18]; - - struct timespec t1, t2; - int i; - long nanos, ms; - struct ndb_note *note; - - clock_gettime(CLOCK_MONOTONIC, &t1); - for (i = 0; i < times; i++) { - if (!ndb_note_from_json(json, len, ¬e, buf, sizeof(buf))) { - return 0; - } - } - clock_gettime(CLOCK_MONOTONIC, &t2); - - nanos = (t2.tv_sec - t1.tv_sec) * (long)1e9 + (t2.tv_nsec - t1.tv_nsec); - ms = nanos / 1e6; - printf("ns/run\t%ld\nms/run\t%f\nns\t%ld\nms\t%ld\n", - nanos/times, (double)ms/(double)times, nanos, ms); - - return 1; -} - -int main(int argc, char *argv[], char **env) -{ - static const int alloc_size = 2 << 18; - int times = 10000, len = 0; - unsigned char buf[alloc_size]; - - if (!read_file("testdata/contacts.json", buf, alloc_size, &len)) - return 1; - - if (argc >= 2) - times = atoi(argv[1]); - - fprintf(stderr, "benching parser %d times\n", times); - if (!bench_parser(times, (const char*)&buf[0], len)) - return 2; - - return 0; -} - diff --git a/compiler.h b/compiler.h @@ -1,317 +0,0 @@ -/* CC0 (Public domain) - see LICENSE file for details */ -#ifndef CCAN_COMPILER_H -#define CCAN_COMPILER_H -#include "config.h" - -#ifndef COLD -#if HAVE_ATTRIBUTE_COLD -/** - * COLD - a function is unlikely to be called. - * - * Used to mark an unlikely code path and optimize appropriately. - * It is usually used on logging or error routines. - * - * Example: - * static void COLD moan(const char *reason) - * { - * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno)); - * } - */ -#define COLD __attribute__((__cold__)) -#else -#define COLD -#endif -#endif - -#ifndef NORETURN -#if HAVE_ATTRIBUTE_NORETURN -/** - * NORETURN - a function does not return - * - * Used to mark a function which exits; useful for suppressing warnings. - * - * Example: - * static void NORETURN fail(const char *reason) - * { - * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno)); - * exit(1); - * } - */ -#define NORETURN __attribute__((__noreturn__)) -#else -#define NORETURN -#endif -#endif - -#ifndef PRINTF_FMT -#if HAVE_ATTRIBUTE_PRINTF -/** - * PRINTF_FMT - a function takes printf-style arguments - * @nfmt: the 1-based number of the function's format argument. - * @narg: the 1-based number of the function's first variable argument. - * - * This allows the compiler to check your parameters as it does for printf(). - * - * Example: - * void PRINTF_FMT(2,3) my_printf(const char *prefix, const char *fmt, ...); - */ -#define PRINTF_FMT(nfmt, narg) \ - __attribute__((format(__printf__, nfmt, narg))) -#else -#define PRINTF_FMT(nfmt, narg) -#endif -#endif - -#ifndef CONST_FUNCTION -#if HAVE_ATTRIBUTE_CONST -/** - * CONST_FUNCTION - a function's return depends only on its argument - * - * This allows the compiler to assume that the function will return the exact - * same value for the exact same arguments. This implies that the function - * must not use global variables, or dereference pointer arguments. - */ -#define CONST_FUNCTION __attribute__((__const__)) -#else -#define CONST_FUNCTION -#endif - -#ifndef PURE_FUNCTION -#if HAVE_ATTRIBUTE_PURE -/** - * PURE_FUNCTION - a function is pure - * - * A pure function is one that has no side effects other than it's return value - * and uses no inputs other than it's arguments and global variables. - */ -#define PURE_FUNCTION __attribute__((__pure__)) -#else -#define PURE_FUNCTION -#endif -#endif -#endif - -#if HAVE_ATTRIBUTE_UNUSED -#ifndef UNNEEDED -/** - * UNNEEDED - a variable/function may not be needed - * - * This suppresses warnings about unused variables or functions, but tells - * the compiler that if it is unused it need not emit it into the source code. - * - * Example: - * // With some preprocessor options, this is unnecessary. - * static UNNEEDED int counter; - * - * // With some preprocessor options, this is unnecessary. - * static UNNEEDED void add_to_counter(int add) - * { - * counter += add; - * } - */ -#define UNNEEDED __attribute__((__unused__)) -#endif - -#ifndef NEEDED -#if HAVE_ATTRIBUTE_USED -/** - * NEEDED - a variable/function is needed - * - * This suppresses warnings about unused variables or functions, but tells - * the compiler that it must exist even if it (seems) unused. - * - * Example: - * // Even if this is unused, these are vital for debugging. - * static NEEDED int counter; - * static NEEDED void dump_counter(void) - * { - * printf("Counter is %i\n", counter); - * } - */ -#define NEEDED __attribute__((__used__)) -#else -/* Before used, unused functions and vars were always emitted. */ -#define NEEDED __attribute__((__unused__)) -#endif -#endif - -#ifndef UNUSED -/** - * UNUSED - a parameter is unused - * - * Some compilers (eg. gcc with -W or -Wunused) warn about unused - * function parameters. This suppresses such warnings and indicates - * to the reader that it's deliberate. - * - * Example: - * // This is used as a callback, so needs to have this prototype. - * static int some_callback(void *unused UNUSED) - * { - * return 0; - * } - */ -#define UNUSED __attribute__((__unused__)) -#endif -#else -#ifndef UNNEEDED -#define UNNEEDED -#endif -#ifndef NEEDED -#define NEEDED -#endif -#ifndef UNUSED -#define UNUSED -#endif -#endif - -#ifndef IS_COMPILE_CONSTANT -#if HAVE_BUILTIN_CONSTANT_P -/** - * IS_COMPILE_CONSTANT - does the compiler know the value of this expression? - * @expr: the expression to evaluate - * - * When an expression manipulation is complicated, it is usually better to - * implement it in a function. However, if the expression being manipulated is - * known at compile time, it is better to have the compiler see the entire - * expression so it can simply substitute the result. - * - * This can be done using the IS_COMPILE_CONSTANT() macro. - * - * Example: - * enum greek { ALPHA, BETA, GAMMA, DELTA, EPSILON }; - * - * // Out-of-line version. - * const char *greek_name(enum greek greek); - * - * // Inline version. - * static inline const char *_greek_name(enum greek greek) - * { - * switch (greek) { - * case ALPHA: return "alpha"; - * case BETA: return "beta"; - * case GAMMA: return "gamma"; - * case DELTA: return "delta"; - * case EPSILON: return "epsilon"; - * default: return "**INVALID**"; - * } - * } - * - * // Use inline if compiler knows answer. Otherwise call function - * // to avoid copies of the same code everywhere. - * #define greek_name(g) \ - * (IS_COMPILE_CONSTANT(greek) ? _greek_name(g) : greek_name(g)) - */ -#define IS_COMPILE_CONSTANT(expr) __builtin_constant_p(expr) -#else -/* If we don't know, assume it's not. */ -#define IS_COMPILE_CONSTANT(expr) 0 -#endif -#endif - -#ifndef WARN_UNUSED_RESULT -#if HAVE_WARN_UNUSED_RESULT -/** - * WARN_UNUSED_RESULT - warn if a function return value is unused. - * - * Used to mark a function where it is extremely unlikely that the caller - * can ignore the result, eg realloc(). - * - * Example: - * // buf param may be freed by this; need return value! - * static char *WARN_UNUSED_RESULT enlarge(char *buf, unsigned *size) - * { - * return realloc(buf, (*size) *= 2); - * } - */ -#define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__)) -#else -#define WARN_UNUSED_RESULT -#endif -#endif - - -#if HAVE_ATTRIBUTE_DEPRECATED -/** - * WARN_DEPRECATED - warn that a function/type/variable is deprecated when used. - * - * Used to mark a function, type or variable should not be used. - * - * Example: - * WARN_DEPRECATED char *oldfunc(char *buf); - */ -#define WARN_DEPRECATED __attribute__((__deprecated__)) -#else -#define WARN_DEPRECATED -#endif - - -#if HAVE_ATTRIBUTE_NONNULL -/** - * NO_NULL_ARGS - specify that no arguments to this function can be NULL. - * - * The compiler will warn if any pointer args are NULL. - * - * Example: - * NO_NULL_ARGS char *my_copy(char *buf); - */ -#define NO_NULL_ARGS __attribute__((__nonnull__)) - -/** - * NON_NULL_ARGS - specify that some arguments to this function can't be NULL. - * @...: 1-based argument numbers for which args can't be NULL. - * - * The compiler will warn if any of the specified pointer args are NULL. - * - * Example: - * char *my_copy2(char *buf, char *maybenull) NON_NULL_ARGS(1); - */ -#define NON_NULL_ARGS(...) __attribute__((__nonnull__(__VA_ARGS__))) -#else -#define NO_NULL_ARGS -#define NON_NULL_ARGS(...) -#endif - -#if HAVE_ATTRIBUTE_RETURNS_NONNULL -/** - * RETURNS_NONNULL - specify that this function cannot return NULL. - * - * Mainly an optimization opportunity, but can also suppress warnings. - * - * Example: - * RETURNS_NONNULL char *my_copy(char *buf); - */ -#define RETURNS_NONNULL __attribute__((__returns_nonnull__)) -#else -#define RETURNS_NONNULL -#endif - -#if HAVE_ATTRIBUTE_SENTINEL -/** - * LAST_ARG_NULL - specify the last argument of a variadic function must be NULL. - * - * The compiler will warn if the last argument isn't NULL. - * - * Example: - * char *join_string(char *buf, ...) LAST_ARG_NULL; - */ -#define LAST_ARG_NULL __attribute__((__sentinel__)) -#else -#define LAST_ARG_NULL -#endif - -#if HAVE_BUILTIN_CPU_SUPPORTS -/** - * cpu_supports - test if current CPU supports the named feature. - * - * This takes a literal string, and currently only works on glibc platforms. - * - * Example: - * if (cpu_supports("mmx")) - * printf("MMX support engaged!\n"); - */ -#define cpu_supports(x) __builtin_cpu_supports(x) -#else -#define cpu_supports(x) 0 -#endif /* HAVE_BUILTIN_CPU_SUPPORTS */ - -#endif /* CCAN_COMPILER_H */ diff --git a/bindings/c/.dir b/src/bindings/c/.dir diff --git a/bindings/c/flatbuffers_common_builder.h b/src/bindings/c/flatbuffers_common_builder.h diff --git a/bindings/c/flatbuffers_common_reader.h b/src/bindings/c/flatbuffers_common_reader.h diff --git a/bindings/c/meta_builder.h b/src/bindings/c/meta_builder.h diff --git a/bindings/c/meta_json_parser.h b/src/bindings/c/meta_json_parser.h diff --git a/bindings/c/meta_reader.h b/src/bindings/c/meta_reader.h diff --git a/bindings/c/meta_verifier.h b/src/bindings/c/meta_verifier.h diff --git a/bindings/c/profile_builder.h b/src/bindings/c/profile_builder.h diff --git a/bindings/c/profile_json_parser.h b/src/bindings/c/profile_json_parser.h diff --git a/bindings/c/profile_reader.h b/src/bindings/c/profile_reader.h diff --git a/bindings/c/profile_verifier.h b/src/bindings/c/profile_verifier.h diff --git a/bindings/rust/.dir b/src/bindings/rust/.dir diff --git a/bindings/rust/ndb_meta.rs b/src/bindings/rust/ndb_meta.rs diff --git a/bindings/rust/ndb_profile.rs b/src/bindings/rust/ndb_profile.rs diff --git a/bindings/swift/NdbMeta.swift b/src/bindings/swift/NdbMeta.swift diff --git a/bindings/swift/NdbProfile.swift b/src/bindings/swift/NdbProfile.swift diff --git a/bolt11/alignof.h b/src/bolt11/alignof.h diff --git a/bolt11/amount.c b/src/bolt11/amount.c diff --git a/bolt11/amount.h b/src/bolt11/amount.h diff --git a/bolt11/array_size.h b/src/bolt11/array_size.h diff --git a/bolt11/bech32.c b/src/bolt11/bech32.c diff --git a/bolt11/bech32.h b/src/bolt11/bech32.h diff --git a/bolt11/bech32_util.c b/src/bolt11/bech32_util.c diff --git a/bolt11/bech32_util.h b/src/bolt11/bech32_util.h diff --git a/bolt11/bolt11.c b/src/bolt11/bolt11.c diff --git a/bolt11/bolt11.h b/src/bolt11/bolt11.h diff --git a/bolt11/build_assert.h b/src/bolt11/build_assert.h diff --git a/bolt11/check_type.h b/src/bolt11/check_type.h diff --git a/bolt11/container_of.h b/src/bolt11/container_of.h diff --git a/bolt11/cppmagic.h b/src/bolt11/cppmagic.h diff --git a/bolt11/debug.h b/src/bolt11/debug.h diff --git a/bolt11/error.c b/src/bolt11/error.c diff --git a/bolt11/error.h b/src/bolt11/error.h diff --git a/bolt11/hash_u5.c b/src/bolt11/hash_u5.c diff --git a/bolt11/hash_u5.h b/src/bolt11/hash_u5.h diff --git a/bolt11/libnostrdb.a b/src/bolt11/libnostrdb.a Binary files differ. diff --git a/bolt11/likely.h b/src/bolt11/likely.h diff --git a/bolt11/list.c b/src/bolt11/list.c diff --git a/bolt11/list.h b/src/bolt11/list.h diff --git a/bolt11/mem.c b/src/bolt11/mem.c diff --git a/bolt11/mem.h b/src/bolt11/mem.h diff --git a/bolt11/node_id.c b/src/bolt11/node_id.c diff --git a/bolt11/node_id.h b/src/bolt11/node_id.h diff --git a/bolt11/overflows.h b/src/bolt11/overflows.h diff --git a/bolt11/short_types.h b/src/bolt11/short_types.h diff --git a/bolt11/str.h b/src/bolt11/str.h diff --git a/bolt11/str_debug.h b/src/bolt11/str_debug.h diff --git a/bolt11/structeq.h b/src/bolt11/structeq.h diff --git a/bolt11/take.c b/src/bolt11/take.c diff --git a/bolt11/take.h b/src/bolt11/take.h diff --git a/bolt11/tal.c b/src/bolt11/tal.c diff --git a/bolt11/tal.h b/src/bolt11/tal.h diff --git a/bolt11/talstr.c b/src/bolt11/talstr.c diff --git a/bolt11/talstr.h b/src/bolt11/talstr.h diff --git a/bolt11/typesafe_cb.h b/src/bolt11/typesafe_cb.h diff --git a/bolt11/utf8.c b/src/bolt11/utf8.c diff --git a/bolt11/utf8.h b/src/bolt11/utf8.h diff --git a/src/compiler.h b/src/compiler.h @@ -0,0 +1,323 @@ +/* CC0 (Public domain) - see LICENSE file for details */ +#ifndef CCAN_COMPILER_H +#define CCAN_COMPILER_H +#include "config.h" + +#if HAVE_UNALIGNED_ACCESS +#define alignment_ok(p, n) 1 +#else +#define alignment_ok(p, n) ((size_t)(p) % (n) == 0) +#endif + +#ifndef COLD +#if HAVE_ATTRIBUTE_COLD +/** + * COLD - a function is unlikely to be called. + * + * Used to mark an unlikely code path and optimize appropriately. + * It is usually used on logging or error routines. + * + * Example: + * static void COLD moan(const char *reason) + * { + * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno)); + * } + */ +#define COLD __attribute__((__cold__)) +#else +#define COLD +#endif +#endif + +#ifndef NORETURN +#if HAVE_ATTRIBUTE_NORETURN +/** + * NORETURN - a function does not return + * + * Used to mark a function which exits; useful for suppressing warnings. + * + * Example: + * static void NORETURN fail(const char *reason) + * { + * fprintf(stderr, "Error: %s (%s)\n", reason, strerror(errno)); + * exit(1); + * } + */ +#define NORETURN __attribute__((__noreturn__)) +#else +#define NORETURN +#endif +#endif + +#ifndef PRINTF_FMT +#if HAVE_ATTRIBUTE_PRINTF +/** + * PRINTF_FMT - a function takes printf-style arguments + * @nfmt: the 1-based number of the function's format argument. + * @narg: the 1-based number of the function's first variable argument. + * + * This allows the compiler to check your parameters as it does for printf(). + * + * Example: + * void PRINTF_FMT(2,3) my_printf(const char *prefix, const char *fmt, ...); + */ +#define PRINTF_FMT(nfmt, narg) \ + __attribute__((format(__printf__, nfmt, narg))) +#else +#define PRINTF_FMT(nfmt, narg) +#endif +#endif + +#ifndef CONST_FUNCTION +#if HAVE_ATTRIBUTE_CONST +/** + * CONST_FUNCTION - a function's return depends only on its argument + * + * This allows the compiler to assume that the function will return the exact + * same value for the exact same arguments. This implies that the function + * must not use global variables, or dereference pointer arguments. + */ +#define CONST_FUNCTION __attribute__((__const__)) +#else +#define CONST_FUNCTION +#endif + +#ifndef PURE_FUNCTION +#if HAVE_ATTRIBUTE_PURE +/** + * PURE_FUNCTION - a function is pure + * + * A pure function is one that has no side effects other than it's return value + * and uses no inputs other than it's arguments and global variables. + */ +#define PURE_FUNCTION __attribute__((__pure__)) +#else +#define PURE_FUNCTION +#endif +#endif +#endif + +#if HAVE_ATTRIBUTE_UNUSED +#ifndef UNNEEDED +/** + * UNNEEDED - a variable/function may not be needed + * + * This suppresses warnings about unused variables or functions, but tells + * the compiler that if it is unused it need not emit it into the source code. + * + * Example: + * // With some preprocessor options, this is unnecessary. + * static UNNEEDED int counter; + * + * // With some preprocessor options, this is unnecessary. + * static UNNEEDED void add_to_counter(int add) + * { + * counter += add; + * } + */ +#define UNNEEDED __attribute__((__unused__)) +#endif + +#ifndef NEEDED +#if HAVE_ATTRIBUTE_USED +/** + * NEEDED - a variable/function is needed + * + * This suppresses warnings about unused variables or functions, but tells + * the compiler that it must exist even if it (seems) unused. + * + * Example: + * // Even if this is unused, these are vital for debugging. + * static NEEDED int counter; + * static NEEDED void dump_counter(void) + * { + * printf("Counter is %i\n", counter); + * } + */ +#define NEEDED __attribute__((__used__)) +#else +/* Before used, unused functions and vars were always emitted. */ +#define NEEDED __attribute__((__unused__)) +#endif +#endif + +#ifndef UNUSED +/** + * UNUSED - a parameter is unused + * + * Some compilers (eg. gcc with -W or -Wunused) warn about unused + * function parameters. This suppresses such warnings and indicates + * to the reader that it's deliberate. + * + * Example: + * // This is used as a callback, so needs to have this prototype. + * static int some_callback(void *unused UNUSED) + * { + * return 0; + * } + */ +#define UNUSED __attribute__((__unused__)) +#endif +#else +#ifndef UNNEEDED +#define UNNEEDED +#endif +#ifndef NEEDED +#define NEEDED +#endif +#ifndef UNUSED +#define UNUSED +#endif +#endif + +#ifndef IS_COMPILE_CONSTANT +#if HAVE_BUILTIN_CONSTANT_P +/** + * IS_COMPILE_CONSTANT - does the compiler know the value of this expression? + * @expr: the expression to evaluate + * + * When an expression manipulation is complicated, it is usually better to + * implement it in a function. However, if the expression being manipulated is + * known at compile time, it is better to have the compiler see the entire + * expression so it can simply substitute the result. + * + * This can be done using the IS_COMPILE_CONSTANT() macro. + * + * Example: + * enum greek { ALPHA, BETA, GAMMA, DELTA, EPSILON }; + * + * // Out-of-line version. + * const char *greek_name(enum greek greek); + * + * // Inline version. + * static inline const char *_greek_name(enum greek greek) + * { + * switch (greek) { + * case ALPHA: return "alpha"; + * case BETA: return "beta"; + * case GAMMA: return "gamma"; + * case DELTA: return "delta"; + * case EPSILON: return "epsilon"; + * default: return "**INVALID**"; + * } + * } + * + * // Use inline if compiler knows answer. Otherwise call function + * // to avoid copies of the same code everywhere. + * #define greek_name(g) \ + * (IS_COMPILE_CONSTANT(greek) ? _greek_name(g) : greek_name(g)) + */ +#define IS_COMPILE_CONSTANT(expr) __builtin_constant_p(expr) +#else +/* If we don't know, assume it's not. */ +#define IS_COMPILE_CONSTANT(expr) 0 +#endif +#endif + +#ifndef WARN_UNUSED_RESULT +#if HAVE_WARN_UNUSED_RESULT +/** + * WARN_UNUSED_RESULT - warn if a function return value is unused. + * + * Used to mark a function where it is extremely unlikely that the caller + * can ignore the result, eg realloc(). + * + * Example: + * // buf param may be freed by this; need return value! + * static char *WARN_UNUSED_RESULT enlarge(char *buf, unsigned *size) + * { + * return realloc(buf, (*size) *= 2); + * } + */ +#define WARN_UNUSED_RESULT __attribute__((__warn_unused_result__)) +#else +#define WARN_UNUSED_RESULT +#endif +#endif + + +#if HAVE_ATTRIBUTE_DEPRECATED +/** + * WARN_DEPRECATED - warn that a function/type/variable is deprecated when used. + * + * Used to mark a function, type or variable should not be used. + * + * Example: + * WARN_DEPRECATED char *oldfunc(char *buf); + */ +#define WARN_DEPRECATED __attribute__((__deprecated__)) +#else +#define WARN_DEPRECATED +#endif + + +#if HAVE_ATTRIBUTE_NONNULL +/** + * NO_NULL_ARGS - specify that no arguments to this function can be NULL. + * + * The compiler will warn if any pointer args are NULL. + * + * Example: + * NO_NULL_ARGS char *my_copy(char *buf); + */ +#define NO_NULL_ARGS __attribute__((__nonnull__)) + +/** + * NON_NULL_ARGS - specify that some arguments to this function can't be NULL. + * @...: 1-based argument numbers for which args can't be NULL. + * + * The compiler will warn if any of the specified pointer args are NULL. + * + * Example: + * char *my_copy2(char *buf, char *maybenull) NON_NULL_ARGS(1); + */ +#define NON_NULL_ARGS(...) __attribute__((__nonnull__(__VA_ARGS__))) +#else +#define NO_NULL_ARGS +#define NON_NULL_ARGS(...) +#endif + +#if HAVE_ATTRIBUTE_RETURNS_NONNULL +/** + * RETURNS_NONNULL - specify that this function cannot return NULL. + * + * Mainly an optimization opportunity, but can also suppress warnings. + * + * Example: + * RETURNS_NONNULL char *my_copy(char *buf); + */ +#define RETURNS_NONNULL __attribute__((__returns_nonnull__)) +#else +#define RETURNS_NONNULL +#endif + +#if HAVE_ATTRIBUTE_SENTINEL +/** + * LAST_ARG_NULL - specify the last argument of a variadic function must be NULL. + * + * The compiler will warn if the last argument isn't NULL. + * + * Example: + * char *join_string(char *buf, ...) LAST_ARG_NULL; + */ +#define LAST_ARG_NULL __attribute__((__sentinel__)) +#else +#define LAST_ARG_NULL +#endif + +#if HAVE_BUILTIN_CPU_SUPPORTS +/** + * cpu_supports - test if current CPU supports the named feature. + * + * This takes a literal string, and currently only works on glibc platforms. + * + * Example: + * if (cpu_supports("mmx")) + * printf("MMX support engaged!\n"); + */ +#define cpu_supports(x) __builtin_cpu_supports(x) +#else +#define cpu_supports(x) 0 +#endif /* HAVE_BUILTIN_CPU_SUPPORTS */ + +#endif /* CCAN_COMPILER_H */ diff --git a/config.h b/src/config.h diff --git a/configurator.c b/src/configurator.c diff --git a/cpu.h b/src/cpu.h diff --git a/cursor.h b/src/cursor.h diff --git a/endian.h b/src/endian.h diff --git a/hex.h b/src/hex.h diff --git a/io.h b/src/io.h diff --git a/jsmn.h b/src/jsmn.h diff --git a/lmdb_util.h b/src/lmdb_util.h diff --git a/memchr.h b/src/memchr.h diff --git a/nostr_bech32.c b/src/nostr_bech32.c diff --git a/nostr_bech32.h b/src/nostr_bech32.h diff --git a/nostrdb.c b/src/nostrdb.c diff --git a/nostrdb.h b/src/nostrdb.h diff --git a/print_util.h b/src/print_util.h diff --git a/protected_queue.h b/src/protected_queue.h diff --git a/random.h b/src/random.h diff --git a/sha256.c b/src/sha256.c diff --git a/sha256.h b/src/sha256.h diff --git a/threadpool.h b/src/threadpool.h diff --git a/typedefs.h b/src/typedefs.h diff --git a/util.h b/src/util.h