Bug fix.
[wine/multimedia.git] / msdos / dosmem.c
blobe06c5379ee6bd8b952d8a62f679ec5f3fda374a7
1 /*
2 * DOS memory emulation
4 * Copyright 1995 Alexandre Julliard
5 * Copyright 1996 Marcus Meissner
6 */
8 #include <signal.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include "winbase.h"
12 #include "wine/winbase16.h"
13 #include "global.h"
14 #include "ldt.h"
15 #include "miscemu.h"
16 #include "vga.h"
17 #include "module.h"
18 #include "task.h"
19 #include "debug.h"
21 HANDLE16 DOSMEM_BiosDataSeg; /* BIOS data segment at 0x40:0 */
22 HANDLE16 DOSMEM_BiosSysSeg; /* BIOS ROM segment at 0xf000:0 */
24 #pragma pack(4)
26 static char *DOSMEM_dosmem;
28 DWORD DOSMEM_CollateTable;
30 DWORD DOSMEM_ErrorCall;
31 DWORD DOSMEM_ErrorBuffer;
33 /* use 2 low bits of 'size' for the housekeeping */
35 #define DM_BLOCK_DEBUG 0xABE00000
36 #define DM_BLOCK_TERMINAL 0x00000001
37 #define DM_BLOCK_FREE 0x00000002
38 #define DM_BLOCK_MASK 0x001FFFFC
41 #define __DOSMEM_DEBUG__
44 typedef struct {
45 unsigned size;
46 } dosmem_entry;
48 typedef struct {
49 unsigned blocks;
50 unsigned free;
51 } dosmem_info;
53 #define NEXT_BLOCK(block) \
54 (dosmem_entry*)(((char*)(block)) + \
55 sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
57 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
58 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
60 /***********************************************************************
61 * DOSMEM_MemoryBase
63 * Gets the DOS memory base.
65 char *DOSMEM_MemoryBase(HMODULE16 hModule)
67 TDB *pTask = hModule ? NULL : (TDB *)GlobalLock16( GetCurrentTask() );
68 NE_MODULE *pModule = (hModule || pTask) ? NE_GetPtr( hModule ? hModule : pTask->hModule ) : NULL;
70 GlobalUnlock16( GetCurrentTask() );
71 if (pModule && pModule->dos_image)
72 return pModule->dos_image;
73 else
74 return DOSMEM_dosmem;
77 /***********************************************************************
78 * DOSMEM_MemoryTop
80 * Gets the DOS memory top.
82 static char *DOSMEM_MemoryTop(HMODULE16 hModule)
84 return DOSMEM_MemoryBase(hModule)+0x9FFFC; /* 640K */
87 /***********************************************************************
88 * DOSMEM_InfoBlock
90 * Gets the DOS memory info block.
92 static dosmem_info *DOSMEM_InfoBlock(HMODULE16 hModule)
94 return (dosmem_info*)(DOSMEM_MemoryBase(hModule)+0x10000); /* 64K */
97 /***********************************************************************
98 * DOSMEM_RootBlock
100 * Gets the DOS memory root block.
102 static dosmem_entry *DOSMEM_RootBlock(HMODULE16 hModule)
104 /* first block has to be paragraph-aligned */
105 return (dosmem_entry*)(((char*)DOSMEM_InfoBlock(hModule)) +
106 ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
109 /***********************************************************************
110 * DOSMEM_FillIsrTable
112 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
114 * NOTES:
115 * Linux normally only traps INTs performed from or destined to BIOSSEG
116 * for us to handle, if the int_revectored table is empty. Filling the
117 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
118 * to hook interrupts, as well as use their familiar retf tricks to call
119 * them, AND let Wine handle any unhooked interrupts transparently.
121 static void DOSMEM_FillIsrTable(HMODULE16 hModule)
123 SEGPTR *isr = (SEGPTR*)DOSMEM_MemoryBase(hModule);
124 DWORD *stub = (DWORD*)((char*)isr + (VM_STUB_SEGMENT << 4));
125 int x;
127 for (x=0; x<256; x++) isr[x]=PTR_SEG_OFF_TO_SEGPTR(VM_STUB_SEGMENT,x*4);
128 for (x=0; x<256; x++) stub[x]=VM_STUB(x);
131 /***********************************************************************
132 * DOSMEM_InitDPMI
134 * Allocate the global DPMI RMCB wrapper.
136 static void DOSMEM_InitDPMI(void)
138 extern UINT16 DPMI_wrap_seg;
139 static char wrap_code[]={
140 0xCD,0x31, /* int $0x31 */
141 0xCB /* lret */
143 LPSTR wrapper = (LPSTR)DOSMEM_GetBlock(0, sizeof(wrap_code), &DPMI_wrap_seg);
145 memcpy(wrapper, wrap_code, sizeof(wrap_code));
148 BIOSDATA * DOSMEM_BiosData()
150 return (BIOSDATA *)(DOSMEM_MemoryBase(0)+0x400);
153 BYTE * DOSMEM_BiosSys()
155 return DOSMEM_MemoryBase(0)+0xf0000;
158 /***********************************************************************
159 * DOSMEM_FillBiosSegments
161 * Fill the BIOS data segment with dummy values.
163 static void DOSMEM_FillBiosSegments(void)
165 BYTE *pBiosSys = (BYTE *)GlobalLock16( DOSMEM_BiosSysSeg );
166 BYTE *pBiosROMTable = pBiosSys+0xe6f5;
167 BIOSDATA *pBiosData = (BIOSDATA *)GlobalLock16( DOSMEM_BiosDataSeg );
169 /* bogus 0xe0xx addresses !! Adapt int 0x10/0x1b if change needed */
170 BYTE *pVideoStaticFuncTable = pBiosSys+0xe000;
171 BYTE *pVideoStateInfo = pBiosSys+0xe010;
172 BYTE *p;
173 int i;
175 /* Clear all unused values */
176 memset( pBiosData, 0, sizeof(*pBiosData) );
178 /* FIXME: should check the number of configured drives and ports */
180 pBiosData->Com1Addr = 0x3f8;
181 pBiosData->Com2Addr = 0x2f8;
182 pBiosData->Lpt1Addr = 0x378;
183 pBiosData->Lpt2Addr = 0x278;
184 pBiosData->InstalledHardware = 0x5463;
185 pBiosData->MemSize = 640;
186 pBiosData->NextKbdCharPtr = 0x1e;
187 pBiosData->FirstKbdCharPtr = 0x1e;
188 pBiosData->VideoMode = 3;
189 pBiosData->VideoColumns = 80;
190 pBiosData->VideoPageSize = 80 * 25 * 2;
191 pBiosData->VideoPageStartAddr = 0xb800;
192 pBiosData->VideoCtrlAddr = 0x3d4;
193 pBiosData->Ticks = INT1A_GetTicksSinceMidnight();
194 pBiosData->NbHardDisks = 2;
195 pBiosData->KbdBufferStart = 0x1e;
196 pBiosData->KbdBufferEnd = 0x3e;
197 pBiosData->RowsOnScreenMinus1 = 23;
198 pBiosData->BytesPerChar = 0x10;
199 pBiosData->ModeOptions = 0x64;
200 pBiosData->FeatureBitsSwitches = 0xf9;
201 pBiosData->VGASettings = 0x51;
202 pBiosData->DisplayCombination = 0x08;
203 pBiosData->DiskDataRate = 0;
205 /* fill ROM configuration table (values from Award) */
206 *(WORD *)(pBiosROMTable)= 0x08; /* number of bytes following */
207 *(pBiosROMTable+0x2) = 0xfc; /* model */
208 *(pBiosROMTable+0x3) = 0x01; /* submodel */
209 *(pBiosROMTable+0x4) = 0x00; /* BIOS revision */
210 *(pBiosROMTable+0x5) = 0x74; /* feature byte 1 */
211 *(pBiosROMTable+0x6) = 0x00; /* feature byte 2 */
212 *(pBiosROMTable+0x7) = 0x00; /* feature byte 3 */
213 *(pBiosROMTable+0x8) = 0x00; /* feature byte 4 */
214 *(pBiosROMTable+0x9) = 0x00; /* feature byte 5 */
216 p = pVideoStaticFuncTable;
217 for (i=0; i < 7; i++)
218 *(p+i) = 0xff; /* modes supported 1 to 7 */
220 *(p+0x7) = 7; /* scan lines supported */
221 *(p+0x8) = 0; /* tot nr of char blocks in text mode */
222 *(p+0x9) = 0; /* max nr of active char blocks in text */
223 *(WORD *)(p+0xa) = 0x8ff; /* misc support flags */
224 *(WORD *)(p+0xc) = 0; /* reserved */
225 *(p+0xe) = 0x3f; /* save pointer function flags */
226 *(p+0xf) = 0; /* reserved */
228 p = pVideoStateInfo;
229 *(DWORD *)p = 0xf000e000; /* address of pVideoStaticFuncTable, FIXME: always real mode ? */
230 *(p+0x04) = /* current video mode, needs updates ! */
231 pBiosData->VideoMode;
232 *(WORD *)(p+0x05) = /* number of columns, needs updates ! */
233 pBiosData->VideoColumns;
234 *(WORD *)(p+0x07) = 0; /* length of regen (???) buffer in bytes */
235 *(WORD *)(p+0x09) = 0; /* starting address of regen (?) buffer */
236 *(WORD *)(p+0x0b) = 0; /* cursorpos page 0 */
237 *(WORD *)(p+0x0d) = 0; /* cursorpos page 1 */
238 *(WORD *)(p+0x0f) = 0; /* page 2 */
239 *(WORD *)(p+0x11) = 0; /* page 3 */
240 *(WORD *)(p+0x13) = 0; /* page 4 */
241 *(WORD *)(p+0x15) = 0; /* page 5 */
242 *(WORD *)(p+0x17) = 0; /* page 6 */
243 *(WORD *)(p+0x19) = 0; /* page 7 */
244 *(WORD *)(p+0x1b) = 0x0a0b; /* cursor size (start/end line) */
245 *(p+0x1d) = 0; /* active display page */
246 *(WORD *)(p+0x1e) = 0x3da; /* CRTC port address */
247 *(p+0x20) = 0x0; /* current setting of port 0x3x8 */
248 *(p+0x21) = 0x0; /* current setting of port 0x3x9 */
249 *(p+0x22) = 23; /* number of rows - 1 */
250 *(WORD *)(p+0x23) = 0x10; /* bytes/character */
251 *(p+0x25) = /* comb. of active display */
252 pBiosData->DisplayCombination;
253 *(p+0x26) = 0; /* DCC (???) of alternate display */
254 *(WORD *)(p+0x27) = 16; /* number of colors in current mode */
255 *(p+0x29) = 1; /* number of pages in current mode */
256 *(p+0x2a) = 3; /* number of scan lines active */
257 /* (0,1,2,3) = (200,350,400,480) */
258 *(p+0x2b) = 0; /* primary character block (?) */
259 *(p+0x2c) = 0; /* secondary character block (?) */
260 *(p+0x2d) = /* miscellaneous flags */
261 (pBiosData->VGASettings & 0x0f)
262 | ((pBiosData->ModeOptions & 1) << 4); /* cursor emulation */
263 *(p+0x2e) = 0; /* non-VGA mode support */
264 *(WORD *)(p+0x2f) = 0; /* reserved */
265 *(p+0x31) = /* video memory available */
266 (pBiosData->ModeOptions & 0x60 >> 5);
267 *(p+0x32) = 0; /* save pointer state flags */
268 *(p+0x33) = 4; /* display info and status */
270 /* BIOS date string */
271 strcpy((char *)pBiosSys+0xfff5, "13/01/99");
273 /* BIOS ID */
274 *(pBiosSys+0xfffe) = 0xfc;
277 /***********************************************************************
278 * DOSMEM_InitCollateTable
280 * Initialises the collate table (character sorting, language dependent)
282 static void DOSMEM_InitCollateTable()
284 DWORD x;
285 unsigned char *tbl;
286 int i;
288 x = GlobalDOSAlloc16(258);
289 DOSMEM_CollateTable = MAKELONG(0,(x>>16));
290 tbl = DOSMEM_MapRealToLinear(DOSMEM_CollateTable);
291 *(WORD*)tbl = 0x100;
292 tbl += 2;
293 for ( i = 0; i < 0x100; i++) *tbl++ = i;
296 /***********************************************************************
297 * DOSMEM_InitErrorTable
299 * Initialises the error tables (DOS 5+)
301 static void DOSMEM_InitErrorTable()
303 DWORD x;
304 char *call;
306 /* We will use a snippet of real mode code that calls */
307 /* a WINE-only interrupt to handle moving the requested */
308 /* message into the buffer... */
310 /* FIXME - There is still something wrong... */
312 /* FIXME - Find hex values for opcodes...
314 (On call, AX contains message number
315 DI contains 'offset' (??)
316 Resturn, ES:DI points to counted string )
318 PUSH BX
319 MOV BX, AX
320 MOV AX, (arbitrary subfunction number)
321 INT (WINE-only interrupt)
322 POP BX
327 const int code = 4;
328 const int buffer = 80;
329 const int SIZE_TO_ALLOCATE = code + buffer;
331 /* FIXME - Complete rewrite of the table system to save */
332 /* precious DOS space. Now, we return the 0001:???? as */
333 /* DOS 4+ (??, it seems to be the case in MS 7.10) treats that */
334 /* as a special case and programs will use the alternate */
335 /* interface (a farcall returned with INT 24 (AX = 0x122e, DL = */
336 /* 0x08) which lets us have a smaller memory footprint anyway. */
338 x = GlobalDOSAlloc16(SIZE_TO_ALLOCATE);
340 DOSMEM_ErrorCall = MAKELONG(0,(x>>16));
341 DOSMEM_ErrorBuffer = DOSMEM_ErrorCall + code;
343 call = DOSMEM_MapRealToLinear(DOSMEM_ErrorCall);
345 memset(call, 0, SIZE_TO_ALLOCATE);
347 /* Fixme - Copy assembly into buffer here */
350 /***********************************************************************
351 * DOSMEM_InitMemory
353 * Initialises the DOS memory structures.
355 static void DOSMEM_InitMemory(HMODULE16 hModule)
357 /* Low 64Kb are reserved for DOS/BIOS so the useable area starts at
358 * 1000:0000 and ends at 9FFF:FFEF. */
360 dosmem_info* info_block = DOSMEM_InfoBlock(hModule);
361 dosmem_entry* root_block = DOSMEM_RootBlock(hModule);
362 dosmem_entry* dm;
364 root_block->size = DOSMEM_MemoryTop(hModule) - (((char*)root_block) + sizeof(dosmem_entry));
366 info_block->blocks = 0;
367 info_block->free = root_block->size;
369 dm = NEXT_BLOCK(root_block);
370 dm->size = DM_BLOCK_TERMINAL;
371 root_block->size |= DM_BLOCK_FREE
372 #ifdef __DOSMEM_DEBUG__
373 | DM_BLOCK_DEBUG;
374 #endif
378 /***********************************************************************
379 * DOSMEM_Init
381 * Create the dos memory segments, and store them into the KERNEL
382 * exported values.
384 BOOL DOSMEM_Init(HMODULE16 hModule)
386 if (!hModule)
388 /* Allocate 1 MB dosmemory
389 * - it is mostly wasted but we can use some of it to
390 * store internal translation tables, etc...
392 DOSMEM_dosmem = VirtualAlloc( NULL, 0x100000, MEM_COMMIT,
393 PAGE_EXECUTE_READWRITE );
394 if (!DOSMEM_dosmem)
396 WARN(dosmem, "Could not allocate DOS memory.\n" );
397 return FALSE;
399 DOSMEM_BiosDataSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0x400,
400 0x100, 0, FALSE, FALSE, FALSE, NULL );
401 DOSMEM_BiosSysSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0xf0000,
402 0x10000, 0, FALSE, FALSE, FALSE, NULL );
403 DOSMEM_FillIsrTable(0);
404 DOSMEM_FillBiosSegments();
405 DOSMEM_InitMemory(0);
406 DOSMEM_InitCollateTable();
407 DOSMEM_InitErrorTable();
408 DOSMEM_InitDPMI();
410 else
412 #if 0
413 DOSMEM_FillIsrTable(hModule);
414 DOSMEM_InitMemory(hModule);
415 #else
416 /* bootstrap the new V86 task with a copy of the "system" memory */
417 memcpy(DOSMEM_MemoryBase(hModule), DOSMEM_dosmem, 0x100000);
418 #endif
420 return TRUE;
424 /***********************************************************************
425 * DOSMEM_Tick
427 * Increment the BIOS tick counter. Called by timer signal handler.
429 void DOSMEM_Tick( WORD timer )
431 BIOSDATA *pBiosData = DOSMEM_BiosData();
432 if (pBiosData) pBiosData->Ticks++;
435 /***********************************************************************
436 * DOSMEM_GetBlock
438 * Carve a chunk of the DOS memory block (without selector).
440 LPVOID DOSMEM_GetBlock(HMODULE16 hModule, UINT size, UINT16* pseg)
442 UINT blocksize;
443 char *block = NULL;
444 dosmem_info *info_block = DOSMEM_InfoBlock(hModule);
445 dosmem_entry *dm;
446 #ifdef __DOSMEM_DEBUG_
447 dosmem_entry *prev = NULL;
448 #endif
450 if( size > info_block->free ) return NULL;
451 dm = DOSMEM_RootBlock(hModule);
453 while (dm && dm->size != DM_BLOCK_TERMINAL)
455 #ifdef __DOSMEM_DEBUG__
456 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
458 WARN(dosmem,"MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
459 return NULL;
461 prev = dm;
462 #endif
463 if( dm->size & DM_BLOCK_FREE )
465 dosmem_entry *next = NEXT_BLOCK(dm);
467 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
469 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
470 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
471 next = NEXT_BLOCK(dm);
474 blocksize = dm->size & DM_BLOCK_MASK;
475 if( blocksize >= size )
477 block = ((char*)dm) + sizeof(dosmem_entry);
478 if( blocksize - size > 0x20 )
480 /* split dm so that the next one stays
481 * paragraph-aligned (and dm loses free bit) */
483 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
484 sizeof(dosmem_entry));
485 next = (dosmem_entry*)(((char*)dm) +
486 sizeof(dosmem_entry) + dm->size);
487 next->size = (blocksize - (dm->size +
488 sizeof(dosmem_entry))) | DM_BLOCK_FREE
489 #ifdef __DOSMEM_DEBUG__
490 | DM_BLOCK_DEBUG
491 #endif
493 } else dm->size &= DM_BLOCK_MASK;
495 info_block->blocks++;
496 info_block->free -= dm->size;
497 if( pseg ) *pseg = (block - DOSMEM_MemoryBase(hModule)) >> 4;
498 #ifdef __DOSMEM_DEBUG__
499 dm->size |= DM_BLOCK_DEBUG;
500 #endif
501 break;
503 dm = next;
505 else dm = NEXT_BLOCK(dm);
507 return (LPVOID)block;
510 /***********************************************************************
511 * DOSMEM_FreeBlock
513 BOOL DOSMEM_FreeBlock(HMODULE16 hModule, void* ptr)
515 dosmem_info *info_block = DOSMEM_InfoBlock(hModule);
517 if( ptr >= (void*)(((char*)DOSMEM_RootBlock(hModule)) + sizeof(dosmem_entry)) &&
518 ptr < (void*)DOSMEM_MemoryTop(hModule) && !((((char*)ptr)
519 - DOSMEM_MemoryBase(hModule)) & 0xf) )
521 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
523 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
524 #ifdef __DOSMEM_DEBUG__
525 && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
526 #endif
529 info_block->blocks--;
530 info_block->free += dm->size;
532 dm->size |= DM_BLOCK_FREE;
533 return TRUE;
536 return FALSE;
539 /***********************************************************************
540 * DOSMEM_ResizeBlock
542 LPVOID DOSMEM_ResizeBlock(HMODULE16 hModule, void* ptr, UINT size, UINT16* pseg)
544 char *block = NULL;
545 dosmem_info *info_block = DOSMEM_InfoBlock(hModule);
547 if( ptr >= (void*)(((char*)DOSMEM_RootBlock(hModule)) + sizeof(dosmem_entry)) &&
548 ptr < (void*)DOSMEM_MemoryTop(hModule) && !((((char*)ptr)
549 - DOSMEM_MemoryBase(hModule)) & 0xf) )
551 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
553 if( pseg ) *pseg = ((char*)ptr - DOSMEM_MemoryBase(hModule)) >> 4;
555 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
558 dosmem_entry *next = NEXT_BLOCK(dm);
559 UINT blocksize, orgsize = dm->size & DM_BLOCK_MASK;
561 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
563 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
564 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
565 next = NEXT_BLOCK(dm);
568 blocksize = dm->size & DM_BLOCK_MASK;
569 if (blocksize >= size)
571 block = ((char*)dm) + sizeof(dosmem_entry);
572 if( blocksize - size > 0x20 )
574 /* split dm so that the next one stays
575 * paragraph-aligned (and next gains free bit) */
577 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
578 sizeof(dosmem_entry));
579 next = (dosmem_entry*)(((char*)dm) +
580 sizeof(dosmem_entry) + dm->size);
581 next->size = (blocksize - (dm->size +
582 sizeof(dosmem_entry))) | DM_BLOCK_FREE
584 } else dm->size &= DM_BLOCK_MASK;
586 info_block->free += orgsize - dm->size;
587 } else {
588 /* the collapse didn't help, try getting a new block */
589 block = DOSMEM_GetBlock(hModule, size, pseg);
590 if (block) {
591 /* we got one, copy the old data there (we do need to, right?) */
592 memcpy(block, ((char*)dm) + sizeof(dosmem_entry),
593 (size<orgsize) ? size : orgsize);
594 /* free old block */
595 info_block->blocks--;
596 info_block->free += dm->size;
598 dm->size |= DM_BLOCK_FREE;
599 } else {
600 /* and Bill Gates said 640K should be enough for everyone... */
602 /* need to split original and collapsed blocks apart again,
603 * and free the collapsed blocks again, before exiting */
604 if( blocksize - orgsize > 0x20 )
606 /* split dm so that the next one stays
607 * paragraph-aligned (and next gains free bit) */
609 dm->size = (((orgsize + 0xf + sizeof(dosmem_entry)) & ~0xf) -
610 sizeof(dosmem_entry));
611 next = (dosmem_entry*)(((char*)dm) +
612 sizeof(dosmem_entry) + dm->size);
613 next->size = (blocksize - (dm->size +
614 sizeof(dosmem_entry))) | DM_BLOCK_FREE
616 } else dm->size &= DM_BLOCK_MASK;
621 return (LPVOID)block;
625 /***********************************************************************
626 * DOSMEM_Available
628 UINT DOSMEM_Available(HMODULE16 hModule)
630 UINT blocksize, available = 0;
631 dosmem_entry *dm;
633 dm = DOSMEM_RootBlock(hModule);
635 while (dm && dm->size != DM_BLOCK_TERMINAL)
637 #ifdef __DOSMEM_DEBUG__
638 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
640 WARN(dosmem,"MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
641 return NULL;
643 prev = dm;
644 #endif
645 if( dm->size & DM_BLOCK_FREE )
647 dosmem_entry *next = NEXT_BLOCK(dm);
649 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
651 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
652 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
653 next = NEXT_BLOCK(dm);
656 blocksize = dm->size & DM_BLOCK_MASK;
657 if ( blocksize > available ) available = blocksize;
658 dm = next;
660 else dm = NEXT_BLOCK(dm);
662 return available;
666 /***********************************************************************
667 * DOSMEM_MapLinearToDos
669 * Linear address to the DOS address space.
671 UINT DOSMEM_MapLinearToDos(LPVOID ptr)
673 if (((char*)ptr >= DOSMEM_MemoryBase(0)) &&
674 ((char*)ptr < DOSMEM_MemoryBase(0) + 0x100000))
675 return (UINT)ptr - (UINT)DOSMEM_MemoryBase(0);
676 return (UINT)ptr;
680 /***********************************************************************
681 * DOSMEM_MapDosToLinear
683 * DOS linear address to the linear address space.
685 LPVOID DOSMEM_MapDosToLinear(UINT ptr)
687 if (ptr < 0x100000) return (LPVOID)(ptr + (UINT)DOSMEM_MemoryBase(0));
688 return (LPVOID)ptr;
692 /***********************************************************************
693 * DOSMEM_MapRealToLinear
695 * Real mode DOS address into a linear pointer
697 LPVOID DOSMEM_MapRealToLinear(DWORD x)
699 LPVOID lin;
701 lin=DOSMEM_MemoryBase(0)+(x&0xffff)+(((x&0xffff0000)>>16)*16);
702 TRACE(selector,"(0x%08lx) returns 0x%p.\n",
703 x,lin );
704 return lin;
707 /***********************************************************************
708 * DOSMEM_AllocSelector
710 * Allocates a protected mode selector for a realmode segment.
712 WORD DOSMEM_AllocSelector(WORD realsel)
714 HMODULE16 hModule = GetModuleHandle16("KERNEL");
715 WORD sel;
717 sel=GLOBAL_CreateBlock(
718 GMEM_FIXED,DOSMEM_dosmem+realsel*16,0x10000,
719 hModule,FALSE,FALSE,FALSE,NULL
721 TRACE(selector,"(0x%04x) returns 0x%04x.\n",
722 realsel,sel
724 return sel;