From 53e75d82098a68a607332afe4626637f1832548c Mon Sep 17 00:00:00 2001 From: Cory McWilliams Date: Wed, 13 Nov 2024 20:22:42 -0500 Subject: [PATCH] cleanup: Consolidate countof macros. --- src/httpd.js.c | 2 -- src/main.c | 16 ++++++---------- src/ssb.c | 6 +----- src/ssb.connections.c | 9 +++------ src/ssb.js.c | 8 ++------ src/ssb.rpc.c | 10 +++------- src/task.c | 12 ++++-------- src/trace.c | 2 -- src/util.js.c | 2 -- src/util.js.h | 7 +++++++ 10 files changed, 26 insertions(+), 48 deletions(-) diff --git a/src/httpd.js.c b/src/httpd.js.c index 61a31b68..2387c8fa 100644 --- a/src/httpd.js.c +++ b/src/httpd.js.c @@ -30,8 +30,6 @@ #include #endif -#define tf_countof(a) ((int)(sizeof((a)) / sizeof(*(a)))) - #define CYAN "\e[1;36m" #define MAGENTA "\e[1;35m" #define YELLOW "\e[1;33m" diff --git a/src/main.c b/src/main.c index 0dfb9d2d..daaadb0a 100644 --- a/src/main.c +++ b/src/main.c @@ -39,10 +39,6 @@ #include "jni.h" #endif -#if !defined(_countof) -#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a)))) -#endif - struct backtrace_state* g_backtrace_state; const char* k_db_path_default = "db.sqlite"; @@ -264,7 +260,7 @@ static int _tf_command_export(const char* file, int argc, char* argv[]) "ssb", "todo", }; - for (int i = 0; i < (int)_countof(k_export); i++) + for (int i = 0; i < tf_countof(k_export); i++) { char buffer[256]; snprintf(buffer, sizeof(buffer), "/~%s/%s", user, k_export[i]); @@ -778,7 +774,7 @@ static int _tf_command_usage(const char* file) { tf_printf("Usage: %s command [command-options]\n", file); tf_printf("commands:\n"); - for (int i = 0; i < (int)_countof(k_commands); i++) + for (int i = 0; i < tf_countof(k_commands); i++) { tf_printf(" %s - %s\n", k_commands[i].name, k_commands[i].description); } @@ -908,7 +904,7 @@ static jint _tf_server_main(JNIEnv* env, jobject this_object, jstring files_dir, }; tf_task_set_android_service_callbacks(_tf_service_start, _tf_service_stop); - result = _tf_command_run(apk, _countof(args), (char**)args); + result = _tf_command_run(apk, tf_countof(args), (char**)args); tf_task_set_android_service_callbacks(NULL, NULL); (*env)->ReleaseStringUTFChars(env, files_dir, files); @@ -939,7 +935,7 @@ static jint _tf_sandbox_main(JNIEnv* env, jobject this_object, int pipe_fd) fd, }; - int result = _tf_command_sandbox(NULL, _countof(args), (char**)args); + int result = _tf_command_sandbox(NULL, tf_countof(args), (char**)args); tf_mem_shutdown(); tf_printf("tf_sandbox_main finished with %d.", result); @@ -971,7 +967,7 @@ JNIEXPORT jint JNI_OnLoad(JavaVM* vm, void* reserved) { "tf_server_main", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;Landroid/net/ConnectivityManager;)I", _tf_server_main }, { "tf_sandbox_main", "(I)I", _tf_sandbox_main }, }; - int result = (*env)->RegisterNatives(env, c, methods, (int)_countof(methods)); + int result = (*env)->RegisterNatives(env, c, methods, tf_countof(methods)); if (result != JNI_OK) { return result; @@ -1018,7 +1014,7 @@ int main(int argc, char* argv[]) int result = 0; if (argc >= 2) { - for (int i = 0; i < (int)_countof(k_commands); i++) + for (int i = 0; i < tf_countof(k_commands); i++) { const command_t* command = &k_commands[i]; if (strcmp(argv[1], command->name) == 0) diff --git a/src/ssb.c b/src/ssb.c index 39d28ac8..3386ea6c 100644 --- a/src/ssb.c +++ b/src/ssb.c @@ -31,10 +31,6 @@ #include #include -#if !defined(_countof) -#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a)))) -#endif - #define GREEN "\e[1;32m" #define MAGENTA "\e[1;35m" #define CYAN "\e[1;36m" @@ -2228,7 +2224,7 @@ static void _tf_ssb_trace_timer(uv_timer_t* timer) ssb->broadcasts_changed_count, }; - tf_trace_counter(ssb->trace, "ssb", _countof(values), names, values); + tf_trace_counter(ssb->trace, "ssb", tf_countof(values), names, values); } void tf_ssb_get_stats(tf_ssb_t* ssb, tf_ssb_stats_t* out_stats) diff --git a/src/ssb.connections.c b/src/ssb.connections.c index 6fd479ff..3bafe51e 100644 --- a/src/ssb.connections.c +++ b/src/ssb.connections.c @@ -3,16 +3,13 @@ #include "log.h" #include "mem.h" #include "ssb.h" +#include "util.js.h" #include "sqlite3.h" #include "uv.h" #include -#if !defined(_countof) -#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a)))) -#endif - typedef struct _tf_ssb_connections_t { tf_ssb_t* ssb; @@ -116,8 +113,8 @@ static void _tf_ssb_connections_timer(uv_timer_t* timer) { tf_ssb_connections_t* connections = timer->data; tf_ssb_connection_t* active[4]; - int count = tf_ssb_get_connections(connections->ssb, active, _countof(active)); - if (count < (int)_countof(active)) + int count = tf_ssb_get_connections(connections->ssb, active, tf_countof(active)); + if (count < tf_countof(active)) { tf_ssb_connections_get_next_t* next = tf_malloc(sizeof(tf_ssb_connections_get_next_t)); *next = (tf_ssb_connections_get_next_t) { diff --git a/src/ssb.js.c b/src/ssb.js.c index 37a0f4d8..7cd0c825 100644 --- a/src/ssb.js.c +++ b/src/ssb.js.c @@ -20,10 +20,6 @@ #include #include -#if !defined(_countof) -#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a)))) -#endif - static const int k_sql_async_timeout_ms = 60 * 1000; static JSClassID _tf_ssb_classId; @@ -271,7 +267,7 @@ static JSValue _set_server_following_internal(tf_ssb_t* ssb, JSValueConst this_v server_id, message, }; - JSValue result = _tf_ssb_appendMessageWithIdentity(context, this_val, _countof(args), args); + JSValue result = _tf_ssb_appendMessageWithIdentity(context, this_val, tf_countof(args), args); JS_FreeValue(context, server_id); JS_FreeValue(context, server_user); JS_FreeValue(context, message); @@ -1088,7 +1084,7 @@ static JSValue _tf_ssb_connections(JSContext* context, JSValueConst this_val, in if (ssb) { tf_ssb_connection_t* connections[32]; - int count = tf_ssb_get_connections(ssb, connections, _countof(connections)); + int count = tf_ssb_get_connections(ssb, connections, tf_countof(connections)); result = JS_NewArray(context); for (int i = 0; i < count; i++) diff --git a/src/ssb.rpc.c b/src/ssb.rpc.c index 3919e669..37486eba 100644 --- a/src/ssb.rpc.c +++ b/src/ssb.rpc.c @@ -14,10 +14,6 @@ #include #include -#if !defined(_countof) -#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a)))) -#endif - static void _tf_ssb_connection_send_history_stream( tf_ssb_connection_t* connection, int32_t request_number, const char* author, int64_t sequence, bool keys, bool live, bool end_request); static void _tf_ssb_rpc_send_peers_exchange(tf_ssb_connection_t* connection); @@ -240,7 +236,7 @@ static void _tf_ssb_request_blob_wants_work(tf_ssb_connection_t* connection, voi if (sqlite3_prepare(db, "SELECT id FROM blob_wants_view WHERE id > ? AND timestamp > ? ORDER BY id LIMIT ?", -1, &statement, NULL) == SQLITE_OK) { if (sqlite3_bind_text(statement, 1, blob_wants->last_id, -1, NULL) == SQLITE_OK && sqlite3_bind_int64(statement, 2, timestamp) == SQLITE_OK && - sqlite3_bind_int(statement, 3, _countof(work->out_id)) == SQLITE_OK) + sqlite3_bind_int(statement, 3, tf_countof(work->out_id)) == SQLITE_OK) { while (sqlite3_step(statement) == SQLITE_ROW) { @@ -456,7 +452,7 @@ static void _tf_ssb_rpc_room_attendants(tf_ssb_connection_t* connection, uint8_t JSValue ids = JS_NewArray(context); int id_count = 0; tf_ssb_connection_t* connections[1024]; - int count = tf_ssb_get_connections(ssb, connections, _countof(connections)); + int count = tf_ssb_get_connections(ssb, connections, tf_countof(connections)); for (int i = 0; i < count; i++) { @@ -1292,7 +1288,7 @@ static void _tf_ssb_rpc_connections_changed_callback(tf_ssb_t* ssb, tf_ssb_chang JS_SetPropertyStr(context, left, "type", JS_NewString(context, "left")); JS_SetPropertyStr(context, left, "id", JS_NewString(context, id)); tf_ssb_connection_t* connections[1024]; - int count = tf_ssb_get_connections(ssb, connections, _countof(connections)); + int count = tf_ssb_get_connections(ssb, connections, tf_countof(connections)); for (int i = 0; i < count; i++) { if (tf_ssb_connection_is_attendant(connections[i])) diff --git a/src/task.c b/src/task.c index 5eccbdfa..53289094 100644 --- a/src/task.c +++ b/src/task.c @@ -43,10 +43,6 @@ #include #endif -#if !defined(_countof) -#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a)))) -#endif - static JSClassID _import_class_id; static int _count; @@ -919,7 +915,7 @@ char* tf_task_get_hitches(tf_task_t* task) JSContext* context = task->_context; tf_trace_begin(task->_trace, __func__); JSValue object = JS_NewObject(context); - for (int i = 0; i < (int)_countof(task->hitches); i++) + for (int i = 0; i < tf_countof(task->hitches); i++) { if (*task->hitches[i].name) { @@ -1658,13 +1654,13 @@ static void _tf_task_trace_to_parent(tf_trace_t* trace, const char* buffer, size static void _tf_task_record_hitch(const char* name, uint64_t duration_ns, void* user_data) { tf_task_t* task = user_data; - for (int i = 0; i < (int)_countof(task->hitches); i++) + for (int i = 0; i < tf_countof(task->hitches); i++) { if (duration_ns > task->hitches[i].duration_ns) { - if (i + 1 < (int)_countof(task->hitches)) + if (i + 1 < tf_countof(task->hitches)) { - memmove(task->hitches + i + 1, task->hitches + i, sizeof(hitch_t) * ((int)_countof(task->hitches) - i - 1)); + memmove(task->hitches + i + 1, task->hitches + i, sizeof(hitch_t) * (tf_countof(task->hitches) - i - 1)); } snprintf(task->hitches[i].name, sizeof(task->hitches[i].name), "%s", name); task->hitches[i].duration_ns = duration_ns; diff --git a/src/trace.c b/src/trace.c index acb35408..f898bc0f 100644 --- a/src/trace.c +++ b/src/trace.c @@ -16,8 +16,6 @@ #include #include -#define tf_countof(a) ((int)(sizeof((a)) / sizeof(*(a)))) - enum { k_buffer_size = 4 * 1024 * 1024, diff --git a/src/util.js.c b/src/util.js.c index 47ae730a..bd701268 100644 --- a/src/util.js.c +++ b/src/util.js.c @@ -20,8 +20,6 @@ #include #endif -#define tf_countof(a) ((int)(sizeof((a)) / sizeof(*(a)))) - static JSValue _util_utf8_encode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) { size_t length = 0; diff --git a/src/util.js.h b/src/util.js.h index 73827f9e..876640dd 100644 --- a/src/util.js.h +++ b/src/util.js.h @@ -150,4 +150,11 @@ const char* tf_util_function_to_string(void* function); _a > _b ? _b : _a; \ }) +/** +** Get the number of elements in an array. +** @param a The array. +** @return The number of array elements. +*/ +#define tf_countof(a) ((int)(sizeof((a)) / sizeof(*(a)))) + /** @} */