xref: /aosp_15_r20/external/libjpeg-turbo/jmemmgr.c (revision dfc6aa5c1cfd4bc4e2018dc74aa96e29ee49c6da)
1*dfc6aa5cSAndroid Build Coastguard Worker /*
2*dfc6aa5cSAndroid Build Coastguard Worker  * jmemmgr.c
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) 1991-1997, Thomas G. Lane.
6*dfc6aa5cSAndroid Build Coastguard Worker  * libjpeg-turbo Modifications:
7*dfc6aa5cSAndroid Build Coastguard Worker  * Copyright (C) 2016, 2021-2022, D. R. Commander.
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 file contains the JPEG system-independent memory management
12*dfc6aa5cSAndroid Build Coastguard Worker  * routines.  This code is usable across a wide variety of machines; most
13*dfc6aa5cSAndroid Build Coastguard Worker  * of the system dependencies have been isolated in a separate file.
14*dfc6aa5cSAndroid Build Coastguard Worker  * The major functions provided here are:
15*dfc6aa5cSAndroid Build Coastguard Worker  *   * pool-based allocation and freeing of memory;
16*dfc6aa5cSAndroid Build Coastguard Worker  *   * policy decisions about how to divide available memory among the
17*dfc6aa5cSAndroid Build Coastguard Worker  *     virtual arrays;
18*dfc6aa5cSAndroid Build Coastguard Worker  *   * control logic for swapping virtual arrays between main memory and
19*dfc6aa5cSAndroid Build Coastguard Worker  *     backing storage.
20*dfc6aa5cSAndroid Build Coastguard Worker  * The separate system-dependent file provides the actual backing-storage
21*dfc6aa5cSAndroid Build Coastguard Worker  * access code, and it contains the policy decision about how much total
22*dfc6aa5cSAndroid Build Coastguard Worker  * main memory to use.
23*dfc6aa5cSAndroid Build Coastguard Worker  * This file is system-dependent in the sense that some of its functions
24*dfc6aa5cSAndroid Build Coastguard Worker  * are unnecessary in some systems.  For example, if there is enough virtual
25*dfc6aa5cSAndroid Build Coastguard Worker  * memory so that backing storage will never be used, much of the virtual
26*dfc6aa5cSAndroid Build Coastguard Worker  * array control logic could be removed.  (Of course, if you have that much
27*dfc6aa5cSAndroid Build Coastguard Worker  * memory then you shouldn't care about a little bit of unused code...)
28*dfc6aa5cSAndroid Build Coastguard Worker  */
29*dfc6aa5cSAndroid Build Coastguard Worker 
30*dfc6aa5cSAndroid Build Coastguard Worker #define JPEG_INTERNALS
31*dfc6aa5cSAndroid Build Coastguard Worker #define AM_MEMORY_MANAGER       /* we define jvirt_Xarray_control structs */
32*dfc6aa5cSAndroid Build Coastguard Worker #include "jinclude.h"
33*dfc6aa5cSAndroid Build Coastguard Worker #include "jpeglib.h"
34*dfc6aa5cSAndroid Build Coastguard Worker #include "jmemsys.h"            /* import the system-dependent declarations */
35*dfc6aa5cSAndroid Build Coastguard Worker #if !defined(_MSC_VER) || _MSC_VER > 1600
36*dfc6aa5cSAndroid Build Coastguard Worker #include <stdint.h>
37*dfc6aa5cSAndroid Build Coastguard Worker #endif
38*dfc6aa5cSAndroid Build Coastguard Worker #include <limits.h>
39*dfc6aa5cSAndroid Build Coastguard Worker 
40*dfc6aa5cSAndroid Build Coastguard Worker 
41*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(size_t)
round_up_pow2(size_t a,size_t b)42*dfc6aa5cSAndroid Build Coastguard Worker round_up_pow2(size_t a, size_t b)
43*dfc6aa5cSAndroid Build Coastguard Worker /* a rounded up to the next multiple of b, i.e. ceil(a/b)*b */
44*dfc6aa5cSAndroid Build Coastguard Worker /* Assumes a >= 0, b > 0, and b is a power of 2 */
45*dfc6aa5cSAndroid Build Coastguard Worker {
46*dfc6aa5cSAndroid Build Coastguard Worker   return ((a + b - 1) & (~(b - 1)));
47*dfc6aa5cSAndroid Build Coastguard Worker }
48*dfc6aa5cSAndroid Build Coastguard Worker 
49*dfc6aa5cSAndroid Build Coastguard Worker 
50*dfc6aa5cSAndroid Build Coastguard Worker /*
51*dfc6aa5cSAndroid Build Coastguard Worker  * Some important notes:
52*dfc6aa5cSAndroid Build Coastguard Worker  *   The allocation routines provided here must never return NULL.
53*dfc6aa5cSAndroid Build Coastguard Worker  *   They should exit to error_exit if unsuccessful.
54*dfc6aa5cSAndroid Build Coastguard Worker  *
55*dfc6aa5cSAndroid Build Coastguard Worker  *   It's not a good idea to try to merge the sarray and barray routines,
56*dfc6aa5cSAndroid Build Coastguard Worker  *   even though they are textually almost the same, because samples are
57*dfc6aa5cSAndroid Build Coastguard Worker  *   usually stored as bytes while coefficients are shorts or ints.  Thus,
58*dfc6aa5cSAndroid Build Coastguard Worker  *   in machines where byte pointers have a different representation from
59*dfc6aa5cSAndroid Build Coastguard Worker  *   word pointers, the resulting machine code could not be the same.
60*dfc6aa5cSAndroid Build Coastguard Worker  */
61*dfc6aa5cSAndroid Build Coastguard Worker 
62*dfc6aa5cSAndroid Build Coastguard Worker 
63*dfc6aa5cSAndroid Build Coastguard Worker /*
64*dfc6aa5cSAndroid Build Coastguard Worker  * Many machines require storage alignment: longs must start on 4-byte
65*dfc6aa5cSAndroid Build Coastguard Worker  * boundaries, doubles on 8-byte boundaries, etc.  On such machines, malloc()
66*dfc6aa5cSAndroid Build Coastguard Worker  * always returns pointers that are multiples of the worst-case alignment
67*dfc6aa5cSAndroid Build Coastguard Worker  * requirement, and we had better do so too.
68*dfc6aa5cSAndroid Build Coastguard Worker  * There isn't any really portable way to determine the worst-case alignment
69*dfc6aa5cSAndroid Build Coastguard Worker  * requirement.  This module assumes that the alignment requirement is
70*dfc6aa5cSAndroid Build Coastguard Worker  * multiples of ALIGN_SIZE.
71*dfc6aa5cSAndroid Build Coastguard Worker  * By default, we define ALIGN_SIZE as the maximum of sizeof(double) and
72*dfc6aa5cSAndroid Build Coastguard Worker  * sizeof(void *).  This is necessary on some workstations (where doubles
73*dfc6aa5cSAndroid Build Coastguard Worker  * really do need 8-byte alignment) and will work fine on nearly everything.
74*dfc6aa5cSAndroid Build Coastguard Worker  * We use the maximum of sizeof(double) and sizeof(void *) since sizeof(double)
75*dfc6aa5cSAndroid Build Coastguard Worker  * may be insufficient, for example, on CHERI-enabled platforms with 16-byte
76*dfc6aa5cSAndroid Build Coastguard Worker  * pointers and a 16-byte alignment requirement.  If your machine has lesser
77*dfc6aa5cSAndroid Build Coastguard Worker  * alignment needs, you can save a few bytes by making ALIGN_SIZE smaller.
78*dfc6aa5cSAndroid Build Coastguard Worker  * The only place I know of where this will NOT work is certain Macintosh
79*dfc6aa5cSAndroid Build Coastguard Worker  * 680x0 compilers that define double as a 10-byte IEEE extended float.
80*dfc6aa5cSAndroid Build Coastguard Worker  * Doing 10-byte alignment is counterproductive because longwords won't be
81*dfc6aa5cSAndroid Build Coastguard Worker  * aligned well.  Put "#define ALIGN_SIZE 4" in jconfig.h if you have
82*dfc6aa5cSAndroid Build Coastguard Worker  * such a compiler.
83*dfc6aa5cSAndroid Build Coastguard Worker  */
84*dfc6aa5cSAndroid Build Coastguard Worker 
85*dfc6aa5cSAndroid Build Coastguard Worker #ifndef ALIGN_SIZE              /* so can override from jconfig.h */
86*dfc6aa5cSAndroid Build Coastguard Worker #ifndef WITH_SIMD
87*dfc6aa5cSAndroid Build Coastguard Worker #define ALIGN_SIZE  MAX(sizeof(void *), sizeof(double))
88*dfc6aa5cSAndroid Build Coastguard Worker #else
89*dfc6aa5cSAndroid Build Coastguard Worker #define ALIGN_SIZE  32 /* Most of the SIMD instructions we support require
90*dfc6aa5cSAndroid Build Coastguard Worker                           16-byte (128-bit) alignment, but AVX2 requires
91*dfc6aa5cSAndroid Build Coastguard Worker                           32-byte alignment. */
92*dfc6aa5cSAndroid Build Coastguard Worker #endif
93*dfc6aa5cSAndroid Build Coastguard Worker #endif
94*dfc6aa5cSAndroid Build Coastguard Worker 
95*dfc6aa5cSAndroid Build Coastguard Worker /*
96*dfc6aa5cSAndroid Build Coastguard Worker  * We allocate objects from "pools", where each pool is gotten with a single
97*dfc6aa5cSAndroid Build Coastguard Worker  * request to jpeg_get_small() or jpeg_get_large().  There is no per-object
98*dfc6aa5cSAndroid Build Coastguard Worker  * overhead within a pool, except for alignment padding.  Each pool has a
99*dfc6aa5cSAndroid Build Coastguard Worker  * header with a link to the next pool of the same class.
100*dfc6aa5cSAndroid Build Coastguard Worker  * Small and large pool headers are identical.
101*dfc6aa5cSAndroid Build Coastguard Worker  */
102*dfc6aa5cSAndroid Build Coastguard Worker 
103*dfc6aa5cSAndroid Build Coastguard Worker typedef struct small_pool_struct *small_pool_ptr;
104*dfc6aa5cSAndroid Build Coastguard Worker 
105*dfc6aa5cSAndroid Build Coastguard Worker typedef struct small_pool_struct {
106*dfc6aa5cSAndroid Build Coastguard Worker   small_pool_ptr next;          /* next in list of pools */
107*dfc6aa5cSAndroid Build Coastguard Worker   size_t bytes_used;            /* how many bytes already used within pool */
108*dfc6aa5cSAndroid Build Coastguard Worker   size_t bytes_left;            /* bytes still available in this pool */
109*dfc6aa5cSAndroid Build Coastguard Worker } small_pool_hdr;
110*dfc6aa5cSAndroid Build Coastguard Worker 
111*dfc6aa5cSAndroid Build Coastguard Worker typedef struct large_pool_struct *large_pool_ptr;
112*dfc6aa5cSAndroid Build Coastguard Worker 
113*dfc6aa5cSAndroid Build Coastguard Worker typedef struct large_pool_struct {
114*dfc6aa5cSAndroid Build Coastguard Worker   large_pool_ptr next;          /* next in list of pools */
115*dfc6aa5cSAndroid Build Coastguard Worker   size_t bytes_used;            /* how many bytes already used within pool */
116*dfc6aa5cSAndroid Build Coastguard Worker   size_t bytes_left;            /* bytes still available in this pool */
117*dfc6aa5cSAndroid Build Coastguard Worker } large_pool_hdr;
118*dfc6aa5cSAndroid Build Coastguard Worker 
119*dfc6aa5cSAndroid Build Coastguard Worker /*
120*dfc6aa5cSAndroid Build Coastguard Worker  * Here is the full definition of a memory manager object.
121*dfc6aa5cSAndroid Build Coastguard Worker  */
122*dfc6aa5cSAndroid Build Coastguard Worker 
123*dfc6aa5cSAndroid Build Coastguard Worker typedef struct {
124*dfc6aa5cSAndroid Build Coastguard Worker   struct jpeg_memory_mgr pub;   /* public fields */
125*dfc6aa5cSAndroid Build Coastguard Worker 
126*dfc6aa5cSAndroid Build Coastguard Worker   /* Each pool identifier (lifetime class) names a linked list of pools. */
127*dfc6aa5cSAndroid Build Coastguard Worker   small_pool_ptr small_list[JPOOL_NUMPOOLS];
128*dfc6aa5cSAndroid Build Coastguard Worker   large_pool_ptr large_list[JPOOL_NUMPOOLS];
129*dfc6aa5cSAndroid Build Coastguard Worker 
130*dfc6aa5cSAndroid Build Coastguard Worker   /* Since we only have one lifetime class of virtual arrays, only one
131*dfc6aa5cSAndroid Build Coastguard Worker    * linked list is necessary (for each datatype).  Note that the virtual
132*dfc6aa5cSAndroid Build Coastguard Worker    * array control blocks being linked together are actually stored somewhere
133*dfc6aa5cSAndroid Build Coastguard Worker    * in the small-pool list.
134*dfc6aa5cSAndroid Build Coastguard Worker    */
135*dfc6aa5cSAndroid Build Coastguard Worker   jvirt_sarray_ptr virt_sarray_list;
136*dfc6aa5cSAndroid Build Coastguard Worker   jvirt_barray_ptr virt_barray_list;
137*dfc6aa5cSAndroid Build Coastguard Worker 
138*dfc6aa5cSAndroid Build Coastguard Worker   /* This counts total space obtained from jpeg_get_small/large */
139*dfc6aa5cSAndroid Build Coastguard Worker   size_t total_space_allocated;
140*dfc6aa5cSAndroid Build Coastguard Worker 
141*dfc6aa5cSAndroid Build Coastguard Worker   /* alloc_sarray and alloc_barray set this value for use by virtual
142*dfc6aa5cSAndroid Build Coastguard Worker    * array routines.
143*dfc6aa5cSAndroid Build Coastguard Worker    */
144*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION last_rowsperchunk; /* from most recent alloc_sarray/barray */
145*dfc6aa5cSAndroid Build Coastguard Worker } my_memory_mgr;
146*dfc6aa5cSAndroid Build Coastguard Worker 
147*dfc6aa5cSAndroid Build Coastguard Worker typedef my_memory_mgr *my_mem_ptr;
148*dfc6aa5cSAndroid Build Coastguard Worker 
149*dfc6aa5cSAndroid Build Coastguard Worker 
150*dfc6aa5cSAndroid Build Coastguard Worker /*
151*dfc6aa5cSAndroid Build Coastguard Worker  * The control blocks for virtual arrays.
152*dfc6aa5cSAndroid Build Coastguard Worker  * Note that these blocks are allocated in the "small" pool area.
153*dfc6aa5cSAndroid Build Coastguard Worker  * System-dependent info for the associated backing store (if any) is hidden
154*dfc6aa5cSAndroid Build Coastguard Worker  * inside the backing_store_info struct.
155*dfc6aa5cSAndroid Build Coastguard Worker  */
156*dfc6aa5cSAndroid Build Coastguard Worker 
157*dfc6aa5cSAndroid Build Coastguard Worker struct jvirt_sarray_control {
158*dfc6aa5cSAndroid Build Coastguard Worker   JSAMPARRAY mem_buffer;        /* => the in-memory buffer */
159*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION rows_in_array;     /* total virtual array height */
160*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION samplesperrow;     /* width of array (and of memory buffer) */
161*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION maxaccess;         /* max rows accessed by access_virt_sarray */
162*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION rows_in_mem;       /* height of memory buffer */
163*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION rowsperchunk;      /* allocation chunk size in mem_buffer */
164*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION cur_start_row;     /* first logical row # in the buffer */
165*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION first_undef_row;   /* row # of first uninitialized row */
166*dfc6aa5cSAndroid Build Coastguard Worker   boolean pre_zero;             /* pre-zero mode requested? */
167*dfc6aa5cSAndroid Build Coastguard Worker   boolean dirty;                /* do current buffer contents need written? */
168*dfc6aa5cSAndroid Build Coastguard Worker   boolean b_s_open;             /* is backing-store data valid? */
169*dfc6aa5cSAndroid Build Coastguard Worker   jvirt_sarray_ptr next;        /* link to next virtual sarray control block */
170*dfc6aa5cSAndroid Build Coastguard Worker   backing_store_info b_s_info;  /* System-dependent control info */
171*dfc6aa5cSAndroid Build Coastguard Worker };
172*dfc6aa5cSAndroid Build Coastguard Worker 
173*dfc6aa5cSAndroid Build Coastguard Worker struct jvirt_barray_control {
174*dfc6aa5cSAndroid Build Coastguard Worker   JBLOCKARRAY mem_buffer;       /* => the in-memory buffer */
175*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION rows_in_array;     /* total virtual array height */
176*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION blocksperrow;      /* width of array (and of memory buffer) */
177*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION maxaccess;         /* max rows accessed by access_virt_barray */
178*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION rows_in_mem;       /* height of memory buffer */
179*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION rowsperchunk;      /* allocation chunk size in mem_buffer */
180*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION cur_start_row;     /* first logical row # in the buffer */
181*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION first_undef_row;   /* row # of first uninitialized row */
182*dfc6aa5cSAndroid Build Coastguard Worker   boolean pre_zero;             /* pre-zero mode requested? */
183*dfc6aa5cSAndroid Build Coastguard Worker   boolean dirty;                /* do current buffer contents need written? */
184*dfc6aa5cSAndroid Build Coastguard Worker   boolean b_s_open;             /* is backing-store data valid? */
185*dfc6aa5cSAndroid Build Coastguard Worker   jvirt_barray_ptr next;        /* link to next virtual barray control block */
186*dfc6aa5cSAndroid Build Coastguard Worker   backing_store_info b_s_info;  /* System-dependent control info */
187*dfc6aa5cSAndroid Build Coastguard Worker };
188*dfc6aa5cSAndroid Build Coastguard Worker 
189*dfc6aa5cSAndroid Build Coastguard Worker 
190*dfc6aa5cSAndroid Build Coastguard Worker #ifdef MEM_STATS                /* optional extra stuff for statistics */
191*dfc6aa5cSAndroid Build Coastguard Worker 
192*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(void)
print_mem_stats(j_common_ptr cinfo,int pool_id)193*dfc6aa5cSAndroid Build Coastguard Worker print_mem_stats(j_common_ptr cinfo, int pool_id)
194*dfc6aa5cSAndroid Build Coastguard Worker {
195*dfc6aa5cSAndroid Build Coastguard Worker   my_mem_ptr mem = (my_mem_ptr)cinfo->mem;
196*dfc6aa5cSAndroid Build Coastguard Worker   small_pool_ptr shdr_ptr;
197*dfc6aa5cSAndroid Build Coastguard Worker   large_pool_ptr lhdr_ptr;
198*dfc6aa5cSAndroid Build Coastguard Worker 
199*dfc6aa5cSAndroid Build Coastguard Worker   /* Since this is only a debugging stub, we can cheat a little by using
200*dfc6aa5cSAndroid Build Coastguard Worker    * fprintf directly rather than going through the trace message code.
201*dfc6aa5cSAndroid Build Coastguard Worker    * This is helpful because message parm array can't handle longs.
202*dfc6aa5cSAndroid Build Coastguard Worker    */
203*dfc6aa5cSAndroid Build Coastguard Worker   fprintf(stderr, "Freeing pool %d, total space = %ld\n",
204*dfc6aa5cSAndroid Build Coastguard Worker           pool_id, mem->total_space_allocated);
205*dfc6aa5cSAndroid Build Coastguard Worker 
206*dfc6aa5cSAndroid Build Coastguard Worker   for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
207*dfc6aa5cSAndroid Build Coastguard Worker        lhdr_ptr = lhdr_ptr->next) {
208*dfc6aa5cSAndroid Build Coastguard Worker     fprintf(stderr, "  Large chunk used %ld\n", (long)lhdr_ptr->bytes_used);
209*dfc6aa5cSAndroid Build Coastguard Worker   }
210*dfc6aa5cSAndroid Build Coastguard Worker 
211*dfc6aa5cSAndroid Build Coastguard Worker   for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;
212*dfc6aa5cSAndroid Build Coastguard Worker        shdr_ptr = shdr_ptr->next) {
213*dfc6aa5cSAndroid Build Coastguard Worker     fprintf(stderr, "  Small chunk used %ld free %ld\n",
214*dfc6aa5cSAndroid Build Coastguard Worker             (long)shdr_ptr->bytes_used, (long)shdr_ptr->bytes_left);
215*dfc6aa5cSAndroid Build Coastguard Worker   }
216*dfc6aa5cSAndroid Build Coastguard Worker }
217*dfc6aa5cSAndroid Build Coastguard Worker 
218*dfc6aa5cSAndroid Build Coastguard Worker #endif /* MEM_STATS */
219*dfc6aa5cSAndroid Build Coastguard Worker 
220*dfc6aa5cSAndroid Build Coastguard Worker 
221*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(void)
out_of_memory(j_common_ptr cinfo,int which)222*dfc6aa5cSAndroid Build Coastguard Worker out_of_memory(j_common_ptr cinfo, int which)
223*dfc6aa5cSAndroid Build Coastguard Worker /* Report an out-of-memory error and stop execution */
224*dfc6aa5cSAndroid Build Coastguard Worker /* If we compiled MEM_STATS support, report alloc requests before dying */
225*dfc6aa5cSAndroid Build Coastguard Worker {
226*dfc6aa5cSAndroid Build Coastguard Worker #ifdef MEM_STATS
227*dfc6aa5cSAndroid Build Coastguard Worker   cinfo->err->trace_level = 2;  /* force self_destruct to report stats */
228*dfc6aa5cSAndroid Build Coastguard Worker #endif
229*dfc6aa5cSAndroid Build Coastguard Worker   ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
230*dfc6aa5cSAndroid Build Coastguard Worker }
231*dfc6aa5cSAndroid Build Coastguard Worker 
232*dfc6aa5cSAndroid Build Coastguard Worker 
233*dfc6aa5cSAndroid Build Coastguard Worker /*
234*dfc6aa5cSAndroid Build Coastguard Worker  * Allocation of "small" objects.
235*dfc6aa5cSAndroid Build Coastguard Worker  *
236*dfc6aa5cSAndroid Build Coastguard Worker  * For these, we use pooled storage.  When a new pool must be created,
237*dfc6aa5cSAndroid Build Coastguard Worker  * we try to get enough space for the current request plus a "slop" factor,
238*dfc6aa5cSAndroid Build Coastguard Worker  * where the slop will be the amount of leftover space in the new pool.
239*dfc6aa5cSAndroid Build Coastguard Worker  * The speed vs. space tradeoff is largely determined by the slop values.
240*dfc6aa5cSAndroid Build Coastguard Worker  * A different slop value is provided for each pool class (lifetime),
241*dfc6aa5cSAndroid Build Coastguard Worker  * and we also distinguish the first pool of a class from later ones.
242*dfc6aa5cSAndroid Build Coastguard Worker  * NOTE: the values given work fairly well on both 16- and 32-bit-int
243*dfc6aa5cSAndroid Build Coastguard Worker  * machines, but may be too small if longs are 64 bits or more.
244*dfc6aa5cSAndroid Build Coastguard Worker  *
245*dfc6aa5cSAndroid Build Coastguard Worker  * Since we do not know what alignment malloc() gives us, we have to
246*dfc6aa5cSAndroid Build Coastguard Worker  * allocate ALIGN_SIZE-1 extra space per pool to have room for alignment
247*dfc6aa5cSAndroid Build Coastguard Worker  * adjustment.
248*dfc6aa5cSAndroid Build Coastguard Worker  */
249*dfc6aa5cSAndroid Build Coastguard Worker 
250*dfc6aa5cSAndroid Build Coastguard Worker static const size_t first_pool_slop[JPOOL_NUMPOOLS] = {
251*dfc6aa5cSAndroid Build Coastguard Worker   1600,                         /* first PERMANENT pool */
252*dfc6aa5cSAndroid Build Coastguard Worker   16000                         /* first IMAGE pool */
253*dfc6aa5cSAndroid Build Coastguard Worker };
254*dfc6aa5cSAndroid Build Coastguard Worker 
255*dfc6aa5cSAndroid Build Coastguard Worker static const size_t extra_pool_slop[JPOOL_NUMPOOLS] = {
256*dfc6aa5cSAndroid Build Coastguard Worker   0,                            /* additional PERMANENT pools */
257*dfc6aa5cSAndroid Build Coastguard Worker   5000                          /* additional IMAGE pools */
258*dfc6aa5cSAndroid Build Coastguard Worker };
259*dfc6aa5cSAndroid Build Coastguard Worker 
260*dfc6aa5cSAndroid Build Coastguard Worker #define MIN_SLOP  50            /* greater than 0 to avoid futile looping */
261*dfc6aa5cSAndroid Build Coastguard Worker 
262*dfc6aa5cSAndroid Build Coastguard Worker 
263*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void *)
alloc_small(j_common_ptr cinfo,int pool_id,size_t sizeofobject)264*dfc6aa5cSAndroid Build Coastguard Worker alloc_small(j_common_ptr cinfo, int pool_id, size_t sizeofobject)
265*dfc6aa5cSAndroid Build Coastguard Worker /* Allocate a "small" object */
266*dfc6aa5cSAndroid Build Coastguard Worker {
267*dfc6aa5cSAndroid Build Coastguard Worker   my_mem_ptr mem = (my_mem_ptr)cinfo->mem;
268*dfc6aa5cSAndroid Build Coastguard Worker   small_pool_ptr hdr_ptr, prev_hdr_ptr;
269*dfc6aa5cSAndroid Build Coastguard Worker   char *data_ptr;
270*dfc6aa5cSAndroid Build Coastguard Worker   size_t min_request, slop;
271*dfc6aa5cSAndroid Build Coastguard Worker 
272*dfc6aa5cSAndroid Build Coastguard Worker   /*
273*dfc6aa5cSAndroid Build Coastguard Worker    * Round up the requested size to a multiple of ALIGN_SIZE in order
274*dfc6aa5cSAndroid Build Coastguard Worker    * to assure alignment for the next object allocated in the same pool
275*dfc6aa5cSAndroid Build Coastguard Worker    * and so that algorithms can straddle outside the proper area up
276*dfc6aa5cSAndroid Build Coastguard Worker    * to the next alignment.
277*dfc6aa5cSAndroid Build Coastguard Worker    */
278*dfc6aa5cSAndroid Build Coastguard Worker   if (sizeofobject > MAX_ALLOC_CHUNK) {
279*dfc6aa5cSAndroid Build Coastguard Worker     /* This prevents overflow/wrap-around in round_up_pow2() if sizeofobject
280*dfc6aa5cSAndroid Build Coastguard Worker        is close to SIZE_MAX. */
281*dfc6aa5cSAndroid Build Coastguard Worker     out_of_memory(cinfo, 7);
282*dfc6aa5cSAndroid Build Coastguard Worker   }
283*dfc6aa5cSAndroid Build Coastguard Worker   sizeofobject = round_up_pow2(sizeofobject, ALIGN_SIZE);
284*dfc6aa5cSAndroid Build Coastguard Worker 
285*dfc6aa5cSAndroid Build Coastguard Worker   /* Check for unsatisfiable request (do now to ensure no overflow below) */
286*dfc6aa5cSAndroid Build Coastguard Worker   if ((sizeof(small_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) >
287*dfc6aa5cSAndroid Build Coastguard Worker       MAX_ALLOC_CHUNK)
288*dfc6aa5cSAndroid Build Coastguard Worker     out_of_memory(cinfo, 1);    /* request exceeds malloc's ability */
289*dfc6aa5cSAndroid Build Coastguard Worker 
290*dfc6aa5cSAndroid Build Coastguard Worker   /* See if space is available in any existing pool */
291*dfc6aa5cSAndroid Build Coastguard Worker   if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
292*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
293*dfc6aa5cSAndroid Build Coastguard Worker   prev_hdr_ptr = NULL;
294*dfc6aa5cSAndroid Build Coastguard Worker   hdr_ptr = mem->small_list[pool_id];
295*dfc6aa5cSAndroid Build Coastguard Worker   while (hdr_ptr != NULL) {
296*dfc6aa5cSAndroid Build Coastguard Worker     if (hdr_ptr->bytes_left >= sizeofobject)
297*dfc6aa5cSAndroid Build Coastguard Worker       break;                    /* found pool with enough space */
298*dfc6aa5cSAndroid Build Coastguard Worker     prev_hdr_ptr = hdr_ptr;
299*dfc6aa5cSAndroid Build Coastguard Worker     hdr_ptr = hdr_ptr->next;
300*dfc6aa5cSAndroid Build Coastguard Worker   }
301*dfc6aa5cSAndroid Build Coastguard Worker 
302*dfc6aa5cSAndroid Build Coastguard Worker   /* Time to make a new pool? */
303*dfc6aa5cSAndroid Build Coastguard Worker   if (hdr_ptr == NULL) {
304*dfc6aa5cSAndroid Build Coastguard Worker     /* min_request is what we need now, slop is what will be leftover */
305*dfc6aa5cSAndroid Build Coastguard Worker     min_request = sizeof(small_pool_hdr) + sizeofobject + ALIGN_SIZE - 1;
306*dfc6aa5cSAndroid Build Coastguard Worker     if (prev_hdr_ptr == NULL)   /* first pool in class? */
307*dfc6aa5cSAndroid Build Coastguard Worker       slop = first_pool_slop[pool_id];
308*dfc6aa5cSAndroid Build Coastguard Worker     else
309*dfc6aa5cSAndroid Build Coastguard Worker       slop = extra_pool_slop[pool_id];
310*dfc6aa5cSAndroid Build Coastguard Worker     /* Don't ask for more than MAX_ALLOC_CHUNK */
311*dfc6aa5cSAndroid Build Coastguard Worker     if (slop > (size_t)(MAX_ALLOC_CHUNK - min_request))
312*dfc6aa5cSAndroid Build Coastguard Worker       slop = (size_t)(MAX_ALLOC_CHUNK - min_request);
313*dfc6aa5cSAndroid Build Coastguard Worker     /* Try to get space, if fail reduce slop and try again */
314*dfc6aa5cSAndroid Build Coastguard Worker     for (;;) {
315*dfc6aa5cSAndroid Build Coastguard Worker       hdr_ptr = (small_pool_ptr)jpeg_get_small(cinfo, min_request + slop);
316*dfc6aa5cSAndroid Build Coastguard Worker       if (hdr_ptr != NULL)
317*dfc6aa5cSAndroid Build Coastguard Worker         break;
318*dfc6aa5cSAndroid Build Coastguard Worker       slop /= 2;
319*dfc6aa5cSAndroid Build Coastguard Worker       if (slop < MIN_SLOP)      /* give up when it gets real small */
320*dfc6aa5cSAndroid Build Coastguard Worker         out_of_memory(cinfo, 2); /* jpeg_get_small failed */
321*dfc6aa5cSAndroid Build Coastguard Worker     }
322*dfc6aa5cSAndroid Build Coastguard Worker     mem->total_space_allocated += min_request + slop;
323*dfc6aa5cSAndroid Build Coastguard Worker     /* Success, initialize the new pool header and add to end of list */
324*dfc6aa5cSAndroid Build Coastguard Worker     hdr_ptr->next = NULL;
325*dfc6aa5cSAndroid Build Coastguard Worker     hdr_ptr->bytes_used = 0;
326*dfc6aa5cSAndroid Build Coastguard Worker     hdr_ptr->bytes_left = sizeofobject + slop;
327*dfc6aa5cSAndroid Build Coastguard Worker     if (prev_hdr_ptr == NULL)   /* first pool in class? */
328*dfc6aa5cSAndroid Build Coastguard Worker       mem->small_list[pool_id] = hdr_ptr;
329*dfc6aa5cSAndroid Build Coastguard Worker     else
330*dfc6aa5cSAndroid Build Coastguard Worker       prev_hdr_ptr->next = hdr_ptr;
331*dfc6aa5cSAndroid Build Coastguard Worker   }
332*dfc6aa5cSAndroid Build Coastguard Worker 
333*dfc6aa5cSAndroid Build Coastguard Worker   /* OK, allocate the object from the current pool */
334*dfc6aa5cSAndroid Build Coastguard Worker   data_ptr = (char *)hdr_ptr; /* point to first data byte in pool... */
335*dfc6aa5cSAndroid Build Coastguard Worker   data_ptr += sizeof(small_pool_hdr); /* ...by skipping the header... */
336*dfc6aa5cSAndroid Build Coastguard Worker   if ((size_t)data_ptr % ALIGN_SIZE) /* ...and adjust for alignment */
337*dfc6aa5cSAndroid Build Coastguard Worker     data_ptr += ALIGN_SIZE - (size_t)data_ptr % ALIGN_SIZE;
338*dfc6aa5cSAndroid Build Coastguard Worker   data_ptr += hdr_ptr->bytes_used; /* point to place for object */
339*dfc6aa5cSAndroid Build Coastguard Worker   hdr_ptr->bytes_used += sizeofobject;
340*dfc6aa5cSAndroid Build Coastguard Worker   hdr_ptr->bytes_left -= sizeofobject;
341*dfc6aa5cSAndroid Build Coastguard Worker 
342*dfc6aa5cSAndroid Build Coastguard Worker   return (void *)data_ptr;
343*dfc6aa5cSAndroid Build Coastguard Worker }
344*dfc6aa5cSAndroid Build Coastguard Worker 
345*dfc6aa5cSAndroid Build Coastguard Worker 
346*dfc6aa5cSAndroid Build Coastguard Worker /*
347*dfc6aa5cSAndroid Build Coastguard Worker  * Allocation of "large" objects.
348*dfc6aa5cSAndroid Build Coastguard Worker  *
349*dfc6aa5cSAndroid Build Coastguard Worker  * The external semantics of these are the same as "small" objects.  However,
350*dfc6aa5cSAndroid Build Coastguard Worker  * the pool management heuristics are quite different.  We assume that each
351*dfc6aa5cSAndroid Build Coastguard Worker  * request is large enough that it may as well be passed directly to
352*dfc6aa5cSAndroid Build Coastguard Worker  * jpeg_get_large; the pool management just links everything together
353*dfc6aa5cSAndroid Build Coastguard Worker  * so that we can free it all on demand.
354*dfc6aa5cSAndroid Build Coastguard Worker  * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY
355*dfc6aa5cSAndroid Build Coastguard Worker  * structures.  The routines that create these structures (see below)
356*dfc6aa5cSAndroid Build Coastguard Worker  * deliberately bunch rows together to ensure a large request size.
357*dfc6aa5cSAndroid Build Coastguard Worker  */
358*dfc6aa5cSAndroid Build Coastguard Worker 
359*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void *)
alloc_large(j_common_ptr cinfo,int pool_id,size_t sizeofobject)360*dfc6aa5cSAndroid Build Coastguard Worker alloc_large(j_common_ptr cinfo, int pool_id, size_t sizeofobject)
361*dfc6aa5cSAndroid Build Coastguard Worker /* Allocate a "large" object */
362*dfc6aa5cSAndroid Build Coastguard Worker {
363*dfc6aa5cSAndroid Build Coastguard Worker   my_mem_ptr mem = (my_mem_ptr)cinfo->mem;
364*dfc6aa5cSAndroid Build Coastguard Worker   large_pool_ptr hdr_ptr;
365*dfc6aa5cSAndroid Build Coastguard Worker   char *data_ptr;
366*dfc6aa5cSAndroid Build Coastguard Worker 
367*dfc6aa5cSAndroid Build Coastguard Worker   /*
368*dfc6aa5cSAndroid Build Coastguard Worker    * Round up the requested size to a multiple of ALIGN_SIZE so that
369*dfc6aa5cSAndroid Build Coastguard Worker    * algorithms can straddle outside the proper area up to the next
370*dfc6aa5cSAndroid Build Coastguard Worker    * alignment.
371*dfc6aa5cSAndroid Build Coastguard Worker    */
372*dfc6aa5cSAndroid Build Coastguard Worker   if (sizeofobject > MAX_ALLOC_CHUNK) {
373*dfc6aa5cSAndroid Build Coastguard Worker     /* This prevents overflow/wrap-around in round_up_pow2() if sizeofobject
374*dfc6aa5cSAndroid Build Coastguard Worker        is close to SIZE_MAX. */
375*dfc6aa5cSAndroid Build Coastguard Worker     out_of_memory(cinfo, 8);
376*dfc6aa5cSAndroid Build Coastguard Worker   }
377*dfc6aa5cSAndroid Build Coastguard Worker   sizeofobject = round_up_pow2(sizeofobject, ALIGN_SIZE);
378*dfc6aa5cSAndroid Build Coastguard Worker 
379*dfc6aa5cSAndroid Build Coastguard Worker   /* Check for unsatisfiable request (do now to ensure no overflow below) */
380*dfc6aa5cSAndroid Build Coastguard Worker   if ((sizeof(large_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) >
381*dfc6aa5cSAndroid Build Coastguard Worker       MAX_ALLOC_CHUNK)
382*dfc6aa5cSAndroid Build Coastguard Worker     out_of_memory(cinfo, 3);    /* request exceeds malloc's ability */
383*dfc6aa5cSAndroid Build Coastguard Worker 
384*dfc6aa5cSAndroid Build Coastguard Worker   /* Always make a new pool */
385*dfc6aa5cSAndroid Build Coastguard Worker   if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
386*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
387*dfc6aa5cSAndroid Build Coastguard Worker 
388*dfc6aa5cSAndroid Build Coastguard Worker   hdr_ptr = (large_pool_ptr)jpeg_get_large(cinfo, sizeofobject +
389*dfc6aa5cSAndroid Build Coastguard Worker                                            sizeof(large_pool_hdr) +
390*dfc6aa5cSAndroid Build Coastguard Worker                                            ALIGN_SIZE - 1);
391*dfc6aa5cSAndroid Build Coastguard Worker   if (hdr_ptr == NULL)
392*dfc6aa5cSAndroid Build Coastguard Worker     out_of_memory(cinfo, 4);    /* jpeg_get_large failed */
393*dfc6aa5cSAndroid Build Coastguard Worker   mem->total_space_allocated += sizeofobject + sizeof(large_pool_hdr) +
394*dfc6aa5cSAndroid Build Coastguard Worker                                 ALIGN_SIZE - 1;
395*dfc6aa5cSAndroid Build Coastguard Worker 
396*dfc6aa5cSAndroid Build Coastguard Worker   /* Success, initialize the new pool header and add to list */
397*dfc6aa5cSAndroid Build Coastguard Worker   hdr_ptr->next = mem->large_list[pool_id];
398*dfc6aa5cSAndroid Build Coastguard Worker   /* We maintain space counts in each pool header for statistical purposes,
399*dfc6aa5cSAndroid Build Coastguard Worker    * even though they are not needed for allocation.
400*dfc6aa5cSAndroid Build Coastguard Worker    */
401*dfc6aa5cSAndroid Build Coastguard Worker   hdr_ptr->bytes_used = sizeofobject;
402*dfc6aa5cSAndroid Build Coastguard Worker   hdr_ptr->bytes_left = 0;
403*dfc6aa5cSAndroid Build Coastguard Worker   mem->large_list[pool_id] = hdr_ptr;
404*dfc6aa5cSAndroid Build Coastguard Worker 
405*dfc6aa5cSAndroid Build Coastguard Worker   data_ptr = (char *)hdr_ptr; /* point to first data byte in pool... */
406*dfc6aa5cSAndroid Build Coastguard Worker   data_ptr += sizeof(small_pool_hdr); /* ...by skipping the header... */
407*dfc6aa5cSAndroid Build Coastguard Worker   if ((size_t)data_ptr % ALIGN_SIZE) /* ...and adjust for alignment */
408*dfc6aa5cSAndroid Build Coastguard Worker     data_ptr += ALIGN_SIZE - (size_t)data_ptr % ALIGN_SIZE;
409*dfc6aa5cSAndroid Build Coastguard Worker 
410*dfc6aa5cSAndroid Build Coastguard Worker   return (void *)data_ptr;
411*dfc6aa5cSAndroid Build Coastguard Worker }
412*dfc6aa5cSAndroid Build Coastguard Worker 
413*dfc6aa5cSAndroid Build Coastguard Worker 
414*dfc6aa5cSAndroid Build Coastguard Worker /*
415*dfc6aa5cSAndroid Build Coastguard Worker  * Creation of 2-D sample arrays.
416*dfc6aa5cSAndroid Build Coastguard Worker  *
417*dfc6aa5cSAndroid Build Coastguard Worker  * To minimize allocation overhead and to allow I/O of large contiguous
418*dfc6aa5cSAndroid Build Coastguard Worker  * blocks, we allocate the sample rows in groups of as many rows as possible
419*dfc6aa5cSAndroid Build Coastguard Worker  * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.
420*dfc6aa5cSAndroid Build Coastguard Worker  * NB: the virtual array control routines, later in this file, know about
421*dfc6aa5cSAndroid Build Coastguard Worker  * this chunking of rows.  The rowsperchunk value is left in the mem manager
422*dfc6aa5cSAndroid Build Coastguard Worker  * object so that it can be saved away if this sarray is the workspace for
423*dfc6aa5cSAndroid Build Coastguard Worker  * a virtual array.
424*dfc6aa5cSAndroid Build Coastguard Worker  *
425*dfc6aa5cSAndroid Build Coastguard Worker  * Since we are often upsampling with a factor 2, we align the size (not
426*dfc6aa5cSAndroid Build Coastguard Worker  * the start) to 2 * ALIGN_SIZE so that the upsampling routines don't have
427*dfc6aa5cSAndroid Build Coastguard Worker  * to be as careful about size.
428*dfc6aa5cSAndroid Build Coastguard Worker  */
429*dfc6aa5cSAndroid Build Coastguard Worker 
430*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(JSAMPARRAY)
alloc_sarray(j_common_ptr cinfo,int pool_id,JDIMENSION samplesperrow,JDIMENSION numrows)431*dfc6aa5cSAndroid Build Coastguard Worker alloc_sarray(j_common_ptr cinfo, int pool_id, JDIMENSION samplesperrow,
432*dfc6aa5cSAndroid Build Coastguard Worker              JDIMENSION numrows)
433*dfc6aa5cSAndroid Build Coastguard Worker /* Allocate a 2-D sample array */
434*dfc6aa5cSAndroid Build Coastguard Worker {
435*dfc6aa5cSAndroid Build Coastguard Worker   my_mem_ptr mem = (my_mem_ptr)cinfo->mem;
436*dfc6aa5cSAndroid Build Coastguard Worker   JSAMPARRAY result;
437*dfc6aa5cSAndroid Build Coastguard Worker   JSAMPROW workspace;
438*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION rowsperchunk, currow, i;
439*dfc6aa5cSAndroid Build Coastguard Worker   long ltemp;
440*dfc6aa5cSAndroid Build Coastguard Worker 
441*dfc6aa5cSAndroid Build Coastguard Worker   /* Make sure each row is properly aligned */
442*dfc6aa5cSAndroid Build Coastguard Worker   if ((ALIGN_SIZE % sizeof(JSAMPLE)) != 0)
443*dfc6aa5cSAndroid Build Coastguard Worker     out_of_memory(cinfo, 5);    /* safety check */
444*dfc6aa5cSAndroid Build Coastguard Worker 
445*dfc6aa5cSAndroid Build Coastguard Worker   if (samplesperrow > MAX_ALLOC_CHUNK) {
446*dfc6aa5cSAndroid Build Coastguard Worker     /* This prevents overflow/wrap-around in round_up_pow2() if sizeofobject
447*dfc6aa5cSAndroid Build Coastguard Worker        is close to SIZE_MAX. */
448*dfc6aa5cSAndroid Build Coastguard Worker     out_of_memory(cinfo, 9);
449*dfc6aa5cSAndroid Build Coastguard Worker   }
450*dfc6aa5cSAndroid Build Coastguard Worker   samplesperrow = (JDIMENSION)round_up_pow2(samplesperrow, (2 * ALIGN_SIZE) /
451*dfc6aa5cSAndroid Build Coastguard Worker                                                            sizeof(JSAMPLE));
452*dfc6aa5cSAndroid Build Coastguard Worker 
453*dfc6aa5cSAndroid Build Coastguard Worker   /* Calculate max # of rows allowed in one allocation chunk */
454*dfc6aa5cSAndroid Build Coastguard Worker   ltemp = (MAX_ALLOC_CHUNK - sizeof(large_pool_hdr)) /
455*dfc6aa5cSAndroid Build Coastguard Worker           ((long)samplesperrow * sizeof(JSAMPLE));
456*dfc6aa5cSAndroid Build Coastguard Worker   if (ltemp <= 0)
457*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
458*dfc6aa5cSAndroid Build Coastguard Worker   if (ltemp < (long)numrows)
459*dfc6aa5cSAndroid Build Coastguard Worker     rowsperchunk = (JDIMENSION)ltemp;
460*dfc6aa5cSAndroid Build Coastguard Worker   else
461*dfc6aa5cSAndroid Build Coastguard Worker     rowsperchunk = numrows;
462*dfc6aa5cSAndroid Build Coastguard Worker   mem->last_rowsperchunk = rowsperchunk;
463*dfc6aa5cSAndroid Build Coastguard Worker 
464*dfc6aa5cSAndroid Build Coastguard Worker   /* Get space for row pointers (small object) */
465*dfc6aa5cSAndroid Build Coastguard Worker   result = (JSAMPARRAY)alloc_small(cinfo, pool_id,
466*dfc6aa5cSAndroid Build Coastguard Worker                                    (size_t)(numrows * sizeof(JSAMPROW)));
467*dfc6aa5cSAndroid Build Coastguard Worker 
468*dfc6aa5cSAndroid Build Coastguard Worker   /* Get the rows themselves (large objects) */
469*dfc6aa5cSAndroid Build Coastguard Worker   currow = 0;
470*dfc6aa5cSAndroid Build Coastguard Worker   while (currow < numrows) {
471*dfc6aa5cSAndroid Build Coastguard Worker     rowsperchunk = MIN(rowsperchunk, numrows - currow);
472*dfc6aa5cSAndroid Build Coastguard Worker     workspace = (JSAMPROW)alloc_large(cinfo, pool_id,
473*dfc6aa5cSAndroid Build Coastguard Worker       (size_t)((size_t)rowsperchunk * (size_t)samplesperrow *
474*dfc6aa5cSAndroid Build Coastguard Worker                sizeof(JSAMPLE)));
475*dfc6aa5cSAndroid Build Coastguard Worker     for (i = rowsperchunk; i > 0; i--) {
476*dfc6aa5cSAndroid Build Coastguard Worker       result[currow++] = workspace;
477*dfc6aa5cSAndroid Build Coastguard Worker       workspace += samplesperrow;
478*dfc6aa5cSAndroid Build Coastguard Worker     }
479*dfc6aa5cSAndroid Build Coastguard Worker   }
480*dfc6aa5cSAndroid Build Coastguard Worker 
481*dfc6aa5cSAndroid Build Coastguard Worker   return result;
482*dfc6aa5cSAndroid Build Coastguard Worker }
483*dfc6aa5cSAndroid Build Coastguard Worker 
484*dfc6aa5cSAndroid Build Coastguard Worker 
485*dfc6aa5cSAndroid Build Coastguard Worker /*
486*dfc6aa5cSAndroid Build Coastguard Worker  * Creation of 2-D coefficient-block arrays.
487*dfc6aa5cSAndroid Build Coastguard Worker  * This is essentially the same as the code for sample arrays, above.
488*dfc6aa5cSAndroid Build Coastguard Worker  */
489*dfc6aa5cSAndroid Build Coastguard Worker 
490*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(JBLOCKARRAY)
alloc_barray(j_common_ptr cinfo,int pool_id,JDIMENSION blocksperrow,JDIMENSION numrows)491*dfc6aa5cSAndroid Build Coastguard Worker alloc_barray(j_common_ptr cinfo, int pool_id, JDIMENSION blocksperrow,
492*dfc6aa5cSAndroid Build Coastguard Worker              JDIMENSION numrows)
493*dfc6aa5cSAndroid Build Coastguard Worker /* Allocate a 2-D coefficient-block array */
494*dfc6aa5cSAndroid Build Coastguard Worker {
495*dfc6aa5cSAndroid Build Coastguard Worker   my_mem_ptr mem = (my_mem_ptr)cinfo->mem;
496*dfc6aa5cSAndroid Build Coastguard Worker   JBLOCKARRAY result;
497*dfc6aa5cSAndroid Build Coastguard Worker   JBLOCKROW workspace;
498*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION rowsperchunk, currow, i;
499*dfc6aa5cSAndroid Build Coastguard Worker   long ltemp;
500*dfc6aa5cSAndroid Build Coastguard Worker 
501*dfc6aa5cSAndroid Build Coastguard Worker   /* Make sure each row is properly aligned */
502*dfc6aa5cSAndroid Build Coastguard Worker   if ((sizeof(JBLOCK) % ALIGN_SIZE) != 0)
503*dfc6aa5cSAndroid Build Coastguard Worker     out_of_memory(cinfo, 6);    /* safety check */
504*dfc6aa5cSAndroid Build Coastguard Worker 
505*dfc6aa5cSAndroid Build Coastguard Worker   /* Calculate max # of rows allowed in one allocation chunk */
506*dfc6aa5cSAndroid Build Coastguard Worker   ltemp = (MAX_ALLOC_CHUNK - sizeof(large_pool_hdr)) /
507*dfc6aa5cSAndroid Build Coastguard Worker           ((long)blocksperrow * sizeof(JBLOCK));
508*dfc6aa5cSAndroid Build Coastguard Worker   if (ltemp <= 0)
509*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
510*dfc6aa5cSAndroid Build Coastguard Worker   if (ltemp < (long)numrows)
511*dfc6aa5cSAndroid Build Coastguard Worker     rowsperchunk = (JDIMENSION)ltemp;
512*dfc6aa5cSAndroid Build Coastguard Worker   else
513*dfc6aa5cSAndroid Build Coastguard Worker     rowsperchunk = numrows;
514*dfc6aa5cSAndroid Build Coastguard Worker   mem->last_rowsperchunk = rowsperchunk;
515*dfc6aa5cSAndroid Build Coastguard Worker 
516*dfc6aa5cSAndroid Build Coastguard Worker   /* Get space for row pointers (small object) */
517*dfc6aa5cSAndroid Build Coastguard Worker   result = (JBLOCKARRAY)alloc_small(cinfo, pool_id,
518*dfc6aa5cSAndroid Build Coastguard Worker                                     (size_t)(numrows * sizeof(JBLOCKROW)));
519*dfc6aa5cSAndroid Build Coastguard Worker 
520*dfc6aa5cSAndroid Build Coastguard Worker   /* Get the rows themselves (large objects) */
521*dfc6aa5cSAndroid Build Coastguard Worker   currow = 0;
522*dfc6aa5cSAndroid Build Coastguard Worker   while (currow < numrows) {
523*dfc6aa5cSAndroid Build Coastguard Worker     rowsperchunk = MIN(rowsperchunk, numrows - currow);
524*dfc6aa5cSAndroid Build Coastguard Worker     workspace = (JBLOCKROW)alloc_large(cinfo, pool_id,
525*dfc6aa5cSAndroid Build Coastguard Worker         (size_t)((size_t)rowsperchunk * (size_t)blocksperrow *
526*dfc6aa5cSAndroid Build Coastguard Worker                   sizeof(JBLOCK)));
527*dfc6aa5cSAndroid Build Coastguard Worker     for (i = rowsperchunk; i > 0; i--) {
528*dfc6aa5cSAndroid Build Coastguard Worker       result[currow++] = workspace;
529*dfc6aa5cSAndroid Build Coastguard Worker       workspace += blocksperrow;
530*dfc6aa5cSAndroid Build Coastguard Worker     }
531*dfc6aa5cSAndroid Build Coastguard Worker   }
532*dfc6aa5cSAndroid Build Coastguard Worker 
533*dfc6aa5cSAndroid Build Coastguard Worker   return result;
534*dfc6aa5cSAndroid Build Coastguard Worker }
535*dfc6aa5cSAndroid Build Coastguard Worker 
536*dfc6aa5cSAndroid Build Coastguard Worker 
537*dfc6aa5cSAndroid Build Coastguard Worker /*
538*dfc6aa5cSAndroid Build Coastguard Worker  * About virtual array management:
539*dfc6aa5cSAndroid Build Coastguard Worker  *
540*dfc6aa5cSAndroid Build Coastguard Worker  * The above "normal" array routines are only used to allocate strip buffers
541*dfc6aa5cSAndroid Build Coastguard Worker  * (as wide as the image, but just a few rows high).  Full-image-sized buffers
542*dfc6aa5cSAndroid Build Coastguard Worker  * are handled as "virtual" arrays.  The array is still accessed a strip at a
543*dfc6aa5cSAndroid Build Coastguard Worker  * time, but the memory manager must save the whole array for repeated
544*dfc6aa5cSAndroid Build Coastguard Worker  * accesses.  The intended implementation is that there is a strip buffer in
545*dfc6aa5cSAndroid Build Coastguard Worker  * memory (as high as is possible given the desired memory limit), plus a
546*dfc6aa5cSAndroid Build Coastguard Worker  * backing file that holds the rest of the array.
547*dfc6aa5cSAndroid Build Coastguard Worker  *
548*dfc6aa5cSAndroid Build Coastguard Worker  * The request_virt_array routines are told the total size of the image and
549*dfc6aa5cSAndroid Build Coastguard Worker  * the maximum number of rows that will be accessed at once.  The in-memory
550*dfc6aa5cSAndroid Build Coastguard Worker  * buffer must be at least as large as the maxaccess value.
551*dfc6aa5cSAndroid Build Coastguard Worker  *
552*dfc6aa5cSAndroid Build Coastguard Worker  * The request routines create control blocks but not the in-memory buffers.
553*dfc6aa5cSAndroid Build Coastguard Worker  * That is postponed until realize_virt_arrays is called.  At that time the
554*dfc6aa5cSAndroid Build Coastguard Worker  * total amount of space needed is known (approximately, anyway), so free
555*dfc6aa5cSAndroid Build Coastguard Worker  * memory can be divided up fairly.
556*dfc6aa5cSAndroid Build Coastguard Worker  *
557*dfc6aa5cSAndroid Build Coastguard Worker  * The access_virt_array routines are responsible for making a specific strip
558*dfc6aa5cSAndroid Build Coastguard Worker  * area accessible (after reading or writing the backing file, if necessary).
559*dfc6aa5cSAndroid Build Coastguard Worker  * Note that the access routines are told whether the caller intends to modify
560*dfc6aa5cSAndroid Build Coastguard Worker  * the accessed strip; during a read-only pass this saves having to rewrite
561*dfc6aa5cSAndroid Build Coastguard Worker  * data to disk.  The access routines are also responsible for pre-zeroing
562*dfc6aa5cSAndroid Build Coastguard Worker  * any newly accessed rows, if pre-zeroing was requested.
563*dfc6aa5cSAndroid Build Coastguard Worker  *
564*dfc6aa5cSAndroid Build Coastguard Worker  * In current usage, the access requests are usually for nonoverlapping
565*dfc6aa5cSAndroid Build Coastguard Worker  * strips; that is, successive access start_row numbers differ by exactly
566*dfc6aa5cSAndroid Build Coastguard Worker  * num_rows = maxaccess.  This means we can get good performance with simple
567*dfc6aa5cSAndroid Build Coastguard Worker  * buffer dump/reload logic, by making the in-memory buffer be a multiple
568*dfc6aa5cSAndroid Build Coastguard Worker  * of the access height; then there will never be accesses across bufferload
569*dfc6aa5cSAndroid Build Coastguard Worker  * boundaries.  The code will still work with overlapping access requests,
570*dfc6aa5cSAndroid Build Coastguard Worker  * but it doesn't handle bufferload overlaps very efficiently.
571*dfc6aa5cSAndroid Build Coastguard Worker  */
572*dfc6aa5cSAndroid Build Coastguard Worker 
573*dfc6aa5cSAndroid Build Coastguard Worker 
574*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(jvirt_sarray_ptr)
request_virt_sarray(j_common_ptr cinfo,int pool_id,boolean pre_zero,JDIMENSION samplesperrow,JDIMENSION numrows,JDIMENSION maxaccess)575*dfc6aa5cSAndroid Build Coastguard Worker request_virt_sarray(j_common_ptr cinfo, int pool_id, boolean pre_zero,
576*dfc6aa5cSAndroid Build Coastguard Worker                     JDIMENSION samplesperrow, JDIMENSION numrows,
577*dfc6aa5cSAndroid Build Coastguard Worker                     JDIMENSION maxaccess)
578*dfc6aa5cSAndroid Build Coastguard Worker /* Request a virtual 2-D sample array */
579*dfc6aa5cSAndroid Build Coastguard Worker {
580*dfc6aa5cSAndroid Build Coastguard Worker   my_mem_ptr mem = (my_mem_ptr)cinfo->mem;
581*dfc6aa5cSAndroid Build Coastguard Worker   jvirt_sarray_ptr result;
582*dfc6aa5cSAndroid Build Coastguard Worker 
583*dfc6aa5cSAndroid Build Coastguard Worker   /* Only IMAGE-lifetime virtual arrays are currently supported */
584*dfc6aa5cSAndroid Build Coastguard Worker   if (pool_id != JPOOL_IMAGE)
585*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
586*dfc6aa5cSAndroid Build Coastguard Worker 
587*dfc6aa5cSAndroid Build Coastguard Worker   /* get control block */
588*dfc6aa5cSAndroid Build Coastguard Worker   result = (jvirt_sarray_ptr)alloc_small(cinfo, pool_id,
589*dfc6aa5cSAndroid Build Coastguard Worker                                          sizeof(struct jvirt_sarray_control));
590*dfc6aa5cSAndroid Build Coastguard Worker 
591*dfc6aa5cSAndroid Build Coastguard Worker   result->mem_buffer = NULL;    /* marks array not yet realized */
592*dfc6aa5cSAndroid Build Coastguard Worker   result->rows_in_array = numrows;
593*dfc6aa5cSAndroid Build Coastguard Worker   result->samplesperrow = samplesperrow;
594*dfc6aa5cSAndroid Build Coastguard Worker   result->maxaccess = maxaccess;
595*dfc6aa5cSAndroid Build Coastguard Worker   result->pre_zero = pre_zero;
596*dfc6aa5cSAndroid Build Coastguard Worker   result->b_s_open = FALSE;     /* no associated backing-store object */
597*dfc6aa5cSAndroid Build Coastguard Worker   result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
598*dfc6aa5cSAndroid Build Coastguard Worker   mem->virt_sarray_list = result;
599*dfc6aa5cSAndroid Build Coastguard Worker 
600*dfc6aa5cSAndroid Build Coastguard Worker   return result;
601*dfc6aa5cSAndroid Build Coastguard Worker }
602*dfc6aa5cSAndroid Build Coastguard Worker 
603*dfc6aa5cSAndroid Build Coastguard Worker 
604*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(jvirt_barray_ptr)
request_virt_barray(j_common_ptr cinfo,int pool_id,boolean pre_zero,JDIMENSION blocksperrow,JDIMENSION numrows,JDIMENSION maxaccess)605*dfc6aa5cSAndroid Build Coastguard Worker request_virt_barray(j_common_ptr cinfo, int pool_id, boolean pre_zero,
606*dfc6aa5cSAndroid Build Coastguard Worker                     JDIMENSION blocksperrow, JDIMENSION numrows,
607*dfc6aa5cSAndroid Build Coastguard Worker                     JDIMENSION maxaccess)
608*dfc6aa5cSAndroid Build Coastguard Worker /* Request a virtual 2-D coefficient-block array */
609*dfc6aa5cSAndroid Build Coastguard Worker {
610*dfc6aa5cSAndroid Build Coastguard Worker   my_mem_ptr mem = (my_mem_ptr)cinfo->mem;
611*dfc6aa5cSAndroid Build Coastguard Worker   jvirt_barray_ptr result;
612*dfc6aa5cSAndroid Build Coastguard Worker 
613*dfc6aa5cSAndroid Build Coastguard Worker   /* Only IMAGE-lifetime virtual arrays are currently supported */
614*dfc6aa5cSAndroid Build Coastguard Worker   if (pool_id != JPOOL_IMAGE)
615*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
616*dfc6aa5cSAndroid Build Coastguard Worker 
617*dfc6aa5cSAndroid Build Coastguard Worker   /* get control block */
618*dfc6aa5cSAndroid Build Coastguard Worker   result = (jvirt_barray_ptr)alloc_small(cinfo, pool_id,
619*dfc6aa5cSAndroid Build Coastguard Worker                                          sizeof(struct jvirt_barray_control));
620*dfc6aa5cSAndroid Build Coastguard Worker 
621*dfc6aa5cSAndroid Build Coastguard Worker   result->mem_buffer = NULL;    /* marks array not yet realized */
622*dfc6aa5cSAndroid Build Coastguard Worker   result->rows_in_array = numrows;
623*dfc6aa5cSAndroid Build Coastguard Worker   result->blocksperrow = blocksperrow;
624*dfc6aa5cSAndroid Build Coastguard Worker   result->maxaccess = maxaccess;
625*dfc6aa5cSAndroid Build Coastguard Worker   result->pre_zero = pre_zero;
626*dfc6aa5cSAndroid Build Coastguard Worker   result->b_s_open = FALSE;     /* no associated backing-store object */
627*dfc6aa5cSAndroid Build Coastguard Worker   result->next = mem->virt_barray_list; /* add to list of virtual arrays */
628*dfc6aa5cSAndroid Build Coastguard Worker   mem->virt_barray_list = result;
629*dfc6aa5cSAndroid Build Coastguard Worker 
630*dfc6aa5cSAndroid Build Coastguard Worker   return result;
631*dfc6aa5cSAndroid Build Coastguard Worker }
632*dfc6aa5cSAndroid Build Coastguard Worker 
633*dfc6aa5cSAndroid Build Coastguard Worker 
634*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
realize_virt_arrays(j_common_ptr cinfo)635*dfc6aa5cSAndroid Build Coastguard Worker realize_virt_arrays(j_common_ptr cinfo)
636*dfc6aa5cSAndroid Build Coastguard Worker /* Allocate the in-memory buffers for any unrealized virtual arrays */
637*dfc6aa5cSAndroid Build Coastguard Worker {
638*dfc6aa5cSAndroid Build Coastguard Worker   my_mem_ptr mem = (my_mem_ptr)cinfo->mem;
639*dfc6aa5cSAndroid Build Coastguard Worker   size_t space_per_minheight, maximum_space, avail_mem;
640*dfc6aa5cSAndroid Build Coastguard Worker   size_t minheights, max_minheights;
641*dfc6aa5cSAndroid Build Coastguard Worker   jvirt_sarray_ptr sptr;
642*dfc6aa5cSAndroid Build Coastguard Worker   jvirt_barray_ptr bptr;
643*dfc6aa5cSAndroid Build Coastguard Worker 
644*dfc6aa5cSAndroid Build Coastguard Worker   /* Compute the minimum space needed (maxaccess rows in each buffer)
645*dfc6aa5cSAndroid Build Coastguard Worker    * and the maximum space needed (full image height in each buffer).
646*dfc6aa5cSAndroid Build Coastguard Worker    * These may be of use to the system-dependent jpeg_mem_available routine.
647*dfc6aa5cSAndroid Build Coastguard Worker    */
648*dfc6aa5cSAndroid Build Coastguard Worker   space_per_minheight = 0;
649*dfc6aa5cSAndroid Build Coastguard Worker   maximum_space = 0;
650*dfc6aa5cSAndroid Build Coastguard Worker   for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
651*dfc6aa5cSAndroid Build Coastguard Worker     if (sptr->mem_buffer == NULL) { /* if not realized yet */
652*dfc6aa5cSAndroid Build Coastguard Worker       size_t new_space = (long)sptr->rows_in_array *
653*dfc6aa5cSAndroid Build Coastguard Worker                          (long)sptr->samplesperrow * sizeof(JSAMPLE);
654*dfc6aa5cSAndroid Build Coastguard Worker 
655*dfc6aa5cSAndroid Build Coastguard Worker       space_per_minheight += (long)sptr->maxaccess *
656*dfc6aa5cSAndroid Build Coastguard Worker                              (long)sptr->samplesperrow * sizeof(JSAMPLE);
657*dfc6aa5cSAndroid Build Coastguard Worker       if (SIZE_MAX - maximum_space < new_space)
658*dfc6aa5cSAndroid Build Coastguard Worker         out_of_memory(cinfo, 10);
659*dfc6aa5cSAndroid Build Coastguard Worker       maximum_space += new_space;
660*dfc6aa5cSAndroid Build Coastguard Worker     }
661*dfc6aa5cSAndroid Build Coastguard Worker   }
662*dfc6aa5cSAndroid Build Coastguard Worker   for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
663*dfc6aa5cSAndroid Build Coastguard Worker     if (bptr->mem_buffer == NULL) { /* if not realized yet */
664*dfc6aa5cSAndroid Build Coastguard Worker       size_t new_space = (long)bptr->rows_in_array *
665*dfc6aa5cSAndroid Build Coastguard Worker                          (long)bptr->blocksperrow * sizeof(JBLOCK);
666*dfc6aa5cSAndroid Build Coastguard Worker 
667*dfc6aa5cSAndroid Build Coastguard Worker       space_per_minheight += (long)bptr->maxaccess *
668*dfc6aa5cSAndroid Build Coastguard Worker                              (long)bptr->blocksperrow * sizeof(JBLOCK);
669*dfc6aa5cSAndroid Build Coastguard Worker       if (SIZE_MAX - maximum_space < new_space)
670*dfc6aa5cSAndroid Build Coastguard Worker         out_of_memory(cinfo, 11);
671*dfc6aa5cSAndroid Build Coastguard Worker       maximum_space += new_space;
672*dfc6aa5cSAndroid Build Coastguard Worker     }
673*dfc6aa5cSAndroid Build Coastguard Worker   }
674*dfc6aa5cSAndroid Build Coastguard Worker 
675*dfc6aa5cSAndroid Build Coastguard Worker   if (space_per_minheight <= 0)
676*dfc6aa5cSAndroid Build Coastguard Worker     return;                     /* no unrealized arrays, no work */
677*dfc6aa5cSAndroid Build Coastguard Worker 
678*dfc6aa5cSAndroid Build Coastguard Worker   /* Determine amount of memory to actually use; this is system-dependent. */
679*dfc6aa5cSAndroid Build Coastguard Worker   avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
680*dfc6aa5cSAndroid Build Coastguard Worker                                  mem->total_space_allocated);
681*dfc6aa5cSAndroid Build Coastguard Worker 
682*dfc6aa5cSAndroid Build Coastguard Worker   /* If the maximum space needed is available, make all the buffers full
683*dfc6aa5cSAndroid Build Coastguard Worker    * height; otherwise parcel it out with the same number of minheights
684*dfc6aa5cSAndroid Build Coastguard Worker    * in each buffer.
685*dfc6aa5cSAndroid Build Coastguard Worker    */
686*dfc6aa5cSAndroid Build Coastguard Worker   if (avail_mem >= maximum_space)
687*dfc6aa5cSAndroid Build Coastguard Worker     max_minheights = 1000000000L;
688*dfc6aa5cSAndroid Build Coastguard Worker   else {
689*dfc6aa5cSAndroid Build Coastguard Worker     max_minheights = avail_mem / space_per_minheight;
690*dfc6aa5cSAndroid Build Coastguard Worker     /* If there doesn't seem to be enough space, try to get the minimum
691*dfc6aa5cSAndroid Build Coastguard Worker      * anyway.  This allows a "stub" implementation of jpeg_mem_available().
692*dfc6aa5cSAndroid Build Coastguard Worker      */
693*dfc6aa5cSAndroid Build Coastguard Worker     if (max_minheights <= 0)
694*dfc6aa5cSAndroid Build Coastguard Worker       max_minheights = 1;
695*dfc6aa5cSAndroid Build Coastguard Worker   }
696*dfc6aa5cSAndroid Build Coastguard Worker 
697*dfc6aa5cSAndroid Build Coastguard Worker   /* Allocate the in-memory buffers and initialize backing store as needed. */
698*dfc6aa5cSAndroid Build Coastguard Worker 
699*dfc6aa5cSAndroid Build Coastguard Worker   for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
700*dfc6aa5cSAndroid Build Coastguard Worker     if (sptr->mem_buffer == NULL) { /* if not realized yet */
701*dfc6aa5cSAndroid Build Coastguard Worker       minheights = ((long)sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
702*dfc6aa5cSAndroid Build Coastguard Worker       if (minheights <= max_minheights) {
703*dfc6aa5cSAndroid Build Coastguard Worker         /* This buffer fits in memory */
704*dfc6aa5cSAndroid Build Coastguard Worker         sptr->rows_in_mem = sptr->rows_in_array;
705*dfc6aa5cSAndroid Build Coastguard Worker       } else {
706*dfc6aa5cSAndroid Build Coastguard Worker         /* It doesn't fit in memory, create backing store. */
707*dfc6aa5cSAndroid Build Coastguard Worker         sptr->rows_in_mem = (JDIMENSION)(max_minheights * sptr->maxaccess);
708*dfc6aa5cSAndroid Build Coastguard Worker         jpeg_open_backing_store(cinfo, &sptr->b_s_info,
709*dfc6aa5cSAndroid Build Coastguard Worker                                 (long)sptr->rows_in_array *
710*dfc6aa5cSAndroid Build Coastguard Worker                                 (long)sptr->samplesperrow *
711*dfc6aa5cSAndroid Build Coastguard Worker                                 (long)sizeof(JSAMPLE));
712*dfc6aa5cSAndroid Build Coastguard Worker         sptr->b_s_open = TRUE;
713*dfc6aa5cSAndroid Build Coastguard Worker       }
714*dfc6aa5cSAndroid Build Coastguard Worker       sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
715*dfc6aa5cSAndroid Build Coastguard Worker                                       sptr->samplesperrow, sptr->rows_in_mem);
716*dfc6aa5cSAndroid Build Coastguard Worker       sptr->rowsperchunk = mem->last_rowsperchunk;
717*dfc6aa5cSAndroid Build Coastguard Worker       sptr->cur_start_row = 0;
718*dfc6aa5cSAndroid Build Coastguard Worker       sptr->first_undef_row = 0;
719*dfc6aa5cSAndroid Build Coastguard Worker       sptr->dirty = FALSE;
720*dfc6aa5cSAndroid Build Coastguard Worker     }
721*dfc6aa5cSAndroid Build Coastguard Worker   }
722*dfc6aa5cSAndroid Build Coastguard Worker 
723*dfc6aa5cSAndroid Build Coastguard Worker   for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
724*dfc6aa5cSAndroid Build Coastguard Worker     if (bptr->mem_buffer == NULL) { /* if not realized yet */
725*dfc6aa5cSAndroid Build Coastguard Worker       minheights = ((long)bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
726*dfc6aa5cSAndroid Build Coastguard Worker       if (minheights <= max_minheights) {
727*dfc6aa5cSAndroid Build Coastguard Worker         /* This buffer fits in memory */
728*dfc6aa5cSAndroid Build Coastguard Worker         bptr->rows_in_mem = bptr->rows_in_array;
729*dfc6aa5cSAndroid Build Coastguard Worker       } else {
730*dfc6aa5cSAndroid Build Coastguard Worker         /* It doesn't fit in memory, create backing store. */
731*dfc6aa5cSAndroid Build Coastguard Worker         bptr->rows_in_mem = (JDIMENSION)(max_minheights * bptr->maxaccess);
732*dfc6aa5cSAndroid Build Coastguard Worker         jpeg_open_backing_store(cinfo, &bptr->b_s_info,
733*dfc6aa5cSAndroid Build Coastguard Worker                                 (long)bptr->rows_in_array *
734*dfc6aa5cSAndroid Build Coastguard Worker                                 (long)bptr->blocksperrow *
735*dfc6aa5cSAndroid Build Coastguard Worker                                 (long)sizeof(JBLOCK));
736*dfc6aa5cSAndroid Build Coastguard Worker         bptr->b_s_open = TRUE;
737*dfc6aa5cSAndroid Build Coastguard Worker       }
738*dfc6aa5cSAndroid Build Coastguard Worker       bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
739*dfc6aa5cSAndroid Build Coastguard Worker                                       bptr->blocksperrow, bptr->rows_in_mem);
740*dfc6aa5cSAndroid Build Coastguard Worker       bptr->rowsperchunk = mem->last_rowsperchunk;
741*dfc6aa5cSAndroid Build Coastguard Worker       bptr->cur_start_row = 0;
742*dfc6aa5cSAndroid Build Coastguard Worker       bptr->first_undef_row = 0;
743*dfc6aa5cSAndroid Build Coastguard Worker       bptr->dirty = FALSE;
744*dfc6aa5cSAndroid Build Coastguard Worker     }
745*dfc6aa5cSAndroid Build Coastguard Worker   }
746*dfc6aa5cSAndroid Build Coastguard Worker }
747*dfc6aa5cSAndroid Build Coastguard Worker 
748*dfc6aa5cSAndroid Build Coastguard Worker 
749*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(void)
do_sarray_io(j_common_ptr cinfo,jvirt_sarray_ptr ptr,boolean writing)750*dfc6aa5cSAndroid Build Coastguard Worker do_sarray_io(j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
751*dfc6aa5cSAndroid Build Coastguard Worker /* Do backing store read or write of a virtual sample array */
752*dfc6aa5cSAndroid Build Coastguard Worker {
753*dfc6aa5cSAndroid Build Coastguard Worker   long bytesperrow, file_offset, byte_count, rows, thisrow, i;
754*dfc6aa5cSAndroid Build Coastguard Worker 
755*dfc6aa5cSAndroid Build Coastguard Worker   bytesperrow = (long)ptr->samplesperrow * sizeof(JSAMPLE);
756*dfc6aa5cSAndroid Build Coastguard Worker   file_offset = ptr->cur_start_row * bytesperrow;
757*dfc6aa5cSAndroid Build Coastguard Worker   /* Loop to read or write each allocation chunk in mem_buffer */
758*dfc6aa5cSAndroid Build Coastguard Worker   for (i = 0; i < (long)ptr->rows_in_mem; i += ptr->rowsperchunk) {
759*dfc6aa5cSAndroid Build Coastguard Worker     /* One chunk, but check for short chunk at end of buffer */
760*dfc6aa5cSAndroid Build Coastguard Worker     rows = MIN((long)ptr->rowsperchunk, (long)ptr->rows_in_mem - i);
761*dfc6aa5cSAndroid Build Coastguard Worker     /* Transfer no more than is currently defined */
762*dfc6aa5cSAndroid Build Coastguard Worker     thisrow = (long)ptr->cur_start_row + i;
763*dfc6aa5cSAndroid Build Coastguard Worker     rows = MIN(rows, (long)ptr->first_undef_row - thisrow);
764*dfc6aa5cSAndroid Build Coastguard Worker     /* Transfer no more than fits in file */
765*dfc6aa5cSAndroid Build Coastguard Worker     rows = MIN(rows, (long)ptr->rows_in_array - thisrow);
766*dfc6aa5cSAndroid Build Coastguard Worker     if (rows <= 0)              /* this chunk might be past end of file! */
767*dfc6aa5cSAndroid Build Coastguard Worker       break;
768*dfc6aa5cSAndroid Build Coastguard Worker     byte_count = rows * bytesperrow;
769*dfc6aa5cSAndroid Build Coastguard Worker     if (writing)
770*dfc6aa5cSAndroid Build Coastguard Worker       (*ptr->b_s_info.write_backing_store) (cinfo, &ptr->b_s_info,
771*dfc6aa5cSAndroid Build Coastguard Worker                                             (void *)ptr->mem_buffer[i],
772*dfc6aa5cSAndroid Build Coastguard Worker                                             file_offset, byte_count);
773*dfc6aa5cSAndroid Build Coastguard Worker     else
774*dfc6aa5cSAndroid Build Coastguard Worker       (*ptr->b_s_info.read_backing_store) (cinfo, &ptr->b_s_info,
775*dfc6aa5cSAndroid Build Coastguard Worker                                            (void *)ptr->mem_buffer[i],
776*dfc6aa5cSAndroid Build Coastguard Worker                                            file_offset, byte_count);
777*dfc6aa5cSAndroid Build Coastguard Worker     file_offset += byte_count;
778*dfc6aa5cSAndroid Build Coastguard Worker   }
779*dfc6aa5cSAndroid Build Coastguard Worker }
780*dfc6aa5cSAndroid Build Coastguard Worker 
781*dfc6aa5cSAndroid Build Coastguard Worker 
782*dfc6aa5cSAndroid Build Coastguard Worker LOCAL(void)
do_barray_io(j_common_ptr cinfo,jvirt_barray_ptr ptr,boolean writing)783*dfc6aa5cSAndroid Build Coastguard Worker do_barray_io(j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
784*dfc6aa5cSAndroid Build Coastguard Worker /* Do backing store read or write of a virtual coefficient-block array */
785*dfc6aa5cSAndroid Build Coastguard Worker {
786*dfc6aa5cSAndroid Build Coastguard Worker   long bytesperrow, file_offset, byte_count, rows, thisrow, i;
787*dfc6aa5cSAndroid Build Coastguard Worker 
788*dfc6aa5cSAndroid Build Coastguard Worker   bytesperrow = (long)ptr->blocksperrow * sizeof(JBLOCK);
789*dfc6aa5cSAndroid Build Coastguard Worker   file_offset = ptr->cur_start_row * bytesperrow;
790*dfc6aa5cSAndroid Build Coastguard Worker   /* Loop to read or write each allocation chunk in mem_buffer */
791*dfc6aa5cSAndroid Build Coastguard Worker   for (i = 0; i < (long)ptr->rows_in_mem; i += ptr->rowsperchunk) {
792*dfc6aa5cSAndroid Build Coastguard Worker     /* One chunk, but check for short chunk at end of buffer */
793*dfc6aa5cSAndroid Build Coastguard Worker     rows = MIN((long)ptr->rowsperchunk, (long)ptr->rows_in_mem - i);
794*dfc6aa5cSAndroid Build Coastguard Worker     /* Transfer no more than is currently defined */
795*dfc6aa5cSAndroid Build Coastguard Worker     thisrow = (long)ptr->cur_start_row + i;
796*dfc6aa5cSAndroid Build Coastguard Worker     rows = MIN(rows, (long)ptr->first_undef_row - thisrow);
797*dfc6aa5cSAndroid Build Coastguard Worker     /* Transfer no more than fits in file */
798*dfc6aa5cSAndroid Build Coastguard Worker     rows = MIN(rows, (long)ptr->rows_in_array - thisrow);
799*dfc6aa5cSAndroid Build Coastguard Worker     if (rows <= 0)              /* this chunk might be past end of file! */
800*dfc6aa5cSAndroid Build Coastguard Worker       break;
801*dfc6aa5cSAndroid Build Coastguard Worker     byte_count = rows * bytesperrow;
802*dfc6aa5cSAndroid Build Coastguard Worker     if (writing)
803*dfc6aa5cSAndroid Build Coastguard Worker       (*ptr->b_s_info.write_backing_store) (cinfo, &ptr->b_s_info,
804*dfc6aa5cSAndroid Build Coastguard Worker                                             (void *)ptr->mem_buffer[i],
805*dfc6aa5cSAndroid Build Coastguard Worker                                             file_offset, byte_count);
806*dfc6aa5cSAndroid Build Coastguard Worker     else
807*dfc6aa5cSAndroid Build Coastguard Worker       (*ptr->b_s_info.read_backing_store) (cinfo, &ptr->b_s_info,
808*dfc6aa5cSAndroid Build Coastguard Worker                                            (void *)ptr->mem_buffer[i],
809*dfc6aa5cSAndroid Build Coastguard Worker                                            file_offset, byte_count);
810*dfc6aa5cSAndroid Build Coastguard Worker     file_offset += byte_count;
811*dfc6aa5cSAndroid Build Coastguard Worker   }
812*dfc6aa5cSAndroid Build Coastguard Worker }
813*dfc6aa5cSAndroid Build Coastguard Worker 
814*dfc6aa5cSAndroid Build Coastguard Worker 
815*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(JSAMPARRAY)
access_virt_sarray(j_common_ptr cinfo,jvirt_sarray_ptr ptr,JDIMENSION start_row,JDIMENSION num_rows,boolean writable)816*dfc6aa5cSAndroid Build Coastguard Worker access_virt_sarray(j_common_ptr cinfo, jvirt_sarray_ptr ptr,
817*dfc6aa5cSAndroid Build Coastguard Worker                    JDIMENSION start_row, JDIMENSION num_rows, boolean writable)
818*dfc6aa5cSAndroid Build Coastguard Worker /* Access the part of a virtual sample array starting at start_row */
819*dfc6aa5cSAndroid Build Coastguard Worker /* and extending for num_rows rows.  writable is true if  */
820*dfc6aa5cSAndroid Build Coastguard Worker /* caller intends to modify the accessed area. */
821*dfc6aa5cSAndroid Build Coastguard Worker {
822*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION end_row = start_row + num_rows;
823*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION undef_row;
824*dfc6aa5cSAndroid Build Coastguard Worker 
825*dfc6aa5cSAndroid Build Coastguard Worker   /* debugging check */
826*dfc6aa5cSAndroid Build Coastguard Worker   if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
827*dfc6aa5cSAndroid Build Coastguard Worker       ptr->mem_buffer == NULL)
828*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
829*dfc6aa5cSAndroid Build Coastguard Worker 
830*dfc6aa5cSAndroid Build Coastguard Worker   /* Make the desired part of the virtual array accessible */
831*dfc6aa5cSAndroid Build Coastguard Worker   if (start_row < ptr->cur_start_row ||
832*dfc6aa5cSAndroid Build Coastguard Worker       end_row > ptr->cur_start_row + ptr->rows_in_mem) {
833*dfc6aa5cSAndroid Build Coastguard Worker     if (!ptr->b_s_open)
834*dfc6aa5cSAndroid Build Coastguard Worker       ERREXIT(cinfo, JERR_VIRTUAL_BUG);
835*dfc6aa5cSAndroid Build Coastguard Worker     /* Flush old buffer contents if necessary */
836*dfc6aa5cSAndroid Build Coastguard Worker     if (ptr->dirty) {
837*dfc6aa5cSAndroid Build Coastguard Worker       do_sarray_io(cinfo, ptr, TRUE);
838*dfc6aa5cSAndroid Build Coastguard Worker       ptr->dirty = FALSE;
839*dfc6aa5cSAndroid Build Coastguard Worker     }
840*dfc6aa5cSAndroid Build Coastguard Worker     /* Decide what part of virtual array to access.
841*dfc6aa5cSAndroid Build Coastguard Worker      * Algorithm: if target address > current window, assume forward scan,
842*dfc6aa5cSAndroid Build Coastguard Worker      * load starting at target address.  If target address < current window,
843*dfc6aa5cSAndroid Build Coastguard Worker      * assume backward scan, load so that target area is top of window.
844*dfc6aa5cSAndroid Build Coastguard Worker      * Note that when switching from forward write to forward read, will have
845*dfc6aa5cSAndroid Build Coastguard Worker      * start_row = 0, so the limiting case applies and we load from 0 anyway.
846*dfc6aa5cSAndroid Build Coastguard Worker      */
847*dfc6aa5cSAndroid Build Coastguard Worker     if (start_row > ptr->cur_start_row) {
848*dfc6aa5cSAndroid Build Coastguard Worker       ptr->cur_start_row = start_row;
849*dfc6aa5cSAndroid Build Coastguard Worker     } else {
850*dfc6aa5cSAndroid Build Coastguard Worker       /* use long arithmetic here to avoid overflow & unsigned problems */
851*dfc6aa5cSAndroid Build Coastguard Worker       long ltemp;
852*dfc6aa5cSAndroid Build Coastguard Worker 
853*dfc6aa5cSAndroid Build Coastguard Worker       ltemp = (long)end_row - (long)ptr->rows_in_mem;
854*dfc6aa5cSAndroid Build Coastguard Worker       if (ltemp < 0)
855*dfc6aa5cSAndroid Build Coastguard Worker         ltemp = 0;              /* don't fall off front end of file */
856*dfc6aa5cSAndroid Build Coastguard Worker       ptr->cur_start_row = (JDIMENSION)ltemp;
857*dfc6aa5cSAndroid Build Coastguard Worker     }
858*dfc6aa5cSAndroid Build Coastguard Worker     /* Read in the selected part of the array.
859*dfc6aa5cSAndroid Build Coastguard Worker      * During the initial write pass, we will do no actual read
860*dfc6aa5cSAndroid Build Coastguard Worker      * because the selected part is all undefined.
861*dfc6aa5cSAndroid Build Coastguard Worker      */
862*dfc6aa5cSAndroid Build Coastguard Worker     do_sarray_io(cinfo, ptr, FALSE);
863*dfc6aa5cSAndroid Build Coastguard Worker   }
864*dfc6aa5cSAndroid Build Coastguard Worker   /* Ensure the accessed part of the array is defined; prezero if needed.
865*dfc6aa5cSAndroid Build Coastguard Worker    * To improve locality of access, we only prezero the part of the array
866*dfc6aa5cSAndroid Build Coastguard Worker    * that the caller is about to access, not the entire in-memory array.
867*dfc6aa5cSAndroid Build Coastguard Worker    */
868*dfc6aa5cSAndroid Build Coastguard Worker   if (ptr->first_undef_row < end_row) {
869*dfc6aa5cSAndroid Build Coastguard Worker     if (ptr->first_undef_row < start_row) {
870*dfc6aa5cSAndroid Build Coastguard Worker       if (writable)             /* writer skipped over a section of array */
871*dfc6aa5cSAndroid Build Coastguard Worker         ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
872*dfc6aa5cSAndroid Build Coastguard Worker       undef_row = start_row;    /* but reader is allowed to read ahead */
873*dfc6aa5cSAndroid Build Coastguard Worker     } else {
874*dfc6aa5cSAndroid Build Coastguard Worker       undef_row = ptr->first_undef_row;
875*dfc6aa5cSAndroid Build Coastguard Worker     }
876*dfc6aa5cSAndroid Build Coastguard Worker     if (writable)
877*dfc6aa5cSAndroid Build Coastguard Worker       ptr->first_undef_row = end_row;
878*dfc6aa5cSAndroid Build Coastguard Worker     if (ptr->pre_zero) {
879*dfc6aa5cSAndroid Build Coastguard Worker       size_t bytesperrow = (size_t)ptr->samplesperrow * sizeof(JSAMPLE);
880*dfc6aa5cSAndroid Build Coastguard Worker       undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
881*dfc6aa5cSAndroid Build Coastguard Worker       end_row -= ptr->cur_start_row;
882*dfc6aa5cSAndroid Build Coastguard Worker       while (undef_row < end_row) {
883*dfc6aa5cSAndroid Build Coastguard Worker         jzero_far((void *)ptr->mem_buffer[undef_row], bytesperrow);
884*dfc6aa5cSAndroid Build Coastguard Worker         undef_row++;
885*dfc6aa5cSAndroid Build Coastguard Worker       }
886*dfc6aa5cSAndroid Build Coastguard Worker     } else {
887*dfc6aa5cSAndroid Build Coastguard Worker       if (!writable)            /* reader looking at undefined data */
888*dfc6aa5cSAndroid Build Coastguard Worker         ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
889*dfc6aa5cSAndroid Build Coastguard Worker     }
890*dfc6aa5cSAndroid Build Coastguard Worker   }
891*dfc6aa5cSAndroid Build Coastguard Worker   /* Flag the buffer dirty if caller will write in it */
892*dfc6aa5cSAndroid Build Coastguard Worker   if (writable)
893*dfc6aa5cSAndroid Build Coastguard Worker     ptr->dirty = TRUE;
894*dfc6aa5cSAndroid Build Coastguard Worker   /* Return address of proper part of the buffer */
895*dfc6aa5cSAndroid Build Coastguard Worker   return ptr->mem_buffer + (start_row - ptr->cur_start_row);
896*dfc6aa5cSAndroid Build Coastguard Worker }
897*dfc6aa5cSAndroid Build Coastguard Worker 
898*dfc6aa5cSAndroid Build Coastguard Worker 
899*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(JBLOCKARRAY)
access_virt_barray(j_common_ptr cinfo,jvirt_barray_ptr ptr,JDIMENSION start_row,JDIMENSION num_rows,boolean writable)900*dfc6aa5cSAndroid Build Coastguard Worker access_virt_barray(j_common_ptr cinfo, jvirt_barray_ptr ptr,
901*dfc6aa5cSAndroid Build Coastguard Worker                    JDIMENSION start_row, JDIMENSION num_rows, boolean writable)
902*dfc6aa5cSAndroid Build Coastguard Worker /* Access the part of a virtual block array starting at start_row */
903*dfc6aa5cSAndroid Build Coastguard Worker /* and extending for num_rows rows.  writable is true if  */
904*dfc6aa5cSAndroid Build Coastguard Worker /* caller intends to modify the accessed area. */
905*dfc6aa5cSAndroid Build Coastguard Worker {
906*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION end_row = start_row + num_rows;
907*dfc6aa5cSAndroid Build Coastguard Worker   JDIMENSION undef_row;
908*dfc6aa5cSAndroid Build Coastguard Worker 
909*dfc6aa5cSAndroid Build Coastguard Worker   /* debugging check */
910*dfc6aa5cSAndroid Build Coastguard Worker   if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
911*dfc6aa5cSAndroid Build Coastguard Worker       ptr->mem_buffer == NULL)
912*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
913*dfc6aa5cSAndroid Build Coastguard Worker 
914*dfc6aa5cSAndroid Build Coastguard Worker   /* Make the desired part of the virtual array accessible */
915*dfc6aa5cSAndroid Build Coastguard Worker   if (start_row < ptr->cur_start_row ||
916*dfc6aa5cSAndroid Build Coastguard Worker       end_row > ptr->cur_start_row + ptr->rows_in_mem) {
917*dfc6aa5cSAndroid Build Coastguard Worker     if (!ptr->b_s_open)
918*dfc6aa5cSAndroid Build Coastguard Worker       ERREXIT(cinfo, JERR_VIRTUAL_BUG);
919*dfc6aa5cSAndroid Build Coastguard Worker     /* Flush old buffer contents if necessary */
920*dfc6aa5cSAndroid Build Coastguard Worker     if (ptr->dirty) {
921*dfc6aa5cSAndroid Build Coastguard Worker       do_barray_io(cinfo, ptr, TRUE);
922*dfc6aa5cSAndroid Build Coastguard Worker       ptr->dirty = FALSE;
923*dfc6aa5cSAndroid Build Coastguard Worker     }
924*dfc6aa5cSAndroid Build Coastguard Worker     /* Decide what part of virtual array to access.
925*dfc6aa5cSAndroid Build Coastguard Worker      * Algorithm: if target address > current window, assume forward scan,
926*dfc6aa5cSAndroid Build Coastguard Worker      * load starting at target address.  If target address < current window,
927*dfc6aa5cSAndroid Build Coastguard Worker      * assume backward scan, load so that target area is top of window.
928*dfc6aa5cSAndroid Build Coastguard Worker      * Note that when switching from forward write to forward read, will have
929*dfc6aa5cSAndroid Build Coastguard Worker      * start_row = 0, so the limiting case applies and we load from 0 anyway.
930*dfc6aa5cSAndroid Build Coastguard Worker      */
931*dfc6aa5cSAndroid Build Coastguard Worker     if (start_row > ptr->cur_start_row) {
932*dfc6aa5cSAndroid Build Coastguard Worker       ptr->cur_start_row = start_row;
933*dfc6aa5cSAndroid Build Coastguard Worker     } else {
934*dfc6aa5cSAndroid Build Coastguard Worker       /* use long arithmetic here to avoid overflow & unsigned problems */
935*dfc6aa5cSAndroid Build Coastguard Worker       long ltemp;
936*dfc6aa5cSAndroid Build Coastguard Worker 
937*dfc6aa5cSAndroid Build Coastguard Worker       ltemp = (long)end_row - (long)ptr->rows_in_mem;
938*dfc6aa5cSAndroid Build Coastguard Worker       if (ltemp < 0)
939*dfc6aa5cSAndroid Build Coastguard Worker         ltemp = 0;              /* don't fall off front end of file */
940*dfc6aa5cSAndroid Build Coastguard Worker       ptr->cur_start_row = (JDIMENSION)ltemp;
941*dfc6aa5cSAndroid Build Coastguard Worker     }
942*dfc6aa5cSAndroid Build Coastguard Worker     /* Read in the selected part of the array.
943*dfc6aa5cSAndroid Build Coastguard Worker      * During the initial write pass, we will do no actual read
944*dfc6aa5cSAndroid Build Coastguard Worker      * because the selected part is all undefined.
945*dfc6aa5cSAndroid Build Coastguard Worker      */
946*dfc6aa5cSAndroid Build Coastguard Worker     do_barray_io(cinfo, ptr, FALSE);
947*dfc6aa5cSAndroid Build Coastguard Worker   }
948*dfc6aa5cSAndroid Build Coastguard Worker   /* Ensure the accessed part of the array is defined; prezero if needed.
949*dfc6aa5cSAndroid Build Coastguard Worker    * To improve locality of access, we only prezero the part of the array
950*dfc6aa5cSAndroid Build Coastguard Worker    * that the caller is about to access, not the entire in-memory array.
951*dfc6aa5cSAndroid Build Coastguard Worker    */
952*dfc6aa5cSAndroid Build Coastguard Worker   if (ptr->first_undef_row < end_row) {
953*dfc6aa5cSAndroid Build Coastguard Worker     if (ptr->first_undef_row < start_row) {
954*dfc6aa5cSAndroid Build Coastguard Worker       if (writable)             /* writer skipped over a section of array */
955*dfc6aa5cSAndroid Build Coastguard Worker         ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
956*dfc6aa5cSAndroid Build Coastguard Worker       undef_row = start_row;    /* but reader is allowed to read ahead */
957*dfc6aa5cSAndroid Build Coastguard Worker     } else {
958*dfc6aa5cSAndroid Build Coastguard Worker       undef_row = ptr->first_undef_row;
959*dfc6aa5cSAndroid Build Coastguard Worker     }
960*dfc6aa5cSAndroid Build Coastguard Worker     if (writable)
961*dfc6aa5cSAndroid Build Coastguard Worker       ptr->first_undef_row = end_row;
962*dfc6aa5cSAndroid Build Coastguard Worker     if (ptr->pre_zero) {
963*dfc6aa5cSAndroid Build Coastguard Worker       size_t bytesperrow = (size_t)ptr->blocksperrow * sizeof(JBLOCK);
964*dfc6aa5cSAndroid Build Coastguard Worker       undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
965*dfc6aa5cSAndroid Build Coastguard Worker       end_row -= ptr->cur_start_row;
966*dfc6aa5cSAndroid Build Coastguard Worker       while (undef_row < end_row) {
967*dfc6aa5cSAndroid Build Coastguard Worker         jzero_far((void *)ptr->mem_buffer[undef_row], bytesperrow);
968*dfc6aa5cSAndroid Build Coastguard Worker         undef_row++;
969*dfc6aa5cSAndroid Build Coastguard Worker       }
970*dfc6aa5cSAndroid Build Coastguard Worker     } else {
971*dfc6aa5cSAndroid Build Coastguard Worker       if (!writable)            /* reader looking at undefined data */
972*dfc6aa5cSAndroid Build Coastguard Worker         ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
973*dfc6aa5cSAndroid Build Coastguard Worker     }
974*dfc6aa5cSAndroid Build Coastguard Worker   }
975*dfc6aa5cSAndroid Build Coastguard Worker   /* Flag the buffer dirty if caller will write in it */
976*dfc6aa5cSAndroid Build Coastguard Worker   if (writable)
977*dfc6aa5cSAndroid Build Coastguard Worker     ptr->dirty = TRUE;
978*dfc6aa5cSAndroid Build Coastguard Worker   /* Return address of proper part of the buffer */
979*dfc6aa5cSAndroid Build Coastguard Worker   return ptr->mem_buffer + (start_row - ptr->cur_start_row);
980*dfc6aa5cSAndroid Build Coastguard Worker }
981*dfc6aa5cSAndroid Build Coastguard Worker 
982*dfc6aa5cSAndroid Build Coastguard Worker 
983*dfc6aa5cSAndroid Build Coastguard Worker /*
984*dfc6aa5cSAndroid Build Coastguard Worker  * Release all objects belonging to a specified pool.
985*dfc6aa5cSAndroid Build Coastguard Worker  */
986*dfc6aa5cSAndroid Build Coastguard Worker 
987*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
free_pool(j_common_ptr cinfo,int pool_id)988*dfc6aa5cSAndroid Build Coastguard Worker free_pool(j_common_ptr cinfo, int pool_id)
989*dfc6aa5cSAndroid Build Coastguard Worker {
990*dfc6aa5cSAndroid Build Coastguard Worker   my_mem_ptr mem = (my_mem_ptr)cinfo->mem;
991*dfc6aa5cSAndroid Build Coastguard Worker   small_pool_ptr shdr_ptr;
992*dfc6aa5cSAndroid Build Coastguard Worker   large_pool_ptr lhdr_ptr;
993*dfc6aa5cSAndroid Build Coastguard Worker   size_t space_freed;
994*dfc6aa5cSAndroid Build Coastguard Worker 
995*dfc6aa5cSAndroid Build Coastguard Worker   if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
996*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
997*dfc6aa5cSAndroid Build Coastguard Worker 
998*dfc6aa5cSAndroid Build Coastguard Worker #ifdef MEM_STATS
999*dfc6aa5cSAndroid Build Coastguard Worker   if (cinfo->err->trace_level > 1)
1000*dfc6aa5cSAndroid Build Coastguard Worker     print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
1001*dfc6aa5cSAndroid Build Coastguard Worker #endif
1002*dfc6aa5cSAndroid Build Coastguard Worker 
1003*dfc6aa5cSAndroid Build Coastguard Worker   /* If freeing IMAGE pool, close any virtual arrays first */
1004*dfc6aa5cSAndroid Build Coastguard Worker   if (pool_id == JPOOL_IMAGE) {
1005*dfc6aa5cSAndroid Build Coastguard Worker     jvirt_sarray_ptr sptr;
1006*dfc6aa5cSAndroid Build Coastguard Worker     jvirt_barray_ptr bptr;
1007*dfc6aa5cSAndroid Build Coastguard Worker 
1008*dfc6aa5cSAndroid Build Coastguard Worker     for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
1009*dfc6aa5cSAndroid Build Coastguard Worker       if (sptr->b_s_open) {     /* there may be no backing store */
1010*dfc6aa5cSAndroid Build Coastguard Worker         sptr->b_s_open = FALSE; /* prevent recursive close if error */
1011*dfc6aa5cSAndroid Build Coastguard Worker         (*sptr->b_s_info.close_backing_store) (cinfo, &sptr->b_s_info);
1012*dfc6aa5cSAndroid Build Coastguard Worker       }
1013*dfc6aa5cSAndroid Build Coastguard Worker     }
1014*dfc6aa5cSAndroid Build Coastguard Worker     mem->virt_sarray_list = NULL;
1015*dfc6aa5cSAndroid Build Coastguard Worker     for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
1016*dfc6aa5cSAndroid Build Coastguard Worker       if (bptr->b_s_open) {     /* there may be no backing store */
1017*dfc6aa5cSAndroid Build Coastguard Worker         bptr->b_s_open = FALSE; /* prevent recursive close if error */
1018*dfc6aa5cSAndroid Build Coastguard Worker         (*bptr->b_s_info.close_backing_store) (cinfo, &bptr->b_s_info);
1019*dfc6aa5cSAndroid Build Coastguard Worker       }
1020*dfc6aa5cSAndroid Build Coastguard Worker     }
1021*dfc6aa5cSAndroid Build Coastguard Worker     mem->virt_barray_list = NULL;
1022*dfc6aa5cSAndroid Build Coastguard Worker   }
1023*dfc6aa5cSAndroid Build Coastguard Worker 
1024*dfc6aa5cSAndroid Build Coastguard Worker   /* Release large objects */
1025*dfc6aa5cSAndroid Build Coastguard Worker   lhdr_ptr = mem->large_list[pool_id];
1026*dfc6aa5cSAndroid Build Coastguard Worker   mem->large_list[pool_id] = NULL;
1027*dfc6aa5cSAndroid Build Coastguard Worker 
1028*dfc6aa5cSAndroid Build Coastguard Worker   while (lhdr_ptr != NULL) {
1029*dfc6aa5cSAndroid Build Coastguard Worker     large_pool_ptr next_lhdr_ptr = lhdr_ptr->next;
1030*dfc6aa5cSAndroid Build Coastguard Worker     space_freed = lhdr_ptr->bytes_used +
1031*dfc6aa5cSAndroid Build Coastguard Worker                   lhdr_ptr->bytes_left +
1032*dfc6aa5cSAndroid Build Coastguard Worker                   sizeof(large_pool_hdr) + ALIGN_SIZE - 1;
1033*dfc6aa5cSAndroid Build Coastguard Worker     jpeg_free_large(cinfo, (void *)lhdr_ptr, space_freed);
1034*dfc6aa5cSAndroid Build Coastguard Worker     mem->total_space_allocated -= space_freed;
1035*dfc6aa5cSAndroid Build Coastguard Worker     lhdr_ptr = next_lhdr_ptr;
1036*dfc6aa5cSAndroid Build Coastguard Worker   }
1037*dfc6aa5cSAndroid Build Coastguard Worker 
1038*dfc6aa5cSAndroid Build Coastguard Worker   /* Release small objects */
1039*dfc6aa5cSAndroid Build Coastguard Worker   shdr_ptr = mem->small_list[pool_id];
1040*dfc6aa5cSAndroid Build Coastguard Worker   mem->small_list[pool_id] = NULL;
1041*dfc6aa5cSAndroid Build Coastguard Worker 
1042*dfc6aa5cSAndroid Build Coastguard Worker   while (shdr_ptr != NULL) {
1043*dfc6aa5cSAndroid Build Coastguard Worker     small_pool_ptr next_shdr_ptr = shdr_ptr->next;
1044*dfc6aa5cSAndroid Build Coastguard Worker     space_freed = shdr_ptr->bytes_used + shdr_ptr->bytes_left +
1045*dfc6aa5cSAndroid Build Coastguard Worker                   sizeof(small_pool_hdr) + ALIGN_SIZE - 1;
1046*dfc6aa5cSAndroid Build Coastguard Worker     jpeg_free_small(cinfo, (void *)shdr_ptr, space_freed);
1047*dfc6aa5cSAndroid Build Coastguard Worker     mem->total_space_allocated -= space_freed;
1048*dfc6aa5cSAndroid Build Coastguard Worker     shdr_ptr = next_shdr_ptr;
1049*dfc6aa5cSAndroid Build Coastguard Worker   }
1050*dfc6aa5cSAndroid Build Coastguard Worker }
1051*dfc6aa5cSAndroid Build Coastguard Worker 
1052*dfc6aa5cSAndroid Build Coastguard Worker 
1053*dfc6aa5cSAndroid Build Coastguard Worker /*
1054*dfc6aa5cSAndroid Build Coastguard Worker  * Close up shop entirely.
1055*dfc6aa5cSAndroid Build Coastguard Worker  * Note that this cannot be called unless cinfo->mem is non-NULL.
1056*dfc6aa5cSAndroid Build Coastguard Worker  */
1057*dfc6aa5cSAndroid Build Coastguard Worker 
1058*dfc6aa5cSAndroid Build Coastguard Worker METHODDEF(void)
self_destruct(j_common_ptr cinfo)1059*dfc6aa5cSAndroid Build Coastguard Worker self_destruct(j_common_ptr cinfo)
1060*dfc6aa5cSAndroid Build Coastguard Worker {
1061*dfc6aa5cSAndroid Build Coastguard Worker   int pool;
1062*dfc6aa5cSAndroid Build Coastguard Worker 
1063*dfc6aa5cSAndroid Build Coastguard Worker   /* Close all backing store, release all memory.
1064*dfc6aa5cSAndroid Build Coastguard Worker    * Releasing pools in reverse order might help avoid fragmentation
1065*dfc6aa5cSAndroid Build Coastguard Worker    * with some (brain-damaged) malloc libraries.
1066*dfc6aa5cSAndroid Build Coastguard Worker    */
1067*dfc6aa5cSAndroid Build Coastguard Worker   for (pool = JPOOL_NUMPOOLS - 1; pool >= JPOOL_PERMANENT; pool--) {
1068*dfc6aa5cSAndroid Build Coastguard Worker     free_pool(cinfo, pool);
1069*dfc6aa5cSAndroid Build Coastguard Worker   }
1070*dfc6aa5cSAndroid Build Coastguard Worker 
1071*dfc6aa5cSAndroid Build Coastguard Worker   /* Release the memory manager control block too. */
1072*dfc6aa5cSAndroid Build Coastguard Worker   jpeg_free_small(cinfo, (void *)cinfo->mem, sizeof(my_memory_mgr));
1073*dfc6aa5cSAndroid Build Coastguard Worker   cinfo->mem = NULL;            /* ensures I will be called only once */
1074*dfc6aa5cSAndroid Build Coastguard Worker 
1075*dfc6aa5cSAndroid Build Coastguard Worker   jpeg_mem_term(cinfo);         /* system-dependent cleanup */
1076*dfc6aa5cSAndroid Build Coastguard Worker }
1077*dfc6aa5cSAndroid Build Coastguard Worker 
1078*dfc6aa5cSAndroid Build Coastguard Worker 
1079*dfc6aa5cSAndroid Build Coastguard Worker /*
1080*dfc6aa5cSAndroid Build Coastguard Worker  * Memory manager initialization.
1081*dfc6aa5cSAndroid Build Coastguard Worker  * When this is called, only the error manager pointer is valid in cinfo!
1082*dfc6aa5cSAndroid Build Coastguard Worker  */
1083*dfc6aa5cSAndroid Build Coastguard Worker 
1084*dfc6aa5cSAndroid Build Coastguard Worker GLOBAL(void)
jinit_memory_mgr(j_common_ptr cinfo)1085*dfc6aa5cSAndroid Build Coastguard Worker jinit_memory_mgr(j_common_ptr cinfo)
1086*dfc6aa5cSAndroid Build Coastguard Worker {
1087*dfc6aa5cSAndroid Build Coastguard Worker   my_mem_ptr mem;
1088*dfc6aa5cSAndroid Build Coastguard Worker   long max_to_use;
1089*dfc6aa5cSAndroid Build Coastguard Worker   int pool;
1090*dfc6aa5cSAndroid Build Coastguard Worker   size_t test_mac;
1091*dfc6aa5cSAndroid Build Coastguard Worker 
1092*dfc6aa5cSAndroid Build Coastguard Worker   cinfo->mem = NULL;            /* for safety if init fails */
1093*dfc6aa5cSAndroid Build Coastguard Worker 
1094*dfc6aa5cSAndroid Build Coastguard Worker   /* Check for configuration errors.
1095*dfc6aa5cSAndroid Build Coastguard Worker    * sizeof(ALIGN_TYPE) should be a power of 2; otherwise, it probably
1096*dfc6aa5cSAndroid Build Coastguard Worker    * doesn't reflect any real hardware alignment requirement.
1097*dfc6aa5cSAndroid Build Coastguard Worker    * The test is a little tricky: for X>0, X and X-1 have no one-bits
1098*dfc6aa5cSAndroid Build Coastguard Worker    * in common if and only if X is a power of 2, ie has only one one-bit.
1099*dfc6aa5cSAndroid Build Coastguard Worker    * Some compilers may give an "unreachable code" warning here; ignore it.
1100*dfc6aa5cSAndroid Build Coastguard Worker    */
1101*dfc6aa5cSAndroid Build Coastguard Worker   if ((ALIGN_SIZE & (ALIGN_SIZE - 1)) != 0)
1102*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
1103*dfc6aa5cSAndroid Build Coastguard Worker   /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
1104*dfc6aa5cSAndroid Build Coastguard Worker    * a multiple of ALIGN_SIZE.
1105*dfc6aa5cSAndroid Build Coastguard Worker    * Again, an "unreachable code" warning may be ignored here.
1106*dfc6aa5cSAndroid Build Coastguard Worker    * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
1107*dfc6aa5cSAndroid Build Coastguard Worker    */
1108*dfc6aa5cSAndroid Build Coastguard Worker   test_mac = (size_t)MAX_ALLOC_CHUNK;
1109*dfc6aa5cSAndroid Build Coastguard Worker   if ((long)test_mac != MAX_ALLOC_CHUNK ||
1110*dfc6aa5cSAndroid Build Coastguard Worker       (MAX_ALLOC_CHUNK % ALIGN_SIZE) != 0)
1111*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
1112*dfc6aa5cSAndroid Build Coastguard Worker 
1113*dfc6aa5cSAndroid Build Coastguard Worker   max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */
1114*dfc6aa5cSAndroid Build Coastguard Worker 
1115*dfc6aa5cSAndroid Build Coastguard Worker   /* Attempt to allocate memory manager's control block */
1116*dfc6aa5cSAndroid Build Coastguard Worker   mem = (my_mem_ptr)jpeg_get_small(cinfo, sizeof(my_memory_mgr));
1117*dfc6aa5cSAndroid Build Coastguard Worker 
1118*dfc6aa5cSAndroid Build Coastguard Worker   if (mem == NULL) {
1119*dfc6aa5cSAndroid Build Coastguard Worker     jpeg_mem_term(cinfo);       /* system-dependent cleanup */
1120*dfc6aa5cSAndroid Build Coastguard Worker     ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
1121*dfc6aa5cSAndroid Build Coastguard Worker   }
1122*dfc6aa5cSAndroid Build Coastguard Worker 
1123*dfc6aa5cSAndroid Build Coastguard Worker   /* OK, fill in the method pointers */
1124*dfc6aa5cSAndroid Build Coastguard Worker   mem->pub.alloc_small = alloc_small;
1125*dfc6aa5cSAndroid Build Coastguard Worker   mem->pub.alloc_large = alloc_large;
1126*dfc6aa5cSAndroid Build Coastguard Worker   mem->pub.alloc_sarray = alloc_sarray;
1127*dfc6aa5cSAndroid Build Coastguard Worker   mem->pub.alloc_barray = alloc_barray;
1128*dfc6aa5cSAndroid Build Coastguard Worker   mem->pub.request_virt_sarray = request_virt_sarray;
1129*dfc6aa5cSAndroid Build Coastguard Worker   mem->pub.request_virt_barray = request_virt_barray;
1130*dfc6aa5cSAndroid Build Coastguard Worker   mem->pub.realize_virt_arrays = realize_virt_arrays;
1131*dfc6aa5cSAndroid Build Coastguard Worker   mem->pub.access_virt_sarray = access_virt_sarray;
1132*dfc6aa5cSAndroid Build Coastguard Worker   mem->pub.access_virt_barray = access_virt_barray;
1133*dfc6aa5cSAndroid Build Coastguard Worker   mem->pub.free_pool = free_pool;
1134*dfc6aa5cSAndroid Build Coastguard Worker   mem->pub.self_destruct = self_destruct;
1135*dfc6aa5cSAndroid Build Coastguard Worker 
1136*dfc6aa5cSAndroid Build Coastguard Worker   /* Make MAX_ALLOC_CHUNK accessible to other modules */
1137*dfc6aa5cSAndroid Build Coastguard Worker   mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK;
1138*dfc6aa5cSAndroid Build Coastguard Worker 
1139*dfc6aa5cSAndroid Build Coastguard Worker   /* Initialize working state */
1140*dfc6aa5cSAndroid Build Coastguard Worker   mem->pub.max_memory_to_use = max_to_use;
1141*dfc6aa5cSAndroid Build Coastguard Worker 
1142*dfc6aa5cSAndroid Build Coastguard Worker   for (pool = JPOOL_NUMPOOLS - 1; pool >= JPOOL_PERMANENT; pool--) {
1143*dfc6aa5cSAndroid Build Coastguard Worker     mem->small_list[pool] = NULL;
1144*dfc6aa5cSAndroid Build Coastguard Worker     mem->large_list[pool] = NULL;
1145*dfc6aa5cSAndroid Build Coastguard Worker   }
1146*dfc6aa5cSAndroid Build Coastguard Worker   mem->virt_sarray_list = NULL;
1147*dfc6aa5cSAndroid Build Coastguard Worker   mem->virt_barray_list = NULL;
1148*dfc6aa5cSAndroid Build Coastguard Worker 
1149*dfc6aa5cSAndroid Build Coastguard Worker   mem->total_space_allocated = sizeof(my_memory_mgr);
1150*dfc6aa5cSAndroid Build Coastguard Worker 
1151*dfc6aa5cSAndroid Build Coastguard Worker   /* Declare ourselves open for business */
1152*dfc6aa5cSAndroid Build Coastguard Worker   cinfo->mem = &mem->pub;
1153*dfc6aa5cSAndroid Build Coastguard Worker 
1154*dfc6aa5cSAndroid Build Coastguard Worker   /* Check for an environment variable JPEGMEM; if found, override the
1155*dfc6aa5cSAndroid Build Coastguard Worker    * default max_memory setting from jpeg_mem_init.  Note that the
1156*dfc6aa5cSAndroid Build Coastguard Worker    * surrounding application may again override this value.
1157*dfc6aa5cSAndroid Build Coastguard Worker    * If your system doesn't support getenv(), define NO_GETENV to disable
1158*dfc6aa5cSAndroid Build Coastguard Worker    * this feature.
1159*dfc6aa5cSAndroid Build Coastguard Worker    */
1160*dfc6aa5cSAndroid Build Coastguard Worker #ifndef NO_GETENV
1161*dfc6aa5cSAndroid Build Coastguard Worker   {
1162*dfc6aa5cSAndroid Build Coastguard Worker     char memenv[30] = { 0 };
1163*dfc6aa5cSAndroid Build Coastguard Worker 
1164*dfc6aa5cSAndroid Build Coastguard Worker     if (!GETENV_S(memenv, 30, "JPEGMEM") && strlen(memenv) > 0) {
1165*dfc6aa5cSAndroid Build Coastguard Worker       char ch = 'x';
1166*dfc6aa5cSAndroid Build Coastguard Worker 
1167*dfc6aa5cSAndroid Build Coastguard Worker #ifdef _MSC_VER
1168*dfc6aa5cSAndroid Build Coastguard Worker       if (sscanf_s(memenv, "%ld%c", &max_to_use, &ch, 1) > 0) {
1169*dfc6aa5cSAndroid Build Coastguard Worker #else
1170*dfc6aa5cSAndroid Build Coastguard Worker       if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
1171*dfc6aa5cSAndroid Build Coastguard Worker #endif
1172*dfc6aa5cSAndroid Build Coastguard Worker         if (ch == 'm' || ch == 'M')
1173*dfc6aa5cSAndroid Build Coastguard Worker           max_to_use *= 1000L;
1174*dfc6aa5cSAndroid Build Coastguard Worker         mem->pub.max_memory_to_use = max_to_use * 1000L;
1175*dfc6aa5cSAndroid Build Coastguard Worker       }
1176*dfc6aa5cSAndroid Build Coastguard Worker     }
1177*dfc6aa5cSAndroid Build Coastguard Worker   }
1178*dfc6aa5cSAndroid Build Coastguard Worker #endif
1179*dfc6aa5cSAndroid Build Coastguard Worker 
1180*dfc6aa5cSAndroid Build Coastguard Worker }
1181