Skip to content

Commit 9dc9d16

Browse files
committed
Reduce memory allocations
1 parent 1e28f09 commit 9dc9d16

2 files changed

Lines changed: 53 additions & 52 deletions

File tree

tasks/task_core_updater.c

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -278,18 +278,20 @@ static void task_core_updater_get_list_handler(retro_task_t *task)
278278
case CORE_UPDATER_LIST_BEGIN:
279279
{
280280
char buildbot_url[PATH_MAX_LENGTH];
281-
settings_t *settings = config_get_ptr();
282-
file_transfer_t *transf = NULL;
283-
char *tmp_url = NULL;
284-
const char *net_buildbot_url =
285-
settings->paths.network_buildbot_url;
281+
settings_t *settings = config_get_ptr();
282+
file_transfer_t *transf = NULL;
283+
const char *net_buildbot_url;
286284

287285
/* Reset core updater list */
288286
core_updater_list_reset(list_handle->core_list);
289-
/* Get core listing URL */
287+
288+
/* Get core listing URL - check settings first to
289+
* avoid dereferencing NULL */
290290
if (!settings)
291291
goto task_finished;
292292

293+
net_buildbot_url = settings->paths.network_buildbot_url;
294+
293295
if (string_is_empty(net_buildbot_url))
294296
goto task_finished;
295297

@@ -299,12 +301,15 @@ static void task_core_updater_get_list_handler(retro_task_t *task)
299301
".index-extended",
300302
sizeof(buildbot_url));
301303

302-
tmp_url = strdup(buildbot_url);
303-
buildbot_url[0] = '\0';
304-
net_http_urlencode_full(
305-
buildbot_url, tmp_url, sizeof(buildbot_url));
306-
if (tmp_url)
307-
free(tmp_url);
304+
/* URL-encode in place using a stack buffer
305+
* instead of heap-allocating a copy */
306+
{
307+
char tmp_url[PATH_MAX_LENGTH];
308+
strlcpy(tmp_url, buildbot_url, sizeof(tmp_url));
309+
buildbot_url[0] = '\0';
310+
net_http_urlencode_full(
311+
buildbot_url, tmp_url, sizeof(buildbot_url));
312+
}
308313

309314
if (string_is_empty(buildbot_url))
310315
goto task_finished;
@@ -314,10 +319,7 @@ static void task_core_updater_get_list_handler(retro_task_t *task)
314319
sizeof(file_transfer_t))))
315320
goto task_finished;
316321

317-
/* > Seems to be required - not sure why the
318-
* underlying code is implemented like this... */
319322
strlcpy(transf->path, buildbot_url, sizeof(transf->path));
320-
321323
transf->user_data = (void*)list_handle;
322324

