From 83b193738d7d17ddaa11d8de3b451ab265b554dd Mon Sep 17 00:00:00 2001 From: Alex Duchesne Date: Fri, 16 Aug 2024 22:51:59 -0400 Subject: [PATCH] Introduced RG_ASSERT_ARG to validate function arguments This reduces code size slightly and is also more explicit. --- components/retro-go/drivers/display/ili9341.h | 2 +- components/retro-go/rg_display.c | 2 +- components/retro-go/rg_gui.c | 5 +++-- components/retro-go/rg_network.c | 6 +++--- components/retro-go/rg_storage.c | 6 +++--- components/retro-go/rg_surface.c | 4 ++-- components/retro-go/rg_system.c | 4 ++-- components/retro-go/rg_system.h | 1 + launcher/main/applications.c | 8 ++++---- launcher/main/bookmarks.c | 8 ++++---- launcher/main/gui.c | 2 +- launcher/main/updater.c | 4 ++-- 12 files changed, 27 insertions(+), 25 deletions(-) diff --git a/components/retro-go/drivers/display/ili9341.h b/components/retro-go/drivers/display/ili9341.h index d7f2e4385..d079c6b8e 100644 --- a/components/retro-go/drivers/display/ili9341.h +++ b/components/retro-go/drivers/display/ili9341.h @@ -174,7 +174,7 @@ static void lcd_set_window(int left, int top, int width, int height) static inline uint16_t *lcd_get_buffer(size_t length) { - // RG_ASSERT(length < LCD_BUFFER_LENGTH, "Invalid length"); + // RG_ASSERT_ARG(length < LCD_BUFFER_LENGTH); return spi_take_buffer(); } diff --git a/components/retro-go/rg_display.c b/components/retro-go/rg_display.c index 675e2e802..0193e5cfa 100644 --- a/components/retro-go/rg_display.c +++ b/components/retro-go/rg_display.c @@ -468,7 +468,7 @@ bool rg_display_sync(bool block) void rg_display_write(int left, int top, int width, int height, int stride, const uint16_t *buffer, uint32_t flags) { - RG_ASSERT(buffer, "Bad param"); + RG_ASSERT_ARG(buffer); // Offsets can be negative to indicate N pixels from the end if (left < 0) diff --git a/components/retro-go/rg_gui.c b/components/retro-go/rg_gui.c index 3b8ffde38..31a39e65e 100644 --- a/components/retro-go/rg_gui.c +++ b/components/retro-go/rg_gui.c @@ -785,7 +785,8 @@ void rg_gui_draw_dialog(const char *title, const rg_gui_option_t *options, int s void rg_gui_draw_message(const char *format, ...) { - RG_ASSERT(format, "Bad param"); + RG_ASSERT_ARG(format); + char buffer[512]; va_list va; va_start(va, format); @@ -1025,7 +1026,7 @@ char *rg_gui_file_picker(const char *title, const char *path, bool (*validator)( void rg_gui_draw_keyboard(const rg_keyboard_map_t *map, size_t cursor) { - RG_ASSERT(map, "Bad param"); + RG_ASSERT_ARG(map); int width = map->columns * 16 + 16; int height = map->rows * 16 + 16; diff --git a/components/retro-go/rg_network.c b/components/retro-go/rg_network.c index 70ae76ada..a83bc1058 100644 --- a/components/retro-go/rg_network.c +++ b/components/retro-go/rg_network.c @@ -151,7 +151,7 @@ bool rg_network_wifi_load_config(int slot) bool rg_network_wifi_set_config(const rg_wifi_config_t *config) { #ifdef RG_ENABLE_NETWORKING - RG_ASSERT(config, "bad param"); + RG_ASSERT_ARG(config != NULL); wifi_config = *config; return true; #else @@ -278,7 +278,7 @@ bool rg_network_init(void) rg_http_req_t *rg_network_http_open(const char *url, const rg_http_cfg_t *cfg) { - RG_ASSERT(url, "bad param"); + RG_ASSERT_ARG(url != NULL); #ifdef RG_ENABLE_NETWORKING esp_http_client_config_t http_config = {.url = url, .buffer_size = 1024, .buffer_size_tx = 1024}; esp_http_client_handle_t http_client = esp_http_client_init(&http_config); @@ -329,7 +329,7 @@ rg_http_req_t *rg_network_http_open(const char *url, const rg_http_cfg_t *cfg) int rg_network_http_read(rg_http_req_t *req, void *buffer, size_t buffer_len) { - RG_ASSERT(req && buffer, "bad param"); + RG_ASSERT_ARG(req && buffer); #ifdef RG_ENABLE_NETWORKING // if (req->content_length >= 0 && req->received_bytes >= req->content_length) // return 0; diff --git a/components/retro-go/rg_storage.c b/components/retro-go/rg_storage.c index 3b0fbac03..339aad9f1 100644 --- a/components/retro-go/rg_storage.c +++ b/components/retro-go/rg_storage.c @@ -411,7 +411,7 @@ int64_t rg_storage_get_free_space(const char *path) bool rg_storage_read_file(const char *path, void **data_out, size_t *data_len, uint32_t flags) { - RG_ASSERT(data_out && data_len, "Bad param"); + RG_ASSERT_ARG(data_out && data_len); CHECK_PATH(path); size_t output_buffer_align = RG_MAX(0x400, (flags & 0xF) * 0x2000); @@ -466,7 +466,7 @@ bool rg_storage_read_file(const char *path, void **data_out, size_t *data_len, u bool rg_storage_write_file(const char *path, const void *data_ptr, size_t data_len, uint32_t flags) { - RG_ASSERT(data_ptr || !data_len, "Bad param"); + RG_ASSERT_ARG(data_ptr || !data_len); CHECK_PATH(path); // TODO: If atomic is true we should write to a temp file and only replace the target on success @@ -519,7 +519,7 @@ typedef struct __attribute__((packed)) bool rg_storage_unzip_file(const char *zip_path, const char *filter, void **data_out, size_t *data_len, uint32_t flags) { #if RG_ZIP_SUPPORT - RG_ASSERT(data_out && data_len, "Bad param"); + RG_ASSERT_ARG(data_out && data_len); CHECK_PATH(zip_path); zip_header_t header = {0}; diff --git a/components/retro-go/rg_surface.c b/components/retro-go/rg_surface.c index 9c60abc85..2e0347382 100644 --- a/components/retro-go/rg_surface.c +++ b/components/retro-go/rg_surface.c @@ -226,7 +226,7 @@ bool rg_surface_fill(rg_surface_t *dest, const rg_rect_t *rect, rg_color_t color rg_surface_t *rg_surface_load_image(const uint8_t *data, size_t data_len, uint32_t flags) { - RG_ASSERT(data && data_len >= 16, "bad param"); + RG_ASSERT_ARG(data && data_len >= 16); const uint16_t *data16 = (const uint16_t *)data; if (memcmp(data, "\x89PNG", 4) == 0) @@ -261,7 +261,7 @@ rg_surface_t *rg_surface_load_image(const uint8_t *data, size_t data_len, uint32 rg_surface_t *rg_surface_load_image_file(const char *filename, uint32_t flags) { - RG_ASSERT(filename, "bad param"); + RG_ASSERT_ARG(filename); size_t data_len; void *data; diff --git a/components/retro-go/rg_system.c b/components/retro-go/rg_system.c index 13e8cbeef..25b965ff1 100644 --- a/components/retro-go/rg_system.c +++ b/components/retro-go/rg_system.c @@ -509,7 +509,7 @@ static int task_wrapper(void *arg) rg_task_t *rg_task_create(const char *name, void (*taskFunc)(void *arg), void *arg, size_t stackSize, int priority, int affinity) { - RG_ASSERT(name && taskFunc, "bad param"); + RG_ASSERT_ARG(name && taskFunc); rg_task_t *task = NULL; for (size_t i = 1; i < RG_COUNT(tasks); ++i) @@ -547,7 +547,7 @@ rg_task_t *rg_task_create(const char *name, void (*taskFunc)(void *arg), void *a rg_task_t *rg_task_find(const char *name) { - RG_ASSERT(name, "bad param"); + RG_ASSERT_ARG(name != NULL); for (size_t i = 0; i < RG_COUNT(tasks); ++i) { if (strncmp(tasks[i].name, name, 16) == 0) diff --git a/components/retro-go/rg_system.h b/components/retro-go/rg_system.h index 26169b1e7..040ca05ad 100644 --- a/components/retro-go/rg_system.h +++ b/components/retro-go/rg_system.h @@ -294,6 +294,7 @@ float rg_emu_get_speed(void); // This should really support printf format... #define RG_PANIC(x) rg_system_panic(__func__, x) #define RG_ASSERT(cond, msg) while (!(cond)) { RG_PANIC("Assertion failed: `" #cond "` : " msg); } +#define RG_ASSERT_ARG(cond) while (!(cond)) { RG_PANIC("Invalid function argument"); } #ifndef RG_LOG_TAG #define RG_LOG_TAG __func__ diff --git a/launcher/main/applications.c b/launcher/main/applications.c index 7e847210a..39354e685 100644 --- a/launcher/main/applications.c +++ b/launcher/main/applications.c @@ -118,14 +118,14 @@ static void application_init(retro_app_t *app) static const char *get_file_path(retro_file_t *file) { static char buffer[RG_PATH_MAX + 1]; - RG_ASSERT(file, "Bad param"); + RG_ASSERT_ARG(file); snprintf(buffer, RG_PATH_MAX, "%s/%s", file->folder, file->name); return buffer; } static void application_start(retro_file_t *file, int load_state) { - RG_ASSERT(file, "Unable to find file..."); + RG_ASSERT_ARG(file); char *part = strdup(file->app->partition); char *name = strdup(file->app->short_name); char *path = strdup(get_file_path(file)); @@ -475,7 +475,7 @@ static void event_handler(gui_event_t event, tab_t *tab) bool application_path_to_file(const char *path, retro_file_t *file) { - RG_ASSERT(path && file, "Bad param"); + RG_ASSERT_ARG(path && file); for (int i = 0; i < apps_count; ++i) { @@ -654,7 +654,7 @@ void application_show_file_menu(retro_file_t *file, bool advanced) static void application(const char *desc, const char *name, const char *exts, const char *part, uint16_t crc_offset) { - RG_ASSERT(desc && name && exts && part, "Bad param"); + RG_ASSERT_ARG(desc && name && exts && part); if (!rg_system_have_app(part)) { diff --git a/launcher/main/bookmarks.c b/launcher/main/bookmarks.c index 2e7f419ca..5d7d3743f 100644 --- a/launcher/main/bookmarks.c +++ b/launcher/main/bookmarks.c @@ -210,7 +210,7 @@ static void book_init(book_type_t book_type, const char *name, const char *desc, retro_file_t *bookmark_find_by_app(book_type_t book_type, const retro_app_t *app) { - RG_ASSERT(book_type < BOOK_TYPE_COUNT && app != NULL, "bad param"); + RG_ASSERT_ARG(book_type < BOOK_TYPE_COUNT && app != NULL); book_t *book = &books[book_type]; @@ -231,14 +231,14 @@ retro_file_t *bookmark_find_by_app(book_type_t book_type, const retro_app_t *app bool bookmark_exists(book_type_t book_type, const retro_file_t *file) { - RG_ASSERT(book_type < BOOK_TYPE_COUNT && file != NULL, "bad param"); + RG_ASSERT_ARG(book_type < BOOK_TYPE_COUNT && file != NULL); return book_find(&books[book_type], file) != NULL; } bool bookmark_add(book_type_t book_type, const retro_file_t *file) { - RG_ASSERT(book_type < BOOK_TYPE_COUNT && file != NULL, "bad param"); + RG_ASSERT_ARG(book_type < BOOK_TYPE_COUNT && file != NULL); book_t *book = &books[book_type]; @@ -254,7 +254,7 @@ bool bookmark_add(book_type_t book_type, const retro_file_t *file) bool bookmark_remove(book_type_t book_type, const retro_file_t *file) { - RG_ASSERT(book_type < BOOK_TYPE_COUNT && file != NULL, "bad param"); + RG_ASSERT_ARG(book_type < BOOK_TYPE_COUNT && file != NULL); book_t *book = &books[book_type]; size_t found = 0; diff --git a/launcher/main/gui.c b/launcher/main/gui.c index 500d9cece..ddfaa2c6b 100644 --- a/launcher/main/gui.c +++ b/launcher/main/gui.c @@ -59,7 +59,7 @@ void gui_event(gui_event_t event, tab_t *tab) tab_t *gui_add_tab(const char *name, const char *desc, void *arg, void *event_handler) { - RG_ASSERT(name && desc, "Bad param"); + RG_ASSERT_ARG(name && desc); tab_t *tab = calloc(1, sizeof(tab_t)); diff --git a/launcher/main/updater.c b/launcher/main/updater.c index 4aad61766..979053830 100644 --- a/launcher/main/updater.c +++ b/launcher/main/updater.c @@ -30,7 +30,7 @@ typedef struct static bool download_file(const char *url, const char *filename) { - RG_ASSERT(url && filename, "bad param"); + RG_ASSERT_ARG(url && filename); rg_http_req_t *req = NULL; FILE *fp = NULL; @@ -91,7 +91,7 @@ static bool download_file(const char *url, const char *filename) static cJSON *fetch_json(const char *url) { - RG_ASSERT(url, "bad param"); + RG_ASSERT_ARG(url); RG_LOGI("Fetching: '%s'", url); rg_gui_draw_hourglass();