Changed the Win32 dll descriptor to be in IMAGE_NT_HEADERS format.
[wine/multimedia.git] / msdos / dosmem.c
blobcf632eb7cef61f874378282aa59dfbabc359cd7b
1 /*
2 * DOS memory emulation
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Marcus Meissner
6 */
8 #include "config.h"
10 #include <signal.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #ifdef HAVE_SYS_MMAN_H
14 # include <sys/mman.h>
15 #endif
17 #include "winbase.h"
18 #include "wine/winbase16.h"
20 #include "global.h"
21 #include "ldt.h"
22 #include "selectors.h"
23 #include "miscemu.h"
24 #include "vga.h"
25 #include "dosexe.h"
26 #include "debugtools.h"
28 DEFAULT_DEBUG_CHANNEL(dosmem);
29 DECLARE_DEBUG_CHANNEL(selector);
31 HANDLE16 DOSMEM_BiosDataSeg; /* BIOS data segment at 0x40:0 */
32 HANDLE16 DOSMEM_BiosSysSeg; /* BIOS ROM segment at 0xf000:0 */
34 DWORD DOSMEM_CollateTable;
36 /* use 2 low bits of 'size' for the housekeeping */
38 #define DM_BLOCK_DEBUG 0xABE00000
39 #define DM_BLOCK_TERMINAL 0x00000001
40 #define DM_BLOCK_FREE 0x00000002
41 #define DM_BLOCK_MASK 0x001FFFFC
44 #define __DOSMEM_DEBUG__
47 typedef struct {
48 unsigned size;
49 } dosmem_entry;
51 typedef struct {
52 unsigned blocks;
53 unsigned free;
54 } dosmem_info;
56 #define NEXT_BLOCK(block) \
57 (dosmem_entry*)(((char*)(block)) + \
58 sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
60 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
61 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
63 /* DOS memory base */
64 static char *DOSMEM_dosmem;
65 /* bios area; normally at offset 0x400, but can be moved to trap NULL pointers */
66 static void *DOSMEM_biosdata;
68 /* various real-mode code stubs */
69 WORD DOSMEM_wrap_seg;
70 WORD DOSMEM_xms_seg;
71 WORD DOSMEM_dpmi_seg;
72 WORD DOSMEM_dpmi_sel;
74 /***********************************************************************
75 * DOSMEM_MemoryBase
77 * Gets the DOS memory base.
79 char *DOSMEM_MemoryBase(void)
81 return DOSMEM_dosmem;
84 /***********************************************************************
85 * DOSMEM_MemoryTop
87 * Gets the DOS memory top.
89 static char *DOSMEM_MemoryTop(void)
91 return DOSMEM_dosmem+0x9FFFC; /* 640K */
94 /***********************************************************************
95 * DOSMEM_InfoBlock
97 * Gets the DOS memory info block.
99 static dosmem_info *DOSMEM_InfoBlock(void)
101 return (dosmem_info*)(DOSMEM_dosmem+0x10000); /* 64K */
104 /***********************************************************************
105 * DOSMEM_RootBlock
107 * Gets the DOS memory root block.
109 static dosmem_entry *DOSMEM_RootBlock(void)
111 /* first block has to be paragraph-aligned */
112 return (dosmem_entry*)(((char*)DOSMEM_InfoBlock()) +
113 ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
116 /***********************************************************************
117 * DOSMEM_FillIsrTable
119 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
121 * NOTES:
122 * Linux normally only traps INTs performed from or destined to BIOSSEG
123 * for us to handle, if the int_revectored table is empty. Filling the
124 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
125 * to hook interrupts, as well as use their familiar retf tricks to call
126 * them, AND let Wine handle any unhooked interrupts transparently.
128 static void DOSMEM_FillIsrTable(void)
130 SEGPTR *isr = (SEGPTR*)DOSMEM_dosmem;
131 DWORD *stub = (DWORD*)((char*)isr + (VM_STUB_SEGMENT << 4));
132 int x;
134 for (x=0; x<256; x++) isr[x]=PTR_SEG_OFF_TO_SEGPTR(VM_STUB_SEGMENT,x*4);
135 for (x=0; x<256; x++) stub[x]=VM_STUB(x);
138 /***********************************************************************
139 * DOSMEM_InitDPMI
141 * Allocate the global DPMI RMCB wrapper.
143 static void DOSMEM_InitDPMI(void)
145 LPSTR ptr;
147 static const char wrap_code[]={
148 0xCD,0x31, /* int $0x31 */
149 0xCB /* lret */
152 static const char enter_xms[]=
154 /* XMS hookable entry point */
155 0xEB,0x03, /* jmp entry */
156 0x90,0x90,0x90, /* nop;nop;nop */
157 /* entry: */
158 /* real entry point */
159 /* for simplicity, we'll just use the same hook as DPMI below */
160 0xCD,0x31, /* int $0x31 */
161 0xCB /* lret */
164 static const char enter_pm[]=
166 0x50, /* pushw %ax */
167 0x52, /* pushw %dx */
168 0x55, /* pushw %bp */
169 0x89,0xE5, /* movw %sp,%bp */
170 /* get return CS */
171 0x8B,0x56,0x08, /* movw 8(%bp),%dx */
172 /* just call int 31 here to get into protected mode... */
173 /* it'll check whether it was called from dpmi_seg... */
174 0xCD,0x31, /* int $0x31 */
175 /* we are now in the context of a 16-bit relay call */
176 /* need to fixup our stack;
177 * 16-bit relay return address will be lost, but we won't worry quite yet */
178 0x8E,0xD0, /* movw %ax,%ss */
179 0x66,0x0F,0xB7,0xE5, /* movzwl %bp,%esp */
180 /* set return CS */
181 0x89,0x56,0x08, /* movw %dx,8(%bp) */
182 0x5D, /* popw %bp */
183 0x5A, /* popw %dx */
184 0x58, /* popw %ax */
185 0xCB /* lret */
188 ptr = DOSMEM_GetBlock( sizeof(wrap_code), &DOSMEM_wrap_seg );
189 memcpy( ptr, wrap_code, sizeof(wrap_code) );
190 ptr = DOSMEM_GetBlock( sizeof(enter_xms), &DOSMEM_xms_seg );
191 memcpy( ptr, enter_xms, sizeof(enter_xms) );
192 ptr = DOSMEM_GetBlock( sizeof(enter_pm), &DOSMEM_dpmi_seg );
193 memcpy( ptr, enter_pm, sizeof(enter_pm) );
194 DOSMEM_dpmi_sel = SELECTOR_AllocBlock( ptr, sizeof(enter_pm), SEGMENT_CODE, FALSE, FALSE );
197 BIOSDATA * DOSMEM_BiosData()
199 return (BIOSDATA *)DOSMEM_biosdata;
202 BYTE * DOSMEM_BiosSys()
204 return DOSMEM_dosmem+0xf0000;
207 struct _DOS_LISTOFLISTS * DOSMEM_LOL()
209 return (struct _DOS_LISTOFLISTS *)DOSMEM_MapRealToLinear
210 (PTR_SEG_OFF_TO_SEGPTR(HIWORD(DOS_LOLSeg),0));
213 /***********************************************************************
214 * DOSMEM_FillBiosSegments
216 * Fill the BIOS data segment with dummy values.
218 static void DOSMEM_FillBiosSegments(void)
220 BYTE *pBiosSys = DOSMEM_BiosSys();
221 BYTE *pBiosROMTable = pBiosSys+0xe6f5;
222 BIOSDATA *pBiosData = DOSMEM_BiosData();
224 /* bogus 0xe0xx addresses !! Adapt int 0x10/0x1b if change needed */
225 VIDEOFUNCTIONALITY *pVidFunc = (VIDEOFUNCTIONALITY *)(pBiosSys+0xe000);
226 VIDEOSTATE *pVidState = (VIDEOSTATE *)(pBiosSys+0xe010);
227 int i;
229 /* Clear all unused values */
230 memset( pBiosData, 0, sizeof(*pBiosData) );
231 memset( pVidFunc, 0, sizeof(*pVidFunc ) );
232 memset( pVidState, 0, sizeof(*pVidState) );
234 /* FIXME: should check the number of configured drives and ports */
236 pBiosData->Com1Addr = 0x3f8;
237 pBiosData->Com2Addr = 0x2f8;
238 pBiosData->Lpt1Addr = 0x378;
239 pBiosData->Lpt2Addr = 0x278;
240 pBiosData->InstalledHardware = 0x5463;
241 pBiosData->MemSize = 640;
242 pBiosData->NextKbdCharPtr = 0x1e;
243 pBiosData->FirstKbdCharPtr = 0x1e;
244 pBiosData->VideoMode = 3;
245 pBiosData->VideoColumns = 80;
246 pBiosData->VideoPageSize = 80 * 25 * 2;
247 pBiosData->VideoPageStartAddr = 0xb800;
248 pBiosData->VideoCtrlAddr = 0x3d4;
249 pBiosData->Ticks = INT1A_GetTicksSinceMidnight();
250 pBiosData->NbHardDisks = 2;
251 pBiosData->KbdBufferStart = 0x1e;
252 pBiosData->KbdBufferEnd = 0x3e;
253 pBiosData->RowsOnScreenMinus1 = 23;
254 pBiosData->BytesPerChar = 0x10;
255 pBiosData->ModeOptions = 0x64;
256 pBiosData->FeatureBitsSwitches = 0xf9;
257 pBiosData->VGASettings = 0x51;
258 pBiosData->DisplayCombination = 0x08;
259 pBiosData->DiskDataRate = 0;
261 /* fill ROM configuration table (values from Award) */
262 *(pBiosROMTable+0x0) = 0x08; /* number of bytes following LO */
263 *(pBiosROMTable+0x1) = 0x00; /* number of bytes following HI */
264 *(pBiosROMTable+0x2) = 0xfc; /* model */
265 *(pBiosROMTable+0x3) = 0x01; /* submodel */
266 *(pBiosROMTable+0x4) = 0x00; /* BIOS revision */
267 *(pBiosROMTable+0x5) = 0x74; /* feature byte 1 */
268 *(pBiosROMTable+0x6) = 0x00; /* feature byte 2 */
269 *(pBiosROMTable+0x7) = 0x00; /* feature byte 3 */
270 *(pBiosROMTable+0x8) = 0x00; /* feature byte 4 */
271 *(pBiosROMTable+0x9) = 0x00; /* feature byte 5 */
274 for (i = 0; i < 7; i++)
275 pVidFunc->ModeSupport[i] = 0xff;
277 pVidFunc->ScanlineSupport = 7;
278 pVidFunc->NumberCharBlocks = 0;
279 pVidFunc->ActiveCharBlocks = 0;
280 pVidFunc->MiscFlags = 0x8ff;
281 pVidFunc->SavePointerFlags = 0x3f;
283 pVidState->StaticFuncTable = 0xf000e000; /* FIXME: always real mode ? */
284 pVidState->VideoMode = pBiosData->VideoMode; /* needs updates! */
285 pVidState->NumberColumns = pBiosData->VideoColumns; /* needs updates! */
286 pVidState->RegenBufLen = 0;
287 pVidState->RegenBufAddr = 0;
289 for (i = 0; i < 8; i++)
290 pVidState->CursorPos[i] = 0;
292 pVidState->CursorType = 0x0a0b; /* start/end line */
293 pVidState->ActivePage = 0;
294 pVidState->CRTCPort = 0x3da;
295 pVidState->Port3x8 = 0;
296 pVidState->Port3x9 = 0;
297 pVidState->NumberRows = 23; /* number of rows - 1 */
298 pVidState->BytesPerChar = 0x10;
299 pVidState->DCCActive = pBiosData->DisplayCombination;
300 pVidState->DCCAlternate = 0;
301 pVidState->NumberColors = 16;
302 pVidState->NumberPages = 1;
303 pVidState->NumberScanlines = 3; /* (0,1,2,3) = (200,350,400,480) */
304 pVidState->CharBlockPrimary = 0;
305 pVidState->CharBlockSecondary = 0;
306 pVidState->MiscFlags =
307 (pBiosData->VGASettings & 0x0f)
308 | ((pBiosData->ModeOptions & 1) << 4); /* cursor emulation */
309 pVidState->NonVGASupport = 0;
310 pVidState->VideoMem = (pBiosData->ModeOptions & 0x60 >> 5);
311 pVidState->SavePointerState = 0;
312 pVidState->DisplayStatus = 4;
314 /* BIOS date string */
315 strcpy((char *)pBiosSys+0xfff5, "13/01/99");
317 /* BIOS ID */
318 *(pBiosSys+0xfffe) = 0xfc;
321 /***********************************************************************
322 * DOSMEM_InitCollateTable
324 * Initialises the collate table (character sorting, language dependent)
326 static void DOSMEM_InitCollateTable()
328 DWORD x;
329 unsigned char *tbl;
330 int i;
332 x = GlobalDOSAlloc16(258);
333 DOSMEM_CollateTable = MAKELONG(0,(x>>16));
334 tbl = DOSMEM_MapRealToLinear(DOSMEM_CollateTable);
335 *(WORD*)tbl = 0x100;
336 tbl += 2;
337 for ( i = 0; i < 0x100; i++) *tbl++ = i;
340 /***********************************************************************
341 * DOSMEM_InitErrorTable
343 * Initialises the error tables (DOS 5+)
345 static void DOSMEM_InitErrorTable()
347 #if 0 /* no longer used */
348 DWORD x;
349 char *call;
351 /* We will use a snippet of real mode code that calls */
352 /* a WINE-only interrupt to handle moving the requested */
353 /* message into the buffer... */
355 /* FIXME - There is still something wrong... */
357 /* FIXME - Find hex values for opcodes...
359 (On call, AX contains message number
360 DI contains 'offset' (??)
361 Resturn, ES:DI points to counted string )
363 PUSH BX
364 MOV BX, AX
365 MOV AX, (arbitrary subfunction number)
366 INT (WINE-only interrupt)
367 POP BX
372 const int code = 4;
373 const int buffer = 80;
374 const int SIZE_TO_ALLOCATE = code + buffer;
376 /* FIXME - Complete rewrite of the table system to save */
377 /* precious DOS space. Now, we return the 0001:???? as */
378 /* DOS 4+ (??, it seems to be the case in MS 7.10) treats that */
379 /* as a special case and programs will use the alternate */
380 /* interface (a farcall returned with INT 24 (AX = 0x122e, DL = */
381 /* 0x08) which lets us have a smaller memory footprint anyway. */
383 x = GlobalDOSAlloc16(SIZE_TO_ALLOCATE);
385 DOSMEM_ErrorCall = MAKELONG(0,(x>>16));
386 DOSMEM_ErrorBuffer = DOSMEM_ErrorCall + code;
388 call = DOSMEM_MapRealToLinear(DOSMEM_ErrorCall);
390 memset(call, 0, SIZE_TO_ALLOCATE);
391 #endif
392 /* Fixme - Copy assembly into buffer here */
395 /***********************************************************************
396 * DOSMEM_InitMemory
398 * Initialises the DOS memory structures.
400 static void DOSMEM_InitMemory(void)
402 /* Low 64Kb are reserved for DOS/BIOS so the useable area starts at
403 * 1000:0000 and ends at 9FFF:FFEF. */
405 dosmem_info* info_block = DOSMEM_InfoBlock();
406 dosmem_entry* root_block = DOSMEM_RootBlock();
407 dosmem_entry* dm;
409 root_block->size = DOSMEM_MemoryTop() - (((char*)root_block) + sizeof(dosmem_entry));
411 info_block->blocks = 0;
412 info_block->free = root_block->size;
414 dm = NEXT_BLOCK(root_block);
415 dm->size = DM_BLOCK_TERMINAL;
416 root_block->size |= DM_BLOCK_FREE
417 #ifdef __DOSMEM_DEBUG__
418 | DM_BLOCK_DEBUG;
419 #endif
423 /**********************************************************************
424 * setup_dos_mem
426 * Setup the first megabyte for DOS memory access
428 static void setup_dos_mem( int dos_init )
430 int bios_offset = 0x400;
431 int page_size = VIRTUAL_GetPageSize();
432 void *addr = VIRTUAL_mmap( -1, (void *)page_size, 0x110000-page_size, 0,
433 PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
434 if (addr == (void *)page_size) /* we got what we wanted */
436 /* now map from address 0 */
437 addr = VIRTUAL_mmap( -1, NULL, 0x110000, 0,
438 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED );
439 if (addr)
441 ERR("MAP_FIXED failed at address 0 for DOS address space\n" );
442 ExitProcess(1);
445 /* inform the memory manager that there is a mapping here */
446 VirtualAlloc( addr, 0x110000, MEM_RESERVE | MEM_SYSTEM, PAGE_EXECUTE_READWRITE );
448 /* protect the first 64K to catch NULL pointers */
449 if (!dos_init)
451 VirtualProtect( addr, 0x10000, PAGE_NOACCESS, NULL );
452 /* move the BIOS data from 0x400 to 0xf0400 */
453 bios_offset += 0xf0000;
456 else
458 ERR("Cannot use first megabyte for DOS address space, please report\n" );
459 if (dos_init) ExitProcess(1);
460 /* allocate the DOS area somewhere else */
461 addr = VirtualAlloc( NULL, 0x110000, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
462 if (!addr)
464 ERR( "Cannot allocate DOS memory\n" );
465 ExitProcess(1);
468 DOSMEM_dosmem = addr;
469 DOSMEM_biosdata = (char *)addr + bios_offset;
473 /***********************************************************************
474 * DOSMEM_Init
476 * Create the dos memory segments, and store them into the KERNEL
477 * exported values.
479 BOOL DOSMEM_Init(BOOL dos_init)
481 static int already_done;
483 if (!already_done)
485 setup_dos_mem( dos_init );
487 DOSMEM_BiosDataSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_biosdata,
488 0x100, 0, FALSE, FALSE, FALSE );
489 DOSMEM_BiosSysSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0xf0000,
490 0x10000, 0, FALSE, FALSE, FALSE );
491 DOSMEM_FillBiosSegments();
492 DOSMEM_InitMemory();
493 DOSMEM_InitCollateTable();
494 DOSMEM_InitErrorTable();
495 DOSMEM_InitDPMI();
496 DOSDEV_InstallDOSDevices();
497 already_done = 1;
499 else if (dos_init)
501 if (DOSMEM_dosmem)
503 ERR( "Needs access to the first megabyte for DOS mode\n" );
504 ExitProcess(1);
506 MESSAGE( "Warning: unprotecting the first 64KB of memory to allow real-mode calls.\n"
507 " NULL pointer accesses will no longer be caught.\n" );
508 VirtualProtect( NULL, 0x10000, PAGE_EXECUTE_READWRITE, NULL );
509 /* copy the BIOS area down to 0x400 */
510 memcpy( DOSMEM_dosmem + 0x400, DOSMEM_biosdata, 0x100 );
511 DOSMEM_biosdata = DOSMEM_dosmem + 0x400;
512 SetSelectorBase( DOSMEM_BiosDataSeg, 0x400 );
514 /* interrupt table is at addr 0, so only do this when setting up DOS mode */
515 if (dos_init) DOSMEM_FillIsrTable();
516 return TRUE;
520 /***********************************************************************
521 * DOSMEM_Tick
523 * Increment the BIOS tick counter. Called by timer signal handler.
525 void DOSMEM_Tick( WORD timer )
527 BIOSDATA *pBiosData = DOSMEM_BiosData();
528 if (pBiosData) pBiosData->Ticks++;
531 /***********************************************************************
532 * DOSMEM_GetBlock
534 * Carve a chunk of the DOS memory block (without selector).
536 LPVOID DOSMEM_GetBlock(UINT size, UINT16* pseg)
538 UINT blocksize;
539 char *block = NULL;
540 dosmem_info *info_block = DOSMEM_InfoBlock();
541 dosmem_entry *dm;
542 #ifdef __DOSMEM_DEBUG_
543 dosmem_entry *prev = NULL;
544 #endif
546 if( size > info_block->free ) return NULL;
547 dm = DOSMEM_RootBlock();
549 while (dm && dm->size != DM_BLOCK_TERMINAL)
551 #ifdef __DOSMEM_DEBUG__
552 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
554 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
555 return NULL;
557 prev = dm;
558 #endif
559 if( dm->size & DM_BLOCK_FREE )
561 dosmem_entry *next = NEXT_BLOCK(dm);
563 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
565 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
566 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
567 next = NEXT_BLOCK(dm);
570 blocksize = dm->size & DM_BLOCK_MASK;
571 if( blocksize >= size )
573 block = ((char*)dm) + sizeof(dosmem_entry);
574 if( blocksize - size > 0x20 )
576 /* split dm so that the next one stays
577 * paragraph-aligned (and dm loses free bit) */
579 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
580 sizeof(dosmem_entry));
581 next = (dosmem_entry*)(((char*)dm) +
582 sizeof(dosmem_entry) + dm->size);
583 next->size = (blocksize - (dm->size +
584 sizeof(dosmem_entry))) | DM_BLOCK_FREE
585 #ifdef __DOSMEM_DEBUG__
586 | DM_BLOCK_DEBUG
587 #endif
589 } else dm->size &= DM_BLOCK_MASK;
591 info_block->blocks++;
592 info_block->free -= dm->size;
593 if( pseg ) *pseg = (block - DOSMEM_dosmem) >> 4;
594 #ifdef __DOSMEM_DEBUG__
595 dm->size |= DM_BLOCK_DEBUG;
596 #endif
597 break;
599 dm = next;
601 else dm = NEXT_BLOCK(dm);
603 return (LPVOID)block;
606 /***********************************************************************
607 * DOSMEM_FreeBlock
609 BOOL DOSMEM_FreeBlock(void* ptr)
611 dosmem_info *info_block = DOSMEM_InfoBlock();
613 if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
614 ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
615 - DOSMEM_dosmem) & 0xf) )
617 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
619 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
620 #ifdef __DOSMEM_DEBUG__
621 && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
622 #endif
625 info_block->blocks--;
626 info_block->free += dm->size;
628 dm->size |= DM_BLOCK_FREE;
629 return TRUE;
632 return FALSE;
635 /***********************************************************************
636 * DOSMEM_ResizeBlock
638 LPVOID DOSMEM_ResizeBlock(void* ptr, UINT size, UINT16* pseg)
640 char *block = NULL;
641 dosmem_info *info_block = DOSMEM_InfoBlock();
643 if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
644 ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
645 - DOSMEM_dosmem) & 0xf) )
647 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
649 if( pseg ) *pseg = ((char*)ptr - DOSMEM_dosmem) >> 4;
651 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
654 dosmem_entry *next = NEXT_BLOCK(dm);
655 UINT blocksize, orgsize = dm->size & DM_BLOCK_MASK;
657 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
659 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
660 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
661 next = NEXT_BLOCK(dm);
664 blocksize = dm->size & DM_BLOCK_MASK;
665 if (blocksize >= size)
667 block = ((char*)dm) + sizeof(dosmem_entry);
668 if( blocksize - size > 0x20 )
670 /* split dm so that the next one stays
671 * paragraph-aligned (and next gains free bit) */
673 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
674 sizeof(dosmem_entry));
675 next = (dosmem_entry*)(((char*)dm) +
676 sizeof(dosmem_entry) + dm->size);
677 next->size = (blocksize - (dm->size +
678 sizeof(dosmem_entry))) | DM_BLOCK_FREE
680 } else dm->size &= DM_BLOCK_MASK;
682 info_block->free += orgsize - dm->size;
683 } else {
684 /* the collapse didn't help, try getting a new block */
685 block = DOSMEM_GetBlock(size, pseg);
686 if (block) {
687 /* we got one, copy the old data there (we do need to, right?) */
688 memcpy(block, ((char*)dm) + sizeof(dosmem_entry),
689 (size<orgsize) ? size : orgsize);
690 /* free old block */
691 info_block->blocks--;
692 info_block->free += dm->size;
694 dm->size |= DM_BLOCK_FREE;
695 } else {
696 /* and Bill Gates said 640K should be enough for everyone... */
698 /* need to split original and collapsed blocks apart again,
699 * and free the collapsed blocks again, before exiting */
700 if( blocksize - orgsize > 0x20 )
702 /* split dm so that the next one stays
703 * paragraph-aligned (and next gains free bit) */
705 dm->size = (((orgsize + 0xf + sizeof(dosmem_entry)) & ~0xf) -
706 sizeof(dosmem_entry));
707 next = (dosmem_entry*)(((char*)dm) +
708 sizeof(dosmem_entry) + dm->size);
709 next->size = (blocksize - (dm->size +
710 sizeof(dosmem_entry))) | DM_BLOCK_FREE
712 } else dm->size &= DM_BLOCK_MASK;
717 return (LPVOID)block;
721 /***********************************************************************
722 * DOSMEM_Available
724 UINT DOSMEM_Available(void)
726 UINT blocksize, available = 0;
727 dosmem_entry *dm;
729 dm = DOSMEM_RootBlock();
731 while (dm && dm->size != DM_BLOCK_TERMINAL)
733 #ifdef __DOSMEM_DEBUG__
734 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
736 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
737 return NULL;
739 prev = dm;
740 #endif
741 if( dm->size & DM_BLOCK_FREE )
743 dosmem_entry *next = NEXT_BLOCK(dm);
745 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
747 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
748 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
749 next = NEXT_BLOCK(dm);
752 blocksize = dm->size & DM_BLOCK_MASK;
753 if ( blocksize > available ) available = blocksize;
754 dm = next;
756 else dm = NEXT_BLOCK(dm);
758 return available;
762 /***********************************************************************
763 * DOSMEM_MapLinearToDos
765 * Linear address to the DOS address space.
767 UINT DOSMEM_MapLinearToDos(LPVOID ptr)
769 if (((char*)ptr >= DOSMEM_dosmem) &&
770 ((char*)ptr < DOSMEM_dosmem + 0x100000))
771 return (UINT)ptr - (UINT)DOSMEM_dosmem;
772 return (UINT)ptr;
776 /***********************************************************************
777 * DOSMEM_MapDosToLinear
779 * DOS linear address to the linear address space.
781 LPVOID DOSMEM_MapDosToLinear(UINT ptr)
783 if (ptr < 0x100000) return (LPVOID)(ptr + (UINT)DOSMEM_dosmem);
784 return (LPVOID)ptr;
788 /***********************************************************************
789 * DOSMEM_MapRealToLinear
791 * Real mode DOS address into a linear pointer
793 LPVOID DOSMEM_MapRealToLinear(DWORD x)
795 LPVOID lin;
797 lin=DOSMEM_dosmem+(x&0xffff)+(((x&0xffff0000)>>16)*16);
798 TRACE_(selector)("(0x%08lx) returns 0x%p.\n", x, lin );
799 return lin;
802 /***********************************************************************
803 * DOSMEM_AllocSelector
805 * Allocates a protected mode selector for a realmode segment.
807 WORD DOSMEM_AllocSelector(WORD realsel)
809 HMODULE16 hModule = GetModuleHandle16("KERNEL");
810 WORD sel;
812 sel=GLOBAL_CreateBlock(
813 GMEM_FIXED,DOSMEM_dosmem+realsel*16,0x10000,
814 hModule,FALSE,FALSE,FALSE );
815 TRACE_(selector)("(0x%04x) returns 0x%04x.\n", realsel,sel);
816 return sel;