mirror of
https://github.com/ksherlock/mpw.git
synced 2025-01-03 04:30:32 +00:00
MM::RecoverHandle
This commit is contained in:
parent
1a70abf995
commit
841af35840
123
toolbox/mm.cpp
123
toolbox/mm.cpp
@ -1063,112 +1063,51 @@ namespace MM
|
||||
Log("%04x SetHandleSize(%08x, %08x)\n", trap, hh, newSize);
|
||||
|
||||
return Native::SetHandleSize(hh, newSize);
|
||||
|
||||
#if 0
|
||||
auto iter = HandleMap.find(hh);
|
||||
|
||||
if (iter == HandleMap.end()) return SetMemError(MacOS::memWZErr);
|
||||
// todo -- if handle ptr is null, other logic?
|
||||
// todo -- if locked, can't move.
|
||||
|
||||
auto &info = iter->second;
|
||||
|
||||
// 0 - no change in size.
|
||||
if (info.size == newSize) return SetMemError(0);
|
||||
|
||||
uint32_t mcptr = info.address;
|
||||
uint8_t *ptr = mcptr + Memory;
|
||||
|
||||
|
||||
// 1. - resizing to 0.
|
||||
if (!newSize)
|
||||
{
|
||||
if (info.locked) return SetMemError(MacOS::memLockedErr);
|
||||
|
||||
mplite_free(&pool, ptr);
|
||||
info.address = 0;
|
||||
info.size = 0;
|
||||
|
||||
memoryWriteLong(info.address, hh);
|
||||
return SetMemError(0);
|
||||
}
|
||||
|
||||
// 2. - resizing from 0.
|
||||
|
||||
if (!mcptr)
|
||||
uint32_t RecoverHandle(uint16_t trap)
|
||||
{
|
||||
if (info.locked) return SetMemError(MacOS::memLockedErr);
|
||||
// FUNCTION RecoverHandle (p: Ptr): Handle;
|
||||
|
||||
ptr = (uint8_t *)mplite_malloc(&pool, newSize);
|
||||
if (!ptr) return SetMemError(MacOS::memFullErr);
|
||||
/*
|
||||
* on entry:
|
||||
* A0 Master pointer
|
||||
*
|
||||
* on exit:
|
||||
* A0 Handle to master pointer’s relocatable block
|
||||
* D0 Unchanged
|
||||
*
|
||||
*/
|
||||
|
||||
mcptr = ptr - Memory;
|
||||
info.address = mcptr;
|
||||
info.size = newSize;
|
||||
|
||||
memoryWriteLong(info.address, hh);
|
||||
return SetMemError(0);
|
||||
}
|
||||
uint32_t p = cpuGetAReg(0);
|
||||
uint32_t hh = 0;
|
||||
|
||||
for (unsigned i = 0; i < 2; ++i)
|
||||
Log("%04x RecoverHandle(%08x)\n", trap, p);
|
||||
|
||||
uint16_t error = MacOS::memBCErr;
|
||||
for (const auto kv : HandleMap)
|
||||
{
|
||||
const HandleInfo &info = kv.second;
|
||||
|
||||
// 3. - locked
|
||||
if (info.locked)
|
||||
if (!info.address) continue;
|
||||
|
||||
uint32_t begin = info.address;
|
||||
uint32_t end = info.address + info.size;
|
||||
if (!info.size) end++;
|
||||
if (p >= begin && p < end)
|
||||
{
|
||||
if (mplite_resize(&pool, ptr, mplite_roundup(&pool, newSize)) == MPLITE_OK)
|
||||
{
|
||||
info.size = newSize;
|
||||
return SetMemError(0);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
// 4. - resize.
|
||||
|
||||
ptr = (uint8_t *)mplite_realloc(&pool, ptr, mplite_roundup(&pool, newSize));
|
||||
|
||||
if (ptr)
|
||||
{
|
||||
mcptr = ptr - Memory;
|
||||
info.address = mcptr;
|
||||
info.size = newSize;
|
||||
|
||||
memoryWriteLong(info.address, hh);
|
||||
return SetMemError(0);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fprintf(stderr, "mplite_realloc failed.\n");
|
||||
Native::PrintMemoryStats();
|
||||
|
||||
|
||||
if (i > 0) return SetMemError(MacOS::memFullErr);
|
||||
|
||||
// purge...
|
||||
for (auto & kv : HandleMap)
|
||||
{
|
||||
uint32_t handle = kv.first;
|
||||
auto &info = kv.second;
|
||||
|
||||
if (handle == hh) continue;
|
||||
if (info.size && info.purgeable && !info.locked)
|
||||
{
|
||||
mplite_free(&pool, Memory + info.address);
|
||||
info.size = 0;
|
||||
info.address = 0;
|
||||
|
||||
// also need to update memory
|
||||
memoryWriteLong(0, handle);
|
||||
hh = kv.first;
|
||||
error = MacOS::noErr;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
SetMemError(error);
|
||||
cpuSetAReg(0, hh);
|
||||
|
||||
return SetMemError(MacOS::memFullErr);
|
||||
#endif
|
||||
// return d0 register unchanged.
|
||||
return cpuGetDReg(0);
|
||||
}
|
||||
|
||||
|
||||
|
@ -59,6 +59,8 @@ namespace MM
|
||||
uint16_t SetHandleSize(uint16_t);
|
||||
uint16_t SetPtrSize(uint16_t);
|
||||
|
||||
uint32_t RecoverHandle(uint16_t);
|
||||
|
||||
uint16_t HGetState(uint16_t trap);
|
||||
|
||||
uint16_t HLock(uint16_t trap);
|
||||
|
@ -289,6 +289,10 @@ namespace ToolBox {
|
||||
d0 = MM::HandleZone(trap);
|
||||
break;
|
||||
|
||||
case 0xa128:
|
||||
d0 = MM::RecoverHandle(trap);
|
||||
break;
|
||||
|
||||
// MaxApplZone
|
||||
case 0xa063:
|
||||
d0 = MM::MaxApplZone(trap);
|
||||
|
Loading…
Reference in New Issue
Block a user