Add libdefaultheap.

Gives you access to MEMAllocFromDefaultHeap{Ex} and MEMFreeToDefaultHeap.
This is a workaround until we can support data imports.
This commit is contained in:
James Benton 2017-05-10 17:27:00 +01:00
parent c9d177651e
commit ad35efc97c
4 changed files with 127 additions and 0 deletions

View File

@ -28,6 +28,16 @@ externalproject_add(rpl
-DCMAKE_INSTALL_PREFIX:string=<INSTALL_DIR> -DCMAKE_INSTALL_PREFIX:string=<INSTALL_DIR>
-DCMAKE_TOOLCHAIN_FILE:string=${CMAKE_SOURCE_DIR}/cmake/wut-toolchain.cmake) -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=<INSTALL_DIR>
-DCMAKE_TOOLCHAIN_FILE:string=${CMAKE_SOURCE_DIR}/cmake/wut-toolchain.cmake)
externalproject_add(libgfd externalproject_add(libgfd
SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/libgfd" SOURCE_DIR "${CMAKE_SOURCE_DIR}/src/libgfd"
CMAKE_GENERATOR "Unix Makefiles" CMAKE_GENERATOR "Unix Makefiles"
@ -57,6 +67,12 @@ externalproject_add_step(rpl forcebuild
DEPENDERS "build" DEPENDERS "build"
ALWAYS 1) 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 externalproject_add_step(libgfd forcebuild
COMMAND ${CMAKE_COMMAND} -E echo "Force build of libgfd" COMMAND ${CMAKE_COMMAND} -E echo "Force build of libgfd"
DEPENDEES "configure" DEPENDEES "configure"

View File

@ -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*")

View File

@ -0,0 +1,20 @@
#pragma once
#include <wut.h>
#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

View File

@ -0,0 +1,75 @@
#include "defaultheap.h"
#include <coreinit/dynload.h>
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);
}