Fix formatting and remove namespaces in libmaxsi heap.cpp.
What a mess.
This commit is contained in:
parent
d81cdc09e9
commit
31df7c0c93
|
@ -54,10 +54,6 @@
|
||||||
typedef uintptr_t addr_t;
|
typedef uintptr_t addr_t;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace Maxsi
|
|
||||||
{
|
|
||||||
namespace Memory
|
|
||||||
{
|
|
||||||
//
|
//
|
||||||
// This first section is just magic compiler/platform stuff, you should
|
// This first section is just magic compiler/platform stuff, you should
|
||||||
// skip ahead to the actual algorithm.
|
// skip ahead to the actual algorithm.
|
||||||
|
@ -77,17 +73,17 @@ namespace Maxsi
|
||||||
extern addr_t wilderness;
|
extern addr_t wilderness;
|
||||||
|
|
||||||
#ifdef SORTIX_KERNEL
|
#ifdef SORTIX_KERNEL
|
||||||
addr_t GetHeapStart()
|
static addr_t GetHeapStart()
|
||||||
{
|
{
|
||||||
return Sortix::Memory::GetHeapUpper();
|
return Sortix::Memory::GetHeapUpper();
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t GetHeapMaxSize()
|
static size_t GetHeapMaxSize()
|
||||||
{
|
{
|
||||||
return Sortix::Memory::GetHeapUpper() - Sortix::Memory::GetHeapLower();
|
return Sortix::Memory::GetHeapUpper() - Sortix::Memory::GetHeapLower();
|
||||||
}
|
}
|
||||||
|
|
||||||
void FreeMemory(addr_t where, size_t bytes)
|
static void FreeMemory(addr_t where, size_t bytes)
|
||||||
{
|
{
|
||||||
assert(Sortix::Page::IsAligned(where + bytes));
|
assert(Sortix::Page::IsAligned(where + bytes));
|
||||||
|
|
||||||
|
@ -101,7 +97,7 @@ namespace Maxsi
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool AllocateMemory(addr_t where, size_t bytes)
|
static bool AllocateMemory(addr_t where, size_t bytes)
|
||||||
{
|
{
|
||||||
assert(Sortix::Page::IsAligned(where + bytes));
|
assert(Sortix::Page::IsAligned(where + bytes));
|
||||||
|
|
||||||
|
@ -130,7 +126,7 @@ namespace Maxsi
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExtendHeap(size_t bytesneeded)
|
static bool ExtendHeap(size_t bytesneeded)
|
||||||
{
|
{
|
||||||
#ifdef HEAP_GROWS_DOWNWARDS
|
#ifdef HEAP_GROWS_DOWNWARDS
|
||||||
addr_t newwilderness = wilderness - bytesneeded;
|
addr_t newwilderness = wilderness - bytesneeded;
|
||||||
|
@ -141,7 +137,7 @@ namespace Maxsi
|
||||||
return AllocateMemory(newwilderness, bytesneeded);
|
return AllocateMemory(newwilderness, bytesneeded);
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
addr_t GetHeapStart()
|
static addr_t GetHeapStart()
|
||||||
{
|
{
|
||||||
addr_t base = (addr_t) sbrk(0);
|
addr_t base = (addr_t) sbrk(0);
|
||||||
addr_t unaligned = base % ALIGNMENT;
|
addr_t unaligned = base % ALIGNMENT;
|
||||||
|
@ -153,13 +149,13 @@ namespace Maxsi
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t GetHeapMaxSize()
|
static size_t GetHeapMaxSize()
|
||||||
{
|
{
|
||||||
// TODO: A bit of a hack!
|
// TODO: A bit of a hack!
|
||||||
return SIZE_MAX;
|
return SIZE_MAX;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ExtendHeap(size_t bytesneeded)
|
static bool ExtendHeap(size_t bytesneeded)
|
||||||
{
|
{
|
||||||
void* newheapend = sbrk(bytesneeded);
|
void* newheapend = sbrk(bytesneeded);
|
||||||
return newheapend != (void*) -1UL;
|
return newheapend != (void*) -1UL;
|
||||||
|
@ -274,7 +270,7 @@ namespace Maxsi
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
// A trailer ro every chunk providing meta-information.
|
// A trailer to every chunk providing meta-information.
|
||||||
struct Trailer
|
struct Trailer
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -363,7 +359,7 @@ namespace Maxsi
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void InsertChunk(Chunk* chunk)
|
static void InsertChunk(Chunk* chunk)
|
||||||
{
|
{
|
||||||
// Insert the chunk into the right bin.
|
// Insert the chunk into the right bin.
|
||||||
size_t binindex = BSR(chunk->size);
|
size_t binindex = BSR(chunk->size);
|
||||||
|
@ -379,7 +375,7 @@ namespace Maxsi
|
||||||
assert(chunk->IsSane());
|
assert(chunk->IsSane());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ValidateHeap()
|
static bool ValidateHeap()
|
||||||
{
|
{
|
||||||
bool foundbin[NUMBINS];
|
bool foundbin[NUMBINS];
|
||||||
for ( size_t i = 0; i < NUMBINS; i++ ) { foundbin[i] = false; }
|
for ( size_t i = 0; i < NUMBINS; i++ ) { foundbin[i] = false; }
|
||||||
|
@ -421,7 +417,7 @@ namespace Maxsi
|
||||||
// This is where the actual memory allocation algorithm starts.
|
// This is where the actual memory allocation algorithm starts.
|
||||||
//
|
//
|
||||||
|
|
||||||
void Init()
|
extern "C" void _init_heap()
|
||||||
{
|
{
|
||||||
heapstart = GetHeapStart();
|
heapstart = GetHeapStart();
|
||||||
heapmaxsize = GetHeapMaxSize();
|
heapmaxsize = GetHeapMaxSize();
|
||||||
|
@ -435,15 +431,10 @@ namespace Maxsi
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
extern "C" void _init_heap()
|
|
||||||
{
|
|
||||||
Init();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Attempts to expand the wilderness such that it contains at least
|
// Attempts to expand the wilderness such that it contains at least
|
||||||
// bytesneeded bytes. This is done by mapping new pages onto into the
|
// bytesneeded bytes. This is done by mapping new pages onto into the
|
||||||
// virtual address-space.
|
// virtual address-space.
|
||||||
bool ExpandWilderness(size_t bytesneeded)
|
static bool ExpandWilderness(size_t bytesneeded)
|
||||||
{
|
{
|
||||||
if ( bytesneeded <= wildernesssize ) { return true; }
|
if ( bytesneeded <= wildernesssize ) { return true; }
|
||||||
|
|
||||||
|
@ -587,7 +578,7 @@ namespace Maxsi
|
||||||
return (void*) result;
|
return (void*) result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsLeftmostChunk(Chunk* chunk)
|
static bool IsLeftmostChunk(Chunk* chunk)
|
||||||
{
|
{
|
||||||
#ifdef HEAP_GROWS_DOWNWARDS
|
#ifdef HEAP_GROWS_DOWNWARDS
|
||||||
return (addr_t) chunk <= wilderness + wildernesssize;
|
return (addr_t) chunk <= wilderness + wildernesssize;
|
||||||
|
@ -596,7 +587,7 @@ namespace Maxsi
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IsRightmostChunk(Chunk* chunk)
|
static bool IsRightmostChunk(Chunk* chunk)
|
||||||
{
|
{
|
||||||
#ifdef HEAP_GROWS_DOWNWARDS
|
#ifdef HEAP_GROWS_DOWNWARDS
|
||||||
return heapstart <= (addr_t) chunk + chunk->size;
|
return heapstart <= (addr_t) chunk + chunk->size;
|
||||||
|
@ -606,7 +597,7 @@ namespace Maxsi
|
||||||
}
|
}
|
||||||
|
|
||||||
// Removes a chunk from its bin.
|
// Removes a chunk from its bin.
|
||||||
void UnlinkChunk(Chunk* chunk)
|
static void UnlinkChunk(Chunk* chunk)
|
||||||
{
|
{
|
||||||
assert(chunk->IsSane());
|
assert(chunk->IsSane());
|
||||||
Trailer* trailer = chunk->GetTrailer();
|
Trailer* trailer = chunk->GetTrailer();
|
||||||
|
@ -636,7 +627,7 @@ namespace Maxsi
|
||||||
}
|
}
|
||||||
|
|
||||||
// Transforms a chunk and its neighbors into a single chunk if possible.
|
// Transforms a chunk and its neighbors into a single chunk if possible.
|
||||||
void UnifyNeighbors(Chunk** chunk)
|
static void UnifyNeighbors(Chunk** chunk)
|
||||||
{
|
{
|
||||||
if ( !IsLeftmostChunk(*chunk) )
|
if ( !IsLeftmostChunk(*chunk) )
|
||||||
{
|
{
|
||||||
|
@ -726,5 +717,3 @@ namespace Maxsi
|
||||||
free(ptr);
|
free(ptr);
|
||||||
return newptr;
|
return newptr;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue