git.ucc.asn.au
/
tpg
/
acess2.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Added -mno-red-zone
[tpg/acess2.git]
/
Kernel
/
heap.c
diff --git
a/Kernel/heap.c
b/Kernel/heap.c
index
9d282b9
..
1863f56
100644
(file)
--- a/
Kernel/heap.c
+++ b/
Kernel/heap.c
@@
-10,10
+10,8
@@
#define DEBUG_TRACE 0
// === CONSTANTS ===
#define DEBUG_TRACE 0
// === CONSTANTS ===
-#define HEAP_BASE 0xE0800000
-#define HEAP_MAX 0xF0000000 // 120MiB, Plenty
#define HEAP_INIT_SIZE 0x8000 // 32 KiB
#define HEAP_INIT_SIZE 0x8000 // 32 KiB
-#define BLOCK_SIZE (sizeof(void*)) // 8 Machine Words
+#define BLOCK_SIZE (sizeof(void*))
*8
// 8 Machine Words
#define COMPACT_HEAP 0 // Use 4 byte header?
#define FIRST_FIT 0
#define COMPACT_HEAP 0 // Use 4 byte header?
#define FIRST_FIT 0
@@
-22,20
+20,20
@@
#define MAGIC_USED 0x862B0505 // MAGIC_FOOT ^ MAGIC_FREE
// === PROTOTYPES ===
#define MAGIC_USED 0x862B0505 // MAGIC_FOOT ^ MAGIC_FREE
// === PROTOTYPES ===
-void Heap_Install();
+void Heap_Install(
void
);
void *Heap_Extend(int Bytes);
void *Heap_Merge(tHeapHead *Head);
void *malloc(size_t Bytes);
void free(void *Ptr);
void *Heap_Extend(int Bytes);
void *Heap_Merge(tHeapHead *Head);
void *malloc(size_t Bytes);
void free(void *Ptr);
-void Heap_Dump();
+void Heap_Dump(
void
);
// === GLOBALS ===
// === GLOBALS ===
-
int
glHeap;
+
tSpinlock
glHeap;
void *gHeapStart;
void *gHeapEnd;
// === CODE ===
void *gHeapStart;
void *gHeapEnd;
// === CODE ===
-void Heap_Install()
+void Heap_Install(
void
)
{
gHeapStart = (void*)MM_KHEAP_BASE;
gHeapEnd = (void*)MM_KHEAP_BASE;
{
gHeapStart = (void*)MM_KHEAP_BASE;
gHeapEnd = (void*)MM_KHEAP_BASE;
@@
-53,18
+51,18
@@
void *Heap_Extend(int Bytes)
tHeapFoot *foot;
// Bounds Check
tHeapFoot *foot;
// Bounds Check
- if( (
Uint
)gHeapEnd == MM_KHEAP_MAX )
+ if( (
tVAddr
)gHeapEnd == MM_KHEAP_MAX )
return NULL;
// Bounds Check
return NULL;
// Bounds Check
- if( (
Uint
)gHeapEnd + ((Bytes+0xFFF)&~0xFFF) > MM_KHEAP_MAX ) {
- Bytes = MM_KHEAP_MAX - (
Uint
)gHeapEnd;
+ if( (
tVAddr
)gHeapEnd + ((Bytes+0xFFF)&~0xFFF) > MM_KHEAP_MAX ) {
+ Bytes = MM_KHEAP_MAX - (
tVAddr
)gHeapEnd;
return NULL;
}
// Heap expands in pages
for(i=0;i<(Bytes+0xFFF)>>12;i++)
return NULL;
}
// Heap expands in pages
for(i=0;i<(Bytes+0xFFF)>>12;i++)
- MM_Allocate( (
Uint
)gHeapEnd+(i<<12) );
+ MM_Allocate( (
tVAddr
)gHeapEnd+(i<<12) );
// Increas heap end
gHeapEnd += i << 12;
// Increas heap end
gHeapEnd += i << 12;
@@
-76,7
+74,6
@@
void *Heap_Extend(int Bytes)
foot->Head = head;
foot->Magic = MAGIC_FOOT;
foot->Head = head;
foot->Magic = MAGIC_FOOT;
- //Log(" Heap_Extend: head = %p", head);
return Heap_Merge(head); // Merge with previous block
}
return Heap_Merge(head); // Merge with previous block
}
@@
-97,7
+94,7
@@
void *Heap_Merge(tHeapHead *Head)
// Merge Left (Down)
foot = (void*)( (Uint)Head - sizeof(tHeapFoot) );
// Merge Left (Down)
foot = (void*)( (Uint)Head - sizeof(tHeapFoot) );
- if( ((Uint)foot < (Uint)gHeapEnd && (Uint)foot >
HEAP_BASE
)
+ if( ((Uint)foot < (Uint)gHeapEnd && (Uint)foot >
(Uint)gHeapStart
)
&& foot->Head->Magic == MAGIC_FREE) {
foot->Head->Size += Head->Size; // Increase size
thisFoot->Head = foot->Head; // Change backlink
&& foot->Head->Magic == MAGIC_FREE) {
foot->Head->Size += Head->Size; // Increase size
thisFoot->Head = foot->Head; // Change backlink
@@
-139,6
+136,8
@@
void *malloc(size_t Bytes)
// Get required size
Bytes = (Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot) + BLOCK_SIZE-1) & ~(BLOCK_SIZE-1);
// Get required size
Bytes = (Bytes + sizeof(tHeapHead) + sizeof(tHeapFoot) + BLOCK_SIZE-1) & ~(BLOCK_SIZE-1);
+ //if(glHeap)
+ // Debug("glHeap = %i", glHeap);
// Lock Heap
LOCK(&glHeap);
// Lock Heap
LOCK(&glHeap);
@@
-150,11
+149,11
@@
void *malloc(size_t Bytes)
{
// Alignment Check
if( head->Size & (BLOCK_SIZE-1) ) {
{
// Alignment Check
if( head->Size & (BLOCK_SIZE-1) ) {
+ RELEASE(&glHeap); // Release spinlock
#if WARNINGS
Log_Warning("Heap", "Size of heap address %p is invalid not aligned (0x%x)", head, head->Size);
Heap_Dump();
#endif
#if WARNINGS
Log_Warning("Heap", "Size of heap address %p is invalid not aligned (0x%x)", head, head->Size);
Heap_Dump();
#endif
- RELEASE(&glHeap);
return NULL;
}
return NULL;
}
@@
-162,11
+161,11
@@
void *malloc(size_t Bytes)
if(head->Magic == MAGIC_USED) continue;
// Error check
if(head->Magic != MAGIC_FREE) {
if(head->Magic == MAGIC_USED) continue;
// Error check
if(head->Magic != MAGIC_FREE) {
+ RELEASE(&glHeap); // Release spinlock
#if WARNINGS
Log_Warning("Heap", "Magic of heap address %p is invalid (0x%x)", head, head->Magic);
Heap_Dump();
#endif
#if WARNINGS
Log_Warning("Heap", "Magic of heap address %p is invalid (0x%x)", head, head->Magic);
Heap_Dump();
#endif
- RELEASE(&glHeap); // Release spinlock
return NULL;
}
return NULL;
}
@@
-208,6
+207,7
@@
void *malloc(size_t Bytes)
}
// Check size
if(best->Size == Bytes) {
}
// Check size
if(best->Size == Bytes) {
+ best->Magic = MAGIC_USED; // Mark block as used
RELEASE(&glHeap); // Release spinlock
#if DEBUG_TRACE
Log("[Heap ] Malloc'd %p (%i bytes), returning to %p", best->Data, best->Size, __builtin_return_address(0));
RELEASE(&glHeap); // Release spinlock
#if DEBUG_TRACE
Log("[Heap ] Malloc'd %p (%i bytes), returning to %p", best->Data, best->Size, __builtin_return_address(0));
@@
-348,8
+348,15
@@
void *realloc(void *__ptr, size_t __size)
if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
{
Uint size = nextHead->Size + head->Size;
if(nextHead->Magic == MAGIC_FREE && nextHead->Size+head->Size >= newSize)
{
Uint size = nextHead->Size + head->Size;
+ // Inexact fit, split things up
+ if(size > newSize)
+ {
+ // TODO
+ Warning("[Heap ] TODO: Space efficient realloc when new size is smaller");
+ }
+
// Exact fit
// Exact fit
- if(size
=
= newSize)
+ if(size
>
= newSize)
{
Uint oldDataSize;
// Set 1st (new/lower) header
{
Uint oldDataSize;
// Set 1st (new/lower) header
@@
-363,11
+370,27
@@
void *realloc(void *__ptr, size_t __size)
// Clear old header
head->Size = 0;
head->Magic = 0;
// Clear old header
head->Size = 0;
head->Magic = 0;
+ // Copy data
memcpy(nextHead->Data, __ptr, oldDataSize);
memcpy(nextHead->Data, __ptr, oldDataSize);
+ // Return
+ return nextHead->Data;
}
}
+ // On to the expensive then
}
}
- return NULL;
+ // Well, darn
+ nextHead = malloc( __size );
+ nextHead -= 1;
+
+ memcpy(
+ nextHead->Data,
+ __ptr,
+ head->Size - sizeof(tHeapFoot) - sizeof(tHeapHead)
+ );
+
+ free(__ptr);
+
+ return nextHead->Data;
}
/**
}
/**
@@
-405,7
+428,7
@@
int IsHeap(void *Ptr)
}
#if WARNINGS
}
#if WARNINGS
-void Heap_Dump()
+void Heap_Dump(
void
)
{
tHeapHead *head;
tHeapFoot *foot;
{
tHeapHead *head;
tHeapFoot *foot;
@@
-414,7
+437,8
@@
void Heap_Dump()
while( (Uint)head < (Uint)gHeapEnd )
{
foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
while( (Uint)head < (Uint)gHeapEnd )
{
foot = (void*)( (Uint)head + head->Size - sizeof(tHeapFoot) );
- Log_Log("Heap", "%p (0x%x): 0x%08lx 0x%lx", head, MM_GetPhysAddr((Uint)head), head->Size, head->Magic);
+ Log_Log("Heap", "%p (0x%x): 0x%08lx 0x%lx",
+ head, MM_GetPhysAddr((Uint)head), head->Size, head->Magic);
Log_Log("Heap", "%p 0x%lx", foot->Head, foot->Magic);
Log_Log("Heap", "");
Log_Log("Heap", "%p 0x%lx", foot->Head, foot->Magic);
Log_Log("Heap", "");
UCC
git Repository :: git.ucc.asn.au