(tasks/task_content.c) Reduce stack usage

This commit is contained in:
twinaphex 2017-09-09 23:17:28 +02:00
parent 3301c04a97
commit 750621e28c

View File

@ -372,9 +372,10 @@ static bool load_content_from_compressed_archive(
char **error_string) char **error_string)
{ {
union string_list_elem_attr attributes; union string_list_elem_attr attributes;
char new_path[PATH_MAX_LENGTH];
char new_basedir[PATH_MAX_LENGTH];
ssize_t new_path_len = 0; ssize_t new_path_len = 0;
size_t path_size = PATH_MAX_LENGTH * sizeof(char);
char *new_basedir = (char*)malloc(PATH_MAX_LENGTH * sizeof(char));
char *new_path = (char*)malloc(PATH_MAX_LENGTH * sizeof(char));
bool ret = false; bool ret = false;
new_path[0] = '\0'; new_path[0] = '\0';
@ -386,7 +387,7 @@ static bool load_content_from_compressed_archive(
if (!string_is_empty(content_ctx->directory_cache)) if (!string_is_empty(content_ctx->directory_cache))
strlcpy(new_basedir, content_ctx->directory_cache, strlcpy(new_basedir, content_ctx->directory_cache,
sizeof(new_basedir)); path_size);
if (string_is_empty(new_basedir) || !path_is_directory(new_basedir)) if (string_is_empty(new_basedir) || !path_is_directory(new_basedir))
{ {
@ -395,35 +396,47 @@ static bool load_content_from_compressed_archive(
"Setting cache directory to directory " "Setting cache directory to directory "
"derived by basename...\n"); "derived by basename...\n");
fill_pathname_basedir(new_basedir, path, fill_pathname_basedir(new_basedir, path,
sizeof(new_basedir)); path_size);
} }
new_path[0] = '\0'; new_path[0] = '\0';
new_basedir[0] = '\0'; new_basedir[0] = '\0';
fill_pathname_join(new_path, new_basedir, fill_pathname_join(new_path, new_basedir,
path_basename(path), sizeof(new_path)); path_basename(path), path_size);
ret = file_archive_compressed_read(path, NULL, new_path, &new_path_len); ret = file_archive_compressed_read(path,
NULL, new_path, &new_path_len);
if (!ret || new_path_len < 0) if (!ret || new_path_len < 0)
{ {
char str[1024]; char *str = (char*)malloc(1024 * sizeof(char));
snprintf(str, sizeof(str), "%s \"%s\".\n", snprintf(str,
1024 * sizeof(char),
"%s \"%s\".\n",
msg_hash_to_str(MSG_COULD_NOT_READ_CONTENT_FILE), msg_hash_to_str(MSG_COULD_NOT_READ_CONTENT_FILE),
path); path);
*error_string = strdup(str); *error_string = strdup(str);
return false; free(str);
goto error;
} }
string_list_append(additional_path_allocs, new_path, attributes); string_list_append(additional_path_allocs, new_path, attributes);
info[i].path = info[i].path =
additional_path_allocs->elems[additional_path_allocs->size -1 ].data; additional_path_allocs->elems[additional_path_allocs->size -1 ].data;
if (!string_list_append(content_ctx->temporary_content, new_path, attributes)) if (!string_list_append(content_ctx->temporary_content,
return false; new_path, attributes))
goto error;
free(new_basedir);
free(new_path);
return true; return true;
error:
free(new_basedir);
free(new_path);
return false;
} }
static bool content_file_init_extract( static bool content_file_init_extract(
@ -508,9 +521,10 @@ static bool content_file_load(
{ {
unsigned i; unsigned i;
retro_ctx_load_content_info_t load_info; retro_ctx_load_content_info_t load_info;
char msg[1024]; size_t msg_size = 1024 * sizeof(char);
char *msg = (char*)malloc(1024 * sizeof(char));
msg[0] = '\0'; msg[0] = '\0';
for (i = 0; i < content->size; i++) for (i = 0; i < content->size; i++)
{ {
@ -523,10 +537,10 @@ static bool content_file_load(
{ {
strlcpy(msg, strlcpy(msg,
msg_hash_to_str(MSG_ERROR_LIBRETRO_CORE_REQUIRES_CONTENT), msg_hash_to_str(MSG_ERROR_LIBRETRO_CORE_REQUIRES_CONTENT),
sizeof(msg) msg_size
); );
*error_string = strdup(msg); *error_string = strdup(msg);
return false; goto error;
} }
info[i].path = NULL; info[i].path = NULL;
@ -544,12 +558,13 @@ static bool content_file_load(
content_ctx, content_ctx,
i, path, (void**)&info[i].data, &len)) i, path, (void**)&info[i].data, &len))
{ {
snprintf(msg, sizeof(msg), snprintf(msg,
msg_size,
"%s \"%s\".\n", "%s \"%s\".\n",
msg_hash_to_str(MSG_COULD_NOT_READ_CONTENT_FILE), msg_hash_to_str(MSG_COULD_NOT_READ_CONTENT_FILE),
path); path);
*error_string = strdup(msg); *error_string = strdup(msg);
return false; goto error;
} }
info[i].size = len; info[i].size = len;
@ -569,7 +584,7 @@ static bool content_file_load(
&info[i], i, &info[i], i,
additional_path_allocs, need_fullpath, path, additional_path_allocs, need_fullpath, path,
error_string)) error_string))
return false; goto error;
#endif #endif
} }
} }
@ -580,10 +595,11 @@ static bool content_file_load(
if (!core_load_game(&load_info)) if (!core_load_game(&load_info))
{ {
snprintf(msg, sizeof(msg), snprintf(msg,
msg_size,
"%s.", msg_hash_to_str(MSG_FAILED_TO_LOAD_CONTENT)); "%s.", msg_hash_to_str(MSG_FAILED_TO_LOAD_CONTENT));
*error_string = strdup(msg); *error_string = strdup(msg);
return false; goto error;
} }
#ifdef HAVE_CHEEVOS #ifdef HAVE_CHEEVOS
@ -599,15 +615,22 @@ static bool content_file_load(
} }
#endif #endif
free(msg);
return true; return true;
error:
free(msg);
return false;
} }
static const struct retro_subsystem_info *content_file_init_subsystem( static const struct
retro_subsystem_info *content_file_init_subsystem(
content_information_ctx_t *content_ctx, content_information_ctx_t *content_ctx,
char **error_string, char **error_string,
bool *ret) bool *ret)
{ {
char msg[1024]; size_t path_size = 1024 * sizeof(char);
char *msg = (char*)malloc(1024 * sizeof(char));
struct string_list *subsystem = path_get_subsystem_list(); struct string_list *subsystem = path_get_subsystem_list();
const struct retro_subsystem_info *special = libretro_find_subsystem_info( const struct retro_subsystem_info *special = libretro_find_subsystem_info(
content_ctx->subsystem.data, content_ctx->subsystem.size, content_ctx->subsystem.data, content_ctx->subsystem.size,
@ -617,7 +640,7 @@ static const struct retro_subsystem_info *content_file_init_subsystem(
if (!special) if (!special)
{ {
snprintf(msg, sizeof(msg), snprintf(msg, path_size,
"Failed to find subsystem \"%s\" in libretro implementation.\n", "Failed to find subsystem \"%s\" in libretro implementation.\n",
path_get(RARCH_PATH_SUBSYSTEM)); path_get(RARCH_PATH_SUBSYSTEM));
*error_string = strdup(msg); *error_string = strdup(msg);
@ -628,14 +651,15 @@ static const struct retro_subsystem_info *content_file_init_subsystem(
{ {
strlcpy(msg, strlcpy(msg,
msg_hash_to_str(MSG_ERROR_LIBRETRO_CORE_REQUIRES_SPECIAL_CONTENT), msg_hash_to_str(MSG_ERROR_LIBRETRO_CORE_REQUIRES_SPECIAL_CONTENT),
sizeof(msg) path_size
); );
*error_string = strdup(msg); *error_string = strdup(msg);
goto error; goto error;
} }
else if (special->num_roms && (special->num_roms != subsystem->size)) else if (special->num_roms && (special->num_roms != subsystem->size))
{ {
snprintf(msg, sizeof(msg), snprintf(msg,
path_size,
"Libretro core requires %u content files for " "Libretro core requires %u content files for "
"subsystem \"%s\", but %u content files were provided.\n", "subsystem \"%s\", but %u content files were provided.\n",
special->num_roms, special->desc, special->num_roms, special->desc,
@ -645,7 +669,8 @@ static const struct retro_subsystem_info *content_file_init_subsystem(
} }
else if (!special->num_roms && subsystem && subsystem->size) else if (!special->num_roms && subsystem && subsystem->size)
{ {
snprintf(msg, sizeof(msg), snprintf(msg,
path_size,
"Libretro core takes no content for subsystem \"%s\", " "Libretro core takes no content for subsystem \"%s\", "
"but %u content files were provided.\n", "but %u content files were provided.\n",
special->desc, special->desc,
@ -655,10 +680,12 @@ static const struct retro_subsystem_info *content_file_init_subsystem(
} }
*ret = true; *ret = true;
free(msg);
return special; return special;
error: error:
*ret = false; *ret = false;
free(msg);
return NULL; return NULL;
} }
@ -955,15 +982,15 @@ static bool command_event_cmd_exec(const char *data,
static bool firmware_update_status( static bool firmware_update_status(
content_information_ctx_t *content_ctx) content_information_ctx_t *content_ctx)
{ {
char s[PATH_MAX_LENGTH];
core_info_ctx_firmware_t firmware_info; core_info_ctx_firmware_t firmware_info;
core_info_t *core_info = NULL; core_info_t *core_info = NULL;
size_t s_size = PATH_MAX_LENGTH * sizeof(char);
char *s = (char*)malloc(PATH_MAX_LENGTH * sizeof(char));
core_info_get_current_core(&core_info); core_info_get_current_core(&core_info);
if (!core_info) if (!core_info)
return false; goto error;
firmware_info.path = core_info->path; firmware_info.path = core_info->path;
@ -971,12 +998,13 @@ static bool firmware_update_status(
firmware_info.directory.system = content_ctx->directory_system; firmware_info.directory.system = content_ctx->directory_system;
else else
{ {
strlcpy(s, path_get(RARCH_PATH_CONTENT) ,sizeof(s)); strlcpy(s, path_get(RARCH_PATH_CONTENT), s_size);
path_basedir_wrapper(s); path_basedir_wrapper(s);
firmware_info.directory.system = s; firmware_info.directory.system = s;
} }
RARCH_LOG("Updating firmware status for: %s on %s\n", core_info->path, RARCH_LOG("Updating firmware status for: %s on %s\n",
core_info->path,
firmware_info.directory.system); firmware_info.directory.system);
core_info_list_update_missing_firmware(&firmware_info); core_info_list_update_missing_firmware(&firmware_info);
@ -984,12 +1012,18 @@ static bool firmware_update_status(
content_ctx->bios_is_missing && content_ctx->bios_is_missing &&
content_ctx->check_firmware_before_loading) content_ctx->check_firmware_before_loading)
{ {
runloop_msg_queue_push(msg_hash_to_str(MSG_FIRMWARE), 100, 500, true); runloop_msg_queue_push(
RARCH_LOG("Load content blocked. Reason: %s\n", msg_hash_to_str(MSG_FIRMWARE)); msg_hash_to_str(MSG_FIRMWARE),
100, 500, true);
RARCH_LOG("Load content blocked. Reason: %s\n",
msg_hash_to_str(MSG_FIRMWARE));
free(s);
return true; return true;
} }
error:
free(s);
return false; return false;
} }