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