From b5ce2347f6642d242aeddf5ac677ec4089a605db Mon Sep 17 00:00:00 2001 From: Kbz-8 Date: Mon, 11 Dec 2023 19:30:37 +0100 Subject: [PATCH] improving custom malloc, removing unused code in input system --- src/core/application.cpp | 4 +++- src/core/bridge.cpp | 8 +++++--- src/core/memory.cpp | 23 +++++++++++++++++++++-- src/core/memory.h | 10 ++++++---- src/platform/inputs.cpp | 12 +----------- src/platform/inputs.h | 11 ++--------- src/renderer/text_pipeline.cpp | 6 +++--- 7 files changed, 41 insertions(+), 33 deletions(-) diff --git a/src/core/application.cpp b/src/core/application.cpp index a7c3749..2136d81 100644 --- a/src/core/application.cpp +++ b/src/core/application.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 22:10:52 by maldavid #+# #+# */ -/* Updated: 2023/12/09 17:44:13 by kbz_8 ### ########.fr */ +/* Updated: 2023/12/11 15:12:39 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,11 +16,13 @@ #include #include #include +#include namespace mlx::core { Application::Application() : _in(std::make_unique()) { + SDL_SetMemoryFunctions(MemManager::malloc, MemManager::calloc, MemManager::realloc, MemManager::free); if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS | SDL_INIT_TIMER) != 0) error::report(e_kind::fatal_error, "SDL error : unable to init all subsystems : %s", SDL_GetError()); } diff --git a/src/core/bridge.cpp b/src/core/bridge.cpp index 854d9f5..af8ff8b 100644 --- a/src/core/bridge.cpp +++ b/src/core/bridge.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */ -/* Updated: 2023/12/07 23:05:05 by kbz_8 ### ########.fr */ +/* Updated: 2023/12/11 15:56:18 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,6 +16,7 @@ #include #include #include +#include extern "C" { @@ -25,8 +26,9 @@ extern "C" if(init) { mlx::core::error::report(e_kind::error, "MLX cannot be initialized multiple times"); - return NULL; + return NULL; // not nullptr for the C compatibility } + mlx::MemManager::get(); // just to initialize the C garbage collector mlx::core::Application* app = new mlx::core::Application; mlx::Render_Core::get().init(); if(app == nullptr) @@ -219,4 +221,4 @@ extern "C" static_cast(mlx)->getScreenSize(w, h); return 0; } -} \ No newline at end of file +} diff --git a/src/core/memory.cpp b/src/core/memory.cpp index 3fd1c5b..8edf9a3 100644 --- a/src/core/memory.cpp +++ b/src/core/memory.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/07 16:32:01 by kbz_8 #+# #+# */ -/* Updated: 2023/12/08 12:56:14 by kbz_8 ### ########.fr */ +/* Updated: 2023/12/11 15:25:02 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -17,7 +17,7 @@ namespace mlx { - void* MemManager::alloc(std::size_t size) + void* MemManager::malloc(std::size_t size) { void* ptr = std::malloc(size); if(ptr != nullptr) @@ -25,6 +25,25 @@ namespace mlx return ptr; } + void* MemManager::calloc(std::size_t n, std::size_t size) + { + void* ptr = std::calloc(n, size); + if(ptr != nullptr) + _blocks.push_back(ptr); + return ptr; + } + + void* MemManager::realloc(void* ptr, std::size_t size) + { + void* ptr2 = std::realloc(ptr, size); + if(ptr2 != nullptr) + _blocks.push_back(ptr2); + auto it = std::find(_blocks.begin(), _blocks.end(), ptr); + if(it != _blocks.end()) + _blocks.erase(it); + return ptr2; + } + void MemManager::free(void* ptr) { auto it = std::find(_blocks.begin(), _blocks.end(), ptr); diff --git a/src/core/memory.h b/src/core/memory.h index b4649e1..e669a07 100644 --- a/src/core/memory.h +++ b/src/core/memory.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/07 16:31:51 by kbz_8 #+# #+# */ -/* Updated: 2023/12/08 19:05:15 by kbz_8 ### ########.fr */ +/* Updated: 2023/12/11 15:13:46 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,15 +24,17 @@ namespace mlx friend class Singleton; public: - void* alloc(std::size_t size); - void free(void* ptr); + static void* malloc(std::size_t size); + static void* calloc(std::size_t n, std::size_t size); + static void* realloc(void* ptr, std::size_t size); + static void free(void* ptr); private: MemManager() = default; ~MemManager(); private: - std::list _blocks; + inline static std::list _blocks; }; } diff --git a/src/platform/inputs.cpp b/src/platform/inputs.cpp index 7dce064..bb369db 100644 --- a/src/platform/inputs.cpp +++ b/src/platform/inputs.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/05 16:30:19 by maldavid #+# #+# */ -/* Updated: 2023/12/08 12:17:40 by kbz_8 ### ########.fr */ +/* Updated: 2023/12/11 19:01:14 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -16,12 +16,6 @@ namespace mlx { - Input::Input() - { - std::memset(_keys.data(), 0, SDL_NUM_SCANCODES); - std::memset(_mouse.data(), 0, 8); - } - void Input::update() { _xRel = 0; @@ -47,7 +41,6 @@ namespace mlx { case SDL_KEYDOWN: { - _keys[_event.key.keysym.scancode] = static_cast(action::down); if(hooks[MLX_KEYDOWN].hook) hooks[MLX_KEYDOWN].hook(_event.key.keysym.scancode, hooks[MLX_KEYDOWN].param); break; @@ -55,7 +48,6 @@ namespace mlx case SDL_KEYUP: { - _keys[_event.key.keysym.scancode] = static_cast(action::up); if(hooks[MLX_KEYUP].hook) hooks[MLX_KEYUP].hook(_event.key.keysym.scancode, hooks[MLX_KEYUP].param); break; @@ -63,7 +55,6 @@ namespace mlx case SDL_MOUSEBUTTONDOWN: { - _mouse[_event.button.button] = static_cast(action::down); if(hooks[MLX_MOUSEDOWN].hook) hooks[MLX_MOUSEDOWN].hook(_event.button.button, hooks[MLX_MOUSEDOWN].param); break; @@ -71,7 +62,6 @@ namespace mlx case SDL_MOUSEBUTTONUP: { - _mouse[_event.button.button] = static_cast(action::up); if(hooks[MLX_MOUSEUP].hook) hooks[MLX_MOUSEUP].hook(_event.button.button, hooks[MLX_MOUSEUP].param); break; diff --git a/src/platform/inputs.h b/src/platform/inputs.h index 332d8e0..8ebb428 100644 --- a/src/platform/inputs.h +++ b/src/platform/inputs.h @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2022/10/05 16:27:35 by maldavid #+# #+# */ -/* Updated: 2023/12/08 18:54:03 by kbz_8 ### ########.fr */ +/* Updated: 2023/12/11 19:06:13 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -24,8 +24,6 @@ namespace mlx { - enum class action : uint8_t { up = (1 << 1), down = (1 << 2) }; - struct Hook { std::function hook; @@ -35,13 +33,10 @@ namespace mlx class Input { public: - Input(); + Input() = default; void update(); - inline bool getInKey(const SDL_Scancode key, action type = action::down) const noexcept { return _keys[key] & static_cast(type); } - - inline bool getInMouse(const uint8_t button, action type = action::down) const noexcept { return _mouse[button] & static_cast(type); } inline bool isMouseMoving() const noexcept { return _xRel || _yRel; } inline int getX() const noexcept { return _x; } @@ -68,11 +63,9 @@ namespace mlx ~Input() = default; private: - std::array _keys; std::unordered_map> _windows; std::unordered_map> _events_hooks; SDL_Event _event; - std::array _mouse; int _x = 0; int _y = 0; diff --git a/src/renderer/text_pipeline.cpp b/src/renderer/text_pipeline.cpp index eecc004..4468a0b 100644 --- a/src/renderer/text_pipeline.cpp +++ b/src/renderer/text_pipeline.cpp @@ -6,7 +6,7 @@ /* By: maldavid +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */ -/* Updated: 2023/12/10 22:34:35 by kbz_8 ### ########.fr */ +/* Updated: 2023/12/11 15:12:02 by kbz_8 ### ########.fr */ /* */ /* ************************************************************************** */ @@ -23,8 +23,8 @@ #include #define STB_TRUETYPE_IMPLEMENTATION -#define STB_malloc(x, u) ((void)(u), MemManager::get().alloc(x)) -#define STB_free(x, u) ((void)(u), MemManager::get().free(x)) +#define STB_malloc(x, u) ((void)(u), MemManager::malloc(x)) +#define STB_free(x, u) ((void)(u), MemManager::free(x)) #include constexpr const int RANGE = 1024;