mirror of
https://github.com/ksherlock/mpw.git
synced 2024-06-26 03:29:29 +00:00
Native::NewHandle, Native::DisposeHandle
This commit is contained in:
parent
e3a3c4a933
commit
4136209d84
138
toolbox/mm.cpp
138
toolbox/mm.cpp
|
@ -114,7 +114,7 @@ namespace MM
|
||||||
ptr = (uint8_t *)mplite_malloc(&pool, size);
|
ptr = (uint8_t *)mplite_malloc(&pool, size);
|
||||||
if (!ptr)
|
if (!ptr)
|
||||||
{
|
{
|
||||||
return memFullErr;
|
return SetMemError(memFullErr);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (clear)
|
if (clear)
|
||||||
|
@ -123,7 +123,7 @@ namespace MM
|
||||||
mcptr = ptr - Memory;
|
mcptr = ptr - Memory;
|
||||||
PtrMap.emplace(std::make_pair(mcptr, size));
|
PtrMap.emplace(std::make_pair(mcptr, size));
|
||||||
|
|
||||||
return 0;
|
return SetMemError(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t DisposePtr(uint32_t mcptr)
|
uint16_t DisposePtr(uint32_t mcptr)
|
||||||
|
@ -131,14 +131,84 @@ namespace MM
|
||||||
|
|
||||||
auto iter = PtrMap.find(mcptr);
|
auto iter = PtrMap.find(mcptr);
|
||||||
|
|
||||||
if (iter == PtrMap.end()) return memWZErr;
|
if (iter == PtrMap.end()) return SetMemError(memWZErr);
|
||||||
PtrMap.erase(iter);
|
PtrMap.erase(iter);
|
||||||
|
|
||||||
uint8_t *ptr = mcptr + Memory;
|
uint8_t *ptr = mcptr + Memory;
|
||||||
|
|
||||||
mplite_free(&pool, ptr);
|
mplite_free(&pool, ptr);
|
||||||
|
|
||||||
return 0;
|
return SetMemError(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t NewHandle(uint32_t size, bool clear, uint32_t &handle, uint32_t &mcptr)
|
||||||
|
{
|
||||||
|
uint8_t *ptr;
|
||||||
|
uint32_t hh;
|
||||||
|
|
||||||
|
handle = 0;
|
||||||
|
mcptr = 0;
|
||||||
|
|
||||||
|
if (!HandleQueue.size())
|
||||||
|
{
|
||||||
|
if (!alloc_handle_block())
|
||||||
|
{
|
||||||
|
return SetMemError(memFullErr);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
hh = HandleQueue.front();
|
||||||
|
HandleQueue.pop_front();
|
||||||
|
|
||||||
|
ptr = nullptr;
|
||||||
|
|
||||||
|
if (size)
|
||||||
|
{
|
||||||
|
ptr = (uint8_t *)mplite_malloc(&pool, size);
|
||||||
|
if (!ptr)
|
||||||
|
{
|
||||||
|
HandleQueue.push_back(hh);
|
||||||
|
return SetMemError(memFullErr);
|
||||||
|
}
|
||||||
|
mcptr = ptr - Memory;
|
||||||
|
|
||||||
|
if (clear)
|
||||||
|
std::memset(ptr, 0, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
// need a handle -> ptr map?
|
||||||
|
HandleMap.emplace(std::make_pair(hh, HandleInfo(mcptr, size)));
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
uint16_t DisposeHandle(uint32_t handle)
|
||||||
|
{
|
||||||
|
auto iter = HandleMap.find(handle);
|
||||||
|
|
||||||
|
if (iter == HandleMap.end()) return SetMemError(memWZErr);
|
||||||
|
|
||||||
|
HandleInfo info = iter->second;
|
||||||
|
|
||||||
|
HandleMap.erase(iter);
|
||||||
|
|
||||||
|
uint8_t *ptr = info.address + Memory;
|
||||||
|
|
||||||
|
mplite_free(&pool, ptr);
|
||||||
|
|
||||||
|
HandleQueue.push_back(handle);
|
||||||
|
|
||||||
|
return SetMemError(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -258,7 +328,7 @@ namespace MM
|
||||||
error = Native::NewPtr(size, clear, mcptr);
|
error = Native::NewPtr(size, clear, mcptr);
|
||||||
|
|
||||||
cpuSetAReg(0, mcptr);
|
cpuSetAReg(0, mcptr);
|
||||||
return SetMemError(error);
|
return error; //SetMemError(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t DisposePtr(uint16_t trap)
|
uint16_t DisposePtr(uint16_t trap)
|
||||||
|
@ -280,7 +350,7 @@ namespace MM
|
||||||
uint16_t error;
|
uint16_t error;
|
||||||
error = Native::DisposePtr(mcptr);
|
error = Native::DisposePtr(mcptr);
|
||||||
|
|
||||||
return SetMemError(error);
|
return error; //SetMemError(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t GetPtrSize(uint16_t trap)
|
uint32_t GetPtrSize(uint16_t trap)
|
||||||
|
@ -354,8 +424,7 @@ namespace MM
|
||||||
*/
|
*/
|
||||||
|
|
||||||
uint32_t hh = 0;
|
uint32_t hh = 0;
|
||||||
uint8_t *ptr;
|
uint16_t error;
|
||||||
uint32_t mcptr;
|
|
||||||
|
|
||||||
bool clear = trap & (1 << 9);
|
bool clear = trap & (1 << 9);
|
||||||
//bool sys = trap & (1 << 10);
|
//bool sys = trap & (1 << 10);
|
||||||
|
@ -364,42 +433,10 @@ namespace MM
|
||||||
|
|
||||||
Log("%04x NewHandle(%08x)\n", trap, size);
|
Log("%04x NewHandle(%08x)\n", trap, size);
|
||||||
|
|
||||||
if (!HandleQueue.size())
|
error = Native::NewHandle(size, clear, hh);
|
||||||
{
|
|
||||||
if (!alloc_handle_block())
|
|
||||||
{
|
|
||||||
cpuSetAReg(0, 0);
|
|
||||||
return SetMemError(memFullErr);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
hh = HandleQueue.front();
|
|
||||||
HandleQueue.pop_front();
|
|
||||||
|
|
||||||
ptr = nullptr;
|
|
||||||
mcptr = 0;
|
|
||||||
|
|
||||||
if (size)
|
|
||||||
{
|
|
||||||
ptr = (uint8_t *)mplite_malloc(&pool, size);
|
|
||||||
if (!ptr)
|
|
||||||
{
|
|
||||||
HandleQueue.push_back(hh);
|
|
||||||
cpuSetAReg(0, 0);
|
|
||||||
return SetMemError(memFullErr);
|
|
||||||
}
|
|
||||||
mcptr = ptr - Memory;
|
|
||||||
|
|
||||||
if (clear)
|
|
||||||
std::memset(ptr, 0, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
// need a handle -> ptr map?
|
|
||||||
HandleMap.emplace(std::make_pair(hh, HandleInfo(mcptr, size)));
|
|
||||||
|
|
||||||
memoryWriteLong(mcptr, hh);
|
|
||||||
cpuSetAReg(0, hh);
|
cpuSetAReg(0, hh);
|
||||||
return SetMemError(0);
|
return error;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint16_t DisposeHandle(uint16_t trap)
|
uint16_t DisposeHandle(uint16_t trap)
|
||||||
|
@ -417,22 +454,7 @@ namespace MM
|
||||||
|
|
||||||
Log("%04x DisposeHandle(%08x)\n", trap, hh);
|
Log("%04x DisposeHandle(%08x)\n", trap, hh);
|
||||||
|
|
||||||
|
return Native::DisposeHandle(hh);
|
||||||
auto iter = HandleMap.find(hh);
|
|
||||||
|
|
||||||
if (iter == HandleMap.end()) return SetMemError(memWZErr);
|
|
||||||
|
|
||||||
HandleInfo info = iter->second;
|
|
||||||
|
|
||||||
HandleMap.erase(iter);
|
|
||||||
|
|
||||||
uint8_t *ptr = info.address + Memory;
|
|
||||||
|
|
||||||
mplite_free(&pool, ptr);
|
|
||||||
|
|
||||||
HandleQueue.push_back(hh);
|
|
||||||
|
|
||||||
return SetMemError(0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#pragma mark Handle attributes
|
#pragma mark Handle attributes
|
||||||
|
|
|
@ -18,7 +18,9 @@ namespace MM
|
||||||
|
|
||||||
void PrintMemoryStats();
|
void PrintMemoryStats();
|
||||||
|
|
||||||
uint16_t NewHandle(uint32_t size, uint32_t &handle);
|
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 NewPtr(uint32_t size, bool clear, uint32_t &pointer);
|
||||||
|
|
||||||
uint16_t DisposeHandle(uint32_t handle);
|
uint16_t DisposeHandle(uint32_t handle);
|
||||||
|
|
Loading…
Reference in New Issue
Block a user