323325
/* Push HTTP transfer task */
@@ -342,10 +344,8 @@ static void task_core_updater_get_list_handler(retro_task_t *task)
342344
{
343345
uint8_t _flg = task_get_flags(list_handle->http_task);
344346

345-
if ((_flg & (RETRO_TASK_FLG_FINISHED)) > 0)
346-
list_handle->http_task_finished = true;
347-
else
348-
list_handle->http_task_finished = false;
347+
list_handle->http_task_finished =
348+
((_flg & RETRO_TASK_FLG_FINISHED) > 0);
349349

350350
/* If HTTP task is running, copy current
351351
* progress value to *this* task */
@@ -362,20 +362,22 @@ static void task_core_updater_get_list_handler(retro_task_t *task)
362362
break;
363363
case CORE_UPDATER_LIST_END:
364364
{
365-
settings_t *settings = config_get_ptr();
366-
367365
/* Check whether HTTP task was successful */
368366
if (list_handle->http_task_success)
369367
{
370368
/* Parse HTTP transfer data */
371369
if (list_handle->http_data)
372-
core_updater_list_parse_network_data(
373-
list_handle->core_list,
374-
settings->paths.directory_libretro,
375-
settings->paths.path_libretro_info,
376-
settings->paths.network_buildbot_url,
377-
list_handle->http_data->data,
378-
list_handle->http_data->len);
370+
{
371+
settings_t *settings = config_get_ptr();
372+
if (settings)
373+
core_updater_list_parse_network_data(
374+
list_handle->core_list,
375+
settings->paths.directory_libretro,
376+
settings->paths.path_libretro_info,
377+
settings->paths.network_buildbot_url,
378+
list_handle->http_data->data,
379+
list_handle->http_data->len);
380+
}
379381
}
380382
else
381383
{
@@ -386,7 +388,6 @@ static void task_core_updater_get_list_handler(retro_task_t *task)
386388

387389
/* Enable menu refresh, if required */
388390
#if defined(RARCH_INTERNAL) && defined(HAVE_MENU)
389-
if (list_handle->refresh_menu)
390391
{
391392
struct menu_state *menu_st = menu_state_get_ptr();
392393
if (list_handle->refresh_menu)

tasks/task_save.c

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1252,10 +1252,10 @@ static void content_load_and_save_state_cb(retro_task_t *task,
12521252
static void task_push_load_and_save_state(const char *path, void *data,
12531253
size_t len, bool load_to_backup_buffer, bool autosave)
12541254
{
1255-
retro_task_t *task = NULL;
1256-
settings_t *settings = config_get_ptr();
1257-
video_driver_state_t *video_st = video_state_get_ptr();
1258-
save_task_state_t *state = (save_task_state_t*)
1255+
retro_task_t *task = NULL;
1256+
settings_t *settings = config_get_ptr();
1257+
video_driver_state_t *video_st = video_state_get_ptr();
1258+
save_task_state_t *state = (save_task_state_t*)
12591259
calloc(1, sizeof(*state));
12601260

12611261
if (!state)
@@ -1270,35 +1270,35 @@ static void task_push_load_and_save_state(const char *path, void *data,
12701270

12711271
strlcpy(state->path, path, sizeof(state->path));
12721272
if (load_to_backup_buffer)
1273-
state->flags |= SAVE_TASK_FLAG_LOAD_TO_BACKUP_BUFF;
1274-
state->undo_size = len;
1275-
state->undo_data = data;
1273+
state->flags |= SAVE_TASK_FLAG_LOAD_TO_BACKUP_BUFF;
1274+
state->undo_size = len;
1275+
state->undo_data = data;
12761276
/* Don't show OSD messages if we are auto-saving */
12771277
if (autosave)
1278-
state->flags |= (SAVE_TASK_FLAG_AUTOSAVE |
1279-
SAVE_TASK_FLAG_MUTE);
1278+
state->flags |= ( SAVE_TASK_FLAG_AUTOSAVE
1279+
| SAVE_TASK_FLAG_MUTE);
12801280
if (load_to_backup_buffer)
1281-
state->flags |= SAVE_TASK_FLAG_MUTE;
1282-
state->state_slot = settings->ints.state_slot;
1281+
state->flags |= SAVE_TASK_FLAG_MUTE;
1282+
state->state_slot = settings->ints.state_slot;
12831283
if (video_st->frame_cache_data && (video_st->frame_cache_data == RETRO_HW_FRAME_BUFFER_VALID))
1284-
state->flags |= SAVE_TASK_FLAG_HAS_VALID_FB;
1284+
state->flags |= SAVE_TASK_FLAG_HAS_VALID_FB;
12851285
#if defined(HAVE_ZLIB)
12861286
if (settings->bools.savestate_file_compression)
1287-
state->flags |= SAVE_TASK_FLAG_COMPRESS_FILES;
1287+
state->flags |= SAVE_TASK_FLAG_COMPRESS_FILES;
12881288
#endif
12891289
if (!settings->bools.notification_show_save_state)
1290-
state->flags |= SAVE_TASK_FLAG_MUTE;
1290+
state->flags |= SAVE_TASK_FLAG_MUTE;
12911291

1292-
task->state = state;
1293-
task->type = TASK_TYPE_BLOCKING;
1294-
task->handler = task_load_handler;
1295-
task->callback = content_load_and_save_state_cb;
1296-
task->title = strdup(msg_hash_to_str(MSG_LOADING_STATE));
1292+
task->state = state;
1293+
task->type = TASK_TYPE_BLOCKING;
1294+
task->handler = task_load_handler;
1295+
task->callback = content_load_and_save_state_cb;
1296+
task->title = strdup(msg_hash_to_str(MSG_LOADING_STATE));
12971297

12981298
if (state->flags & SAVE_TASK_FLAG_MUTE)
1299-
task->flags |= RETRO_TASK_FLG_MUTE;
1299+
task->flags |= RETRO_TASK_FLG_MUTE;
13001300
else
1301-
task->flags &= ~RETRO_TASK_FLG_MUTE;
1301+
task->flags &= ~RETRO_TASK_FLG_MUTE;
13021302

13031303
if (!task_queue_push(task))
13041304
{
@@ -1325,8 +1325,8 @@ bool content_auto_save_state(const char *path)
13251325
{
13261326
size_t _len;
13271327
settings_t *settings = config_get_ptr();
1328-
void *serial_data = NULL;
1329-
intfstream_t *file = NULL;
1328+
void *serial_data = NULL;
1329+
intfstream_t *file = NULL;
13301330

13311331
if (!core_info_current_supports_savestate())
13321332
{

0 commit comments

Comments
 (0)