Skip to content

Commit

Permalink
Introduced RG_ASSERT_ARG to validate function arguments
Browse files Browse the repository at this point in the history
This reduces code size slightly and is also more explicit.
  • Loading branch information
ducalex committed Aug 17, 2024
1 parent ce57d15 commit 83b1937
Show file tree
Hide file tree
Showing 12 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion components/retro-go/drivers/display/ili9341.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

Expand Down
2 changes: 1 addition & 1 deletion components/retro-go/rg_display.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions components/retro-go/rg_gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions components/retro-go/rg_network.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions components/retro-go/rg_storage.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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};
Expand Down
4 changes: 2 additions & 2 deletions components/retro-go/rg_surface.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions components/retro-go/rg_system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions components/retro-go/rg_system.h
Original file line number Diff line number Diff line change
Expand Up @@ -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__
Expand Down
8 changes: 4 additions & 4 deletions launcher/main/applications.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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))
{
Expand Down
8 changes: 4 additions & 4 deletions launcher/main/bookmarks.c
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand All @@ -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];

Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion launcher/main/gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
4 changes: 2 additions & 2 deletions launcher/main/updater.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down

0 comments on commit 83b1937

Please sign in to comment.