usbloadergx/source/memory/mem2alloc.cpp

227 lines
5.2 KiB
C++
Raw Normal View History

#include "mem2alloc.hpp"
#include <ogc/system.h>
#include <algorithm>
#include <string.h>
class LockMutex
{
mutex_t &m_mutex;
public:
2010-09-24 02:48:03 +02:00
LockMutex(mutex_t &m) :
m_mutex(m)
{
LWP_MutexLock(m_mutex);
}
~LockMutex(void)
{
LWP_MutexUnlock(m_mutex);
}
};
2010-09-24 02:48:03 +02:00
void CMEM2Alloc::init(unsigned int size)
{
2010-09-24 02:48:03 +02:00
m_baseAddress = (SBlock *) (((u32) SYS_GetArena2Lo() + 31) & ~31);
m_endAddress = (SBlock *) ((char *) m_baseAddress + std::min(size * 0x100000, SYS_GetArena2Size() & ~31));
if (m_endAddress > (SBlock *) 0x93300000) //rest is reserved for usb/usb2/network and other stuff... (0xE0000 bytes)
m_endAddress = (SBlock *) 0x93300000;
SYS_SetArena2Lo(m_endAddress);
LWP_MutexInit(&m_mutex, 0);
}
2010-09-24 02:48:03 +02:00
void CMEM2Alloc::init(void *addr, void *end)
{
2010-09-24 02:48:03 +02:00
m_baseAddress = (SBlock *) (((u32) addr + 31) & ~31);
m_endAddress = (SBlock *) ((u32) end & ~31);
LWP_MutexInit(&m_mutex, 0);
}
2010-09-24 02:48:03 +02:00
void CMEM2Alloc::cleanup(void)
{
2010-09-24 02:48:03 +02:00
LWP_MutexDestroy(m_mutex);
m_mutex = 0;
m_first = 0;
// Try to release the range we took through SYS functions
2010-09-24 02:48:03 +02:00
if (SYS_GetArena2Lo() == m_endAddress) SYS_SetArena2Lo(m_baseAddress);
m_baseAddress = 0;
m_endAddress = 0;
}
2010-09-24 02:48:03 +02:00
void CMEM2Alloc::clear(void)
{
m_first = 0;
2010-09-24 02:48:03 +02:00
memset(m_baseAddress, 0, (u8 *) m_endAddress - (u8 *) m_endAddress);
}
2010-09-24 02:48:03 +02:00
unsigned int CMEM2Alloc::usableSize(void *p)
{
2010-09-24 02:48:03 +02:00
return p == 0 ? 0 : ((SBlock *) p - 1)->s * sizeof(SBlock);
}
2010-09-24 02:48:03 +02:00
void *CMEM2Alloc::allocate(unsigned int s)
{
2010-09-24 02:48:03 +02:00
if (s == 0) s = 1;
//
2010-09-24 02:48:03 +02:00
LockMutex lock(m_mutex);
//
2010-09-24 02:48:03 +02:00
s = (s - 1) / sizeof(SBlock) + 1;
// First block
2010-09-24 02:48:03 +02:00
if (m_first == 0)
{
2010-09-24 02:48:03 +02:00
if (m_baseAddress + s + 1 >= m_endAddress) return 0;
m_first = m_baseAddress;
m_first->next = 0;
m_first->prev = 0;
m_first->s = s;
m_first->f = false;
2010-09-24 02:48:03 +02:00
return (void *) (m_first + 1);
}
// Search for a free block
SBlock *i;
SBlock *j;
2010-09-24 02:48:03 +02:00
for (i = m_first; i != 0; i = i->next)
{
2010-09-24 02:48:03 +02:00
if (i->f && i->s >= s) break;
j = i;
}
// Create a new block
2010-09-24 02:48:03 +02:00
if (i == 0)
{
i = j + j->s + 1;
2010-09-24 02:48:03 +02:00
if (i + s + 1 >= m_endAddress) return 0;
j->next = i;
i->prev = j;
i->next = 0;
i->s = s;
i->f = false;
2010-09-24 02:48:03 +02:00
return (void *) (i + 1);
}
// Reuse a free block
i->f = false;
// Split it
2010-09-24 02:48:03 +02:00
if (i->s > s + 1)
{
j = i + s + 1;
j->f = true;
j->s = i->s - s - 1;
i->s = s;
j->next = i->next;
j->prev = i;
i->next = j;
2010-09-24 02:48:03 +02:00
if (j->next != 0) j->next->prev = j;
}
2010-09-24 02:48:03 +02:00
return (void *) (i + 1);
}
2010-09-24 02:48:03 +02:00
void CMEM2Alloc::release(void *p)
{
2010-09-24 02:48:03 +02:00
if (p == 0) return;
2010-09-24 02:48:03 +02:00
LockMutex lock(m_mutex);
SBlock *i = (SBlock *) p - 1;
i->f = true;
// If there are no other blocks following yet,
// set the remaining size to free size. - Dimok
2010-09-24 02:48:03 +02:00
if (i->next == 0) i->s = m_endAddress - i - 1;
// Merge with previous block
2010-09-24 02:48:03 +02:00
if (i->prev != 0 && i->prev->f)
{
i = i->prev;
i->s += i->next->s + 1;
i->next = i->next->next;
2010-09-24 02:48:03 +02:00
if (i->next != 0) i->next->prev = i;
}
// Merge with next block
2010-09-24 02:48:03 +02:00
if (i->next != 0 && i->next->f)
{
i->s += i->next->s + 1;
i->next = i->next->next;
2010-09-24 02:48:03 +02:00
if (i->next != 0) i->next->prev = i;
}
}
2010-09-24 02:48:03 +02:00
void *CMEM2Alloc::reallocate(void *p, unsigned int s)
{
SBlock *i;
SBlock *j;
void *n;
2010-09-24 02:48:03 +02:00
if (s == 0) s = 1;
if (p == 0) return allocate(s);
2010-09-24 02:48:03 +02:00
i = (SBlock *) p - 1;
s = (s - 1) / sizeof(SBlock) + 1;
{
2010-09-24 02:48:03 +02:00
LockMutex lock(m_mutex);
//out of memory /* Dimok */
2010-09-24 02:48:03 +02:00
if (i + s + 1 >= m_endAddress)
{
return 0;
}
// Last block
2010-09-24 02:48:03 +02:00
if (i->next == 0 && i + s + 1 < m_endAddress)
{
i->s = s;
return p;
}
// Size <= current size + next block
2010-09-24 02:48:03 +02:00
if (i->next != 0 && i->s < s && i->next->f && i->s + i->next->s + 1 >= s)
{
// Merge
i->s += i->next->s + 1;
i->next = i->next->next;
2010-09-24 02:48:03 +02:00
if (i->next != 0) i->next->prev = i;
}
// Size <= current size
2010-09-24 02:48:03 +02:00
if (i->s >= s)
{
// Split
2010-09-24 02:48:03 +02:00
if (i->s > s + 1)
{
j = i + s + 1;
j->f = true;
j->s = i->s - s - 1;
i->s = s;
j->next = i->next;
j->prev = i;
i->next = j;
2010-09-24 02:48:03 +02:00
if (j->next != 0) j->next->prev = j;
}
return p;
}
}
// Size > current size
2010-09-24 02:48:03 +02:00
n = allocate(s * sizeof(SBlock));
if (n == 0) return 0;
memcpy(n, p, i->s * sizeof(SBlock));
release(p);
return n;
}
unsigned int CMEM2Alloc::FreeSize()
{
2010-09-24 02:48:03 +02:00
LockMutex lock(m_mutex);
2010-09-24 02:48:03 +02:00
if (m_first == 0) return (const char *) m_endAddress - (const char *) m_baseAddress;
SBlock *i;
unsigned int size = 0;
2010-09-24 02:48:03 +02:00
for (i = m_first; i != 0; i = i->next)
{
2010-09-24 02:48:03 +02:00
if (i->f && i->next != 0)
size += i->s;
2010-09-24 02:48:03 +02:00
else if (i->f && i->next == 0)
size += m_endAddress - i - 1;
2010-09-24 02:48:03 +02:00
else if (!i->f && i->next == 0) size += m_endAddress - i - i->s - 1;
}
2010-09-24 02:48:03 +02:00
return size * sizeof(SBlock);
}