2 Written by Alexander J. Russell 1994
4 Placed in the public Domain by Alec Russell, March 1995
6 Slightly higher level xms calls than xmem.asm
15 #include "src\lib\exmm\xmem.h"
17 xms_head_t xms_head={0}; // set handle to zero
20 /* ---------------------- alloc_xms() ----------------- February 19,1994 */
21 short alloc_xms(unsigned short far *size) // size in 16k blocks
23 return(XMS_alloc(0, size));
28 /* ---------------------- xms_to_mem() ---------------- February 19,1994 */
29 short xms_to_mem(unsigned short handle, void far *p, unsigned long off,
32 unsigned short block, boff;
34 block=off >> XMSBLOCKSHIFT;
35 boff=off - (block << XMSBLOCKSHIFT);
37 return(XMStoMem(handle, block, boff, n, p));
41 /* ---------------------- mem_to_xms() ---------------- February 19,1994 */
42 short mem_to_xms(unsigned short handle, void far *p, unsigned long off,
45 unsigned short block, boff;
47 block=off >> XMSBLOCKSHIFT;
48 boff=off - (block << XMSBLOCKSHIFT);
50 return(MemToXMS(handle, block, boff, n, p));
54 /* ---------------------- qalloc_xms() -------------------- March 8,1994 */
55 xms_node_t *qalloc_xms(unsigned long size)
57 xms_node_t *node=NULL;
60 if ( size <= xms_head.avail )
62 // look for existing node
66 if ( t1->used == 0 && t1->size >= size )
77 if ( node == NULL ) // didn't find existing node
79 node=malloc(sizeof(xms_node_t));
86 node->start=xms_head.next_off;
88 xms_head.next_off+=size;
89 if ( xms_head.next == NULL )
102 printf("out of near mem in qalloc_xms");
106 printf("out of xms mem in qalloc size %lu avail %lu", size,
113 /* ---------------------- qfree_xms() --------------------- March 8,1994 */
114 void qfree_xms(xms_node_t *node)
121 while ( t1 != node && t1 )
129 printf("ERROR didn't find node qfree");
133 printf("ATTEMPTED to qfree empty list");
139 /* ---------------------- xms_open() ---------------------- March 8,1994 */
140 xms_node_t *xms_open(char *file)
143 xms_node_t *node=NULL;
148 fp=fopen(file, "rb");
151 node=qalloc_xms(filelength(fileno(fp)));
158 while ( (i=fread(buffer, 1, 4096, fp)) )
160 mem_to_xms(xms_head.handle, (char far *)buffer,
168 printf("out of mem in xms_open 1");
174 printf("ERROR opening %s in xms_open", file);
180 /* ---------------------- xms_read() ---------------------- March 8,1994 */
181 short xms_read(void far *buffer, unsigned short n, xms_node_t *node)
184 if ( node->off >= node->size )
187 if ( n+node->off > node->size )
188 n=node->size - node->off;
190 xms_to_mem(xms_head.handle, buffer, node->start+node->off, n);
197 /* ---------------------- xms_write() ---------------------- March 8,1994 */
198 short xms_write(void far *buffer, unsigned short n, xms_node_t *node)
201 if ( node->off >= node->size )
204 if ( n+node->off > node->size )
205 n=node->size - node->off;
207 mem_to_xms(xms_head.handle, buffer, node->start+node->off, n);
214 /* ---------------------- xms_tell() ---------------------- March 8,1994 */
215 long xms_tell(xms_node_t *node)
222 /* ---------------------- xms_seek() ---------------------- March 8,1994 */
223 short xms_seek(xms_node_t *node, long off, short whence)
230 if ( off < 0l || off > node->size )
237 if ( off > 0l || (node->size + off) < 0l )
240 node->off=node->size + off;
244 if ( node->off + off < 0l || node->off + off > node->size )
255 /* ---------------------- xms_close() --------------------- March 8,1994 */
256 void xms_close(xms_node_t *node)
264 /* ---------------------- init_xms() ---------------------- March 8,1994 */
265 short init_xms(unsigned short min_blocks)
267 unsigned short blocks;
269 blocks=XMSblk_available();
270 if ( blocks >= min_blocks )
272 memset(&xms_head, 0, sizeof(xms_head_t));
273 if ( (xms_head.handle=alloc_xms(&blocks)) )
275 printf("blocks minus by = %u", blocks);
277 xms_head.avail=xms_head.total=(unsigned long)min_blocks*XMSBLOCK;
290 /* ---------------------- deinit_xms() -------------------- March 8,1994 */
291 void deinit_xms(void)
295 if ( xms_head.handle )
297 XMS_dealloc(xms_head.handle);
310 memset(&xms_head, 0, sizeof(xms_head_t));
316 /* --------------------------- end of file ------------------------- */
320 Not sure how to use this?
\r
322 call init_xms(x) to allocate a big chunk of xms.
\r
323 x is in 'blocks' of 16Kb. Pick X big enough to buffer all the files
\r
324 you want to place in xms.
\r
326 call xms_open("filename); for each file to be buffered. This copies the file
\r
329 then use xms_read(), xms_write(), and xms_seek() to read the file from
\r
330 xms instead of disk.
\r
332 call deinit_xms() just before exit to clean up.
\r
334 You can also use the lower level calls directly.
\r