diff --git a/toolbox/mm.cpp b/toolbox/mm.cpp index 239ba6a..abe9c8f 100644 --- a/toolbox/mm.cpp +++ b/toolbox/mm.cpp @@ -44,6 +44,7 @@ using ToolBox::Log; + namespace { mplite_t pool; @@ -58,28 +59,23 @@ namespace // map of ptr -> size std::map PtrMap; - struct HandleInfo - { - uint32_t address; - uint32_t size; - bool locked; - bool purgeable; - bool resource; - HandleInfo(uint32_t a = 0, uint32_t s = 0) : - address(a), size(s), locked(false), purgeable(false), resource(false) - {} - }; // map of handle -> size [? just use Ptr map?] - std::map HandleMap; + std::map HandleMap; - inline int16_t SetMemError(int16_t error) + inline MacOS::macos_error SetMemError(MacOS::macos_error error) { memoryWriteWord(error, MacOS::MemErr); return error; } + inline MacOS::macos_error SetMemError(int16_t error) + { + return SetMemError((MacOS::macos_error)error); + } + + bool alloc_handle_block() { const unsigned HandleCount = 128; // 512 bytes of handle blocks. @@ -584,6 +580,31 @@ namespace MM } + #pragma mark -- + + tool_return GetHandleSize(uint32_t handle) + { + + const auto iter = HandleMap.find(handle); + + if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr); + + SetMemError(0); + return iter->second.size; + } + + tool_return GetHandleInfo(uint32_t handle) + { + const auto iter = HandleMap.find(handle); + + if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr); + SetMemError(0); + return iter->second; + } + + + #pragma mark -- + uint16_t BlockMove(uint16_t trap) { diff --git a/toolbox/mm.h b/toolbox/mm.h index 6e2ad99..a286f88 100644 --- a/toolbox/mm.h +++ b/toolbox/mm.h @@ -3,6 +3,8 @@ #include +#include + namespace MM { // native functions. @@ -35,6 +37,26 @@ namespace MM bool Init(uint8_t *memory, uint32_t memorySize, uint32_t globals, uint32_t stack); + struct HandleInfo + { + uint32_t address = 0; + uint32_t size = 0; + bool locked = false; + bool purgeable = false; + bool resource = false; + + HandleInfo(uint32_t a = 0, uint32_t s = 0) : + address(a), size(s) + {} + }; + + using MacOS::tool_return; + + tool_return GetHandleInfo(uint32_t handle); + + tool_return GetHandleSize(uint32_t handle); + + uint16_t BlockMove(uint16_t trap); uint32_t CompactMem(uint16_t trap); uint32_t MaxMem(uint16_t trap);