winedos: Implement MCB.

This commit is contained in:
Markus Amsler 2005-12-12 11:50:01 +01:00 committed by Alexandre Julliard
parent ac459e7ea0
commit 19d60d3c5a
1 changed files with 185 additions and 226 deletions

View File

@ -51,38 +51,47 @@ WINE_DEFAULT_DEBUG_CHANNEL(dosmem);
static char *DOSMEM_dosmem; static char *DOSMEM_dosmem;
static char *DOSMEM_sysmem; static char *DOSMEM_sysmem;
/* use 2 low bits of 'size' for the housekeeping */ /*
#define DM_BLOCK_DEBUG 0xABE00000 * Memory Control Block (MCB) definition
#define DM_BLOCK_TERMINAL 0x00000001 * FIXME: implement Allocation Strategy
#define DM_BLOCK_FREE 0x00000002 */
#define DM_BLOCK_MASK 0x001FFFFC
#define MCB_DUMP(mc) \
TRACE ("MCB_DUMP base=%p type=%02xh psp=%04xh size=%04xh\n", mc, mc->type, mc->psp , mc->size )
#define MCB_NEXT(mc) \
(MCB*) ((mc->type==MCB_TYPE_LAST) ? NULL : (char*)(mc) + ((mc->size + 1) << 4) )
/* FIXME: should we check more? */
#define MCB_VALID(mc) \
((mc->type==MCB_TYPE_NORMAL) || (mc->type==MCB_TYPE_LAST))
#define MCB_TYPE_NORMAL 0x4d
#define MCB_TYPE_LAST 0x5a
#define MCB_PSP_DOS 0x0060
#define MCB_PSP_FREE 0
#include "pshpack1.h"
typedef struct {
BYTE type;
WORD psp; /* segment of owner psp */
WORD size; /* in paragraphs */
BYTE pad[3];
BYTE name[8];
} MCB;
#include "poppack.h"
/* /*
#define __DOSMEM_DEBUG__ #define __DOSMEM_DEBUG__
*/ */
typedef struct {
unsigned size;
} dosmem_entry;
typedef struct {
unsigned blocks;
unsigned free;
} dosmem_info;
static inline dosmem_entry* next_block(dosmem_entry* block)
{
return (dosmem_entry*)((char*)block +
sizeof(dosmem_entry) + (block->size & DM_BLOCK_MASK));
}
#define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */ #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
#define VM_STUB_SEGMENT 0xf000 /* BIOS segment */ #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
/* FIXME: this should be moved to the LOL, and the whole allocation strategy /* FIXME: this should be moved to the LOL */
* should use real MCB static MCB* DOSMEM_root_block;
*/
static dosmem_info* DOSMEM_info_block;
/*********************************************************************** /***********************************************************************
* DOSMEM_MemoryTop * DOSMEM_MemoryTop
@ -94,18 +103,6 @@ static char *DOSMEM_MemoryTop(void)
return DOSMEM_dosmem+0x9FFFC; /* 640K */ return DOSMEM_dosmem+0x9FFFC; /* 640K */
} }
/***********************************************************************
* DOSMEM_RootBlock
*
* Gets the DOS memory root block.
*/
static dosmem_entry *DOSMEM_RootBlock(void)
{
/* first block has to be paragraph-aligned */
return (dosmem_entry*)(((char*)DOSMEM_info_block) +
((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
}
/*********************************************************************** /***********************************************************************
* DOSMEM_FillIsrTable * DOSMEM_FillIsrTable
* *
@ -233,6 +230,24 @@ void BiosTick( WORD timer )
if (pBiosData) pBiosData->Ticks++; if (pBiosData) pBiosData->Ticks++;
} }
/***********************************************************************
* DOSMEM_Collapse
*
* Helper function for internal use only.
* Atach all following free blocks to this one, even if this one is not free.
*/
void DOSMEM_Collapse( MCB* mcb )
{
MCB* next = MCB_NEXT( mcb );
while (next && next->psp == MCB_PSP_FREE)
{
mcb->size = mcb->size + next->size + 1;
mcb->type = next->type; /* make sure keeping MCB_TYPE_LAST */
next = MCB_NEXT( next );
}
}
/*********************************************************************** /***********************************************************************
* DOSMEM_AllocBlock * DOSMEM_AllocBlock
* *
@ -240,72 +255,58 @@ void BiosTick( WORD timer )
*/ */
LPVOID DOSMEM_AllocBlock(UINT size, UINT16* pseg) LPVOID DOSMEM_AllocBlock(UINT size, UINT16* pseg)
{ {
UINT blocksize; MCB *curr = DOSMEM_root_block;
char *block = NULL; MCB *next = NULL;
dosmem_info *info_block = DOSMEM_info_block; WORD psp = DOSVM_psp;
dosmem_entry *dm;
#ifdef __DOSMEM_DEBUG_ if (!psp)
dosmem_entry *prev = NULL; psp = MCB_PSP_DOS;
#endif
*pseg = 0;
TRACE( "(%04xh)\n", size );
/* round up to paragraph */
size = (size + 15) >> 4;
if( size > info_block->free ) return NULL;
dm = DOSMEM_RootBlock();
while (dm && dm->size != DM_BLOCK_TERMINAL)
{
#ifdef __DOSMEM_DEBUG__ #ifdef __DOSMEM_DEBUG__
if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG ) DOSMEM_Available(); /* checks the whole MCB list */
{
WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
return NULL;
}
prev = dm;
#endif #endif
if( dm->size & DM_BLOCK_FREE )
{ /* loop over all MCB and search the next large enough MCB */
dosmem_entry *next = next_block(dm); while (curr)
{
while ( next->size & DM_BLOCK_FREE ) /* collapse free blocks */ if (!MCB_VALID (curr))
{ {
dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK); ERR( "MCB List Corrupt\n" );
next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL); MCB_DUMP( curr );
next = next_block(dm); return NULL;
} }
if (curr->psp == MCB_PSP_FREE)
blocksize = dm->size & DM_BLOCK_MASK; {
if( blocksize >= size ) DOSMEM_Collapse( curr );
{ /* is it large enough (one paragaph for the MCB)? */
block = ((char*)dm) + sizeof(dosmem_entry); if (curr->size >= size)
if( blocksize - size > 0x20 ) {
{ if (curr->size > size)
/* split dm so that the next one stays {
* paragraph-aligned (and dm loses free bit) */ /* split curr */
next = (MCB *) ((char*) curr + ((size+1) << 4));
dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) - next->psp = MCB_PSP_FREE;
sizeof(dosmem_entry)); next->size = curr->size - (size+1);
next = (dosmem_entry*)(((char*)dm) + next->type = curr->type;
sizeof(dosmem_entry) + dm->size); curr->type = MCB_TYPE_NORMAL;
next->size = (blocksize - (dm->size + curr->size = size;
sizeof(dosmem_entry))) | DM_BLOCK_FREE }
#ifdef __DOSMEM_DEBUG__ /* curr is the found block */
| DM_BLOCK_DEBUG curr->psp = psp;
#endif if( pseg ) *pseg = (((char*)curr) + 16 - (char*)DOSMEM_dosmem) >> 4;
; return (LPVOID) ((char*)curr + 16);
} else dm->size &= DM_BLOCK_MASK; }
}
info_block->blocks++; curr = MCB_NEXT(curr);
info_block->free -= dm->size; }
if( pseg ) *pseg = (block - DOSMEM_dosmem) >> 4; return NULL;
#ifdef __DOSMEM_DEBUG__
dm->size |= DM_BLOCK_DEBUG;
#endif
break;
}
dm = next;
}
else dm = next_block(dm);
}
return (LPVOID)block;
} }
/*********************************************************************** /***********************************************************************
@ -313,28 +314,24 @@ LPVOID DOSMEM_AllocBlock(UINT size, UINT16* pseg)
*/ */
BOOL DOSMEM_FreeBlock(void* ptr) BOOL DOSMEM_FreeBlock(void* ptr)
{ {
dosmem_info *info_block = DOSMEM_info_block; MCB* mcb = (MCB*) ((char*)ptr - 16);
TRACE( "(%p)\n", ptr );
if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
- DOSMEM_dosmem) & 0xf) )
{
dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
#ifdef __DOSMEM_DEBUG__ #ifdef __DOSMEM_DEBUG__
&& ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG ) DOSMEM_Available();
#endif #endif
)
{ if (!MCB_VALID (mcb))
info_block->blocks--; {
info_block->free += dm->size; ERR( "MCB invalid\n" );
MCB_DUMP( mcb );
dm->size |= DM_BLOCK_FREE; return FALSE;
return TRUE; }
}
} mcb->psp = MCB_PSP_FREE;
return FALSE; DOSMEM_Collapse( mcb );
return TRUE;
} }
/*********************************************************************** /***********************************************************************
@ -345,72 +342,55 @@ BOOL DOSMEM_FreeBlock(void* ptr)
* If exact is TRUE, returned value is either old or requested block * If exact is TRUE, returned value is either old or requested block
* size. If exact is FALSE, block is expanded even if there is not * size. If exact is FALSE, block is expanded even if there is not
* enough space for full requested block size. * enough space for full requested block size.
*
* TODO: return also biggest block size
*/ */
UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact) UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact)
{ {
char *block = NULL; MCB* mcb = (MCB*) ((char*)ptr - 16);
dosmem_info *info_block = DOSMEM_info_block; MCB* next;
dosmem_entry *dm;
dosmem_entry *next; TRACE( "(%p,%04xh,%s)\n", ptr, size, exact ? "TRUE" : "FALSE" );
UINT blocksize;
UINT orgsize; /* round up to paragraph */
size = (size + 15) >> 4;
if( (ptr < (void*)(sizeof(dosmem_entry) + (char*)DOSMEM_RootBlock())) || #ifdef __DOSMEM_DEBUG__
(ptr >= (void*)DOSMEM_MemoryTop()) || DOSMEM_Available();
(((((char*)ptr) - DOSMEM_dosmem) & 0xf) != 0) ) #endif
return (UINT)-1;
if (!MCB_VALID (mcb))
{
ERR( "MCB invalid\n" );
MCB_DUMP( mcb );
return -1;
}
dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry)); /* resize needed? */
if( dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL) ) if (mcb->size == size)
return (UINT)-1; return size;
next = next_block(dm); /* collapse free blocks */
orgsize = dm->size & DM_BLOCK_MASK; DOSMEM_Collapse( mcb );
/* collapse free blocks */ /* shrink mcb ? */
while ( next->size & DM_BLOCK_FREE ) if (mcb->size > size)
{ {
dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK); next = (MCB *) ((char*)mcb + ((size+1) << 4));
next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL); next->type = mcb->type;
next = next_block(dm); next->psp = MCB_PSP_FREE;
} next->size = mcb->size - (size+1);
mcb->type = MCB_TYPE_NORMAL;
blocksize = dm->size & DM_BLOCK_MASK; mcb->size = size;
return size << 4;
/* }
* If collapse didn't help we either expand block to maximum
* available size (exact == FALSE) or give collapsed blocks if (!exact)
* back to free storage (exact == TRUE). {
*/ return mcb->size << 4;
if (blocksize < size) }
size = exact ? orgsize : blocksize;
return -1;
block = ((char*)dm) + sizeof(dosmem_entry);
if( blocksize - size > 0x20 )
{
/*
* split dm so that the next one stays
* paragraph-aligned (and next gains free bit)
*/
dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
sizeof(dosmem_entry));
next = (dosmem_entry*)(((char*)dm) +
sizeof(dosmem_entry) + dm->size);
next->size = (blocksize - (dm->size +
sizeof(dosmem_entry))) | DM_BLOCK_FREE;
}
else
{
dm->size &= DM_BLOCK_MASK;
}
/*
* Adjust available memory if block size changes.
*/
info_block->free += orgsize - dm->size;
return size;
} }
/*********************************************************************** /***********************************************************************
@ -418,39 +398,30 @@ UINT DOSMEM_ResizeBlock(void *ptr, UINT size, BOOL exact)
*/ */
UINT DOSMEM_Available(void) UINT DOSMEM_Available(void)
{ {
UINT blocksize, available = 0; UINT available = 0;
dosmem_entry *dm; UINT total = 0;
MCB *curr = DOSMEM_root_block;
dm = DOSMEM_RootBlock(); /* loop over all MCB and search the largest free MCB */
while (curr)
while (dm && dm->size != DM_BLOCK_TERMINAL) {
{
#ifdef __DOSMEM_DEBUG__ #ifdef __DOSMEM_DEBUG__
if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG ) MCB_DUMP( curr );
{
WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
return NULL;
}
prev = dm;
#endif #endif
if( dm->size & DM_BLOCK_FREE ) if (!MCB_VALID (curr))
{ {
dosmem_entry *next = next_block(dm); ERR( "MCB List Corrupt\n" );
MCB_DUMP( curr );
while ( next->size & DM_BLOCK_FREE ) /* collapse free blocks */ return 0;
{ }
dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK); if (curr->psp == MCB_PSP_FREE &&
next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL); curr->size > available )
next = next_block(dm); available = curr->size;
}
total += curr->size + 1;
blocksize = dm->size & DM_BLOCK_MASK; curr = MCB_NEXT( curr );
if ( blocksize > available ) available = blocksize; }
dm = next; TRACE( " %04xh of %04xh paragraphs available\n", available, total );
} return available << 4;
else dm = next_block(dm);
}
return available;
} }
/*********************************************************************** /***********************************************************************
@ -460,26 +431,14 @@ UINT DOSMEM_Available(void)
*/ */
static void DOSMEM_InitMemory(char* addr) static void DOSMEM_InitMemory(char* addr)
{ {
dosmem_entry* root_block;
dosmem_entry* dm;
DOSMEM_FillBiosSegments(); DOSMEM_FillBiosSegments();
DOSMEM_FillIsrTable(); DOSMEM_FillIsrTable();
DOSMEM_info_block = (dosmem_info*)addr; /* align root block to paragraph */
root_block = DOSMEM_RootBlock(); DOSMEM_root_block = (MCB*) (( (DWORD_PTR)(addr+0xf) >> 4) << 4);
root_block->size = DOSMEM_MemoryTop() - (((char*)root_block) + sizeof(dosmem_entry)); DOSMEM_root_block->type = MCB_TYPE_LAST;
DOSMEM_root_block->psp = MCB_PSP_FREE;
DOSMEM_info_block->blocks = 0; DOSMEM_root_block->size = (DOSMEM_MemoryTop() - ((char*)DOSMEM_root_block)) >> 4;
DOSMEM_info_block->free = root_block->size;
dm = next_block(root_block);
dm->size = DM_BLOCK_TERMINAL;
root_block->size |= DM_BLOCK_FREE
#ifdef __DOSMEM_DEBUG__
| DM_BLOCK_DEBUG
#endif
;
TRACE("DOS conventional memory initialized, %d bytes free.\n", TRACE("DOS conventional memory initialized, %d bytes free.\n",
DOSMEM_Available()); DOSMEM_Available());