1 /* This file implements rudimentary XMS memory handling.
2 * Documentation on the XMS API was found on http://www.qzx.com/pc-gpe/xms30.txt
5 #include "src\lib\xms.h"
7 /* Set up the XMS driver, returns 0 on success and non-zero on failure */
8 static int initxms(void)
12 if ( XMSControl == 0 )
15 ; Is an XMS driver installed?
21 if ( XMSStatus == 0x80 )
24 ; Get the address of the driver control function
27 mov word ptr [XMSControl] ,bx
28 mov word ptr [XMSControl+2],es
33 return ( XMSControl == 0 );
36 /* Allocate a slab of memory from XMS */
37 void huge * xmsmalloc(long unsigned int size)
40 unsigned int XMSStatus = 0;
41 unsigned int XMSHandle = 0;
42 void huge * XMSPointer = NULL;
45 /* If we can not initialize XMS, the allocation fails */
49 /* It is not possible to allocate more kilobytes than a 16-bit register can hold :-) */
50 if ( size / 1024 > UINT_MAX )
53 /* Get the next free entry in the handle <-> pointer mapping */
54 for ( n = 0; n < MAX_XMS_ALLOCATIONS; n++ )
56 if ( allocMapXMS[n].XMSPointer == NULL )
60 if ( n == MAX_XMS_ALLOCATIONS )
63 kB = size / 1024 + (size % 1024 > 0);
66 ; Allocate [kB] kilobytes of XMS memory
74 /* Check if XMS allocation failed */
79 ; Convert XMS handle to normal pointer
85 mov word ptr [XMSPointer], bx
86 mov word ptr [XMSPointer+2],dx
91 /* Lock failed, deallocate the handle */
98 ; Return value is not really interesting
104 /* Create an entry in the handle <-> pointer mapping */
105 allocMapXMS[n].XMSHandle = XMSHandle;
106 allocMapXMS[n].XMSPointer = XMSPointer;
111 /* Free a pointer allocated with xmsalloc */
112 void xmsfree(void huge * XMSPointer)
116 if ( XMSPointer == NULL )
122 for ( n = 0; n < MAX_XMS_ALLOCATIONS; n++ )
124 if ( allocMapXMS[n].XMSPointer == XMSPointer )
126 int XMSHandle = allocMapXMS[n].XMSHandle;
129 ; Unlock handle so we can free the memory block
139 ; Return value ignored
142 /* Clear handle <-> pointer map entry so it can be reused */
143 allocMapXMS[n].XMSHandle = 0;
144 allocMapXMS[n].XMSPointer = NULL;
151 /* Write a memory report for debugging purposes */
152 void xmsreport(void/*FILE * stream*/)
154 int XMSVersionNumber = 0;
155 unsigned int XMSLargestBlock = 0;
156 unsigned int XMSTotal = 0;
160 puts("Could not initialize XMS Driver!");
165 ; Get the driver version number
167 call [XMSControl] ; Get XMS Version Number
168 mov [XMSVersionNumber], ax
170 ; Get the amount of free XMS memory
173 mov [XMSLargestBlock], ax
177 //fprintf(stream, "XMS Version number: %d\n", XMSVersionNumber);
178 //fprintf(stream, "Largest available block: %d kB (%d kB total)\n", XMSLargestBlock, XMSTotal);
179 printf("XMS Version number: %d\n", XMSVersionNumber);
180 printf("Largest available block: %d kB (%d kB total)\n", XMSLargestBlock, XMSTotal);
185 xmsreport(fopen("xms.log", "w"));