Blame gtkmm-osx/jpeg-6b/jmemdos.c

darco 56a656
/*
darco 56a656
 * jmemdos.c
darco 56a656
 *
darco 56a656
 * Copyright (C) 1992-1997, Thomas G. Lane.
darco 56a656
 * This file is part of the Independent JPEG Group's software.
darco 56a656
 * For conditions of distribution and use, see the accompanying README file.
darco 56a656
 *
darco 56a656
 * This file provides an MS-DOS-compatible implementation of the system-
darco 56a656
 * dependent portion of the JPEG memory manager.  Temporary data can be
darco 56a656
 * stored in extended or expanded memory as well as in regular DOS files.
darco 56a656
 *
darco 56a656
 * If you use this file, you must be sure that NEED_FAR_POINTERS is defined
darco 56a656
 * if you compile in a small-data memory model; it should NOT be defined if
darco 56a656
 * you use a large-data memory model.  This file is not recommended if you
darco 56a656
 * are using a flat-memory-space 386 environment such as DJGCC or Watcom C.
darco 56a656
 * Also, this code will NOT work if struct fields are aligned on greater than
darco 56a656
 * 2-byte boundaries.
darco 56a656
 *
darco 56a656
 * Based on code contributed by Ge' Weijers.
darco 56a656
 */
darco 56a656
darco 56a656
/*
darco 56a656
 * If you have both extended and expanded memory, you may want to change the
darco 56a656
 * order in which they are tried in jopen_backing_store.  On a 286 machine
darco 56a656
 * expanded memory is usually faster, since extended memory access involves
darco 56a656
 * an expensive protected-mode-and-back switch.  On 386 and better, extended
darco 56a656
 * memory is usually faster.  As distributed, the code tries extended memory
darco 56a656
 * first (what? not everyone has a 386? :-).
darco 56a656
 *
darco 56a656
 * You can disable use of extended/expanded memory entirely by altering these
darco 56a656
 * definitions or overriding them from the Makefile (eg, -DEMS_SUPPORTED=0).
darco 56a656
 */
darco 56a656
darco 56a656
#ifndef XMS_SUPPORTED
darco 56a656
#define XMS_SUPPORTED  1
darco 56a656
#endif
darco 56a656
#ifndef EMS_SUPPORTED
darco 56a656
#define EMS_SUPPORTED  1
darco 56a656
#endif
darco 56a656
darco 56a656
darco 56a656
#define JPEG_INTERNALS
darco 56a656
#include "jinclude.h"
darco 56a656
#include "jpeglib.h"
darco 56a656
#include "jmemsys.h"		/* import the system-dependent declarations */
darco 56a656
darco 56a656
#ifndef HAVE_STDLIB_H		/* <stdlib.h> should declare these */</stdlib.h>
darco 56a656
extern void * malloc JPP((size_t size));
darco 56a656
extern void free JPP((void *ptr));
darco 56a656
extern char * getenv JPP((const char * name));
darco 56a656
#endif
darco 56a656
darco 56a656
#ifdef NEED_FAR_POINTERS
darco 56a656
darco 56a656
#ifdef __TURBOC__
darco 56a656
/* These definitions work for Borland C (Turbo C) */
darco 56a656
#include <alloc.h>		/* need farmalloc(), farfree() */</alloc.h>
darco 56a656
#define far_malloc(x)	farmalloc(x)
darco 56a656
#define far_free(x)	farfree(x)
darco 56a656
#else
darco 56a656
/* These definitions work for Microsoft C and compatible compilers */
darco 56a656
#include <malloc.h>		/* need _fmalloc(), _ffree() */</malloc.h>
darco 56a656
#define far_malloc(x)	_fmalloc(x)
darco 56a656
#define far_free(x)	_ffree(x)
darco 56a656
#endif
darco 56a656
darco 56a656
#else /* not NEED_FAR_POINTERS */
darco 56a656
darco 56a656
#define far_malloc(x)	malloc(x)
darco 56a656
#define far_free(x)	free(x)
darco 56a656
darco 56a656
#endif /* NEED_FAR_POINTERS */
darco 56a656
darco 56a656
#ifdef DONT_USE_B_MODE		/* define mode parameters for fopen() */
darco 56a656
#define READ_BINARY	"r"
darco 56a656
#else
darco 56a656
#define READ_BINARY	"rb"
darco 56a656
#endif
darco 56a656
darco 56a656
#ifndef USE_MSDOS_MEMMGR	/* make sure user got configuration right */
darco 56a656
  You forgot to define USE_MSDOS_MEMMGR in jconfig.h. /* deliberate syntax error */
