blob: 54589521a62506fd9690c3ccb26a26a789721fc7 [file] [log] [blame]
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001/*
2 * jmemmgr.c
3 *
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00004 * Copyright (C) 1991-1997, Thomas G. Lane.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00005 * This file is part of the Independent JPEG Group's software.
6 * For conditions of distribution and use, see the accompanying README file.
7 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00008 * This file contains the JPEG system-independent memory management
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00009 * routines. This code is usable across a wide variety of machines; most
10 * of the system dependencies have been isolated in a separate file.
11 * The major functions provided here are:
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000012 * * pool-based allocation and freeing of memory;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000013 * * policy decisions about how to divide available memory among the
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000014 * virtual arrays;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000015 * * control logic for swapping virtual arrays between main memory and
16 * backing storage.
17 * The separate system-dependent file provides the actual backing-storage
18 * access code, and it contains the policy decision about how much total
19 * main memory to use.
20 * This file is system-dependent in the sense that some of its functions
21 * are unnecessary in some systems. For example, if there is enough virtual
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000022 * memory so that backing storage will never be used, much of the virtual
23 * array control logic could be removed. (Of course, if you have that much
24 * memory then you shouldn't care about a little bit of unused code...)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000025 */
26
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000027#define JPEG_INTERNALS
28#define AM_MEMORY_MANAGER /* we define jvirt_Xarray_control structs */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000029#include "jinclude.h"
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000030#include "jpeglib.h"
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000031#include "jmemsys.h" /* import the system-dependent declarations */
32
Thomas G. Lane88aeed41992-12-10 00:00:00 +000033#ifndef NO_GETENV
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000034#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare getenv() */
35extern char * getenv JPP((const char * name));
Thomas G. Lane88aeed41992-12-10 00:00:00 +000036#endif
37#endif
38
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000039
40/*
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000041 * Some important notes:
42 * The allocation routines provided here must never return NULL.
43 * They should exit to error_exit if unsuccessful.
44 *
45 * It's not a good idea to try to merge the sarray and barray routines,
46 * even though they are textually almost the same, because samples are
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000047 * usually stored as bytes while coefficients are shorts or ints. Thus,
48 * in machines where byte pointers have a different representation from
49 * word pointers, the resulting machine code could not be the same.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +000050 */
51
52
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000053/*
54 * Many machines require storage alignment: longs must start on 4-byte
55 * boundaries, doubles on 8-byte boundaries, etc. On such machines, malloc()
56 * always returns pointers that are multiples of the worst-case alignment
57 * requirement, and we had better do so too.
58 * There isn't any really portable way to determine the worst-case alignment
59 * requirement. This module assumes that the alignment requirement is
Pierre Ossman5557fd22009-03-09 10:34:53 +000060 * multiples of ALIGN_SIZE.
61 * By default, we define ALIGN_SIZE as sizeof(double). This is necessary on some
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000062 * workstations (where doubles really do need 8-byte alignment) and will work
63 * fine on nearly everything. If your machine has lesser alignment needs,
Pierre Ossman5557fd22009-03-09 10:34:53 +000064 * you can save a few bytes by making ALIGN_SIZE smaller.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000065 * The only place I know of where this will NOT work is certain Macintosh
66 * 680x0 compilers that define double as a 10-byte IEEE extended float.
67 * Doing 10-byte alignment is counterproductive because longwords won't be
Pierre Ossman5557fd22009-03-09 10:34:53 +000068 * aligned well. Put "#define ALIGN_SIZE 4" in jconfig.h if you have
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000069 * such a compiler.
70 */
71
Pierre Ossman5557fd22009-03-09 10:34:53 +000072#ifndef ALIGN_SIZE /* so can override from jconfig.h */
Pierre Ossman73118302009-03-09 13:30:47 +000073#ifndef WITH_SIMD
Pierre Ossman5557fd22009-03-09 10:34:53 +000074#define ALIGN_SIZE SIZEOF(double)
Pierre Ossman73118302009-03-09 13:30:47 +000075#else
76#define ALIGN_SIZE 16 /* Most SIMD implementations require this */
77#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000078#endif
79
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000080/*
81 * We allocate objects from "pools", where each pool is gotten with a single
82 * request to jpeg_get_small() or jpeg_get_large(). There is no per-object
83 * overhead within a pool, except for alignment padding. Each pool has a
84 * header with a link to the next pool of the same class.
85 * Small and large pool headers are identical except that the latter's
86 * link pointer must be FAR on 80x86 machines.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000087 */
88
Pierre Ossman5557fd22009-03-09 10:34:53 +000089typedef struct small_pool_struct * small_pool_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000090
Pierre Ossman5557fd22009-03-09 10:34:53 +000091typedef struct small_pool_struct {
92 small_pool_ptr next; /* next in list of pools */
93 size_t bytes_used; /* how many bytes already used within pool */
94 size_t bytes_left; /* bytes still available in this pool */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000095} small_pool_hdr;
96
Pierre Ossman5557fd22009-03-09 10:34:53 +000097typedef struct large_pool_struct FAR * large_pool_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +000098
Pierre Ossman5557fd22009-03-09 10:34:53 +000099typedef struct large_pool_struct {
100 large_pool_ptr next; /* next in list of pools */
101 size_t bytes_used; /* how many bytes already used within pool */
102 size_t bytes_left; /* bytes still available in this pool */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000103} large_pool_hdr;
104
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000105/*
106 * Here is the full definition of a memory manager object.
107 */
108
109typedef struct {
110 struct jpeg_memory_mgr pub; /* public fields */
111
112 /* Each pool identifier (lifetime class) names a linked list of pools. */
113 small_pool_ptr small_list[JPOOL_NUMPOOLS];
114 large_pool_ptr large_list[JPOOL_NUMPOOLS];
115
116 /* Since we only have one lifetime class of virtual arrays, only one
117 * linked list is necessary (for each datatype). Note that the virtual
118 * array control blocks being linked together are actually stored somewhere
119 * in the small-pool list.
120 */
121 jvirt_sarray_ptr virt_sarray_list;
122 jvirt_barray_ptr virt_barray_list;
123
124 /* This counts total space obtained from jpeg_get_small/large */
DRC04899092010-02-26 23:01:19 +0000125 size_t total_space_allocated;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000126
127 /* alloc_sarray and alloc_barray set this value for use by virtual
128 * array routines.
129 */
130 JDIMENSION last_rowsperchunk; /* from most recent alloc_sarray/barray */
131} my_memory_mgr;
132
133typedef my_memory_mgr * my_mem_ptr;
134
135
136/*
137 * The control blocks for virtual arrays.
138 * Note that these blocks are allocated in the "small" pool area.
139 * System-dependent info for the associated backing store (if any) is hidden
140 * inside the backing_store_info struct.
141 */
142
143struct jvirt_sarray_control {
144 JSAMPARRAY mem_buffer; /* => the in-memory buffer */
145 JDIMENSION rows_in_array; /* total virtual array height */
146 JDIMENSION samplesperrow; /* width of array (and of memory buffer) */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000147 JDIMENSION maxaccess; /* max rows accessed by access_virt_sarray */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000148 JDIMENSION rows_in_mem; /* height of memory buffer */
149 JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
150 JDIMENSION cur_start_row; /* first logical row # in the buffer */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000151 JDIMENSION first_undef_row; /* row # of first uninitialized row */
152 boolean pre_zero; /* pre-zero mode requested? */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000153 boolean dirty; /* do current buffer contents need written? */
154 boolean b_s_open; /* is backing-store data valid? */
155 jvirt_sarray_ptr next; /* link to next virtual sarray control block */
156 backing_store_info b_s_info; /* System-dependent control info */
157};
158
159struct jvirt_barray_control {
160 JBLOCKARRAY mem_buffer; /* => the in-memory buffer */
161 JDIMENSION rows_in_array; /* total virtual array height */
162 JDIMENSION blocksperrow; /* width of array (and of memory buffer) */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000163 JDIMENSION maxaccess; /* max rows accessed by access_virt_barray */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000164 JDIMENSION rows_in_mem; /* height of memory buffer */
165 JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
166 JDIMENSION cur_start_row; /* first logical row # in the buffer */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000167 JDIMENSION first_undef_row; /* row # of first uninitialized row */
168 boolean pre_zero; /* pre-zero mode requested? */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000169 boolean dirty; /* do current buffer contents need written? */
170 boolean b_s_open; /* is backing-store data valid? */
171 jvirt_barray_ptr next; /* link to next virtual barray control block */
172 backing_store_info b_s_info; /* System-dependent control info */
173};
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000174
175
176#ifdef MEM_STATS /* optional extra stuff for statistics */
177
Thomas G. Lane489583f1996-02-07 00:00:00 +0000178LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000179print_mem_stats (j_common_ptr cinfo, int pool_id)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000180{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000181 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
182 small_pool_ptr shdr_ptr;
183 large_pool_ptr lhdr_ptr;
184
185 /* Since this is only a debugging stub, we can cheat a little by using
186 * fprintf directly rather than going through the trace message code.
187 * This is helpful because message parm array can't handle longs.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000188 */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000189 fprintf(stderr, "Freeing pool %d, total space = %ld\n",
190 pool_id, mem->total_space_allocated);
191
192 for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000193 lhdr_ptr = lhdr_ptr->next) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000194 fprintf(stderr, " Large chunk used %ld\n",
Pierre Ossman5557fd22009-03-09 10:34:53 +0000195 (long) lhdr_ptr->bytes_used);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000196 }
197
198 for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000199 shdr_ptr = shdr_ptr->next) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000200 fprintf(stderr, " Small chunk used %ld free %ld\n",
Pierre Ossman5557fd22009-03-09 10:34:53 +0000201 (long) shdr_ptr->bytes_used,
202 (long) shdr_ptr->bytes_left);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000203 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000204}
205
206#endif /* MEM_STATS */
207
208
Thomas G. Lane489583f1996-02-07 00:00:00 +0000209LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000210out_of_memory (j_common_ptr cinfo, int which)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000211/* Report an out-of-memory error and stop execution */
212/* If we compiled MEM_STATS support, report alloc requests before dying */
213{
214#ifdef MEM_STATS
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000215 cinfo->err->trace_level = 2; /* force self_destruct to report stats */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000216#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000217 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000218}
219
220
221/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000222 * Allocation of "small" objects.
223 *
224 * For these, we use pooled storage. When a new pool must be created,
225 * we try to get enough space for the current request plus a "slop" factor,
226 * where the slop will be the amount of leftover space in the new pool.
227 * The speed vs. space tradeoff is largely determined by the slop values.
228 * A different slop value is provided for each pool class (lifetime),
229 * and we also distinguish the first pool of a class from later ones.
230 * NOTE: the values given work fairly well on both 16- and 32-bit-int
231 * machines, but may be too small if longs are 64 bits or more.
Pierre Ossman5557fd22009-03-09 10:34:53 +0000232 *
233 * Since we do not know what alignment malloc() gives us, we have to
234 * allocate ALIGN_SIZE-1 extra space per pool to have room for alignment
235 * adjustment.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000236 */
237
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000238static const size_t first_pool_slop[JPOOL_NUMPOOLS] =
239{
240 1600, /* first PERMANENT pool */
241 16000 /* first IMAGE pool */
242};
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000243
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000244static const size_t extra_pool_slop[JPOOL_NUMPOOLS] =
245{
246 0, /* additional PERMANENT pools */
247 5000 /* additional IMAGE pools */
248};
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000249
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000250#define MIN_SLOP 50 /* greater than 0 to avoid futile looping */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000251
252
Thomas G. Lane489583f1996-02-07 00:00:00 +0000253METHODDEF(void *)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000254alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000255/* Allocate a "small" object */
256{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000257 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
258 small_pool_ptr hdr_ptr, prev_hdr_ptr;
259 char * data_ptr;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000260 size_t min_request, slop;
261
262 /*
263 * Round up the requested size to a multiple of ALIGN_SIZE in order
264 * to assure alignment for the next object allocated in the same pool
265 * and so that algorithms can straddle outside the proper area up
266 * to the next alignment.
267 */
268 sizeofobject = jround_up(sizeofobject, ALIGN_SIZE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000269
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000270 /* Check for unsatisfiable request (do now to ensure no overflow below) */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000271 if ((SIZEOF(small_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) > MAX_ALLOC_CHUNK)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000272 out_of_memory(cinfo, 1); /* request exceeds malloc's ability */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000273
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000274 /* See if space is available in any existing pool */
275 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
276 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
277 prev_hdr_ptr = NULL;
278 hdr_ptr = mem->small_list[pool_id];
279 while (hdr_ptr != NULL) {
Pierre Ossman5557fd22009-03-09 10:34:53 +0000280 if (hdr_ptr->bytes_left >= sizeofobject)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000281 break; /* found pool with enough space */
282 prev_hdr_ptr = hdr_ptr;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000283 hdr_ptr = hdr_ptr->next;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000284 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000285
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000286 /* Time to make a new pool? */
287 if (hdr_ptr == NULL) {
288 /* min_request is what we need now, slop is what will be leftover */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000289 min_request = SIZEOF(small_pool_hdr) + sizeofobject + ALIGN_SIZE - 1;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000290 if (prev_hdr_ptr == NULL) /* first pool in class? */
291 slop = first_pool_slop[pool_id];
292 else
293 slop = extra_pool_slop[pool_id];
294 /* Don't ask for more than MAX_ALLOC_CHUNK */
295 if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
296 slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
297 /* Try to get space, if fail reduce slop and try again */
298 for (;;) {
299 hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
300 if (hdr_ptr != NULL)
301 break;
302 slop /= 2;
303 if (slop < MIN_SLOP) /* give up when it gets real small */
304 out_of_memory(cinfo, 2); /* jpeg_get_small failed */
305 }
306 mem->total_space_allocated += min_request + slop;
307 /* Success, initialize the new pool header and add to end of list */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000308 hdr_ptr->next = NULL;
309 hdr_ptr->bytes_used = 0;
310 hdr_ptr->bytes_left = sizeofobject + slop;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000311 if (prev_hdr_ptr == NULL) /* first pool in class? */
312 mem->small_list[pool_id] = hdr_ptr;
313 else
Pierre Ossman5557fd22009-03-09 10:34:53 +0000314 prev_hdr_ptr->next = hdr_ptr;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000315 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000316
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000317 /* OK, allocate the object from the current pool */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000318 data_ptr = (char *) hdr_ptr; /* point to first data byte in pool... */
319 data_ptr += SIZEOF(small_pool_hdr); /* ...by skipping the header... */
DRC04899092010-02-26 23:01:19 +0000320 if ((size_t)data_ptr % ALIGN_SIZE) /* ...and adjust for alignment */
321 data_ptr += ALIGN_SIZE - (size_t)data_ptr % ALIGN_SIZE;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000322 data_ptr += hdr_ptr->bytes_used; /* point to place for object */
323 hdr_ptr->bytes_used += sizeofobject;
324 hdr_ptr->bytes_left -= sizeofobject;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000325
326 return (void *) data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000327}
328
329
330/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000331 * Allocation of "large" objects.
332 *
333 * The external semantics of these are the same as "small" objects,
334 * except that FAR pointers are used on 80x86. However the pool
335 * management heuristics are quite different. We assume that each
336 * request is large enough that it may as well be passed directly to
337 * jpeg_get_large; the pool management just links everything together
338 * so that we can free it all on demand.
339 * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY
340 * structures. The routines that create these structures (see below)
341 * deliberately bunch rows together to ensure a large request size.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000342 */
343
Thomas G. Lane489583f1996-02-07 00:00:00 +0000344METHODDEF(void FAR *)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000345alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
346/* Allocate a "large" object */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000347{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000348 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
349 large_pool_ptr hdr_ptr;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000350 char FAR * data_ptr;
351
352 /*
353 * Round up the requested size to a multiple of ALIGN_SIZE so that
354 * algorithms can straddle outside the proper area up to the next
355 * alignment.
356 */
357 sizeofobject = jround_up(sizeofobject, ALIGN_SIZE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000358
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000359 /* Check for unsatisfiable request (do now to ensure no overflow below) */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000360 if ((SIZEOF(large_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) > MAX_ALLOC_CHUNK)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000361 out_of_memory(cinfo, 3); /* request exceeds malloc's ability */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000362
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000363 /* Always make a new pool */
364 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
365 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000366
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000367 hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
Pierre Ossman5557fd22009-03-09 10:34:53 +0000368 SIZEOF(large_pool_hdr) +
369 ALIGN_SIZE - 1);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000370 if (hdr_ptr == NULL)
371 out_of_memory(cinfo, 4); /* jpeg_get_large failed */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000372 mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr) + ALIGN_SIZE - 1;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000373
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000374 /* Success, initialize the new pool header and add to list */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000375 hdr_ptr->next = mem->large_list[pool_id];
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000376 /* We maintain space counts in each pool header for statistical purposes,
377 * even though they are not needed for allocation.
378 */
Pierre Ossman5557fd22009-03-09 10:34:53 +0000379 hdr_ptr->bytes_used = sizeofobject;
380 hdr_ptr->bytes_left = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000381 mem->large_list[pool_id] = hdr_ptr;
382
Pierre Ossman5557fd22009-03-09 10:34:53 +0000383 data_ptr = (char *) hdr_ptr; /* point to first data byte in pool... */
384 data_ptr += SIZEOF(small_pool_hdr); /* ...by skipping the header... */
DRC04899092010-02-26 23:01:19 +0000385 if ((size_t)data_ptr % ALIGN_SIZE) /* ...and adjust for alignment */
386 data_ptr += ALIGN_SIZE - (size_t)data_ptr % ALIGN_SIZE;
Pierre Ossman5557fd22009-03-09 10:34:53 +0000387
388 return (void FAR *) data_ptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000389}
390
391
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000392/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000393 * Creation of 2-D sample arrays.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000394 * The pointers are in near heap, the samples themselves in FAR heap.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000395 *
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000396 * To minimize allocation overhead and to allow I/O of large contiguous
397 * blocks, we allocate the sample rows in groups of as many rows as possible
398 * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000399 * NB: the virtual array control routines, later in this file, know about
400 * this chunking of rows. The rowsperchunk value is left in the mem manager
401 * object so that it can be saved away if this sarray is the workspace for
402 * a virtual array.
Pierre Ossman5557fd22009-03-09 10:34:53 +0000403 *
404 * Since we are often upsampling with a factor 2, we align the size (not
405 * the start) to 2 * ALIGN_SIZE so that the upsampling routines don't have
406 * to be as careful about size.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000407 */
408
Thomas G. Lane489583f1996-02-07 00:00:00 +0000409METHODDEF(JSAMPARRAY)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000410alloc_sarray (j_common_ptr cinfo, int pool_id,
411 JDIMENSION samplesperrow, JDIMENSION numrows)
412/* Allocate a 2-D sample array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000413{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000414 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000415 JSAMPARRAY result;
416 JSAMPROW workspace;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000417 JDIMENSION rowsperchunk, currow, i;
418 long ltemp;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000419
Pierre Ossman5557fd22009-03-09 10:34:53 +0000420 /* Make sure each row is properly aligned */
421 if ((ALIGN_SIZE % SIZEOF(JSAMPLE)) != 0)
422 out_of_memory(cinfo, 5); /* safety check */
DRC04899092010-02-26 23:01:19 +0000423 samplesperrow = (JDIMENSION)jround_up(samplesperrow, (2 * ALIGN_SIZE) / SIZEOF(JSAMPLE));
Pierre Ossman5557fd22009-03-09 10:34:53 +0000424
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000425 /* Calculate max # of rows allowed in one allocation chunk */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000426 ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
427 ((long) samplesperrow * SIZEOF(JSAMPLE));
428 if (ltemp <= 0)
429 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
430 if (ltemp < (long) numrows)
431 rowsperchunk = (JDIMENSION) ltemp;
432 else
433 rowsperchunk = numrows;
434 mem->last_rowsperchunk = rowsperchunk;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000435
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000436 /* Get space for row pointers (small object) */
437 result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
438 (size_t) (numrows * SIZEOF(JSAMPROW)));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000439
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000440 /* Get the rows themselves (large objects) */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000441 currow = 0;
442 while (currow < numrows) {
443 rowsperchunk = MIN(rowsperchunk, numrows - currow);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000444 workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
445 (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
446 * SIZEOF(JSAMPLE)));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000447 for (i = rowsperchunk; i > 0; i--) {
448 result[currow++] = workspace;
449 workspace += samplesperrow;
450 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000451 }
452
453 return result;
454}
455
456
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000457/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000458 * Creation of 2-D coefficient-block arrays.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000459 * This is essentially the same as the code for sample arrays, above.
460 */
461
Thomas G. Lane489583f1996-02-07 00:00:00 +0000462METHODDEF(JBLOCKARRAY)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000463alloc_barray (j_common_ptr cinfo, int pool_id,
464 JDIMENSION blocksperrow, JDIMENSION numrows)
465/* Allocate a 2-D coefficient-block array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000466{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000467 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000468 JBLOCKARRAY result;
469 JBLOCKROW workspace;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000470 JDIMENSION rowsperchunk, currow, i;
471 long ltemp;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000472
Pierre Ossman5557fd22009-03-09 10:34:53 +0000473 /* Make sure each row is properly aligned */
474 if ((SIZEOF(JBLOCK) % ALIGN_SIZE) != 0)
475 out_of_memory(cinfo, 6); /* safety check */
476
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000477 /* Calculate max # of rows allowed in one allocation chunk */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000478 ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
479 ((long) blocksperrow * SIZEOF(JBLOCK));
480 if (ltemp <= 0)
481 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
482 if (ltemp < (long) numrows)
483 rowsperchunk = (JDIMENSION) ltemp;
484 else
485 rowsperchunk = numrows;
486 mem->last_rowsperchunk = rowsperchunk;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000487
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000488 /* Get space for row pointers (small object) */
489 result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
490 (size_t) (numrows * SIZEOF(JBLOCKROW)));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000491
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000492 /* Get the rows themselves (large objects) */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000493 currow = 0;
494 while (currow < numrows) {
495 rowsperchunk = MIN(rowsperchunk, numrows - currow);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000496 workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
497 (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
498 * SIZEOF(JBLOCK)));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000499 for (i = rowsperchunk; i > 0; i--) {
500 result[currow++] = workspace;
501 workspace += blocksperrow;
502 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000503 }
504
505 return result;
506}
507
508
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000509/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000510 * About virtual array management:
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000511 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000512 * The above "normal" array routines are only used to allocate strip buffers
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000513 * (as wide as the image, but just a few rows high). Full-image-sized buffers
514 * are handled as "virtual" arrays. The array is still accessed a strip at a
515 * time, but the memory manager must save the whole array for repeated
516 * accesses. The intended implementation is that there is a strip buffer in
517 * memory (as high as is possible given the desired memory limit), plus a
518 * backing file that holds the rest of the array.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000519 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000520 * The request_virt_array routines are told the total size of the image and
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000521 * the maximum number of rows that will be accessed at once. The in-memory
522 * buffer must be at least as large as the maxaccess value.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000523 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000524 * The request routines create control blocks but not the in-memory buffers.
525 * That is postponed until realize_virt_arrays is called. At that time the
526 * total amount of space needed is known (approximately, anyway), so free
527 * memory can be divided up fairly.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000528 *
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000529 * The access_virt_array routines are responsible for making a specific strip
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000530 * area accessible (after reading or writing the backing file, if necessary).
531 * Note that the access routines are told whether the caller intends to modify
532 * the accessed strip; during a read-only pass this saves having to rewrite
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000533 * data to disk. The access routines are also responsible for pre-zeroing
534 * any newly accessed rows, if pre-zeroing was requested.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000535 *
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000536 * In current usage, the access requests are usually for nonoverlapping
537 * strips; that is, successive access start_row numbers differ by exactly
538 * num_rows = maxaccess. This means we can get good performance with simple
539 * buffer dump/reload logic, by making the in-memory buffer be a multiple
540 * of the access height; then there will never be accesses across bufferload
541 * boundaries. The code will still work with overlapping access requests,
542 * but it doesn't handle bufferload overlaps very efficiently.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000543 */
544
545
Thomas G. Lane489583f1996-02-07 00:00:00 +0000546METHODDEF(jvirt_sarray_ptr)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000547request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000548 JDIMENSION samplesperrow, JDIMENSION numrows,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000549 JDIMENSION maxaccess)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000550/* Request a virtual 2-D sample array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000551{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000552 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
553 jvirt_sarray_ptr result;
554
555 /* Only IMAGE-lifetime virtual arrays are currently supported */
556 if (pool_id != JPOOL_IMAGE)
557 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
558
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000559 /* get control block */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000560 result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
561 SIZEOF(struct jvirt_sarray_control));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000562
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000563 result->mem_buffer = NULL; /* marks array not yet realized */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000564 result->rows_in_array = numrows;
565 result->samplesperrow = samplesperrow;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000566 result->maxaccess = maxaccess;
567 result->pre_zero = pre_zero;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000568 result->b_s_open = FALSE; /* no associated backing-store object */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000569 result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
570 mem->virt_sarray_list = result;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000571
572 return result;
573}
574
575
Thomas G. Lane489583f1996-02-07 00:00:00 +0000576METHODDEF(jvirt_barray_ptr)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000577request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000578 JDIMENSION blocksperrow, JDIMENSION numrows,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000579 JDIMENSION maxaccess)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000580/* Request a virtual 2-D coefficient-block array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000581{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000582 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
583 jvirt_barray_ptr result;
584
585 /* Only IMAGE-lifetime virtual arrays are currently supported */
586 if (pool_id != JPOOL_IMAGE)
587 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
588
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000589 /* get control block */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000590 result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
591 SIZEOF(struct jvirt_barray_control));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000592
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000593 result->mem_buffer = NULL; /* marks array not yet realized */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000594 result->rows_in_array = numrows;
595 result->blocksperrow = blocksperrow;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000596 result->maxaccess = maxaccess;
597 result->pre_zero = pre_zero;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000598 result->b_s_open = FALSE; /* no associated backing-store object */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000599 result->next = mem->virt_barray_list; /* add to list of virtual arrays */
600 mem->virt_barray_list = result;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000601
602 return result;
603}
604
605
Thomas G. Lane489583f1996-02-07 00:00:00 +0000606METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000607realize_virt_arrays (j_common_ptr cinfo)
608/* Allocate the in-memory buffers for any unrealized virtual arrays */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000609{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000610 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
DRC04899092010-02-26 23:01:19 +0000611 size_t space_per_minheight, maximum_space, avail_mem;
612 size_t minheights, max_minheights;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000613 jvirt_sarray_ptr sptr;
614 jvirt_barray_ptr bptr;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000615
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000616 /* Compute the minimum space needed (maxaccess rows in each buffer)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000617 * and the maximum space needed (full image height in each buffer).
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000618 * These may be of use to the system-dependent jpeg_mem_available routine.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000619 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000620 space_per_minheight = 0;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000621 maximum_space = 0;
622 for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000623 if (sptr->mem_buffer == NULL) { /* if not realized yet */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000624 space_per_minheight += (long) sptr->maxaccess *
625 (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000626 maximum_space += (long) sptr->rows_in_array *
627 (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000628 }
629 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000630 for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000631 if (bptr->mem_buffer == NULL) { /* if not realized yet */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000632 space_per_minheight += (long) bptr->maxaccess *
633 (long) bptr->blocksperrow * SIZEOF(JBLOCK);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000634 maximum_space += (long) bptr->rows_in_array *
635 (long) bptr->blocksperrow * SIZEOF(JBLOCK);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000636 }
637 }
638
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000639 if (space_per_minheight <= 0)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000640 return; /* no unrealized arrays, no work */
641
642 /* Determine amount of memory to actually use; this is system-dependent. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000643 avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000644 mem->total_space_allocated);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000645
646 /* If the maximum space needed is available, make all the buffers full
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000647 * height; otherwise parcel it out with the same number of minheights
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000648 * in each buffer.
649 */
650 if (avail_mem >= maximum_space)
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000651 max_minheights = 1000000000L;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000652 else {
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000653 max_minheights = avail_mem / space_per_minheight;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000654 /* If there doesn't seem to be enough space, try to get the minimum
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000655 * anyway. This allows a "stub" implementation of jpeg_mem_available().
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000656 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000657 if (max_minheights <= 0)
658 max_minheights = 1;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000659 }
660
661 /* Allocate the in-memory buffers and initialize backing store as needed. */
662
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000663 for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000664 if (sptr->mem_buffer == NULL) { /* if not realized yet */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000665 minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
666 if (minheights <= max_minheights) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000667 /* This buffer fits in memory */
668 sptr->rows_in_mem = sptr->rows_in_array;
669 } else {
670 /* It doesn't fit in memory, create backing store. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000671 sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000672 jpeg_open_backing_store(cinfo, & sptr->b_s_info,
673 (long) sptr->rows_in_array *
674 (long) sptr->samplesperrow *
675 (long) SIZEOF(JSAMPLE));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000676 sptr->b_s_open = TRUE;
677 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000678 sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
679 sptr->samplesperrow, sptr->rows_in_mem);
680 sptr->rowsperchunk = mem->last_rowsperchunk;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000681 sptr->cur_start_row = 0;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000682 sptr->first_undef_row = 0;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000683 sptr->dirty = FALSE;
684 }
685 }
686
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000687 for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000688 if (bptr->mem_buffer == NULL) { /* if not realized yet */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000689 minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
690 if (minheights <= max_minheights) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000691 /* This buffer fits in memory */
692 bptr->rows_in_mem = bptr->rows_in_array;
693 } else {
694 /* It doesn't fit in memory, create backing store. */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000695 bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000696 jpeg_open_backing_store(cinfo, & bptr->b_s_info,
697 (long) bptr->rows_in_array *
698 (long) bptr->blocksperrow *
699 (long) SIZEOF(JBLOCK));
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000700 bptr->b_s_open = TRUE;
701 }
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000702 bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
703 bptr->blocksperrow, bptr->rows_in_mem);
704 bptr->rowsperchunk = mem->last_rowsperchunk;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000705 bptr->cur_start_row = 0;
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000706 bptr->first_undef_row = 0;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000707 bptr->dirty = FALSE;
708 }
709 }
710}
711
712
Thomas G. Lane489583f1996-02-07 00:00:00 +0000713LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000714do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
715/* Do backing store read or write of a virtual sample array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000716{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000717 long bytesperrow, file_offset, byte_count, rows, thisrow, i;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000718
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000719 bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000720 file_offset = ptr->cur_start_row * bytesperrow;
721 /* Loop to read or write each allocation chunk in mem_buffer */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000722 for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000723 /* One chunk, but check for short chunk at end of buffer */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000724 rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000725 /* Transfer no more than is currently defined */
726 thisrow = (long) ptr->cur_start_row + i;
727 rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000728 /* Transfer no more than fits in file */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000729 rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000730 if (rows <= 0) /* this chunk might be past end of file! */
731 break;
732 byte_count = rows * bytesperrow;
733 if (writing)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000734 (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000735 (void FAR *) ptr->mem_buffer[i],
736 file_offset, byte_count);
737 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000738 (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000739 (void FAR *) ptr->mem_buffer[i],
740 file_offset, byte_count);
741 file_offset += byte_count;
742 }
743}
744
745
Thomas G. Lane489583f1996-02-07 00:00:00 +0000746LOCAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000747do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
748/* Do backing store read or write of a virtual coefficient-block array */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000749{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000750 long bytesperrow, file_offset, byte_count, rows, thisrow, i;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000751
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000752 bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000753 file_offset = ptr->cur_start_row * bytesperrow;
754 /* Loop to read or write each allocation chunk in mem_buffer */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000755 for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000756 /* One chunk, but check for short chunk at end of buffer */
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000757 rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000758 /* Transfer no more than is currently defined */
759 thisrow = (long) ptr->cur_start_row + i;
760 rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000761 /* Transfer no more than fits in file */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000762 rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000763 if (rows <= 0) /* this chunk might be past end of file! */
764 break;
765 byte_count = rows * bytesperrow;
766 if (writing)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000767 (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000768 (void FAR *) ptr->mem_buffer[i],
769 file_offset, byte_count);
770 else
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000771 (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000772 (void FAR *) ptr->mem_buffer[i],
773 file_offset, byte_count);
774 file_offset += byte_count;
775 }
776}
777
778
Thomas G. Lane489583f1996-02-07 00:00:00 +0000779METHODDEF(JSAMPARRAY)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000780access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000781 JDIMENSION start_row, JDIMENSION num_rows,
782 boolean writable)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000783/* Access the part of a virtual sample array starting at start_row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000784/* and extending for num_rows rows. writable is true if */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000785/* caller intends to modify the accessed area. */
786{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000787 JDIMENSION end_row = start_row + num_rows;
788 JDIMENSION undef_row;
789
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000790 /* debugging check */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000791 if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
792 ptr->mem_buffer == NULL)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000793 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000794
795 /* Make the desired part of the virtual array accessible */
796 if (start_row < ptr->cur_start_row ||
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000797 end_row > ptr->cur_start_row+ptr->rows_in_mem) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000798 if (! ptr->b_s_open)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000799 ERREXIT(cinfo, JERR_VIRTUAL_BUG);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000800 /* Flush old buffer contents if necessary */
801 if (ptr->dirty) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000802 do_sarray_io(cinfo, ptr, TRUE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000803 ptr->dirty = FALSE;
804 }
805 /* Decide what part of virtual array to access.
806 * Algorithm: if target address > current window, assume forward scan,
807 * load starting at target address. If target address < current window,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000808 * assume backward scan, load so that target area is top of window.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000809 * Note that when switching from forward write to forward read, will have
810 * start_row = 0, so the limiting case applies and we load from 0 anyway.
811 */
812 if (start_row > ptr->cur_start_row) {
813 ptr->cur_start_row = start_row;
814 } else {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000815 /* use long arithmetic here to avoid overflow & unsigned problems */
816 long ltemp;
817
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000818 ltemp = (long) end_row - (long) ptr->rows_in_mem;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000819 if (ltemp < 0)
820 ltemp = 0; /* don't fall off front end of file */
821 ptr->cur_start_row = (JDIMENSION) ltemp;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000822 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000823 /* Read in the selected part of the array.
824 * During the initial write pass, we will do no actual read
825 * because the selected part is all undefined.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000826 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000827 do_sarray_io(cinfo, ptr, FALSE);
828 }
829 /* Ensure the accessed part of the array is defined; prezero if needed.
830 * To improve locality of access, we only prezero the part of the array
831 * that the caller is about to access, not the entire in-memory array.
832 */
833 if (ptr->first_undef_row < end_row) {
834 if (ptr->first_undef_row < start_row) {
835 if (writable) /* writer skipped over a section of array */
836 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
837 undef_row = start_row; /* but reader is allowed to read ahead */
838 } else {
839 undef_row = ptr->first_undef_row;
840 }
841 if (writable)
842 ptr->first_undef_row = end_row;
843 if (ptr->pre_zero) {
844 size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE);
845 undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
846 end_row -= ptr->cur_start_row;
847 while (undef_row < end_row) {
848 jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
849 undef_row++;
850 }
851 } else {
852 if (! writable) /* reader looking at undefined data */
853 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000854 }
855 }
856 /* Flag the buffer dirty if caller will write in it */
857 if (writable)
858 ptr->dirty = TRUE;
859 /* Return address of proper part of the buffer */
860 return ptr->mem_buffer + (start_row - ptr->cur_start_row);
861}
862
863
Thomas G. Lane489583f1996-02-07 00:00:00 +0000864METHODDEF(JBLOCKARRAY)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000865access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000866 JDIMENSION start_row, JDIMENSION num_rows,
867 boolean writable)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000868/* Access the part of a virtual block array starting at start_row */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000869/* and extending for num_rows rows. writable is true if */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000870/* caller intends to modify the accessed area. */
871{
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000872 JDIMENSION end_row = start_row + num_rows;
873 JDIMENSION undef_row;
874
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000875 /* debugging check */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000876 if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
877 ptr->mem_buffer == NULL)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000878 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000879
880 /* Make the desired part of the virtual array accessible */
881 if (start_row < ptr->cur_start_row ||
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000882 end_row > ptr->cur_start_row+ptr->rows_in_mem) {
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000883 if (! ptr->b_s_open)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000884 ERREXIT(cinfo, JERR_VIRTUAL_BUG);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000885 /* Flush old buffer contents if necessary */
886 if (ptr->dirty) {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000887 do_barray_io(cinfo, ptr, TRUE);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000888 ptr->dirty = FALSE;
889 }
890 /* Decide what part of virtual array to access.
891 * Algorithm: if target address > current window, assume forward scan,
892 * load starting at target address. If target address < current window,
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000893 * assume backward scan, load so that target area is top of window.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000894 * Note that when switching from forward write to forward read, will have
895 * start_row = 0, so the limiting case applies and we load from 0 anyway.
896 */
897 if (start_row > ptr->cur_start_row) {
898 ptr->cur_start_row = start_row;
899 } else {
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000900 /* use long arithmetic here to avoid overflow & unsigned problems */
901 long ltemp;
902
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000903 ltemp = (long) end_row - (long) ptr->rows_in_mem;
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000904 if (ltemp < 0)
905 ltemp = 0; /* don't fall off front end of file */
906 ptr->cur_start_row = (JDIMENSION) ltemp;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000907 }
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000908 /* Read in the selected part of the array.
909 * During the initial write pass, we will do no actual read
910 * because the selected part is all undefined.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000911 */
Thomas G. Lanebc79e061995-08-02 00:00:00 +0000912 do_barray_io(cinfo, ptr, FALSE);
913 }
914 /* Ensure the accessed part of the array is defined; prezero if needed.
915 * To improve locality of access, we only prezero the part of the array
916 * that the caller is about to access, not the entire in-memory array.
917 */
918 if (ptr->first_undef_row < end_row) {
919 if (ptr->first_undef_row < start_row) {
920 if (writable) /* writer skipped over a section of array */
921 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
922 undef_row = start_row; /* but reader is allowed to read ahead */
923 } else {
924 undef_row = ptr->first_undef_row;
925 }
926 if (writable)
927 ptr->first_undef_row = end_row;
928 if (ptr->pre_zero) {
929 size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK);
930 undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
931 end_row -= ptr->cur_start_row;
932 while (undef_row < end_row) {
933 jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
934 undef_row++;
935 }
936 } else {
937 if (! writable) /* reader looking at undefined data */
938 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000939 }
940 }
941 /* Flag the buffer dirty if caller will write in it */
942 if (writable)
943 ptr->dirty = TRUE;
944 /* Return address of proper part of the buffer */
945 return ptr->mem_buffer + (start_row - ptr->cur_start_row);
946}
947
948
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000949/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000950 * Release all objects belonging to a specified pool.
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000951 */
952
Thomas G. Lane489583f1996-02-07 00:00:00 +0000953METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000954free_pool (j_common_ptr cinfo, int pool_id)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000955{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000956 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
957 small_pool_ptr shdr_ptr;
958 large_pool_ptr lhdr_ptr;
959 size_t space_freed;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000960
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000961 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
962 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000963
964#ifdef MEM_STATS
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000965 if (cinfo->err->trace_level > 1)
966 print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +0000967#endif
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000968
969 /* If freeing IMAGE pool, close any virtual arrays first */
970 if (pool_id == JPOOL_IMAGE) {
971 jvirt_sarray_ptr sptr;
972 jvirt_barray_ptr bptr;
973
974 for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
975 if (sptr->b_s_open) { /* there may be no backing store */
976 sptr->b_s_open = FALSE; /* prevent recursive close if error */
977 (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
978 }
979 }
980 mem->virt_sarray_list = NULL;
981 for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
982 if (bptr->b_s_open) { /* there may be no backing store */
983 bptr->b_s_open = FALSE; /* prevent recursive close if error */
984 (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
985 }
986 }
987 mem->virt_barray_list = NULL;
988 }
989
990 /* Release large objects */
991 lhdr_ptr = mem->large_list[pool_id];
992 mem->large_list[pool_id] = NULL;
993
994 while (lhdr_ptr != NULL) {
Pierre Ossman5557fd22009-03-09 10:34:53 +0000995 large_pool_ptr next_lhdr_ptr = lhdr_ptr->next;
996 space_freed = lhdr_ptr->bytes_used +
997 lhdr_ptr->bytes_left +
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +0000998 SIZEOF(large_pool_hdr);
999 jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed);
1000 mem->total_space_allocated -= space_freed;
1001 lhdr_ptr = next_lhdr_ptr;
1002 }
1003
1004 /* Release small objects */
1005 shdr_ptr = mem->small_list[pool_id];
1006 mem->small_list[pool_id] = NULL;
1007
1008 while (shdr_ptr != NULL) {
Pierre Ossman5557fd22009-03-09 10:34:53 +00001009 small_pool_ptr next_shdr_ptr = shdr_ptr->next;
1010 space_freed = shdr_ptr->bytes_used +
1011 shdr_ptr->bytes_left +
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001012 SIZEOF(small_pool_hdr);
1013 jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
1014 mem->total_space_allocated -= space_freed;
1015 shdr_ptr = next_shdr_ptr;
1016 }
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001017}
1018
1019
1020/*
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001021 * Close up shop entirely.
1022 * Note that this cannot be called unless cinfo->mem is non-NULL.
1023 */
1024
Thomas G. Lane489583f1996-02-07 00:00:00 +00001025METHODDEF(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001026self_destruct (j_common_ptr cinfo)
1027{
1028 int pool;
1029
1030 /* Close all backing store, release all memory.
1031 * Releasing pools in reverse order might help avoid fragmentation
1032 * with some (brain-damaged) malloc libraries.
1033 */
1034 for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
1035 free_pool(cinfo, pool);
1036 }
1037
1038 /* Release the memory manager control block too. */
1039 jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr));
1040 cinfo->mem = NULL; /* ensures I will be called only once */
1041
1042 jpeg_mem_term(cinfo); /* system-dependent cleanup */
1043}
1044
1045
1046/*
1047 * Memory manager initialization.
1048 * When this is called, only the error manager pointer is valid in cinfo!
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001049 */
1050
Thomas G. Lane489583f1996-02-07 00:00:00 +00001051GLOBAL(void)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001052jinit_memory_mgr (j_common_ptr cinfo)
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001053{
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001054 my_mem_ptr mem;
1055 long max_to_use;
1056 int pool;
1057 size_t test_mac;
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001058
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001059 cinfo->mem = NULL; /* for safety if init fails */
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001060
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001061 /* Check for configuration errors.
1062 * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably
1063 * doesn't reflect any real hardware alignment requirement.
1064 * The test is a little tricky: for X>0, X and X-1 have no one-bits
1065 * in common if and only if X is a power of 2, ie has only one one-bit.
1066 * Some compilers may give an "unreachable code" warning here; ignore it.
1067 */
Pierre Ossman5557fd22009-03-09 10:34:53 +00001068 if ((ALIGN_SIZE & (ALIGN_SIZE-1)) != 0)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001069 ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
1070 /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
Pierre Ossman5557fd22009-03-09 10:34:53 +00001071 * a multiple of ALIGN_SIZE.
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001072 * Again, an "unreachable code" warning may be ignored here.
1073 * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
1074 */
1075 test_mac = (size_t) MAX_ALLOC_CHUNK;
1076 if ((long) test_mac != MAX_ALLOC_CHUNK ||
Pierre Ossman5557fd22009-03-09 10:34:53 +00001077 (MAX_ALLOC_CHUNK % ALIGN_SIZE) != 0)
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001078 ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001079
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001080 max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */
1081
1082 /* Attempt to allocate memory manager's control block */
1083 mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr));
1084
1085 if (mem == NULL) {
1086 jpeg_mem_term(cinfo); /* system-dependent cleanup */
1087 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
1088 }
1089
1090 /* OK, fill in the method pointers */
1091 mem->pub.alloc_small = alloc_small;
1092 mem->pub.alloc_large = alloc_large;
1093 mem->pub.alloc_sarray = alloc_sarray;
1094 mem->pub.alloc_barray = alloc_barray;
1095 mem->pub.request_virt_sarray = request_virt_sarray;
1096 mem->pub.request_virt_barray = request_virt_barray;
1097 mem->pub.realize_virt_arrays = realize_virt_arrays;
1098 mem->pub.access_virt_sarray = access_virt_sarray;
1099 mem->pub.access_virt_barray = access_virt_barray;
1100 mem->pub.free_pool = free_pool;
1101 mem->pub.self_destruct = self_destruct;
1102
Thomas G. Lane5ead57a1998-03-27 00:00:00 +00001103 /* Make MAX_ALLOC_CHUNK accessible to other modules */
1104 mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK;
1105
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001106 /* Initialize working state */
1107 mem->pub.max_memory_to_use = max_to_use;
1108
1109 for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
1110 mem->small_list[pool] = NULL;
1111 mem->large_list[pool] = NULL;
1112 }
1113 mem->virt_sarray_list = NULL;
1114 mem->virt_barray_list = NULL;
1115
1116 mem->total_space_allocated = SIZEOF(my_memory_mgr);
1117
1118 /* Declare ourselves open for business */
1119 cinfo->mem = & mem->pub;
Thomas G. Lane88aeed41992-12-10 00:00:00 +00001120
1121 /* Check for an environment variable JPEGMEM; if found, override the
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001122 * default max_memory setting from jpeg_mem_init. Note that the
1123 * surrounding application may again override this value.
Thomas G. Lane88aeed41992-12-10 00:00:00 +00001124 * If your system doesn't support getenv(), define NO_GETENV to disable
1125 * this feature.
1126 */
1127#ifndef NO_GETENV
1128 { char * memenv;
1129
1130 if ((memenv = getenv("JPEGMEM")) != NULL) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +00001131 char ch = 'x';
1132
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001133 if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
Thomas G. Lane88aeed41992-12-10 00:00:00 +00001134 if (ch == 'm' || ch == 'M')
Thomas G. Lane36a4ccc1994-09-24 00:00:00 +00001135 max_to_use *= 1000L;
1136 mem->pub.max_memory_to_use = max_to_use * 1000L;
Thomas G. Lane88aeed41992-12-10 00:00:00 +00001137 }
1138 }
1139 }
1140#endif
1141
Thomas G. Lane4a6b7301992-03-17 00:00:00 +00001142}