4 Copyright 1994 Alec Russell, ALL rights reserved
5 Permission granted to use as you wish.
7 Slightly higher level xms calls than xmem.asm
18 xms_head_t xms_head={0}; // set handle to zero
21 /* ---------------------- alloc_xms() ----------------- February 19,1994 */
22 short alloc_xms(unsigned short far *size) // size in 16k blocks
24 return(XMS_alloc(0, size));
27 /* ---------------------- xms_to_mem() ---------------- February 19,1994 */
28 short xms_to_mem(unsigned short handle, void far *p, unsigned long off, unsigned short n)
30 unsigned short block, boff;
32 block=off >> XMSBLOCKSHIFT;
33 boff=off - (block << XMSBLOCKSHIFT);
35 return(XMStoMem(handle, block, boff, n, p));
38 /* ---------------------- mem_to_xms() ---------------- February 19,1994 */
39 short mem_to_xms(unsigned short handle, void far *p, unsigned long off, unsigned short n)
41 unsigned short block, boff;
43 block=off >> XMSBLOCKSHIFT;
44 boff=off - (block << XMSBLOCKSHIFT);
46 return(MemToXMS(handle, block, boff, n, p));
49 /* ---------------------- qalloc_xms() -------------------- March 8,1994 */
50 xms_node_t *qalloc_xms(unsigned long size)
52 xms_node_t *node=NULL;
55 if ( size <= xms_head.avail )
57 // look for existing node
61 if ( t1->used == 0 && t1->size >= size )
72 if ( node == NULL ) // didn't find existing node
74 node=malloc(sizeof(xms_node_t));
81 node->start=xms_head.next_off;
83 xms_head.next_off+=size;
84 if ( xms_head.next == NULL )
97 pr2("out of near mem in qalloc_xms");
101 pr2("out of xms mem in qalloc size %lu avail %lu", size, xms_head.avail);
106 /* ---------------------- qfree_xms() --------------------- March 8,1994 */
107 void qfree_xms(xms_node_t *node)
114 while ( t1 != node && t1 )
122 pr2("ERROR didn't find node qfree");
126 pr2("ATTEMPTED to qfree empty list");
130 /* ---------------------- xms_open() ---------------------- March 8,1994 */
131 xms_node_t *xms_open(char *file)
134 xms_node_t *node=NULL;
139 fp=fopen(file, "rb");
142 node=qalloc_xms(filelength(fileno(fp)));
149 while ( (i=fread(buffer, 1, 4096, fp)) )
151 mem_to_xms(xms_head.handle, (char far *)buffer, off+node->start, i);
158 pr2("out of mem in xms_open 1");
164 pr2("ERROR opening %s in xms_open", file);
169 /* ---------------------- xms_read() ---------------------- March 8,1994 */
170 short xms_read(void far *buffer, unsigned short n, xms_node_t *node)
173 if ( node->off >= node->size )
176 if ( n+node->off > node->size )
177 n=node->size - node->off;
179 xms_to_mem(xms_head.handle, buffer, node->start+node->off, n);
185 /* ---------------------- xms_write() ---------------------- March 8,1994 */
186 short xms_write(void far *buffer, unsigned short n, xms_node_t *node)
189 if ( node->off >= node->size )
192 if ( n+node->off > node->size )
193 n=node->size - node->off;
195 mem_to_xms(xms_head.handle, buffer, node->start+node->off, n);
202 /* ---------------------- xms_tell() ---------------------- March 8,1994 */
203 long xms_tell(xms_node_t *node)
208 /* ---------------------- xms_seek() ---------------------- March 8,1994 */
209 short xms_seek(xms_node_t *node, long off, short whence)
216 if ( off < 0l || off > node->size )
223 if ( off > 0l || (node->size + off) < 0l )
226 node->off=node->size + off;
230 if ( node->off + off < 0l || node->off + off > node->size )
240 /* ---------------------- xms_close() --------------------- March 8,1994 */
241 void xms_close(xms_node_t *node)
246 /* ---------------------- init_xms() ---------------------- March 8,1994 */
247 short init_xms(unsigned short min_blocks)
249 unsigned short blocks;
251 blocks=XMSblk_available();
252 if ( blocks >= min_blocks )
254 memset(&xms_head, 0, sizeof(xms_head_t));
255 if ( (xms_head.handle=alloc_xms(&blocks)) )
257 pr2("blocks minus by = %u", blocks);
259 xms_head.avail=xms_head.total=(unsigned long)min_blocks*XMSBLOCK;
271 /* ---------------------- deinit_xms() -------------------- March 8,1994 */
272 void deinit_xms(void)
276 if ( xms_head.handle )
278 XMS_dealloc(xms_head.handle);
291 memset(&xms_head, 0, sizeof(xms_head_t));
294 /* --------------------------- end of file ------------------------- */
298 Not sure how to use this?
300 call init_xms(x) to allocate a big chunk of xms.
301 x is in 'blocks' of 16Kb. Pick X big enough to buffer all the files
302 you want to place in xms.
304 call xms_open("filename); for each file to be buffered. This copies the file
307 then use xms_read(), xms_write(), and xms_seek() to read the file from
310 call deinit_xms() just before exit to clean up.
312 You can also use the lower level calls directly.