bsp
jmemsys.h
00001 /*
00002  * jmemsys.h
00003  *
00004  * Copyright (C) 1992-1994, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This include file defines the interface between the system-independent
00009  * and system-dependent portions of the JPEG memory manager.  No other
00010  * modules need include it.  (The system-independent portion is jmemmgr.c;
00011  * there are several different versions of the system-dependent portion.)
00012  *
00013  * This file works as-is for the system-dependent memory managers supplied
00014  * in the IJG distribution.  You may need to modify it if you write a
00015  * custom memory manager.  If system-dependent changes are needed in
00016  * this file, the best method is to #ifdef them based on a configuration
00017  * symbol supplied in jconfig.h, as we have done with USE_MSDOS_MEMMGR.
00018  */
00019 
00020 
00021 /* Short forms of external names for systems with brain-damaged linkers. */
00022 
00023 #ifdef NEED_SHORT_EXTERNAL_NAMES
00024 #define jpeg_get_small          jGetSmall
00025 #define jpeg_free_small         jFreeSmall
00026 #define jpeg_get_large          jGetLarge
00027 #define jpeg_free_large         jFreeLarge
00028 #define jpeg_mem_available      jMemAvail
00029 #define jpeg_open_backing_store jOpenBackStore
00030 #define jpeg_mem_init           jMemInit
00031 #define jpeg_mem_term           jMemTerm
00032 #endif /* NEED_SHORT_EXTERNAL_NAMES */
00033 
00034 
00035 /*
00036  * These two functions are used to allocate and release small chunks of
00037  * memory.  (Typically the total amount requested through jpeg_get_small is
00038  * no more than 20K or so; this will be requested in chunks of a few K each.)
00039  * Behavior should be the same as for the standard library functions malloc
00040  * and free; in particular, jpeg_get_small must return NULL on failure.
00041  * On most systems, these ARE malloc and free.  jpeg_free_small is passed the
00042  * size of the object being freed, just in case it's needed.
00043  * On an 80x86 machine using small-data memory model, these manage near heap.
00044  */
00045 
00046 EXTERN void * jpeg_get_small JPP((j_common_ptr cinfo, size_t sizeofobject));
00047 EXTERN void jpeg_free_small JPP((j_common_ptr cinfo, void * object,
00048                                  size_t sizeofobject));
00049 
00050 /*
00051  * These two functions are used to allocate and release large chunks of
00052  * memory (up to the total free space designated by jpeg_mem_available).
00053  * The interface is the same as above, except that on an 80x86 machine,
00054  * far pointers are used.  On most other machines these are identical to
00055  * the jpeg_get/free_small routines; but we keep them separate anyway,
00056  * in case a different allocation strategy is desirable for large chunks.
00057  */
00058 
00059 EXTERN void FAR * jpeg_get_large JPP((j_common_ptr cinfo,size_t sizeofobject));
00060 EXTERN void jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object,
00061                                  size_t sizeofobject));
00062 
00063 /*
00064  * The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may
00065  * be requested in a single call to jpeg_get_large (and jpeg_get_small for that
00066  * matter, but that case should never come into play).  This macro is needed
00067  * to model the 64Kb-segment-size limit of far addressing on 80x86 machines.
00068  * On those machines, we expect that jconfig.h will provide a proper value.
00069  * On machines with 32-bit flat address spaces, any large constant may be used.
00070  *
00071  * NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type
00072  * size_t and will be a multiple of sizeof(align_type).
00073  */
00074 
00075 #ifndef MAX_ALLOC_CHUNK         /* may be overridden in jconfig.h */
00076 #define MAX_ALLOC_CHUNK  1000000000L
00077 #endif
00078 
00079 /*
00080  * This routine computes the total space still available for allocation by
00081  * jpeg_get_large.  If more space than this is needed, backing store will be
00082  * used.  NOTE: any memory already allocated must not be counted.
00083  *
00084  * There is a minimum space requirement, corresponding to the minimum
00085  * feasible buffer sizes; jmemmgr.c will request that much space even if
00086  * jpeg_mem_available returns zero.  The maximum space needed, enough to hold
00087  * all working storage in memory, is also passed in case it is useful.
00088  * Finally, the total space already allocated is passed.  If no better
00089  * method is available, cinfo->mem->max_memory_to_use - already_allocated
00090  * is often a suitable calculation.
00091  *
00092  * It is OK for jpeg_mem_available to underestimate the space available
00093  * (that'll just lead to more backing-store access than is really necessary).
00094  * However, an overestimate will lead to failure.  Hence it's wise to subtract
00095  * a slop factor from the true available space.  5% should be enough.
00096  *
00097  * On machines with lots of virtual memory, any large constant may be returned.
00098  * Conversely, zero may be returned to always use the minimum amount of memory.
00099  */
00100 
00101 EXTERN long jpeg_mem_available JPP((j_common_ptr cinfo,
00102                                     long min_bytes_needed,
00103                                     long max_bytes_needed,
00104                                     long already_allocated));
00105 
00106 
00107 /*
00108  * This structure holds whatever state is needed to access a single
00109  * backing-store object.  The read/write/close method pointers are called
00110  * by jmemmgr.c to manipulate the backing-store object; all other fields
00111  * are private to the system-dependent backing store routines.
00112  */
00113 
00114 #define TEMP_NAME_LENGTH   64   /* max length of a temporary file's name */
00115 
00116 #ifdef USE_MSDOS_MEMMGR         /* DOS-specific junk */
00117 
00118 typedef unsigned short XMSH;    /* type of extended-memory handles */
00119 typedef unsigned short EMSH;    /* type of expanded-memory handles */
00120 
00121 typedef union
00122 {
00123     short file_handle;          /* DOS file handle if it's a temp file */
00124     XMSH xms_handle;            /* handle if it's a chunk of XMS */
00125     EMSH ems_handle;            /* handle if it's a chunk of EMS */
00126 } handle_union;
00127 
00128 #endif /* USE_MSDOS_MEMMGR */
00129 
00130 typedef struct backing_store_struct * backing_store_ptr;
00131 
00132 typedef struct backing_store_struct
00133 {
00134     /* Methods for reading/writing/closing this backing-store object */
00135     JMETHOD(void, read_backing_store, (j_common_ptr cinfo,
00136                                        backing_store_ptr info,
00137                                        void FAR * buffer_address,
00138                                        long file_offset, long byte_count));
00139     JMETHOD(void, write_backing_store, (j_common_ptr cinfo,
00140                                         backing_store_ptr info,
00141                                         void FAR * buffer_address,
00142                                         long file_offset, long byte_count));
00143     JMETHOD(void, close_backing_store, (j_common_ptr cinfo,
00144                                         backing_store_ptr info));
00145 
00146     /* Private fields for system-dependent backing-store management */
00147 #ifdef USE_MSDOS_MEMMGR
00148     /* For the MS-DOS manager (jmemdos.c), we need: */
00149     handle_union handle;                /* reference to backing-store storage object */
00150     char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
00151 #else
00152     /* For a typical implementation with temp files, we need: */
00153     FILE * temp_file;           /* stdio reference to temp file */
00154     char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */
00155 #endif
00156 } backing_store_info;
00157 
00158 /*
00159  * Initial opening of a backing-store object.  This must fill in the
00160  * read/write/close pointers in the object.  The read/write routines
00161  * may take an error exit if the specified maximum file size is exceeded.
00162  * (If jpeg_mem_available always returns a large value, this routine can
00163  * just take an error exit.)
00164  */
00165 
00166 EXTERN void jpeg_open_backing_store JPP((j_common_ptr cinfo,
00167                                         backing_store_ptr info,
00168                                         long total_bytes_needed));
00169 
00170 
00171 /*
00172  * These routines take care of any system-dependent initialization and
00173  * cleanup required.  jpeg_mem_init will be called before anything is
00174  * allocated (and, therefore, nothing in cinfo is of use except the error
00175  * manager pointer).  It should return a suitable default value for
00176  * max_memory_to_use; this may subsequently be overridden by the surrounding
00177  * application.  (Note that max_memory_to_use is only important if
00178  * jpeg_mem_available chooses to consult it ... no one else will.)
00179  * jpeg_mem_term may assume that all requested memory has been freed and that
00180  * all opened backing-store objects have been closed.
00181  */
00182 
00183 EXTERN long jpeg_mem_init JPP((j_common_ptr cinfo));
00184 EXTERN void jpeg_mem_term JPP((j_common_ptr cinfo));
 All Classes Functions