darco 56a656
#endif
darco 56a656
darco 56a656
#if MAX_ALLOC_CHUNK >= 65535L	/* make sure jconfig.h got this right */
darco 56a656
  MAX_ALLOC_CHUNK should be less than 64K. /* deliberate syntax error */
darco 56a656
#endif
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Declarations for assembly-language support routines (see jmemdosa.asm).
darco 56a656
 *
darco 56a656
 * The functions are declared "far" as are all their pointer arguments;
darco 56a656
 * this ensures the assembly source code will work regardless of the
darco 56a656
 * compiler memory model.  We assume "short" is 16 bits, "long" is 32.
darco 56a656
 */
darco 56a656
darco 56a656
typedef void far * XMSDRIVER;	/* actually a pointer to code */
darco 56a656
typedef struct {		/* registers for calling XMS driver */
darco 56a656
	unsigned short ax, dx, bx;
darco 56a656
	void far * ds_si;
darco 56a656
      } XMScontext;
darco 56a656
typedef struct {		/* registers for calling EMS driver */
darco 56a656
	unsigned short ax, dx, bx;
darco 56a656
	void far * ds_si;
darco 56a656
      } EMScontext;
darco 56a656
darco 56a656
extern short far jdos_open JPP((short far * handle, char far * filename));
darco 56a656
extern short far jdos_close JPP((short handle));
darco 56a656
extern short far jdos_seek JPP((short handle, long offset));
darco 56a656
extern short far jdos_read JPP((short handle, void far * buffer,
darco 56a656
				unsigned short count));
darco 56a656
extern short far jdos_write JPP((short handle, void far * buffer,
darco 56a656
				 unsigned short count));
darco 56a656
extern void far jxms_getdriver JPP((XMSDRIVER far *));
darco 56a656
extern void far jxms_calldriver JPP((XMSDRIVER, XMScontext far *));
darco 56a656
extern short far jems_available JPP((void));
darco 56a656
extern void far jems_calldriver JPP((EMScontext far *));
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Selection of a file name for a temporary file.
darco 56a656
 * This is highly system-dependent, and you may want to customize it.
darco 56a656
 */
