From 94f4c1c5284592d412daef74d0f718eb0a4daea9 Mon Sep 17 00:00:00 2001 From: Kelvin Sherlock Date: Mon, 1 Aug 2016 13:56:43 -0400 Subject: [PATCH] update Native tool calls to use tool_return<> for return values. --- mpw/mpw.cpp | 38 +++---- toolbox/dispatch.cpp | 3 +- toolbox/loader.cpp | 73 +++++++++--- toolbox/loader.h | 1 + toolbox/mm.cpp | 265 +++++++++++++++++++------------------------ toolbox/mm.h | 77 ++++++++----- toolbox/rm.cpp | 58 ++++++---- toolbox/rm.h | 10 +- toolbox/utility.cpp | 25 ++-- 9 files changed, 293 insertions(+), 257 deletions(-) diff --git a/mpw/mpw.cpp b/mpw/mpw.cpp index a6f3a23..dc265fb 100644 --- a/mpw/mpw.cpp +++ b/mpw/mpw.cpp @@ -263,8 +263,6 @@ namespace MPW uint32_t devptr = 0; uint32_t fptr = 0; - uint16_t error; - // create the argv-data. { uint32_t size = 0; @@ -278,8 +276,9 @@ namespace MPW size += l; } - error = MM::Native::NewPtr(size, true, argvptr); - if (error) return error; + auto tmp = MM::Native::NewPtr(size, true); + if (tmp.error()) return tmp.error(); + argvptr = tmp.value(); uint8_t *xptr = memoryPointer(argvptr); @@ -331,9 +330,9 @@ namespace MPW size += 4; // space for null terminator. - error = MM::Native::NewPtr(size, true, envptr); - if (error) return error; - + auto tmp = MM::Native::NewPtr(size, true); + if (tmp.error()) return tmp.error(); + envptr = tmp.value(); uint8_t *xptr = memoryPointer(envptr); uint32_t offset = 0; @@ -364,9 +363,9 @@ namespace MPW // these are ftraps for emulated/native function ptrs. uint32_t size = 6 * 4; - error = MM::Native::NewPtr(size, true, fptr); - - if (error) return error; + auto tmp = MM::Native::NewPtr(size, true); + if (tmp.error()) return tmp.error(); + fptr = tmp.value(); memoryWriteWord(fQuit, fptr + 0); memoryWriteWord(0x4E75, fptr + 2); // rts @@ -393,9 +392,9 @@ namespace MPW { uint32_t size = 0x78; - error = MM::Native::NewPtr(size, true, devptr); - - if (error) return error; + auto tmp = MM::Native::NewPtr(size, true); + if (tmp.error()) return tmp.error(); + devptr = tmp.value(); memoryWriteLong(0x46535953, devptr + 0); // 'FSYS' memoryWriteLong(fptr + 4, devptr + 4); @@ -415,9 +414,9 @@ namespace MPW uint32_t size = 0x3c; uint32_t ptr; - error = MM::Native::NewPtr(size, true, ioptr); - - if (error) return error; + auto tmp = MM::Native::NewPtr(size, true); + if (tmp.error()) return tmp.error(); + ioptr = tmp.value(); ptr = ioptr; // stdin @@ -450,10 +449,9 @@ namespace MPW - uint32_t mpi = 0; - - error = MM::Native::NewPtr(8 + 0x30, true, mpi); - if (error) return error; + auto tmp = MM::Native::NewPtr(8 + 0x30, true); + if (tmp.error()) return tmp.error(); + uint32_t mpi = tmp.value(); MacProgramInfo = mpi + 8; diff --git a/toolbox/dispatch.cpp b/toolbox/dispatch.cpp index e2a2901..c522618 100644 --- a/toolbox/dispatch.cpp +++ b/toolbox/dispatch.cpp @@ -350,7 +350,8 @@ namespace ToolBox { // alternate entry code - MM::Native::NewPtr(1024 * 4 + 256 * 4, false, ToolGlue); + auto tmp = MM::Native::NewPtr(1024 * 4 + 256 * 4, false); + ToolGlue = tmp.value(); OSGlue = ToolGlue + 1024 * 4; uint16_t *code = (uint16_t *)memoryPointer(ToolGlue); diff --git a/toolbox/loader.cpp b/toolbox/loader.cpp index 32330f1..7f66d35 100644 --- a/toolbox/loader.cpp +++ b/toolbox/loader.cpp @@ -148,21 +148,24 @@ namespace Loader { // load code seg 0. uint16_t LoadCode0(Segment0Info &rv) { - uint16_t err; uint32_t rHandle; uint32_t size; uint32_t address; SegmentInfo si; - err = RM::Native::GetResource(kCODE, 0, rHandle); - if (err) return err; - + { + auto tmp = RM::Native::GetResource(kCODE, 0); + if (tmp.error()) return tmp.error(); + rHandle = tmp.value(); + } MM::Native::HLock(rHandle); - MM::Native::GetHandleSize(rHandle, size); - - address = memoryReadLong(rHandle); + { + auto tmp = MM::Native::GetHandleInfo(rHandle); + address = tmp.value().address; + size = tmp.value().size; + } uint32_t above = memoryReadLong(address); uint32_t below = memoryReadLong(address + 4); @@ -173,8 +176,12 @@ namespace Loader { // create a new handle for the a5 segment. - err = MM::Native::NewHandle(si.size, true, si.handle, si.address); - if (err) return err; + { + auto tmp = MM::Native::NewHandle(si.size, true); + if (tmp.error()) return tmp.error(); + si.handle = tmp.value().handle; + si.address = tmp.value().pointer; + } MM::Native::HLock(si.handle); @@ -198,17 +205,21 @@ namespace Loader { // load a standard code segment. uint16_t LoadCode(uint16_t segment) { - uint16_t err; SegmentInfo si; - err = RM::Native::GetResource(kCODE, segment, si.handle); - if (err) return err; - + { + auto tmp = RM::Native::GetResource(kCODE, segment); + if (tmp.error()) return tmp.error(); + si.handle = tmp.value(); + } MM::Native::HLock(si.handle); - MM::Native::GetHandleSize(si.handle, si.size); - si.address = memoryReadLong(si.handle); + { + auto tmp = MM::Native::GetHandleInfo(si.handle); + si.size = tmp.value().size; + si.address = tmp.value().address; + } if (memoryReadWord(si.address) == 0xffff) si.farModel = true; @@ -343,7 +354,7 @@ namespace Loader { { std::string s; - auto ix = path.rfind('/'); + auto ix = path.find_last_of("/:", 0, 2); if (ix == path.npos) { s = path.substr(0, 31); @@ -552,5 +563,35 @@ namespace Loader { } + uint16_t LoadSeg(uint16_t trap) { + + + /* + * Jump Table Entry: + * + * Unloaded: + * +0 offset + * +2 instruction that pushes segment number onto stack + * +6 LoadSeg trap + * + * Loaded: + * +0 offset + * +2 jump instruction. + * + */ + + + + uint32_t sp; + uint16_t segment; + + sp = StackFrame<2>(segment); + + Log("%04x LoadSeg(%04x)\n", trap, segment); + + return 0; + + } + } diff --git a/toolbox/loader.h b/toolbox/loader.h index 5793ea2..0b6b63e 100644 --- a/toolbox/loader.h +++ b/toolbox/loader.h @@ -26,6 +26,7 @@ namespace Loader { } + uint16_t LoadSeg(uint16_t trap); uint16_t UnloadSeg(uint16_t trap); } diff --git a/toolbox/mm.cpp b/toolbox/mm.cpp index 7f8daa9..2d165d2 100644 --- a/toolbox/mm.cpp +++ b/toolbox/mm.cpp @@ -45,6 +45,9 @@ using ToolBox::Log; +using MacOS::tool_return; +using MacOS::macos_error_from_errno; +using MacOS::macos_error; namespace { @@ -65,15 +68,15 @@ namespace // map of handle -> size [? just use Ptr map?] std::map HandleMap; - inline MacOS::macos_error SetMemError(MacOS::macos_error error) + inline macos_error SetMemError(macos_error error) { memoryWriteWord(error, MacOS::MemErr); return error; } - inline MacOS::macos_error SetMemError(int16_t error) + inline macos_error SetMemError(int16_t error) { - return SetMemError((MacOS::macos_error)error); + return SetMemError((macos_error)error); } @@ -98,13 +101,6 @@ namespace } - template - int16_t with_handle(uint32_t handle, Fx fx) - { - auto iter = HandleMap.find(handle); - if (iter == HandleMap.end()) return MacOS::memWZErr; - return fx(iter->second); - } } namespace MM @@ -139,6 +135,46 @@ namespace MM namespace Native { + template + struct tool_return_base { typedef T type; }; + + template + struct tool_return_base> { typedef T type; }; + + + template + tool_return with_handle_helper(F &&f, HandleInfo &info, typename std::enable_if::value>::type* = 0) { + tool_return rv = f(info); + return rv; + } + + template + tool_return with_handle_helper(F &&f, HandleInfo &info, typename std::enable_if::value>::type* = 0) { + f(info); + return tool_return(); + } + + + + template::type>::type> + tool_return + __with_handle(uint32_t handle, F &&f) + { + const auto iter = HandleMap.find(handle); + + if (iter == HandleMap.end()) { + tool_return rv = SetMemError(MacOS::memWZErr); + return rv; + } + + auto &info = iter->second; + //tool_return rv = f(info); + tool_return rv = with_handle_helper(std::forward(f), info); + SetMemError(rv.error()); + return rv; + } + + // debugger support. // print info on an address. void MemoryInfo(uint32_t address) @@ -233,11 +269,11 @@ namespace MM } - uint16_t NewPtr(uint32_t size, bool clear, uint32_t &mcptr) + tool_return NewPtr(uint32_t size, bool clear) { // native pointers. - mcptr = 0; + uint32_t mcptr = 0; //if (size == 0) return 0; uint8_t *ptr = nullptr; @@ -253,10 +289,11 @@ namespace MM mcptr = ptr - Memory; PtrMap.emplace(std::make_pair(mcptr, size)); - return SetMemError(0); + SetMemError(0); + return mcptr; } - uint16_t DisposePtr(uint32_t mcptr) + tool_return DisposePtr(uint32_t mcptr) { auto iter = PtrMap.find(mcptr); @@ -271,13 +308,13 @@ namespace MM return SetMemError(0); } - uint16_t NewHandle(uint32_t size, bool clear, uint32_t &handle, uint32_t &mcptr) + tool_return NewHandle(uint32_t size, bool clear) { uint8_t *ptr; uint32_t hh; - handle = 0; - mcptr = 0; + uint32_t handle = 0; + uint32_t mcptr = 0; if (!HandleQueue.size()) { @@ -316,18 +353,12 @@ namespace MM memoryWriteLong(mcptr, hh); handle = hh; - return SetMemError(0); - } - - uint16_t NewHandle(uint32_t size, bool clear, uint32_t &handle) - { - uint32_t ptr; - return NewHandle(size, clear, handle, ptr); + SetMemError(0); + return hp{handle, mcptr}; } - - uint16_t DisposeHandle(uint32_t handle) + tool_return DisposeHandle(uint32_t handle) { auto iter = HandleMap.find(handle); @@ -349,19 +380,9 @@ namespace MM } - uint16_t GetHandleSize(uint32_t handle, uint32_t &handleSize) - { - handleSize = 0; - - const auto iter = HandleMap.find(handle); - - if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr); - handleSize = iter->second.size; - return SetMemError(0); - } - uint16_t ReallocHandle(uint32_t handle, uint32_t logicalSize) + tool_return ReallocHandle(uint32_t handle, uint32_t logicalSize) { auto iter = HandleMap.find(handle); @@ -400,12 +421,12 @@ namespace MM // lock? clear purged flag? - return 0; + return MacOS::noErr; } - uint16_t SetHandleSize(uint32_t handle, uint32_t newSize) + tool_return SetHandleSize(uint32_t handle, uint32_t newSize) { if (handle == 0) return SetMemError(MacOS::nilHandleErr); @@ -519,61 +540,35 @@ namespace MM } - // template class to validate handle and work on it. - template - uint16_t HandleIt(uint32_t handle, FX fx) + + tool_return HSetRBit(uint32_t handle) { - const auto iter = HandleMap.find(handle); - - if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr); - - auto &info = iter->second; - fx(info); - return SetMemError(0); + return __with_handle(handle, [](HandleInfo &info) { info.resource = true; }); } - uint16_t HSetRBit(uint32_t handle) + tool_return HClrRBit(uint32_t handle) { - const auto iter = HandleMap.find(handle); - - if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr); - - auto &info = iter->second; - info.resource = true; - return SetMemError(0); + return __with_handle(handle, [](HandleInfo &info) { info.resource = false; }); } - uint16_t HClrRBit(uint32_t handle) + tool_return HLock(uint32_t handle) { - const auto iter = HandleMap.find(handle); - - if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr); - - auto &info = iter->second; - info.resource = false; - return SetMemError(0); + return __with_handle(handle, [](HandleInfo &info) { info.locked = true; }); } - uint16_t HLock(uint32_t handle) + tool_return HUnlock(uint32_t handle) { - const auto iter = HandleMap.find(handle); - - if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr); - - auto &info = iter->second; - info.locked = true; - return SetMemError(0); + return __with_handle(handle, [](HandleInfo &info) { info.locked = false; }); } - uint16_t HUnlock(uint32_t handle) + tool_return GetHandleSize(uint32_t handle) { - const auto iter = HandleMap.find(handle); + return __with_handle(handle, [](const HandleInfo &info){ return info.size; }); + } - if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr); - - auto &info = iter->second; - info.locked = false; - return SetMemError(0); + tool_return GetHandleInfo(uint32_t handle) + { + return __with_handle(handle, [](const HandleInfo &info){ return info; }); } @@ -583,25 +578,7 @@ 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 -- @@ -828,12 +805,10 @@ namespace MM // todo -- separate pools for sys vs non-sys? // todo -- NewPtr(0) -- null or empty ptr? - uint32_t mcptr; - uint16_t error; - error = Native::NewPtr(size, clear, mcptr); + auto rv = Native::NewPtr(size, clear); - cpuSetAReg(0, mcptr); - return error; //SetMemError(error); + cpuSetAReg(0, rv.value()); + return rv.error(); //SetMemError(error); } uint16_t DisposePtr(uint16_t trap) @@ -852,10 +827,9 @@ namespace MM Log("%04x DisposePtr(%08x)\n", trap, mcptr); - uint16_t error; - error = Native::DisposePtr(mcptr); + auto rv = Native::DisposePtr(mcptr); - return error; //SetMemError(error); + return rv.error(); //SetMemError(error); } uint32_t GetPtrSize(uint16_t trap) @@ -928,8 +902,6 @@ namespace MM * */ - uint32_t hh = 0; - uint16_t error; bool clear = trap & (1 << 9); //bool sys = trap & (1 << 10); @@ -938,10 +910,10 @@ namespace MM Log("%04x NewHandle(%08x)\n", trap, size); - error = Native::NewHandle(size, clear, hh); + auto rv = Native::NewHandle(size, clear); - cpuSetAReg(0, hh); - return error; + cpuSetAReg(0, rv.value().handle); + return rv.error(); } uint16_t DisposeHandle(uint16_t trap) @@ -959,7 +931,7 @@ namespace MM Log("%04x DisposeHandle(%08x)\n", trap, hh); - return Native::DisposeHandle(hh); + return Native::DisposeHandle(hh).error(); } uint16_t EmptyHandle(uint16_t trap) @@ -1026,7 +998,7 @@ namespace MM Log("%04x ReallocHandle(%08x, %08x)\n", trap, hh, logicalSize); - return Native::ReallocHandle(hh, logicalSize); + return Native::ReallocHandle(hh, logicalSize).error(); #if 0 auto iter = HandleMap.find(hh); @@ -1121,7 +1093,7 @@ namespace MM Log("%04x SetHandleSize(%08x, %08x)\n", trap, hh, newSize); - return Native::SetHandleSize(hh, newSize); + return Native::SetHandleSize(hh, newSize).error(); } uint32_t RecoverHandle(uint16_t trap) @@ -1368,16 +1340,15 @@ namespace MM auto const info = iter->second; - uint32_t destHandle; - uint32_t destPtr; - uint32_t d0 = Native::NewHandle(info.size, false, destHandle, destPtr); - if (d0 == 0) + auto rv = Native::NewHandle(info.size, false); + + if (!rv.error()) { - std::memmove(memoryPointer(destPtr), memoryPointer(info.address), info.size); + std::memmove(memoryPointer(rv.value().pointer), memoryPointer(info.address), info.size); } - cpuSetAReg(0, destHandle); - return d0; // SetMemError called by Native::NewHandle. + cpuSetAReg(0, rv.value().handle); + return rv.error(); // SetMemError called by Native::NewHandle. } @@ -1399,16 +1370,14 @@ namespace MM Log("%04x PtrToHand(%08x, %08x)\n", trap, mcptr, size); - uint32_t destHandle; - uint32_t destPtr; - uint32_t d0 = Native::NewHandle(size, false, destHandle, destPtr); - if (d0 == 0) + auto rv = Native::NewHandle(size, false); + if (!rv.error()) { - std::memmove(memoryPointer(destPtr), memoryPointer(mcptr), size); + std::memmove(memoryPointer(rv.value().pointer), memoryPointer(mcptr), size); } - cpuSetAReg(0, destHandle); - return d0; // SetMemError called by Native::NewHandle. + cpuSetAReg(0, rv.value().handle); + return rv.error(); // SetMemError called by Native::NewHandle. } uint16_t PtrAndHand(uint16_t trap) @@ -1435,18 +1404,16 @@ namespace MM cpuSetAReg(0, handle); - uint32_t oldSize = 0; - uint32_t d0; - - d0 = Native::GetHandleSize(handle, oldSize); - if (d0) return d0; + auto tmp = Native::GetHandleSize(handle); + if (tmp.error()) return tmp.error(); + uint32_t oldSize = tmp.value(); if ((uint64_t)oldSize + (uint64_t)size > UINT32_MAX) return SetMemError(MacOS::memFullErr); - d0 = Native::SetHandleSize(handle, oldSize + size); - if (d0) return d0; + auto err = Native::SetHandleSize(handle, oldSize + size); + if (err.error()) return err.error(); auto iter = HandleMap.find(handle); if (iter == HandleMap.end()) @@ -1637,20 +1604,18 @@ namespace MM { // FUNCTION TempNewHandle (logicalSize: Size; // VAR resultCode: OSErr): Handle; - uint16_t rv; uint32_t logicalSize; uint32_t resultCode; - uint32_t theHandle; uint32_t sp = StackFrame<8>(logicalSize, resultCode); Log(" TempNewHandle(%08x, %08x)\n", logicalSize, resultCode); - rv = Native::NewHandle(logicalSize, true, theHandle); + auto rv = Native::NewHandle(logicalSize, true); - if (resultCode) memoryWriteWord(rv, resultCode); - ToolReturn<4>(sp, theHandle); - return rv; + if (resultCode) memoryWriteWord(rv.error(), resultCode); + ToolReturn<4>(sp, rv.value().handle); + return rv.error(); } uint16_t TempHLock(void) @@ -1663,10 +1628,10 @@ namespace MM Log(" TempHLock(%08x, %08x)\n", theHandle, resultCode); - uint16_t rv = Native::HLock(theHandle); + auto rv = Native::HLock(theHandle); - if (resultCode) memoryWriteWord(rv, resultCode); - return rv; + if (resultCode) memoryWriteWord(rv.error(), resultCode); + return rv.error(); } uint16_t TempHUnlock(void) @@ -1679,10 +1644,10 @@ namespace MM Log(" TempHUnlock(%08x, %08x)\n", theHandle, resultCode); - uint16_t rv = Native::HUnlock(theHandle); + auto rv = Native::HUnlock(theHandle); - if (resultCode) memoryWriteWord(rv, resultCode); - return rv; + if (resultCode) memoryWriteWord(rv.error(), resultCode); + return rv.error(); } @@ -1696,9 +1661,9 @@ namespace MM Log(" TempDisposeHandle(%08x, %08x)\n", theHandle, resultCode); - uint16_t rv = Native::DisposeHandle(theHandle); + auto rv = Native::DisposeHandle(theHandle); - if (resultCode) memoryWriteWord(rv, resultCode); - return rv; + if (resultCode) memoryWriteWord(rv.error(), resultCode); + return rv.error(); } } diff --git a/toolbox/mm.h b/toolbox/mm.h index 6b8b909..e7a9399 100644 --- a/toolbox/mm.h +++ b/toolbox/mm.h @@ -5,37 +5,9 @@ #include + namespace MM { - // native functions. - namespace Native - { - - void MemoryInfo(uint32_t address); - void PrintMemoryStats(); - - uint16_t NewHandle(uint32_t size, bool clear, uint32_t &handle); - uint16_t NewHandle(uint32_t size, bool clear, uint32_t &handle, uint32_t &ptr); - - uint16_t NewPtr(uint32_t size, bool clear, uint32_t &pointer); - - uint16_t DisposeHandle(uint32_t handle); - uint16_t DisposePtr(uint32_t pointer); - - uint16_t GetHandleSize(uint32_t handle, uint32_t &handleSize); - - uint16_t SetHandleSize(uint32_t handle, uint32_t newSize); - uint16_t ReallocHandle(uint32_t handle, uint32_t newSize); - - uint16_t HSetRBit(uint32_t handle); - uint16_t HClrRBit(uint32_t handle); - - uint16_t HLock(uint32_t handle); - uint16_t HUnlock(uint32_t handle); - } - - bool Init(uint8_t *memory, uint32_t memorySize, uint32_t globals, uint32_t stack); - struct HandleInfo { @@ -50,11 +22,54 @@ namespace MM {} }; + + // native functions. + namespace Native + { + + using MacOS::tool_return; + + struct hp { + uint32_t handle = 0; + uint32_t pointer = 0; + hp() = default; + hp(uint32_t a, uint32_t b) : handle(a), pointer(b) {}; + }; + + void MemoryInfo(uint32_t address); + void PrintMemoryStats(); + + tool_return NewHandle(uint32_t size, bool clear); + + tool_return NewPtr(uint32_t size, bool clear); + + tool_return DisposeHandle(uint32_t handle); + tool_return DisposePtr(uint32_t pointer); + + tool_return GetHandleSize(uint32_t handle); + + tool_return SetHandleSize(uint32_t handle, uint32_t newSize); + tool_return ReallocHandle(uint32_t handle, uint32_t newSize); + + tool_return HSetRBit(uint32_t handle); + tool_return HClrRBit(uint32_t handle); + + tool_return HLock(uint32_t handle); + tool_return HUnlock(uint32_t handle); + + + tool_return GetHandleInfo(uint32_t handle); + + tool_return GetHandleSize(uint32_t handle); + } + + bool Init(uint8_t *memory, uint32_t memorySize, uint32_t globals, uint32_t stack); + + + using MacOS::tool_return; - tool_return GetHandleInfo(uint32_t handle); - tool_return GetHandleSize(uint32_t handle); uint16_t BlockMove(uint16_t trap); diff --git a/toolbox/rm.cpp b/toolbox/rm.cpp index e282f4d..ad96c0a 100644 --- a/toolbox/rm.cpp +++ b/toolbox/rm.cpp @@ -112,10 +112,10 @@ namespace std::map rhandle_map; - inline uint16_t SetResError(uint16_t error) + inline macos_error SetResError(uint16_t error) { memoryWriteWord(error, MacOS::ResErr); - return error; + return (macos_error)error; } bool LoadResType(uint32_t type) @@ -165,10 +165,9 @@ namespace RM // not to be confused with MacOS LoadResource(theHandle) template - uint16_t LoadResource(uint32_t type, uint32_t &theHandle, FX fx) + macos_error LoadResource(uint32_t type, uint32_t &theHandle, FX fx) { uint32_t ptr; - uint16_t error; Handle nativeHandle; uint32_t size; @@ -190,14 +189,18 @@ namespace RM if (ResLoad) ::LoadResource(nativeHandle); size = ::GetHandleSize(nativeHandle); - error = MM::Native::NewHandle(size, false, theHandle, ptr); + { + auto tmp = MM::Native::NewHandle(size, false); + if (tmp.error()) { + ::ReleaseResource(nativeHandle); + return SetResError(tmp.error()); + } + theHandle = tmp.value().handle; + ptr = tmp.value().pointer; + } // TODO -- need to lock if native handle locked. - if (!theHandle) - { - ::ReleaseResource(nativeHandle); - return SetResError(error); - } + MM::Native::HSetRBit(theHandle); if (size) @@ -213,15 +216,19 @@ namespace RM // used by GetString (utility.h) // used by Loader. - uint16_t GetResource(uint32_t type, uint16_t id, uint32_t &theHandle) + tool_return GetResource(uint32_t type, uint16_t id) { - return LoadResource(type, theHandle, + uint32_t theHandle; + auto err = LoadResource(type, theHandle, [type, id](){ return ::GetResource(type, id); }); + + if (!err) return theHandle; + return err; } - uint16_t SetResLoad(bool load) + tool_return SetResLoad(bool load) { ResLoad = load; @@ -232,6 +239,19 @@ namespace RM } + + tool_return CloseResFile(uint16_t refNum) + { + // If the value of the refNum parameter is 0, it represents the System file and is ignored. + + if (refNum != 0) + { + ::CloseResFile(refNum); + return SetResError(::ResError()); + } + return SetResError(0); + } + } uint16_t CloseResFile(uint16_t trap) @@ -844,7 +864,7 @@ namespace RM // check if handle size changed, resync data - auto info = MM::GetHandleInfo(theResource); + auto info = MM::Native::GetHandleInfo(theResource); if (!info.error()) return SetResError(info.error()); @@ -928,7 +948,7 @@ namespace RM Handle nativeHandle = NULL; - auto info = MM::GetHandleInfo(theData); + auto info = MM::Native::GetHandleInfo(theData); if (info.error()) return SetResError(MacOS::addResFailed); @@ -1179,7 +1199,7 @@ namespace RM // if it has a size, it's loaded... - auto info = MM::GetHandleInfo(theResource); + auto info = MM::Native::GetHandleInfo(theResource); if (info.error()) return SetResError(resNotFound); if (info->size) return SetResError(0); @@ -1198,14 +1218,14 @@ namespace RM size = ::GetHandleSize(nativeHandle); - err = MM::Native::ReallocHandle(theResource, size); - if (err) return SetResError(err); + auto tmp = MM::Native::ReallocHandle(theResource, size); + if (tmp.error()) return SetResError(tmp.error()); // todo -- need to lock if resource locked. if (size) { - info = MM::GetHandleInfo(theResource); + info = MM::Native::GetHandleInfo(theResource); std::memcpy(memoryPointer(info->address), *(void **)nativeHandle, size); } diff --git a/toolbox/rm.h b/toolbox/rm.h index 428ce04..b6d30ae 100644 --- a/toolbox/rm.h +++ b/toolbox/rm.h @@ -3,13 +3,19 @@ #include +#include + namespace RM { namespace Native { - uint16_t SetResLoad(bool tf); - uint16_t GetResource(uint32_t type, uint16_t id, uint32_t &theHandle); + using MacOS::tool_return; + + tool_return SetResLoad(bool tf); + tool_return GetResource(uint32_t type, uint16_t id); + tool_return OpenResFile(const std::string &path_name, uint16_t permissions = 0); + tool_return CloseResFile(uint16_t refNum); } uint16_t CloseResFile(uint16_t trap); diff --git a/toolbox/utility.cpp b/toolbox/utility.cpp index 82d5f5f..236b80e 100644 --- a/toolbox/utility.cpp +++ b/toolbox/utility.cpp @@ -49,10 +49,8 @@ namespace Utility { */ uint32_t theString; - uint32_t theHandle; uint32_t sp; uint32_t length = 0; - uint32_t d0; std::string s; @@ -64,19 +62,12 @@ namespace Utility { length = s.length() + 1; - // if (length) // always true... - { - uint32_t ptr; - d0 = MM::Native::NewHandle(s.length(), false, theHandle, ptr); - if (!d0) - { - ToolBox::WritePString(ptr, s); - } - } + auto rv = MM::Native::NewHandle(s.length(), false); + if (!rv.error()) ToolBox::WritePString(rv.value().pointer, s); - ToolReturn<4>(sp, theHandle); - return d0; + ToolReturn<4>(sp, rv.value().handle); + return rv.error(); } // FUNCTION GetString (stringID: Integer): StringHandle; @@ -91,17 +82,15 @@ namespace Utility { uint16_t stringID; uint32_t sp; - uint32_t d0; - uint32_t theHandle; sp = StackFrame<2>(stringID); Log("%04x GetString($%04x)\n", trap, stringID); - d0 = RM::Native::GetResource(0x53545220, stringID, theHandle); + auto rv = RM::Native::GetResource(0x53545220, stringID); - ToolReturn<4>(sp, theHandle); - return d0; + ToolReturn<4>(sp, rv.value()); + return rv.error(); } // FUNCTION BitTst (bytePtr: Ptr; bitNum: LONGINT) : BOOLEAN;