media/libjpeg/jmemmgr.c

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial