blob: b17c5c5c656e45f53a5ce8d0d5b860228ab59047 [file] [log] [blame]
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001/*
2 * jmemmgr.c
3 *
DRC5033f3e2014-05-18 18:33:44 +00004 * This file was part of the Independent JPEG Group's software:
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00005 * Copyright (C) 1991-1997, Thomas G. Lane.
DRC04dd34c2016-02-04 10:59:21 -06006 * libjpeg-turbo Modifications:
7 * Copyright (C) 2016, D. R. Commander.
DRC7e3acc02015-10-10 10:25:46 -05008 * For conditions of distribution and use, see the accompanying README.ijg
9 * file.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000010 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000011 * This file contains the JPEG system-independent memory management
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000012 * routines. This code is usable across a wide variety of machines; most
13 * of the system dependencies have been isolated in a separate file.
14 * The major functions provided here are:
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000015 * * pool-based allocation and freeing of memory;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000016 * * policy decisions about how to divide available memory among the
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000017 * virtual arrays;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000018 * * control logic for swapping virtual arrays between main memory and
19 * backing storage.
20 * The separate system-dependent file provides the actual backing-storage
21 * access code, and it contains the policy decision about how much total
22 * main memory to use.
23 * This file is system-dependent in the sense that some of its functions
24 * are unnecessary in some systems. For example, if there is enough virtual
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000025 * memory so that backing storage will never be used, much of the virtual
26 * array control logic could be removed. (Of course, if you have that much
27 * memory then you shouldn't care about a little bit of unused code...)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000028 */
29
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000030#define JPEG_INTERNALS
DRCe5eaf372014-05-09 18:00:32 +000031#define AM_MEMORY_MANAGER /* we define jvirt_Xarray_control structs */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000032#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000033#include "jpeglib.h"
DRCe5eaf372014-05-09 18:00:32 +000034#include "jmemsys.h" /* import the system-dependent declarations */
DRCa09ba292016-09-07 16:40:10 -050035#include <stdint.h>
DRCdfefba72016-09-22 14:19:29 -050036#include <limits.h>
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000037
Thomas G. Lane88aeed41992-12-10 00:00:00 +000038#ifndef NO_GETENV
DRCe5eaf372014-05-09 18:00:32 +000039#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare getenv() */
DRCbd498032016-02-19 08:53:33 -060040extern char *getenv (const char *name);
Thomas G. Lane88aeed41992-12-10 00:00:00 +000041#endif
42#endif
43
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000044
DRCa8eabfe2011-03-29 04:58:40 +000045LOCAL(size_t)
46round_up_pow2 (size_t a, size_t b)
47/* a rounded up to the next multiple of b, i.e. ceil(a/b)*b */
48/* Assumes a >= 0, b > 0, and b is a power of 2 */
49{
50 return ((a + b - 1) & (~(b - 1)));
51}
52
53
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000054/*
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000055 * Some important notes:
56 * The allocation routines provided here must never return NULL.
57 * They should exit to error_exit if unsuccessful.
58 *
59 * It's not a good idea to try to merge the sarray and barray routines,
60 * even though they are textually almost the same, because samples are
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000061 * usually stored as bytes while coefficients are shorts or ints. Thus,
62 * in machines where byte pointers have a different representation from
63 * word pointers, the resulting machine code could not be the same.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000064 */
65
66
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000067/*
68 * Many machines require storage alignment: longs must start on 4-byte
69 * boundaries, doubles on 8-byte boundaries, etc. On such machines, malloc()
70 * always returns pointers that are multiples of the worst-case alignment
71 * requirement, and we had better do so too.
72 * There isn't any really portable way to determine the worst-case alignment
73 * requirement. This module assumes that the alignment requirement is
Pierre Ossman5557fd22009-03-09 10:34:53 +000074 * multiples of ALIGN_SIZE.
DRC271b0bf2016-02-04 10:08:38 -060075 * By default, we define ALIGN_SIZE as sizeof(double). This is necessary on
76 * some workstations (where doubles really do need 8-byte alignment) and will
77 * work fine on nearly everything. If your machine has lesser alignment needs,
Pierre Ossman5557fd22009-03-09 10:34:53 +000078 * you can save a few bytes by making ALIGN_SIZE smaller.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000079 * The only place I know of where this will NOT work is certain Macintosh
80 * 680x0 compilers that define double as a 10-byte IEEE extended float.
81 * Doing 10-byte alignment is counterproductive because longwords won't be
Pierre Ossman5557fd22009-03-09 10:34:53 +000082 * aligned well. Put "#define ALIGN_SIZE 4" in jconfig.h if you have
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000083 * such a compiler.
84 */
85
DRCe5eaf372014-05-09 18:00:32 +000086#ifndef ALIGN_SIZE /* so can override from jconfig.h */
Pierre Ossman73118302009-03-09 13:30:47 +000087#ifndef WITH_SIMD
DRC5de454b2014-05-18 19:04:03 +000088#define ALIGN_SIZE sizeof(double)
Pierre Ossman73118302009-03-09 13:30:47 +000089#else
90#define ALIGN_SIZE 16 /* Most SIMD implementations require this */
91#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000092#endif
93
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000094/*
95 * We allocate objects from "pools", where each pool is gotten with a single
96 * request to jpeg_get_small() or jpeg_get_large(). There is no per-object
97 * overhead within a pool, except for alignment padding. Each pool has a
98 * header with a link to the next pool of the same class.
DRC5033f3e2014-05-18 18:33:44 +000099 * Small and large pool headers are identical.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000100 */
101
DRCbd498032016-02-19 08:53:33 -0600102typedef struct small_pool_struct *small_pool_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000103
Pierre Ossman5557fd22009-03-09 10:34:53 +0000104typedef struct small_pool_struct {
DRCe5eaf372014-05-09 18:00:32 +0000105 small_pool_ptr next; /* next in list of pools */
106 size_t bytes_used; /* how many bytes already used within pool */
107 size_t bytes_left; /* bytes still available in this pool */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000108} small_pool_hdr;
109
DRCbd498032016-02-19 08:53:33 -0600110typedef struct large_pool_struct *large_pool_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000111
Pierre Ossman5557fd22009-03-09 10:34:53 +0000112typedef struct large_pool_struct {
DRCe5eaf372014-05-09 18:00:32 +0000113 large_pool_ptr next; /* next in list of pools */
114 size_t bytes_used; /* how many bytes already used within pool */
115 size_t bytes_left; /* bytes still available in this pool */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000116} large_pool_hdr;
117
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000118/*
119 * Here is the full definition of a memory manager object.
120 */
121
122typedef struct {
DRCe5eaf372014-05-09 18:00:32 +0000123 struct jpeg_memory_mgr pub; /* public fields */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000124
125 /* Each pool identifier (lifetime class) names a linked list of pools. */
126 small_pool_ptr small_list[JPOOL_NUMPOOLS];
127 large_pool_ptr large_list[JPOOL_NUMPOOLS];
128
129 /* Since we only have one lifetime class of virtual arrays, only one
130 * linked list is necessary (for each datatype). Note that the virtual
131 * array control blocks being linked together are actually stored somewhere
132 * in the small-pool list.
133 */
134 jvirt_sarray_ptr virt_sarray_list;
135 jvirt_barray_ptr virt_barray_list;
136
137 /* This counts total space obtained from jpeg_get_small/large */
DRC04899092010-02-26 23:01:19 +0000138 size_t total_space_allocated;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000139
140 /* alloc_sarray and alloc_barray set this value for use by virtual
141 * array routines.
142 */
DRCe5eaf372014-05-09 18:00:32 +0000143 JDIMENSION last_rowsperchunk; /* from most recent alloc_sarray/barray */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000144} my_memory_mgr;
145
DRCbd498032016-02-19 08:53:33 -0600146typedef my_memory_mgr *my_mem_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000147
148
149/*
150 * The control blocks for virtual arrays.
151 * Note that these blocks are allocated in the "small" pool area.
152 * System-dependent info for the associated backing store (if any) is hidden
153 * inside the backing_store_info struct.
154 */
155
156struct jvirt_sarray_control {
DRCe5eaf372014-05-09 18:00:32 +0000157 JSAMPARRAY mem_buffer; /* => the in-memory buffer */
158 JDIMENSION rows_in_array; /* total virtual array height */
159 JDIMENSION samplesperrow; /* width of array (and of memory buffer) */
160 JDIMENSION maxaccess; /* max rows accessed by access_virt_sarray */
161 JDIMENSION rows_in_mem; /* height of memory buffer */
162 JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
163 JDIMENSION cur_start_row; /* first logical row # in the buffer */
164 JDIMENSION first_undef_row; /* row # of first uninitialized row */
165 boolean pre_zero; /* pre-zero mode requested? */
166 boolean dirty; /* do current buffer contents need written? */
167 boolean b_s_open; /* is backing-store data valid? */
168 jvirt_sarray_ptr next; /* link to next virtual sarray control block */
169 backing_store_info b_s_info; /* System-dependent control info */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000170};
171
172struct jvirt_barray_control {
DRCe5eaf372014-05-09 18:00:32 +0000173 JBLOCKARRAY mem_buffer; /* => the in-memory buffer */
174 JDIMENSION rows_in_array; /* total virtual array height */
175 JDIMENSION blocksperrow; /* width of array (and of memory buffer) */
176 JDIMENSION maxaccess; /* max rows accessed by access_virt_barray */
177 JDIMENSION rows_in_mem; /* height of memory buffer */
178 JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
179 JDIMENSION cur_start_row; /* first logical row # in the buffer */
180 JDIMENSION first_undef_row; /* row # of first uninitialized row */
181 boolean pre_zero; /* pre-zero mode requested? */
182 boolean dirty; /* do current buffer contents need written? */
183 boolean b_s_open; /* is backing-store data valid? */
184 jvirt_barray_ptr next; /* link to next virtual barray control block */
185 backing_store_info b_s_info; /* System-dependent control info */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000186};
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000187
188
DRCe5eaf372014-05-09 18:00:32 +0000189#ifdef MEM_STATS /* optional extra stuff for statistics */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000190
Thomas G. Lane489583f1996-02-07 00:00:00 +0000191LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000192print_mem_stats (j_common_ptr cinfo, int pool_id)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000193{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000194 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
195 small_pool_ptr shdr_ptr;
196 large_pool_ptr lhdr_ptr;
197
198 /* Since this is only a debugging stub, we can cheat a little by using
199 * fprintf directly rather than going through the trace message code.
200 * This is helpful because message parm array can't handle longs.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000201 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000202 fprintf(stderr, "Freeing pool %d, total space = %ld\n",
DRCe5eaf372014-05-09 18:00:32 +0000203 pool_id, mem->total_space_allocated);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000204
205 for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000206 lhdr_ptr = lhdr_ptr->next) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000207 fprintf(stderr, " Large chunk used %ld\n",
DRCe5eaf372014-05-09 18:00:32 +0000208 (long) lhdr_ptr->bytes_used);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000209 }
210
211 for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000212 shdr_ptr = shdr_ptr->next) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000213 fprintf(stderr, " Small chunk used %ld free %ld\n",
DRCe5eaf372014-05-09 18:00:32 +0000214 (long) shdr_ptr->bytes_used,
215 (long) shdr_ptr->bytes_left);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000216 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000217}
218
219#endif /* MEM_STATS */
220
221
Thomas G. Lane489583f1996-02-07 00:00:00 +0000222LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000223out_of_memory (j_common_ptr cinfo, int which)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000224/* Report an out-of-memory error and stop execution */
225/* If we compiled MEM_STATS support, report alloc requests before dying */
226{
227#ifdef MEM_STATS
DRCe5eaf372014-05-09 18:00:32 +0000228 cinfo->err->trace_level = 2; /* force self_destruct to report stats */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000229#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000230 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000231}
232
233
234/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000235 * Allocation of "small" objects.
236 *
237 * For these, we use pooled storage. When a new pool must be created,
238 * we try to get enough space for the current request plus a "slop" factor,
239 * where the slop will be the amount of leftover space in the new pool.
240 * The speed vs. space tradeoff is largely determined by the slop values.
241 * A different slop value is provided for each pool class (lifetime),
242 * and we also distinguish the first pool of a class from later ones.
243 * NOTE: the values given work fairly well on both 16- and 32-bit-int
244 * machines, but may be too small if longs are 64 bits or more.
Pierre Ossman5557fd22009-03-09 10:34:53 +0000245 *
246 * Since we do not know what alignment malloc() gives us, we have to
247 * allocate ALIGN_SIZE-1 extra space per pool to have room for alignment
248 * adjustment.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000249 */
250
DRCe5eaf372014-05-09 18:00:32 +0000251static const size_t first_pool_slop[JPOOL_NUMPOOLS] =
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000252{
DRCe5eaf372014-05-09 18:00:32 +0000253 1600, /* first PERMANENT pool */
254 16000 /* first IMAGE pool */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000255};
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000256
DRCe5eaf372014-05-09 18:00:32 +0000257static const size_t extra_pool_slop[JPOOL_NUMPOOLS] =
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000258{
DRCe5eaf372014-05-09 18:00:32 +0000259 0, /* additional PERMANENT pools */
260 5000 /* additional IMAGE pools */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000261};
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000262
DRCe5eaf372014-05-09 18:00:32 +0000263#define MIN_SLOP 50 /* greater than 0 to avoid futile looping */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000264
265
Thomas G. Lane489583f1996-02-07 00:00:00 +0000266METHODDEF(void *)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000267alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000268/* Allocate a "small" object */
269{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000270 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
271 small_pool_ptr hdr_ptr, prev_hdr_ptr;
DRCbd498032016-02-19 08:53:33 -0600272 char *data_ptr;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000273 size_t min_request, slop;
274
275 /*
276 * Round up the requested size to a multiple of ALIGN_SIZE in order
277 * to assure alignment for the next object allocated in the same pool
278 * and so that algorithms can straddle outside the proper area up
279 * to the next alignment.
280 */
DRC04dd34c2016-02-04 10:59:21 -0600281 if (sizeofobject > MAX_ALLOC_CHUNK) {
282 /* This prevents overflow/wrap-around in round_up_pow2() if sizeofobject
283 is close to SIZE_MAX. */
284 out_of_memory(cinfo, 7);
285 }
DRCa8eabfe2011-03-29 04:58:40 +0000286 sizeofobject = round_up_pow2(sizeofobject, ALIGN_SIZE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000287
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000288 /* Check for unsatisfiable request (do now to ensure no overflow below) */
DRC271b0bf2016-02-04 10:08:38 -0600289 if ((sizeof(small_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) >
290 MAX_ALLOC_CHUNK)
DRCe5eaf372014-05-09 18:00:32 +0000291 out_of_memory(cinfo, 1); /* request exceeds malloc's ability */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000292
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000293 /* See if space is available in any existing pool */
294 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
DRCe5eaf372014-05-09 18:00:32 +0000295 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000296 prev_hdr_ptr = NULL;
297 hdr_ptr = mem->small_list[pool_id];
298 while (hdr_ptr != NULL) {
Pierre Ossman5557fd22009-03-09 10:34:53 +0000299 if (hdr_ptr->bytes_left >= sizeofobject)
DRCe5eaf372014-05-09 18:00:32 +0000300 break; /* found pool with enough space */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000301 prev_hdr_ptr = hdr_ptr;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000302 hdr_ptr = hdr_ptr->next;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000303 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000304
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000305 /* Time to make a new pool? */
306 if (hdr_ptr == NULL) {
307 /* min_request is what we need now, slop is what will be leftover */
DRC5de454b2014-05-18 19:04:03 +0000308 min_request = sizeof(small_pool_hdr) + sizeofobject + ALIGN_SIZE - 1;
DRCe5eaf372014-05-09 18:00:32 +0000309 if (prev_hdr_ptr == NULL) /* first pool in class? */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000310 slop = first_pool_slop[pool_id];
311 else
312 slop = extra_pool_slop[pool_id];
313 /* Don't ask for more than MAX_ALLOC_CHUNK */
314 if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
315 slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
316 /* Try to get space, if fail reduce slop and try again */
317 for (;;) {
318 hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
319 if (hdr_ptr != NULL)
DRCe5eaf372014-05-09 18:00:32 +0000320 break;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000321 slop /= 2;
DRCe5eaf372014-05-09 18:00:32 +0000322 if (slop < MIN_SLOP) /* give up when it gets real small */
323 out_of_memory(cinfo, 2); /* jpeg_get_small failed */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000324 }
325 mem->total_space_allocated += min_request + slop;
326 /* Success, initialize the new pool header and add to end of list */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000327 hdr_ptr->next = NULL;
328 hdr_ptr->bytes_used = 0;
329 hdr_ptr->bytes_left = sizeofobject + slop;
DRCe5eaf372014-05-09 18:00:32 +0000330 if (prev_hdr_ptr == NULL) /* first pool in class? */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000331 mem->small_list[pool_id] = hdr_ptr;
332 else
Pierre Ossman5557fd22009-03-09 10:34:53 +0000333 prev_hdr_ptr->next = hdr_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000334 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000335
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000336 /* OK, allocate the object from the current pool */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000337 data_ptr = (char *) hdr_ptr; /* point to first data byte in pool... */
DRC5de454b2014-05-18 19:04:03 +0000338 data_ptr += sizeof(small_pool_hdr); /* ...by skipping the header... */
DRC04899092010-02-26 23:01:19 +0000339 if ((size_t)data_ptr % ALIGN_SIZE) /* ...and adjust for alignment */
340 data_ptr += ALIGN_SIZE - (size_t)data_ptr % ALIGN_SIZE;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000341 data_ptr += hdr_ptr->bytes_used; /* point to place for object */
342 hdr_ptr->bytes_used += sizeofobject;
343 hdr_ptr->bytes_left -= sizeofobject;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000344
345 return (void *) data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000346}
347
348
349/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000350 * Allocation of "large" objects.
351 *
DRC5033f3e2014-05-18 18:33:44 +0000352 * The external semantics of these are the same as "small" objects. However,
353 * the pool management heuristics are quite different. We assume that each
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000354 * request is large enough that it may as well be passed directly to
355 * jpeg_get_large; the pool management just links everything together
356 * so that we can free it all on demand.
357 * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY
358 * structures. The routines that create these structures (see below)
359 * deliberately bunch rows together to ensure a large request size.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000360 */
361
DRC5033f3e2014-05-18 18:33:44 +0000362METHODDEF(void *)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000363alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
364/* Allocate a "large" object */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000365{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000366 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
367 large_pool_ptr hdr_ptr;
DRCbd498032016-02-19 08:53:33 -0600368 char *data_ptr;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000369
370 /*
371 * Round up the requested size to a multiple of ALIGN_SIZE so that
372 * algorithms can straddle outside the proper area up to the next
373 * alignment.
374 */
DRC04dd34c2016-02-04 10:59:21 -0600375 if (sizeofobject > MAX_ALLOC_CHUNK) {
376 /* This prevents overflow/wrap-around in round_up_pow2() if sizeofobject
377 is close to SIZE_MAX. */
378 out_of_memory(cinfo, 8);
379 }
DRCa8eabfe2011-03-29 04:58:40 +0000380 sizeofobject = round_up_pow2(sizeofobject, ALIGN_SIZE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000381
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000382 /* Check for unsatisfiable request (do now to ensure no overflow below) */
DRC271b0bf2016-02-04 10:08:38 -0600383 if ((sizeof(large_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) >
384 MAX_ALLOC_CHUNK)
DRCe5eaf372014-05-09 18:00:32 +0000385 out_of_memory(cinfo, 3); /* request exceeds malloc's ability */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000386
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000387 /* Always make a new pool */
388 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
DRCe5eaf372014-05-09 18:00:32 +0000389 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000390
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000391 hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
DRC5de454b2014-05-18 19:04:03 +0000392 sizeof(large_pool_hdr) +
DRCe5eaf372014-05-09 18:00:32 +0000393 ALIGN_SIZE - 1);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000394 if (hdr_ptr == NULL)
DRCe5eaf372014-05-09 18:00:32 +0000395 out_of_memory(cinfo, 4); /* jpeg_get_large failed */
DRC271b0bf2016-02-04 10:08:38 -0600396 mem->total_space_allocated += sizeofobject + sizeof(large_pool_hdr) +
397 ALIGN_SIZE - 1;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000398
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000399 /* Success, initialize the new pool header and add to list */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000400 hdr_ptr->next = mem->large_list[pool_id];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000401 /* We maintain space counts in each pool header for statistical purposes,
402 * even though they are not needed for allocation.
403 */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000404 hdr_ptr->bytes_used = sizeofobject;
405 hdr_ptr->bytes_left = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000406 mem->large_list[pool_id] = hdr_ptr;
407
Pierre Ossman5557fd22009-03-09 10:34:53 +0000408 data_ptr = (char *) hdr_ptr; /* point to first data byte in pool... */
DRC5de454b2014-05-18 19:04:03 +0000409 data_ptr += sizeof(small_pool_hdr); /* ...by skipping the header... */
DRC04899092010-02-26 23:01:19 +0000410 if ((size_t)data_ptr % ALIGN_SIZE) /* ...and adjust for alignment */
411 data_ptr += ALIGN_SIZE - (size_t)data_ptr % ALIGN_SIZE;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000412
DRC5033f3e2014-05-18 18:33:44 +0000413 return (void *) data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000414}
415
416
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000417/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000418 * Creation of 2-D sample arrays.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000419 *
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000420 * To minimize allocation overhead and to allow I/O of large contiguous
421 * blocks, we allocate the sample rows in groups of as many rows as possible
422 * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000423 * NB: the virtual array control routines, later in this file, know about
424 * this chunking of rows. The rowsperchunk value is left in the mem manager
425 * object so that it can be saved away if this sarray is the workspace for
426 * a virtual array.
Pierre Ossman5557fd22009-03-09 10:34:53 +0000427 *
428 * Since we are often upsampling with a factor 2, we align the size (not
429 * the start) to 2 * ALIGN_SIZE so that the upsampling routines don't have
430 * to be as careful about size.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000431 */
432
Thomas G. Lane489583f1996-02-07 00:00:00 +0000433METHODDEF(JSAMPARRAY)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000434alloc_sarray (j_common_ptr cinfo, int pool_id,
DRCe5eaf372014-05-09 18:00:32 +0000435 JDIMENSION samplesperrow, JDIMENSION numrows)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000436/* Allocate a 2-D sample array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000437{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000438 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000439 JSAMPARRAY result;
440 JSAMPROW workspace;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000441 JDIMENSION rowsperchunk, currow, i;
442 long ltemp;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000443
Pierre Ossman5557fd22009-03-09 10:34:53 +0000444 /* Make sure each row is properly aligned */
DRC5de454b2014-05-18 19:04:03 +0000445 if ((ALIGN_SIZE % sizeof(JSAMPLE)) != 0)
DRCe5eaf372014-05-09 18:00:32 +0000446 out_of_memory(cinfo, 5); /* safety check */
DRC04dd34c2016-02-04 10:59:21 -0600447
448 if (samplesperrow > MAX_ALLOC_CHUNK) {
449 /* This prevents overflow/wrap-around in round_up_pow2() if sizeofobject
450 is close to SIZE_MAX. */
451 out_of_memory(cinfo, 9);
452 }
DRC271b0bf2016-02-04 10:08:38 -0600453 samplesperrow = (JDIMENSION)round_up_pow2(samplesperrow, (2 * ALIGN_SIZE) /
454 sizeof(JSAMPLE));
Pierre Ossman5557fd22009-03-09 10:34:53 +0000455
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000456 /* Calculate max # of rows allowed in one allocation chunk */
DRC5de454b2014-05-18 19:04:03 +0000457 ltemp = (MAX_ALLOC_CHUNK-sizeof(large_pool_hdr)) /
458 ((long) samplesperrow * sizeof(JSAMPLE));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000459 if (ltemp <= 0)
460 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
461 if (ltemp < (long) numrows)
462 rowsperchunk = (JDIMENSION) ltemp;
463 else
464 rowsperchunk = numrows;
465 mem->last_rowsperchunk = rowsperchunk;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000466
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000467 /* Get space for row pointers (small object) */
468 result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
DRC5de454b2014-05-18 19:04:03 +0000469 (size_t) (numrows * sizeof(JSAMPROW)));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000470
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000471 /* Get the rows themselves (large objects) */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000472 currow = 0;
473 while (currow < numrows) {
474 rowsperchunk = MIN(rowsperchunk, numrows - currow);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000475 workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
DRCe5eaf372014-05-09 18:00:32 +0000476 (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
DRC5de454b2014-05-18 19:04:03 +0000477 * sizeof(JSAMPLE)));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000478 for (i = rowsperchunk; i > 0; i--) {
479 result[currow++] = workspace;
480 workspace += samplesperrow;
481 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000482 }
483
484 return result;
485}
486
487
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000488/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000489 * Creation of 2-D coefficient-block arrays.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000490 * This is essentially the same as the code for sample arrays, above.
491 */
492
Thomas G. Lane489583f1996-02-07 00:00:00 +0000493METHODDEF(JBLOCKARRAY)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000494alloc_barray (j_common_ptr cinfo, int pool_id,
DRCe5eaf372014-05-09 18:00:32 +0000495 JDIMENSION blocksperrow, JDIMENSION numrows)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000496/* Allocate a 2-D coefficient-block array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000497{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000498 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000499 JBLOCKARRAY result;
500 JBLOCKROW workspace;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000501 JDIMENSION rowsperchunk, currow, i;
502 long ltemp;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000503
Pierre Ossman5557fd22009-03-09 10:34:53 +0000504 /* Make sure each row is properly aligned */
DRC5de454b2014-05-18 19:04:03 +0000505 if ((sizeof(JBLOCK) % ALIGN_SIZE) != 0)
DRCe5eaf372014-05-09 18:00:32 +0000506 out_of_memory(cinfo, 6); /* safety check */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000507
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000508 /* Calculate max # of rows allowed in one allocation chunk */
DRC5de454b2014-05-18 19:04:03 +0000509 ltemp = (MAX_ALLOC_CHUNK-sizeof(large_pool_hdr)) /
510 ((long) blocksperrow * sizeof(JBLOCK));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000511 if (ltemp <= 0)
512 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
513 if (ltemp < (long) numrows)
514 rowsperchunk = (JDIMENSION) ltemp;
515 else
516 rowsperchunk = numrows;
517 mem->last_rowsperchunk = rowsperchunk;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000518
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000519 /* Get space for row pointers (small object) */
520 result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
DRC5de454b2014-05-18 19:04:03 +0000521 (size_t) (numrows * sizeof(JBLOCKROW)));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000522
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000523 /* Get the rows themselves (large objects) */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000524 currow = 0;
525 while (currow < numrows) {
526 rowsperchunk = MIN(rowsperchunk, numrows - currow);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000527 workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
DRCe5eaf372014-05-09 18:00:32 +0000528 (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
DRC5de454b2014-05-18 19:04:03 +0000529 * sizeof(JBLOCK)));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000530 for (i = rowsperchunk; i > 0; i--) {
531 result[currow++] = workspace;
532 workspace += blocksperrow;
533 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000534 }
535
536 return result;
537}
538
539
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000540/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000541 * About virtual array management:
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000542 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000543 * The above "normal" array routines are only used to allocate strip buffers
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000544 * (as wide as the image, but just a few rows high). Full-image-sized buffers
545 * are handled as "virtual" arrays. The array is still accessed a strip at a
546 * time, but the memory manager must save the whole array for repeated
547 * accesses. The intended implementation is that there is a strip buffer in
548 * memory (as high as is possible given the desired memory limit), plus a
549 * backing file that holds the rest of the array.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000550 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000551 * The request_virt_array routines are told the total size of the image and
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000552 * the maximum number of rows that will be accessed at once. The in-memory
553 * buffer must be at least as large as the maxaccess value.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000554 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000555 * The request routines create control blocks but not the in-memory buffers.
556 * That is postponed until realize_virt_arrays is called. At that time the
557 * total amount of space needed is known (approximately, anyway), so free
558 * memory can be divided up fairly.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000559 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000560 * The access_virt_array routines are responsible for making a specific strip
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000561 * area accessible (after reading or writing the backing file, if necessary).
562 * Note that the access routines are told whether the caller intends to modify
563 * the accessed strip; during a read-only pass this saves having to rewrite
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000564 * data to disk. The access routines are also responsible for pre-zeroing
565 * any newly accessed rows, if pre-zeroing was requested.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000566 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000567 * In current usage, the access requests are usually for nonoverlapping
568 * strips; that is, successive access start_row numbers differ by exactly
569 * num_rows = maxaccess. This means we can get good performance with simple
570 * buffer dump/reload logic, by making the in-memory buffer be a multiple
571 * of the access height; then there will never be accesses across bufferload
572 * boundaries. The code will still work with overlapping access requests,
573 * but it doesn't handle bufferload overlaps very efficiently.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000574 */
575
576
Thomas G. Lane489583f1996-02-07 00:00:00 +0000577METHODDEF(jvirt_sarray_ptr)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000578request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
DRCe5eaf372014-05-09 18:00:32 +0000579 JDIMENSION samplesperrow, JDIMENSION numrows,
580 JDIMENSION maxaccess)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000581/* Request a virtual 2-D sample array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000582{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000583 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
584 jvirt_sarray_ptr result;
585
586 /* Only IMAGE-lifetime virtual arrays are currently supported */
587 if (pool_id != JPOOL_IMAGE)
DRCe5eaf372014-05-09 18:00:32 +0000588 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000589
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000590 /* get control block */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000591 result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
DRC5de454b2014-05-18 19:04:03 +0000592 sizeof(struct jvirt_sarray_control));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000593
DRCe5eaf372014-05-09 18:00:32 +0000594 result->mem_buffer = NULL; /* marks array not yet realized */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000595 result->rows_in_array = numrows;
596 result->samplesperrow = samplesperrow;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000597 result->maxaccess = maxaccess;
598 result->pre_zero = pre_zero;
DRCe5eaf372014-05-09 18:00:32 +0000599 result->b_s_open = FALSE; /* no associated backing-store object */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000600 result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
601 mem->virt_sarray_list = result;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000602
603 return result;
604}
605
606
Thomas G. Lane489583f1996-02-07 00:00:00 +0000607METHODDEF(jvirt_barray_ptr)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000608request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
DRCe5eaf372014-05-09 18:00:32 +0000609 JDIMENSION blocksperrow, JDIMENSION numrows,
610 JDIMENSION maxaccess)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000611/* Request a virtual 2-D coefficient-block array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000612{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000613 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
614 jvirt_barray_ptr result;
615
616 /* Only IMAGE-lifetime virtual arrays are currently supported */
617 if (pool_id != JPOOL_IMAGE)
DRCe5eaf372014-05-09 18:00:32 +0000618 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000619
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000620 /* get control block */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000621 result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
DRC5de454b2014-05-18 19:04:03 +0000622 sizeof(struct jvirt_barray_control));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000623
DRCe5eaf372014-05-09 18:00:32 +0000624 result->mem_buffer = NULL; /* marks array not yet realized */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000625 result->rows_in_array = numrows;
626 result->blocksperrow = blocksperrow;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000627 result->maxaccess = maxaccess;
628 result->pre_zero = pre_zero;
DRCe5eaf372014-05-09 18:00:32 +0000629 result->b_s_open = FALSE; /* no associated backing-store object */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000630 result->next = mem->virt_barray_list; /* add to list of virtual arrays */
631 mem->virt_barray_list = result;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000632
633 return result;
634}
635
636
Thomas G. Lane489583f1996-02-07 00:00:00 +0000637METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000638realize_virt_arrays (j_common_ptr cinfo)
639/* Allocate the in-memory buffers for any unrealized virtual arrays */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000640{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000641 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
DRC04899092010-02-26 23:01:19 +0000642 size_t space_per_minheight, maximum_space, avail_mem;
643 size_t minheights, max_minheights;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000644 jvirt_sarray_ptr sptr;
645 jvirt_barray_ptr bptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000646
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000647 /* Compute the minimum space needed (maxaccess rows in each buffer)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000648 * and the maximum space needed (full image height in each buffer).
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000649 * These may be of use to the system-dependent jpeg_mem_available routine.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000650 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000651 space_per_minheight = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000652 maximum_space = 0;
653 for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000654 if (sptr->mem_buffer == NULL) { /* if not realized yet */
DRCa09ba292016-09-07 16:40:10 -0500655 size_t new_space = (long) sptr->rows_in_array *
656 (long) sptr->samplesperrow * sizeof(JSAMPLE);
657
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000658 space_per_minheight += (long) sptr->maxaccess *
DRC5de454b2014-05-18 19:04:03 +0000659 (long) sptr->samplesperrow * sizeof(JSAMPLE);
DRCa09ba292016-09-07 16:40:10 -0500660 if (SIZE_MAX - maximum_space < new_space)
661 out_of_memory(cinfo, 10);
662 maximum_space += new_space;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000663 }
664 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000665 for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000666 if (bptr->mem_buffer == NULL) { /* if not realized yet */
DRCa09ba292016-09-07 16:40:10 -0500667 size_t new_space = (long) bptr->rows_in_array *
668 (long) bptr->blocksperrow * sizeof(JBLOCK);
669
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000670 space_per_minheight += (long) bptr->maxaccess *
DRC5de454b2014-05-18 19:04:03 +0000671 (long) bptr->blocksperrow * sizeof(JBLOCK);
DRCa09ba292016-09-07 16:40:10 -0500672 if (SIZE_MAX - maximum_space < new_space)
673 out_of_memory(cinfo, 11);
674 maximum_space += new_space;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000675 }
676 }
677
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000678 if (space_per_minheight <= 0)
DRCe5eaf372014-05-09 18:00:32 +0000679 return; /* no unrealized arrays, no work */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000680
681 /* Determine amount of memory to actually use; this is system-dependent. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000682 avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
DRCe5eaf372014-05-09 18:00:32 +0000683 mem->total_space_allocated);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000684
685 /* If the maximum space needed is available, make all the buffers full
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000686 * height; otherwise parcel it out with the same number of minheights
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000687 * in each buffer.
688 */
689 if (avail_mem >= maximum_space)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000690 max_minheights = 1000000000L;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000691 else {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000692 max_minheights = avail_mem / space_per_minheight;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000693 /* If there doesn't seem to be enough space, try to get the minimum
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000694 * anyway. This allows a "stub" implementation of jpeg_mem_available().
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000695 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000696 if (max_minheights <= 0)
697 max_minheights = 1;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000698 }
699
700 /* Allocate the in-memory buffers and initialize backing store as needed. */
701
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000702 for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000703 if (sptr->mem_buffer == NULL) { /* if not realized yet */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000704 minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
705 if (minheights <= max_minheights) {
DRCe5eaf372014-05-09 18:00:32 +0000706 /* This buffer fits in memory */
707 sptr->rows_in_mem = sptr->rows_in_array;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000708 } else {
DRCe5eaf372014-05-09 18:00:32 +0000709 /* It doesn't fit in memory, create backing store. */
710 sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
711 jpeg_open_backing_store(cinfo, & sptr->b_s_info,
712 (long) sptr->rows_in_array *
713 (long) sptr->samplesperrow *
DRC5de454b2014-05-18 19:04:03 +0000714 (long) sizeof(JSAMPLE));
DRCe5eaf372014-05-09 18:00:32 +0000715 sptr->b_s_open = TRUE;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000716 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000717 sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
DRCe5eaf372014-05-09 18:00:32 +0000718 sptr->samplesperrow, sptr->rows_in_mem);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000719 sptr->rowsperchunk = mem->last_rowsperchunk;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000720 sptr->cur_start_row = 0;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000721 sptr->first_undef_row = 0;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000722 sptr->dirty = FALSE;
723 }
724 }
725
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000726 for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000727 if (bptr->mem_buffer == NULL) { /* if not realized yet */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000728 minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
729 if (minheights <= max_minheights) {
DRCe5eaf372014-05-09 18:00:32 +0000730 /* This buffer fits in memory */
731 bptr->rows_in_mem = bptr->rows_in_array;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000732 } else {
DRCe5eaf372014-05-09 18:00:32 +0000733 /* It doesn't fit in memory, create backing store. */
734 bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
735 jpeg_open_backing_store(cinfo, & bptr->b_s_info,
736 (long) bptr->rows_in_array *
737 (long) bptr->blocksperrow *
DRC5de454b2014-05-18 19:04:03 +0000738 (long) sizeof(JBLOCK));
DRCe5eaf372014-05-09 18:00:32 +0000739 bptr->b_s_open = TRUE;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000740 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000741 bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
DRCe5eaf372014-05-09 18:00:32 +0000742 bptr->blocksperrow, bptr->rows_in_mem);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000743 bptr->rowsperchunk = mem->last_rowsperchunk;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000744 bptr->cur_start_row = 0;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000745 bptr->first_undef_row = 0;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000746 bptr->dirty = FALSE;
747 }
748 }
749}
750
751
Thomas G. Lane489583f1996-02-07 00:00:00 +0000752LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000753do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
754/* Do backing store read or write of a virtual sample array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000755{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000756 long bytesperrow, file_offset, byte_count, rows, thisrow, i;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000757
DRC5de454b2014-05-18 19:04:03 +0000758 bytesperrow = (long) ptr->samplesperrow * sizeof(JSAMPLE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000759 file_offset = ptr->cur_start_row * bytesperrow;
760 /* Loop to read or write each allocation chunk in mem_buffer */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000761 for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000762 /* One chunk, but check for short chunk at end of buffer */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000763 rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000764 /* Transfer no more than is currently defined */
765 thisrow = (long) ptr->cur_start_row + i;
766 rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000767 /* Transfer no more than fits in file */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000768 rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
DRCe5eaf372014-05-09 18:00:32 +0000769 if (rows <= 0) /* this chunk might be past end of file! */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000770 break;
771 byte_count = rows * bytesperrow;
772 if (writing)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000773 (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
DRC5033f3e2014-05-18 18:33:44 +0000774 (void *) ptr->mem_buffer[i],
DRCe5eaf372014-05-09 18:00:32 +0000775 file_offset, byte_count);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000776 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000777 (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
DRC5033f3e2014-05-18 18:33:44 +0000778 (void *) ptr->mem_buffer[i],
DRCe5eaf372014-05-09 18:00:32 +0000779 file_offset, byte_count);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000780 file_offset += byte_count;
781 }
782}
783
784
Thomas G. Lane489583f1996-02-07 00:00:00 +0000785LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000786do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
787/* Do backing store read or write of a virtual coefficient-block array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000788{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000789 long bytesperrow, file_offset, byte_count, rows, thisrow, i;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000790
DRC5de454b2014-05-18 19:04:03 +0000791 bytesperrow = (long) ptr->blocksperrow * sizeof(JBLOCK);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000792 file_offset = ptr->cur_start_row * bytesperrow;
793 /* Loop to read or write each allocation chunk in mem_buffer */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000794 for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000795 /* One chunk, but check for short chunk at end of buffer */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000796 rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000797 /* Transfer no more than is currently defined */
798 thisrow = (long) ptr->cur_start_row + i;
799 rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000800 /* Transfer no more than fits in file */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000801 rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
DRCe5eaf372014-05-09 18:00:32 +0000802 if (rows <= 0) /* this chunk might be past end of file! */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000803 break;
804 byte_count = rows * bytesperrow;
805 if (writing)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000806 (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
DRC5033f3e2014-05-18 18:33:44 +0000807 (void *) ptr->mem_buffer[i],
DRCe5eaf372014-05-09 18:00:32 +0000808 file_offset, byte_count);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000809 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000810 (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
DRC5033f3e2014-05-18 18:33:44 +0000811 (void *) ptr->mem_buffer[i],
DRCe5eaf372014-05-09 18:00:32 +0000812 file_offset, byte_count);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000813 file_offset += byte_count;
814 }
815}
816
817
Thomas G. Lane489583f1996-02-07 00:00:00 +0000818METHODDEF(JSAMPARRAY)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000819access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
DRCe5eaf372014-05-09 18:00:32 +0000820 JDIMENSION start_row, JDIMENSION num_rows,
821 boolean writable)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000822/* Access the part of a virtual sample array starting at start_row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000823/* and extending for num_rows rows. writable is true if */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000824/* caller intends to modify the accessed area. */
825{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000826 JDIMENSION end_row = start_row + num_rows;
827 JDIMENSION undef_row;
828
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000829 /* debugging check */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000830 if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
831 ptr->mem_buffer == NULL)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000832 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000833
834 /* Make the desired part of the virtual array accessible */
835 if (start_row < ptr->cur_start_row ||
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000836 end_row > ptr->cur_start_row+ptr->rows_in_mem) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000837 if (! ptr->b_s_open)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000838 ERREXIT(cinfo, JERR_VIRTUAL_BUG);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000839 /* Flush old buffer contents if necessary */
840 if (ptr->dirty) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000841 do_sarray_io(cinfo, ptr, TRUE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000842 ptr->dirty = FALSE;
843 }
844 /* Decide what part of virtual array to access.
845 * Algorithm: if target address > current window, assume forward scan,
846 * load starting at target address. If target address < current window,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000847 * assume backward scan, load so that target area is top of window.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000848 * Note that when switching from forward write to forward read, will have
849 * start_row = 0, so the limiting case applies and we load from 0 anyway.
850 */
851 if (start_row > ptr->cur_start_row) {
852 ptr->cur_start_row = start_row;
853 } else {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000854 /* use long arithmetic here to avoid overflow & unsigned problems */
855 long ltemp;
856
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000857 ltemp = (long) end_row - (long) ptr->rows_in_mem;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000858 if (ltemp < 0)
DRCe5eaf372014-05-09 18:00:32 +0000859 ltemp = 0; /* don't fall off front end of file */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000860 ptr->cur_start_row = (JDIMENSION) ltemp;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000861 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000862 /* Read in the selected part of the array.
863 * During the initial write pass, we will do no actual read
864 * because the selected part is all undefined.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000865 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000866 do_sarray_io(cinfo, ptr, FALSE);
867 }
868 /* Ensure the accessed part of the array is defined; prezero if needed.
869 * To improve locality of access, we only prezero the part of the array
870 * that the caller is about to access, not the entire in-memory array.
871 */
872 if (ptr->first_undef_row < end_row) {
873 if (ptr->first_undef_row < start_row) {
DRCe5eaf372014-05-09 18:00:32 +0000874 if (writable) /* writer skipped over a section of array */
875 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
876 undef_row = start_row; /* but reader is allowed to read ahead */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000877 } else {
878 undef_row = ptr->first_undef_row;
879 }
880 if (writable)
881 ptr->first_undef_row = end_row;
882 if (ptr->pre_zero) {
DRC5de454b2014-05-18 19:04:03 +0000883 size_t bytesperrow = (size_t) ptr->samplesperrow * sizeof(JSAMPLE);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000884 undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
885 end_row -= ptr->cur_start_row;
886 while (undef_row < end_row) {
DRC5033f3e2014-05-18 18:33:44 +0000887 jzero_far((void *) ptr->mem_buffer[undef_row], bytesperrow);
DRCe5eaf372014-05-09 18:00:32 +0000888 undef_row++;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000889 }
890 } else {
DRCe5eaf372014-05-09 18:00:32 +0000891 if (! writable) /* reader looking at undefined data */
892 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000893 }
894 }
895 /* Flag the buffer dirty if caller will write in it */
896 if (writable)
897 ptr->dirty = TRUE;
898 /* Return address of proper part of the buffer */
899 return ptr->mem_buffer + (start_row - ptr->cur_start_row);
900}
901
902
Thomas G. Lane489583f1996-02-07 00:00:00 +0000903METHODDEF(JBLOCKARRAY)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000904access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
DRCe5eaf372014-05-09 18:00:32 +0000905 JDIMENSION start_row, JDIMENSION num_rows,
906 boolean writable)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000907/* Access the part of a virtual block array starting at start_row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000908/* and extending for num_rows rows. writable is true if */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000909/* caller intends to modify the accessed area. */
910{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000911 JDIMENSION end_row = start_row + num_rows;
912 JDIMENSION undef_row;
913
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000914 /* debugging check */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000915 if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
916 ptr->mem_buffer == NULL)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000917 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000918
919 /* Make the desired part of the virtual array accessible */
920 if (start_row < ptr->cur_start_row ||
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000921 end_row > ptr->cur_start_row+ptr->rows_in_mem) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000922 if (! ptr->b_s_open)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000923 ERREXIT(cinfo, JERR_VIRTUAL_BUG);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000924 /* Flush old buffer contents if necessary */
925 if (ptr->dirty) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000926 do_barray_io(cinfo, ptr, TRUE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000927 ptr->dirty = FALSE;
928 }
929 /* Decide what part of virtual array to access.
930 * Algorithm: if target address > current window, assume forward scan,
931 * load starting at target address. If target address < current window,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000932 * assume backward scan, load so that target area is top of window.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000933 * Note that when switching from forward write to forward read, will have
934 * start_row = 0, so the limiting case applies and we load from 0 anyway.
935 */
936 if (start_row > ptr->cur_start_row) {
937 ptr->cur_start_row = start_row;
938 } else {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000939 /* use long arithmetic here to avoid overflow & unsigned problems */
940 long ltemp;
941
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000942 ltemp = (long) end_row - (long) ptr->rows_in_mem;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000943 if (ltemp < 0)
DRCe5eaf372014-05-09 18:00:32 +0000944 ltemp = 0; /* don't fall off front end of file */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000945 ptr->cur_start_row = (JDIMENSION) ltemp;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000946 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000947 /* Read in the selected part of the array.
948 * During the initial write pass, we will do no actual read
949 * because the selected part is all undefined.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000950 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000951 do_barray_io(cinfo, ptr, FALSE);
952 }
953 /* Ensure the accessed part of the array is defined; prezero if needed.
954 * To improve locality of access, we only prezero the part of the array
955 * that the caller is about to access, not the entire in-memory array.
956 */
957 if (ptr->first_undef_row < end_row) {
958 if (ptr->first_undef_row < start_row) {
DRCe5eaf372014-05-09 18:00:32 +0000959 if (writable) /* writer skipped over a section of array */
960 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
961 undef_row = start_row; /* but reader is allowed to read ahead */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000962 } else {
963 undef_row = ptr->first_undef_row;
964 }
965 if (writable)
966 ptr->first_undef_row = end_row;
967 if (ptr->pre_zero) {
DRC5de454b2014-05-18 19:04:03 +0000968 size_t bytesperrow = (size_t) ptr->blocksperrow * sizeof(JBLOCK);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000969 undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
970 end_row -= ptr->cur_start_row;
971 while (undef_row < end_row) {
DRC5033f3e2014-05-18 18:33:44 +0000972 jzero_far((void *) ptr->mem_buffer[undef_row], bytesperrow);
DRCe5eaf372014-05-09 18:00:32 +0000973 undef_row++;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000974 }
975 } else {
DRCe5eaf372014-05-09 18:00:32 +0000976 if (! writable) /* reader looking at undefined data */
977 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000978 }
979 }
980 /* Flag the buffer dirty if caller will write in it */
981 if (writable)
982 ptr->dirty = TRUE;
983 /* Return address of proper part of the buffer */
984 return ptr->mem_buffer + (start_row - ptr->cur_start_row);
985}
986
987
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000988/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000989 * Release all objects belonging to a specified pool.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000990 */
991
Thomas G. Lane489583f1996-02-07 00:00:00 +0000992METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000993free_pool (j_common_ptr cinfo, int pool_id)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000994{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000995 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
996 small_pool_ptr shdr_ptr;
997 large_pool_ptr lhdr_ptr;
998 size_t space_freed;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000999
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001000 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
DRCe5eaf372014-05-09 18:00:32 +00001001 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001002
1003#ifdef MEM_STATS
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001004 if (cinfo->err->trace_level > 1)
1005 print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001006#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001007
1008 /* If freeing IMAGE pool, close any virtual arrays first */
1009 if (pool_id == JPOOL_IMAGE) {
1010 jvirt_sarray_ptr sptr;
1011 jvirt_barray_ptr bptr;
1012
1013 for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
DRCe5eaf372014-05-09 18:00:32 +00001014 if (sptr->b_s_open) { /* there may be no backing store */
1015 sptr->b_s_open = FALSE; /* prevent recursive close if error */
1016 (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001017 }
1018 }
1019 mem->virt_sarray_list = NULL;
1020 for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
DRCe5eaf372014-05-09 18:00:32 +00001021 if (bptr->b_s_open) { /* there may be no backing store */
1022 bptr->b_s_open = FALSE; /* prevent recursive close if error */
1023 (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001024 }
1025 }
1026 mem->virt_barray_list = NULL;
1027 }
1028
1029 /* Release large objects */
1030 lhdr_ptr = mem->large_list[pool_id];
1031 mem->large_list[pool_id] = NULL;
1032
1033 while (lhdr_ptr != NULL) {
Pierre Ossman5557fd22009-03-09 10:34:53 +00001034 large_pool_ptr next_lhdr_ptr = lhdr_ptr->next;
1035 space_freed = lhdr_ptr->bytes_used +
DRCe5eaf372014-05-09 18:00:32 +00001036 lhdr_ptr->bytes_left +
DRC5de454b2014-05-18 19:04:03 +00001037 sizeof(large_pool_hdr);
DRC5033f3e2014-05-18 18:33:44 +00001038 jpeg_free_large(cinfo, (void *) lhdr_ptr, space_freed);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001039 mem->total_space_allocated -= space_freed;
1040 lhdr_ptr = next_lhdr_ptr;
1041 }
1042
1043 /* Release small objects */
1044 shdr_ptr = mem->small_list[pool_id];
1045 mem->small_list[pool_id] = NULL;
1046
1047 while (shdr_ptr != NULL) {
Pierre Ossman5557fd22009-03-09 10:34:53 +00001048 small_pool_ptr next_shdr_ptr = shdr_ptr->next;
1049 space_freed = shdr_ptr->bytes_used +
DRCe5eaf372014-05-09 18:00:32 +00001050 shdr_ptr->bytes_left +
DRC5de454b2014-05-18 19:04:03 +00001051 sizeof(small_pool_hdr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001052 jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
1053 mem->total_space_allocated -= space_freed;
1054 shdr_ptr = next_shdr_ptr;
1055 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001056}
1057
1058
1059/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001060 * Close up shop entirely.
1061 * Note that this cannot be called unless cinfo->mem is non-NULL.
1062 */
1063
Thomas G. Lane489583f1996-02-07 00:00:00 +00001064METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001065self_destruct (j_common_ptr cinfo)
1066{
1067 int pool;
1068
1069 /* Close all backing store, release all memory.
1070 * Releasing pools in reverse order might help avoid fragmentation
1071 * with some (brain-damaged) malloc libraries.
1072 */
1073 for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
1074 free_pool(cinfo, pool);
1075 }
1076
1077 /* Release the memory manager control block too. */
DRC5de454b2014-05-18 19:04:03 +00001078 jpeg_free_small(cinfo, (void *) cinfo->mem, sizeof(my_memory_mgr));
DRCe5eaf372014-05-09 18:00:32 +00001079 cinfo->mem = NULL; /* ensures I will be called only once */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001080
DRCe5eaf372014-05-09 18:00:32 +00001081 jpeg_mem_term(cinfo); /* system-dependent cleanup */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001082}
1083
1084
1085/*
1086 * Memory manager initialization.
1087 * When this is called, only the error manager pointer is valid in cinfo!
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001088 */
1089
Thomas G. Lane489583f1996-02-07 00:00:00 +00001090GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001091jinit_memory_mgr (j_common_ptr cinfo)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001092{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001093 my_mem_ptr mem;
1094 long max_to_use;
1095 int pool;
1096 size_t test_mac;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001097
DRCe5eaf372014-05-09 18:00:32 +00001098 cinfo->mem = NULL; /* for safety if init fails */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001099
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001100 /* Check for configuration errors.
DRC5de454b2014-05-18 19:04:03 +00001101 * sizeof(ALIGN_TYPE) should be a power of 2; otherwise, it probably
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001102 * doesn't reflect any real hardware alignment requirement.
1103 * The test is a little tricky: for X>0, X and X-1 have no one-bits
1104 * in common if and only if X is a power of 2, ie has only one one-bit.
1105 * Some compilers may give an "unreachable code" warning here; ignore it.
1106 */
Pierre Ossman5557fd22009-03-09 10:34:53 +00001107 if ((ALIGN_SIZE & (ALIGN_SIZE-1)) != 0)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001108 ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
1109 /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
Pierre Ossman5557fd22009-03-09 10:34:53 +00001110 * a multiple of ALIGN_SIZE.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001111 * Again, an "unreachable code" warning may be ignored here.
1112 * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
1113 */
1114 test_mac = (size_t) MAX_ALLOC_CHUNK;
1115 if ((long) test_mac != MAX_ALLOC_CHUNK ||
Pierre Ossman5557fd22009-03-09 10:34:53 +00001116 (MAX_ALLOC_CHUNK % ALIGN_SIZE) != 0)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001117 ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001118
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001119 max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */
1120
1121 /* Attempt to allocate memory manager's control block */
DRC5de454b2014-05-18 19:04:03 +00001122 mem = (my_mem_ptr) jpeg_get_small(cinfo, sizeof(my_memory_mgr));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001123
1124 if (mem == NULL) {
DRCe5eaf372014-05-09 18:00:32 +00001125 jpeg_mem_term(cinfo); /* system-dependent cleanup */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001126 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
1127 }
1128
1129 /* OK, fill in the method pointers */
1130 mem->pub.alloc_small = alloc_small;
1131 mem->pub.alloc_large = alloc_large;
1132 mem->pub.alloc_sarray = alloc_sarray;
1133 mem->pub.alloc_barray = alloc_barray;
1134 mem->pub.request_virt_sarray = request_virt_sarray;
1135 mem->pub.request_virt_barray = request_virt_barray;
1136 mem->pub.realize_virt_arrays = realize_virt_arrays;
1137 mem->pub.access_virt_sarray = access_virt_sarray;
1138 mem->pub.access_virt_barray = access_virt_barray;
1139 mem->pub.free_pool = free_pool;
1140 mem->pub.self_destruct = self_destruct;
1141
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00001142 /* Make MAX_ALLOC_CHUNK accessible to other modules */
1143 mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK;
1144
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001145 /* Initialize working state */
1146 mem->pub.max_memory_to_use = max_to_use;
1147
1148 for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
1149 mem->small_list[pool] = NULL;
1150 mem->large_list[pool] = NULL;
1151 }
1152 mem->virt_sarray_list = NULL;
1153 mem->virt_barray_list = NULL;
1154
DRC5de454b2014-05-18 19:04:03 +00001155 mem->total_space_allocated = sizeof(my_memory_mgr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001156
1157 /* Declare ourselves open for business */
1158 cinfo->mem = & mem->pub;
Thomas G. Lane88aeed41992-12-10 00:00:00 +00001159
1160 /* Check for an environment variable JPEGMEM; if found, override the
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001161 * default max_memory setting from jpeg_mem_init. Note that the
1162 * surrounding application may again override this value.
Thomas G. Lane88aeed41992-12-10 00:00:00 +00001163 * If your system doesn't support getenv(), define NO_GETENV to disable
1164 * this feature.
1165 */
1166#ifndef NO_GETENV
DRCbd498032016-02-19 08:53:33 -06001167 { char *memenv;
Thomas G. Lane88aeed41992-12-10 00:00:00 +00001168
1169 if ((memenv = getenv("JPEGMEM")) != NULL) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +00001170 char ch = 'x';
1171
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001172 if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
DRCe5eaf372014-05-09 18:00:32 +00001173 if (ch == 'm' || ch == 'M')
1174 max_to_use *= 1000L;
1175 mem->pub.max_memory_to_use = max_to_use * 1000L;
Thomas G. Lane88aeed41992-12-10 00:00:00 +00001176 }
1177 }
1178 }
1179#endif
1180
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001181}