Initialize the output buffer parameters to 0 in RegQueryValue*
[wine.git] / msdos / dosmem.c
blob9699184fba2c03dc857fcdc575bbcd2872381d1b
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 DECLARE_DEBUG_CHANNEL(dosmem)
22 DECLARE_DEBUG_CHANNEL(selector)
24 HANDLE16 DOSMEM_BiosDataSeg; /* BIOS data segment at 0x40:0 */
25 HANDLE16 DOSMEM_BiosSysSeg; /* BIOS ROM segment at 0xf000:0 */
27 #pragma pack(4)
29 static char *DOSMEM_dosmem;
31 DWORD DOSMEM_CollateTable;
33 DWORD DOSMEM_ErrorCall;
34 DWORD DOSMEM_ErrorBuffer;
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 /***********************************************************************
64 * DOSMEM_MemoryBase
66 * Gets the DOS memory base.
68 char *DOSMEM_MemoryBase(HMODULE16 hModule)
70 TDB *pTask = hModule ? NULL : (TDB *)GlobalLock16( GetCurrentTask() );
71 NE_MODULE *pModule = (hModule || pTask) ? NE_GetPtr( hModule ? hModule : pTask->hModule ) : NULL;
73 GlobalUnlock16( GetCurrentTask() );
74 if (pModule && pModule->dos_image)
75 return pModule->dos_image;
76 else
77 return DOSMEM_dosmem;
80 /***********************************************************************
81 * DOSMEM_MemoryTop
83 * Gets the DOS memory top.
85 static char *DOSMEM_MemoryTop(HMODULE16 hModule)
87 return DOSMEM_MemoryBase(hModule)+0x9FFFC; /* 640K */
90 /***********************************************************************
91 * DOSMEM_InfoBlock
93 * Gets the DOS memory info block.
95 static dosmem_info *DOSMEM_InfoBlock(HMODULE16 hModule)
97 return (dosmem_info*)(DOSMEM_MemoryBase(hModule)+0x10000); /* 64K */
100 /***********************************************************************
101 * DOSMEM_RootBlock
103 * Gets the DOS memory root block.
105 static dosmem_entry *DOSMEM_RootBlock(HMODULE16 hModule)
107 /* first block has to be paragraph-aligned */
108 return (dosmem_entry*)(((char*)DOSMEM_InfoBlock(hModule)) +
109 ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
112 /***********************************************************************
113 * DOSMEM_FillIsrTable
115 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
117 * NOTES:
118 * Linux normally only traps INTs performed from or destined to BIOSSEG
119 * for us to handle, if the int_revectored table is empty. Filling the
120 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
121 * to hook interrupts, as well as use their familiar retf tricks to call
122 * them, AND let Wine handle any unhooked interrupts transparently.
124 static void DOSMEM_FillIsrTable(HMODULE16 hModule)
126 SEGPTR *isr = (SEGPTR*)DOSMEM_MemoryBase(hModule);
127 DWORD *stub = (DWORD*)((char*)isr + (VM_STUB_SEGMENT << 4));
128 int x;
130 for (x=0; x<256; x++) isr[x]=PTR_SEG_OFF_TO_SEGPTR(VM_STUB_SEGMENT,x*4);
131 for (x=0; x<256; x++) stub[x]=VM_STUB(x);
134 /***********************************************************************
135 * DOSMEM_InitDPMI
137 * Allocate the global DPMI RMCB wrapper.
139 static void DOSMEM_InitDPMI(void)
141 extern UINT16 DPMI_wrap_seg;
142 static char wrap_code[]={
143 0xCD,0x31, /* int $0x31 */
144 0xCB /* lret */
146 LPSTR wrapper = (LPSTR)DOSMEM_GetBlock(0, sizeof(wrap_code), &DPMI_wrap_seg);
148 memcpy(wrapper, wrap_code, sizeof(wrap_code));
151 BIOSDATA * DOSMEM_BiosData()
153 return (BIOSDATA *)(DOSMEM_MemoryBase(0)+0x400);
156 BYTE * DOSMEM_BiosSys()
158 return DOSMEM_MemoryBase(0)+0xf0000;
161 /***********************************************************************
162 * DOSMEM_FillBiosSegments
164 * Fill the BIOS data segment with dummy values.
166 static void DOSMEM_FillBiosSegments(void)
168 BYTE *pBiosSys = (BYTE *)GlobalLock16( DOSMEM_BiosSysSeg );
169 BYTE *pBiosROMTable = pBiosSys+0xe6f5;
170 BIOSDATA *pBiosData = (BIOSDATA *)GlobalLock16( DOSMEM_BiosDataSeg );
172 /* bogus 0xe0xx addresses !! Adapt int 0x10/0x1b if change needed */
173 BYTE *pVideoStaticFuncTable = pBiosSys+0xe000;
174 BYTE *pVideoStateInfo = pBiosSys+0xe010;
175 BYTE *p;
176 int i;
178 /* Clear all unused values */
179 memset( pBiosData, 0, sizeof(*pBiosData) );
181 /* FIXME: should check the number of configured drives and ports */
183 pBiosData->Com1Addr = 0x3f8;
184 pBiosData->Com2Addr = 0x2f8;
185 pBiosData->Lpt1Addr = 0x378;
186 pBiosData->Lpt2Addr = 0x278;
187 pBiosData->InstalledHardware = 0x5463;
188 pBiosData->MemSize = 640;
189 pBiosData->NextKbdCharPtr = 0x1e;
190 pBiosData->FirstKbdCharPtr = 0x1e;
191 pBiosData->VideoMode = 3;
192 pBiosData->VideoColumns = 80;
193 pBiosData->VideoPageSize = 80 * 25 * 2;
194 pBiosData->VideoPageStartAddr = 0xb800;
195 pBiosData->VideoCtrlAddr = 0x3d4;
196 pBiosData->Ticks = INT1A_GetTicksSinceMidnight();
197 pBiosData->NbHardDisks = 2;
198 pBiosData->KbdBufferStart = 0x1e;
199 pBiosData->KbdBufferEnd = 0x3e;
200 pBiosData->RowsOnScreenMinus1 = 23;
201 pBiosData->BytesPerChar = 0x10;
202 pBiosData->ModeOptions = 0x64;
203 pBiosData->FeatureBitsSwitches = 0xf9;
204 pBiosData->VGASettings = 0x51;
205 pBiosData->DisplayCombination = 0x08;
206 pBiosData->DiskDataRate = 0;
208 /* fill ROM configuration table (values from Award) */
209 *(WORD *)(pBiosROMTable)= 0x08; /* number of bytes following */
210 *(pBiosROMTable+0x2) = 0xfc; /* model */
211 *(pBiosROMTable+0x3) = 0x01; /* submodel */
212 *(pBiosROMTable+0x4) = 0x00; /* BIOS revision */
213 *(pBiosROMTable+0x5) = 0x74; /* feature byte 1 */
214 *(pBiosROMTable+0x6) = 0x00; /* feature byte 2 */
215 *(pBiosROMTable+0x7) = 0x00; /* feature byte 3 */
216 *(pBiosROMTable+0x8) = 0x00; /* feature byte 4 */
217 *(pBiosROMTable+0x9) = 0x00; /* feature byte 5 */
219 p = pVideoStaticFuncTable;
220 for (i=0; i < 7; i++)
221 *(p+i) = 0xff; /* modes supported 1 to 7 */
223 *(p+0x7) = 7; /* scan lines supported */
224 *(p+0x8) = 0; /* tot nr of char blocks in text mode */
225 *(p+0x9) = 0; /* max nr of active char blocks in text */
226 *(WORD *)(p+0xa) = 0x8ff; /* misc support flags */
227 *(WORD *)(p+0xc) = 0; /* reserved */
228 *(p+0xe) = 0x3f; /* save pointer function flags */
229 *(p+0xf) = 0; /* reserved */
231 p = pVideoStateInfo;
232 *(DWORD *)p = 0xf000e000; /* address of pVideoStaticFuncTable, FIXME: always real mode ? */
233 *(p+0x04) = /* current video mode, needs updates ! */
234 pBiosData->VideoMode;
235 *(WORD *)(p+0x05) = /* number of columns, needs updates ! */
236 pBiosData->VideoColumns;
237 *(WORD *)(p+0x07) = 0; /* length of regen (???) buffer in bytes */
238 *(WORD *)(p+0x09) = 0; /* starting address of regen (?) buffer */
239 *(WORD *)(p+0x0b) = 0; /* cursorpos page 0 */
240 *(WORD *)(p+0x0d) = 0; /* cursorpos page 1 */
241 *(WORD *)(p+0x0f) = 0; /* page 2 */
242 *(WORD *)(p+0x11) = 0; /* page 3 */
243 *(WORD *)(p+0x13) = 0; /* page 4 */
244 *(WORD *)(p+0x15) = 0; /* page 5 */
245 *(WORD *)(p+0x17) = 0; /* page 6 */
246 *(WORD *)(p+0x19) = 0; /* page 7 */
247 *(WORD *)(p+0x1b) = 0x0a0b; /* cursor size (start/end line) */
248 *(p+0x1d) = 0; /* active display page */
249 *(WORD *)(p+0x1e) = 0x3da; /* CRTC port address */
250 *(p+0x20) = 0x0; /* current setting of port 0x3x8 */
251 *(p+0x21) = 0x0; /* current setting of port 0x3x9 */
252 *(p+0x22) = 23; /* number of rows - 1 */
253 *(WORD *)(p+0x23) = 0x10; /* bytes/character */
254 *(p+0x25) = /* comb. of active display */
255 pBiosData->DisplayCombination;
256 *(p+0x26) = 0; /* DCC (???) of alternate display */
257 *(WORD *)(p+0x27) = 16; /* number of colors in current mode */
258 *(p+0x29) = 1; /* number of pages in current mode */
259 *(p+0x2a) = 3; /* number of scan lines active */
260 /* (0,1,2,3) = (200,350,400,480) */
261 *(p+0x2b) = 0; /* primary character block (?) */
262 *(p+0x2c) = 0; /* secondary character block (?) */
263 *(p+0x2d) = /* miscellaneous flags */
264 (pBiosData->VGASettings & 0x0f)
265 | ((pBiosData->ModeOptions & 1) << 4); /* cursor emulation */
266 *(p+0x2e) = 0; /* non-VGA mode support */
267 *(WORD *)(p+0x2f) = 0; /* reserved */
268 *(p+0x31) = /* video memory available */
269 (pBiosData->ModeOptions & 0x60 >> 5);
270 *(p+0x32) = 0; /* save pointer state flags */
271 *(p+0x33) = 4; /* display info and status */
273 /* BIOS date string */
274 strcpy((char *)pBiosSys+0xfff5, "13/01/99");
276 /* BIOS ID */
277 *(pBiosSys+0xfffe) = 0xfc;
280 /***********************************************************************
281 * DOSMEM_InitCollateTable
283 * Initialises the collate table (character sorting, language dependent)
285 static void DOSMEM_InitCollateTable()
287 DWORD x;
288 unsigned char *tbl;
289 int i;
291 x = GlobalDOSAlloc16(258);
292 DOSMEM_CollateTable = MAKELONG(0,(x>>16));
293 tbl = DOSMEM_MapRealToLinear(DOSMEM_CollateTable);
294 *(WORD*)tbl = 0x100;
295 tbl += 2;
296 for ( i = 0; i < 0x100; i++) *tbl++ = i;
299 /***********************************************************************
300 * DOSMEM_InitErrorTable
302 * Initialises the error tables (DOS 5+)
304 static void DOSMEM_InitErrorTable()
306 DWORD x;
307 char *call;
309 /* We will use a snippet of real mode code that calls */
310 /* a WINE-only interrupt to handle moving the requested */
311 /* message into the buffer... */
313 /* FIXME - There is still something wrong... */
315 /* FIXME - Find hex values for opcodes...
317 (On call, AX contains message number
318 DI contains 'offset' (??)
319 Resturn, ES:DI points to counted string )
321 PUSH BX
322 MOV BX, AX
323 MOV AX, (arbitrary subfunction number)
324 INT (WINE-only interrupt)
325 POP BX
330 const int code = 4;
331 const int buffer = 80;
332 const int SIZE_TO_ALLOCATE = code + buffer;
334 /* FIXME - Complete rewrite of the table system to save */
335 /* precious DOS space. Now, we return the 0001:???? as */
336 /* DOS 4+ (??, it seems to be the case in MS 7.10) treats that */
337 /* as a special case and programs will use the alternate */
338 /* interface (a farcall returned with INT 24 (AX = 0x122e, DL = */
339 /* 0x08) which lets us have a smaller memory footprint anyway. */
341 x = GlobalDOSAlloc16(SIZE_TO_ALLOCATE);
343 DOSMEM_ErrorCall = MAKELONG(0,(x>>16));
344 DOSMEM_ErrorBuffer = DOSMEM_ErrorCall + code;
346 call = DOSMEM_MapRealToLinear(DOSMEM_ErrorCall);
348 memset(call, 0, SIZE_TO_ALLOCATE);
350 /* Fixme - Copy assembly into buffer here */
353 /***********************************************************************
354 * DOSMEM_InitMemory
356 * Initialises the DOS memory structures.
358 static void DOSMEM_InitMemory(HMODULE16 hModule)
360 /* Low 64Kb are reserved for DOS/BIOS so the useable area starts at
361 * 1000:0000 and ends at 9FFF:FFEF. */
363 dosmem_info* info_block = DOSMEM_InfoBlock(hModule);
364 dosmem_entry* root_block = DOSMEM_RootBlock(hModule);
365 dosmem_entry* dm;
367 root_block->size = DOSMEM_MemoryTop(hModule) - (((char*)root_block) + sizeof(dosmem_entry));
369 info_block->blocks = 0;
370 info_block->free = root_block->size;
372 dm = NEXT_BLOCK(root_block);
373 dm->size = DM_BLOCK_TERMINAL;
374 root_block->size |= DM_BLOCK_FREE
375 #ifdef __DOSMEM_DEBUG__
376 | DM_BLOCK_DEBUG;
377 #endif
381 /***********************************************************************
382 * DOSMEM_Init
384 * Create the dos memory segments, and store them into the KERNEL
385 * exported values.
387 BOOL DOSMEM_Init(HMODULE16 hModule)
389 if (!hModule)
391 /* Allocate 1 MB dosmemory
392 * - it is mostly wasted but we can use some of it to
393 * store internal translation tables, etc...
395 DOSMEM_dosmem = VirtualAlloc( NULL, 0x100000, MEM_COMMIT,
396 PAGE_EXECUTE_READWRITE );
397 if (!DOSMEM_dosmem)
399 WARN(dosmem, "Could not allocate DOS memory.\n" );
400 return FALSE;
402 DOSMEM_BiosDataSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0x400,
403 0x100, 0, FALSE, FALSE, FALSE, NULL );
404 DOSMEM_BiosSysSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0xf0000,
405 0x10000, 0, FALSE, FALSE, FALSE, NULL );
406 DOSMEM_FillIsrTable(0);
407 DOSMEM_FillBiosSegments();
408 DOSMEM_InitMemory(0);
409 DOSMEM_InitCollateTable();
410 DOSMEM_InitErrorTable();
411 DOSMEM_InitDPMI();
413 else
415 #if 0
416 DOSMEM_FillIsrTable(hModule);
417 DOSMEM_InitMemory(hModule);
418 #else
419 /* bootstrap the new V86 task with a copy of the "system" memory */
420 memcpy(DOSMEM_MemoryBase(hModule), DOSMEM_dosmem, 0x100000);
421 #endif
423 return TRUE;
427 /***********************************************************************
428 * DOSMEM_Tick
430 * Increment the BIOS tick counter. Called by timer signal handler.
432 void DOSMEM_Tick( WORD timer )
434 BIOSDATA *pBiosData = DOSMEM_BiosData();
435 if (pBiosData) pBiosData->Ticks++;
438 /***********************************************************************
439 * DOSMEM_GetBlock
441 * Carve a chunk of the DOS memory block (without selector).
443 LPVOID DOSMEM_GetBlock(HMODULE16 hModule, UINT size, UINT16* pseg)
445 UINT blocksize;
446 char *block = NULL;
447 dosmem_info *info_block = DOSMEM_InfoBlock(hModule);
448 dosmem_entry *dm;
449 #ifdef __DOSMEM_DEBUG_
450 dosmem_entry *prev = NULL;
451 #endif
453 if( size > info_block->free ) return NULL;
454 dm = DOSMEM_RootBlock(hModule);
456 while (dm && dm->size != DM_BLOCK_TERMINAL)
458 #ifdef __DOSMEM_DEBUG__
459 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
461 WARN(dosmem,"MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
462 return NULL;
464 prev = dm;
465 #endif
466 if( dm->size & DM_BLOCK_FREE )
468 dosmem_entry *next = NEXT_BLOCK(dm);
470 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
472 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
473 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
474 next = NEXT_BLOCK(dm);
477 blocksize = dm->size & DM_BLOCK_MASK;
478 if( blocksize >= size )
480 block = ((char*)dm) + sizeof(dosmem_entry);
481 if( blocksize - size > 0x20 )
483 /* split dm so that the next one stays
484 * paragraph-aligned (and dm loses free bit) */
486 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
487 sizeof(dosmem_entry));
488 next = (dosmem_entry*)(((char*)dm) +
489 sizeof(dosmem_entry) + dm->size);
490 next->size = (blocksize - (dm->size +
491 sizeof(dosmem_entry))) | DM_BLOCK_FREE
492 #ifdef __DOSMEM_DEBUG__
493 | DM_BLOCK_DEBUG
494 #endif
496 } else dm->size &= DM_BLOCK_MASK;
498 info_block->blocks++;
499 info_block->free -= dm->size;
500 if( pseg ) *pseg = (block - DOSMEM_MemoryBase(hModule)) >> 4;
501 #ifdef __DOSMEM_DEBUG__
502 dm->size |= DM_BLOCK_DEBUG;
503 #endif
504 break;
506 dm = next;
508 else dm = NEXT_BLOCK(dm);
510 return (LPVOID)block;
513 /***********************************************************************
514 * DOSMEM_FreeBlock
516 BOOL DOSMEM_FreeBlock(HMODULE16 hModule, void* ptr)
518 dosmem_info *info_block = DOSMEM_InfoBlock(hModule);
520 if( ptr >= (void*)(((char*)DOSMEM_RootBlock(hModule)) + sizeof(dosmem_entry)) &&
521 ptr < (void*)DOSMEM_MemoryTop(hModule) && !((((char*)ptr)
522 - DOSMEM_MemoryBase(hModule)) & 0xf) )
524 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
526 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
527 #ifdef __DOSMEM_DEBUG__
528 && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
529 #endif
532 info_block->blocks--;
533 info_block->free += dm->size;
535 dm->size |= DM_BLOCK_FREE;
536 return TRUE;
539 return FALSE;
542 /***********************************************************************
543 * DOSMEM_ResizeBlock
545 LPVOID DOSMEM_ResizeBlock(HMODULE16 hModule, void* ptr, UINT size, UINT16* pseg)
547 char *block = NULL;
548 dosmem_info *info_block = DOSMEM_InfoBlock(hModule);
550 if( ptr >= (void*)(((char*)DOSMEM_RootBlock(hModule)) + sizeof(dosmem_entry)) &&
551 ptr < (void*)DOSMEM_MemoryTop(hModule) && !((((char*)ptr)
552 - DOSMEM_MemoryBase(hModule)) & 0xf) )
554 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
556 if( pseg ) *pseg = ((char*)ptr - DOSMEM_MemoryBase(hModule)) >> 4;
558 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
561 dosmem_entry *next = NEXT_BLOCK(dm);
562 UINT blocksize, orgsize = dm->size & DM_BLOCK_MASK;
564 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
566 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
567 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
568 next = NEXT_BLOCK(dm);
571 blocksize = dm->size & DM_BLOCK_MASK;
572 if (blocksize >= size)
574 block = ((char*)dm) + sizeof(dosmem_entry);
575 if( blocksize - size > 0x20 )
577 /* split dm so that the next one stays
578 * paragraph-aligned (and next gains free bit) */
580 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
581 sizeof(dosmem_entry));
582 next = (dosmem_entry*)(((char*)dm) +
583 sizeof(dosmem_entry) + dm->size);
584 next->size = (blocksize - (dm->size +
585 sizeof(dosmem_entry))) | DM_BLOCK_FREE
587 } else dm->size &= DM_BLOCK_MASK;
589 info_block->free += orgsize - dm->size;
590 } else {
591 /* the collapse didn't help, try getting a new block */
592 block = DOSMEM_GetBlock(hModule, size, pseg);
593 if (block) {
594 /* we got one, copy the old data there (we do need to, right?) */
595 memcpy(block, ((char*)dm) + sizeof(dosmem_entry),
596 (size<orgsize) ? size : orgsize);
597 /* free old block */
598 info_block->blocks--;
599 info_block->free += dm->size;
601 dm->size |= DM_BLOCK_FREE;
602 } else {
603 /* and Bill Gates said 640K should be enough for everyone... */
605 /* need to split original and collapsed blocks apart again,
606 * and free the collapsed blocks again, before exiting */
607 if( blocksize - orgsize > 0x20 )
609 /* split dm so that the next one stays
610 * paragraph-aligned (and next gains free bit) */
612 dm->size = (((orgsize + 0xf + sizeof(dosmem_entry)) & ~0xf) -
613 sizeof(dosmem_entry));
614 next = (dosmem_entry*)(((char*)dm) +
615 sizeof(dosmem_entry) + dm->size);
616 next->size = (blocksize - (dm->size +
617 sizeof(dosmem_entry))) | DM_BLOCK_FREE
619 } else dm->size &= DM_BLOCK_MASK;
624 return (LPVOID)block;
628 /***********************************************************************
629 * DOSMEM_Available
631 UINT DOSMEM_Available(HMODULE16 hModule)
633 UINT blocksize, available = 0;
634 dosmem_entry *dm;
636 dm = DOSMEM_RootBlock(hModule);
638 while (dm && dm->size != DM_BLOCK_TERMINAL)
640 #ifdef __DOSMEM_DEBUG__
641 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
643 WARN(dosmem,"MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
644 return NULL;
646 prev = dm;
647 #endif
648 if( dm->size & DM_BLOCK_FREE )
650 dosmem_entry *next = NEXT_BLOCK(dm);
652 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
654 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
655 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
656 next = NEXT_BLOCK(dm);
659 blocksize = dm->size & DM_BLOCK_MASK;
660 if ( blocksize > available ) available = blocksize;
661 dm = next;
663 else dm = NEXT_BLOCK(dm);
665 return available;
669 /***********************************************************************
670 * DOSMEM_MapLinearToDos
672 * Linear address to the DOS address space.
674 UINT DOSMEM_MapLinearToDos(LPVOID ptr)
676 if (((char*)ptr >= DOSMEM_MemoryBase(0)) &&
677 ((char*)ptr < DOSMEM_MemoryBase(0) + 0x100000))
678 return (UINT)ptr - (UINT)DOSMEM_MemoryBase(0);
679 return (UINT)ptr;
683 /***********************************************************************
684 * DOSMEM_MapDosToLinear
686 * DOS linear address to the linear address space.
688 LPVOID DOSMEM_MapDosToLinear(UINT ptr)
690 if (ptr < 0x100000) return (LPVOID)(ptr + (UINT)DOSMEM_MemoryBase(0));
691 return (LPVOID)ptr;
695 /***********************************************************************
696 * DOSMEM_MapRealToLinear
698 * Real mode DOS address into a linear pointer
700 LPVOID DOSMEM_MapRealToLinear(DWORD x)
702 LPVOID lin;
704 lin=DOSMEM_MemoryBase(0)+(x&0xffff)+(((x&0xffff0000)>>16)*16);
705 TRACE(selector,"(0x%08lx) returns 0x%p.\n",
706 x,lin );
707 return lin;
710 /***********************************************************************
711 * DOSMEM_AllocSelector
713 * Allocates a protected mode selector for a realmode segment.
715 WORD DOSMEM_AllocSelector(WORD realsel)
717 HMODULE16 hModule = GetModuleHandle16("KERNEL");
718 WORD sel;
720 sel=GLOBAL_CreateBlock(
721 GMEM_FIXED,DOSMEM_dosmem+realsel*16,0x10000,
722 hModule,FALSE,FALSE,FALSE,NULL
724 TRACE(selector,"(0x%04x) returns 0x%04x.\n",
725 realsel,sel
727 return sel;