From 40d4391788c6ba2be9d10ac4b4986040d92823a7 Mon Sep 17 00:00:00 2001 From: Maschell Date: Sat, 22 Feb 2020 23:24:10 +0100 Subject: [PATCH] - Move lib from $(DESTDIR)$(DEVKITPRO)/wut/ to $(DESTDIR)$(DEVKITPRO)/wut/usr - Remove fs dependencies - Replace CMutex with std::recursive_mutex - Remove the AsyncDeleter (dependency) - memoryInitialize was renamed to libgui_memoryInialized and added to this lib (same for memoryRelease) --- .gitignore | 1 + Makefile | 8 +- include/gui/GuiElement.h | 3 +- include/gui/GuiImageAsync.h | 4 +- include/gui/GuiImageData.h | 4 +- include/gui/GuiSound.h | 4 +- {source/system => include/gui}/memory.h | 4 +- include/gui/sounds/SoundDecoder.hpp | 6 +- include/gui/system/AsyncDeleter.h | 76 ---------- include/gui/system/CMutex.h | 69 --------- include/gui/system/CThread.h | 35 +++-- libgui.layout | 169 ---------------------- source/fs/CFile.cpp | 175 ++++++++++++++++++++++ source/fs/DirList.h | 103 ------------- source/fs/FSUtils.h | 16 -- source/gui/GuiImageAsync.cpp | 37 ++++- source/gui/GuiImageData.cpp | 2 +- source/system/memory.c | 185 ++++++++++++++++++++++++ source/video/CVideo.cpp | 3 +- 19 files changed, 432 insertions(+), 472 deletions(-) rename {source/system => include/gui}/memory.h (94%) delete mode 100644 include/gui/system/AsyncDeleter.h delete mode 100644 include/gui/system/CMutex.h delete mode 100644 libgui.layout create mode 100644 source/fs/CFile.cpp delete mode 100644 source/fs/DirList.h delete mode 100644 source/fs/FSUtils.h create mode 100644 source/system/memory.c diff --git a/.gitignore b/.gitignore index 5d5c112..ff51bd5 100644 --- a/.gitignore +++ b/.gitignore @@ -4,3 +4,4 @@ libgui.cbp lib/ *.bz2 libgui.layout +obj/ diff --git a/Makefile b/Makefile index 0741e5a..db555a8 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ include $(DEVKITPRO)/wut/share/wut_rules export VER_MAJOR := 1 export VER_MINOR := 0 -export VER_PATCH := 0 +export VER_PATCH := 1 VERSION := $(VER_MAJOR).$(VER_MINOR).$(VER_PATCH) @@ -26,8 +26,10 @@ VERSION := $(VER_MAJOR).$(VER_MINOR).$(VER_PATCH) TARGET := $(notdir $(CURDIR)) BUILD := build SOURCES := source \ + source/fs \ source/gui \ source/sounds \ + source/system \ source/video \ source/video/shaders \ @@ -115,8 +117,8 @@ dist-src: dist: dist-src dist-bin install: dist-bin - mkdir -p $(DESTDIR)$(DEVKITPRO)/wut - bzip2 -cd libgui-$(VERSION).tar.bz2 | tar -xf - -C $(DESTDIR)$(DEVKITPRO)/wut + mkdir -p $(DESTDIR)$(DEVKITPRO)/wut/usr + bzip2 -cd libgui-$(VERSION).tar.bz2 | tar -xf - -C $(DESTDIR)$(DEVKITPRO)/wut/usr lib: @[ -d $@ ] || mkdir -p $@ diff --git a/include/gui/GuiElement.h b/include/gui/GuiElement.h index c819ba6..bb983b2 100644 --- a/include/gui/GuiElement.h +++ b/include/gui/GuiElement.h @@ -33,7 +33,6 @@ #include #include #include -#include enum { @@ -68,7 +67,7 @@ class GuiController; class CVideo; //!Primary GUI class. Most other classes inherit from this class. -class GuiElement : public AsyncDeleter::Element { +class GuiElement { public: //!Constructor GuiElement(); diff --git a/include/gui/GuiImageAsync.h b/include/gui/GuiImageAsync.h index 12ea8a0..d583664 100644 --- a/include/gui/GuiImageAsync.h +++ b/include/gui/GuiImageAsync.h @@ -17,10 +17,10 @@ #ifndef _GUIIMAGEASYNC_H_ #define _GUIIMAGEASYNC_H_ +#include #include #include #include -#include class GuiImageAsync : public GuiImage { public: @@ -51,7 +51,7 @@ private: static std::vector imageQueue; static CThread *pThread; - static CMutex * pMutex; + static std::recursive_mutex * pMutex; static uint32_t threadRefCounter; static GuiImageAsync * pInUse; static bool bExitRequested; diff --git a/include/gui/GuiImageData.h b/include/gui/GuiImageData.h index 9e00ba8..7f8b658 100644 --- a/include/gui/GuiImageData.h +++ b/include/gui/GuiImageData.h @@ -18,11 +18,11 @@ #define GUI_IMAGEDATA_H_ #include -#include +#include #include #include -class GuiImageData : public AsyncDeleter::Element { +class GuiImageData : public GuiElement { public: //!Constructor GuiImageData(); diff --git a/include/gui/GuiSound.h b/include/gui/GuiSound.h index 283bf6b..b34655e 100644 --- a/include/gui/GuiSound.h +++ b/include/gui/GuiSound.h @@ -17,10 +17,10 @@ #ifndef GUI_SOUND_H_ #define GUI_SOUND_H_ -#include +#include //!Sound conversion and playback. A wrapper for other sound libraries - ASND, libmad, ltremor, etc -class GuiSound : public AsyncDeleter::Element { +class GuiSound : public GuiElement { public: //!Constructor //!\param sound Pointer to the sound data diff --git a/source/system/memory.h b/include/gui/memory.h similarity index 94% rename from source/system/memory.h rename to include/gui/memory.h index 3a1f551..d679b24 100644 --- a/source/system/memory.h +++ b/include/gui/memory.h @@ -23,8 +23,8 @@ extern "C" { #include -void memoryInitialize(void); -void memoryRelease(void); +void libgui_memoryInitialize(void); +void libgui_memoryRelease(void); void * MEM2_alloc(uint32_t size, uint32_t align); void MEM2_free(void *ptr); diff --git a/include/gui/sounds/SoundDecoder.hpp b/include/gui/sounds/SoundDecoder.hpp index f51a961..2548230 100644 --- a/include/gui/sounds/SoundDecoder.hpp +++ b/include/gui/sounds/SoundDecoder.hpp @@ -27,7 +27,7 @@ #define SOUND_DECODER_HPP #include -#include +#include #include class CFile; @@ -49,7 +49,7 @@ public: return CurPos; } virtual int32_t Seek(int32_t pos); - + virtual int32_t Rewind(); virtual uint16_t GetFormat() { return Format; @@ -131,7 +131,7 @@ protected: uint16_t SampleRate; uint8_t *ResampleBuffer; uint32_t ResampleRatio; - CMutex mutex; + std::recursive_mutex mutex; }; diff --git a/include/gui/system/AsyncDeleter.h b/include/gui/system/AsyncDeleter.h deleted file mode 100644 index fc5dfea..0000000 --- a/include/gui/system/AsyncDeleter.h +++ /dev/null @@ -1,76 +0,0 @@ -/**************************************************************************** - * Copyright (C) 2015 Dimok - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - ****************************************************************************/ -#ifndef _ASYNC_DELETER_H -#define _ASYNC_DELETER_H - -#include -#include "CThread.h" -#include "CMutex.h" - -class AsyncDeleter : public CThread { -public: - static void destroyInstance() { - if(deleterInstance != NULL) { - delete deleterInstance; - deleterInstance = NULL; - } - } - - class Element { - public: - Element() {} - virtual ~Element() {} - }; - - static void pushForDelete(AsyncDeleter::Element *e) { - if(!deleterInstance) { - deleterInstance = new AsyncDeleter(); - } - deleterInstance->deleteElements.push(e); - } - - static BOOL deleteListEmpty() { - if(!deleterInstance) { - return true; - } - return deleterInstance->deleteElements.empty(); - } - - static BOOL realListEmpty() { - if(!deleterInstance) { - return true; - } - return deleterInstance->realDeleteElements.empty(); - } - - static void triggerDeleteProcess(void); - -private: - AsyncDeleter(); - virtual ~AsyncDeleter(); - - static AsyncDeleter *deleterInstance; - - void executeThread(void); - - BOOL exitApplication; - std::queue deleteElements; - std::queue realDeleteElements; - CMutex deleteMutex; -}; - -#endif // _ASYNC_DELETER_H diff --git a/include/gui/system/CMutex.h b/include/gui/system/CMutex.h deleted file mode 100644 index 76f23ac..0000000 --- a/include/gui/system/CMutex.h +++ /dev/null @@ -1,69 +0,0 @@ -/**************************************************************************** - * Copyright (C) 2015 Dimok - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . - ****************************************************************************/ -#ifndef _CMUTEX_H_ -#define _CMUTEX_H_ - -#include -#include - -class CMutex -{ -public: - CMutex() { - pMutex = (OSMutex*) malloc(sizeof(OSMutex)); - if(!pMutex) - return; - - OSInitMutex(pMutex); - } - virtual ~CMutex() { - if(pMutex) - free(pMutex); - } - - void lock(void) { - if(pMutex) - OSLockMutex(pMutex); - } - void unlock(void) { - if(pMutex) - OSUnlockMutex(pMutex); - } - BOOL tryLock(void) { - if(!pMutex) - return false; - - return (OSTryLockMutex(pMutex) != 0); - } -private: - OSMutex *pMutex; -}; - -class CMutexLock -{ -public: - CMutexLock() { - mutex.lock(); - } - virtual ~CMutexLock() { - mutex.unlock(); - } -private: - CMutex mutex; -}; - -#endif // _CMUTEX_H_ diff --git a/include/gui/system/CThread.h b/include/gui/system/CThread.h index 21a1700..4d5acc1 100644 --- a/include/gui/system/CThread.h +++ b/include/gui/system/CThread.h @@ -17,10 +17,8 @@ #ifndef CTHREAD_H_ #define CTHREAD_H_ - #include #include -#include #include class CThread { @@ -64,30 +62,37 @@ public: } //! Suspend thread virtual void suspendThread(void) { - if(isThreadSuspended()) return; - if(pThread) OSSuspendThread(pThread); + if(isThreadSuspended()) + return; + if(pThread) + OSSuspendThread(pThread); } //! Resume thread virtual void resumeThread(void) { - if(!isThreadSuspended()) return; - if(pThread) OSResumeThread(pThread); + if(!isThreadSuspended()) + return; + if(pThread) + OSResumeThread(pThread); } //! Set thread priority - virtual void setThreadPriority(int prio) { - if(pThread) OSSetThreadPriority(pThread, prio); + virtual void setThreadPriority(int32_t prio) { + if(pThread) + OSSetThreadPriority(pThread, prio); } //! Check if thread is suspended - virtual BOOL isThreadSuspended(void) const { - if(pThread) return OSIsThreadSuspended(pThread); + virtual bool isThreadSuspended(void) const { + if(pThread) + return OSIsThreadSuspended(pThread); return false; } //! Check if thread is terminated - virtual BOOL isThreadTerminated(void) const { - if(pThread) return OSIsThreadTerminated(pThread); + virtual bool isThreadTerminated(void) const { + if(pThread) + return OSIsThreadTerminated(pThread); return false; } //! Check if thread is running - virtual BOOL isThreadRunning(void) const { + virtual bool isThreadRunning(void) const { return !isThreadSuspended() && !isThreadRunning(); } //! Shutdown thread @@ -118,12 +123,12 @@ public: eAttributePinnedAff = 0x10 }; private: - static int threadCallback(int argc, const char **argv) { + static int32_t threadCallback(int32_t argc, const char **argv) { //! After call to start() continue with the internal function ((CThread *) argv)->executeThread(); return 0; } - int iAttributes; + int32_t iAttributes; OSThread *pThread; uint8_t *pThreadStack; Callback pCallback; diff --git a/libgui.layout b/libgui.layout deleted file mode 100644 index 2522cbd..0000000 --- a/libgui.layout +++ /dev/null @@ -1,169 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/source/fs/CFile.cpp b/source/fs/CFile.cpp new file mode 100644 index 0000000..c2a7ae6 --- /dev/null +++ b/source/fs/CFile.cpp @@ -0,0 +1,175 @@ + +#include +#include +#include +#include +#include + +CFile::CFile() { + iFd = -1; + mem_file = NULL; + filesize = 0; + pos = 0; +} + +CFile::CFile(const std::string & filepath, eOpenTypes mode) { + iFd = -1; + this->open(filepath, mode); +} + +CFile::CFile(const uint8_t * mem, int32_t size) { + iFd = -1; + this->open(mem, size); +} + +CFile::~CFile() { + this->close(); +} + +int32_t CFile::open(const std::string & filepath, eOpenTypes mode) { + this->close(); + int32_t openMode = 0; + + // This depend on the devoptab implementation. + // see https://github.com/devkitPro/wut/blob/master/libraries/wutdevoptab/devoptab_fs_open.c#L21 fpr reference + + switch(mode) { + default: + case ReadOnly: // file must exist + openMode = O_RDONLY; + break; + case WriteOnly: // file will be created / zerod + openMode = O_TRUNC | O_CREAT | O_WRONLY; + break; + case ReadWrite: // file must exist + openMode = O_RDWR; + break; + case Append: // append to file, file will be created if missing. write only + openMode = O_CREAT | O_APPEND | O_WRONLY; + break; + } + + //! Using fopen works only on the first launch as expected + //! on the second launch it causes issues because we don't overwrite + //! the .data sections which is needed for a normal application to re-init + //! this will be added with launching as RPX + iFd = ::open(filepath.c_str(), openMode); + if(iFd < 0) + return iFd; + + + filesize = ::lseek(iFd, 0, SEEK_END); + ::lseek(iFd, 0, SEEK_SET); + + return 0; +} + +int32_t CFile::open(const uint8_t * mem, int32_t size) { + this->close(); + + mem_file = mem; + filesize = size; + + return 0; +} + +void CFile::close() { + if(iFd >= 0) + ::close(iFd); + + iFd = -1; + mem_file = NULL; + filesize = 0; + pos = 0; +} + +int32_t CFile::read(uint8_t * ptr, size_t size) { + if(iFd >= 0) { + int32_t ret = ::read(iFd, ptr,size); + if(ret > 0) + pos += ret; + return ret; + } + + int32_t readsize = size; + + if(readsize > (int64_t) (filesize-pos)) + readsize = filesize-pos; + + if(readsize <= 0) + return readsize; + + if(mem_file != NULL) { + memcpy(ptr, mem_file+pos, readsize); + pos += readsize; + return readsize; + } + + return -1; +} + +int32_t CFile::write(const uint8_t * ptr, size_t size) { + if(iFd >= 0) { + size_t done = 0; + while(done < size) { + int32_t ret = ::write(iFd, ptr, size - done); + if(ret <= 0) + return ret; + + ptr += ret; + done += ret; + pos += ret; + } + return done; + } + + return -1; +} + +int32_t CFile::seek(long int offset, int32_t origin) { + int32_t ret = 0; + int64_t newPos = pos; + + if(origin == SEEK_SET) { + newPos = offset; + } else if(origin == SEEK_CUR) { + newPos += offset; + } else if(origin == SEEK_END) { + newPos = filesize+offset; + } + + if(newPos < 0) { + pos = 0; + } else { + pos = newPos; + } + + if(iFd >= 0) + ret = ::lseek(iFd, pos, SEEK_SET); + + if(mem_file != NULL) { + if(pos > filesize) { + pos = filesize; + } + } + + return ret; +} + +int32_t CFile::fwrite(const char *format, ...) { + char tmp[512]; + tmp[0] = 0; + int32_t result = -1; + + va_list va; + va_start(va, format); + if((vsprintf(tmp, format, va) >= 0)) { + result = this->write((uint8_t *)tmp, strlen(tmp)); + } + va_end(va); + + + return result; +} + + diff --git a/source/fs/DirList.h b/source/fs/DirList.h deleted file mode 100644 index 64072df..0000000 --- a/source/fs/DirList.h +++ /dev/null @@ -1,103 +0,0 @@ -/**************************************************************************** - * Copyright (C) 2010 - * by Dimok - * - * This software is provided 'as-is', without any express or implied - * warranty. In no event will the authors be held liable for any - * damages arising from the use of this software. - * - * Permission is granted to anyone to use this software for any - * purpose, including commercial applications, and to alter it and - * redistribute it freely, subject to the following restrictions: - * - * 1. The origin of this software must not be misrepresented; you - * must not claim that you wrote the original software. If you use - * this software in a product, an acknowledgment in the product - * documentation would be appreciated but is not required. - * - * 2. Altered source versions must be plainly marked as such, and - * must not be misrepresented as being the original software. - * - * 3. This notice may not be removed or altered from any source - * distribution. - * - * DirList Class - * for WiiXplorer 2010 - ***************************************************************************/ -#ifndef ___DIRLIST_H_ -#define ___DIRLIST_H_ - -#include -#include -#include - -typedef struct { - char * FilePath; - BOOL isDir; -} DirEntry; - -class DirList { -public: - //!Constructor - DirList(void); - //!\param path Path from where to load the filelist of all files - //!\param filter A fileext that needs to be filtered - //!\param flags search/filter flags from the enum - DirList(const std::string & path, const char *filter = NULL, uint32_t flags = Files | Dirs, uint32_t maxDepth = 0xffffffff); - //!Destructor - virtual ~DirList(); - //! Load all the files from a directory - BOOL LoadPath(const std::string & path, const char *filter = NULL, uint32_t flags = Files | Dirs, uint32_t maxDepth = 0xffffffff); - //! Get a filename of the list - //!\param list index - const char * GetFilename(int32_t index) const; - //! Get the a filepath of the list - //!\param list index - const char *GetFilepath(int32_t index) const { - if (!valid(index)) return ""; - else return FileInfo[index].FilePath; - } - //! Get the a filesize of the list - //!\param list index - uint64_t GetFilesize(int32_t index) const; - //! Is index a dir or a file - //!\param list index - BOOL IsDir(int32_t index) const { - if(!valid(index)) return false; - return FileInfo[index].isDir; - }; - //! Get the filecount of the whole list - int32_t GetFilecount() const { - return FileInfo.size(); - }; - //! Sort list by filepath - void SortList(); - //! Custom sort command for custom sort functions definitions - void SortList(BOOL (*SortFunc)(const DirEntry &a, const DirEntry &b)); - //! Get the index of the specified filename - int32_t GetFileIndex(const char *filename) const; - //! Enum for search/filter flags - enum { - Files = 0x01, - Dirs = 0x02, - CheckSubfolders = 0x08, - }; -protected: - // Internal parser - BOOL InternalLoadPath(std::string &path); - //!Add a list entrie - void AddEntrie(const std::string &filepath, const char * filename, BOOL isDir); - //! Clear the list - void ClearList(); - //! Check if valid pos is requested - inline BOOL valid(uint32_t pos) const { - return (pos < FileInfo.size()); - }; - - uint32_t Flags; - uint32_t Depth; - const char *Filter; - std::vector FileInfo; -}; - -#endif diff --git a/source/fs/FSUtils.h b/source/fs/FSUtils.h deleted file mode 100644 index b589a44..0000000 --- a/source/fs/FSUtils.h +++ /dev/null @@ -1,16 +0,0 @@ -#ifndef __FS_UTILS_H_ -#define __FS_UTILS_H_ - -#include - -class FSUtils { -public: - static int32_t LoadFileToMem(const char *filepath, uint8_t **inbuffer, uint32_t *size); - - //! todo: C++ class - static int32_t CreateSubfolder(const char * fullpath); - static int32_t CheckFile(const char * filepath); - static BOOL saveBufferToFile(const char * path, void * buffer, uint32_t size); -}; - -#endif // __FS_UTILS_H_ diff --git a/source/gui/GuiImageAsync.cpp b/source/gui/GuiImageAsync.cpp index 865043d..c9eb0e6 100644 --- a/source/gui/GuiImageAsync.cpp +++ b/source/gui/GuiImageAsync.cpp @@ -16,11 +16,11 @@ ****************************************************************************/ #include #include -#include "fs/FSUtils.h" +#include "../fs/CFile.hpp" std::vector GuiImageAsync::imageQueue; CThread * GuiImageAsync::pThread = NULL; -CMutex * GuiImageAsync::pMutex = NULL; +std::recursive_mutex * GuiImageAsync::pMutex = NULL; uint32_t GuiImageAsync::threadRefCounter = 0; bool GuiImageAsync::bExitRequested = false; GuiImageAsync * GuiImageAsync::pInUse = NULL; @@ -98,10 +98,35 @@ void GuiImageAsync::guiImageAsyncThread(CThread *thread, void *arg) { pInUse->imgData = new GuiImageData(pInUse->imgBuffer, pInUse->imgBufferSize); } else { uint8_t *buffer = NULL; - uint32_t bufferSize = 0; + uint64_t bufferSize = 0; - int32_t iResult = FSUtils::LoadFileToMem(pInUse->filename.c_str(), &buffer, &bufferSize); - if(iResult > 0) { + CFile file(pInUse->filename, CFile::ReadOnly); + if(file.isOpen()) { + uint64_t filesize = file.size(); + buffer = (uint8_t *) malloc(filesize); + if (buffer != NULL) { + uint32_t blocksize = 0x4000; + uint32_t done = 0; + int32_t readBytes = 0; + while(done < filesize) { + if(done + blocksize > filesize) { + blocksize = filesize - done; + } + readBytes = file.read(buffer + done, blocksize); + if(readBytes <= 0) + break; + done += readBytes; + } + if(done == filesize){ + bufferSize = filesize; + }else{ + free(buffer); + } + } + file.close(); + } + + if(buffer != NULL && bufferSize > 0) { pInUse->imgData = new GuiImageData(buffer, bufferSize, GX2_TEX_CLAMP_MODE_MIRROR); //! free original image buffer which is converted to texture now and not needed anymore @@ -128,7 +153,7 @@ void GuiImageAsync::guiImageAsyncThread(CThread *thread, void *arg) { void GuiImageAsync::threadInit() { if (pThread == NULL) { bExitRequested = false; - pMutex = new CMutex(); + pMutex = new std::recursive_mutex(); pThread = CThread::create(GuiImageAsync::guiImageAsyncThread, NULL, CThread::eAttributeAffCore1 | CThread::eAttributePinnedAff, 10); pThread->resumeThread(); } diff --git a/source/gui/GuiImageData.cpp b/source/gui/GuiImageData.cpp index 51b8f9b..b540691 100644 --- a/source/gui/GuiImageData.cpp +++ b/source/gui/GuiImageData.cpp @@ -18,7 +18,7 @@ #include #include #include -#include +#include /** * Constructor for the GuiImageData class. */ diff --git a/source/system/memory.c b/source/system/memory.c new file mode 100644 index 0000000..77929e0 --- /dev/null +++ b/source/system/memory.c @@ -0,0 +1,185 @@ +/**************************************************************************** + * Copyright (C) 2015 Dimok + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + ****************************************************************************/ +#include +#include +#include +#include +#include +#include "memory.h" + +#define MEMORY_ARENA_1 0 +#define MEMORY_ARENA_2 1 +#define MEMORY_ARENA_3 2 +#define MEMORY_ARENA_4 3 +#define MEMORY_ARENA_5 4 +#define MEMORY_ARENA_6 5 +#define MEMORY_ARENA_7 6 +#define MEMORY_ARENA_8 7 +#define MEMORY_ARENA_FG_BUCKET 8 + +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +//! Memory functions +//! This is the only place where those are needed so lets keep them more or less private +//!---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +static MEMHeapHandle mem1_heap = NULL; +static MEMHeapHandle bucket_heap = NULL; + +void libgui_memoryInitialize(void) { + if(!mem1_heap) { + MEMHeapHandle mem1_heap_handle = MEMGetBaseHeapHandle(MEMORY_ARENA_1); + uint32_t mem1_allocatable_size = MEMGetAllocatableSizeForFrmHeapEx(mem1_heap_handle, 4); + void *mem1_memory = MEMAllocFromFrmHeapEx(mem1_heap_handle, mem1_allocatable_size, 4); + if(mem1_memory) + mem1_heap = MEMCreateExpHeapEx(mem1_memory, mem1_allocatable_size, 0); + } + + if(!bucket_heap) { + MEMHeapHandle bucket_heap_handle = MEMGetBaseHeapHandle(MEMORY_ARENA_FG_BUCKET); + uint32_t bucket_allocatable_size = MEMGetAllocatableSizeForFrmHeapEx(bucket_heap_handle, 4); + void *bucket_memory = MEMAllocFromFrmHeapEx(bucket_heap_handle, bucket_allocatable_size, 4); + if(bucket_memory) + bucket_heap = MEMCreateExpHeapEx(bucket_memory, bucket_allocatable_size, 0); + } +} + +void libgui_memoryRelease(void) { + if(mem1_heap) { + MEMDestroyExpHeap(mem1_heap); + MEMFreeToFrmHeap(MEMGetBaseHeapHandle(MEMORY_ARENA_1), 3); + mem1_heap = NULL; + } + if(bucket_heap) { + MEMDestroyExpHeap(bucket_heap); + MEMFreeToFrmHeap(MEMGetBaseHeapHandle(MEMORY_ARENA_FG_BUCKET), 3); + bucket_heap = NULL; + } +} +/* +//!------------------------------------------------------------------------------------------- +//! wraps +//!------------------------------------------------------------------------------------------- +void *__wrap_malloc(size_t size) +{ + // pointer to a function resolve + return ((void * (*)(size_t))(*pMEMAllocFromDefaultHeap))(size); +} + +void *__wrap_memalign(size_t align, size_t size) +{ + if (align < 4) + align = 4; + + // pointer to a function resolve + return ((void * (*)(size_t, size_t))(*pMEMAllocFromDefaultHeapEx))(size, align); +} + +void __wrap_free(void *p) +{ + // pointer to a function resolve + if(p != 0) + ((void (*)(void *))(*pMEMFreeToDefaultHeap))(p); +} + +void *__wrap_calloc(size_t n, size_t size) +{ + void *p = __wrap_malloc(n * size); + if (p != 0) { + memset(p, 0, n * size); + } + return p; +} + +size_t __wrap_malloc_usable_size(void *p) +{ + //! TODO: this is totally wrong and needs to be addressed + return 0x7FFFFFFF; +} + +void *__wrap_realloc(void *p, size_t size) +{ + void *new_ptr = __wrap_malloc(size); + if (new_ptr != 0) + { + memcpy(new_ptr, p, __wrap_malloc_usable_size(p) < size ? __wrap_malloc_usable_size(p) : size); + __wrap_free(p); + } + return new_ptr; +} + +//!------------------------------------------------------------------------------------------- +//! reent versions +//!------------------------------------------------------------------------------------------- +void *__wrap__malloc_r(struct _reent *r, size_t size) +{ + return __wrap_malloc(size); +} + +void *__wrap__calloc_r(struct _reent *r, size_t n, size_t size) +{ + return __wrap_calloc(n, size); +} + +void *__wrap__memalign_r(struct _reent *r, size_t align, size_t size) +{ + return __wrap_memalign(align, size); +} + +void __wrap__free_r(struct _reent *r, void *p) +{ + __wrap_free(p); +} + +size_t __wrap__malloc_usable_size_r(struct _reent *r, void *p) +{ + return __wrap_malloc_usable_size(p); +} + +void *__wrap__realloc_r(struct _reent *r, void *p, size_t size) +{ + return __wrap_realloc(p, size); +} +*/ +//!------------------------------------------------------------------------------------------- +//! some wrappers +//!------------------------------------------------------------------------------------------- +void * MEM2_alloc(uint32_t size, uint32_t align) { + return memalign(align, size); +} + +void MEM2_free(void *ptr) { + free(ptr); +} + +void * MEM1_alloc(uint32_t size, uint32_t align) { + if (align < 4) + align = 4; + return MEMAllocFromExpHeapEx(mem1_heap, size, align); +} + +void MEM1_free(void *ptr) { + MEMFreeToExpHeap(mem1_heap, ptr); +} + +void * MEMBucket_alloc(uint32_t size, uint32_t align) { + if (align < 4) + align = 4; + return MEMAllocFromExpHeapEx(bucket_heap, size, align); +} + +void MEMBucket_free(void *ptr) { + MEMFreeToExpHeap(bucket_heap, ptr); +} diff --git a/source/video/CVideo.cpp b/source/video/CVideo.cpp index 75ae646..e639698 100644 --- a/source/video/CVideo.cpp +++ b/source/video/CVideo.cpp @@ -17,7 +17,7 @@ #include #include #include -#include "system/memory.h" +#include #include #include #include @@ -239,6 +239,7 @@ CVideo::~CVideo() { ColorShader::destroyInstance(); FXAAShader::destroyInstance(); Shader3D::destroyInstance(); + ShaderFractalColor::destroyInstance(); Texture2DShader::destroyInstance(); }