Skip to content

Commit 689c9b3

Browse files
committed
fixing memory leaks, begenning xmake support to build on windows
1 parent ecd43de commit 689c9b3

14 files changed

+1306
-17
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
# By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ #
77
# +#+#+#+#+#+ +#+ #
88
# Created: 2022/10/04 16:43:41 by maldavid #+# #+# #
9-
# Updated: 2023/11/24 10:03:17 by maldavid ### ########.fr #
9+
# Updated: 2023/12/07 15:25:52 by kbz_8 ### ########.fr #
1010
# #
1111
# **************************************************************************** #
1212

@@ -17,7 +17,7 @@ SRCS += $(wildcard $(addsuffix /*.cpp, ./src/platform))
1717
SRCS += $(wildcard $(addsuffix /*.cpp, ./src/renderer))
1818
SRCS += $(wildcard $(addsuffix /*.cpp, ./src/renderer/**))
1919

20-
OBJ_DIR = objs
20+
OBJ_DIR = objs/makefile
2121
OBJS = $(addprefix $(OBJ_DIR)/, $(SRCS:.cpp=.o))
2222

2323
OS = $(shell uname -s)

src/core/bridge.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2022/10/04 17:35:20 by maldavid #+# #+# */
9-
/* Updated: 2023/11/25 10:12:36 by maldavid ### ########.fr */
9+
/* Updated: 2023/12/07 23:05:05 by kbz_8 ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -26,8 +26,8 @@ extern "C"
2626
mlx::core::error::report(e_kind::error, "MLX cannot be initialized multiple times");
2727
return NULL;
2828
}
29-
mlx::Render_Core::get().init();
3029
mlx::core::Application* app = new mlx::core::Application;
30+
mlx::Render_Core::get().init();
3131
if(app == nullptr)
3232
mlx::core::error::report(e_kind::fatal_error, "Tout a pété");
3333
init = true;

src/core/memory.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* memory.cpp :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: kbz_8 </var/spool/mail/kbz_8> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2023/12/07 16:32:01 by kbz_8 #+# #+# */
9+
/* Updated: 2023/12/07 16:43:56 by kbz_8 ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include <core/memory.h>
14+
#include <core/errors.h>
15+
#include <algorithm>
16+
#include <stdlib.h>
17+
18+
namespace mlx
19+
{
20+
void* MemManager::alloc(std::size_t size)
21+
{
22+
void* ptr = std::malloc(size);
23+
if(ptr != nullptr)
24+
_blocks.push_back(ptr);
25+
return ptr;
26+
}
27+
28+
void MemManager::free(void* ptr)
29+
{
30+
auto it = std::find(_blocks.begin(), _blocks.end(), ptr);
31+
if(it == _blocks.end())
32+
{
33+
core::error::report(e_kind::error, "Memory Manager : trying to free a pointer not allocated by the memory manager");
34+
return;
35+
}
36+
std::free(*it);
37+
_blocks.erase(it);
38+
}
39+
40+
MemManager::~MemManager()
41+
{
42+
std::for_each(_blocks.begin(), _blocks.end(), [](void* ptr)
43+
{
44+
std::free(ptr);
45+
});
46+
}
47+
}

src/core/memory.h

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* memory.h :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: kbz_8 </var/spool/mail/kbz_8> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2023/12/07 16:31:51 by kbz_8 #+# #+# */
9+
/* Updated: 2023/12/07 16:37:15 by kbz_8 ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef __MLX_MEMORY__
14+
#define __MLX_MEMORY__
15+
16+
#include <utils/singleton.h>
17+
#include <list>
18+
19+
namespace mlx
20+
{
21+
class MemManager : public Singleton<MemManager>
22+
{
23+
friend class Singleton<MemManager>;
24+
25+
public:
26+
void* alloc(std::size_t size);
27+
void free(void* ptr);
28+
29+
private:
30+
MemManager() = default;
31+
~MemManager();
32+
33+
private:
34+
std::list<void*> _blocks;
35+
};
36+
}
37+
38+
#endif

src/renderer/descriptors/vk_descriptor_set.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/23 18:40:44 by maldavid #+# #+# */
9-
/* Updated: 2023/11/12 01:14:52 by maldavid ### ########.fr */
9+
/* Updated: 2023/12/07 20:00:13 by kbz_8 ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

src/renderer/descriptors/vk_descriptor_set.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/01/23 18:39:36 by maldavid #+# #+# */
9-
/* Updated: 2023/03/31 17:28:36 by maldavid ### ########.fr */
9+
/* Updated: 2023/12/07 19:47:07 by kbz_8 ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

src/renderer/images/texture_atlas.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/04/07 16:36:33 by maldavid #+# #+# */
9-
/* Updated: 2023/11/14 05:36:30 by maldavid ### ########.fr */
9+
/* Updated: 2023/12/07 18:50:53 by kbz_8 ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -28,7 +28,7 @@ namespace mlx
2828
void render(class Renderer& renderer, int x, int y, uint32_t ibo_size);
2929
void destroy() noexcept override;
3030

31-
inline void setDescriptor(DescriptorSet set) noexcept { _set = std::move(set); }
31+
inline void setDescriptor(DescriptorSet&& set) noexcept { _set = set; }
3232
inline VkDescriptorSet getSet() noexcept { return _set.isInit() ? _set.get() : VK_NULL_HANDLE; }
3333
inline void updateSet(int binding) noexcept { _set.writeDescriptor(binding, getImageView(), getSampler()); }
3434

src/renderer/text_pipeline.cpp

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/* By: maldavid <kbz_8.dev@akel-engine.com> +#+ +:+ +#+ */
77
/* +#+#+#+#+#+ +#+ */
88
/* Created: 2023/04/06 16:41:13 by maldavid #+# #+# */
9-
/* Updated: 2023/11/25 10:40:39 by maldavid ### ########.fr */
9+
/* Updated: 2023/12/07 22:29:45 by kbz_8 ### ########.fr */
1010
/* */
1111
/* ************************************************************************** */
1212

@@ -20,7 +20,11 @@
2020
#define STB_RECT_PACK_IMPLEMENTATION
2121
#include <stb_rect_pack.h>
2222

23+
#include <core/memory.h>
24+
2325
#define STB_TRUETYPE_IMPLEMENTATION
26+
#define STB_malloc(x, u) ((void)(u), MemManager::get().alloc(x))
27+
#define STB_free(x, u) ((void)(u), MemManager::get().free(x))
2428
#include <stb_truetype.h>
2529

2630
constexpr const int RANGE = 1024;
@@ -74,10 +78,10 @@ namespace mlx
7478
void TextPutPipeline::init(Renderer* renderer) noexcept
7579
{
7680
_renderer = renderer;
77-
uint8_t tmp_bitmap[RANGE * RANGE];
78-
uint8_t vulkan_bitmap[RANGE * RANGE * 4];
81+
std::vector<uint8_t> tmp_bitmap(RANGE * RANGE);
82+
std::vector<uint8_t> vulkan_bitmap(RANGE * RANGE * 4);
7983
stbtt_pack_context pc;
80-
stbtt_PackBegin(&pc, tmp_bitmap, RANGE, RANGE, RANGE, 1, nullptr);
84+
stbtt_PackBegin(&pc, tmp_bitmap.data(), RANGE, RANGE, RANGE, 1, nullptr);
8185
stbtt_PackFontRange(&pc, dogica_ttf, 0, 6.0, 32, 96, _cdata.data());
8286
stbtt_PackEnd(&pc);
8387
for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4)
@@ -87,14 +91,14 @@ namespace mlx
8791
vulkan_bitmap[j + 2] = tmp_bitmap[i];
8892
vulkan_bitmap[j + 3] = tmp_bitmap[i];
8993
}
90-
_atlas.create(vulkan_bitmap, RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_texts_pipeline_texture_atlas", true);
94+
_atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_texts_pipeline_texture_atlas", true);
9195
_atlas.setDescriptor(renderer->getFragDescriptorSet().duplicate());
9296
}
9397

9498
void TextPutPipeline::loadFont(const std::filesystem::path& filepath, float scale)
9599
{
96-
uint8_t tmp_bitmap[RANGE * RANGE];
97-
uint8_t vulkan_bitmap[RANGE * RANGE * 4];
100+
std::vector<uint8_t> tmp_bitmap(RANGE * RANGE);
101+
std::vector<uint8_t> vulkan_bitmap(RANGE * RANGE * 4);
98102

99103
std::ifstream file(filepath, std::ios::binary);
100104
if(!file.is_open())
@@ -109,7 +113,7 @@ namespace mlx
109113
file.close();
110114

111115
stbtt_pack_context pc;
112-
stbtt_PackBegin(&pc, tmp_bitmap, RANGE, RANGE, RANGE, 1, nullptr);
116+
stbtt_PackBegin(&pc, tmp_bitmap.data(), RANGE, RANGE, RANGE, 1, nullptr);
113117
stbtt_PackFontRange(&pc, bytes.data(), 0, scale, 32, 96, _cdata.data());
114118
stbtt_PackEnd(&pc);
115119
for(int i = 0, j = 0; i < RANGE * RANGE; i++, j += 4)
@@ -120,7 +124,7 @@ namespace mlx
120124
vulkan_bitmap[j + 3] = tmp_bitmap[i];
121125
}
122126
destroy();
123-
_atlas.create(vulkan_bitmap, RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_texts_pipeline_texture_atlas", true);
127+
_atlas.create(vulkan_bitmap.data(), RANGE, RANGE, VK_FORMAT_R8G8B8A8_UNORM, "__mlx_texts_pipeline_texture_atlas", true);
124128
_atlas.setDescriptor(_renderer->getFragDescriptorSet().duplicate());
125129
}
126130

0 commit comments

Comments
 (0)