(Runahead) Cleanups / style nits / etc.

This commit is contained in:
twinaphex 2018-03-29 15:13:33 +02:00
parent eeb2a0a934
commit 2bdfd2e1d0
11 changed files with 379 additions and 377 deletions

View File

@ -1,26 +1,32 @@
#include "libretro.h"
#include "mem_util.h"
#include "core.h"
#include <string.h>
#include <malloc.h>
#include "lists/string_list.h"
#include <libretro.h>
#include <lists/string_list.h>
#include "../core.h"
#include "mem_util.h"
retro_ctx_load_content_info_t *load_content_info;
enum rarch_core_type last_core_type;
static void free_retro_game_info(struct retro_game_info *dest)
{
if (dest == NULL) return;
if (dest == NULL)
return;
FREE(dest->path);
FREE(dest->data);
FREE(dest->meta);
}
static struct retro_game_info* clone_retro_game_info(const struct retro_game_info *src)
static struct retro_game_info* clone_retro_game_info(const
struct retro_game_info *src)
{
struct retro_game_info *dest;
if (src == NULL) return NULL;
dest = (struct retro_game_info*)malloc_zero(sizeof(struct retro_game_info));
if (src == NULL)
return NULL;
dest = (struct retro_game_info*)malloc_zero(
sizeof(struct retro_game_info));
dest->path = strcpy_alloc(src->path);
dest->data = memcpy_alloc(src->data, src->size);
dest->size = src->size;
@ -31,23 +37,29 @@ static struct retro_game_info* clone_retro_game_info(const struct retro_game_inf
static void free_string_list(struct string_list *dest)
{
int i;
if (dest == NULL) return;
if (dest == NULL)
return;
for (i = 0; i < dest->size; i++)
{
FREE(dest->elems[i].data);
}
FREE(dest->elems);
}
static struct string_list* clone_string_list(const struct string_list *src)
{
int i;
struct string_list *dest;
if (src == NULL) return NULL;
dest = (struct string_list*)malloc_zero(sizeof(struct string_list));
dest->size = src->size;
dest->cap = src->cap;
dest->elems = (struct string_list_elem*)malloc_zero(sizeof(struct string_list_elem) * dest->size);
struct string_list *dest = NULL;
if (src == NULL)
return NULL;
dest = (struct string_list*)malloc_zero(sizeof(struct string_list));
dest->size = src->size;
dest->cap = src->cap;
dest->elems = (struct string_list_elem*)malloc_zero(sizeof(struct string_list_elem) * dest->size);
for (i = 0; i < src->size; i++)
{
dest->elems[i].data = strcpy_alloc(src->elems[i].data);
@ -57,105 +69,134 @@ static struct string_list* clone_string_list(const struct string_list *src)
}
#if 0
/* for cloning the Special field, however, attempting to use this feature crashes retroarch */
static void free_retro_subsystem_memory_info(struct retro_subsystem_memory_info *dest)
/* for cloning the Special field, however, attempting
* to use this feature crashes retroarch */
static void free_retro_subsystem_memory_info(struct
retro_subsystem_memory_info *dest)
{
if (dest == NULL) return;
FREE(dest->extension);
}
static void clone_retro_subsystem_memory_info(struct retro_subsystem_memory_info* dest, const struct retro_subsystem_memory_info *src)
static void clone_retro_subsystem_memory_info(struct
retro_subsystem_memory_info* dest,
const struct retro_subsystem_memory_info *src)
{
dest->extension = strcpy_alloc(src->extension);
dest->type = src->type;
dest->type = src->type;
}
static void free_retro_subsystem_rom_info(struct retro_subsystem_rom_info *dest)
static void free_retro_subsystem_rom_info(struct
retro_subsystem_rom_info *dest)
{
int i;
if (dest == NULL) return;
if (dest == NULL)
return;
FREE(dest->desc);
FREE(dest->valid_extensions);
for (i = 0; i < dest->num_memory; i++)
{
free_retro_subsystem_memory_info((struct retro_subsystem_memory_info*)&dest->memory[i]);
}
free_retro_subsystem_memory_info((struct
retro_subsystem_memory_info*)&dest->memory[i]);
FREE(dest->memory);
}
static void clone_retro_subsystem_rom_info(struct retro_subsystem_rom_info *dest, const struct retro_subsystem_rom_info *src)
static void clone_retro_subsystem_rom_info(struct
retro_subsystem_rom_info *dest,
const struct retro_subsystem_rom_info *src)
{
int i;
retro_subsystem_memory_info *memory;
dest->need_fullpath = src->need_fullpath;
dest->block_extract = src->block_extract;
dest->required = src->required;
dest->num_memory = src->num_memory;
dest->desc = strcpy_alloc(src->desc);
retro_subsystem_memory_info *memory = NULL;
dest->need_fullpath = src->need_fullpath;
dest->block_extract = src->block_extract;
dest->required = src->required;
dest->num_memory = src->num_memory;
dest->desc = strcpy_alloc(src->desc);
dest->valid_extensions = strcpy_alloc(src->valid_extensions);
memory = (struct retro_subsystem_memory_info*)malloc_zero(dest->num_memory * sizeof(struct retro_subsystem_memory_info));
dest->memory = memory;
memory = (struct retro_subsystem_memory_info*)malloc_zero(
dest->num_memory * sizeof(struct retro_subsystem_memory_info));
dest->memory = memory;
for (i = 0; i < dest->num_memory; i++)
{
clone_retro_subsystem_memory_info(&memory[i], &src->memory[i]);
}
}
static void free_retro_subsystem_info(struct retro_subsystem_info *dest)
{
int i;
if (dest == NULL) return;
if (dest == NULL)
return;
FREE(dest->desc);
FREE(dest->ident);
for (i = 0; i < dest->num_roms; i++)
{
free_retro_subsystem_rom_info((struct retro_subsystem_rom_info*)&dest->roms[i]);
}
free_retro_subsystem_rom_info((struct
retro_subsystem_rom_info*)&dest->roms[i]);
FREE(dest->roms);
}
static retro_subsystem_info* clone_retro_subsystem_info(struct const retro_subsystem_info *src)
static retro_subsystem_info* clone_retro_subsystem_info(struct
const retro_subsystem_info *src)
{
int i;
retro_subsystem_info *dest;
retro_subsystem_rom_info *roms;
if (src == NULL) return NULL;
dest = (struct retro_subsystem_info*)malloc_zero(sizeof(struct retro_subsystem_info));
dest->desc = strcpy_alloc(src->desc);
dest->ident = strcpy_alloc(src->ident);
dest->num_roms = src->num_roms;
dest->id = src->id;
retro_subsystem_info *dest = NULL;
retro_subsystem_rom_info *roms = NULL;
if (src == NULL)
return NULL;
dest = (struct retro_subsystem_info*)malloc_zero(
sizeof(struct retro_subsystem_info));
dest->desc = strcpy_alloc(src->desc);
dest->ident = strcpy_alloc(src->ident);
dest->num_roms = src->num_roms;
dest->id = src->id;
roms = (struct retro_subsystem_rom_info*)
malloc_zero(src->num_roms * sizeof(struct retro_subsystem_rom_info));
dest->roms = roms;
roms = (struct retro_subsystem_rom_info*)malloc_zero(src->num_roms * sizeof(struct retro_subsystem_rom_info));
dest->roms = roms;
for (i = 0; i < src->num_roms; i++)
{
clone_retro_subsystem_rom_info(&roms[i], &src->roms[i]);
}
return dest;
}
#endif
static void free_retro_ctx_load_content_info(struct retro_ctx_load_content_info *dest)
static void free_retro_ctx_load_content_info(struct
retro_ctx_load_content_info *dest)
{
if (dest == NULL) return;
if (dest == NULL)
return;
free_retro_game_info(dest->info);
free_string_list((struct string_list*)dest->content);
FREE(dest->info);
FREE(dest->content);
#if 0
free_retro_subsystem_info((retro_subsystem_info*)dest->special);
FREE(dest->special);
#endif
}
static struct retro_ctx_load_content_info* clone_retro_ctx_load_content_info(const struct retro_ctx_load_content_info *src)
static struct retro_ctx_load_content_info
*clone_retro_ctx_load_content_info(
const struct retro_ctx_load_content_info *src)
{
struct retro_ctx_load_content_info *dest;
if (src == NULL || src->special != NULL) return NULL; /* refuse to deal with the Special field */
if (src == NULL || src->special != NULL)
return NULL; /* refuse to deal with the Special field */
dest = (struct retro_ctx_load_content_info*)malloc_zero(sizeof(struct retro_ctx_load_content_info));
dest->info = clone_retro_game_info(src->info);
dest = (struct retro_ctx_load_content_info*)malloc_zero(
sizeof(struct retro_ctx_load_content_info));
dest->info = clone_retro_game_info(src->info);
dest->content = clone_string_list(src->content);
dest->special = NULL;
#if 0

View File

@ -1,10 +1,11 @@
#ifndef __COPY_LOAD_INFO_H__
#define __COPY_LOAD_INFO_H__
#include "retro_common_api.h"
#include "libretro.h"
#include "core.h"
#include "boolean.h"
#include <retro_common_api.h>
#include <libretro.h>
#include <boolean.h>
#include "../core.h"
RETRO_BEGIN_DECLS

View File

@ -1,7 +1,9 @@
#include "core.h"
#include "boolean.h"
#include <boolean.h>
#include "../core.h"
#include "../dynamic.h"
#include "mylist.h"
#include "dynamic.h"
#include "mem_util.h"
bool input_is_dirty;
@ -30,9 +32,9 @@ static bool unserialze_hook(const void *buf, size_t size);
static void* InputListElementConstructor(void)
{
void *ptr;
const int size = sizeof(InputListElement);
ptr = malloc_zero(size);
void *ptr = malloc_zero(size);
return ptr;
}
@ -41,21 +43,21 @@ static void input_state_destory(void)
mylist_destroy(&inputStateList);
}
static void input_state_setlast(unsigned port, unsigned device, unsigned index, unsigned id, int16_t value)
static void input_state_setlast(unsigned port, unsigned device,
unsigned index, unsigned id, int16_t value)
{
int i;
InputListElement *element;
if (inputStateList == NULL)
{
if (!inputStateList)
mylist_create(&inputStateList, 16, InputListElementConstructor, free);
}
/* find list item */
for (i = 0; i < inputStateList->size; i++)
{
element = (InputListElement*)inputStateList->data[i];
if (element->port == port && element->device == device && element->index == index)
if ( element->port == port
&& element->device == device && element->index == index)
{
element->state[id] = value;
return;
@ -71,19 +73,17 @@ static void input_state_setlast(unsigned port, unsigned device, unsigned index,
static int16_t input_state_getlast(unsigned port, unsigned device, unsigned index, unsigned id)
{
int i;
InputListElement *element;
if (inputStateList == NULL)
{
InputListElement *element = NULL;
if (!inputStateList)
return 0;
}
/* find list item */
for (i = 0; i < inputStateList->size; i++)
{
element = (InputListElement*)inputStateList->data[i];
if (element->port == port && element->device == device && element->index == index)
{
return element->state[id];
}
}
return 0;
}
@ -95,9 +95,7 @@ static int16_t input_state_with_logging(unsigned port, unsigned device, unsigned
int16_t result = input_state_callback_original(port, device, index, id);
int16_t lastInput = input_state_getlast(port, device, index, id);
if (result != lastInput)
{
input_is_dirty = true;
}
input_state_setlast(port, device, index, id, result);
return result;
}
@ -108,18 +106,14 @@ static void reset_hook(void)
{
input_is_dirty = true;
if (retro_reset_callback_original)
{
retro_reset_callback_original();
}
}
static bool unserialze_hook(const void *buf, size_t size)
{
input_is_dirty = true;
if (retro_unserialize_callback_original)
{
return retro_unserialize_callback_original(buf, size);
}
return false;
}

View File

@ -2,8 +2,7 @@
void *malloc_zero(size_t size)
{
void *ptr;
ptr = malloc(size);
void *ptr = malloc(size);
memset(ptr, 0, size);
return ptr;
}
@ -15,42 +14,30 @@ void free_str(char **str_p)
void free_ptr(void **data_p)
{
if (data_p == NULL)
{
if (!data_p || !*data_p)
return;
}
if (*data_p == NULL)
{
return;
}
free(*data_p);
*data_p = NULL;
}
void *memcpy_alloc(const void *src, size_t size)
{
void *result;
result = malloc(size);
void *result = malloc(size);
memcpy(result, src, size);
return result;
}
char *strcpy_alloc(const char *sourceStr)
{
size_t len;
char *result;
if (sourceStr == NULL)
{
len = 0;
}
else
{
size_t len = 0;
char *result = NULL;
if (sourceStr)
len = strlen(sourceStr);
}
if (len == 0)
{
return NULL;
}
result = (char*)malloc(len + 1);
strcpy(result, sourceStr);
return result;
@ -58,38 +45,31 @@ char *strcpy_alloc(const char *sourceStr)
char *strcpy_alloc_force(const char *sourceStr)
{
char *result;
result = strcpy_alloc(sourceStr);
if (result == NULL)
{
char *result = strcpy_alloc(sourceStr);
if (!result)
result = (char*)malloc_zero(1);
}
return result;
}
void strcat_alloc(char ** destStr_p, const char *appendStr)
{
size_t len1, len2, newLen;
char *destStr;
char *destStr = *destStr_p;
destStr = *destStr_p;
if (destStr == NULL)
if (!destStr)
{
destStr = strcpy_alloc_force(appendStr);
destStr = strcpy_alloc_force(appendStr);
*destStr_p = destStr;
return;
}
if (appendStr == NULL)
{
if (!appendStr)
return;
}
len1 = strlen(destStr);
len2 = strlen(appendStr);
newLen = len1 + len2 + 1;
destStr = (char*)realloc(destStr, newLen);
len1 = strlen(destStr);
len2 = strlen(appendStr);
newLen = len1 + len2 + 1;
destStr = (char*)realloc(destStr, newLen);
*destStr_p = destStr;
strcpy(destStr + len1, appendStr);
}

View File

@ -1,13 +1,13 @@
#ifndef __MEM_UTIL__
#define __MEM_UTIL__
#include "retro_common_api.h"
#include "boolean.h"
#include <malloc.h>
#include <stddef.h>
#include <string.h>
#include <retro_common_api.h>
#include <boolean.h>
#define FREE(xxxx) if ((xxxx) != NULL) { free((void*)(xxxx)); } (xxxx) = NULL
RETRO_BEGIN_DECLS

View File

@ -8,24 +8,28 @@ void mylist_resize(MyList *list, int newSize, bool runConstructor)
int newCapacity;
int oldSize;
int i;
void *element;
if (newSize < 0) newSize = 0;
if (list == NULL) return;
void *element = NULL;
if (newSize < 0)
newSize = 0;
if (!list)
return;
newCapacity = newSize;
oldSize = list->size;
if (newSize == oldSize) return;
oldSize = list->size;
if (newSize == oldSize)
return;
if (newSize > list->capacity)
{
if (newCapacity < list->capacity * 2)
{
newCapacity = list->capacity * 2;
}
/* try to realloc */
list->data = (void**)realloc((void*)list->data, newCapacity * sizeof(void*));
for (i = list->capacity; i < newCapacity; i++)
{
list->data[i] = NULL;
}
list->capacity = newCapacity;
}
if (newSize <= list->size)
@ -33,7 +37,8 @@ void mylist_resize(MyList *list, int newSize, bool runConstructor)
for (i = newSize; i < list->size; i++)
{
element = list->data[i];
if (element != NULL)
if (element)
{
list->Destructor(element);
list->data[i] = NULL;
@ -45,13 +50,9 @@ void mylist_resize(MyList *list, int newSize, bool runConstructor)
for (i = list->size; i < newSize; i++)
{
if (runConstructor)
{
list->data[i] = list->Constructor();
}
else
{
list->data[i] = NULL;
}
}
}
list->size = newSize;
@ -60,45 +61,54 @@ void mylist_resize(MyList *list, int newSize, bool runConstructor)
void *mylist_add_element(MyList *list)
{
int oldSize;
if (list == NULL) return NULL;
if (!list)
return NULL;
oldSize = list->size;
mylist_resize(list, oldSize + 1, true);
return list->data[oldSize];
}
void mylist_create(MyList **list_p, int initialCapacity, constructor_t constructor, destructor_t destructor)
void mylist_create(MyList **list_p, int initialCapacity,
constructor_t constructor, destructor_t destructor)
{
MyList *list;
if (list_p == NULL) return;
if (initialCapacity < 0) initialCapacity = 0;
list = *list_p;
if (list != NULL)
{
MyList *list = NULL;
if (!list_p)
return;
if (initialCapacity < 0)
initialCapacity = 0;
list = *list_p;
if (list)
mylist_destroy(list_p);
}
list = (MyList*)malloc(sizeof(MyList));
*list_p = list;
list->size = 0;
list->Constructor = constructor;
list->Destructor = destructor;
list = (MyList*)malloc(sizeof(MyList));
*list_p = list;
list->size = 0;
list->Constructor = constructor;
list->Destructor = destructor;
if (initialCapacity > 0)
{
list->data = (void**)malloc_zero(initialCapacity * sizeof(void*));
list->capacity = initialCapacity;
list->data = (void**)malloc_zero(initialCapacity * sizeof(void*));
list->capacity = initialCapacity;
}
else
{
list->data = NULL;
list->capacity = 0;
list->data = NULL;
list->capacity = 0;
}
}
void mylist_destroy(MyList **list_p)
{
if (list_p == NULL) return;
MyList *list;
MyList *list = NULL;
if (!list_p)
return;
list = *list_p;
if (list != NULL)
if (list)
{
mylist_resize(list, 0, false);
free(list->data);
@ -109,11 +119,11 @@ void mylist_destroy(MyList **list_p)
void mylist_assign(MyList *list, int index, void *value)
{
void *oldElement;
void *oldElement = NULL;
if (index < 0 || index >= list->size)
{
return;
}
oldElement = list->data[index];
list->Destructor(oldElement);
list->data[index] = value;
@ -124,14 +134,13 @@ void mylist_remove_at(MyList *list, int index)
int i;
if (index < 0 || index >= list->size)
{
return;
}
mylist_assign(list, index, NULL);
for (i = index + 1; i < list->size; i++)
{
list->data[i - 1] = list->data[i];
}
list->size--;
list->data[list->size] = NULL;
}
@ -144,7 +153,8 @@ void mylist_pop_front(MyList *list)
void mylist_push_back(MyList *list, void *value)
{
int oldSize;
if (list == NULL) return;
if (!list)
return;
oldSize = list->size;
mylist_resize(list, oldSize + 1, false);
list->data[oldSize] = value;

View File

@ -1,8 +1,8 @@
#ifndef __MYLIST_H__
#define __MYLIST_H__
#include "retro_common_api.h"
#include "boolean.h"
#include <retro_common_api.h>
#include <boolean.h>
RETRO_BEGIN_DECLS

View File

@ -62,9 +62,9 @@ static void *runahead_save_state_alloc(void)
static void runahead_save_state_free(void *state)
{
retro_ctx_serialize_info_t *savestate;
savestate = (retro_ctx_serialize_info_t*)state;
if (savestate == NULL) return;
retro_ctx_serialize_info_t *savestate = (retro_ctx_serialize_info_t*)state;
if (!savestate)
return;
free(savestate->data);
free(savestate);
}
@ -72,7 +72,8 @@ static void runahead_save_state_free(void *state)
static void runahead_save_state_list_init(size_t saveStateSize)
{
runahead_save_state_size = saveStateSize;
mylist_create(&runahead_save_state_list, 16, runahead_save_state_alloc, runahead_save_state_free);
mylist_create(&runahead_save_state_list, 16,
runahead_save_state_alloc, runahead_save_state_free);
}
static void runahead_save_state_list_destroy(void)
@ -107,27 +108,30 @@ static void unload_hook(void);
static void add_hooks(void)
{
if (originalRetroDeinit == NULL)
if (!originalRetroDeinit)
{
originalRetroDeinit = current_core.retro_deinit;
originalRetroDeinit = current_core.retro_deinit;
current_core.retro_deinit = deinit_hook;
}
if (originalRetroUnload == NULL)
if (!originalRetroUnload)
{
originalRetroUnload = current_core.retro_unload_game;
current_core.retro_unload_game = unload_hook;
}
add_input_state_hook();
}
static void remove_hooks(void)
{
if (originalRetroDeinit != NULL)
if (originalRetroDeinit)
{
current_core.retro_deinit = originalRetroDeinit;
originalRetroDeinit = NULL;
}
if (originalRetroUnload != NULL)
if (originalRetroUnload)
{
current_core.retro_unload_game = originalRetroUnload;
originalRetroUnload = NULL;
@ -159,33 +163,34 @@ static void unload_hook(void)
/* Runahead Code */
static bool runahead_video_driver_is_active = true;
static bool runahead_available = true;
static bool runahead_video_driver_is_active = true;
static bool runahead_available = true;
static bool runahead_secondary_core_available = true;
static bool runahead_force_input_dirty = true;
static uint64_t runahead_last_frame_count = 0;
static bool runahead_force_input_dirty = true;
static uint64_t runahead_last_frame_count = 0;
static void runahead_clear_variables(void)
{
runahead_save_state_size = -1;
runahead_video_driver_is_active = true;
runahead_available = true;
runahead_save_state_size = -1;
runahead_video_driver_is_active = true;
runahead_available = true;
runahead_secondary_core_available = true;
runahead_force_input_dirty = true;
runahead_last_frame_count = 0;
runahead_force_input_dirty = true;
runahead_last_frame_count = 0;
}
static void runahead_check_for_gui(void)
{
/* Hack: If we were in the GUI, force a resync. */
bool is_dirty;
bool is_alive, is_focused;
uint64_t frame_count;
bool is_dirty = false;
bool is_alive, is_focused = false;
uint64_t frame_count = 0;
video_driver_get_status(&frame_count, &is_alive, &is_focused);
if (frame_count != runahead_last_frame_count + 1)
{
runahead_force_input_dirty = true;
}
runahead_last_frame_count = frame_count;
}
@ -207,7 +212,7 @@ void run_ahead(int runAheadCount, bool useSecondary)
{
if (!runahead_create())
{
/*runloop_msg_queue_push("RunAhead has been disabled because the core does not support savestates", 1, 180, true);*/
/* RunAhead has been disabled because the core does not support savestates. */
core_run();
runahead_force_input_dirty = true;
return;
@ -228,34 +233,30 @@ void run_ahead(int runAheadCount, bool useSecondary)
runahead_suspend_audio();
runahead_suspend_video();
}
if (frameNumber == 0)
{
core_run();
}
else
{
core_run_no_input_polling();
}
if (suspendedFrame)
{
runahead_resume_video();
runahead_resume_audio();
}
if (frameNumber == 0)
{
/* RunAhead has been disabled due to save state failure */
if (!runahead_save_state())
{
/*runloop_msg_queue_push("RunAhead has been disabled due to save state failure", 1, 180, true);*/
return;
}
}
if (lastFrame)
{
/* RunAhead has been disabled due to load state failure */
if (!runahead_load_state())
{
/*runloop_msg_queue_push("RunAhead has been disabled due to load state failure", 1, 180, true);*/
return;
}
}
}
}
@ -269,38 +270,40 @@ void run_ahead(int runAheadCount, bool useSecondary)
if (input_is_dirty || runahead_force_input_dirty)
{
input_is_dirty = false;
unsigned frame_count;
input_is_dirty = false;
if (!runahead_save_state())
{
return;
}
/* Could not create a secondary core.
* RunAhead wll only use the main core now. */
if (!runahead_load_state_secondary())
{
/*runloop_msg_queue_push("Could not create a secondary core. RunAhead will only use the main core now.", 1, 180, true);*/
return;
}
for (int frameCount = 0; frameCount < runAheadCount - 1; frameCount++)
for (frame_count = 0; frame_count < runAheadCount - 1; frame_count++)
{
runahead_suspend_video();
runahead_suspend_audio();
okay = runahead_run_secondary();
runahead_resume_audio();
runahead_resume_video();
/* Could not create a secondary core. RunAhead
* will only use the main core now. */
if (!okay)
{
/*runloop_msg_queue_push("Could not create a secondary core. RunAhead will only use the main core now.", 1, 180, true);*/
return;
}
}
}
runahead_suspend_audio();
okay = runahead_run_secondary();
runahead_resume_audio();
/* Could not create a secondary core. RunAhead
* will only use the main core now. */
if (!okay)
{
/*runloop_msg_queue_push("Could not create a secondary core. RunAhead will only use the main core now.", 1, 180, true);*/
return;
}
#endif
}
runahead_force_input_dirty = false;
@ -328,6 +331,7 @@ static bool runahead_create(void)
runahead_error();
return false;
}
add_hooks();
runahead_force_input_dirty = true;
mylist_resize(runahead_save_state_list, 1, true);
@ -336,54 +340,45 @@ static bool runahead_create(void)
static bool runahead_save_state(void)
{
bool okay;
retro_ctx_serialize_info_t *serialize_info;
serialize_info = (retro_ctx_serialize_info_t*)runahead_save_state_list->data[0];
okay = core_serialize(serialize_info);
retro_ctx_serialize_info_t *serialize_info =
(retro_ctx_serialize_info_t*)runahead_save_state_list->data[0];
bool okay = core_serialize(serialize_info);
if (!okay)
{
runahead_error();
}
return okay;
}
static bool runahead_load_state(void)
{
bool okay;
bool lastDirty;
retro_ctx_serialize_info_t *serialize_info;
serialize_info = (retro_ctx_serialize_info_t*)runahead_save_state_list->data[0];
lastDirty = input_is_dirty;
okay = core_unserialize(serialize_info);
retro_ctx_serialize_info_t *serialize_info = (retro_ctx_serialize_info_t*)runahead_save_state_list->data[0];
bool lastDirty = input_is_dirty;
bool okay = core_unserialize(serialize_info);
input_is_dirty = lastDirty;
if (!okay)
{
runahead_error();
}
return okay;
}
static bool runahead_load_state_secondary(void)
{
bool okay;
retro_ctx_serialize_info_t *serialize_info;
serialize_info = (retro_ctx_serialize_info_t*)runahead_save_state_list->data[0];
okay = secondary_core_deserialize(serialize_info->data_const, serialize_info->size);
retro_ctx_serialize_info_t *serialize_info =
(retro_ctx_serialize_info_t*)runahead_save_state_list->data[0];
bool okay = secondary_core_deserialize(serialize_info->data_const, serialize_info->size);
if (!okay)
{
runahead_secondary_core_available = false;
}
return okay;
}
static bool runahead_run_secondary(void)
{
bool okay;
okay = secondary_core_run_no_input_polling();
bool okay = secondary_core_run_no_input_polling();
if (!okay)
{
runahead_secondary_core_available = false;
}
return okay;
}
@ -405,13 +400,9 @@ static void runahead_suspend_video(void)
static void runahead_resume_video(void)
{
if (runahead_video_driver_is_active)
{
video_driver_set_active();
}
else
{
video_driver_unset_active();
}
}
void runahead_destroy(void)

View File

@ -1,8 +1,8 @@
#ifndef __RUN_AHEAD_H__
#define __RUN_AHEAD_H__
#include "boolean.h"
#include "retro_common_api.h"
#include <boolean.h>
#include <retro_common_api.h>
RETRO_BEGIN_DECLS

View File

@ -10,17 +10,19 @@
#endif
#endif
#include <boolean.h>
#include <encodings/utf.h>
#include <compat/fopen_utf8.h>
#include <compat/unlink_utf8.h>
#include <dynamic/dylib.h>
#include <dynamic.h>
#include <file/file_path.h>
#include "mem_util.h"
#include "boolean.h"
#include "encodings/utf.h"
#include "compat/fopen_utf8.h"
#include "compat/unlink_utf8.h"
#include "dynamic/dylib.h"
#include "dynamic.h"
#include "core.h"
#include "file/file_path.h"
#include "paths.h"
#include "content.h"
#include "../core.h"
#include "../paths.h"
#include "../content.h"
static int port_map[16];
@ -37,18 +39,32 @@ extern enum rarch_core_type last_core_type;
extern struct retro_callbacks retro_ctx;
static char* get_temp_directory_alloc(void);
static char* copy_core_to_temp_file(void);
static void* read_file_data_alloc(const char *fileName, int *size);
static bool write_file_data(const char *fileName, const void *data, int dataSize);
static bool write_file_with_random_name(char **tempDllPath, const char *retroarchTempPath, const void* data, int dataSize);
static bool write_file_with_random_name(char **tempDllPath,
const char *retroarchTempPath, const void* data, int dataSize);
static void* InputListElementConstructor(void);
static void secondary_core_clear(void);
static bool secondary_core_create(void);
bool secondary_core_run_no_input_polling(void);
bool secondary_core_deserialize(const void *buffer, int size);
void secondary_core_destroy(void);
void set_last_core_type(enum rarch_core_type type);
void remember_controller_port_device(long port, long device);
void clear_controller_port_map(void);
static void free_file(FILE **file_p);
@ -88,28 +104,21 @@ char* get_temp_directory_alloc(void)
char* copy_core_to_temp_file(void)
{
bool okay;
const char *corePath = NULL; /* ptr to static buffer, do not need to free this */
const char *coreBaseName = NULL; /* ptr to static buffer, do not need to free this */
char *tempDirectory = NULL;
char *retroarchTempPath = NULL;
char *tempDllPath = NULL;
void *dllFileData = NULL;
int dllFileSize = 0;
corePath = path_get(RARCH_PATH_CORE);
coreBaseName = path_basename(corePath);
bool okay = false;
char *tempDirectory = NULL;
char *retroarchTempPath = NULL;
char *tempDllPath = NULL;
void *dllFileData = NULL;
int dllFileSize = 0;
const char *corePath = path_get(RARCH_PATH_CORE);
const char *coreBaseName = path_basename(corePath);
if (strlen(coreBaseName) == 0)
{
goto failed;
}
tempDirectory = get_temp_directory_alloc();
if (tempDirectory == NULL)
{
if (!tempDirectory)
goto failed;
}
strcat_alloc(&retroarchTempPath, tempDirectory);
strcat_alloc(&retroarchTempPath, path_default_slash());
@ -117,27 +126,25 @@ char* copy_core_to_temp_file(void)
strcat_alloc(&retroarchTempPath, path_default_slash());
okay = path_mkdir(retroarchTempPath);
if (!okay)
{
goto failed;
}
dllFileData = read_file_data_alloc(corePath, &dllFileSize);
if (dllFileData == NULL)
{
if (!dllFileData)
goto failed;
}
strcat_alloc(&tempDllPath, retroarchTempPath);
strcat_alloc(&tempDllPath, coreBaseName);
okay = write_file_data(tempDllPath, dllFileData, dllFileSize);
if (!okay)
{
/* try other file names */
okay = write_file_with_random_name(&tempDllPath, retroarchTempPath, dllFileData, dllFileSize);
if (!okay)
{
goto failed;
}
}
success:
free_str(&tempDirectory);
@ -155,20 +162,19 @@ failed:
void* read_file_data_alloc(const char *fileName, int *size)
{
void *data = NULL;
FILE *f = NULL;
int fileSize = 0;
size_t bytesRead = 0;
void *data = NULL;
int fileSize = 0;
size_t bytesRead = 0;
#ifdef _WIN32
int64_t fileSizeLong = 0;
#else
off64_t fileSizeLong = 0;
#endif
f = (FILE*)fopen_utf8(fileName, "rb");
if (f == NULL)
{
FILE *f = (FILE*)fopen_utf8(fileName, "rb");
if (!f)
goto failed;
}
fseek(f, 0, SEEK_END);
#ifdef _WIN32
fileSizeLong = _ftelli64(f);
@ -176,46 +182,48 @@ void* read_file_data_alloc(const char *fileName, int *size)
fileSizeLong = ftello64(f);
#endif
fseek(f, 0, SEEK_SET);
/* 256MB file size limit for DLL files */
if (fileSizeLong < 0 || fileSizeLong > 256 * 1024 * 1024)
{
goto failed;
}
fileSize = (int)fileSizeLong;
data = malloc(fileSize);
if (data == NULL)
{
data = malloc(fileSize);
if (!data)
goto failed;
}
bytesRead = fread(data, 1, fileSize, f);
if ((int)bytesRead != (int)fileSize)
{
goto failed;
}
success:
free_file(&f);
if (size != NULL) *size = fileSize;
if (size)
*size = fileSize;
return data;
failed:
free_ptr(&data);
free_file(&f);
if (size != NULL) *size = 0;
if (size)
*size = 0;
return NULL;
}
bool write_file_data(const char *fileName, const void *data, int dataSize)
{
bool okay = false;
FILE *f = NULL;
bool okay = false;
size_t bytesWritten = 0;
FILE *f = (FILE*)fopen_utf8(fileName, "wb");
f = (FILE*)fopen_utf8(fileName, "wb");
if (f == NULL) goto failed;
bytesWritten = fwrite(data, 1, dataSize, f);
if (bytesWritten != dataSize)
{
if (!f)
goto failed;
}
bytesWritten = fwrite(data, 1, dataSize, f);
if (bytesWritten != dataSize)
goto failed;
success:
free_file(&f);
return true;
@ -224,21 +232,20 @@ failed:
return false;
}
bool write_file_with_random_name(char **tempDllPath, const char *retroarchTempPath, const void* data, int dataSize)
bool write_file_with_random_name(char **tempDllPath,
const char *retroarchTempPath, const void* data, int dataSize)
{
int extLen;
char *ext = NULL;
bool okay = false;
const int maxAttempts = 30;
const char *prefix = "tmp";
char numberBuf[32];
time_t timeValue = time(NULL);
unsigned int numberValue = (unsigned int)timeValue;
int number = 0;
int i;
char numberBuf[32];
bool okay = false;
const int maxAttempts = 30;
const char *prefix = "tmp";
time_t timeValue = time(NULL);
unsigned int numberValue = (unsigned int)timeValue;
int number = 0;
char *ext = strcpy_alloc_force(path_get_extension(*tempDllPath));
int extLen = strlen(ext);
ext = strcpy_alloc_force(path_get_extension(*tempDllPath));
extLen = strlen(ext);
if (extLen > 0)
{
strcat_alloc(&ext, ".");
@ -247,7 +254,6 @@ bool write_file_with_random_name(char **tempDllPath, const char *retroarchTempPa
extLen++;
}
/* try up to 30 'random' filenames before giving up */
for (i = 0; i < 30; i++)
{
@ -261,9 +267,7 @@ bool write_file_with_random_name(char **tempDllPath, const char *retroarchTempPa
strcat_alloc(tempDllPath, ext);
okay = write_file_data(*tempDllPath, data, dataSize);
if (okay)
{
break;
}
}
success:
free_str(&ext);
@ -276,7 +280,7 @@ failed:
void secondary_core_clear(void)
{
secondary_library_path = NULL;
secondary_module = NULL;
secondary_module = NULL;
memset(&secondary_core, 0, sizeof(struct retro_core_t));
}
@ -285,17 +289,17 @@ bool secondary_core_create(void)
long port, device;
bool contentless, is_inited;
if (last_core_type != CORE_TYPE_PLAIN || load_content_info == NULL || load_content_info->special != NULL)
{
if ( last_core_type != CORE_TYPE_PLAIN ||
!load_content_info ||
load_content_info->special)
return false;
}
free_str(&secondary_library_path);
secondary_library_path = copy_core_to_temp_file();
if (secondary_library_path == NULL)
{
if (!secondary_library_path)
return false;
}
/* Load Core */
if (init_libretro_sym_custom(CORE_TYPE_PLAIN, &secondary_core, secondary_library_path, &secondary_module))
{
@ -315,12 +319,13 @@ bool secondary_core_create(void)
secondary_core.inited = is_inited;
/* Load Content */
if (load_content_info == NULL || load_content_info->special != NULL)
if (!load_content_info || load_content_info->special)
{
/* disabled due to crashes */
return false;
#if 0
secondary_core.game_loaded = secondary_core.retro_load_game_special(loadContentInfo.special->id, loadContentInfo.info, loadContentInfo.content->size);
secondary_core.game_loaded = secondary_core.retro_load_game_special(
loadContentInfo.special->id, loadContentInfo.info, loadContentInfo.content->size);
if (!secondary_core.game_loaded)
{
secondary_core_destroy();
@ -328,7 +333,7 @@ bool secondary_core_create(void)
}
#endif
}
else if (load_content_info->content->size > 0 && load_content_info->content->elems[0].data != NULL)
else if (load_content_info->content->size > 0 && load_content_info->content->elems[0].data)
{
secondary_core.game_loaded = secondary_core.retro_load_game(load_content_info->info);
if (!secondary_core.game_loaded)
@ -360,29 +365,23 @@ bool secondary_core_create(void)
{
device = port_map[port];
if (device >= 0)
{
secondary_core.retro_set_controller_port_device(port, device);
}
}
clear_controller_port_map();
}
else
{
return false;
}
return true;
}
bool secondary_core_run_no_input_polling(void)
{
bool okay;
if (secondary_module == NULL)
if (!secondary_module)
{
okay = secondary_core_create();
bool okay = secondary_core_create();
if (!okay)
{
return false;
}
}
secondary_core.retro_run();
return true;
@ -390,10 +389,9 @@ bool secondary_core_run_no_input_polling(void)
bool secondary_core_deserialize(const void *buffer, int size)
{
bool okay;
if (secondary_module == NULL)
if (!secondary_module)
{
okay = secondary_core_create();
bool okay = secondary_core_create();
if (!okay)
{
secondary_core_destroy();
@ -405,18 +403,14 @@ bool secondary_core_deserialize(const void *buffer, int size)
void secondary_core_destroy(void)
{
if (secondary_module != NULL)
if (secondary_module)
{
/* unload game from core */
if (secondary_core.retro_unload_game != NULL)
{
if (secondary_core.retro_unload_game)
secondary_core.retro_unload_game();
}
/* deinit */
if (secondary_core.retro_deinit != NULL)
{
if (secondary_core.retro_deinit)
secondary_core.retro_deinit();
}
memset(&secondary_core, 0, sizeof(struct retro_core_t));
dylib_close(secondary_module);
@ -429,44 +423,34 @@ void secondary_core_destroy(void)
void remember_controller_port_device(long port, long device)
{
if (port >= 0 && port < 16)
{
port_map[port] = device;
}
if (secondary_module != NULL && secondary_core.retro_set_controller_port_device != NULL)
{
if (secondary_module && secondary_core.retro_set_controller_port_device)
secondary_core.retro_set_controller_port_device(port, device);
}
}
void clear_controller_port_map(void)
{
int port;
unsigned port;
for (port = 0; port < 16; port++)
{
port_map[port] = -1;
}
}
static void free_file(FILE **file_p)
{
bool result;
if (file_p == NULL)
{
bool result = false;
if (!file_p || !*file_p)
return;
}
if (*file_p == NULL)
{
return;
}
result = fclose(*file_p) != 0;
result = fclose(*file_p) != 0;
*file_p = NULL;
return;
}
#else
#include "boolean.h"
#include "core.h"
#include <boolean.h>
#include "../core.h"
bool secondary_core_run_no_input_polling(void)
{
return false;

View File

@ -1,18 +1,19 @@
#ifndef __SECONDARY_CORE_H__
#define __SECONDARY_CORE_H__
#include "core_type.h"
#include "retro_common_api.h"
#include "boolean.h"
#include <retro_common_api.h>
#include <boolean.h>
#include "../core_type.h"
RETRO_BEGIN_DECLS
bool secondary_core_run_no_input_polling();
bool secondary_core_run_no_input_polling(void);
bool secondary_core_deserialize(const void *buffer, int size);
void secondary_core_destroy();
void secondary_core_destroy(void);
void set_last_core_type(enum rarch_core_type type);
void remember_controller_port_device(long port, long device);
void clear_controller_port_map();
void clear_controller_port_map(void);
RETRO_END_DECLS