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