kusano 7d535a
/*
kusano 7d535a
 * jmemsys.h
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 include file defines the interface between the system-independent
kusano 7d535a
 * and system-dependent portions of the JPEG memory manager.  No other
kusano 7d535a
 * modules need include it.  (The system-independent portion is jmemmgr.c;
kusano 7d535a
 * there are several different versions of the system-dependent portion.)
kusano 7d535a
 *
kusano 7d535a
 * This file works as-is for the system-dependent memory managers supplied
kusano 7d535a
 * in the IJG distribution.  You may need to modify it if you write a
kusano 7d535a
 * custom memory manager.  If system-dependent changes are needed in
kusano 7d535a
 * this file, the best method is to #ifdef them based on a configuration
kusano 7d535a
 * symbol supplied in jconfig.h, as we have done with USE_MSDOS_MEMMGR
kusano 7d535a
 * and USE_MAC_MEMMGR.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/* Short forms of external names for systems with brain-damaged linkers. */
kusano 7d535a
kusano 7d535a
#ifdef NEED_SHORT_EXTERNAL_NAMES
kusano 7d535a
#define jpeg_get_small		jGetSmall
kusano 7d535a
#define jpeg_free_small		jFreeSmall
kusano 7d535a
#define jpeg_get_large		jGetLarge
kusano 7d535a
#define jpeg_free_large		jFreeLarge
kusano 7d535a
#define jpeg_mem_available	jMemAvail
kusano 7d535a
#define jpeg_open_backing_store	jOpenBackStore
kusano 7d535a
#define jpeg_mem_init		jMemInit
kusano 7d535a
#define jpeg_mem_term		jMemTerm
kusano 7d535a
#endif /* NEED_SHORT_EXTERNAL_NAMES */
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * These two functions are used to allocate and release small chunks of
kusano 7d535a
 * memory.  (Typically the total amount requested through jpeg_get_small is
kusano 7d535a
 * no more than 20K or so; this will be requested in chunks of a few K each.)
kusano 7d535a
 * Behavior should be the same as for the standard library functions malloc
kusano 7d535a
 * and free; in particular, jpeg_get_small must return NULL on failure.
kusano 7d535a
 * On most systems, these ARE malloc and free.  jpeg_free_small is passed the
kusano 7d535a
 * size of the object being freed, just in case it's needed.
kusano 7d535a
 * On an 80x86 machine using small-data memory model, these manage near heap.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
EXTERN(void *) jpeg_get_small JPP((j_common_ptr cinfo, size_t sizeofobject));
kusano 7d535a
EXTERN(void) jpeg_free_small JPP((j_common_ptr cinfo, void * object,
kusano 7d535a
				  size_t sizeofobject));
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * These two functions are used to allocate and release large chunks of
kusano 7d535a
 * memory (up to the total free space designated by jpeg_mem_available).
kusano 7d535a
 * The interface is the same as above, except that on an 80x86 machine,
kusano 7d535a
 * far pointers are used.  On most other machines these are identical to
kusano 7d535a
 * the jpeg_get/free_small routines; but we keep them separate anyway,
kusano 7d535a
 * in case a different allocation strategy is desirable for large chunks.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
EXTERN(void FAR *) jpeg_get_large JPP((j_common_ptr cinfo,
kusano 7d535a
				       size_t sizeofobject));
kusano 7d535a
EXTERN(void) jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object,
kusano 7d535a
				  size_t sizeofobject));
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may
kusano 7d535a
 * be requested in a single call to jpeg_get_large (and jpeg_get_small for that
kusano 7d535a
 * matter, but that case should never come into play).  This macro is needed
kusano 7d535a
 * to model the 64Kb-segment-size limit of far addressing on 80x86 machines.
kusano 7d535a
 * On those machines, we expect that jconfig.h will provide a proper value.
kusano 7d535a
 * On machines with 32-bit flat address spaces, any large constant may be used.
kusano 7d535a
 *
kusano 7d535a
 * NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type
kusano 7d535a
 * size_t and will be a multiple of sizeof(align_type).
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#ifndef MAX_ALLOC_CHUNK		/* may be overridden in jconfig.h */
kusano 7d535a
#define MAX_ALLOC_CHUNK  1000000000L
kusano 7d535a
#endif
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * This routine computes the total space still available for allocation by
kusano 7d535a
 * jpeg_get_large.  If more space than this is needed, backing store will be
kusano 7d535a
 * used.  NOTE: any memory already allocated must not be counted.
kusano 7d535a
 *
kusano 7d535a
 * There is a minimum space requirement, corresponding to the minimum
kusano 7d535a
 * feasible buffer sizes; jmemmgr.c will request that much space even if
kusano 7d535a
 * jpeg_mem_available returns zero.  The maximum space needed, enough to hold
kusano 7d535a
 * all working storage in memory, is also passed in case it is useful.
kusano 7d535a
 * Finally, the total space already allocated is passed.  If no better
kusano 7d535a
 * method is available, cinfo->mem->max_memory_to_use - already_allocated
kusano 7d535a
 * is often a suitable calculation.
kusano 7d535a
 *
kusano 7d535a
 * It is OK for jpeg_mem_available to underestimate the space available