darco 56a656
darco 56a656
static int next_file_num;	/* to distinguish among several temp files */
darco 56a656
darco 56a656
LOCAL(void)
darco 56a656
select_file_name (char * fname)
darco 56a656
{
darco 56a656
  const char * env;
darco 56a656
  char * ptr;
darco 56a656
  FILE * tfile;
darco 56a656
darco 56a656
  /* Keep generating file names till we find one that's not in use */
darco 56a656
  for (;;) {
darco 56a656
    /* Get temp directory name from environment TMP or TEMP variable;
darco 56a656
     * if none, use "."
darco 56a656
     */
darco 56a656
    if ((env = (const char *) getenv("TMP")) == NULL)
darco 56a656
      if ((env = (const char *) getenv("TEMP")) == NULL)
darco 56a656
	env = ".";
darco 56a656
    if (*env == '\0')		/* null string means "." */
darco 56a656
      env = ".";
darco 56a656
    ptr = fname;		/* copy name to fname */
darco 56a656
    while (*env != '\0')
darco 56a656
      *ptr++ = *env++;
darco 56a656
    if (ptr[-1] != '\\' && ptr[-1] != '/')
darco 56a656
      *ptr++ = '\\';		/* append backslash if not in env variable */
darco 56a656
    /* Append a suitable file name */
darco 56a656
    next_file_num++;		/* advance counter */
darco 56a656
    sprintf(ptr, "JPG%03d.TMP", next_file_num);
darco 56a656
    /* Probe to see if file name is already in use */
darco 56a656
    if ((tfile = fopen(fname, READ_BINARY)) == NULL)
darco 56a656
      break;
darco 56a656
    fclose(tfile);		/* oops, it's there; close tfile & try again */
darco 56a656
  }
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Near-memory allocation and freeing are controlled by the regular library
darco 56a656
 * routines malloc() and free().
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(void *)
darco 56a656
jpeg_get_small (j_common_ptr cinfo, size_t sizeofobject)
darco 56a656
{
darco 56a656
  return (void *) malloc(sizeofobject);
darco 56a656
}
darco 56a656
darco 56a656
GLOBAL(void)
darco 56a656
jpeg_free_small (j_common_ptr cinfo, void * object, size_t sizeofobject)
darco 56a656
{
darco 56a656
  free(object);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * "Large" objects are allocated in far memory, if possible
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(void FAR *)
darco 56a656
jpeg_get_large (j_common_ptr cinfo, size_t sizeofobject)
darco 56a656
{
darco 56a656
  return (void FAR *) far_malloc(sizeofobject);
darco 56a656
}
darco 56a656
darco 56a656
GLOBAL(void)
darco 56a656
jpeg_free_large (j_common_ptr cinfo, void FAR * object, size_t sizeofobject)
darco 56a656
{
darco 56a656
  far_free(object);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * This routine computes the total memory space available for allocation.
darco 56a656
 * It's impossible to do this in a portable way; our current solution is
darco 56a656
 * to make the user tell us (with a default value set at compile time).
darco 56a656
 * If you can actually get the available space, it's a good idea to subtract
darco 56a656
 * a slop factor of 5% or so.
darco 56a656
 */
darco 56a656
darco 56a656
#ifndef DEFAULT_MAX_MEM		/* so can override from makefile */
darco 56a656
#define DEFAULT_MAX_MEM		300000L /* for total usage about 450K */
darco 56a656
#endif
darco 56a656
darco 56a656
GLOBAL(long)
darco 56a656
jpeg_mem_available (j_common_ptr cinfo, long min_bytes_needed,
darco 56a656
		    long max_bytes_needed, long already_allocated)
darco 56a656
{
darco 56a656
  return cinfo->mem->max_memory_to_use - already_allocated;
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Backing store (temporary file) management.
darco 56a656
 * Backing store objects are only used when the value returned by
darco 56a656
 * jpeg_mem_available is less than the total space needed.  You can dispense
darco 56a656
 * with these routines if you have plenty of virtual memory; see jmemnobs.c.
darco 56a656
 */
darco 56a656
darco 56a656
/*
darco 56a656
 * For MS-DOS we support three types of backing storage:
darco 56a656
 *   1. Conventional DOS files.  We access these by direct DOS calls rather
darco 56a656
 *      than via the stdio package.  This provides a bit better performance,
darco 56a656
 *      but the real reason is that the buffers to be read or written are FAR.
darco 56a656
 *      The stdio library for small-data memory models can't cope with that.
darco 56a656
 *   2. Extended memory, accessed per the XMS V2.0 specification.
darco 56a656
 *   3. Expanded memory, accessed per the LIM/EMS 4.0 specification.
darco 56a656
 * You'll need copies of those specs to make sense of the related code.
darco 56a656
 * The specs are available by Internet FTP from the SIMTEL archives 
darco 56a656
 * (oak.oakland.edu and its various mirror sites).  See files
darco 56a656
 * pub/msdos/microsoft/xms20.arc and pub/msdos/info/limems41.zip.
darco 56a656
 */
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Access methods for a DOS file.
darco 56a656
 */
darco 56a656
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
read_file_store (j_common_ptr cinfo, backing_store_ptr info,
darco 56a656
		 void FAR * buffer_address,
darco 56a656
		 long file_offset, long byte_count)
darco 56a656
{
darco 56a656
  if (jdos_seek(info->handle.file_handle, file_offset))
darco 56a656
    ERREXIT(cinfo, JERR_TFILE_SEEK);
darco 56a656
  /* Since MAX_ALLOC_CHUNK is less than 64K, byte_count will be too. */
darco 56a656
  if (byte_count > 65535L)	/* safety check */
darco 56a656
    ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
darco 56a656
  if (jdos_read(info->handle.file_handle, buffer_address,
darco 56a656
		(unsigned short) byte_count))
darco 56a656
    ERREXIT(cinfo, JERR_TFILE_READ);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
write_file_store (j_common_ptr cinfo, backing_store_ptr info,
darco 56a656
		  void FAR * buffer_address,
darco 56a656
		  long file_offset, long byte_count)
darco 56a656
{
darco 56a656
  if (jdos_seek(info->handle.file_handle, file_offset))
darco 56a656
    ERREXIT(cinfo, JERR_TFILE_SEEK);
darco 56a656
  /* Since MAX_ALLOC_CHUNK is less than 64K, byte_count will be too. */
darco 56a656
  if (byte_count > 65535L)	/* safety check */
darco 56a656
    ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
darco 56a656
  if (jdos_write(info->handle.file_handle, buffer_address,
darco 56a656
		 (unsigned short) byte_count))
darco 56a656
    ERREXIT(cinfo, JERR_TFILE_WRITE);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
close_file_store (j_common_ptr cinfo, backing_store_ptr info)
darco 56a656
{
darco 56a656
  jdos_close(info->handle.file_handle);	/* close the file */
darco 56a656
  remove(info->temp_name);	/* delete the file */
darco 56a656
/* If your system doesn't have remove(), try unlink() instead.
darco 56a656
 * remove() is the ANSI-standard name for this function, but
darco 56a656
 * unlink() was more common in pre-ANSI systems.
darco 56a656
 */
darco 56a656
  TRACEMSS(cinfo, 1, JTRC_TFILE_CLOSE, info->temp_name);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
LOCAL(boolean)
darco 56a656
open_file_store (j_common_ptr cinfo, backing_store_ptr info,
darco 56a656
		 long total_bytes_needed)
darco 56a656
{
darco 56a656
  short handle;
darco 56a656
darco 56a656
  select_file_name(info->temp_name);
darco 56a656
  if (jdos_open((short far *) & handle, (char far *) info->temp_name)) {
darco 56a656
    /* might as well exit since jpeg_open_backing_store will fail anyway */
darco 56a656
    ERREXITS(cinfo, JERR_TFILE_CREATE, info->temp_name);
darco 56a656
    return FALSE;
darco 56a656
  }
darco 56a656
  info->handle.file_handle = handle;
darco 56a656
  info->read_backing_store = read_file_store;
darco 56a656
  info->write_backing_store = write_file_store;
darco 56a656
  info->close_backing_store = close_file_store;
darco 56a656
  TRACEMSS(cinfo, 1, JTRC_TFILE_OPEN, info->temp_name);
darco 56a656
  return TRUE;			/* succeeded */
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Access methods for extended memory.
darco 56a656
 */
darco 56a656
darco 56a656
#if XMS_SUPPORTED
darco 56a656
darco 56a656
static XMSDRIVER xms_driver;	/* saved address of XMS driver */
darco 56a656
darco 56a656
typedef union {			/* either long offset or real-mode pointer */
darco 56a656
	long offset;
darco 56a656
	void far * ptr;
darco 56a656
      } XMSPTR;
darco 56a656
darco 56a656
typedef struct {		/* XMS move specification structure */
darco 56a656
	long length;
darco 56a656
	XMSH src_handle;
darco 56a656
	XMSPTR src;
darco 56a656
	XMSH dst_handle;
darco 56a656
	XMSPTR dst;
darco 56a656
      } XMSspec;
darco 56a656
darco 56a656
#define ODD(X)	(((X) & 1L) != 0)
darco 56a656
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
read_xms_store (j_common_ptr cinfo, backing_store_ptr info,
darco 56a656
		void FAR * buffer_address,
darco 56a656
		long file_offset, long byte_count)
darco 56a656
{
darco 56a656
  XMScontext ctx;
darco 56a656
  XMSspec spec;
darco 56a656
  char endbuffer[2];
darco 56a656
darco 56a656
  /* The XMS driver can't cope with an odd length, so handle the last byte
darco 56a656
   * specially if byte_count is odd.  We don't expect this to be common.
darco 56a656
   */
darco 56a656
darco 56a656
  spec.length = byte_count & (~ 1L);
darco 56a656
  spec.src_handle = info->handle.xms_handle;
darco 56a656
  spec.src.offset = file_offset;
darco 56a656
  spec.dst_handle = 0;
darco 56a656
  spec.dst.ptr = buffer_address;
darco 56a656
  
darco 56a656
  ctx.ds_si = (void far *) & spec;
darco 56a656
  ctx.ax = 0x0b00;		/* EMB move */
darco 56a656
  jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
darco 56a656
  if (ctx.ax != 1)
darco 56a656
    ERREXIT(cinfo, JERR_XMS_READ);
darco 56a656
darco 56a656
  if (ODD(byte_count)) {
darco 56a656
    read_xms_store(cinfo, info, (void FAR *) endbuffer,
darco 56a656
		   file_offset + byte_count - 1L, 2L);
darco 56a656
    ((char FAR *) buffer_address)[byte_count - 1L] = endbuffer[0];
darco 56a656
  }
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
write_xms_store (j_common_ptr cinfo, backing_store_ptr info,
darco 56a656
		 void FAR * buffer_address,
darco 56a656
		 long file_offset, long byte_count)
darco 56a656
{
darco 56a656
  XMScontext ctx;
darco 56a656
  XMSspec spec;
darco 56a656
  char endbuffer[2];
darco 56a656
darco 56a656
  /* The XMS driver can't cope with an odd length, so handle the last byte
darco 56a656
   * specially if byte_count is odd.  We don't expect this to be common.
darco 56a656
   */
darco 56a656
darco 56a656
  spec.length = byte_count & (~ 1L);
darco 56a656
  spec.src_handle = 0;
darco 56a656
  spec.src.ptr = buffer_address;
darco 56a656
  spec.dst_handle = info->handle.xms_handle;
darco 56a656
  spec.dst.offset = file_offset;
darco 56a656
darco 56a656
  ctx.ds_si = (void far *) & spec;
darco 56a656
  ctx.ax = 0x0b00;		/* EMB move */
darco 56a656
  jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
darco 56a656
  if (ctx.ax != 1)
darco 56a656
    ERREXIT(cinfo, JERR_XMS_WRITE);
darco 56a656
darco 56a656
  if (ODD(byte_count)) {
darco 56a656
    read_xms_store(cinfo, info, (void FAR *) endbuffer,
darco 56a656
		   file_offset + byte_count - 1L, 2L);
darco 56a656
    endbuffer[0] = ((char FAR *) buffer_address)[byte_count - 1L];
darco 56a656
    write_xms_store(cinfo, info, (void FAR *) endbuffer,
darco 56a656
		    file_offset + byte_count - 1L, 2L);
darco 56a656
  }
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
close_xms_store (j_common_ptr cinfo, backing_store_ptr info)
darco 56a656
{
darco 56a656
  XMScontext ctx;
darco 56a656
darco 56a656
  ctx.dx = info->handle.xms_handle;
darco 56a656
  ctx.ax = 0x0a00;
darco 56a656
  jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
darco 56a656
  TRACEMS1(cinfo, 1, JTRC_XMS_CLOSE, info->handle.xms_handle);
darco 56a656
  /* we ignore any error return from the driver */
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
LOCAL(boolean)
darco 56a656
open_xms_store (j_common_ptr cinfo, backing_store_ptr info,
darco 56a656
		long total_bytes_needed)
darco 56a656
{
darco 56a656
  XMScontext ctx;
darco 56a656
darco 56a656
  /* Get address of XMS driver */
darco 56a656
  jxms_getdriver((XMSDRIVER far *) & xms_driver);
darco 56a656
  if (xms_driver == NULL)
darco 56a656
    return FALSE;		/* no driver to be had */
darco 56a656
darco 56a656
  /* Get version number, must be >= 2.00 */
darco 56a656
  ctx.ax = 0x0000;
darco 56a656
  jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
darco 56a656
  if (ctx.ax < (unsigned short) 0x0200)
darco 56a656
    return FALSE;
darco 56a656
darco 56a656
  /* Try to get space (expressed in kilobytes) */
darco 56a656
  ctx.dx = (unsigned short) ((total_bytes_needed + 1023L) >> 10);
darco 56a656
  ctx.ax = 0x0900;
darco 56a656
  jxms_calldriver(xms_driver, (XMScontext far *) & ctx);
darco 56a656
  if (ctx.ax != 1)
darco 56a656
    return FALSE;
darco 56a656
darco 56a656
  /* Succeeded, save the handle and away we go */
darco 56a656
  info->handle.xms_handle = ctx.dx;
darco 56a656
  info->read_backing_store = read_xms_store;
darco 56a656
  info->write_backing_store = write_xms_store;
darco 56a656
  info->close_backing_store = close_xms_store;
darco 56a656
  TRACEMS1(cinfo, 1, JTRC_XMS_OPEN, ctx.dx);
darco 56a656
  return TRUE;			/* succeeded */
darco 56a656
}
darco 56a656
darco 56a656
#endif /* XMS_SUPPORTED */
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Access methods for expanded memory.
darco 56a656
 */
darco 56a656
darco 56a656
#if EMS_SUPPORTED
darco 56a656
darco 56a656
/* The EMS move specification structure requires word and long fields aligned
darco 56a656
 * at odd byte boundaries.  Some compilers will align struct fields at even
darco 56a656
 * byte boundaries.  While it's usually possible to force byte alignment,
darco 56a656
 * that causes an overall performance penalty and may pose problems in merging
darco 56a656
 * JPEG into a larger application.  Instead we accept some rather dirty code
darco 56a656
 * here.  Note this code would fail if the hardware did not allow odd-byte
darco 56a656
 * word & long accesses, but all 80x86 CPUs do.
darco 56a656
 */
darco 56a656
darco 56a656
typedef void far * EMSPTR;
darco 56a656
darco 56a656
typedef union {			/* EMS move specification structure */
darco 56a656
	long length;		/* It's easy to access first 4 bytes */
darco 56a656
	char bytes[18];		/* Misaligned fields in here! */
darco 56a656
      } EMSspec;
darco 56a656
darco 56a656
/* Macros for accessing misaligned fields */
darco 56a656
#define FIELD_AT(spec,offset,type)  (*((type *) &(spec.bytes[offset])))
darco 56a656
#define SRC_TYPE(spec)		FIELD_AT(spec,4,char)
darco 56a656
#define SRC_HANDLE(spec)	FIELD_AT(spec,5,EMSH)
darco 56a656
#define SRC_OFFSET(spec)	FIELD_AT(spec,7,unsigned short)
darco 56a656
#define SRC_PAGE(spec)		FIELD_AT(spec,9,unsigned short)
darco 56a656
#define SRC_PTR(spec)		FIELD_AT(spec,7,EMSPTR)
darco 56a656
#define DST_TYPE(spec)		FIELD_AT(spec,11,char)
darco 56a656
#define DST_HANDLE(spec)	FIELD_AT(spec,12,EMSH)
darco 56a656
#define DST_OFFSET(spec)	FIELD_AT(spec,14,unsigned short)
darco 56a656
#define DST_PAGE(spec)		FIELD_AT(spec,16,unsigned short)
darco 56a656
#define DST_PTR(spec)		FIELD_AT(spec,14,EMSPTR)
darco 56a656
darco 56a656
#define EMSPAGESIZE	16384L	/* gospel, see the EMS specs */
darco 56a656
darco 56a656
#define HIBYTE(W)  (((W) >> 8) & 0xFF)
darco 56a656
#define LOBYTE(W)  ((W) & 0xFF)
darco 56a656
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
read_ems_store (j_common_ptr cinfo, backing_store_ptr info,
darco 56a656
		void FAR * buffer_address,
darco 56a656
		long file_offset, long byte_count)
darco 56a656
{
darco 56a656
  EMScontext ctx;
darco 56a656
  EMSspec spec;
darco 56a656
darco 56a656
  spec.length = byte_count;
darco 56a656
  SRC_TYPE(spec) = 1;
darco 56a656
  SRC_HANDLE(spec) = info->handle.ems_handle;
darco 56a656
  SRC_PAGE(spec)   = (unsigned short) (file_offset / EMSPAGESIZE);
darco 56a656
  SRC_OFFSET(spec) = (unsigned short) (file_offset % EMSPAGESIZE);
darco 56a656
  DST_TYPE(spec) = 0;
darco 56a656
  DST_HANDLE(spec) = 0;
darco 56a656
  DST_PTR(spec)    = buffer_address;
darco 56a656
  
darco 56a656
  ctx.ds_si = (void far *) & spec;
darco 56a656
  ctx.ax = 0x5700;		/* move memory region */
darco 56a656
  jems_calldriver((EMScontext far *) & ctx);
darco 56a656
  if (HIBYTE(ctx.ax) != 0)
darco 56a656
    ERREXIT(cinfo, JERR_EMS_READ);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
write_ems_store (j_common_ptr cinfo, backing_store_ptr info,
darco 56a656
		 void FAR * buffer_address,
darco 56a656
		 long file_offset, long byte_count)
darco 56a656
{
darco 56a656
  EMScontext ctx;
darco 56a656
  EMSspec spec;
darco 56a656
darco 56a656
  spec.length = byte_count;
darco 56a656
  SRC_TYPE(spec) = 0;
darco 56a656
  SRC_HANDLE(spec) = 0;
darco 56a656
  SRC_PTR(spec)    = buffer_address;
darco 56a656
  DST_TYPE(spec) = 1;
darco 56a656
  DST_HANDLE(spec) = info->handle.ems_handle;
darco 56a656
  DST_PAGE(spec)   = (unsigned short) (file_offset / EMSPAGESIZE);
darco 56a656
  DST_OFFSET(spec) = (unsigned short) (file_offset % EMSPAGESIZE);
darco 56a656
  
darco 56a656
  ctx.ds_si = (void far *) & spec;
darco 56a656
  ctx.ax = 0x5700;		/* move memory region */
darco 56a656
  jems_calldriver((EMScontext far *) & ctx);
darco 56a656
  if (HIBYTE(ctx.ax) != 0)
darco 56a656
    ERREXIT(cinfo, JERR_EMS_WRITE);
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
METHODDEF(void)
darco 56a656
close_ems_store (j_common_ptr cinfo, backing_store_ptr info)
darco 56a656
{
darco 56a656
  EMScontext ctx;
darco 56a656
darco 56a656
  ctx.ax = 0x4500;
darco 56a656
  ctx.dx = info->handle.ems_handle;
darco 56a656
  jems_calldriver((EMScontext far *) & ctx);
darco 56a656
  TRACEMS1(cinfo, 1, JTRC_EMS_CLOSE, info->handle.ems_handle);
darco 56a656
  /* we ignore any error return from the driver */
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
LOCAL(boolean)
darco 56a656
open_ems_store (j_common_ptr cinfo, backing_store_ptr info,
darco 56a656
		long total_bytes_needed)
darco 56a656
{
darco 56a656
  EMScontext ctx;
darco 56a656
darco 56a656
  /* Is EMS driver there? */
darco 56a656
  if (! jems_available())
darco 56a656
    return FALSE;
darco 56a656
darco 56a656
  /* Get status, make sure EMS is OK */
darco 56a656
  ctx.ax = 0x4000;
darco 56a656
  jems_calldriver((EMScontext far *) & ctx);
darco 56a656
  if (HIBYTE(ctx.ax) != 0)
darco 56a656
    return FALSE;
darco 56a656
darco 56a656
  /* Get version, must be >= 4.0 */
darco 56a656
  ctx.ax = 0x4600;
darco 56a656
  jems_calldriver((EMScontext far *) & ctx);
darco 56a656
  if (HIBYTE(ctx.ax) != 0 || LOBYTE(ctx.ax) < 0x40)
darco 56a656
    return FALSE;
darco 56a656
darco 56a656
  /* Try to allocate requested space */
darco 56a656
  ctx.ax = 0x4300;
darco 56a656
  ctx.bx = (unsigned short) ((total_bytes_needed + EMSPAGESIZE-1L) / EMSPAGESIZE);
darco 56a656
  jems_calldriver((EMScontext far *) & ctx);
darco 56a656
  if (HIBYTE(ctx.ax) != 0)
darco 56a656
    return FALSE;
darco 56a656
darco 56a656
  /* Succeeded, save the handle and away we go */
darco 56a656
  info->handle.ems_handle = ctx.dx;
darco 56a656
  info->read_backing_store = read_ems_store;
darco 56a656
  info->write_backing_store = write_ems_store;
darco 56a656
  info->close_backing_store = close_ems_store;
darco 56a656
  TRACEMS1(cinfo, 1, JTRC_EMS_OPEN, ctx.dx);
darco 56a656
  return TRUE;			/* succeeded */
darco 56a656
}
darco 56a656
darco 56a656
#endif /* EMS_SUPPORTED */
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * Initial opening of a backing-store object.
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(void)
darco 56a656
jpeg_open_backing_store (j_common_ptr cinfo, backing_store_ptr info,
darco 56a656
			 long total_bytes_needed)
darco 56a656
{
darco 56a656
  /* Try extended memory, then expanded memory, then regular file. */
darco 56a656
#if XMS_SUPPORTED
darco 56a656
  if (open_xms_store(cinfo, info, total_bytes_needed))
darco 56a656
    return;
darco 56a656
#endif
darco 56a656
#if EMS_SUPPORTED
darco 56a656
  if (open_ems_store(cinfo, info, total_bytes_needed))
darco 56a656
    return;
darco 56a656
#endif
darco 56a656
  if (open_file_store(cinfo, info, total_bytes_needed))
darco 56a656
    return;
darco 56a656
  ERREXITS(cinfo, JERR_TFILE_CREATE, "");
darco 56a656
}
darco 56a656
darco 56a656
darco 56a656
/*
darco 56a656
 * These routines take care of any system-dependent initialization and
darco 56a656
 * cleanup required.
darco 56a656
 */
darco 56a656
darco 56a656
GLOBAL(long)
darco 56a656
jpeg_mem_init (j_common_ptr cinfo)
darco 56a656
{
darco 56a656
  next_file_num = 0;		/* initialize temp file name generator */
darco 56a656
  return DEFAULT_MAX_MEM;	/* default for max_memory_to_use */
darco 56a656
}
darco 56a656
darco 56a656
GLOBAL(void)
darco 56a656
jpeg_mem_term (j_common_ptr cinfo)
darco 56a656
{
darco 56a656
  /* Microsoft C, at least in v6.00A, will not successfully reclaim freed
darco 56a656
   * blocks of size > 32Kbytes unless we give it a kick in the rear, like so:
darco 56a656
   */
darco 56a656
#ifdef NEED_FHEAPMIN
darco 56a656
  _fheapmin();
darco 56a656
#endif
darco 56a656
}