Allow the size of bitmaps to be changed after toolbar buttons have
[wine.git] / msdos / dosmem.c
blob3967a39810221fd992edee27e5d785a7bbc81503
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 "debugtools.h"
21 DEFAULT_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 static char *DOSMEM_dosmem;
29 DWORD DOSMEM_CollateTable;
31 DWORD DOSMEM_ErrorCall;
32 DWORD DOSMEM_ErrorBuffer;
34 /* use 2 low bits of 'size' for the housekeeping */
36 #define DM_BLOCK_DEBUG 0xABE00000
37 #define DM_BLOCK_TERMINAL 0x00000001
38 #define DM_BLOCK_FREE 0x00000002
39 #define DM_BLOCK_MASK 0x001FFFFC
42 #define __DOSMEM_DEBUG__
45 typedef struct {
46 unsigned size;
47 } dosmem_entry;
49 typedef struct {
50 unsigned blocks;
51 unsigned free;
52 } dosmem_info;
54 #define NEXT_BLOCK(block) \
55 (dosmem_entry*)(((char*)(block)) + \
56 sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
58 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
59 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
61 /***********************************************************************
62 * DOSMEM_MemoryBase
64 * Gets the DOS memory base.
66 char *DOSMEM_MemoryBase(HMODULE16 hModule)
68 TDB *pTask = hModule ? NULL : (TDB *)GlobalLock16( GetCurrentTask() );
69 NE_MODULE *pModule = (hModule || pTask) ? NE_GetPtr( hModule ? hModule : pTask->hModule ) : NULL;
71 GlobalUnlock16( GetCurrentTask() );
72 if (pModule && pModule->dos_image)
73 return pModule->dos_image;
74 else
75 return DOSMEM_dosmem;
78 /***********************************************************************
79 * DOSMEM_MemoryTop
81 * Gets the DOS memory top.
83 static char *DOSMEM_MemoryTop(HMODULE16 hModule)
85 return DOSMEM_MemoryBase(hModule)+0x9FFFC; /* 640K */
88 /***********************************************************************
89 * DOSMEM_InfoBlock
91 * Gets the DOS memory info block.
93 static dosmem_info *DOSMEM_InfoBlock(HMODULE16 hModule)
95 return (dosmem_info*)(DOSMEM_MemoryBase(hModule)+0x10000); /* 64K */
98 /***********************************************************************
99 * DOSMEM_RootBlock
101 * Gets the DOS memory root block.
103 static dosmem_entry *DOSMEM_RootBlock(HMODULE16 hModule)
105 /* first block has to be paragraph-aligned */
106 return (dosmem_entry*)(((char*)DOSMEM_InfoBlock(hModule)) +
107 ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
110 /***********************************************************************
111 * DOSMEM_FillIsrTable
113 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
115 * NOTES:
116 * Linux normally only traps INTs performed from or destined to BIOSSEG
117 * for us to handle, if the int_revectored table is empty. Filling the
118 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
119 * to hook interrupts, as well as use their familiar retf tricks to call
120 * them, AND let Wine handle any unhooked interrupts transparently.
122 static void DOSMEM_FillIsrTable(HMODULE16 hModule)
124 SEGPTR *isr = (SEGPTR*)DOSMEM_MemoryBase(hModule);
125 DWORD *stub = (DWORD*)((char*)isr + (VM_STUB_SEGMENT << 4));
126 int x;
128 for (x=0; x<256; x++) isr[x]=PTR_SEG_OFF_TO_SEGPTR(VM_STUB_SEGMENT,x*4);
129 for (x=0; x<256; x++) stub[x]=VM_STUB(x);
132 /***********************************************************************
133 * DOSMEM_InitDPMI
135 * Allocate the global DPMI RMCB wrapper.
137 static void DOSMEM_InitDPMI(void)
139 extern UINT16 DPMI_wrap_seg;
140 static char wrap_code[]={
141 0xCD,0x31, /* int $0x31 */
142 0xCB /* lret */
144 LPSTR wrapper = (LPSTR)DOSMEM_GetBlock(0, sizeof(wrap_code), &DPMI_wrap_seg);
146 memcpy(wrapper, wrap_code, sizeof(wrap_code));
149 BIOSDATA * DOSMEM_BiosData()
151 return (BIOSDATA *)(DOSMEM_MemoryBase(0)+0x400);
154 BYTE * DOSMEM_BiosSys()
156 return DOSMEM_MemoryBase(0)+0xf0000;
159 struct _DOS_LISTOFLISTS * DOSMEM_LOL()
161 return (struct _DOS_LISTOFLISTS *)DOSMEM_MapRealToLinear
162 (PTR_SEG_OFF_TO_SEGPTR(HIWORD(DOS_LOLSeg),0));
165 /***********************************************************************
166 * DOSMEM_FillBiosSegments
168 * Fill the BIOS data segment with dummy values.
170 static void DOSMEM_FillBiosSegments(void)
172 BYTE *pBiosSys = (BYTE *)GlobalLock16( DOSMEM_BiosSysSeg );
173 BYTE *pBiosROMTable = pBiosSys+0xe6f5;
174 BIOSDATA *pBiosData = (BIOSDATA *)GlobalLock16( DOSMEM_BiosDataSeg );
176 /* bogus 0xe0xx addresses !! Adapt int 0x10/0x1b if change needed */
177 VIDEOFUNCTIONALITY *pVidFunc = (VIDEOFUNCTIONALITY *)(pBiosSys+0xe000);
178 VIDEOSTATE *pVidState = (VIDEOSTATE *)(pBiosSys+0xe010);
179 int i;
181 /* Clear all unused values */
182 memset( pBiosData, 0, sizeof(*pBiosData) );
183 memset( pVidFunc, 0, sizeof(*pVidFunc ) );
184 memset( pVidState, 0, sizeof(*pVidState) );
186 /* FIXME: should check the number of configured drives and ports */
188 pBiosData->Com1Addr = 0x3f8;
189 pBiosData->Com2Addr = 0x2f8;
190 pBiosData->Lpt1Addr = 0x378;
191 pBiosData->Lpt2Addr = 0x278;
192 pBiosData->InstalledHardware = 0x5463;
193 pBiosData->MemSize = 640;
194 pBiosData->NextKbdCharPtr = 0x1e;
195 pBiosData->FirstKbdCharPtr = 0x1e;
196 pBiosData->VideoMode = 3;
197 pBiosData->VideoColumns = 80;
198 pBiosData->VideoPageSize = 80 * 25 * 2;
199 pBiosData->VideoPageStartAddr = 0xb800;
200 pBiosData->VideoCtrlAddr = 0x3d4;
201 pBiosData->Ticks = INT1A_GetTicksSinceMidnight();
202 pBiosData->NbHardDisks = 2;
203 pBiosData->KbdBufferStart = 0x1e;
204 pBiosData->KbdBufferEnd = 0x3e;
205 pBiosData->RowsOnScreenMinus1 = 23;
206 pBiosData->BytesPerChar = 0x10;
207 pBiosData->ModeOptions = 0x64;
208 pBiosData->FeatureBitsSwitches = 0xf9;
209 pBiosData->VGASettings = 0x51;
210 pBiosData->DisplayCombination = 0x08;
211 pBiosData->DiskDataRate = 0;
213 /* fill ROM configuration table (values from Award) */
214 *(pBiosROMTable+0x0) = 0x08; /* number of bytes following LO */
215 *(pBiosROMTable+0x1) = 0x00; /* number of bytes following HI */
216 *(pBiosROMTable+0x2) = 0xfc; /* model */
217 *(pBiosROMTable+0x3) = 0x01; /* submodel */
218 *(pBiosROMTable+0x4) = 0x00; /* BIOS revision */
219 *(pBiosROMTable+0x5) = 0x74; /* feature byte 1 */
220 *(pBiosROMTable+0x6) = 0x00; /* feature byte 2 */
221 *(pBiosROMTable+0x7) = 0x00; /* feature byte 3 */
222 *(pBiosROMTable+0x8) = 0x00; /* feature byte 4 */
223 *(pBiosROMTable+0x9) = 0x00; /* feature byte 5 */
226 for (i = 0; i < 7; i++)
227 pVidFunc->ModeSupport[i] = 0xff;
229 pVidFunc->ScanlineSupport = 7;
230 pVidFunc->NumberCharBlocks = 0;
231 pVidFunc->ActiveCharBlocks = 0;
232 pVidFunc->MiscFlags = 0x8ff;
233 pVidFunc->SavePointerFlags = 0x3f;
235 pVidState->StaticFuncTable = 0xf000e000; /* FIXME: always real mode ? */
236 pVidState->VideoMode = pBiosData->VideoMode; /* needs updates! */
237 pVidState->NumberColumns = pBiosData->VideoColumns; /* needs updates! */
238 pVidState->RegenBufLen = 0;
239 pVidState->RegenBufAddr = 0;
241 for (i = 0; i < 8; i++)
242 pVidState->CursorPos[i] = 0;
244 pVidState->CursorType = 0x0a0b; /* start/end line */
245 pVidState->ActivePage = 0;
246 pVidState->CRTCPort = 0x3da;
247 pVidState->Port3x8 = 0;
248 pVidState->Port3x9 = 0;
249 pVidState->NumberRows = 23; /* number of rows - 1 */
250 pVidState->BytesPerChar = 0x10;
251 pVidState->DCCActive = pBiosData->DisplayCombination;
252 pVidState->DCCAlternate = 0;
253 pVidState->NumberColors = 16;
254 pVidState->NumberPages = 1;
255 pVidState->NumberScanlines = 3; /* (0,1,2,3) = (200,350,400,480) */
256 pVidState->CharBlockPrimary = 0;
257 pVidState->CharBlockSecondary = 0;
258 pVidState->MiscFlags =
259 (pBiosData->VGASettings & 0x0f)
260 | ((pBiosData->ModeOptions & 1) << 4); /* cursor emulation */
261 pVidState->NonVGASupport = 0;
262 pVidState->VideoMem = (pBiosData->ModeOptions & 0x60 >> 5);
263 pVidState->SavePointerState = 0;
264 pVidState->DisplayStatus = 4;
266 /* BIOS date string */
267 strcpy((char *)pBiosSys+0xfff5, "13/01/99");
269 /* BIOS ID */
270 *(pBiosSys+0xfffe) = 0xfc;
273 /***********************************************************************
274 * DOSMEM_InitCollateTable
276 * Initialises the collate table (character sorting, language dependent)
278 static void DOSMEM_InitCollateTable()
280 DWORD x;
281 unsigned char *tbl;
282 int i;
284 x = GlobalDOSAlloc16(258);
285 DOSMEM_CollateTable = MAKELONG(0,(x>>16));
286 tbl = DOSMEM_MapRealToLinear(DOSMEM_CollateTable);
287 *(WORD*)tbl = 0x100;
288 tbl += 2;
289 for ( i = 0; i < 0x100; i++) *tbl++ = i;
292 /***********************************************************************
293 * DOSMEM_InitErrorTable
295 * Initialises the error tables (DOS 5+)
297 static void DOSMEM_InitErrorTable()
299 DWORD x;
300 char *call;
302 /* We will use a snippet of real mode code that calls */
303 /* a WINE-only interrupt to handle moving the requested */
304 /* message into the buffer... */
306 /* FIXME - There is still something wrong... */
308 /* FIXME - Find hex values for opcodes...
310 (On call, AX contains message number
311 DI contains 'offset' (??)
312 Resturn, ES:DI points to counted string )
314 PUSH BX
315 MOV BX, AX
316 MOV AX, (arbitrary subfunction number)
317 INT (WINE-only interrupt)
318 POP BX
323 const int code = 4;
324 const int buffer = 80;
325 const int SIZE_TO_ALLOCATE = code + buffer;
327 /* FIXME - Complete rewrite of the table system to save */
328 /* precious DOS space. Now, we return the 0001:???? as */
329 /* DOS 4+ (??, it seems to be the case in MS 7.10) treats that */
330 /* as a special case and programs will use the alternate */
331 /* interface (a farcall returned with INT 24 (AX = 0x122e, DL = */
332 /* 0x08) which lets us have a smaller memory footprint anyway. */
334 x = GlobalDOSAlloc16(SIZE_TO_ALLOCATE);
336 DOSMEM_ErrorCall = MAKELONG(0,(x>>16));
337 DOSMEM_ErrorBuffer = DOSMEM_ErrorCall + code;
339 call = DOSMEM_MapRealToLinear(DOSMEM_ErrorCall);
341 memset(call, 0, SIZE_TO_ALLOCATE);
343 /* Fixme - Copy assembly into buffer here */
346 /***********************************************************************
347 * DOSMEM_InitMemory
349 * Initialises the DOS memory structures.
351 static void DOSMEM_InitMemory(HMODULE16 hModule)
353 /* Low 64Kb are reserved for DOS/BIOS so the useable area starts at
354 * 1000:0000 and ends at 9FFF:FFEF. */
356 dosmem_info* info_block = DOSMEM_InfoBlock(hModule);
357 dosmem_entry* root_block = DOSMEM_RootBlock(hModule);
358 dosmem_entry* dm;
360 root_block->size = DOSMEM_MemoryTop(hModule) - (((char*)root_block) + sizeof(dosmem_entry));
362 info_block->blocks = 0;
363 info_block->free = root_block->size;
365 dm = NEXT_BLOCK(root_block);
366 dm->size = DM_BLOCK_TERMINAL;
367 root_block->size |= DM_BLOCK_FREE
368 #ifdef __DOSMEM_DEBUG__
369 | DM_BLOCK_DEBUG;
370 #endif
374 /***********************************************************************
375 * DOSMEM_MovePointers
377 * Relocates any pointers into DOS memory to a new address space.
379 static void DOSMEM_MovePointers(LPVOID dest, LPVOID src, DWORD size)
381 unsigned long delta = (char *) dest - (char *) src;
382 unsigned cnt;
383 ldt_entry ent;
385 /* relocate base addresses of any selectors pointing into memory */
386 for (cnt=FIRST_LDT_ENTRY_TO_ALLOC; cnt<LDT_SIZE; cnt++) {
387 LDT_GetEntry(cnt, &ent);
388 if ((ent.base >= (unsigned long)src) && \
389 (ent.base < ((unsigned long)src + size))) {
390 ent.base += delta;
391 LDT_SetEntry(cnt, &ent);
396 /***********************************************************************
397 * DOSMEM_Init
399 * Create the dos memory segments, and store them into the KERNEL
400 * exported values.
402 BOOL DOSMEM_Init(HMODULE16 hModule)
404 if (!hModule)
406 /* Allocate 1 MB dosmemory
407 * - it is mostly wasted but we can use some of it to
408 * store internal translation tables, etc...
410 DOSMEM_dosmem = VirtualAlloc( NULL, 0x100000, MEM_COMMIT,
411 PAGE_EXECUTE_READWRITE );
412 if (!DOSMEM_dosmem)
414 WARN("Could not allocate DOS memory.\n" );
415 return FALSE;
417 DOSMEM_BiosDataSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0x400,
418 0x100, 0, FALSE, FALSE, FALSE, NULL );
419 DOSMEM_BiosSysSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0xf0000,
420 0x10000, 0, FALSE, FALSE, FALSE, NULL );
421 DOSMEM_FillIsrTable(0);
422 DOSMEM_FillBiosSegments();
423 DOSMEM_InitMemory(0);
424 DOSMEM_InitCollateTable();
425 DOSMEM_InitErrorTable();
426 DOSMEM_InitDPMI();
427 DOSDEV_InstallDOSDevices();
429 else
431 #if 0
432 DOSMEM_FillIsrTable(hModule);
433 DOSMEM_InitMemory(hModule);
434 #else
435 LPVOID base = DOSMEM_MemoryBase(hModule);
437 /* bootstrap the new V86 task with a copy of the "system" memory */
438 memcpy(base, DOSMEM_dosmem, 0x100000);
439 /* then move existing selectors to it */
440 DOSMEM_MovePointers(base, DOSMEM_dosmem, 0x100000);
441 #endif
443 return TRUE;
447 /***********************************************************************
448 * DOSMEM_Tick
450 * Increment the BIOS tick counter. Called by timer signal handler.
452 void DOSMEM_Tick( WORD timer )
454 BIOSDATA *pBiosData = DOSMEM_BiosData();
455 if (pBiosData) pBiosData->Ticks++;
458 /***********************************************************************
459 * DOSMEM_GetBlock
461 * Carve a chunk of the DOS memory block (without selector).
463 LPVOID DOSMEM_GetBlock(HMODULE16 hModule, UINT size, UINT16* pseg)
465 UINT blocksize;
466 char *block = NULL;
467 dosmem_info *info_block = DOSMEM_InfoBlock(hModule);
468 dosmem_entry *dm;
469 #ifdef __DOSMEM_DEBUG_
470 dosmem_entry *prev = NULL;
471 #endif
473 if( size > info_block->free ) return NULL;
474 dm = DOSMEM_RootBlock(hModule);
476 while (dm && dm->size != DM_BLOCK_TERMINAL)
478 #ifdef __DOSMEM_DEBUG__
479 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
481 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
482 return NULL;
484 prev = dm;
485 #endif
486 if( dm->size & DM_BLOCK_FREE )
488 dosmem_entry *next = NEXT_BLOCK(dm);
490 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
492 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
493 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
494 next = NEXT_BLOCK(dm);
497 blocksize = dm->size & DM_BLOCK_MASK;
498 if( blocksize >= size )
500 block = ((char*)dm) + sizeof(dosmem_entry);
501 if( blocksize - size > 0x20 )
503 /* split dm so that the next one stays
504 * paragraph-aligned (and dm loses free bit) */
506 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
507 sizeof(dosmem_entry));
508 next = (dosmem_entry*)(((char*)dm) +
509 sizeof(dosmem_entry) + dm->size);
510 next->size = (blocksize - (dm->size +
511 sizeof(dosmem_entry))) | DM_BLOCK_FREE
512 #ifdef __DOSMEM_DEBUG__
513 | DM_BLOCK_DEBUG
514 #endif
516 } else dm->size &= DM_BLOCK_MASK;
518 info_block->blocks++;
519 info_block->free -= dm->size;
520 if( pseg ) *pseg = (block - DOSMEM_MemoryBase(hModule)) >> 4;
521 #ifdef __DOSMEM_DEBUG__
522 dm->size |= DM_BLOCK_DEBUG;
523 #endif
524 break;
526 dm = next;
528 else dm = NEXT_BLOCK(dm);
530 return (LPVOID)block;
533 /***********************************************************************
534 * DOSMEM_FreeBlock
536 BOOL DOSMEM_FreeBlock(HMODULE16 hModule, void* ptr)
538 dosmem_info *info_block = DOSMEM_InfoBlock(hModule);
540 if( ptr >= (void*)(((char*)DOSMEM_RootBlock(hModule)) + sizeof(dosmem_entry)) &&
541 ptr < (void*)DOSMEM_MemoryTop(hModule) && !((((char*)ptr)
542 - DOSMEM_MemoryBase(hModule)) & 0xf) )
544 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
546 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
547 #ifdef __DOSMEM_DEBUG__
548 && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
549 #endif
552 info_block->blocks--;
553 info_block->free += dm->size;
555 dm->size |= DM_BLOCK_FREE;
556 return TRUE;
559 return FALSE;
562 /***********************************************************************
563 * DOSMEM_ResizeBlock
565 LPVOID DOSMEM_ResizeBlock(HMODULE16 hModule, void* ptr, UINT size, UINT16* pseg)
567 char *block = NULL;
568 dosmem_info *info_block = DOSMEM_InfoBlock(hModule);
570 if( ptr >= (void*)(((char*)DOSMEM_RootBlock(hModule)) + sizeof(dosmem_entry)) &&
571 ptr < (void*)DOSMEM_MemoryTop(hModule) && !((((char*)ptr)
572 - DOSMEM_MemoryBase(hModule)) & 0xf) )
574 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
576 if( pseg ) *pseg = ((char*)ptr - DOSMEM_MemoryBase(hModule)) >> 4;
578 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
581 dosmem_entry *next = NEXT_BLOCK(dm);
582 UINT blocksize, orgsize = dm->size & DM_BLOCK_MASK;
584 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
586 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
587 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
588 next = NEXT_BLOCK(dm);
591 blocksize = dm->size & DM_BLOCK_MASK;
592 if (blocksize >= size)
594 block = ((char*)dm) + sizeof(dosmem_entry);
595 if( blocksize - size > 0x20 )
597 /* split dm so that the next one stays
598 * paragraph-aligned (and next gains free bit) */
600 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
601 sizeof(dosmem_entry));
602 next = (dosmem_entry*)(((char*)dm) +
603 sizeof(dosmem_entry) + dm->size);
604 next->size = (blocksize - (dm->size +
605 sizeof(dosmem_entry))) | DM_BLOCK_FREE
607 } else dm->size &= DM_BLOCK_MASK;
609 info_block->free += orgsize - dm->size;
610 } else {
611 /* the collapse didn't help, try getting a new block */
612 block = DOSMEM_GetBlock(hModule, size, pseg);
613 if (block) {
614 /* we got one, copy the old data there (we do need to, right?) */
615 memcpy(block, ((char*)dm) + sizeof(dosmem_entry),
616 (size<orgsize) ? size : orgsize);
617 /* free old block */
618 info_block->blocks--;
619 info_block->free += dm->size;
621 dm->size |= DM_BLOCK_FREE;
622 } else {
623 /* and Bill Gates said 640K should be enough for everyone... */
625 /* need to split original and collapsed blocks apart again,
626 * and free the collapsed blocks again, before exiting */
627 if( blocksize - orgsize > 0x20 )
629 /* split dm so that the next one stays
630 * paragraph-aligned (and next gains free bit) */
632 dm->size = (((orgsize + 0xf + sizeof(dosmem_entry)) & ~0xf) -
633 sizeof(dosmem_entry));
634 next = (dosmem_entry*)(((char*)dm) +
635 sizeof(dosmem_entry) + dm->size);
636 next->size = (blocksize - (dm->size +
637 sizeof(dosmem_entry))) | DM_BLOCK_FREE
639 } else dm->size &= DM_BLOCK_MASK;
644 return (LPVOID)block;
648 /***********************************************************************
649 * DOSMEM_Available
651 UINT DOSMEM_Available(HMODULE16 hModule)
653 UINT blocksize, available = 0;
654 dosmem_entry *dm;
656 dm = DOSMEM_RootBlock(hModule);
658 while (dm && dm->size != DM_BLOCK_TERMINAL)
660 #ifdef __DOSMEM_DEBUG__
661 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
663 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
664 return NULL;
666 prev = dm;
667 #endif
668 if( dm->size & DM_BLOCK_FREE )
670 dosmem_entry *next = NEXT_BLOCK(dm);
672 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
674 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
675 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
676 next = NEXT_BLOCK(dm);
679 blocksize = dm->size & DM_BLOCK_MASK;
680 if ( blocksize > available ) available = blocksize;
681 dm = next;
683 else dm = NEXT_BLOCK(dm);
685 return available;
689 /***********************************************************************
690 * DOSMEM_MapLinearToDos
692 * Linear address to the DOS address space.
694 UINT DOSMEM_MapLinearToDos(LPVOID ptr)
696 if (((char*)ptr >= DOSMEM_MemoryBase(0)) &&
697 ((char*)ptr < DOSMEM_MemoryBase(0) + 0x100000))
698 return (UINT)ptr - (UINT)DOSMEM_MemoryBase(0);
699 return (UINT)ptr;
703 /***********************************************************************
704 * DOSMEM_MapDosToLinear
706 * DOS linear address to the linear address space.
708 LPVOID DOSMEM_MapDosToLinear(UINT ptr)
710 if (ptr < 0x100000) return (LPVOID)(ptr + (UINT)DOSMEM_MemoryBase(0));
711 return (LPVOID)ptr;
715 /***********************************************************************
716 * DOSMEM_MapRealToLinear
718 * Real mode DOS address into a linear pointer
720 LPVOID DOSMEM_MapRealToLinear(DWORD x)
722 LPVOID lin;
724 lin=DOSMEM_MemoryBase(0)+(x&0xffff)+(((x&0xffff0000)>>16)*16);
725 TRACE_(selector)("(0x%08lx) returns 0x%p.\n", x, lin );
726 return lin;
729 /***********************************************************************
730 * DOSMEM_AllocSelector
732 * Allocates a protected mode selector for a realmode segment.
734 WORD DOSMEM_AllocSelector(WORD realsel)
736 HMODULE16 hModule = GetModuleHandle16("KERNEL");
737 WORD sel;
739 sel=GLOBAL_CreateBlock(
740 GMEM_FIXED,DOSMEM_dosmem+realsel*16,0x10000,
741 hModule,FALSE,FALSE,FALSE,NULL
743 TRACE_(selector)("(0x%04x) returns 0x%04x.\n", realsel,sel);
744 return sel;