Duplicate resource directory searching routines instead of calling
[wine/testsucceed.git] / msdos / dosmem.c
blob5339d10d01fd176c0a853a552462ca34a2d4ad11
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 WORD DOSMEM_0000H; /* segment at 0:0 */
32 WORD DOSMEM_BiosDataSeg; /* BIOS data segment at 0x40:0 */
33 WORD DOSMEM_BiosSysSeg; /* BIOS ROM segment at 0xf000:0 */
35 DWORD DOSMEM_CollateTable;
37 /* use 2 low bits of 'size' for the housekeeping */
39 #define DM_BLOCK_DEBUG 0xABE00000
40 #define DM_BLOCK_TERMINAL 0x00000001
41 #define DM_BLOCK_FREE 0x00000002
42 #define DM_BLOCK_MASK 0x001FFFFC
45 #define __DOSMEM_DEBUG__
48 typedef struct {
49 unsigned size;
50 } dosmem_entry;
52 typedef struct {
53 unsigned blocks;
54 unsigned free;
55 } dosmem_info;
57 #define NEXT_BLOCK(block) \
58 (dosmem_entry*)(((char*)(block)) + \
59 sizeof(dosmem_entry) + ((block)->size & DM_BLOCK_MASK))
61 #define VM_STUB(x) (0x90CF00CD|(x<<8)) /* INT x; IRET; NOP */
62 #define VM_STUB_SEGMENT 0xf000 /* BIOS segment */
64 /* DOS memory base */
65 static char *DOSMEM_dosmem;
66 /* bios area; normally at offset 0x400, but can be moved to trap NULL pointers */
67 static void *DOSMEM_biosdata;
69 /* various real-mode code stubs */
70 WORD DOSMEM_wrap_seg;
71 WORD DOSMEM_xms_seg;
72 WORD DOSMEM_dpmi_seg;
73 WORD DOSMEM_dpmi_sel;
75 /***********************************************************************
76 * DOSMEM_MemoryBase
78 * Gets the DOS memory base.
80 char *DOSMEM_MemoryBase(void)
82 return DOSMEM_dosmem;
85 /***********************************************************************
86 * DOSMEM_MemoryTop
88 * Gets the DOS memory top.
90 static char *DOSMEM_MemoryTop(void)
92 return DOSMEM_dosmem+0x9FFFC; /* 640K */
95 /***********************************************************************
96 * DOSMEM_InfoBlock
98 * Gets the DOS memory info block.
100 static dosmem_info *DOSMEM_InfoBlock(void)
102 return (dosmem_info*)(DOSMEM_dosmem+0x10000); /* 64K */
105 /***********************************************************************
106 * DOSMEM_RootBlock
108 * Gets the DOS memory root block.
110 static dosmem_entry *DOSMEM_RootBlock(void)
112 /* first block has to be paragraph-aligned */
113 return (dosmem_entry*)(((char*)DOSMEM_InfoBlock()) +
114 ((((sizeof(dosmem_info) + 0xf) & ~0xf) - sizeof(dosmem_entry))));
117 /***********************************************************************
118 * DOSMEM_FillIsrTable
120 * Fill the interrupt table with fake BIOS calls to BIOSSEG (0xf000).
122 * NOTES:
123 * Linux normally only traps INTs performed from or destined to BIOSSEG
124 * for us to handle, if the int_revectored table is empty. Filling the
125 * interrupt table with calls to INT stubs in BIOSSEG allows DOS programs
126 * to hook interrupts, as well as use their familiar retf tricks to call
127 * them, AND let Wine handle any unhooked interrupts transparently.
129 static void DOSMEM_FillIsrTable(void)
131 SEGPTR *isr = (SEGPTR*)DOSMEM_dosmem;
132 DWORD *stub = (DWORD*)((char*)isr + (VM_STUB_SEGMENT << 4));
133 int x;
135 for (x=0; x<256; x++) isr[x]=PTR_SEG_OFF_TO_SEGPTR(VM_STUB_SEGMENT,x*4);
136 for (x=0; x<256; x++) stub[x]=VM_STUB(x);
139 /***********************************************************************
140 * DOSMEM_InitDPMI
142 * Allocate the global DPMI RMCB wrapper.
144 static void DOSMEM_InitDPMI(void)
146 LPSTR ptr;
148 static const char wrap_code[]={
149 0xCD,0x31, /* int $0x31 */
150 0xCB /* lret */
153 static const char enter_xms[]=
155 /* XMS hookable entry point */
156 0xEB,0x03, /* jmp entry */
157 0x90,0x90,0x90, /* nop;nop;nop */
158 /* entry: */
159 /* real entry point */
160 /* for simplicity, we'll just use the same hook as DPMI below */
161 0xCD,0x31, /* int $0x31 */
162 0xCB /* lret */
165 static const char enter_pm[]=
167 0x50, /* pushw %ax */
168 0x52, /* pushw %dx */
169 0x55, /* pushw %bp */
170 0x89,0xE5, /* movw %sp,%bp */
171 /* get return CS */
172 0x8B,0x56,0x08, /* movw 8(%bp),%dx */
173 /* just call int 31 here to get into protected mode... */
174 /* it'll check whether it was called from dpmi_seg... */
175 0xCD,0x31, /* int $0x31 */
176 /* we are now in the context of a 16-bit relay call */
177 /* need to fixup our stack;
178 * 16-bit relay return address will be lost, but we won't worry quite yet */
179 0x8E,0xD0, /* movw %ax,%ss */
180 0x66,0x0F,0xB7,0xE5, /* movzwl %bp,%esp */
181 /* set return CS */
182 0x89,0x56,0x08, /* movw %dx,8(%bp) */
183 0x5D, /* popw %bp */
184 0x5A, /* popw %dx */
185 0x58, /* popw %ax */
186 0xCB /* lret */
189 ptr = DOSMEM_GetBlock( sizeof(wrap_code), &DOSMEM_wrap_seg );
190 memcpy( ptr, wrap_code, sizeof(wrap_code) );
191 ptr = DOSMEM_GetBlock( sizeof(enter_xms), &DOSMEM_xms_seg );
192 memcpy( ptr, enter_xms, sizeof(enter_xms) );
193 ptr = DOSMEM_GetBlock( sizeof(enter_pm), &DOSMEM_dpmi_seg );
194 memcpy( ptr, enter_pm, sizeof(enter_pm) );
195 DOSMEM_dpmi_sel = SELECTOR_AllocBlock( ptr, sizeof(enter_pm), SEGMENT_CODE, FALSE, FALSE );
198 BIOSDATA * DOSMEM_BiosData()
200 return (BIOSDATA *)DOSMEM_biosdata;
203 BYTE * DOSMEM_BiosSys()
205 return DOSMEM_dosmem+0xf0000;
208 struct _DOS_LISTOFLISTS * DOSMEM_LOL()
210 return (struct _DOS_LISTOFLISTS *)DOSMEM_MapRealToLinear
211 (PTR_SEG_OFF_TO_SEGPTR(HIWORD(DOS_LOLSeg),0));
214 /***********************************************************************
215 * DOSMEM_FillBiosSegments
217 * Fill the BIOS data segment with dummy values.
219 static void DOSMEM_FillBiosSegments(void)
221 BYTE *pBiosSys = DOSMEM_BiosSys();
222 BYTE *pBiosROMTable = pBiosSys+0xe6f5;
223 BIOSDATA *pBiosData = DOSMEM_BiosData();
225 /* bogus 0xe0xx addresses !! Adapt int 0x10/0x1b if change needed */
226 VIDEOFUNCTIONALITY *pVidFunc = (VIDEOFUNCTIONALITY *)(pBiosSys+0xe000);
227 VIDEOSTATE *pVidState = (VIDEOSTATE *)(pBiosSys+0xe010);
228 int i;
230 /* Clear all unused values */
231 memset( pBiosData, 0, sizeof(*pBiosData) );
232 memset( pVidFunc, 0, sizeof(*pVidFunc ) );
233 memset( pVidState, 0, sizeof(*pVidState) );
235 /* FIXME: should check the number of configured drives and ports */
237 pBiosData->Com1Addr = 0x3f8;
238 pBiosData->Com2Addr = 0x2f8;
239 pBiosData->Lpt1Addr = 0x378;
240 pBiosData->Lpt2Addr = 0x278;
241 pBiosData->InstalledHardware = 0x5463;
242 pBiosData->MemSize = 640;
243 pBiosData->NextKbdCharPtr = 0x1e;
244 pBiosData->FirstKbdCharPtr = 0x1e;
245 pBiosData->VideoMode = 3;
246 pBiosData->VideoColumns = 80;
247 pBiosData->VideoPageSize = 80 * 25 * 2;
248 pBiosData->VideoPageStartAddr = 0xb800;
249 pBiosData->VideoCtrlAddr = 0x3d4;
250 pBiosData->Ticks = INT1A_GetTicksSinceMidnight();
251 pBiosData->NbHardDisks = 2;
252 pBiosData->KbdBufferStart = 0x1e;
253 pBiosData->KbdBufferEnd = 0x3e;
254 pBiosData->RowsOnScreenMinus1 = 23;
255 pBiosData->BytesPerChar = 0x10;
256 pBiosData->ModeOptions = 0x64;
257 pBiosData->FeatureBitsSwitches = 0xf9;
258 pBiosData->VGASettings = 0x51;
259 pBiosData->DisplayCombination = 0x08;
260 pBiosData->DiskDataRate = 0;
262 /* fill ROM configuration table (values from Award) */
263 *(pBiosROMTable+0x0) = 0x08; /* number of bytes following LO */
264 *(pBiosROMTable+0x1) = 0x00; /* number of bytes following HI */
265 *(pBiosROMTable+0x2) = 0xfc; /* model */
266 *(pBiosROMTable+0x3) = 0x01; /* submodel */
267 *(pBiosROMTable+0x4) = 0x00; /* BIOS revision */
268 *(pBiosROMTable+0x5) = 0x74; /* feature byte 1 */
269 *(pBiosROMTable+0x6) = 0x00; /* feature byte 2 */
270 *(pBiosROMTable+0x7) = 0x00; /* feature byte 3 */
271 *(pBiosROMTable+0x8) = 0x00; /* feature byte 4 */
272 *(pBiosROMTable+0x9) = 0x00; /* feature byte 5 */
275 for (i = 0; i < 7; i++)
276 pVidFunc->ModeSupport[i] = 0xff;
278 pVidFunc->ScanlineSupport = 7;
279 pVidFunc->NumberCharBlocks = 0;
280 pVidFunc->ActiveCharBlocks = 0;
281 pVidFunc->MiscFlags = 0x8ff;
282 pVidFunc->SavePointerFlags = 0x3f;
284 pVidState->StaticFuncTable = 0xf000e000; /* FIXME: always real mode ? */
285 pVidState->VideoMode = pBiosData->VideoMode; /* needs updates! */
286 pVidState->NumberColumns = pBiosData->VideoColumns; /* needs updates! */
287 pVidState->RegenBufLen = 0;
288 pVidState->RegenBufAddr = 0;
290 for (i = 0; i < 8; i++)
291 pVidState->CursorPos[i] = 0;
293 pVidState->CursorType = 0x0a0b; /* start/end line */
294 pVidState->ActivePage = 0;
295 pVidState->CRTCPort = 0x3da;
296 pVidState->Port3x8 = 0;
297 pVidState->Port3x9 = 0;
298 pVidState->NumberRows = 23; /* number of rows - 1 */
299 pVidState->BytesPerChar = 0x10;
300 pVidState->DCCActive = pBiosData->DisplayCombination;
301 pVidState->DCCAlternate = 0;
302 pVidState->NumberColors = 16;
303 pVidState->NumberPages = 1;
304 pVidState->NumberScanlines = 3; /* (0,1,2,3) = (200,350,400,480) */
305 pVidState->CharBlockPrimary = 0;
306 pVidState->CharBlockSecondary = 0;
307 pVidState->MiscFlags =
308 (pBiosData->VGASettings & 0x0f)
309 | ((pBiosData->ModeOptions & 1) << 4); /* cursor emulation */
310 pVidState->NonVGASupport = 0;
311 pVidState->VideoMem = (pBiosData->ModeOptions & 0x60 >> 5);
312 pVidState->SavePointerState = 0;
313 pVidState->DisplayStatus = 4;
315 /* BIOS date string */
316 strcpy((char *)pBiosSys+0xfff5, "13/01/99");
318 /* BIOS ID */
319 *(pBiosSys+0xfffe) = 0xfc;
322 /***********************************************************************
323 * DOSMEM_InitCollateTable
325 * Initialises the collate table (character sorting, language dependent)
327 static void DOSMEM_InitCollateTable()
329 DWORD x;
330 unsigned char *tbl;
331 int i;
333 x = GlobalDOSAlloc16(258);
334 DOSMEM_CollateTable = MAKELONG(0,(x>>16));
335 tbl = DOSMEM_MapRealToLinear(DOSMEM_CollateTable);
336 *(WORD*)tbl = 0x100;
337 tbl += 2;
338 for ( i = 0; i < 0x100; i++) *tbl++ = i;
341 /***********************************************************************
342 * DOSMEM_InitErrorTable
344 * Initialises the error tables (DOS 5+)
346 static void DOSMEM_InitErrorTable()
348 #if 0 /* no longer used */
349 DWORD x;
350 char *call;
352 /* We will use a snippet of real mode code that calls */
353 /* a WINE-only interrupt to handle moving the requested */
354 /* message into the buffer... */
356 /* FIXME - There is still something wrong... */
358 /* FIXME - Find hex values for opcodes...
360 (On call, AX contains message number
361 DI contains 'offset' (??)
362 Resturn, ES:DI points to counted string )
364 PUSH BX
365 MOV BX, AX
366 MOV AX, (arbitrary subfunction number)
367 INT (WINE-only interrupt)
368 POP BX
373 const int code = 4;
374 const int buffer = 80;
375 const int SIZE_TO_ALLOCATE = code + buffer;
377 /* FIXME - Complete rewrite of the table system to save */
378 /* precious DOS space. Now, we return the 0001:???? as */
379 /* DOS 4+ (??, it seems to be the case in MS 7.10) treats that */
380 /* as a special case and programs will use the alternate */
381 /* interface (a farcall returned with INT 24 (AX = 0x122e, DL = */
382 /* 0x08) which lets us have a smaller memory footprint anyway. */
384 x = GlobalDOSAlloc16(SIZE_TO_ALLOCATE);
386 DOSMEM_ErrorCall = MAKELONG(0,(x>>16));
387 DOSMEM_ErrorBuffer = DOSMEM_ErrorCall + code;
389 call = DOSMEM_MapRealToLinear(DOSMEM_ErrorCall);
391 memset(call, 0, SIZE_TO_ALLOCATE);
392 #endif
393 /* Fixme - Copy assembly into buffer here */
396 /***********************************************************************
397 * DOSMEM_InitMemory
399 * Initialises the DOS memory structures.
401 static void DOSMEM_InitMemory(void)
403 /* Low 64Kb are reserved for DOS/BIOS so the useable area starts at
404 * 1000:0000 and ends at 9FFF:FFEF. */
406 dosmem_info* info_block = DOSMEM_InfoBlock();
407 dosmem_entry* root_block = DOSMEM_RootBlock();
408 dosmem_entry* dm;
410 root_block->size = DOSMEM_MemoryTop() - (((char*)root_block) + sizeof(dosmem_entry));
412 info_block->blocks = 0;
413 info_block->free = root_block->size;
415 dm = NEXT_BLOCK(root_block);
416 dm->size = DM_BLOCK_TERMINAL;
417 root_block->size |= DM_BLOCK_FREE
418 #ifdef __DOSMEM_DEBUG__
419 | DM_BLOCK_DEBUG;
420 #endif
424 /**********************************************************************
425 * setup_dos_mem
427 * Setup the first megabyte for DOS memory access
429 static void setup_dos_mem( int dos_init )
431 int bios_offset = 0x400;
432 int page_size = VIRTUAL_GetPageSize();
433 void *addr = VIRTUAL_mmap( -1, (void *)page_size, 0x110000-page_size, 0,
434 PROT_READ | PROT_WRITE | PROT_EXEC, 0 );
435 if (addr == (void *)page_size) /* we got what we wanted */
437 /* now map from address 0 */
438 addr = VIRTUAL_mmap( -1, NULL, 0x110000, 0,
439 PROT_READ | PROT_WRITE | PROT_EXEC, MAP_FIXED );
440 if (addr)
442 ERR("MAP_FIXED failed at address 0 for DOS address space\n" );
443 ExitProcess(1);
446 /* inform the memory manager that there is a mapping here */
447 VirtualAlloc( addr, 0x110000, MEM_RESERVE | MEM_SYSTEM, PAGE_EXECUTE_READWRITE );
449 /* protect the first 64K to catch NULL pointers */
450 if (!dos_init)
452 VirtualProtect( addr, 0x10000, PAGE_NOACCESS, NULL );
453 /* move the BIOS data from 0x400 to 0xf0400 */
454 bios_offset += 0xf0000;
457 else
459 ERR("Cannot use first megabyte for DOS address space, please report\n" );
460 if (dos_init) ExitProcess(1);
461 /* allocate the DOS area somewhere else */
462 addr = VirtualAlloc( NULL, 0x110000, MEM_COMMIT, PAGE_EXECUTE_READWRITE );
463 if (!addr)
465 ERR( "Cannot allocate DOS memory\n" );
466 ExitProcess(1);
469 DOSMEM_dosmem = addr;
470 DOSMEM_biosdata = (char *)addr + bios_offset;
474 /***********************************************************************
475 * DOSMEM_Init
477 * Create the dos memory segments, and store them into the KERNEL
478 * exported values.
480 BOOL DOSMEM_Init(BOOL dos_init)
482 static int already_done;
484 if (!already_done)
486 setup_dos_mem( dos_init );
488 DOSMEM_0000H = GLOBAL_CreateBlock( GMEM_FIXED, DOSMEM_biosdata - 0x400,
489 0x10000, 0, FALSE, FALSE, FALSE );
490 DOSMEM_BiosDataSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_biosdata,
491 0x100, 0, FALSE, FALSE, FALSE );
492 DOSMEM_BiosSysSeg = GLOBAL_CreateBlock(GMEM_FIXED,DOSMEM_dosmem+0xf0000,
493 0x10000, 0, FALSE, FALSE, FALSE );
494 DOSMEM_FillBiosSegments();
495 DOSMEM_InitMemory();
496 DOSMEM_InitCollateTable();
497 DOSMEM_InitErrorTable();
498 DOSMEM_InitDPMI();
499 DOSDEV_InstallDOSDevices();
500 already_done = 1;
502 else if (dos_init)
504 if (DOSMEM_dosmem)
506 ERR( "Needs access to the first megabyte for DOS mode\n" );
507 ExitProcess(1);
509 MESSAGE( "Warning: unprotecting the first 64KB of memory to allow real-mode calls.\n"
510 " NULL pointer accesses will no longer be caught.\n" );
511 VirtualProtect( NULL, 0x10000, PAGE_EXECUTE_READWRITE, NULL );
512 /* copy the BIOS area down to 0x400 */
513 memcpy( DOSMEM_dosmem + 0x400, DOSMEM_biosdata, 0x100 );
514 DOSMEM_biosdata = DOSMEM_dosmem + 0x400;
515 SetSelectorBase( DOSMEM_0000H, 0 );
516 SetSelectorBase( DOSMEM_BiosDataSeg, 0x400 );
518 /* interrupt table is at addr 0, so only do this when setting up DOS mode */
519 if (dos_init) DOSMEM_FillIsrTable();
520 return TRUE;
524 /***********************************************************************
525 * DOSMEM_Tick
527 * Increment the BIOS tick counter. Called by timer signal handler.
529 void DOSMEM_Tick( WORD timer )
531 BIOSDATA *pBiosData = DOSMEM_BiosData();
532 if (pBiosData) pBiosData->Ticks++;
535 /***********************************************************************
536 * DOSMEM_GetBlock
538 * Carve a chunk of the DOS memory block (without selector).
540 LPVOID DOSMEM_GetBlock(UINT size, UINT16* pseg)
542 UINT blocksize;
543 char *block = NULL;
544 dosmem_info *info_block = DOSMEM_InfoBlock();
545 dosmem_entry *dm;
546 #ifdef __DOSMEM_DEBUG_
547 dosmem_entry *prev = NULL;
548 #endif
550 if( size > info_block->free ) return NULL;
551 dm = DOSMEM_RootBlock();
553 while (dm && dm->size != DM_BLOCK_TERMINAL)
555 #ifdef __DOSMEM_DEBUG__
556 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
558 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
559 return NULL;
561 prev = dm;
562 #endif
563 if( dm->size & DM_BLOCK_FREE )
565 dosmem_entry *next = NEXT_BLOCK(dm);
567 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
569 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
570 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
571 next = NEXT_BLOCK(dm);
574 blocksize = dm->size & DM_BLOCK_MASK;
575 if( blocksize >= size )
577 block = ((char*)dm) + sizeof(dosmem_entry);
578 if( blocksize - size > 0x20 )
580 /* split dm so that the next one stays
581 * paragraph-aligned (and dm loses free bit) */
583 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
584 sizeof(dosmem_entry));
585 next = (dosmem_entry*)(((char*)dm) +
586 sizeof(dosmem_entry) + dm->size);
587 next->size = (blocksize - (dm->size +
588 sizeof(dosmem_entry))) | DM_BLOCK_FREE
589 #ifdef __DOSMEM_DEBUG__
590 | DM_BLOCK_DEBUG
591 #endif
593 } else dm->size &= DM_BLOCK_MASK;
595 info_block->blocks++;
596 info_block->free -= dm->size;
597 if( pseg ) *pseg = (block - DOSMEM_dosmem) >> 4;
598 #ifdef __DOSMEM_DEBUG__
599 dm->size |= DM_BLOCK_DEBUG;
600 #endif
601 break;
603 dm = next;
605 else dm = NEXT_BLOCK(dm);
607 return (LPVOID)block;
610 /***********************************************************************
611 * DOSMEM_FreeBlock
613 BOOL DOSMEM_FreeBlock(void* ptr)
615 dosmem_info *info_block = DOSMEM_InfoBlock();
617 if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
618 ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
619 - DOSMEM_dosmem) & 0xf) )
621 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
623 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
624 #ifdef __DOSMEM_DEBUG__
625 && ((dm->size & DM_BLOCK_DEBUG) == DM_BLOCK_DEBUG )
626 #endif
629 info_block->blocks--;
630 info_block->free += dm->size;
632 dm->size |= DM_BLOCK_FREE;
633 return TRUE;
636 return FALSE;
639 /***********************************************************************
640 * DOSMEM_ResizeBlock
642 LPVOID DOSMEM_ResizeBlock(void* ptr, UINT size, UINT16* pseg)
644 char *block = NULL;
645 dosmem_info *info_block = DOSMEM_InfoBlock();
647 if( ptr >= (void*)(((char*)DOSMEM_RootBlock()) + sizeof(dosmem_entry)) &&
648 ptr < (void*)DOSMEM_MemoryTop() && !((((char*)ptr)
649 - DOSMEM_dosmem) & 0xf) )
651 dosmem_entry *dm = (dosmem_entry*)(((char*)ptr) - sizeof(dosmem_entry));
653 if( pseg ) *pseg = ((char*)ptr - DOSMEM_dosmem) >> 4;
655 if( !(dm->size & (DM_BLOCK_FREE | DM_BLOCK_TERMINAL))
658 dosmem_entry *next = NEXT_BLOCK(dm);
659 UINT blocksize, orgsize = dm->size & DM_BLOCK_MASK;
661 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
663 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
664 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
665 next = NEXT_BLOCK(dm);
668 blocksize = dm->size & DM_BLOCK_MASK;
669 if (blocksize >= size)
671 block = ((char*)dm) + sizeof(dosmem_entry);
672 if( blocksize - size > 0x20 )
674 /* split dm so that the next one stays
675 * paragraph-aligned (and next gains free bit) */
677 dm->size = (((size + 0xf + sizeof(dosmem_entry)) & ~0xf) -
678 sizeof(dosmem_entry));
679 next = (dosmem_entry*)(((char*)dm) +
680 sizeof(dosmem_entry) + dm->size);
681 next->size = (blocksize - (dm->size +
682 sizeof(dosmem_entry))) | DM_BLOCK_FREE
684 } else dm->size &= DM_BLOCK_MASK;
686 info_block->free += orgsize - dm->size;
687 } else {
688 /* the collapse didn't help, try getting a new block */
689 block = DOSMEM_GetBlock(size, pseg);
690 if (block) {
691 /* we got one, copy the old data there (we do need to, right?) */
692 memcpy(block, ((char*)dm) + sizeof(dosmem_entry),
693 (size<orgsize) ? size : orgsize);
694 /* free old block */
695 info_block->blocks--;
696 info_block->free += dm->size;
698 dm->size |= DM_BLOCK_FREE;
699 } else {
700 /* and Bill Gates said 640K should be enough for everyone... */
702 /* need to split original and collapsed blocks apart again,
703 * and free the collapsed blocks again, before exiting */
704 if( blocksize - orgsize > 0x20 )
706 /* split dm so that the next one stays
707 * paragraph-aligned (and next gains free bit) */
709 dm->size = (((orgsize + 0xf + sizeof(dosmem_entry)) & ~0xf) -
710 sizeof(dosmem_entry));
711 next = (dosmem_entry*)(((char*)dm) +
712 sizeof(dosmem_entry) + dm->size);
713 next->size = (blocksize - (dm->size +
714 sizeof(dosmem_entry))) | DM_BLOCK_FREE
716 } else dm->size &= DM_BLOCK_MASK;
721 return (LPVOID)block;
725 /***********************************************************************
726 * DOSMEM_Available
728 UINT DOSMEM_Available(void)
730 UINT blocksize, available = 0;
731 dosmem_entry *dm;
733 dm = DOSMEM_RootBlock();
735 while (dm && dm->size != DM_BLOCK_TERMINAL)
737 #ifdef __DOSMEM_DEBUG__
738 if( (dm->size & DM_BLOCK_DEBUG) != DM_BLOCK_DEBUG )
740 WARN("MCB overrun! [prev = 0x%08x]\n", 4 + (UINT)prev);
741 return NULL;
743 prev = dm;
744 #endif
745 if( dm->size & DM_BLOCK_FREE )
747 dosmem_entry *next = NEXT_BLOCK(dm);
749 while( next->size & DM_BLOCK_FREE ) /* collapse free blocks */
751 dm->size += sizeof(dosmem_entry) + (next->size & DM_BLOCK_MASK);
752 next->size = (DM_BLOCK_FREE | DM_BLOCK_TERMINAL);
753 next = NEXT_BLOCK(dm);
756 blocksize = dm->size & DM_BLOCK_MASK;
757 if ( blocksize > available ) available = blocksize;
758 dm = next;
760 else dm = NEXT_BLOCK(dm);
762 return available;
766 /***********************************************************************
767 * DOSMEM_MapLinearToDos
769 * Linear address to the DOS address space.
771 UINT DOSMEM_MapLinearToDos(LPVOID ptr)
773 if (((char*)ptr >= DOSMEM_dosmem) &&
774 ((char*)ptr < DOSMEM_dosmem + 0x100000))
775 return (UINT)ptr - (UINT)DOSMEM_dosmem;
776 return (UINT)ptr;
780 /***********************************************************************
781 * DOSMEM_MapDosToLinear
783 * DOS linear address to the linear address space.
785 LPVOID DOSMEM_MapDosToLinear(UINT ptr)
787 if (ptr < 0x100000) return (LPVOID)(ptr + (UINT)DOSMEM_dosmem);
788 return (LPVOID)ptr;
792 /***********************************************************************
793 * DOSMEM_MapRealToLinear
795 * Real mode DOS address into a linear pointer
797 LPVOID DOSMEM_MapRealToLinear(DWORD x)
799 LPVOID lin;
801 lin=DOSMEM_dosmem+(x&0xffff)+(((x&0xffff0000)>>16)*16);
802 TRACE_(selector)("(0x%08lx) returns 0x%p.\n", x, lin );
803 return lin;
806 /***********************************************************************
807 * DOSMEM_AllocSelector
809 * Allocates a protected mode selector for a realmode segment.
811 WORD DOSMEM_AllocSelector(WORD realsel)
813 HMODULE16 hModule = GetModuleHandle16("KERNEL");
814 WORD sel;
816 sel=GLOBAL_CreateBlock(
817 GMEM_FIXED,DOSMEM_dosmem+realsel*16,0x10000,
818 hModule,FALSE,FALSE,FALSE );
819 TRACE_(selector)("(0x%04x) returns 0x%04x.\n", realsel,sel);
820 return sel;