kusano 7d535a
/*
kusano 7d535a
 * jmemsys.h
kusano 7d535a
 *
shun-iwasawa 82a8f5
 * This file was part of the Independent JPEG Group's software:
kusano 7d535a
 * Copyright (C) 1992-1997, Thomas G. Lane.
shun-iwasawa 82a8f5
 * It was modified by The libjpeg-turbo Project to include only code and
shun-iwasawa 82a8f5
 * information relevant to libjpeg-turbo.
shun-iwasawa 82a8f5
 * For conditions of distribution and use, see the accompanying README.ijg
shun-iwasawa 82a8f5
 * 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
shun-iwasawa 82a8f5
 * symbol supplied in jconfig.h.
kusano 7d535a
 */
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
 */
kusano 7d535a
shun-iwasawa 82a8f5
EXTERN(void *) jpeg_get_small(j_common_ptr cinfo, size_t sizeofobject);
shun-iwasawa 82a8f5
EXTERN(void) jpeg_free_small(j_common_ptr cinfo, void *object,
shun-iwasawa 82a8f5
                             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).
shun-iwasawa 82a8f5
 * These are identical to the jpeg_get/free_small routines; but we keep them
shun-iwasawa 82a8f5
 * separate anyway, in case a different allocation strategy is desirable for
shun-iwasawa 82a8f5
 * large chunks.
kusano 7d535a
 */
kusano 7d535a
shun-iwasawa 82a8f5
EXTERN(void *) jpeg_get_large(j_common_ptr cinfo, size_t sizeofobject);
shun-iwasawa 82a8f5
EXTERN(void) jpeg_free_large(j_common_ptr cinfo, void *object,
shun-iwasawa 82a8f5
                             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
shun-iwasawa 82a8f5
 * matter, but that case should never come into play).  This macro was needed
kusano 7d535a
 * to model the 64Kb-segment-size limit of far addressing on 80x86 machines.
shun-iwasawa 82a8f5
 * On machines with 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
shun-iwasawa 82a8f5
#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
shun-iwasawa 82a8f5
EXTERN(size_t) jpeg_mem_available(j_common_ptr cinfo, size_t min_bytes_needed,
shun-iwasawa 82a8f5
                                  size_t max_bytes_needed,
shun-iwasawa 82a8f5
                                  size_t 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
shun-iwasawa 82a8f5
#define TEMP_NAME_LENGTH   64   /* max length of a temporary file's name */
kusano 7d535a
kusano 7d535a
shun-iwasawa 82a8f5
#ifdef USE_MSDOS_MEMMGR         /* DOS-specific junk */
kusano 7d535a
shun-iwasawa 82a8f5
typedef unsigned short XMSH;    /* type of extended-memory handles */
shun-iwasawa 82a8f5
typedef unsigned short EMSH;    /* type of expanded-memory handles */
kusano 7d535a
kusano 7d535a
typedef union {
shun-iwasawa 82a8f5
  short file_handle;            /* DOS file handle if it's a temp file */
shun-iwasawa 82a8f5
  XMSH xms_handle;              /* handle if it's a chunk of XMS */
shun-iwasawa 82a8f5
  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
shun-iwasawa 82a8f5
#ifdef USE_MAC_MEMMGR           /* Mac-specific junk */
kusano 7d535a
#include <files.h></files.h>
kusano 7d535a
#endif /* USE_MAC_MEMMGR */
kusano 7d535a
kusano 7d535a
shun-iwasawa 82a8f5
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 */
shun-iwasawa 82a8f5
  void (*read_backing_store) (j_common_ptr cinfo, backing_store_ptr info,
shun-iwasawa 82a8f5
                              void *buffer_address, long file_offset,
shun-iwasawa 82a8f5
                              long byte_count);
shun-iwasawa 82a8f5
  void (*write_backing_store) (j_common_ptr cinfo, backing_store_ptr info,
shun-iwasawa 82a8f5
                               void *buffer_address, long file_offset,
shun-iwasawa 82a8f5
                               long byte_count);
shun-iwasawa 82a8f5
  void (*close_backing_store) (j_common_ptr cinfo, 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: */
shun-iwasawa 82a8f5
  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: */
shun-iwasawa 82a8f5
  short temp_file;              /* file reference number to temp file */
shun-iwasawa 82a8f5
  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: */
shun-iwasawa 82a8f5
  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
shun-iwasawa 82a8f5
EXTERN(void) jpeg_open_backing_store(j_common_ptr cinfo,
shun-iwasawa 82a8f5
                                     backing_store_ptr info,
shun-iwasawa 82a8f5
                                     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
shun-iwasawa 82a8f5
EXTERN(long) jpeg_mem_init(j_common_ptr cinfo);
shun-iwasawa 82a8f5
EXTERN(void) jpeg_mem_term(j_common_ptr cinfo);