diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6b19a62..111fca5 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -28,6 +28,16 @@ externalproject_add(rpl -DCMAKE_INSTALL_PREFIX:string= -DCMAKE_TOOLCHAIN_FILE:string=${CMAKE_SOURCE_DIR}/cmake/wut-toolchain.cmake) +externalproject_add(libdefaultheap + SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/libdefaultheap" + CMAKE_GENERATOR "Unix Makefiles" + INSTALL_DIR "${CMAKE_BINARY_DIR}/staging" + CMAKE_CACHE_ARGS + -DDEVKITPPC:string=${DEVKITPPC} + -DWUT_ROOT:string=${CMAKE_SOURCE_DIR} + -DCMAKE_INSTALL_PREFIX:string= + -DCMAKE_TOOLCHAIN_FILE:string=${CMAKE_SOURCE_DIR}/cmake/wut-toolchain.cmake) + externalproject_add(libgfd SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/libgfd" CMAKE_GENERATOR "Unix Makefiles" @@ -57,6 +67,12 @@ externalproject_add_step(rpl forcebuild DEPENDERS "build" ALWAYS 1) +externalproject_add_step(libdefaultheap forcebuild + COMMAND ${CMAKE_COMMAND} -E echo "Force build of libdefaultheap" + DEPENDEES "configure" + DEPENDERS "build" + ALWAYS 1) + externalproject_add_step(libgfd forcebuild COMMAND ${CMAKE_COMMAND} -E echo "Force build of libgfd" DEPENDEES "configure" diff --git a/src/libdefaultheap/CMakeLists.txt b/src/libdefaultheap/CMakeLists.txt new file mode 100644 index 0000000..3fb3a2c --- /dev/null +++ b/src/libdefaultheap/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.2) +project(libdefaultheap) + +file(GLOB_RECURSE SOURCE_FILES *.c) +file(GLOB_RECURSE HEADER_FILES *.h) + +add_library(defaultheap STATIC ${SOURCE_FILES} ${HEADER_FILES}) + +target_include_directories(defaultheap PRIVATE "../../include") +target_include_directories(defaultheap PUBLIC "include") + +install(TARGETS defaultheap + ARCHIVE DESTINATION "${CMAKE_INSTALL_PREFIX}/lib") +install(DIRECTORY ${PROJECT_SOURCE_DIR}/include/ + DESTINATION "${CMAKE_INSTALL_PREFIX}/include" + FILES_MATCHING PATTERN "*.h*") diff --git a/src/libdefaultheap/include/defaultheap.h b/src/libdefaultheap/include/defaultheap.h new file mode 100644 index 0000000..f4f0be1 --- /dev/null +++ b/src/libdefaultheap/include/defaultheap.h @@ -0,0 +1,20 @@ +#pragma once +#include + +#ifdef __cplusplus +extern "C" { +#endif + +void * +MEMAllocFromDefaultHeap(uint32_t size); + +void * +MEMAllocFromDefaultHeapEx(uint32_t size, + int32_t alignment); + +void +MEMFreeToDefaultHeap(void *block); + +#ifdef __cplusplus +} +#endif diff --git a/src/libdefaultheap/src/defaultheap.c b/src/libdefaultheap/src/defaultheap.c new file mode 100644 index 0000000..ef330d2 --- /dev/null +++ b/src/libdefaultheap/src/defaultheap.c @@ -0,0 +1,75 @@ +#include "defaultheap.h" +#include + +typedef void *(*MEMAllocFromDefaultHeapFn)(uint32_t size); +typedef void *(*MEMAllocFromDefaultHeapExFn)(uint32_t size, int32_t alignment); +typedef void (*MEMFreeToDefaultHeapFn)(void *block); + +static OSDynLoadModule sCoreinitHandle = 0; +static MEMAllocFromDefaultHeapFn *MEMAllocFromDefaultHeapPtr = NULL; +static MEMAllocFromDefaultHeapExFn *MEMAllocFromDefaultHeapExPtr = NULL; +static MEMFreeToDefaultHeapFn *MEMFreeToDefaultHeapPtr = NULL; + +void * +MEMAllocFromDefaultHeap(uint32_t size) +{ + if (!sCoreinitHandle) { + if (OSDynLoad_Acquire("coreinit.rpl", &sCoreinitHandle)) { + return NULL; + } + } + + if (!MEMAllocFromDefaultHeapPtr) { + if (OSDynLoad_FindExport(sCoreinitHandle, + TRUE, + "MEMAllocFromDefaultHeap", + (void **)&MEMAllocFromDefaultHeapPtr)) { + return NULL; + } + } + + return (**MEMAllocFromDefaultHeapPtr)(size); +} + +void * +MEMAllocFromDefaultHeapEx(uint32_t size, + int32_t alignment) +{ + if (!sCoreinitHandle) { + if (OSDynLoad_Acquire("coreinit.rpl", &sCoreinitHandle)) { + return NULL; + } + } + + if (!MEMAllocFromDefaultHeapExPtr) { + if (OSDynLoad_FindExport(sCoreinitHandle, + TRUE, + "MEMAllocFromDefaultHeapEx", + (void **)&MEMAllocFromDefaultHeapExPtr)) { + return NULL; + } + } + + return (**MEMAllocFromDefaultHeapExPtr)(size, alignment); +} + +void +MEMFreeToDefaultHeap(void *block) +{ + if (!sCoreinitHandle) { + if (OSDynLoad_Acquire("coreinit.rpl", &sCoreinitHandle)) { + return; + } + } + + if (!MEMFreeToDefaultHeapPtr) { + if (OSDynLoad_FindExport(sCoreinitHandle, + TRUE, + "MEMFreeToDefaultHeap", + (void **)&MEMFreeToDefaultHeapPtr)) { + return; + } + } + + (**MEMFreeToDefaultHeapPtr)(block); +}