From 7fa536895dd10e9a06a801240ce10ec201602b22 Mon Sep 17 00:00:00 2001 From: TheoryOfNekomata Date: Mon, 13 Feb 2023 09:55:29 +0800 Subject: [PATCH] Unify memory handling functions Use abstraction instead of using SDL dependencies. --- CMakeLists.txt | 10 +++++----- TODO.md | 2 +- src/packages/asset-inv/main.c | 2 +- src/packages/config/IZ_config.h | 2 +- src/packages/game/IZ_app.h | 2 +- src/packages/game/IZ_app_config.h | 2 +- src/packages/game/IZ_app_net.c | 4 ++-- src/packages/game/IZ_app_net.h | 1 + src/packages/game/data/IZ_list.c | 4 ++-- src/packages/game/data/IZ_list.h | 2 +- src/packages/game/input/IZ_joystick.c | 4 ++-- src/packages/game/input/IZ_joystick.h | 4 ++-- src/packages/game/input/IZ_keyboard.c | 2 +- src/packages/game/input/IZ_keyboard.h | 4 ++-- src/packages/game/input/IZ_midi.c | 4 ++-- src/packages/game/input/IZ_midi.h | 4 ++-- src/packages/game/input/input.test.c | 2 +- src/packages/game/memory/IZ_pool.c | 4 ++-- src/packages/game/memory/IZ_pool.h | 4 ++-- src/packages/game/output/video/IZ_video.h | 2 +- src/packages/log/IZ_intercept.h | 2 +- src/packages/midi/IZ_midi.h | 2 +- src/packages/net/IZ_net_client.h | 2 +- src/packages/net/IZ_net_server.h | 2 +- src/packages/net/core/IZ_websocket.h | 2 +- src/packages/net/svc/IZ_wsclient.h | 2 +- src/packages/net/svc/IZ_wsserver.h | 2 +- src/packages/server/IZ_app.h | 2 +- src/packages/server/IZ_app_config.h | 2 +- src/packages/server/db/IZ_repo.h | 2 +- src/packages/stdinc/IZ_stdlib.c | 13 +++++++++++++ src/packages/stdinc/IZ_stdlib.h | 10 ++++++++++ src/packages/{string => stdinc}/IZ_string.c | 0 src/packages/{string => stdinc}/IZ_string.h | 0 34 files changed, 66 insertions(+), 42 deletions(-) create mode 100644 src/packages/stdinc/IZ_stdlib.c create mode 100644 src/packages/stdinc/IZ_stdlib.h rename src/packages/{string => stdinc}/IZ_string.c (100%) rename src/packages/{string => stdinc}/IZ_string.h (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index d45e443..3e62e2d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -149,11 +149,11 @@ add_executable( src/packages/game/IZ_app_config.h src/packages/game/asset/IZ_asset.c src/packages/game/asset/IZ_asset.h - src/packages/string/IZ_string.c - src/packages/string/IZ_string.h + src/packages/stdinc/IZ_string.c + src/packages/stdinc/IZ_string.h src/packages/io/IZ_io.c src/packages/io/IZ_io.h - src/packages/log/IZ_log.c src/packages/log/IZ_log.h src/packages/timer/IZ_timer.c src/packages/timer/IZ_timer.h src/packages/compat/IZ_windows.h) + src/packages/log/IZ_log.c src/packages/log/IZ_log.h src/packages/timer/IZ_timer.c src/packages/timer/IZ_timer.h src/packages/compat/IZ_windows.h src/packages/stdinc/IZ_stdlib.c src/packages/stdinc/IZ_stdlib.h) if (WIN32) target_link_libraries( @@ -284,8 +284,8 @@ add_executable( src/packages/server/db/IZ_repo.c src/packages/server/db/IZ_repo.h src/packages/server/IZ_subsystem.h - src/packages/string/IZ_string.c - src/packages/string/IZ_string.h + src/packages/stdinc/IZ_string.c + src/packages/stdinc/IZ_string.h src/packages/io/IZ_io.c src/packages/io/IZ_io.h src/packages/log/IZ_log.c diff --git a/TODO.md b/TODO.md index 755941a..50d3f17 100644 --- a/TODO.md +++ b/TODO.md @@ -1,5 +1,5 @@ - [X] Improve logging (remove SDL/lws dependency) -- [ ] Unify memset/memcpy/free/malloc functions (remove SDL/lws dependency) +- [X] Unify memset/memcpy/free/malloc functions (remove SDL/lws dependency) - [ ] Fix gamepad mapping - [ ] Provide default mapping - [ ] Allow customization of button mappings diff --git a/src/packages/asset-inv/main.c b/src/packages/asset-inv/main.c index 24170c4..ab4d890 100644 --- a/src/packages/asset-inv/main.c +++ b/src/packages/asset-inv/main.c @@ -1,7 +1,7 @@ #include #include "minIni.h" #include "../common/IZ_common.h" -#include "../string/IZ_string.h" +#include "../stdinc/IZ_string.h" #ifdef IZ_WIN64 #include diff --git a/src/packages/config/IZ_config.h b/src/packages/config/IZ_config.h index 81afa81..07a4f76 100644 --- a/src/packages/config/IZ_config.h +++ b/src/packages/config/IZ_config.h @@ -6,7 +6,7 @@ #include #include "../common/IZ_common.h" #include "../log/IZ_log.h" -#include "../string/IZ_string.h" +#include "../stdinc/IZ_string.h" typedef enum { IZ_CONFIG_TYPE_VOID, diff --git a/src/packages/game/IZ_app.h b/src/packages/game/IZ_app.h index 6fe1251..9b738b2 100644 --- a/src/packages/game/IZ_app.h +++ b/src/packages/game/IZ_app.h @@ -11,7 +11,7 @@ #include "IZ_app_video.h" #include "../log/IZ_log.h" #include "../log/IZ_intercept.h" -#include "../string/IZ_string.h" +#include "../stdinc/IZ_string.h" #include "../timer/IZ_timer.h" typedef enum { diff --git a/src/packages/game/IZ_app_config.h b/src/packages/game/IZ_app_config.h index aac4b59..eb0f4df 100644 --- a/src/packages/game/IZ_app_config.h +++ b/src/packages/game/IZ_app_config.h @@ -4,6 +4,6 @@ #include #include "IZ_subsystem.h" #include "../config/IZ_config.h" -#include "../string/IZ_string.h" +#include "../stdinc/IZ_string.h" #endif diff --git a/src/packages/game/IZ_app_net.c b/src/packages/game/IZ_app_net.c index b7bec97..53e88ec 100644 --- a/src/packages/game/IZ_app_net.c +++ b/src/packages/game/IZ_app_net.c @@ -23,7 +23,7 @@ void IZ_AppHandleOutboundNetworking(struct IZ_App* app) { for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) { if (net_state->action[player_index] != input_state->action[player_index]) { u8 player_actions_count = 1; - IZ_AppActionSyncMessage* msg = SDL_malloc( + IZ_AppActionSyncMessage* msg = IZ_malloc( sizeof(IZ_AppMessageHeader) + sizeof(u8) + (sizeof(IZ_AppPlayerActionSyncMessage) * player_actions_count) @@ -39,7 +39,7 @@ void IZ_AppHandleOutboundNetworking(struct IZ_App* app) { msg, sizeof(*msg) ); - SDL_free(msg); + IZ_free(msg); } net_state->action[player_index] = input_state->action[player_index]; } diff --git a/src/packages/game/IZ_app_net.h b/src/packages/game/IZ_app_net.h index bfe4cce..750045e 100644 --- a/src/packages/game/IZ_app_net.h +++ b/src/packages/game/IZ_app_net.h @@ -2,6 +2,7 @@ #define IZ_APP_NET_H #include "IZ_subsystem.h" +#include "../stdinc/IZ_stdlib.h" typedef enum { IZ_MESSAGE_KIND_ACTION_SYNC = 0, diff --git a/src/packages/game/data/IZ_list.c b/src/packages/game/data/IZ_list.c index e22247a..6fd4118 100644 --- a/src/packages/game/data/IZ_list.c +++ b/src/packages/game/data/IZ_list.c @@ -32,7 +32,7 @@ void IZ_ListTeardown(IZ_List* list) { * @return Pointer to the newly created node. */ IZ_ListNode** IZ_ListAppendNode(IZ_List* list, void* node_value) { - IZ_ListNode* new_node = SDL_malloc(sizeof(IZ_ListNode)); + IZ_ListNode* new_node = IZ_malloc(sizeof(IZ_ListNode)); new_node->value = node_value; new_node->next = NULL; list->length += 1; @@ -83,7 +83,7 @@ void IZ_ListDeleteFirstNode(IZ_List* list, IZ_ListFindPredicate filter) { // if there is only one item in the list, root, gets set to NULL } - SDL_free(*list->iterator); + IZ_free(*list->iterator); list->iterator = NULL; if (list->length > 0) { // avoid underflow diff --git a/src/packages/game/data/IZ_list.h b/src/packages/game/data/IZ_list.h index 8bdf714..df7c50b 100644 --- a/src/packages/game/data/IZ_list.h +++ b/src/packages/game/data/IZ_list.h @@ -2,7 +2,7 @@ #define IZ_LIST_H #include "../../common/IZ_common.h" -#include "SDL_stdinc.h" +#include "../../stdinc/IZ_stdlib.h" /** * A node in a linked list. diff --git a/src/packages/game/input/IZ_joystick.c b/src/packages/game/input/IZ_joystick.c index 00587a3..5f308b8 100644 --- a/src/packages/game/input/IZ_joystick.c +++ b/src/packages/game/input/IZ_joystick.c @@ -200,7 +200,7 @@ void IZ_JoystickInitializeConfigItems(IZ_ConfigItem config_items[]) { char* main_section_name; char* control_mapping_section_name; for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) { - main_section_name = SDL_malloc(sizeof(char) * 64); + main_section_name = IZ_calloc(64, sizeof(char)); sprintf(main_section_name, "Joystick.%d", player_index); u8 base_index = (player_index * (IZ_CONTROLS - 4 + 2)); config_items[base_index] = (IZ_ConfigItem) { @@ -238,7 +238,7 @@ void IZ_JoystickInitializeConfigItems(IZ_ConfigItem config_items[]) { // todo add game controller GUID for determining mappings - control_mapping_section_name = SDL_malloc(sizeof(char) * 64); + control_mapping_section_name = IZ_calloc(64, sizeof(char)); sprintf(control_mapping_section_name, "Joystick.%d.ControlMapping", player_index); for (control_index = 4; control_index < IZ_CONTROLS; control_index += 1) { config_items[base_index + 3 + (control_index - 4)] = (IZ_ConfigItem) { diff --git a/src/packages/game/input/IZ_joystick.h b/src/packages/game/input/IZ_joystick.h index 5439848..ca23003 100644 --- a/src/packages/game/input/IZ_joystick.h +++ b/src/packages/game/input/IZ_joystick.h @@ -1,13 +1,13 @@ #ifndef IZ_JOYSTICK_H #define IZ_JOYSTICK_H -#include #include #include #include #include #include "../../config/IZ_config.h" -#include "../../string/IZ_string.h" +#include "../../stdinc/IZ_string.h" +#include "../../stdinc/IZ_stdlib.h" #include "IZ_action.h" typedef u8 IZ_PadButton; diff --git a/src/packages/game/input/IZ_keyboard.c b/src/packages/game/input/IZ_keyboard.c index b17e675..dcca2cf 100644 --- a/src/packages/game/input/IZ_keyboard.c +++ b/src/packages/game/input/IZ_keyboard.c @@ -54,7 +54,7 @@ void IZ_KeyboardInitializeConfigItems(IZ_ConfigItem config_items[]) { u8 control_index; char* control_mapping_section_name; for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) { - control_mapping_section_name = SDL_malloc(sizeof(char) * 64); + control_mapping_section_name = IZ_calloc(64, sizeof(char)); sprintf(control_mapping_section_name, "Keyboard.%d.ControlMapping", player_index); for (control_index = 0; control_index < IZ_CONTROLS; control_index += 1) { config_items[player_index * IZ_CONTROLS + control_index] = (IZ_ConfigItem) { diff --git a/src/packages/game/input/IZ_keyboard.h b/src/packages/game/input/IZ_keyboard.h index b0b1727..2ecbf2a 100644 --- a/src/packages/game/input/IZ_keyboard.h +++ b/src/packages/game/input/IZ_keyboard.h @@ -1,12 +1,12 @@ #ifndef IZ_KEYBOARD_H #define IZ_KEYBOARD_H -#include #include #include #include #include "../../config/IZ_config.h" -#include "../../string/IZ_string.h" +#include "../../stdinc/IZ_string.h" +#include "../../stdinc/IZ_stdlib.h" #include "IZ_action.h" typedef struct { diff --git a/src/packages/game/input/IZ_midi.c b/src/packages/game/input/IZ_midi.c index 05cd0cf..4850a48 100644 --- a/src/packages/game/input/IZ_midi.c +++ b/src/packages/game/input/IZ_midi.c @@ -76,7 +76,7 @@ void IZ_MIDIInputInitializeConfigItems(IZ_ConfigItem config_items[]) { char* main_section_name; char* control_mapping_section_name; for (player_index = 0; player_index < IZ_PLAYERS; player_index += 1) { - main_section_name = SDL_malloc(sizeof(char) * 64); + main_section_name = IZ_calloc(64, sizeof(char)); sprintf(main_section_name, "MIDIInput.%d", player_index); u16 base_index = (player_index * (IZ_CONTROLS + 2)); @@ -110,7 +110,7 @@ void IZ_MIDIInputInitializeConfigItems(IZ_ConfigItem config_items[]) { NULL, }; - control_mapping_section_name = SDL_malloc(sizeof(char) * 64); + control_mapping_section_name = IZ_calloc(64, sizeof(char)); sprintf(control_mapping_section_name, "MIDIInput.%d.ControlMapping", player_index); for (control_index = 0; control_index < IZ_CONTROLS; control_index += 1) { config_items[base_index + 2 + control_index] = (IZ_ConfigItem) { diff --git a/src/packages/game/input/IZ_midi.h b/src/packages/game/input/IZ_midi.h index f34c96c..ccfddfb 100644 --- a/src/packages/game/input/IZ_midi.h +++ b/src/packages/game/input/IZ_midi.h @@ -1,7 +1,6 @@ #ifndef IZ_MIDI_H #define IZ_MIDI_H -#include #include #ifndef PORTMIDI_INCLUDED @@ -12,7 +11,8 @@ #include #include "../../config/IZ_config.h" #include "../../midi/IZ_midi.h" -#include "../../string/IZ_string.h" +#include "../../stdinc/IZ_string.h" +#include "../../stdinc/IZ_stdlib.h" #include "IZ_action.h" #define MIDI_EVENT_BUFFER_SIZE 1024 diff --git a/src/packages/game/input/input.test.c b/src/packages/game/input/input.test.c index f7cea48..73f37a0 100644 --- a/src/packages/game/input/input.test.c +++ b/src/packages/game/input/input.test.c @@ -3,7 +3,7 @@ #include "../../../__mocks__/SDL_stdinc.mock.h" #include "../../../__mocks__/minIni.mock.h" #include "../../../__mocks__/portmidi.mock.h" -//#include "../../string/IZ_string.h" +//#include "../../stdinc/IZ_string.h" #include "IZ_keyboard.h" #include "IZ_joystick.h" #include "IZ_midi.h" diff --git a/src/packages/game/memory/IZ_pool.c b/src/packages/game/memory/IZ_pool.c index 13618d9..8246793 100644 --- a/src/packages/game/memory/IZ_pool.c +++ b/src/packages/game/memory/IZ_pool.c @@ -3,7 +3,7 @@ void IZ_PoolInitialize(IZ_Pool* pool, size_t size) { IZ_LogInfo(IZ_LOG_CATEGORY_GLOBAL, "Setting up memory pools..."); IZ_ListInitialize(&pool->items); - pool->memory = SDL_malloc(size); + pool->memory = IZ_malloc(size); IZ_memset(pool->memory, 0, size); pool->allocated_memory = 0; pool->next_address = 0; @@ -48,6 +48,6 @@ void IZ_PoolDeallocate(IZ_PoolItem* item) { void IZ_PoolTeardown(IZ_Pool* pool) { IZ_LogInfo(IZ_LOG_CATEGORY_GLOBAL, "Shutting down memory pools..."); - SDL_free(pool->memory); + IZ_free(pool->memory); pool->memory = NULL; } diff --git a/src/packages/game/memory/IZ_pool.h b/src/packages/game/memory/IZ_pool.h index 86fb2e0..7e5dedb 100644 --- a/src/packages/game/memory/IZ_pool.h +++ b/src/packages/game/memory/IZ_pool.h @@ -1,10 +1,10 @@ #ifndef IZ_POOL_H #define IZ_POOL_H -#include #include "../../common/IZ_common.h" #include "../../log/IZ_log.h" -#include "../../string/IZ_string.h" +#include "../../stdinc/IZ_string.h" +#include "../../stdinc/IZ_stdlib.h" #include "../data/IZ_list.h" #define POOL_MAX_SIZE (1llu << 26) // 64MB diff --git a/src/packages/game/output/video/IZ_video.h b/src/packages/game/output/video/IZ_video.h index 221c786..eaa3a7b 100644 --- a/src/packages/game/output/video/IZ_video.h +++ b/src/packages/game/output/video/IZ_video.h @@ -10,7 +10,7 @@ #include "../../../config/IZ_config.h" #include "../../../common/IZ_common.h" #include "../../../io/IZ_io.h" -#include "../../../string/IZ_string.h" +#include "../../../stdinc/IZ_string.h" #include "../../geometry/IZ_vector2d.h" #include "../../input/IZ_input.h" diff --git a/src/packages/log/IZ_intercept.h b/src/packages/log/IZ_intercept.h index 511cd72..2f89501 100644 --- a/src/packages/log/IZ_intercept.h +++ b/src/packages/log/IZ_intercept.h @@ -4,7 +4,7 @@ #include #include "libwebsockets.h" #include "../common/IZ_common.h" -#include "../string/IZ_string.h" +#include "../stdinc/IZ_string.h" #include "IZ_log.h" void IZ_LogInterceptWSMessages(i32); diff --git a/src/packages/midi/IZ_midi.h b/src/packages/midi/IZ_midi.h index a528944..6e87a19 100644 --- a/src/packages/midi/IZ_midi.h +++ b/src/packages/midi/IZ_midi.h @@ -3,7 +3,7 @@ #include #include -#include "../string/IZ_string.h" +#include "../stdinc/IZ_string.h" typedef unsigned char IZ_MIDINote; diff --git a/src/packages/net/IZ_net_client.h b/src/packages/net/IZ_net_client.h index 7b06916..4f57fe9 100644 --- a/src/packages/net/IZ_net_client.h +++ b/src/packages/net/IZ_net_client.h @@ -6,7 +6,7 @@ #include "../config/IZ_config.h" #include "../common/IZ_common.h" #include "../game/input/IZ_action.h" -#include "../string/IZ_string.h" +#include "../stdinc/IZ_string.h" #include "svc/IZ_wsclient.h" typedef enum { diff --git a/src/packages/net/IZ_net_server.h b/src/packages/net/IZ_net_server.h index c545c9c..f8f8cd6 100644 --- a/src/packages/net/IZ_net_server.h +++ b/src/packages/net/IZ_net_server.h @@ -4,7 +4,7 @@ #include #include "../config/IZ_config.h" #include "../common/IZ_common.h" -#include "../string/IZ_string.h" +#include "../stdinc/IZ_string.h" #include "core/IZ_websocket.h" #include "svc/IZ_wsserver.h" diff --git a/src/packages/net/core/IZ_websocket.h b/src/packages/net/core/IZ_websocket.h index d4271f1..047ef55 100644 --- a/src/packages/net/core/IZ_websocket.h +++ b/src/packages/net/core/IZ_websocket.h @@ -3,7 +3,7 @@ #include "libwebsockets.h" #include "../../common/IZ_common.h" -#include "../../string/IZ_string.h" +#include "../../stdinc/IZ_string.h" #define NETWORK_PROTOCOL "izanagi-networking" #define RING_COUNT 32 diff --git a/src/packages/net/svc/IZ_wsclient.h b/src/packages/net/svc/IZ_wsclient.h index fa9b841..37d4641 100644 --- a/src/packages/net/svc/IZ_wsclient.h +++ b/src/packages/net/svc/IZ_wsclient.h @@ -2,7 +2,7 @@ #define IZ_WSCLIENT_H #include "../../common/IZ_common.h" -#include "../../string/IZ_string.h" +#include "../../stdinc/IZ_string.h" #include "../core/IZ_websocket.h" typedef struct { diff --git a/src/packages/net/svc/IZ_wsserver.h b/src/packages/net/svc/IZ_wsserver.h index 9906869..ac2dbc4 100644 --- a/src/packages/net/svc/IZ_wsserver.h +++ b/src/packages/net/svc/IZ_wsserver.h @@ -5,7 +5,7 @@ #include #include "../../common/IZ_common.h" #include "../../io/IZ_io.h" -#include "../../string/IZ_string.h" +#include "../../stdinc/IZ_string.h" #include "../core/IZ_websocket.h" #ifndef S_ISDIR diff --git a/src/packages/server/IZ_app.h b/src/packages/server/IZ_app.h index 1057e45..3b183ce 100644 --- a/src/packages/server/IZ_app.h +++ b/src/packages/server/IZ_app.h @@ -6,7 +6,7 @@ #include "../common/IZ_common.h" #include "../net/IZ_net_server.h" #include "../log/IZ_intercept.h" -#include "../string/IZ_string.h" +#include "../stdinc/IZ_string.h" #include "db/IZ_repo.h" #include "IZ_app_config.h" diff --git a/src/packages/server/IZ_app_config.h b/src/packages/server/IZ_app_config.h index 87d3799..080afb5 100644 --- a/src/packages/server/IZ_app_config.h +++ b/src/packages/server/IZ_app_config.h @@ -3,7 +3,7 @@ #include #include "../config/IZ_config.h" -#include "../string/IZ_string.h" +#include "../stdinc/IZ_string.h" #include "IZ_subsystem.h" #endif diff --git a/src/packages/server/db/IZ_repo.h b/src/packages/server/db/IZ_repo.h index 9876af6..c1cfb10 100644 --- a/src/packages/server/db/IZ_repo.h +++ b/src/packages/server/db/IZ_repo.h @@ -6,7 +6,7 @@ #include #include "../../common/IZ_common.h" #include "../../config/IZ_config.h" -#include "../../string/IZ_string.h" +#include "../../stdinc/IZ_string.h" typedef struct { char path[64]; diff --git a/src/packages/stdinc/IZ_stdlib.c b/src/packages/stdinc/IZ_stdlib.c new file mode 100644 index 0000000..eb93679 --- /dev/null +++ b/src/packages/stdinc/IZ_stdlib.c @@ -0,0 +1,13 @@ +#include "IZ_stdlib.h" + +void* IZ_malloc(size_t size) { + return malloc(size); +} + +void* IZ_calloc(unsigned int count, size_t size) { + return calloc(count, size); +} + +void IZ_free(void* p) { + free(p); +} diff --git a/src/packages/stdinc/IZ_stdlib.h b/src/packages/stdinc/IZ_stdlib.h new file mode 100644 index 0000000..9e5c3a3 --- /dev/null +++ b/src/packages/stdinc/IZ_stdlib.h @@ -0,0 +1,10 @@ +#ifndef IZ_STDLIB_H +#define IZ_STDLIB_H + +#include + +void* IZ_malloc(size_t); +void* IZ_calloc(unsigned int, size_t); +void IZ_free(void*); + +#endif //IZ_STDLIB_H diff --git a/src/packages/string/IZ_string.c b/src/packages/stdinc/IZ_string.c similarity index 100% rename from src/packages/string/IZ_string.c rename to src/packages/stdinc/IZ_string.c diff --git a/src/packages/string/IZ_string.h b/src/packages/stdinc/IZ_string.h similarity index 100% rename from src/packages/string/IZ_string.h rename to src/packages/stdinc/IZ_string.h