1/*
2 * reserved comment block
3 * DO NOT REMOVE OR ALTER!
4 */
5/*
6 * jmemsys.h
7 *
8 * Copyright (C) 1992-1997, Thomas G. Lane.
9 * This file is part of the Independent JPEG Group's software.
10 * For conditions of distribution and use, see the accompanying README file.
11 *
12 * This include file defines the interface between the system-independent
13 * and system-dependent portions of the JPEG memory manager.  No other
14 * modules need include it.  (The system-independent portion is jmemmgr.c;
15 * there are several different versions of the system-dependent portion.)
16 *
17 * This file works as-is for the system-dependent memory managers supplied
18 * in the IJG distribution.  You may need to modify it if you write a
19 * custom memory manager.  If system-dependent changes are needed in
20 * this file, the best method is to #ifdef them based on a configuration
21 * symbol supplied in jconfig.h, as we have done with USE_MSDOS_MEMMGR
22 * and USE_MAC_MEMMGR.
23 */
24
25
26/* Short forms of external names for systems with brain-damaged linkers. */
27
28#ifdef NEED_SHORT_EXTERNAL_NAMES
29#define jpeg_get_small          jGetSmall
30#define jpeg_free_small         jFreeSmall
31#define jpeg_get_large          jGetLarge
32#define jpeg_free_large         jFreeLarge
33#define jpeg_mem_available      jMemAvail
34#define jpeg_open_backing_store jOpenBackStore
35#define jpeg_mem_init           jMemInit
36#define jpeg_mem_term           jMemTerm
37#endif /* NEED_SHORT_EXTERNAL_NAMES */
38
39
40/*
41 * These two functions are used to allocate and release small chunks of
42 * memory.  (Typically the total amount requested through jpeg_get_small is
43 * no more than 20K or so; this will be requested in chunks of a few K each.)
44 * Behavior should be the same as for the standard library functions malloc
45 * and free; in particular, jpeg_get_small must return NULL on failure.
46 * On most systems, these ARE malloc and free.  jpeg_free_small is passed the
47 * size of the object being freed, just in case it's needed.
48 * On an 80x86 machine using small-data memory model, these manage near heap.
49 */
50
51EXTERN(void *) jpeg_get_small JPP((j_common_ptr cinfo, size_t sizeofobject));
52EXTERN(void) jpeg_free_small JPP((j_common_ptr cinfo, void * object,
53                                  size_t sizeofobject));
54
55/*
56 * These two functions are used to allocate and release large chunks of
57 * memory (up to the total free space designated by jpeg_mem_available).
58 * The interface is the same as above, except that on an 80x86 machine,
59 * far pointers are used.  On most other machines these are identical to
60 * the jpeg_get/free_small routines; but we keep them separate anyway,
61 * in case a different allocation strategy is desirable for large chunks.
62 */
63
64EXTERN(void FAR *) jpeg_get_large JPP((j_common_ptr cinfo,
65                                       size_t sizeofobject));
66EXTERN(void) jpeg_free_large JPP((j_common_ptr cinfo, void FAR * object,
67                                  size_t sizeofobject));
68
69/*
70 * The macro MAX_ALLOC_CHUNK designates the maximum number of bytes that may
71 * be requested in a single call to jpeg_get_large (and jpeg_get_small for that
72 * matter, but that case should never come into play).  This macro is needed
73 * to model the 64Kb-segment-size limit of far addressing on 80x86 machines.
74 * On those machines, we expect that jconfig.h will provide a proper value.
75 * On machines with 32-bit flat address spaces, any large constant may be used.
76 *
77 * NB: jmemmgr.c expects that MAX_ALLOC_CHUNK will be representable as type
78 * size_t and will be a multiple of sizeof(align_type).
79 */
80
81#ifndef MAX_ALLOC_CHUNK         /* may be overridden in jconfig.h */
82#define MAX_ALLOC_CHUNK  1000000000L
83#endif
84
85/*
86 * This routine computes the total space still available for allocation by
87 * jpeg_get_large.  If more space than this is needed, backing store will be
88 * used.  NOTE: any memory already allocated must not be counted.
89 *
90 * There is a minimum space requirement, corresponding to the minimum
91 * feasible buffer sizes; jmemmgr.c will request that much space even if
92 * jpeg_mem_available returns zero.  The maximum space needed, enough to hold
93 * all working storage in memory, is also passed in case it is useful.
94 * Finally, the total space already allocated is passed.  If no better
95 * method is available, cinfo->mem->max_memory_to_use - already_allocated
96 * is often a suitable calculation.
97 *
98 * It is OK for jpeg_mem_available to underestimate the space available
99 * (that'll just lead to more backing-store access than is really necessary).
100 * However, an overestimate will lead to failure.  Hence it's wise to subtract
101 * a slop factor from the true available space.  5% should be enough.
102 *
103 * On machines with lots of virtual memory, any large constant may be returned.
104 * Conversely, zero may be returned to always use the minimum amount of memory.
105 */
106
107EXTERN(size_t) jpeg_mem_available JPP((j_common_ptr cinfo,
108                                     size_t min_bytes_needed,
109                                     size_t max_bytes_needed,
110                                     size_t already_allocated));
111
112
113/*
114 * This structure holds whatever state is needed to access a single
115 * backing-store object.  The read/write/close method pointers are called
116 * by jmemmgr.c to manipulate the backing-store object; all other fields
117 * are private to the system-dependent backing store routines.
118 */
119
120#define TEMP_NAME_LENGTH   64   /* max length of a temporary file's name */
121
122
123#ifdef USE_MSDOS_MEMMGR         /* DOS-specific junk */
124
125typedef unsigned short XMSH;    /* type of extended-memory handles */
126typedef unsigned short EMSH;    /* type of expanded-memory handles */
127
128typedef union {
129  short file_handle;            /* DOS file handle if it's a temp file */
130  XMSH xms_handle;              /* handle if it's a chunk of XMS */
131  EMSH ems_handle;              /* handle if it's a chunk of EMS */
132} handle_union;
133
134#endif /* USE_MSDOS_MEMMGR */
135
136#ifdef USE_MAC_MEMMGR           /* Mac-specific junk */
137#include <Files.h>
138#endif /* USE_MAC_MEMMGR */
139
140
141typedef struct backing_store_struct * backing_store_ptr;
142
143typedef struct backing_store_struct {
144  /* Methods for reading/writing/closing this backing-store object */
145  JMETHOD(void, read_backing_store, (j_common_ptr cinfo,
146                                     backing_store_ptr info,
147                                     void FAR * buffer_address,
148                                     long file_offset, long byte_count));
149  JMETHOD(void, write_backing_store, (j_common_ptr cinfo,
150                                      backing_store_ptr info,
151                                      void FAR * buffer_address,
152                                      long file_offset, long byte_count));
153  JMETHOD(void, close_backing_store, (j_common_ptr cinfo,
154                                      backing_store_ptr info));
155
156  /* Private fields for system-dependent backing-store management */
157#ifdef USE_MSDOS_MEMMGR
158  /* For the MS-DOS manager (jmemdos.c), we need: */
159  handle_union handle;          /* reference to backing-store storage object */
160  char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
161#else
162#ifdef USE_MAC_MEMMGR
163  /* For the Mac manager (jmemmac.c), we need: */
164  short temp_file;              /* file reference number to temp file */
165  FSSpec tempSpec;              /* the FSSpec for the temp file */
166  char temp_name[TEMP_NAME_LENGTH]; /* name if it's a file */
167#else
168  /* For a typical implementation with temp files, we need: */
169  FILE * temp_file;             /* stdio reference to temp file */
170  char temp_name[TEMP_NAME_LENGTH]; /* name of temp file */
171#endif
172#endif
173} backing_store_info;
174
175
176/*
177 * Initial opening of a backing-store object.  This must fill in the
178 * read/write/close pointers in the object.  The read/write routines
179 * may take an error exit if the specified maximum file size is exceeded.
180 * (If jpeg_mem_available always returns a large value, this routine can
181 * just take an error exit.)
182 */
183
184EXTERN(void) jpeg_open_backing_store JPP((j_common_ptr cinfo,
185                                          backing_store_ptr info,
186                                          long total_bytes_needed));
187
188
189/*
190 * These routines take care of any system-dependent initialization and
191 * cleanup required.  jpeg_mem_init will be called before anything is
192 * allocated (and, therefore, nothing in cinfo is of use except the error
193 * manager pointer).  It should return a suitable default value for
194 * max_memory_to_use; this may subsequently be overridden by the surrounding
195 * application.  (Note that max_memory_to_use is only important if
196 * jpeg_mem_available chooses to consult it ... no one else will.)
197 * jpeg_mem_term may assume that all requested memory has been freed and that
198 * all opened backing-store objects have been closed.
199 */
200
201EXTERN(size_t) jpeg_mem_init JPP((j_common_ptr cinfo));
202EXTERN(void) jpeg_mem_term JPP((j_common_ptr cinfo));
203