cleanup: Consolidate countof macros.

This commit is contained in:
Cory McWilliams 2024-11-13 20:22:42 -05:00
parent 5bdf970c10
commit 53e75d8209
10 changed files with 26 additions and 48 deletions

View File

@ -30,8 +30,6 @@
#include <alloca.h> #include <alloca.h>
#endif #endif
#define tf_countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
#define CYAN "\e[1;36m" #define CYAN "\e[1;36m"
#define MAGENTA "\e[1;35m" #define MAGENTA "\e[1;35m"
#define YELLOW "\e[1;33m" #define YELLOW "\e[1;33m"

View File

@ -39,10 +39,6 @@
#include "jni.h" #include "jni.h"
#endif #endif
#if !defined(_countof)
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
#endif
struct backtrace_state* g_backtrace_state; struct backtrace_state* g_backtrace_state;
const char* k_db_path_default = "db.sqlite"; 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", "ssb",
"todo", "todo",
}; };
for (int i = 0; i < (int)_countof(k_export); i++) for (int i = 0; i < tf_countof(k_export); i++)
{ {
char buffer[256]; char buffer[256];
snprintf(buffer, sizeof(buffer), "/~%s/%s", user, k_export[i]); 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("Usage: %s command [command-options]\n", file);
tf_printf("commands:\n"); 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); 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); 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); tf_task_set_android_service_callbacks(NULL, NULL);
(*env)->ReleaseStringUTFChars(env, files_dir, files); (*env)->ReleaseStringUTFChars(env, files_dir, files);
@ -939,7 +935,7 @@ static jint _tf_sandbox_main(JNIEnv* env, jobject this_object, int pipe_fd)
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_mem_shutdown();
tf_printf("tf_sandbox_main finished with %d.", result); 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_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 }, { "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) if (result != JNI_OK)
{ {
return result; return result;
@ -1018,7 +1014,7 @@ int main(int argc, char* argv[])
int result = 0; int result = 0;
if (argc >= 2) 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]; const command_t* command = &k_commands[i];
if (strcmp(argv[1], command->name) == 0) if (strcmp(argv[1], command->name) == 0)

View File

@ -31,10 +31,6 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#if !defined(_countof)
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
#endif
#define GREEN "\e[1;32m" #define GREEN "\e[1;32m"
#define MAGENTA "\e[1;35m" #define MAGENTA "\e[1;35m"
#define CYAN "\e[1;36m" #define CYAN "\e[1;36m"
@ -2228,7 +2224,7 @@ static void _tf_ssb_trace_timer(uv_timer_t* timer)
ssb->broadcasts_changed_count, 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) void tf_ssb_get_stats(tf_ssb_t* ssb, tf_ssb_stats_t* out_stats)

View File

@ -3,16 +3,13 @@
#include "log.h" #include "log.h"
#include "mem.h" #include "mem.h"
#include "ssb.h" #include "ssb.h"
#include "util.js.h"
#include "sqlite3.h" #include "sqlite3.h"
#include "uv.h" #include "uv.h"
#include <string.h> #include <string.h>
#if !defined(_countof)
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
#endif
typedef struct _tf_ssb_connections_t typedef struct _tf_ssb_connections_t
{ {
tf_ssb_t* ssb; 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_connections_t* connections = timer->data;
tf_ssb_connection_t* active[4]; tf_ssb_connection_t* active[4];
int count = tf_ssb_get_connections(connections->ssb, active, _countof(active)); int count = tf_ssb_get_connections(connections->ssb, active, tf_countof(active));
if (count < (int)_countof(active)) if (count < tf_countof(active))
{ {
tf_ssb_connections_get_next_t* next = tf_malloc(sizeof(tf_ssb_connections_get_next_t)); tf_ssb_connections_get_next_t* next = tf_malloc(sizeof(tf_ssb_connections_get_next_t));
*next = (tf_ssb_connections_get_next_t) { *next = (tf_ssb_connections_get_next_t) {

View File

@ -20,10 +20,6 @@
#include <assert.h> #include <assert.h>
#include <inttypes.h> #include <inttypes.h>
#if !defined(_countof)
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
#endif
static const int k_sql_async_timeout_ms = 60 * 1000; static const int k_sql_async_timeout_ms = 60 * 1000;
static JSClassID _tf_ssb_classId; static JSClassID _tf_ssb_classId;
@ -271,7 +267,7 @@ static JSValue _set_server_following_internal(tf_ssb_t* ssb, JSValueConst this_v
server_id, server_id,
message, 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_id);
JS_FreeValue(context, server_user); JS_FreeValue(context, server_user);
JS_FreeValue(context, message); JS_FreeValue(context, message);
@ -1088,7 +1084,7 @@ static JSValue _tf_ssb_connections(JSContext* context, JSValueConst this_val, in
if (ssb) if (ssb)
{ {
tf_ssb_connection_t* connections[32]; 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); result = JS_NewArray(context);
for (int i = 0; i < count; i++) for (int i = 0; i < count; i++)

View File

@ -14,10 +14,6 @@
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#if !defined(_countof)
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
#endif
static void _tf_ssb_connection_send_history_stream( 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); 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); 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_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 && 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) 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); JSValue ids = JS_NewArray(context);
int id_count = 0; int id_count = 0;
tf_ssb_connection_t* connections[1024]; 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++) 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, "type", JS_NewString(context, "left"));
JS_SetPropertyStr(context, left, "id", JS_NewString(context, id)); JS_SetPropertyStr(context, left, "id", JS_NewString(context, id));
tf_ssb_connection_t* connections[1024]; 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++) for (int i = 0; i < count; i++)
{ {
if (tf_ssb_connection_is_attendant(connections[i])) if (tf_ssb_connection_is_attendant(connections[i]))

View File

@ -43,10 +43,6 @@
#include <malloc.h> #include <malloc.h>
#endif #endif
#if !defined(_countof)
#define _countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
#endif
static JSClassID _import_class_id; static JSClassID _import_class_id;
static int _count; static int _count;
@ -919,7 +915,7 @@ char* tf_task_get_hitches(tf_task_t* task)
JSContext* context = task->_context; JSContext* context = task->_context;
tf_trace_begin(task->_trace, __func__); tf_trace_begin(task->_trace, __func__);
JSValue object = JS_NewObject(context); 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) 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) static void _tf_task_record_hitch(const char* name, uint64_t duration_ns, void* user_data)
{ {
tf_task_t* task = 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 (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); snprintf(task->hitches[i].name, sizeof(task->hitches[i].name), "%s", name);
task->hitches[i].duration_ns = duration_ns; task->hitches[i].duration_ns = duration_ns;

View File

@ -16,8 +16,6 @@
#include <time.h> #include <time.h>
#include <unistd.h> #include <unistd.h>
#define tf_countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
enum enum
{ {
k_buffer_size = 4 * 1024 * 1024, k_buffer_size = 4 * 1024 * 1024,

View File

@ -20,8 +20,6 @@
#include <execinfo.h> #include <execinfo.h>
#endif #endif
#define tf_countof(a) ((int)(sizeof((a)) / sizeof(*(a))))
static JSValue _util_utf8_encode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv) static JSValue _util_utf8_encode(JSContext* context, JSValueConst this_val, int argc, JSValueConst* argv)
{ {
size_t length = 0; size_t length = 0;

View File

@ -150,4 +150,11 @@ const char* tf_util_function_to_string(void* function);
_a > _b ? _b : _a; \ _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))))
/** @} */ /** @} */