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