kusano 7d535a
 * (that'll just lead to more backing-store access than is really necessary).
kusano 7d535a
 * However, an overestimate will lead to failure.  Hence it's wise to subtract
kusano 7d535a
 * a slop factor from the true available space.  5% should be enough.
kusano 7d535a
 *
kusano 7d535a
 * On machines with lots of virtual memory, any large constant may be returned.
kusano 7d535a
 * Conversely, zero may be returned to always use the minimum amount of memory.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
EXTERN(long) jpeg_mem_available JPP((j_common_ptr cinfo,
kusano 7d535a
				     long min_bytes_needed,
kusano 7d535a
				     long max_bytes_needed,
kusano 7d535a
				     long already_allocated));
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * This structure holds whatever state is needed to access a single
kusano 7d535a
 * backing-store object.  The read/write/close method pointers are called
kusano 7d535a
 * by jmemmgr.c to manipulate the backing-store object; all other fields
kusano 7d535a
 * are private to the system-dependent backing store routines.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
#define TEMP_NAME_LENGTH   64	/* max length of a temporary file's name */
kusano 7d535a
kusano 7d535a
kusano 7d535a
#ifdef USE_MSDOS_MEMMGR		/* DOS-specific junk */
kusano 7d535a
kusano 7d535a
typedef unsigned short XMSH;	/* type of extended-memory handles */
kusano 7d535a
typedef unsigned short EMSH;	/* type of expanded-memory handles */
kusano 7d535a
kusano 7d535a
typedef union {
kusano 7d535a
  short file_handle;		/* DOS file handle if it's a temp file */
kusano 7d535a
  XMSH xms_handle;		/* handle if it's a chunk of XMS */
kusano 7d535a
  EMSH ems_handle;		/* handle if it's a chunk of EMS */
kusano 7d535a
} handle_union;
kusano 7d535a
kusano 7d535a
#endif /* USE_MSDOS_MEMMGR */
kusano 7d535a
kusano 7d535a
#ifdef USE_MAC_MEMMGR		/* Mac-specific junk */
kusano 7d535a
#include <files.h></files.h>
kusano 7d535a
#endif /* USE_MAC_MEMMGR */
kusano 7d535a
kusano 7d535a
kusano 7d535a
typedef struct backing_store_struct * backing_store_ptr;
kusano 7d535a
kusano 7d535a
typedef struct backing_store_struct {
kusano 7d535a
  /* Methods for reading/writing/closing this backing-store object */
kusano 7d535a
  JMETHOD(void, read_backing_store, (j_common_ptr cinfo,
kusano 7d535a
				     backing_store_ptr info,
kusano 7d535a
				     void FAR * buffer_address,
kusano 7d535a
				     long file_offset, long byte_count));
kusano 7d535a
  JMETHOD(void, write_backing_store, (j_common_ptr cinfo,
kusano 7d535a
				      backing_store_ptr info,
kusano 7d535a
				      void FAR * buffer_address,
kusano 7d535a
				      long file_offset, long byte_count));
kusano 7d535a
  JMETHOD(void, close_backing_store, (j_common_ptr cinfo,
kusano 7d535a
				      backing_store_ptr info));
kusano 7d535a
kusano 7d535a
  /* Private fields for system-dependent backing-store management */
kusano 7d535a
#ifdef USE_MSDOS_MEMMGR
kusano 7d535a
  /* For the MS-DOS manager (jmemdos.c), we need: */
kusano 7d535a
  handle_union handle;		/* reference to backing-store storage object */
kusano 7d535a
  char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
kusano 7d535a
#else
kusano 7d535a
#ifdef USE_MAC_MEMMGR
kusano 7d535a
  /* For the Mac manager (jmemmac.c), we need: */
kusano 7d535a
  short temp_file;		/* file reference number to temp file */
kusano 7d535a
  FSSpec tempSpec;		/* the FSSpec for the temp file */
kusano 7d535a
  char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
kusano 7d535a
#else
kusano 7d535a
  /* For a typical implementation with temp files, we need: */
kusano 7d535a
  FILE * temp_file;		/* stdio reference to temp file */
kusano 7d535a
  char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */
kusano 7d535a
#endif
kusano 7d535a
#endif
kusano 7d535a
} backing_store_info;
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * Initial opening of a backing-store object.  This must fill in the
kusano 7d535a
 * read/write/close pointers in the object.  The read/write routines
kusano 7d535a
 * may take an error exit if the specified maximum file size is exceeded.
kusano 7d535a
 * (If jpeg_mem_available always returns a large value, this routine can
kusano 7d535a
 * just take an error exit.)
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
EXTERN(void) jpeg_open_backing_store JPP((j_common_ptr cinfo,
kusano 7d535a
					  backing_store_ptr info,
kusano 7d535a
					  long total_bytes_needed));
kusano 7d535a
kusano 7d535a
kusano 7d535a
/*
kusano 7d535a
 * These routines take care of any system-dependent initialization and
kusano 7d535a
 * cleanup required.  jpeg_mem_init will be called before anything is
kusano 7d535a
 * allocated (and, therefore, nothing in cinfo is of use except the error
kusano 7d535a
 * manager pointer).  It should return a suitable default value for
kusano 7d535a
 * max_memory_to_use; this may subsequently be overridden by the surrounding
kusano 7d535a
 * application.  (Note that max_memory_to_use is only important if
kusano 7d535a
 * jpeg_mem_available chooses to consult it ... no one else will.)
kusano 7d535a
 * jpeg_mem_term may assume that all requested memory has been freed and that
kusano 7d535a
 * all opened backing-store objects have been closed.
kusano 7d535a
 */
kusano 7d535a
kusano 7d535a
EXTERN(long) jpeg_mem_init JPP((j_common_ptr cinfo));
kusano 7d535a
EXTERN(void) jpeg_mem_term JPP((j_common_ptr cinfo));