blob: 8dfb633dae2fc05ecc1f5161c9f030e5fe7adb6e [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.
Alex Naidis6eb7d372016-10-16 23:10:08 +02006 * libjpeg-turbo Modifications:
7 * Copyright (C) 2016, D. R. Commander.
8 * 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 */
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -050035#ifndef _WIN32
Alex Naidis6eb7d372016-10-16 23:10:08 +020036#include <stdint.h>
Leon Scroggins IIIbd7903e2018-02-28 14:05:04 -050037#endif
DRCc8d146c2016-09-22 14:19:29 -050038#include <limits.h>
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000039
Thomas G. Lane88aeed41992-12-10 00:00:00 +000040#ifndef NO_GETENV
DRCe5eaf372014-05-09 18:00:32 +000041#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare getenv() */
Alex Naidis6eb7d372016-10-16 23:10:08 +020042extern char *getenv (const char *name);
Thomas G. Lane88aeed41992-12-10 00:00:00 +000043#endif
44#endif
45
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000046
DRCa8eabfe2011-03-29 04:58:40 +000047LOCAL(size_t)
48round_up_pow2 (size_t a, size_t b)
49/* a rounded up to the next multiple of b, i.e. ceil(a/b)*b */
50/* Assumes a >= 0, b > 0, and b is a power of 2 */
51{
52 return ((a + b - 1) & (~(b - 1)));
53}
54
55
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000056/*
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000057 * Some important notes:
58 * The allocation routines provided here must never return NULL.
59 * They should exit to error_exit if unsuccessful.
60 *
61 * It's not a good idea to try to merge the sarray and barray routines,
62 * even though they are textually almost the same, because samples are
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000063 * usually stored as bytes while coefficients are shorts or ints. Thus,
64 * in machines where byte pointers have a different representation from
65 * word pointers, the resulting machine code could not be the same.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000066 */
67
68
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000069/*
70 * Many machines require storage alignment: longs must start on 4-byte
71 * boundaries, doubles on 8-byte boundaries, etc. On such machines, malloc()
72 * always returns pointers that are multiples of the worst-case alignment
73 * requirement, and we had better do so too.
74 * There isn't any really portable way to determine the worst-case alignment
75 * requirement. This module assumes that the alignment requirement is
Pierre Ossman5557fd22009-03-09 10:34:53 +000076 * multiples of ALIGN_SIZE.
Alex Naidis6eb7d372016-10-16 23:10:08 +020077 * By default, we define ALIGN_SIZE as sizeof(double). This is necessary on
78 * some workstations (where doubles really do need 8-byte alignment) and will
79 * work fine on nearly everything. If your machine has lesser alignment needs,
Pierre Ossman5557fd22009-03-09 10:34:53 +000080 * you can save a few bytes by making ALIGN_SIZE smaller.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000081 * The only place I know of where this will NOT work is certain Macintosh
82 * 680x0 compilers that define double as a 10-byte IEEE extended float.
83 * Doing 10-byte alignment is counterproductive because longwords won't be
Pierre Ossman5557fd22009-03-09 10:34:53 +000084 * aligned well. Put "#define ALIGN_SIZE 4" in jconfig.h if you have
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000085 * such a compiler.
86 */
87
DRCe5eaf372014-05-09 18:00:32 +000088#ifndef ALIGN_SIZE /* so can override from jconfig.h */
Pierre Ossman73118302009-03-09 13:30:47 +000089#ifndef WITH_SIMD
DRC5de454b2014-05-18 19:04:03 +000090#define ALIGN_SIZE sizeof(double)
Pierre Ossman73118302009-03-09 13:30:47 +000091#else
92#define ALIGN_SIZE 16 /* Most SIMD implementations require this */
93#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000094#endif
95
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000096/*
97 * We allocate objects from "pools", where each pool is gotten with a single
98 * request to jpeg_get_small() or jpeg_get_large(). There is no per-object
99 * overhead within a pool, except for alignment padding. Each pool has a
100 * header with a link to the next pool of the same class.
DRC5033f3e2014-05-18 18:33:44 +0000101 * Small and large pool headers are identical.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000102 */
103
Alex Naidis6eb7d372016-10-16 23:10:08 +0200104typedef struct small_pool_struct *small_pool_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000105
Pierre Ossman5557fd22009-03-09 10:34:53 +0000106typedef struct small_pool_struct {
DRCe5eaf372014-05-09 18:00:32 +0000107 small_pool_ptr next; /* next in list of pools */
108 size_t bytes_used; /* how many bytes already used within pool */
109 size_t bytes_left; /* bytes still available in this pool */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000110} small_pool_hdr;
111
Alex Naidis6eb7d372016-10-16 23:10:08 +0200112typedef struct large_pool_struct *large_pool_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000113
Pierre Ossman5557fd22009-03-09 10:34:53 +0000114typedef struct large_pool_struct {
DRCe5eaf372014-05-09 18:00:32 +0000115 large_pool_ptr next; /* next in list of pools */
116 size_t bytes_used; /* how many bytes already used within pool */
117 size_t bytes_left; /* bytes still available in this pool */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000118} large_pool_hdr;
119
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000120/*
121 * Here is the full definition of a memory manager object.
122 */
123
124typedef struct {
DRCe5eaf372014-05-09 18:00:32 +0000125 struct jpeg_memory_mgr pub; /* public fields */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000126
127 /* Each pool identifier (lifetime class) names a linked list of pools. */
128 small_pool_ptr small_list[JPOOL_NUMPOOLS];
129 large_pool_ptr large_list[JPOOL_NUMPOOLS];
130
131 /* Since we only have one lifetime class of virtual arrays, only one
132 * linked list is necessary (for each datatype). Note that the virtual
133 * array control blocks being linked together are actually stored somewhere
134 * in the small-pool list.
135 */
136 jvirt_sarray_ptr virt_sarray_list;
137 jvirt_barray_ptr virt_barray_list;
138
139 /* This counts total space obtained from jpeg_get_small/large */
DRC04899092010-02-26 23:01:19 +0000140 size_t total_space_allocated;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000141
142 /* alloc_sarray and alloc_barray set this value for use by virtual
143 * array routines.
144 */
DRCe5eaf372014-05-09 18:00:32 +0000145 JDIMENSION last_rowsperchunk; /* from most recent alloc_sarray/barray */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000146} my_memory_mgr;
147
Alex Naidis6eb7d372016-10-16 23:10:08 +0200148typedef my_memory_mgr *my_mem_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000149
150
151/*
152 * The control blocks for virtual arrays.
153 * Note that these blocks are allocated in the "small" pool area.
154 * System-dependent info for the associated backing store (if any) is hidden
155 * inside the backing_store_info struct.
156 */
157
158struct jvirt_sarray_control {
DRCe5eaf372014-05-09 18:00:32 +0000159 JSAMPARRAY mem_buffer; /* => the in-memory buffer */
160 JDIMENSION rows_in_array; /* total virtual array height */
161 JDIMENSION samplesperrow; /* width of array (and of memory buffer) */
162 JDIMENSION maxaccess; /* max rows accessed by access_virt_sarray */
163 JDIMENSION rows_in_mem; /* height of memory buffer */
164 JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
165 JDIMENSION cur_start_row; /* first logical row # in the buffer */
166 JDIMENSION first_undef_row; /* row # of first uninitialized row */
167 boolean pre_zero; /* pre-zero mode requested? */
168 boolean dirty; /* do current buffer contents need written? */
169 boolean b_s_open; /* is backing-store data valid? */
170 jvirt_sarray_ptr next; /* link to next virtual sarray control block */
171 backing_store_info b_s_info; /* System-dependent control info */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000172};
173
174struct jvirt_barray_control {
DRCe5eaf372014-05-09 18:00:32 +0000175 JBLOCKARRAY mem_buffer; /* => the in-memory buffer */
176 JDIMENSION rows_in_array; /* total virtual array height */
177 JDIMENSION blocksperrow; /* width of array (and of memory buffer) */
178 JDIMENSION maxaccess; /* max rows accessed by access_virt_barray */
179 JDIMENSION rows_in_mem; /* height of memory buffer */
180 JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
181 JDIMENSION cur_start_row; /* first logical row # in the buffer */
182 JDIMENSION first_undef_row; /* row # of first uninitialized row */
183 boolean pre_zero; /* pre-zero mode requested? */
184 boolean dirty; /* do current buffer contents need written? */
185 boolean b_s_open; /* is backing-store data valid? */
186 jvirt_barray_ptr next; /* link to next virtual barray control block */
187 backing_store_info b_s_info; /* System-dependent control info */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000188};
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000189
190
DRCe5eaf372014-05-09 18:00:32 +0000191#ifdef MEM_STATS /* optional extra stuff for statistics */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000192
Thomas G. Lane489583f1996-02-07 00:00:00 +0000193LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000194print_mem_stats (j_common_ptr cinfo, int pool_id)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000195{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000196 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
197 small_pool_ptr shdr_ptr;
198 large_pool_ptr lhdr_ptr;
199
200 /* Since this is only a debugging stub, we can cheat a little by using
201 * fprintf directly rather than going through the trace message code.
202 * This is helpful because message parm array can't handle longs.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000203 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000204 fprintf(stderr, "Freeing pool %d, total space = %ld\n",
DRCe5eaf372014-05-09 18:00:32 +0000205 pool_id, mem->total_space_allocated);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000206
207 for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000208 lhdr_ptr = lhdr_ptr->next) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000209 fprintf(stderr, " Large chunk used %ld\n",
DRCe5eaf372014-05-09 18:00:32 +0000210 (long) lhdr_ptr->bytes_used);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000211 }
212
213 for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000214 shdr_ptr = shdr_ptr->next) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000215 fprintf(stderr, " Small chunk used %ld free %ld\n",
DRCe5eaf372014-05-09 18:00:32 +0000216 (long) shdr_ptr->bytes_used,
217 (long) shdr_ptr->bytes_left);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000218 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000219}
220
221#endif /* MEM_STATS */
222
223
Thomas G. Lane489583f1996-02-07 00:00:00 +0000224LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000225out_of_memory (j_common_ptr cinfo, int which)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000226/* Report an out-of-memory error and stop execution */
227/* If we compiled MEM_STATS support, report alloc requests before dying */
228{
229#ifdef MEM_STATS
DRCe5eaf372014-05-09 18:00:32 +0000230 cinfo->err->trace_level = 2; /* force self_destruct to report stats */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000231#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000232 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000233}
234
235
236/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000237 * Allocation of "small" objects.
238 *
239 * For these, we use pooled storage. When a new pool must be created,
240 * we try to get enough space for the current request plus a "slop" factor,
241 * where the slop will be the amount of leftover space in the new pool.
242 * The speed vs. space tradeoff is largely determined by the slop values.
243 * A different slop value is provided for each pool class (lifetime),
244 * and we also distinguish the first pool of a class from later ones.
245 * NOTE: the values given work fairly well on both 16- and 32-bit-int
246 * machines, but may be too small if longs are 64 bits or more.
Pierre Ossman5557fd22009-03-09 10:34:53 +0000247 *
248 * Since we do not know what alignment malloc() gives us, we have to
249 * allocate ALIGN_SIZE-1 extra space per pool to have room for alignment
250 * adjustment.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000251 */
252
DRCe5eaf372014-05-09 18:00:32 +0000253static const size_t first_pool_slop[JPOOL_NUMPOOLS] =
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000254{
DRCe5eaf372014-05-09 18:00:32 +0000255 1600, /* first PERMANENT pool */
256 16000 /* first IMAGE pool */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000257};
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000258
DRCe5eaf372014-05-09 18:00:32 +0000259static const size_t extra_pool_slop[JPOOL_NUMPOOLS] =
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000260{
DRCe5eaf372014-05-09 18:00:32 +0000261 0, /* additional PERMANENT pools */
262 5000 /* additional IMAGE pools */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000263};
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000264
DRCe5eaf372014-05-09 18:00:32 +0000265#define MIN_SLOP 50 /* greater than 0 to avoid futile looping */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000266
267
Thomas G. Lane489583f1996-02-07 00:00:00 +0000268METHODDEF(void *)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000269alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000270/* Allocate a "small" object */
271{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000272 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
273 small_pool_ptr hdr_ptr, prev_hdr_ptr;
Alex Naidis6eb7d372016-10-16 23:10:08 +0200274 char *data_ptr;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000275 size_t min_request, slop;
276
277 /*
278 * Round up the requested size to a multiple of ALIGN_SIZE in order
279 * to assure alignment for the next object allocated in the same pool
280 * and so that algorithms can straddle outside the proper area up
281 * to the next alignment.
282 */
Alex Naidis6eb7d372016-10-16 23:10:08 +0200283 if (sizeofobject > MAX_ALLOC_CHUNK) {
284 /* This prevents overflow/wrap-around in round_up_pow2() if sizeofobject
285 is close to SIZE_MAX. */
286 out_of_memory(cinfo, 7);
287 }
DRCa8eabfe2011-03-29 04:58:40 +0000288 sizeofobject = round_up_pow2(sizeofobject, ALIGN_SIZE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000289
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000290 /* Check for unsatisfiable request (do now to ensure no overflow below) */
Alex Naidis6eb7d372016-10-16 23:10:08 +0200291 if ((sizeof(small_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) >
292 MAX_ALLOC_CHUNK)
DRCe5eaf372014-05-09 18:00:32 +0000293 out_of_memory(cinfo, 1); /* request exceeds malloc's ability */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000294
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000295 /* See if space is available in any existing pool */
296 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
DRCe5eaf372014-05-09 18:00:32 +0000297 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000298 prev_hdr_ptr = NULL;
299 hdr_ptr = mem->small_list[pool_id];
300 while (hdr_ptr != NULL) {
Pierre Ossman5557fd22009-03-09 10:34:53 +0000301 if (hdr_ptr->bytes_left >= sizeofobject)
DRCe5eaf372014-05-09 18:00:32 +0000302 break; /* found pool with enough space */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000303 prev_hdr_ptr = hdr_ptr;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000304 hdr_ptr = hdr_ptr->next;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000305 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000306
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000307 /* Time to make a new pool? */
308 if (hdr_ptr == NULL) {
309 /* min_request is what we need now, slop is what will be leftover */
DRC5de454b2014-05-18 19:04:03 +0000310 min_request = sizeof(small_pool_hdr) + sizeofobject + ALIGN_SIZE - 1;
DRCe5eaf372014-05-09 18:00:32 +0000311 if (prev_hdr_ptr == NULL) /* first pool in class? */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000312 slop = first_pool_slop[pool_id];
313 else
314 slop = extra_pool_slop[pool_id];
315 /* Don't ask for more than MAX_ALLOC_CHUNK */
316 if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
317 slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
318 /* Try to get space, if fail reduce slop and try again */
319 for (;;) {
320 hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
321 if (hdr_ptr != NULL)
DRCe5eaf372014-05-09 18:00:32 +0000322 break;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000323 slop /= 2;
DRCe5eaf372014-05-09 18:00:32 +0000324 if (slop < MIN_SLOP) /* give up when it gets real small */
325 out_of_memory(cinfo, 2); /* jpeg_get_small failed */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000326 }
327 mem->total_space_allocated += min_request + slop;
328 /* Success, initialize the new pool header and add to end of list */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000329 hdr_ptr->next = NULL;
330 hdr_ptr->bytes_used = 0;
331 hdr_ptr->bytes_left = sizeofobject + slop;
DRCe5eaf372014-05-09 18:00:32 +0000332 if (prev_hdr_ptr == NULL) /* first pool in class? */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000333 mem->small_list[pool_id] = hdr_ptr;
334 else
Pierre Ossman5557fd22009-03-09 10:34:53 +0000335 prev_hdr_ptr->next = hdr_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000336 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000337
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000338 /* OK, allocate the object from the current pool */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000339 data_ptr = (char *) hdr_ptr; /* point to first data byte in pool... */
DRC5de454b2014-05-18 19:04:03 +0000340 data_ptr += sizeof(small_pool_hdr); /* ...by skipping the header... */
DRC04899092010-02-26 23:01:19 +0000341 if ((size_t)data_ptr % ALIGN_SIZE) /* ...and adjust for alignment */
342 data_ptr += ALIGN_SIZE - (size_t)data_ptr % ALIGN_SIZE;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000343 data_ptr += hdr_ptr->bytes_used; /* point to place for object */
344 hdr_ptr->bytes_used += sizeofobject;
345 hdr_ptr->bytes_left -= sizeofobject;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000346
347 return (void *) data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000348}
349
350
351/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000352 * Allocation of "large" objects.
353 *
DRC5033f3e2014-05-18 18:33:44 +0000354 * The external semantics of these are the same as "small" objects. However,
355 * the pool management heuristics are quite different. We assume that each
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000356 * request is large enough that it may as well be passed directly to
357 * jpeg_get_large; the pool management just links everything together
358 * so that we can free it all on demand.
359 * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY
360 * structures. The routines that create these structures (see below)
361 * deliberately bunch rows together to ensure a large request size.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000362 */
363
DRC5033f3e2014-05-18 18:33:44 +0000364METHODDEF(void *)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000365alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
366/* Allocate a "large" object */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000367{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000368 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
369 large_pool_ptr hdr_ptr;
Alex Naidis6eb7d372016-10-16 23:10:08 +0200370 char *data_ptr;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000371
372 /*
373 * Round up the requested size to a multiple of ALIGN_SIZE so that
374 * algorithms can straddle outside the proper area up to the next
375 * alignment.
376 */
Alex Naidis6eb7d372016-10-16 23:10:08 +0200377 if (sizeofobject > MAX_ALLOC_CHUNK) {
378 /* This prevents overflow/wrap-around in round_up_pow2() if sizeofobject
379 is close to SIZE_MAX. */
380 out_of_memory(cinfo, 8);
381 }
DRCa8eabfe2011-03-29 04:58:40 +0000382 sizeofobject = round_up_pow2(sizeofobject, ALIGN_SIZE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000383
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000384 /* Check for unsatisfiable request (do now to ensure no overflow below) */
Alex Naidis6eb7d372016-10-16 23:10:08 +0200385 if ((sizeof(large_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) >
386 MAX_ALLOC_CHUNK)
DRCe5eaf372014-05-09 18:00:32 +0000387 out_of_memory(cinfo, 3); /* request exceeds malloc's ability */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000388
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000389 /* Always make a new pool */
390 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
DRCe5eaf372014-05-09 18:00:32 +0000391 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000392
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000393 hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
DRC5de454b2014-05-18 19:04:03 +0000394 sizeof(large_pool_hdr) +
DRCe5eaf372014-05-09 18:00:32 +0000395 ALIGN_SIZE - 1);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000396 if (hdr_ptr == NULL)
DRCe5eaf372014-05-09 18:00:32 +0000397 out_of_memory(cinfo, 4); /* jpeg_get_large failed */
Alex Naidis6eb7d372016-10-16 23:10:08 +0200398 mem->total_space_allocated += sizeofobject + sizeof(large_pool_hdr) +
399 ALIGN_SIZE - 1;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000400
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000401 /* Success, initialize the new pool header and add to list */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000402 hdr_ptr->next = mem->large_list[pool_id];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000403 /* We maintain space counts in each pool header for statistical purposes,
404 * even though they are not needed for allocation.
405 */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000406 hdr_ptr->bytes_used = sizeofobject;
407 hdr_ptr->bytes_left = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000408 mem->large_list[pool_id] = hdr_ptr;
409
Pierre Ossman5557fd22009-03-09 10:34:53 +0000410 data_ptr = (char *) hdr_ptr; /* point to first data byte in pool... */
DRC5de454b2014-05-18 19:04:03 +0000411 data_ptr += sizeof(small_pool_hdr); /* ...by skipping the header... */
DRC04899092010-02-26 23:01:19 +0000412 if ((size_t)data_ptr % ALIGN_SIZE) /* ...and adjust for alignment */
413 data_ptr += ALIGN_SIZE - (size_t)data_ptr % ALIGN_SIZE;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000414
DRC5033f3e2014-05-18 18:33:44 +0000415 return (void *) data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000416}
417
418
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000419/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000420 * Creation of 2-D sample arrays.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000421 *
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000422 * To minimize allocation overhead and to allow I/O of large contiguous
423 * blocks, we allocate the sample rows in groups of as many rows as possible
424 * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000425 * NB: the virtual array control routines, later in this file, know about
426 * this chunking of rows. The rowsperchunk value is left in the mem manager
427 * object so that it can be saved away if this sarray is the workspace for
428 * a virtual array.
Pierre Ossman5557fd22009-03-09 10:34:53 +0000429 *
430 * Since we are often upsampling with a factor 2, we align the size (not
431 * the start) to 2 * ALIGN_SIZE so that the upsampling routines don't have
432 * to be as careful about size.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000433 */
434
Thomas G. Lane489583f1996-02-07 00:00:00 +0000435METHODDEF(JSAMPARRAY)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000436alloc_sarray (j_common_ptr cinfo, int pool_id,
DRCe5eaf372014-05-09 18:00:32 +0000437 JDIMENSION samplesperrow, JDIMENSION numrows)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000438/* Allocate a 2-D sample array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000439{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000440 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000441 JSAMPARRAY result;
442 JSAMPROW workspace;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000443 JDIMENSION rowsperchunk, currow, i;
444 long ltemp;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000445
Pierre Ossman5557fd22009-03-09 10:34:53 +0000446 /* Make sure each row is properly aligned */
DRC5de454b2014-05-18 19:04:03 +0000447 if ((ALIGN_SIZE % sizeof(JSAMPLE)) != 0)
DRCe5eaf372014-05-09 18:00:32 +0000448 out_of_memory(cinfo, 5); /* safety check */
Alex Naidis6eb7d372016-10-16 23:10:08 +0200449
450 if (samplesperrow > MAX_ALLOC_CHUNK) {
451 /* This prevents overflow/wrap-around in round_up_pow2() if sizeofobject
452 is close to SIZE_MAX. */
453 out_of_memory(cinfo, 9);
454 }
455 samplesperrow = (JDIMENSION)round_up_pow2(samplesperrow, (2 * ALIGN_SIZE) /
456 sizeof(JSAMPLE));
Pierre Ossman5557fd22009-03-09 10:34:53 +0000457
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000458 /* Calculate max # of rows allowed in one allocation chunk */
DRC5de454b2014-05-18 19:04:03 +0000459 ltemp = (MAX_ALLOC_CHUNK-sizeof(large_pool_hdr)) /
460 ((long) samplesperrow * sizeof(JSAMPLE));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000461 if (ltemp <= 0)
462 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
463 if (ltemp < (long) numrows)
464 rowsperchunk = (JDIMENSION) ltemp;
465 else
466 rowsperchunk = numrows;
467 mem->last_rowsperchunk = rowsperchunk;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000468
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000469 /* Get space for row pointers (small object) */
470 result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
DRC5de454b2014-05-18 19:04:03 +0000471 (size_t) (numrows * sizeof(JSAMPROW)));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000472
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000473 /* Get the rows themselves (large objects) */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000474 currow = 0;
475 while (currow < numrows) {
476 rowsperchunk = MIN(rowsperchunk, numrows - currow);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000477 workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
DRCe5eaf372014-05-09 18:00:32 +0000478 (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
DRC5de454b2014-05-18 19:04:03 +0000479 * sizeof(JSAMPLE)));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000480 for (i = rowsperchunk; i > 0; i--) {
481 result[currow++] = workspace;
482 workspace += samplesperrow;
483 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000484 }
485
486 return result;
487}
488
489
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000490/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000491 * Creation of 2-D coefficient-block arrays.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000492 * This is essentially the same as the code for sample arrays, above.
493 */
494
Thomas G. Lane489583f1996-02-07 00:00:00 +0000495METHODDEF(JBLOCKARRAY)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000496alloc_barray (j_common_ptr cinfo, int pool_id,
DRCe5eaf372014-05-09 18:00:32 +0000497 JDIMENSION blocksperrow, JDIMENSION numrows)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000498/* Allocate a 2-D coefficient-block array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000499{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000500 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000501 JBLOCKARRAY result;
502 JBLOCKROW workspace;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000503 JDIMENSION rowsperchunk, currow, i;
504 long ltemp;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000505
Pierre Ossman5557fd22009-03-09 10:34:53 +0000506 /* Make sure each row is properly aligned */
DRC5de454b2014-05-18 19:04:03 +0000507 if ((sizeof(JBLOCK) % ALIGN_SIZE) != 0)
DRCe5eaf372014-05-09 18:00:32 +0000508 out_of_memory(cinfo, 6); /* safety check */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000509
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000510 /* Calculate max # of rows allowed in one allocation chunk */
DRC5de454b2014-05-18 19:04:03 +0000511 ltemp = (MAX_ALLOC_CHUNK-sizeof(large_pool_hdr)) /
512 ((long) blocksperrow * sizeof(JBLOCK));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000513 if (ltemp <= 0)
514 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
515 if (ltemp < (long) numrows)
516 rowsperchunk = (JDIMENSION) ltemp;
517 else
518 rowsperchunk = numrows;
519 mem->last_rowsperchunk = rowsperchunk;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000520
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000521 /* Get space for row pointers (small object) */
522 result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
DRC5de454b2014-05-18 19:04:03 +0000523 (size_t) (numrows * sizeof(JBLOCKROW)));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000524
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000525 /* Get the rows themselves (large objects) */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000526 currow = 0;
527 while (currow < numrows) {
528 rowsperchunk = MIN(rowsperchunk, numrows - currow);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000529 workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
DRCe5eaf372014-05-09 18:00:32 +0000530 (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
DRC5de454b2014-05-18 19:04:03 +0000531 * sizeof(JBLOCK)));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000532 for (i = rowsperchunk; i > 0; i--) {
533 result[currow++] = workspace;
534 workspace += blocksperrow;
535 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000536 }
537
538 return result;
539}
540
541
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000542/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000543 * About virtual array management:
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000544 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000545 * The above "normal" array routines are only used to allocate strip buffers
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000546 * (as wide as the image, but just a few rows high). Full-image-sized buffers
547 * are handled as "virtual" arrays. The array is still accessed a strip at a
548 * time, but the memory manager must save the whole array for repeated
549 * accesses. The intended implementation is that there is a strip buffer in
550 * memory (as high as is possible given the desired memory limit), plus a
551 * backing file that holds the rest of the array.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000552 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000553 * The request_virt_array routines are told the total size of the image and
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000554 * the maximum number of rows that will be accessed at once. The in-memory
555 * buffer must be at least as large as the maxaccess value.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000556 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000557 * The request routines create control blocks but not the in-memory buffers.
558 * That is postponed until realize_virt_arrays is called. At that time the
559 * total amount of space needed is known (approximately, anyway), so free
560 * memory can be divided up fairly.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000561 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000562 * The access_virt_array routines are responsible for making a specific strip
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000563 * area accessible (after reading or writing the backing file, if necessary).
564 * Note that the access routines are told whether the caller intends to modify
565 * the accessed strip; during a read-only pass this saves having to rewrite
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000566 * data to disk. The access routines are also responsible for pre-zeroing
567 * any newly accessed rows, if pre-zeroing was requested.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000568 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000569 * In current usage, the access requests are usually for nonoverlapping
570 * strips; that is, successive access start_row numbers differ by exactly
571 * num_rows = maxaccess. This means we can get good performance with simple
572 * buffer dump/reload logic, by making the in-memory buffer be a multiple
573 * of the access height; then there will never be accesses across bufferload
574 * boundaries. The code will still work with overlapping access requests,
575 * but it doesn't handle bufferload overlaps very efficiently.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000576 */
577
578
Thomas G. Lane489583f1996-02-07 00:00:00 +0000579METHODDEF(jvirt_sarray_ptr)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000580request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
DRCe5eaf372014-05-09 18:00:32 +0000581 JDIMENSION samplesperrow, JDIMENSION numrows,
582 JDIMENSION maxaccess)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000583/* Request a virtual 2-D sample array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000584{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000585 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
586 jvirt_sarray_ptr result;
587
588 /* Only IMAGE-lifetime virtual arrays are currently supported */
589 if (pool_id != JPOOL_IMAGE)
DRCe5eaf372014-05-09 18:00:32 +0000590 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000591
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000592 /* get control block */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000593 result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
DRC5de454b2014-05-18 19:04:03 +0000594 sizeof(struct jvirt_sarray_control));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000595
DRCe5eaf372014-05-09 18:00:32 +0000596 result->mem_buffer = NULL; /* marks array not yet realized */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000597 result->rows_in_array = numrows;
598 result->samplesperrow = samplesperrow;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000599 result->maxaccess = maxaccess;
600 result->pre_zero = pre_zero;
DRCe5eaf372014-05-09 18:00:32 +0000601 result->b_s_open = FALSE; /* no associated backing-store object */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000602 result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
603 mem->virt_sarray_list = result;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000604
605 return result;
606}
607
608
Thomas G. Lane489583f1996-02-07 00:00:00 +0000609METHODDEF(jvirt_barray_ptr)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000610request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
DRCe5eaf372014-05-09 18:00:32 +0000611 JDIMENSION blocksperrow, JDIMENSION numrows,
612 JDIMENSION maxaccess)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000613/* Request a virtual 2-D coefficient-block array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000614{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000615 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
616 jvirt_barray_ptr result;
617
618 /* Only IMAGE-lifetime virtual arrays are currently supported */
619 if (pool_id != JPOOL_IMAGE)
DRCe5eaf372014-05-09 18:00:32 +0000620 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000621
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000622 /* get control block */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000623 result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
DRC5de454b2014-05-18 19:04:03 +0000624 sizeof(struct jvirt_barray_control));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000625
DRCe5eaf372014-05-09 18:00:32 +0000626 result->mem_buffer = NULL; /* marks array not yet realized */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000627 result->rows_in_array = numrows;
628 result->blocksperrow = blocksperrow;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000629 result->maxaccess = maxaccess;
630 result->pre_zero = pre_zero;
DRCe5eaf372014-05-09 18:00:32 +0000631 result->b_s_open = FALSE; /* no associated backing-store object */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000632 result->next = mem->virt_barray_list; /* add to list of virtual arrays */
633 mem->virt_barray_list = result;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000634
635 return result;
636}
637
638
Thomas G. Lane489583f1996-02-07 00:00:00 +0000639METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000640realize_virt_arrays (j_common_ptr cinfo)
641/* Allocate the in-memory buffers for any unrealized virtual arrays */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000642{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000643 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
DRC04899092010-02-26 23:01:19 +0000644 size_t space_per_minheight, maximum_space, avail_mem;
645 size_t minheights, max_minheights;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000646 jvirt_sarray_ptr sptr;
647 jvirt_barray_ptr bptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000648
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000649 /* Compute the minimum space needed (maxaccess rows in each buffer)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000650 * and the maximum space needed (full image height in each buffer).
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000651 * These may be of use to the system-dependent jpeg_mem_available routine.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000652 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000653 space_per_minheight = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000654 maximum_space = 0;
655 for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000656 if (sptr->mem_buffer == NULL) { /* if not realized yet */
Alex Naidis6eb7d372016-10-16 23:10:08 +0200657 size_t new_space = (long) sptr->rows_in_array *
658 (long) sptr->samplesperrow * sizeof(JSAMPLE);
659
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000660 space_per_minheight += (long) sptr->maxaccess *
DRC5de454b2014-05-18 19:04:03 +0000661 (long) sptr->samplesperrow * sizeof(JSAMPLE);
Alex Naidis6eb7d372016-10-16 23:10:08 +0200662 if (SIZE_MAX - maximum_space < new_space)
663 out_of_memory(cinfo, 10);
664 maximum_space += new_space;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000665 }
666 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000667 for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000668 if (bptr->mem_buffer == NULL) { /* if not realized yet */
Alex Naidis6eb7d372016-10-16 23:10:08 +0200669 size_t new_space = (long) bptr->rows_in_array *
670 (long) bptr->blocksperrow * sizeof(JBLOCK);
671
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000672 space_per_minheight += (long) bptr->maxaccess *
DRC5de454b2014-05-18 19:04:03 +0000673 (long) bptr->blocksperrow * sizeof(JBLOCK);
Alex Naidis6eb7d372016-10-16 23:10:08 +0200674 if (SIZE_MAX - maximum_space < new_space)
675 out_of_memory(cinfo, 11);
676 maximum_space += new_space;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000677 }
678 }
679
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000680 if (space_per_minheight <= 0)
DRCe5eaf372014-05-09 18:00:32 +0000681 return; /* no unrealized arrays, no work */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000682
683 /* Determine amount of memory to actually use; this is system-dependent. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000684 avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
DRCe5eaf372014-05-09 18:00:32 +0000685 mem->total_space_allocated);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000686
687 /* If the maximum space needed is available, make all the buffers full
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000688 * height; otherwise parcel it out with the same number of minheights
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000689 * in each buffer.
690 */
691 if (avail_mem >= maximum_space)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000692 max_minheights = 1000000000L;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000693 else {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000694 max_minheights = avail_mem / space_per_minheight;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000695 /* If there doesn't seem to be enough space, try to get the minimum
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000696 * anyway. This allows a "stub" implementation of jpeg_mem_available().
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000697 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000698 if (max_minheights <= 0)
699 max_minheights = 1;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000700 }
701
702 /* Allocate the in-memory buffers and initialize backing store as needed. */
703
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000704 for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000705 if (sptr->mem_buffer == NULL) { /* if not realized yet */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000706 minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
707 if (minheights <= max_minheights) {
DRCe5eaf372014-05-09 18:00:32 +0000708 /* This buffer fits in memory */
709 sptr->rows_in_mem = sptr->rows_in_array;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000710 } else {
DRCe5eaf372014-05-09 18:00:32 +0000711 /* It doesn't fit in memory, create backing store. */
712 sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
713 jpeg_open_backing_store(cinfo, & sptr->b_s_info,
714 (long) sptr->rows_in_array *
715 (long) sptr->samplesperrow *
DRC5de454b2014-05-18 19:04:03 +0000716 (long) sizeof(JSAMPLE));
DRCe5eaf372014-05-09 18:00:32 +0000717 sptr->b_s_open = TRUE;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000718 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000719 sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
DRCe5eaf372014-05-09 18:00:32 +0000720 sptr->samplesperrow, sptr->rows_in_mem);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000721 sptr->rowsperchunk = mem->last_rowsperchunk;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000722 sptr->cur_start_row = 0;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000723 sptr->first_undef_row = 0;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000724 sptr->dirty = FALSE;
725 }
726 }
727
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000728 for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000729 if (bptr->mem_buffer == NULL) { /* if not realized yet */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000730 minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
731 if (minheights <= max_minheights) {
DRCe5eaf372014-05-09 18:00:32 +0000732 /* This buffer fits in memory */
733 bptr->rows_in_mem = bptr->rows_in_array;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000734 } else {
DRCe5eaf372014-05-09 18:00:32 +0000735 /* It doesn't fit in memory, create backing store. */
736 bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
737 jpeg_open_backing_store(cinfo, & bptr->b_s_info,
738 (long) bptr->rows_in_array *
739 (long) bptr->blocksperrow *
DRC5de454b2014-05-18 19:04:03 +0000740 (long) sizeof(JBLOCK));
DRCe5eaf372014-05-09 18:00:32 +0000741 bptr->b_s_open = TRUE;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000742 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000743 bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
DRCe5eaf372014-05-09 18:00:32 +0000744 bptr->blocksperrow, bptr->rows_in_mem);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000745 bptr->rowsperchunk = mem->last_rowsperchunk;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000746 bptr->cur_start_row = 0;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000747 bptr->first_undef_row = 0;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000748 bptr->dirty = FALSE;
749 }
750 }
751}
752
753
Thomas G. Lane489583f1996-02-07 00:00:00 +0000754LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000755do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
756/* Do backing store read or write of a virtual sample array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000757{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000758 long bytesperrow, file_offset, byte_count, rows, thisrow, i;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000759
DRC5de454b2014-05-18 19:04:03 +0000760 bytesperrow = (long) ptr->samplesperrow * sizeof(JSAMPLE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000761 file_offset = ptr->cur_start_row * bytesperrow;
762 /* Loop to read or write each allocation chunk in mem_buffer */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000763 for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000764 /* One chunk, but check for short chunk at end of buffer */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000765 rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000766 /* Transfer no more than is currently defined */
767 thisrow = (long) ptr->cur_start_row + i;
768 rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000769 /* Transfer no more than fits in file */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000770 rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
DRCe5eaf372014-05-09 18:00:32 +0000771 if (rows <= 0) /* this chunk might be past end of file! */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000772 break;
773 byte_count = rows * bytesperrow;
774 if (writing)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000775 (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
DRC5033f3e2014-05-18 18:33:44 +0000776 (void *) ptr->mem_buffer[i],
DRCe5eaf372014-05-09 18:00:32 +0000777 file_offset, byte_count);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000778 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000779 (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
DRC5033f3e2014-05-18 18:33:44 +0000780 (void *) ptr->mem_buffer[i],
DRCe5eaf372014-05-09 18:00:32 +0000781 file_offset, byte_count);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000782 file_offset += byte_count;
783 }
784}
785
786
Thomas G. Lane489583f1996-02-07 00:00:00 +0000787LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000788do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
789/* Do backing store read or write of a virtual coefficient-block array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000790{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000791 long bytesperrow, file_offset, byte_count, rows, thisrow, i;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000792
DRC5de454b2014-05-18 19:04:03 +0000793 bytesperrow = (long) ptr->blocksperrow * sizeof(JBLOCK);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000794 file_offset = ptr->cur_start_row * bytesperrow;
795 /* Loop to read or write each allocation chunk in mem_buffer */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000796 for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000797 /* One chunk, but check for short chunk at end of buffer */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000798 rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000799 /* Transfer no more than is currently defined */
800 thisrow = (long) ptr->cur_start_row + i;
801 rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000802 /* Transfer no more than fits in file */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000803 rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
DRCe5eaf372014-05-09 18:00:32 +0000804 if (rows <= 0) /* this chunk might be past end of file! */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000805 break;
806 byte_count = rows * bytesperrow;
807 if (writing)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000808 (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
DRC5033f3e2014-05-18 18:33:44 +0000809 (void *) ptr->mem_buffer[i],
DRCe5eaf372014-05-09 18:00:32 +0000810 file_offset, byte_count);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000811 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000812 (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
DRC5033f3e2014-05-18 18:33:44 +0000813 (void *) ptr->mem_buffer[i],
DRCe5eaf372014-05-09 18:00:32 +0000814 file_offset, byte_count);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000815 file_offset += byte_count;
816 }
817}
818
819
Thomas G. Lane489583f1996-02-07 00:00:00 +0000820METHODDEF(JSAMPARRAY)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000821access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
DRCe5eaf372014-05-09 18:00:32 +0000822 JDIMENSION start_row, JDIMENSION num_rows,
823 boolean writable)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000824/* Access the part of a virtual sample array starting at start_row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000825/* and extending for num_rows rows. writable is true if */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000826/* caller intends to modify the accessed area. */
827{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000828 JDIMENSION end_row = start_row + num_rows;
829 JDIMENSION undef_row;
830
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000831 /* debugging check */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000832 if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
833 ptr->mem_buffer == NULL)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000834 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000835
836 /* Make the desired part of the virtual array accessible */
837 if (start_row < ptr->cur_start_row ||
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000838 end_row > ptr->cur_start_row+ptr->rows_in_mem) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000839 if (! ptr->b_s_open)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000840 ERREXIT(cinfo, JERR_VIRTUAL_BUG);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000841 /* Flush old buffer contents if necessary */
842 if (ptr->dirty) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000843 do_sarray_io(cinfo, ptr, TRUE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000844 ptr->dirty = FALSE;
845 }
846 /* Decide what part of virtual array to access.
847 * Algorithm: if target address > current window, assume forward scan,
848 * load starting at target address. If target address < current window,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000849 * assume backward scan, load so that target area is top of window.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000850 * Note that when switching from forward write to forward read, will have
851 * start_row = 0, so the limiting case applies and we load from 0 anyway.
852 */
853 if (start_row > ptr->cur_start_row) {
854 ptr->cur_start_row = start_row;
855 } else {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000856 /* use long arithmetic here to avoid overflow & unsigned problems */
857 long ltemp;
858
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000859 ltemp = (long) end_row - (long) ptr->rows_in_mem;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000860 if (ltemp < 0)
DRCe5eaf372014-05-09 18:00:32 +0000861 ltemp = 0; /* don't fall off front end of file */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000862 ptr->cur_start_row = (JDIMENSION) ltemp;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000863 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000864 /* Read in the selected part of the array.
865 * During the initial write pass, we will do no actual read
866 * because the selected part is all undefined.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000867 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000868 do_sarray_io(cinfo, ptr, FALSE);
869 }
870 /* Ensure the accessed part of the array is defined; prezero if needed.
871 * To improve locality of access, we only prezero the part of the array
872 * that the caller is about to access, not the entire in-memory array.
873 */
874 if (ptr->first_undef_row < end_row) {
875 if (ptr->first_undef_row < start_row) {
DRCe5eaf372014-05-09 18:00:32 +0000876 if (writable) /* writer skipped over a section of array */
877 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
878 undef_row = start_row; /* but reader is allowed to read ahead */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000879 } else {
880 undef_row = ptr->first_undef_row;
881 }
882 if (writable)
883 ptr->first_undef_row = end_row;
884 if (ptr->pre_zero) {
DRC5de454b2014-05-18 19:04:03 +0000885 size_t bytesperrow = (size_t) ptr->samplesperrow * sizeof(JSAMPLE);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000886 undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
887 end_row -= ptr->cur_start_row;
888 while (undef_row < end_row) {
DRC5033f3e2014-05-18 18:33:44 +0000889 jzero_far((void *) ptr->mem_buffer[undef_row], bytesperrow);
DRCe5eaf372014-05-09 18:00:32 +0000890 undef_row++;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000891 }
892 } else {
DRCe5eaf372014-05-09 18:00:32 +0000893 if (! writable) /* reader looking at undefined data */
894 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000895 }
896 }
897 /* Flag the buffer dirty if caller will write in it */
898 if (writable)
899 ptr->dirty = TRUE;
900 /* Return address of proper part of the buffer */
901 return ptr->mem_buffer + (start_row - ptr->cur_start_row);
902}
903
904
Thomas G. Lane489583f1996-02-07 00:00:00 +0000905METHODDEF(JBLOCKARRAY)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000906access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
DRCe5eaf372014-05-09 18:00:32 +0000907 JDIMENSION start_row, JDIMENSION num_rows,
908 boolean writable)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000909/* Access the part of a virtual block array starting at start_row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000910/* and extending for num_rows rows. writable is true if */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000911/* caller intends to modify the accessed area. */
912{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000913 JDIMENSION end_row = start_row + num_rows;
914 JDIMENSION undef_row;
915
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000916 /* debugging check */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000917 if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
918 ptr->mem_buffer == NULL)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000919 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000920
921 /* Make the desired part of the virtual array accessible */
922 if (start_row < ptr->cur_start_row ||
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000923 end_row > ptr->cur_start_row+ptr->rows_in_mem) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000924 if (! ptr->b_s_open)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000925 ERREXIT(cinfo, JERR_VIRTUAL_BUG);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000926 /* Flush old buffer contents if necessary */
927 if (ptr->dirty) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000928 do_barray_io(cinfo, ptr, TRUE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000929 ptr->dirty = FALSE;
930 }
931 /* Decide what part of virtual array to access.
932 * Algorithm: if target address > current window, assume forward scan,
933 * load starting at target address. If target address < current window,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000934 * assume backward scan, load so that target area is top of window.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000935 * Note that when switching from forward write to forward read, will have
936 * start_row = 0, so the limiting case applies and we load from 0 anyway.
937 */
938 if (start_row > ptr->cur_start_row) {
939 ptr->cur_start_row = start_row;
940 } else {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000941 /* use long arithmetic here to avoid overflow & unsigned problems */
942 long ltemp;
943
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000944 ltemp = (long) end_row - (long) ptr->rows_in_mem;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000945 if (ltemp < 0)
DRCe5eaf372014-05-09 18:00:32 +0000946 ltemp = 0; /* don't fall off front end of file */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000947 ptr->cur_start_row = (JDIMENSION) ltemp;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000948 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000949 /* Read in the selected part of the array.
950 * During the initial write pass, we will do no actual read
951 * because the selected part is all undefined.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000952 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000953 do_barray_io(cinfo, ptr, FALSE);
954 }
955 /* Ensure the accessed part of the array is defined; prezero if needed.
956 * To improve locality of access, we only prezero the part of the array
957 * that the caller is about to access, not the entire in-memory array.
958 */
959 if (ptr->first_undef_row < end_row) {
960 if (ptr->first_undef_row < start_row) {
DRCe5eaf372014-05-09 18:00:32 +0000961 if (writable) /* writer skipped over a section of array */
962 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
963 undef_row = start_row; /* but reader is allowed to read ahead */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000964 } else {
965 undef_row = ptr->first_undef_row;
966 }
967 if (writable)
968 ptr->first_undef_row = end_row;
969 if (ptr->pre_zero) {
DRC5de454b2014-05-18 19:04:03 +0000970 size_t bytesperrow = (size_t) ptr->blocksperrow * sizeof(JBLOCK);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000971 undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
972 end_row -= ptr->cur_start_row;
973 while (undef_row < end_row) {
DRC5033f3e2014-05-18 18:33:44 +0000974 jzero_far((void *) ptr->mem_buffer[undef_row], bytesperrow);
DRCe5eaf372014-05-09 18:00:32 +0000975 undef_row++;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000976 }
977 } else {
DRCe5eaf372014-05-09 18:00:32 +0000978 if (! writable) /* reader looking at undefined data */
979 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000980 }
981 }
982 /* Flag the buffer dirty if caller will write in it */
983 if (writable)
984 ptr->dirty = TRUE;
985 /* Return address of proper part of the buffer */
986 return ptr->mem_buffer + (start_row - ptr->cur_start_row);
987}
988
989
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000990/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000991 * Release all objects belonging to a specified pool.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000992 */
993
Thomas G. Lane489583f1996-02-07 00:00:00 +0000994METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000995free_pool (j_common_ptr cinfo, int pool_id)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000996{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000997 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
998 small_pool_ptr shdr_ptr;
999 large_pool_ptr lhdr_ptr;
1000 size_t space_freed;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001001
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001002 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
DRCe5eaf372014-05-09 18:00:32 +00001003 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001004
1005#ifdef MEM_STATS
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001006 if (cinfo->err->trace_level > 1)
1007 print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001008#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001009
1010 /* If freeing IMAGE pool, close any virtual arrays first */
1011 if (pool_id == JPOOL_IMAGE) {
1012 jvirt_sarray_ptr sptr;
1013 jvirt_barray_ptr bptr;
1014
1015 for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
DRCe5eaf372014-05-09 18:00:32 +00001016 if (sptr->b_s_open) { /* there may be no backing store */
1017 sptr->b_s_open = FALSE; /* prevent recursive close if error */
1018 (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001019 }
1020 }
1021 mem->virt_sarray_list = NULL;
1022 for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
DRCe5eaf372014-05-09 18:00:32 +00001023 if (bptr->b_s_open) { /* there may be no backing store */
1024 bptr->b_s_open = FALSE; /* prevent recursive close if error */
1025 (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001026 }
1027 }
1028 mem->virt_barray_list = NULL;
1029 }
1030
1031 /* Release large objects */
1032 lhdr_ptr = mem->large_list[pool_id];
1033 mem->large_list[pool_id] = NULL;
1034
1035 while (lhdr_ptr != NULL) {
Pierre Ossman5557fd22009-03-09 10:34:53 +00001036 large_pool_ptr next_lhdr_ptr = lhdr_ptr->next;
1037 space_freed = lhdr_ptr->bytes_used +
DRCe5eaf372014-05-09 18:00:32 +00001038 lhdr_ptr->bytes_left +
DRC5de454b2014-05-18 19:04:03 +00001039 sizeof(large_pool_hdr);
DRC5033f3e2014-05-18 18:33:44 +00001040 jpeg_free_large(cinfo, (void *) lhdr_ptr, space_freed);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001041 mem->total_space_allocated -= space_freed;
1042 lhdr_ptr = next_lhdr_ptr;
1043 }
1044
1045 /* Release small objects */
1046 shdr_ptr = mem->small_list[pool_id];
1047 mem->small_list[pool_id] = NULL;
1048
1049 while (shdr_ptr != NULL) {
Pierre Ossman5557fd22009-03-09 10:34:53 +00001050 small_pool_ptr next_shdr_ptr = shdr_ptr->next;
1051 space_freed = shdr_ptr->bytes_used +
DRCe5eaf372014-05-09 18:00:32 +00001052 shdr_ptr->bytes_left +
DRC5de454b2014-05-18 19:04:03 +00001053 sizeof(small_pool_hdr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001054 jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
1055 mem->total_space_allocated -= space_freed;
1056 shdr_ptr = next_shdr_ptr;
1057 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001058}
1059
1060
1061/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001062 * Close up shop entirely.
1063 * Note that this cannot be called unless cinfo->mem is non-NULL.
1064 */
1065
Thomas G. Lane489583f1996-02-07 00:00:00 +00001066METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001067self_destruct (j_common_ptr cinfo)
1068{
1069 int pool;
1070
1071 /* Close all backing store, release all memory.
1072 * Releasing pools in reverse order might help avoid fragmentation
1073 * with some (brain-damaged) malloc libraries.
1074 */
1075 for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
1076 free_pool(cinfo, pool);
1077 }
1078
1079 /* Release the memory manager control block too. */
DRC5de454b2014-05-18 19:04:03 +00001080 jpeg_free_small(cinfo, (void *) cinfo->mem, sizeof(my_memory_mgr));
DRCe5eaf372014-05-09 18:00:32 +00001081 cinfo->mem = NULL; /* ensures I will be called only once */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001082
DRCe5eaf372014-05-09 18:00:32 +00001083 jpeg_mem_term(cinfo); /* system-dependent cleanup */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001084}
1085
1086
1087/*
1088 * Memory manager initialization.
1089 * When this is called, only the error manager pointer is valid in cinfo!
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001090 */
1091
Thomas G. Lane489583f1996-02-07 00:00:00 +00001092GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001093jinit_memory_mgr (j_common_ptr cinfo)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001094{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001095 my_mem_ptr mem;
1096 long max_to_use;
1097 int pool;
1098 size_t test_mac;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001099
DRCe5eaf372014-05-09 18:00:32 +00001100 cinfo->mem = NULL; /* for safety if init fails */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001101
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001102 /* Check for configuration errors.
DRC5de454b2014-05-18 19:04:03 +00001103 * sizeof(ALIGN_TYPE) should be a power of 2; otherwise, it probably
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001104 * doesn't reflect any real hardware alignment requirement.
1105 * The test is a little tricky: for X>0, X and X-1 have no one-bits
1106 * in common if and only if X is a power of 2, ie has only one one-bit.
1107 * Some compilers may give an "unreachable code" warning here; ignore it.
1108 */
Pierre Ossman5557fd22009-03-09 10:34:53 +00001109 if ((ALIGN_SIZE & (ALIGN_SIZE-1)) != 0)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001110 ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
1111 /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
Pierre Ossman5557fd22009-03-09 10:34:53 +00001112 * a multiple of ALIGN_SIZE.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001113 * Again, an "unreachable code" warning may be ignored here.
1114 * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
1115 */
1116 test_mac = (size_t) MAX_ALLOC_CHUNK;
1117 if ((long) test_mac != MAX_ALLOC_CHUNK ||
Pierre Ossman5557fd22009-03-09 10:34:53 +00001118 (MAX_ALLOC_CHUNK % ALIGN_SIZE) != 0)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001119 ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001120
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001121 max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */
1122
1123 /* Attempt to allocate memory manager's control block */
DRC5de454b2014-05-18 19:04:03 +00001124 mem = (my_mem_ptr) jpeg_get_small(cinfo, sizeof(my_memory_mgr));
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001125
1126 if (mem == NULL) {
DRCe5eaf372014-05-09 18:00:32 +00001127 jpeg_mem_term(cinfo); /* system-dependent cleanup */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001128 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
1129 }
1130
1131 /* OK, fill in the method pointers */
1132 mem->pub.alloc_small = alloc_small;
1133 mem->pub.alloc_large = alloc_large;
1134 mem->pub.alloc_sarray = alloc_sarray;
1135 mem->pub.alloc_barray = alloc_barray;
1136 mem->pub.request_virt_sarray = request_virt_sarray;
1137 mem->pub.request_virt_barray = request_virt_barray;
1138 mem->pub.realize_virt_arrays = realize_virt_arrays;
1139 mem->pub.access_virt_sarray = access_virt_sarray;
1140 mem->pub.access_virt_barray = access_virt_barray;
1141 mem->pub.free_pool = free_pool;
1142 mem->pub.self_destruct = self_destruct;
1143
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00001144 /* Make MAX_ALLOC_CHUNK accessible to other modules */
1145 mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK;
1146
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001147 /* Initialize working state */
1148 mem->pub.max_memory_to_use = max_to_use;
1149
1150 for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
1151 mem->small_list[pool] = NULL;
1152 mem->large_list[pool] = NULL;
1153 }
1154 mem->virt_sarray_list = NULL;
1155 mem->virt_barray_list = NULL;
1156
DRC5de454b2014-05-18 19:04:03 +00001157 mem->total_space_allocated = sizeof(my_memory_mgr);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001158
1159 /* Declare ourselves open for business */
1160 cinfo->mem = & mem->pub;
Thomas G. Lane88aeed41992-12-10 00:00:00 +00001161
1162 /* Check for an environment variable JPEGMEM; if found, override the
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001163 * default max_memory setting from jpeg_mem_init. Note that the
1164 * surrounding application may again override this value.
Thomas G. Lane88aeed41992-12-10 00:00:00 +00001165 * If your system doesn't support getenv(), define NO_GETENV to disable
1166 * this feature.
1167 */
1168#ifndef NO_GETENV
Alex Naidis6eb7d372016-10-16 23:10:08 +02001169 { char *memenv;
Thomas G. Lane88aeed41992-12-10 00:00:00 +00001170
1171 if ((memenv = getenv("JPEGMEM")) != NULL) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +00001172 char ch = 'x';
1173
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001174 if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
DRCe5eaf372014-05-09 18:00:32 +00001175 if (ch == 'm' || ch == 'M')
1176 max_to_use *= 1000L;
1177 mem->pub.max_memory_to_use = max_to_use * 1000L;
Thomas G. Lane88aeed41992-12-10 00:00:00 +00001178 }
1179 }
1180 }
1181#endif
1182
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001183}