TempleOSCDV4.03.ISO

This commit is contained in:
Terry A. Davis authored and minexew committed 2018-09-08 21:47:01 +02:00
1 parent 1541d7328d
commit 0f1a4a5d65
115 files changed
+5618 -4863

No files matched your search

+3 -16
View File
@@ -1,30 +1,17 @@
CHeapCtrl *HeapCtrlInit(CHeapCtrl *hc=NULL,CTask *task=NULL,CBlkPool *bp)
{//Called by $LK,"Adam Task",A="FF:::/Kernel/KStart.CPP,CHeapCtrl.bp"$.
{//See $LK,"HeapLog",A="MN:HeapLog"$() for an example.
//Duplicated for $LK,"Adam Task",A="FF:::/Kernel/KStart.CPP,CHeapCtrl.bp"$.
if (!hc)
hc=ACAlloc(sizeof(CHeapCtrl));
hc->hc_signature=HEAP_CTRL_SIGNATURE_VAL;
hc->mem_task=task;
hc->bp=bp;
QueInit(&hc->next_mem_blk);
hc->last_mergable=NULL;
hc->next_um=hc->last_um=(&hc->next_um)(U8 *)-offset(CMemUsed.next);
return hc;
}
CHeapCtrl *HeapCtrlBPInit(CBlkPool *bp,I64 pages512)
{//Make mem chunk into HeapCtrl and BlkPool.
I64 num;
CMemBlk *m;
CHeapCtrl *hc;
MemSet(bp,0,sizeof(CBlkPool)+sizeof(CHeapCtrl));
hc=HeapCtrlInit(bp(U8 *)+sizeof(CBlkPool),,bp);
m=(bp(U8 *)+sizeof(CBlkPool)+sizeof(CHeapCtrl)+MEM_PAGE_SIZE-1)&
~(MEM_PAGE_SIZE-1);
num=(bp(U8 *)+pages512<<MEM_PAGE_BITS-m(U8 *))>>MEM_PAGE_BITS;
bp->alloced_u8s=(pages512-num)<<MEM_PAGE_BITS;
BlkPoolAdd(bp,m,num);
return hc;
}
U0 HeapCtrlDel(CHeapCtrl *hc)
{//Free all blks alloced to a HeapCtrl.
CMemBlk *m,*m1;
+52 -38
View File
@@ -55,16 +55,14 @@ _MALLOC::
SHR RAX,MEM_PAGE_BITS
PUSH RDX //preserve HeapCtrl
PUSH RAX //num blks (we must pass address of this)
MOV RAX,RSP
PUSH RDX
PUSH RAX //addr of num blks
PUSH RAX
CALL &Mem512TaskAlloc
MOV RSI,RAX
POP RAX //blks that were alloced
POP RDX
TEST RSI,RSI
TEST RAX,RAX
JZ @@45 //Out of memory
MOV RSI,RAX
MOV EAX,U32 CMemBlk.pages[RSI]
SHL RAX,MEM_PAGE_BITS
SUB RAX,sizeof(CMemBlk)
@@ -77,21 +75,17 @@ _MALLOC::
@@40: MOV RBX,RSI
MOV RSI,U64 CMemUnused.next[RBX]
TEST RSI,RSI
JNZ @@55
JNZ I32 @@60
PUSH RAX //-**** save byte size
ADD RAX,16*MEM_PAGE_SIZE-1
SHR RAX,MEM_PAGE_BITS
PUSH RDX //preserve HeapCtrl
PUSH RAX //num blks (we must pass address of this)
MOV RAX,RSP
PUSH RDX
PUSH RAX //addr of num blks
PUSH RAX
CALL &Mem512TaskAlloc
MOV RSI,RAX
POP RAX //blks that were alloced
POP RDX
TEST RSI,RSI
TEST RAX,RAX
JNZ @@50
//Out of memory
@@ -104,8 +98,28 @@ _MALLOC::
CALL I32 &throw
JMP I32 MALLOC_FINAL_EXIT //Never gets here, hopefully.
@@50: LEA RSI,U64 sizeof(CMemBlk)[RSI]
@@50: MOV RSI,RAX
MOV EAX,U32 CMemBlk.pages[RSI]
SHL RAX,MEM_PAGE_BITS
//Can it be combined with last chunk? (Never Free these chunks.)
MOV RDI,U64 CHeapCtrl.last_mergable[RDX]
LEA RBX,U64 [RSI+RAX]
CMP RDI,RBX
JNE @@55
PUSH RAX
MOV EAX,U32 CMemBlk.pages[RDI]
ADD U32 CMemBlk.pages[RSI],EAX
//QueRem
MOV RAX,U64 CMemBlk.next[RDI]
MOV RBX,U64 CMemBlk.last[RDI]
MOV U64 CMemBlk.last[RAX],RBX
MOV U64 CMemBlk.next[RBX],RAX
POP RAX
@@55: MOV U64 CHeapCtrl.last_mergable[RDX],RSI
LEA RSI,U64 sizeof(CMemBlk)[RSI]
SUB RAX,sizeof(CMemBlk)
LEA RBX,U64 CHeapCtrl.malloc_free_lst-CMemUnused.next[RDX]
MOV RDI,U64 CMemUnused.next[RBX]
@@ -113,22 +127,22 @@ _MALLOC::
MOV U64 CMemUnused.size[RSI],RAX
MOV U64 CMemUnused.next[RBX],RSI
POP RAX //+****
JMP @@65
@@55: CMP U64 CMemUnused.size[RSI],RAX
JB @@40
JNE @@65
JMP @@70
@@60: CMP U64 CMemUnused.size[RSI],RAX
JB I32 @@40
JNE @@70
@@60: MOV RDI,U64 CMemUnused.next[RSI]
@@65: MOV RDI,U64 CMemUnused.next[RSI]
MOV U64 CMemUnused.next[RBX],RDI
JMP MALLOC_ALMOST_DONE
@@65: SUB U64 CMemUnused.size[RSI],RAX //UPDATE FREE ENTRY
@@70: SUB U64 CMemUnused.size[RSI],RAX //UPDATE FREE ENTRY
CMP U64 CMemUnused.size[RSI],sizeof(CMemUnused)
JAE @@70 //take from top of block
JAE @@75 //take from top of block
ADD U64 CMemUnused.size[RSI],RAX //doesn't fit, undo
JMP I32 @@40
@@70: ADD RSI,U64 CMemUnused.size[RSI]
@@75: ADD RSI,U64 CMemUnused.size[RSI]
MALLOC_ALMOST_DONE:
//RSI=result-CMemUsed.size
@@ -150,21 +164,21 @@ MALLOC_ALMOST_DONE:
MOV RDX,U64 [SYS_HEAP_LIMIT]
MOV RDI,U64 SF_RIP[RBP]
CMP RDI,RDX
JB @@75
XOR RDI,RDI
MOV U64 CMemUsed.caller1[RSI],RDI
JMP @@85
@@75: MOV U64 CMemUsed.caller1[RSI],RDI
MOV RDI,U64 SF_RBP[RBP]
CMP RDI,RDX
JB @@80
XOR RDI,RDI
JMP @@85
@@80: MOV RDI,U64 SF_RIP[RDI]
MOV U64 CMemUsed.caller1[RSI],RDI
JMP @@90
@@80: MOV U64 CMemUsed.caller1[RSI],RDI
MOV RDI,U64 SF_RBP[RBP]
CMP RDI,RDX
JB @@85
XOR RDI,RDI
@@85: MOV U64 CMemUsed.caller2[RSI],RDI
JMP @@90
@@85: MOV RDI,U64 SF_RIP[RDI]
CMP RDI,RDX
JB @@90
XOR RDI,RDI
@@90: MOV U64 CMemUsed.caller2[RSI],RDI
POP RDX
#endif
@@ -177,19 +191,19 @@ MALLOC_ALMOST_DONE:
LEA RAX,U64 CMemUsed.start[RSI]
TEST U8 [SYS_SEMAS+SYS_SEMA_HEAPLOG_ACTIVE*SEMA_STRUCT_SIZE],1
JZ @@100
JZ @@105
PUSH RAX
PUSH RAX
MOV RAX,U64 [SYS_EXTERN_TABLE]
MOV RAX,U64 EXT_HEAPLOG_MALLOC*8[RAX]
TEST RAX,RAX
JZ @@90
JZ @@95
CALL RAX
JMP @@95
@@90: ADD RSP,8
@@95: POP RAX
JMP @@100
@@95: ADD RSP,8
@@100: POP RAX
@@100: TEST U8 [SYS_HEAP_INIT_FLAG],1
@@105: TEST U8 [SYS_HEAP_INIT_FLAG],1
JZ MALLOC_FINAL_EXIT
PUSH RAX
-1
View File
@@ -1,6 +1,5 @@
#exe {Cd(__DIR__);};
#include "PageTables"
#include "Mem2Meg"
#include "Mem512"
#include "BlkPool"
#include "MAllocFree"
-180
View File
@@ -1,180 +0,0 @@
U8 *Mem2MegAlloc(I64 *_pages2Meg,CBlkPool *bp=NULL)
{/*Alloc 2Meg pages from BlkPool. Don't link to task.
(Linking to a task means they will be freed when the task dies.)
It might give you more than you asked for
so a ptr to a page count is passed.
Return: NULL if out of memory.
*/
I64 i,j,*pte,num=*_pages2Meg;
CMemBlk *result=NULL,*m,*m1;
if (!bp) bp=sys_code_bp;
PUSHFD
CLI
while (LBts(&bp->locked_flags,BPlf_LOCKED))
PAUSE
num<<=21-MEM_PAGE_BITS;
m=&bp->mem_free_2meg_lst;
while (TRUE) {
if (!(result=m->next))
break;
if (result->pages<num)
m=result;
else {
if (result->pages==num) {
m->next=result->next;
goto am_done;
} else {
result->pages-=num;
result(U8 *)+=result->pages<<MEM_PAGE_BITS;
result->pages=num;
goto am_done;
}
}
}
m=&bp->mem_free_lst;
while (TRUE) {
if (!(result=m->next)) {
num=0;
result=NULL; //Out of memory
goto am_done;
}
if (result->pages<num)
m=result;
else {
if (result->pages==num) {
if (result(U8 *)&0x1FFFFF)
m=result;
else {
m->next=result->next;
goto am_done;
}
} else {
if (i=(result(U8 *)&0x1FFFFF)>>MEM_PAGE_BITS) {
j=1<<(21-MEM_PAGE_BITS)-i;
if (result->pages<num+j)
m=result;
else if (result->pages==num+j) {
result->pages-=num;
result(U8 *)+=result->pages<<MEM_PAGE_BITS;
result->pages=num;
goto am_done;
} else {
m1=result;
result(U8 *)+=j<<MEM_PAGE_BITS;
result->pages=num;
m=result(U8 *)+num<<MEM_PAGE_BITS;
m->pages=m1->pages-num-j;
m1->pages=j;
m->next=m1->next;
m1->next=m;
m->mb_signature=MBS_UNUSED_SIGNATURE_VAL;
goto am_done;
}
} else {
m=m->next=result(U8 *)+num<<MEM_PAGE_BITS;
m->next=result->next;
m->pages=result->pages-num;
m->mb_signature=MBS_UNUSED_SIGNATURE_VAL;
result->pages=num;
goto am_done;
}
}
}
}
am_done:
i=num<<MEM_PAGE_BITS;
bp->used_u8s+=i;
num>>=21-MEM_PAGE_BITS;
*_pages2Meg=num;
m=result;
m1=m(U8 *)+i;
while (m<m1) {
pte=MemPageTable(m);
*pte &= ~0x18;
InvlPg(m);
m(U8 *)+=MemPageSize(m);
}
LBtr(&bp->locked_flags,BPlf_LOCKED);
POPFD
return result;
}
U8 *Mem2MegUncachedAlloc(I64 *_pages2Meg,CBlkPool *bp=NULL)
{/*Alloc 2Meg pages from BlkPool. Don't link to task.
(Linking to a task means they will be freed when the task dies.)
It will be marked uncached. It might give you more than you asked for
so a ptr to a page count is passed.
Return: NULL if out of memory.
*/
CMemBlk *result,*m,*m1;
I64 num=*_pages2Meg,*pte;
if (result=Mem2MegAlloc(_pages2Meg,bp)) {
num=*_pages2Meg;
m=result;
m1=m(U8 *)+num<<21;
while (m<m1) {
pte=MemPageTable(m);
*pte= *pte& ~0x18 |0x10;
InvlPg(m);
m(U8 *)+=MemPageSize(m);
}
}
return result;
}
U8 *Mem2MegWriteThruAlloc(I64 *_pages2Meg,CBlkPool *bp=NULL)
{/*Alloc 2Meg pages from BlkPool. Don't link to task.
(Linking to a task means they will be freed when the task dies.)
It will be marked write-through. It might give you more than you asked for
so a ptr to a page count is passed.
Return: NULL if out of memory.
*/
CMemBlk *result,*m,*m1;
I64 num=*_pages2Meg,*pte;
if (result=Mem2MegAlloc(_pages2Meg,bp)) {
num=*_pages2Meg;
m=result;
m1=m(U8 *)+num<<21;
while (m<m1) {
pte=MemPageTable(m);
*pte= *pte& ~0x18 |8;
InvlPg(m);
m(U8 *)+=MemPageSize(m);
}
}
return result;
}
U0 Mem2MegFree(CMemBlk *m,I64 pages2Meg,CBlkPool *bp=NULL)
{//Return non-task 2Meg pages to BlkPool.
I64 *pte;
CMemBlk *m1,*m2;
if (m) {
if (!bp) bp=sys_code_bp;
m2=m;
m1=m(U8 *)+pages2Meg<<21;
while (m2<m1) {
pte=MemPageTable(m2);
*pte=*pte & ~0x18;
InvlPg(m2);
m2(U8 *)+=MemPageSize(m2);
}
PUSHFD
CLI
while (LBts(&bp->locked_flags,BPlf_LOCKED))
PAUSE
m->mb_signature=MBS_UNUSED_SIGNATURE_VAL;
m->pages=pages2Meg<<(21-MEM_PAGE_BITS);
bp->used_u8s-=pages2Meg<<21;
m->next=bp->mem_free_2meg_lst;
bp->mem_free_2meg_lst=m;
LBtr(&bp->locked_flags,BPlf_LOCKED);
POPFD
}
}
+22 -24
View File
@@ -8,7 +8,7 @@ U0 SysBadMAlloc(I64 *ptr)
Dbg("Bad MAlloc:",ptr);
}
U8 *Mem512Alloc(I64 *_pages512,CBlkPool *bp=NULL)
U8 *Mem512Alloc(I64 pages512,CBlkPool *bp=NULL)
{/*Alloc 512Byte pages from BlkPool. Don't link to task.
(Linking to a task means they will be freed when the task dies.)
It might give you more than you asked for
@@ -17,15 +17,15 @@ so a ptr to a page count is passed.
Return: NULL if out of memory.
*/
CMemBlk *result=NULL,*m;
I64 i,num=*_pages512;
I64 i;
if (!bp) bp=sys_code_bp;
PUSHFD
CLI
while (LBts(&bp->locked_flags,BPlf_LOCKED))
PAUSE
if (num<MEM_FREE_PAGE_HASH_SIZE) {
if (result=bp->free_page_hash[num]) {
bp->free_page_hash[num]=result->next;
if (pages512<MEM_FREE_PAGE_HASH_SIZE) {
if (result=bp->free_page_hash[pages512]) {
bp->free_page_hash[pages512]=result->next;
goto at_done;
}
i=Bsr(MEM_FREE_PAGE_HASH_SIZE)+1;
@@ -35,9 +35,9 @@ Return: NULL if out of memory.
//we wouldn't want to round to the next
//power of two if a power of two was requested.
//So we use a little more than a power of two.
num-=MEM_EXTRA_HASH2_PAGES;
i=Bsr(num)+1;
num=1<<i+MEM_EXTRA_HASH2_PAGES;
pages512-=MEM_EXTRA_HASH2_PAGES;
i=Bsr(pages512)+1;
pages512=1<<i+MEM_EXTRA_HASH2_PAGES;
if (result=bp->free_page_hash2[i]) {
bp->free_page_hash2[i]=result->next;
goto at_done;
@@ -50,48 +50,47 @@ Return: NULL if out of memory.
//freed larger size block... and, screw-it, return the whole thing.
do {
if (result=bp->free_page_hash2[++i]) {
num=1<<i+MEM_EXTRA_HASH2_PAGES;
pages512=1<<i+MEM_EXTRA_HASH2_PAGES;
bp->free_page_hash2[i]=result->next;
goto at_done;
}
} while (i<64-MEM_PAGE_BITS-1);
num=0;
pages512=0;
result=NULL; //Out of memory
goto at_done;
}
if (result->pages<num)
if (result->pages<pages512)
m=result;
else {
if (result->pages==num) {
if (result->pages==pages512) {
m->next=result->next;
goto at_done;
} else {
result->pages-=num;
result->pages-=pages512;
result(U8 *)+=result->pages<<MEM_PAGE_BITS;
result->pages=num;
result->pages=pages512;
goto at_done;
}
}
}
at_done:
*_pages512=num;
bp->used_u8s+=num<<MEM_PAGE_BITS;
bp->used_u8s+=pages512<<MEM_PAGE_BITS;
LBtr(&bp->locked_flags,BPlf_LOCKED);
POPFD
return result;
}
U0 Mem512Free(CMemBlk *m,I64 pages512,CBlkPool *bp=NULL)
U0 Mem512Free(CMemBlk *m,CBlkPool *bp=NULL)
{//Return non-task 512Byte pages to BlkPool.
I64 i;
I64 i,pages512;
if (m) {
if (!bp) bp=sys_code_bp;
PUSHFD
CLI
while (LBts(&bp->locked_flags,BPlf_LOCKED))
PAUSE
pages512=m->pages;
m->mb_signature=MBS_UNUSED_SIGNATURE_VAL;
m->pages=pages512;
bp->used_u8s-=pages512<<MEM_PAGE_BITS;
if (pages512<MEM_FREE_PAGE_HASH_SIZE) {
m->next=bp->free_page_hash[pages512];
@@ -112,19 +111,18 @@ U0 Mem512Free(CMemBlk *m,I64 pages512,CBlkPool *bp=NULL)
}
}
CMemBlk *Mem512TaskAlloc(I64 *_pages512,CHeapCtrl *hc)
CMemBlk *Mem512TaskAlloc(I64 pages512,CHeapCtrl *hc)
{/*hc must be locked. Interrupts should probably be off
Currently, this is only called from $LK,"MAlloc",A="MN:MAlloc"$().
Return: NULL if out of memory.
*/
CMemBlk *result;
I64 threshold,cnt,size;
CMemUnused *uum,**_uum,**_ptr;
if (result=Mem512Alloc(_pages512,hc->bp)) {
if (result=Mem512Alloc(pages512,hc->bp)) {
QueIns(result,hc->last_mem_blk);
result->mb_signature=MBS_USED_SIGNATURE_VAL;
hc->alloced_u8s+=*_pages512<<MEM_PAGE_BITS;
hc->alloced_u8s+=result->pages<<MEM_PAGE_BITS;
//Tidy-up free lst (Move into heap hash)
//because if free lst gets long, delay causes crash
@@ -162,7 +160,7 @@ U0 Mem512TaskFree(CMemBlk *m,CHeapCtrl *hc)
else {
QueRem(m);
hc->alloced_u8s-=m->pages<<MEM_PAGE_BITS;
Mem512Free(m,m->pages,hc->bp);
Mem512Free(m,hc->bp);
}
POPFD
}