When freeing XMS memory block, return an error if block has not been
[wine/hacks.git] / dlls / winedos / xms.c
blobfc03cb67a92288cccf65c950c82e4b4ecb35065e
1 /*
2 * XMS v2+ emulation
4 * Copyright 1998 Ove Kåven
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Note: This XMS emulation is hooked through the DPMI interrupt.
23 #include <unistd.h>
24 #include <string.h>
25 #include "winbase.h"
26 #include "wine/winbase16.h"
27 #include "module.h"
28 #include "miscemu.h"
29 #include "toolhelp.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(int31);
34 typedef struct {
35 WORD Handle;
36 DWORD Offset;
37 } WINE_PACKED MOVEOFS;
39 typedef struct {
40 DWORD Length;
41 MOVEOFS Source;
42 MOVEOFS Dest;
43 } WINE_PACKED MOVESTRUCT;
45 static BYTE * XMS_Offset( MOVEOFS *ofs )
47 if (ofs->Handle) return (BYTE*)GlobalLock16(ofs->Handle)+ofs->Offset;
48 else return (BYTE*)DOSMEM_MapRealToLinear(ofs->Offset);
51 /**********************************************************************
52 * XMS_Handler
55 void WINAPI XMS_Handler( CONTEXT86 *context )
57 switch(AH_reg(context))
59 case 0x00: /* Get XMS version number */
60 TRACE("get XMS version number\n");
61 AX_reg(context) = 0x0200; /* 2.0 */
62 BX_reg(context) = 0x0000; /* internal revision */
63 DX_reg(context) = 0x0001; /* HMA exists */
64 break;
65 case 0x08: /* Query Free Extended Memory */
67 MEMMANINFO mmi;
69 TRACE("query free extended memory\n");
70 mmi.dwSize = sizeof(mmi);
71 MemManInfo16(&mmi);
72 AX_reg(context) = mmi.dwLargestFreeBlock >> 10;
73 DX_reg(context) = (mmi.dwFreePages * mmi.wPageSize) >> 10;
74 TRACE("returning largest %dK, total %dK\n", AX_reg(context), DX_reg(context));
76 break;
77 case 0x09: /* Allocate Extended Memory Block */
78 TRACE("allocate extended memory block (%dK)\n",
79 DX_reg(context));
80 DX_reg(context) = GlobalAlloc16(GMEM_MOVEABLE,
81 (DWORD)DX_reg(context)<<10);
82 AX_reg(context) = DX_reg(context) ? 1 : 0;
83 if (!DX_reg(context)) BL_reg(context) = 0xA0; /* out of memory */
84 break;
85 case 0x0a: /* Free Extended Memory Block */
86 TRACE("free extended memory block %04x\n",DX_reg(context));
87 if(!DX_reg(context) || GlobalFree16(DX_reg(context))) {
88 AX_reg(context) = 0; /* failure */
89 BL_reg(context) = 0xa2; /* invalid handle */
90 } else
91 AX_reg(context) = 1; /* success */
92 break;
93 case 0x0b: /* Move Extended Memory Block */
95 MOVESTRUCT*move=CTX_SEG_OFF_TO_LIN(context,
96 context->SegDs,context->Esi);
97 BYTE*src,*dst;
98 TRACE("move extended memory block\n");
99 src=XMS_Offset(&move->Source);
100 dst=XMS_Offset(&move->Dest);
101 memcpy(dst,src,move->Length);
102 if (move->Source.Handle) GlobalUnlock16(move->Source.Handle);
103 if (move->Dest.Handle) GlobalUnlock16(move->Dest.Handle);
104 break;
106 default:
107 INT_BARF( context, 0x31 );
108 AX_reg(context) = 0x0000; /* failure */
109 BL_reg(context) = 0x80; /* function not implemented */
110 break;