/*1* jmemsys.h2*3* Copyright (C) 1992-1997, Thomas G. Lane.4* This file is part of the Independent JPEG Group's software.5* For conditions of distribution and use, see the accompanying README file.6*7* This include file defines the interface between the system-independent8* and system-dependent portions of the JPEG memory manager. No other9* modules need include it. (The system-independent portion is jmemmgr.c;10* there are several different versions of the system-dependent portion.)11*12* This file works as-is for the system-dependent memory managers supplied13* in the IJG distribution. You may need to modify it if you write a14* custom memory manager. If system-dependent changes are needed in15* this file, the best method is to #ifdef them based on a configuration16* symbol supplied in jconfig.h, as we have done with USE_MSDOS_MEMMGR17* and USE_MAC_MEMMGR.18*/192021/* Short forms of external names for systems with brain-damaged linkers. */2223#ifdef NEED_SHORT_EXTERNAL_NAMES24#define jpeg_get_small jGetSmall25#define jpeg_free_small jFreeSmall26#define jpeg_get_large jGetLarge27#define jpeg_free_large jFreeLarge28#define jpeg_mem_available jMemAvail29#define jpeg_open_backing_store jOpenBackStore30#define jpeg_mem_init jMemInit31#define jpeg_mem_term jMemTerm32#endif /* NEED_SHORT_EXTERNAL_NAMES */333435/*36* These two functions are used to allocate and release small chunks of37* memory. (Typically the total amount requested through jpeg_get_small is38* no more than 20K or so; this will be requested in chunks of a few K each.)39* Behavior should be the same as for the standard library functions malloc40* and free; in particular, jpeg_get_small must return NULL on failure.41* On most systems, these ARE malloc and free. jpeg_free_small is passed the42* size of the object being freed, just in case it's needed.43* On an 80x86 machine using small-data memory model, these manage near heap.44*/4546EXTERN(void *) jpeg_get_small JPP((j_common_ptr cinfo, size_t sizeofobject));47EXTERN(void) jpeg_free_small JPP((j_common_ptr cinfo, void * object,48size_t sizeofobject));4950/*51* These two functions are used to allocate and release large chunks of52* memory (up to the total free space designated by jpeg_mem_available).53* The interface is the same as above, except that on an 80x86 machine,54* far pointers are used. On most other machines these are identical to55* the jpeg_get/free_small routines; but we keep them separate anyway,56* in case a different allocation strategy is desirable for large chunks.57*/5859EXTERN(void FAR *) jpeg_get_large JPP((j_common_ptr cinfo,60size_t sizeofobject));61EXTERN(void) jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object,62size_t sizeofobject));6364/*65* The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may66* be requested in a single call to jpeg_get_large (and jpeg_get_small for that67* matter, but that case should never come into play). This macro is needed68* to model the 64Kb-segment-size limit of far addressing on 80x86 machines.69* On those machines, we expect that jconfig.h will provide a proper value.70* On machines with 32-bit flat address spaces, any large constant may be used.71*72* NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type73* size_t and will be a multiple of sizeof(align_type).74*/7576#ifndef MAX_ALLOC_CHUNK /* may be overridden in jconfig.h */77#define MAX_ALLOC_CHUNK 1000000000L78#endif7980/*81* This routine computes the total space still available for allocation by82* jpeg_get_large. If more space than this is needed, backing store will be83* used. NOTE: any memory already allocated must not be counted.84*85* There is a minimum space requirement, corresponding to the minimum86* feasible buffer sizes; jmemmgr.c will request that much space even if87* jpeg_mem_available returns zero. The maximum space needed, enough to hold88* all working storage in memory, is also passed in case it is useful.89* Finally, the total space already allocated is passed. If no better90* method is available, cinfo->mem->max_memory_to_use - already_allocated91* is often a suitable calculation.92*93* It is OK for jpeg_mem_available to underestimate the space available94* (that'll just lead to more backing-store access than is really necessary).95* However, an overestimate will lead to failure. Hence it's wise to subtract96* a slop factor from the true available space. 5% should be enough.97*98* On machines with lots of virtual memory, any large constant may be returned.99* Conversely, zero may be returned to always use the minimum amount of memory.100*/101102EXTERN(long) jpeg_mem_available JPP((j_common_ptr cinfo,103long min_bytes_needed,104long max_bytes_needed,105long already_allocated));106107108/*109* This structure holds whatever state is needed to access a single110* backing-store object. The read/write/close method pointers are called111* by jmemmgr.c to manipulate the backing-store object; all other fields112* are private to the system-dependent backing store routines.113*/114115#define TEMP_NAME_LENGTH 64 /* max length of a temporary file's name */116117118#ifdef USE_MSDOS_MEMMGR /* DOS-specific junk */119120typedef unsigned short XMSH; /* type of extended-memory handles */121typedef unsigned short EMSH; /* type of expanded-memory handles */122123typedef union {124short file_handle; /* DOS file handle if it's a temp file */125XMSH xms_handle; /* handle if it's a chunk of XMS */126EMSH ems_handle; /* handle if it's a chunk of EMS */127} handle_union;128129#endif /* USE_MSDOS_MEMMGR */130131#ifdef USE_MAC_MEMMGR /* Mac-specific junk */132#include <Files.h>133#endif /* USE_MAC_MEMMGR */134135136typedef struct backing_store_struct * backing_store_ptr;137138typedef struct backing_store_struct {139/* Methods for reading/writing/closing this backing-store object */140JMETHOD(void, read_backing_store, (j_common_ptr cinfo,141backing_store_ptr info,142void FAR * buffer_address,143long file_offset, long byte_count));144JMETHOD(void, write_backing_store, (j_common_ptr cinfo,145backing_store_ptr info,146void FAR * buffer_address,147long file_offset, long byte_count));148JMETHOD(void, close_backing_store, (j_common_ptr cinfo,149backing_store_ptr info));150151/* Private fields for system-dependent backing-store management */152#ifdef USE_MSDOS_MEMMGR153/* For the MS-DOS manager (jmemdos.c), we need: */154handle_union handle; /* reference to backing-store storage object */155char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */156#else157#ifdef USE_MAC_MEMMGR158/* For the Mac manager (jmemmac.c), we need: */159short temp_file; /* file reference number to temp file */160FSSpec tempSpec; /* the FSSpec for the temp file */161char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */162#else163/* For a typical implementation with temp files, we need: */164FILE * temp_file; /* stdio reference to temp file */165char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */166#endif167#endif168} backing_store_info;169170171/*172* Initial opening of a backing-store object. This must fill in the173* read/write/close pointers in the object. The read/write routines174* may take an error exit if the specified maximum file size is exceeded.175* (If jpeg_mem_available always returns a large value, this routine can176* just take an error exit.)177*/178179EXTERN(void) jpeg_open_backing_store JPP((j_common_ptr cinfo,180backing_store_ptr info,181long total_bytes_needed));182183184/*185* These routines take care of any system-dependent initialization and186* cleanup required. jpeg_mem_init will be called before anything is187* allocated (and, therefore, nothing in cinfo is of use except the error188* manager pointer). It should return a suitable default value for189* max_memory_to_use; this may subsequently be overridden by the surrounding190* application. (Note that max_memory_to_use is only important if191* jpeg_mem_available chooses to consult it ... no one else will.)192* jpeg_mem_term may assume that all requested memory has been freed and that193* all opened backing-store objects have been closed.194*/195196EXTERN(long) jpeg_mem_init JPP((j_common_ptr cinfo));197EXTERN(void) jpeg_mem_term JPP((j_common_ptr cinfo));198199200