1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/libjpeg/jmemmgr.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1151 @@ 1.4 +/* 1.5 + * jmemmgr.c 1.6 + * 1.7 + * Copyright (C) 1991-1997, Thomas G. Lane. 1.8 + * This file is part of the Independent JPEG Group's software. 1.9 + * For conditions of distribution and use, see the accompanying README file. 1.10 + * 1.11 + * This file contains the JPEG system-independent memory management 1.12 + * routines. This code is usable across a wide variety of machines; most 1.13 + * of the system dependencies have been isolated in a separate file. 1.14 + * The major functions provided here are: 1.15 + * * pool-based allocation and freeing of memory; 1.16 + * * policy decisions about how to divide available memory among the 1.17 + * virtual arrays; 1.18 + * * control logic for swapping virtual arrays between main memory and 1.19 + * backing storage. 1.20 + * The separate system-dependent file provides the actual backing-storage 1.21 + * access code, and it contains the policy decision about how much total 1.22 + * main memory to use. 1.23 + * This file is system-dependent in the sense that some of its functions 1.24 + * are unnecessary in some systems. For example, if there is enough virtual 1.25 + * memory so that backing storage will never be used, much of the virtual 1.26 + * array control logic could be removed. (Of course, if you have that much 1.27 + * memory then you shouldn't care about a little bit of unused code...) 1.28 + */ 1.29 + 1.30 +#define JPEG_INTERNALS 1.31 +#define AM_MEMORY_MANAGER /* we define jvirt_Xarray_control structs */ 1.32 +#include "jinclude.h" 1.33 +#include "jpeglib.h" 1.34 +#include "jmemsys.h" /* import the system-dependent declarations */ 1.35 + 1.36 +#ifndef NO_GETENV 1.37 +#ifndef HAVE_STDLIB_H /* <stdlib.h> should declare getenv() */ 1.38 +extern char * getenv JPP((const char * name)); 1.39 +#endif 1.40 +#endif 1.41 + 1.42 + 1.43 +LOCAL(size_t) 1.44 +round_up_pow2 (size_t a, size_t b) 1.45 +/* a rounded up to the next multiple of b, i.e. ceil(a/b)*b */ 1.46 +/* Assumes a >= 0, b > 0, and b is a power of 2 */ 1.47 +{ 1.48 + return ((a + b - 1) & (~(b - 1))); 1.49 +} 1.50 + 1.51 + 1.52 +/* 1.53 + * Some important notes: 1.54 + * The allocation routines provided here must never return NULL. 1.55 + * They should exit to error_exit if unsuccessful. 1.56 + * 1.57 + * It's not a good idea to try to merge the sarray and barray routines, 1.58 + * even though they are textually almost the same, because samples are 1.59 + * usually stored as bytes while coefficients are shorts or ints. Thus, 1.60 + * in machines where byte pointers have a different representation from 1.61 + * word pointers, the resulting machine code could not be the same. 1.62 + */ 1.63 + 1.64 + 1.65 +/* 1.66 + * Many machines require storage alignment: longs must start on 4-byte 1.67 + * boundaries, doubles on 8-byte boundaries, etc. On such machines, malloc() 1.68 + * always returns pointers that are multiples of the worst-case alignment 1.69 + * requirement, and we had better do so too. 1.70 + * There isn't any really portable way to determine the worst-case alignment 1.71 + * requirement. This module assumes that the alignment requirement is 1.72 + * multiples of ALIGN_SIZE. 1.73 + * By default, we define ALIGN_SIZE as sizeof(double). This is necessary on some 1.74 + * workstations (where doubles really do need 8-byte alignment) and will work 1.75 + * fine on nearly everything. If your machine has lesser alignment needs, 1.76 + * you can save a few bytes by making ALIGN_SIZE smaller. 1.77 + * The only place I know of where this will NOT work is certain Macintosh 1.78 + * 680x0 compilers that define double as a 10-byte IEEE extended float. 1.79 + * Doing 10-byte alignment is counterproductive because longwords won't be 1.80 + * aligned well. Put "#define ALIGN_SIZE 4" in jconfig.h if you have 1.81 + * such a compiler. 1.82 + */ 1.83 + 1.84 +#ifndef ALIGN_SIZE /* so can override from jconfig.h */ 1.85 +#ifndef WITH_SIMD 1.86 +#define ALIGN_SIZE SIZEOF(double) 1.87 +#else 1.88 +#define ALIGN_SIZE 16 /* Most SIMD implementations require this */ 1.89 +#endif 1.90 +#endif 1.91 + 1.92 +/* 1.93 + * We allocate objects from "pools", where each pool is gotten with a single 1.94 + * request to jpeg_get_small() or jpeg_get_large(). There is no per-object 1.95 + * overhead within a pool, except for alignment padding. Each pool has a 1.96 + * header with a link to the next pool of the same class. 1.97 + * Small and large pool headers are identical except that the latter's 1.98 + * link pointer must be FAR on 80x86 machines. 1.99 + */ 1.100 + 1.101 +typedef struct small_pool_struct * small_pool_ptr; 1.102 + 1.103 +typedef struct small_pool_struct { 1.104 + small_pool_ptr next; /* next in list of pools */ 1.105 + size_t bytes_used; /* how many bytes already used within pool */ 1.106 + size_t bytes_left; /* bytes still available in this pool */ 1.107 +} small_pool_hdr; 1.108 + 1.109 +typedef struct large_pool_struct FAR * large_pool_ptr; 1.110 + 1.111 +typedef struct large_pool_struct { 1.112 + large_pool_ptr next; /* next in list of pools */ 1.113 + size_t bytes_used; /* how many bytes already used within pool */ 1.114 + size_t bytes_left; /* bytes still available in this pool */ 1.115 +} large_pool_hdr; 1.116 + 1.117 +/* 1.118 + * Here is the full definition of a memory manager object. 1.119 + */ 1.120 + 1.121 +typedef struct { 1.122 + struct jpeg_memory_mgr pub; /* public fields */ 1.123 + 1.124 + /* Each pool identifier (lifetime class) names a linked list of pools. */ 1.125 + small_pool_ptr small_list[JPOOL_NUMPOOLS]; 1.126 + large_pool_ptr large_list[JPOOL_NUMPOOLS]; 1.127 + 1.128 + /* Since we only have one lifetime class of virtual arrays, only one 1.129 + * linked list is necessary (for each datatype). Note that the virtual 1.130 + * array control blocks being linked together are actually stored somewhere 1.131 + * in the small-pool list. 1.132 + */ 1.133 + jvirt_sarray_ptr virt_sarray_list; 1.134 + jvirt_barray_ptr virt_barray_list; 1.135 + 1.136 + /* This counts total space obtained from jpeg_get_small/large */ 1.137 + size_t total_space_allocated; 1.138 + 1.139 + /* alloc_sarray and alloc_barray set this value for use by virtual 1.140 + * array routines. 1.141 + */ 1.142 + JDIMENSION last_rowsperchunk; /* from most recent alloc_sarray/barray */ 1.143 +} my_memory_mgr; 1.144 + 1.145 +typedef my_memory_mgr * my_mem_ptr; 1.146 + 1.147 + 1.148 +/* 1.149 + * The control blocks for virtual arrays. 1.150 + * Note that these blocks are allocated in the "small" pool area. 1.151 + * System-dependent info for the associated backing store (if any) is hidden 1.152 + * inside the backing_store_info struct. 1.153 + */ 1.154 + 1.155 +struct jvirt_sarray_control { 1.156 + JSAMPARRAY mem_buffer; /* => the in-memory buffer */ 1.157 + JDIMENSION rows_in_array; /* total virtual array height */ 1.158 + JDIMENSION samplesperrow; /* width of array (and of memory buffer) */ 1.159 + JDIMENSION maxaccess; /* max rows accessed by access_virt_sarray */ 1.160 + JDIMENSION rows_in_mem; /* height of memory buffer */ 1.161 + JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */ 1.162 + JDIMENSION cur_start_row; /* first logical row # in the buffer */ 1.163 + JDIMENSION first_undef_row; /* row # of first uninitialized row */ 1.164 + boolean pre_zero; /* pre-zero mode requested? */ 1.165 + boolean dirty; /* do current buffer contents need written? */ 1.166 + boolean b_s_open; /* is backing-store data valid? */ 1.167 + jvirt_sarray_ptr next; /* link to next virtual sarray control block */ 1.168 + backing_store_info b_s_info; /* System-dependent control info */ 1.169 +}; 1.170 + 1.171 +struct jvirt_barray_control { 1.172 + JBLOCKARRAY mem_buffer; /* => the in-memory buffer */ 1.173 + JDIMENSION rows_in_array; /* total virtual array height */ 1.174 + JDIMENSION blocksperrow; /* width of array (and of memory buffer) */ 1.175 + JDIMENSION maxaccess; /* max rows accessed by access_virt_barray */ 1.176 + JDIMENSION rows_in_mem; /* height of memory buffer */ 1.177 + JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */ 1.178 + JDIMENSION cur_start_row; /* first logical row # in the buffer */ 1.179 + JDIMENSION first_undef_row; /* row # of first uninitialized row */ 1.180 + boolean pre_zero; /* pre-zero mode requested? */ 1.181 + boolean dirty; /* do current buffer contents need written? */ 1.182 + boolean b_s_open; /* is backing-store data valid? */ 1.183 + jvirt_barray_ptr next; /* link to next virtual barray control block */ 1.184 + backing_store_info b_s_info; /* System-dependent control info */ 1.185 +}; 1.186 + 1.187 + 1.188 +#ifdef MEM_STATS /* optional extra stuff for statistics */ 1.189 + 1.190 +LOCAL(void) 1.191 +print_mem_stats (j_common_ptr cinfo, int pool_id) 1.192 +{ 1.193 + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; 1.194 + small_pool_ptr shdr_ptr; 1.195 + large_pool_ptr lhdr_ptr; 1.196 + 1.197 + /* Since this is only a debugging stub, we can cheat a little by using 1.198 + * fprintf directly rather than going through the trace message code. 1.199 + * This is helpful because message parm array can't handle longs. 1.200 + */ 1.201 + fprintf(stderr, "Freeing pool %d, total space = %ld\n", 1.202 + pool_id, mem->total_space_allocated); 1.203 + 1.204 + for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL; 1.205 + lhdr_ptr = lhdr_ptr->next) { 1.206 + fprintf(stderr, " Large chunk used %ld\n", 1.207 + (long) lhdr_ptr->bytes_used); 1.208 + } 1.209 + 1.210 + for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL; 1.211 + shdr_ptr = shdr_ptr->next) { 1.212 + fprintf(stderr, " Small chunk used %ld free %ld\n", 1.213 + (long) shdr_ptr->bytes_used, 1.214 + (long) shdr_ptr->bytes_left); 1.215 + } 1.216 +} 1.217 + 1.218 +#endif /* MEM_STATS */ 1.219 + 1.220 + 1.221 +LOCAL(void) 1.222 +out_of_memory (j_common_ptr cinfo, int which) 1.223 +/* Report an out-of-memory error and stop execution */ 1.224 +/* If we compiled MEM_STATS support, report alloc requests before dying */ 1.225 +{ 1.226 +#ifdef MEM_STATS 1.227 + cinfo->err->trace_level = 2; /* force self_destruct to report stats */ 1.228 +#endif 1.229 + ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which); 1.230 +} 1.231 + 1.232 + 1.233 +/* 1.234 + * Allocation of "small" objects. 1.235 + * 1.236 + * For these, we use pooled storage. When a new pool must be created, 1.237 + * we try to get enough space for the current request plus a "slop" factor, 1.238 + * where the slop will be the amount of leftover space in the new pool. 1.239 + * The speed vs. space tradeoff is largely determined by the slop values. 1.240 + * A different slop value is provided for each pool class (lifetime), 1.241 + * and we also distinguish the first pool of a class from later ones. 1.242 + * NOTE: the values given work fairly well on both 16- and 32-bit-int 1.243 + * machines, but may be too small if longs are 64 bits or more. 1.244 + * 1.245 + * Since we do not know what alignment malloc() gives us, we have to 1.246 + * allocate ALIGN_SIZE-1 extra space per pool to have room for alignment 1.247 + * adjustment. 1.248 + */ 1.249 + 1.250 +static const size_t first_pool_slop[JPOOL_NUMPOOLS] = 1.251 +{ 1.252 + 1600, /* first PERMANENT pool */ 1.253 + 16000 /* first IMAGE pool */ 1.254 +}; 1.255 + 1.256 +static const size_t extra_pool_slop[JPOOL_NUMPOOLS] = 1.257 +{ 1.258 + 0, /* additional PERMANENT pools */ 1.259 + 5000 /* additional IMAGE pools */ 1.260 +}; 1.261 + 1.262 +#define MIN_SLOP 50 /* greater than 0 to avoid futile looping */ 1.263 + 1.264 + 1.265 +METHODDEF(void *) 1.266 +alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject) 1.267 +/* Allocate a "small" object */ 1.268 +{ 1.269 + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; 1.270 + small_pool_ptr hdr_ptr, prev_hdr_ptr; 1.271 + char * data_ptr; 1.272 + size_t min_request, slop; 1.273 + 1.274 + /* 1.275 + * Round up the requested size to a multiple of ALIGN_SIZE in order 1.276 + * to assure alignment for the next object allocated in the same pool 1.277 + * and so that algorithms can straddle outside the proper area up 1.278 + * to the next alignment. 1.279 + */ 1.280 + sizeofobject = round_up_pow2(sizeofobject, ALIGN_SIZE); 1.281 + 1.282 + /* Check for unsatisfiable request (do now to ensure no overflow below) */ 1.283 + if ((SIZEOF(small_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) > MAX_ALLOC_CHUNK) 1.284 + out_of_memory(cinfo, 1); /* request exceeds malloc's ability */ 1.285 + 1.286 + /* See if space is available in any existing pool */ 1.287 + if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS) 1.288 + ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ 1.289 + prev_hdr_ptr = NULL; 1.290 + hdr_ptr = mem->small_list[pool_id]; 1.291 + while (hdr_ptr != NULL) { 1.292 + if (hdr_ptr->bytes_left >= sizeofobject) 1.293 + break; /* found pool with enough space */ 1.294 + prev_hdr_ptr = hdr_ptr; 1.295 + hdr_ptr = hdr_ptr->next; 1.296 + } 1.297 + 1.298 + /* Time to make a new pool? */ 1.299 + if (hdr_ptr == NULL) { 1.300 + /* min_request is what we need now, slop is what will be leftover */ 1.301 + min_request = SIZEOF(small_pool_hdr) + sizeofobject + ALIGN_SIZE - 1; 1.302 + if (prev_hdr_ptr == NULL) /* first pool in class? */ 1.303 + slop = first_pool_slop[pool_id]; 1.304 + else 1.305 + slop = extra_pool_slop[pool_id]; 1.306 + /* Don't ask for more than MAX_ALLOC_CHUNK */ 1.307 + if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request)) 1.308 + slop = (size_t) (MAX_ALLOC_CHUNK-min_request); 1.309 + /* Try to get space, if fail reduce slop and try again */ 1.310 + for (;;) { 1.311 + hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop); 1.312 + if (hdr_ptr != NULL) 1.313 + break; 1.314 + slop /= 2; 1.315 + if (slop < MIN_SLOP) /* give up when it gets real small */ 1.316 + out_of_memory(cinfo, 2); /* jpeg_get_small failed */ 1.317 + } 1.318 + mem->total_space_allocated += min_request + slop; 1.319 + /* Success, initialize the new pool header and add to end of list */ 1.320 + hdr_ptr->next = NULL; 1.321 + hdr_ptr->bytes_used = 0; 1.322 + hdr_ptr->bytes_left = sizeofobject + slop; 1.323 + if (prev_hdr_ptr == NULL) /* first pool in class? */ 1.324 + mem->small_list[pool_id] = hdr_ptr; 1.325 + else 1.326 + prev_hdr_ptr->next = hdr_ptr; 1.327 + } 1.328 + 1.329 + /* OK, allocate the object from the current pool */ 1.330 + data_ptr = (char *) hdr_ptr; /* point to first data byte in pool... */ 1.331 + data_ptr += SIZEOF(small_pool_hdr); /* ...by skipping the header... */ 1.332 + if ((size_t)data_ptr % ALIGN_SIZE) /* ...and adjust for alignment */ 1.333 + data_ptr += ALIGN_SIZE - (size_t)data_ptr % ALIGN_SIZE; 1.334 + data_ptr += hdr_ptr->bytes_used; /* point to place for object */ 1.335 + hdr_ptr->bytes_used += sizeofobject; 1.336 + hdr_ptr->bytes_left -= sizeofobject; 1.337 + 1.338 + return (void *) data_ptr; 1.339 +} 1.340 + 1.341 + 1.342 +/* 1.343 + * Allocation of "large" objects. 1.344 + * 1.345 + * The external semantics of these are the same as "small" objects, 1.346 + * except that FAR pointers are used on 80x86. However the pool 1.347 + * management heuristics are quite different. We assume that each 1.348 + * request is large enough that it may as well be passed directly to 1.349 + * jpeg_get_large; the pool management just links everything together 1.350 + * so that we can free it all on demand. 1.351 + * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY 1.352 + * structures. The routines that create these structures (see below) 1.353 + * deliberately bunch rows together to ensure a large request size. 1.354 + */ 1.355 + 1.356 +METHODDEF(void FAR *) 1.357 +alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject) 1.358 +/* Allocate a "large" object */ 1.359 +{ 1.360 + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; 1.361 + large_pool_ptr hdr_ptr; 1.362 + char FAR * data_ptr; 1.363 + 1.364 + /* 1.365 + * Round up the requested size to a multiple of ALIGN_SIZE so that 1.366 + * algorithms can straddle outside the proper area up to the next 1.367 + * alignment. 1.368 + */ 1.369 + sizeofobject = round_up_pow2(sizeofobject, ALIGN_SIZE); 1.370 + 1.371 + /* Check for unsatisfiable request (do now to ensure no overflow below) */ 1.372 + if ((SIZEOF(large_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) > MAX_ALLOC_CHUNK) 1.373 + out_of_memory(cinfo, 3); /* request exceeds malloc's ability */ 1.374 + 1.375 + /* Always make a new pool */ 1.376 + if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS) 1.377 + ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ 1.378 + 1.379 + hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject + 1.380 + SIZEOF(large_pool_hdr) + 1.381 + ALIGN_SIZE - 1); 1.382 + if (hdr_ptr == NULL) 1.383 + out_of_memory(cinfo, 4); /* jpeg_get_large failed */ 1.384 + mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr) + ALIGN_SIZE - 1; 1.385 + 1.386 + /* Success, initialize the new pool header and add to list */ 1.387 + hdr_ptr->next = mem->large_list[pool_id]; 1.388 + /* We maintain space counts in each pool header for statistical purposes, 1.389 + * even though they are not needed for allocation. 1.390 + */ 1.391 + hdr_ptr->bytes_used = sizeofobject; 1.392 + hdr_ptr->bytes_left = 0; 1.393 + mem->large_list[pool_id] = hdr_ptr; 1.394 + 1.395 + data_ptr = (char *) hdr_ptr; /* point to first data byte in pool... */ 1.396 + data_ptr += SIZEOF(small_pool_hdr); /* ...by skipping the header... */ 1.397 + if ((size_t)data_ptr % ALIGN_SIZE) /* ...and adjust for alignment */ 1.398 + data_ptr += ALIGN_SIZE - (size_t)data_ptr % ALIGN_SIZE; 1.399 + 1.400 + return (void FAR *) data_ptr; 1.401 +} 1.402 + 1.403 + 1.404 +/* 1.405 + * Creation of 2-D sample arrays. 1.406 + * The pointers are in near heap, the samples themselves in FAR heap. 1.407 + * 1.408 + * To minimize allocation overhead and to allow I/O of large contiguous 1.409 + * blocks, we allocate the sample rows in groups of as many rows as possible 1.410 + * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request. 1.411 + * NB: the virtual array control routines, later in this file, know about 1.412 + * this chunking of rows. The rowsperchunk value is left in the mem manager 1.413 + * object so that it can be saved away if this sarray is the workspace for 1.414 + * a virtual array. 1.415 + * 1.416 + * Since we are often upsampling with a factor 2, we align the size (not 1.417 + * the start) to 2 * ALIGN_SIZE so that the upsampling routines don't have 1.418 + * to be as careful about size. 1.419 + */ 1.420 + 1.421 +METHODDEF(JSAMPARRAY) 1.422 +alloc_sarray (j_common_ptr cinfo, int pool_id, 1.423 + JDIMENSION samplesperrow, JDIMENSION numrows) 1.424 +/* Allocate a 2-D sample array */ 1.425 +{ 1.426 + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; 1.427 + JSAMPARRAY result; 1.428 + JSAMPROW workspace; 1.429 + JDIMENSION rowsperchunk, currow, i; 1.430 + long ltemp; 1.431 + 1.432 + /* Make sure each row is properly aligned */ 1.433 + if ((ALIGN_SIZE % SIZEOF(JSAMPLE)) != 0) 1.434 + out_of_memory(cinfo, 5); /* safety check */ 1.435 + samplesperrow = (JDIMENSION)round_up_pow2(samplesperrow, (2 * ALIGN_SIZE) / SIZEOF(JSAMPLE)); 1.436 + 1.437 + /* Calculate max # of rows allowed in one allocation chunk */ 1.438 + ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) / 1.439 + ((long) samplesperrow * SIZEOF(JSAMPLE)); 1.440 + if (ltemp <= 0) 1.441 + ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); 1.442 + if (ltemp < (long) numrows) 1.443 + rowsperchunk = (JDIMENSION) ltemp; 1.444 + else 1.445 + rowsperchunk = numrows; 1.446 + mem->last_rowsperchunk = rowsperchunk; 1.447 + 1.448 + /* Get space for row pointers (small object) */ 1.449 + result = (JSAMPARRAY) alloc_small(cinfo, pool_id, 1.450 + (size_t) (numrows * SIZEOF(JSAMPROW))); 1.451 + 1.452 + /* Get the rows themselves (large objects) */ 1.453 + currow = 0; 1.454 + while (currow < numrows) { 1.455 + rowsperchunk = MIN(rowsperchunk, numrows - currow); 1.456 + workspace = (JSAMPROW) alloc_large(cinfo, pool_id, 1.457 + (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow 1.458 + * SIZEOF(JSAMPLE))); 1.459 + for (i = rowsperchunk; i > 0; i--) { 1.460 + result[currow++] = workspace; 1.461 + workspace += samplesperrow; 1.462 + } 1.463 + } 1.464 + 1.465 + return result; 1.466 +} 1.467 + 1.468 + 1.469 +/* 1.470 + * Creation of 2-D coefficient-block arrays. 1.471 + * This is essentially the same as the code for sample arrays, above. 1.472 + */ 1.473 + 1.474 +METHODDEF(JBLOCKARRAY) 1.475 +alloc_barray (j_common_ptr cinfo, int pool_id, 1.476 + JDIMENSION blocksperrow, JDIMENSION numrows) 1.477 +/* Allocate a 2-D coefficient-block array */ 1.478 +{ 1.479 + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; 1.480 + JBLOCKARRAY result; 1.481 + JBLOCKROW workspace; 1.482 + JDIMENSION rowsperchunk, currow, i; 1.483 + long ltemp; 1.484 + 1.485 + /* Make sure each row is properly aligned */ 1.486 + if ((SIZEOF(JBLOCK) % ALIGN_SIZE) != 0) 1.487 + out_of_memory(cinfo, 6); /* safety check */ 1.488 + 1.489 + /* Calculate max # of rows allowed in one allocation chunk */ 1.490 + ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) / 1.491 + ((long) blocksperrow * SIZEOF(JBLOCK)); 1.492 + if (ltemp <= 0) 1.493 + ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); 1.494 + if (ltemp < (long) numrows) 1.495 + rowsperchunk = (JDIMENSION) ltemp; 1.496 + else 1.497 + rowsperchunk = numrows; 1.498 + mem->last_rowsperchunk = rowsperchunk; 1.499 + 1.500 + /* Get space for row pointers (small object) */ 1.501 + result = (JBLOCKARRAY) alloc_small(cinfo, pool_id, 1.502 + (size_t) (numrows * SIZEOF(JBLOCKROW))); 1.503 + 1.504 + /* Get the rows themselves (large objects) */ 1.505 + currow = 0; 1.506 + while (currow < numrows) { 1.507 + rowsperchunk = MIN(rowsperchunk, numrows - currow); 1.508 + workspace = (JBLOCKROW) alloc_large(cinfo, pool_id, 1.509 + (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow 1.510 + * SIZEOF(JBLOCK))); 1.511 + for (i = rowsperchunk; i > 0; i--) { 1.512 + result[currow++] = workspace; 1.513 + workspace += blocksperrow; 1.514 + } 1.515 + } 1.516 + 1.517 + return result; 1.518 +} 1.519 + 1.520 + 1.521 +/* 1.522 + * About virtual array management: 1.523 + * 1.524 + * The above "normal" array routines are only used to allocate strip buffers 1.525 + * (as wide as the image, but just a few rows high). Full-image-sized buffers 1.526 + * are handled as "virtual" arrays. The array is still accessed a strip at a 1.527 + * time, but the memory manager must save the whole array for repeated 1.528 + * accesses. The intended implementation is that there is a strip buffer in 1.529 + * memory (as high as is possible given the desired memory limit), plus a 1.530 + * backing file that holds the rest of the array. 1.531 + * 1.532 + * The request_virt_array routines are told the total size of the image and 1.533 + * the maximum number of rows that will be accessed at once. The in-memory 1.534 + * buffer must be at least as large as the maxaccess value. 1.535 + * 1.536 + * The request routines create control blocks but not the in-memory buffers. 1.537 + * That is postponed until realize_virt_arrays is called. At that time the 1.538 + * total amount of space needed is known (approximately, anyway), so free 1.539 + * memory can be divided up fairly. 1.540 + * 1.541 + * The access_virt_array routines are responsible for making a specific strip 1.542 + * area accessible (after reading or writing the backing file, if necessary). 1.543 + * Note that the access routines are told whether the caller intends to modify 1.544 + * the accessed strip; during a read-only pass this saves having to rewrite 1.545 + * data to disk. The access routines are also responsible for pre-zeroing 1.546 + * any newly accessed rows, if pre-zeroing was requested. 1.547 + * 1.548 + * In current usage, the access requests are usually for nonoverlapping 1.549 + * strips; that is, successive access start_row numbers differ by exactly 1.550 + * num_rows = maxaccess. This means we can get good performance with simple 1.551 + * buffer dump/reload logic, by making the in-memory buffer be a multiple 1.552 + * of the access height; then there will never be accesses across bufferload 1.553 + * boundaries. The code will still work with overlapping access requests, 1.554 + * but it doesn't handle bufferload overlaps very efficiently. 1.555 + */ 1.556 + 1.557 + 1.558 +METHODDEF(jvirt_sarray_ptr) 1.559 +request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero, 1.560 + JDIMENSION samplesperrow, JDIMENSION numrows, 1.561 + JDIMENSION maxaccess) 1.562 +/* Request a virtual 2-D sample array */ 1.563 +{ 1.564 + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; 1.565 + jvirt_sarray_ptr result; 1.566 + 1.567 + /* Only IMAGE-lifetime virtual arrays are currently supported */ 1.568 + if (pool_id != JPOOL_IMAGE) 1.569 + ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ 1.570 + 1.571 + /* get control block */ 1.572 + result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id, 1.573 + SIZEOF(struct jvirt_sarray_control)); 1.574 + 1.575 + result->mem_buffer = NULL; /* marks array not yet realized */ 1.576 + result->rows_in_array = numrows; 1.577 + result->samplesperrow = samplesperrow; 1.578 + result->maxaccess = maxaccess; 1.579 + result->pre_zero = pre_zero; 1.580 + result->b_s_open = FALSE; /* no associated backing-store object */ 1.581 + result->next = mem->virt_sarray_list; /* add to list of virtual arrays */ 1.582 + mem->virt_sarray_list = result; 1.583 + 1.584 + return result; 1.585 +} 1.586 + 1.587 + 1.588 +METHODDEF(jvirt_barray_ptr) 1.589 +request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero, 1.590 + JDIMENSION blocksperrow, JDIMENSION numrows, 1.591 + JDIMENSION maxaccess) 1.592 +/* Request a virtual 2-D coefficient-block array */ 1.593 +{ 1.594 + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; 1.595 + jvirt_barray_ptr result; 1.596 + 1.597 + /* Only IMAGE-lifetime virtual arrays are currently supported */ 1.598 + if (pool_id != JPOOL_IMAGE) 1.599 + ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ 1.600 + 1.601 + /* get control block */ 1.602 + result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id, 1.603 + SIZEOF(struct jvirt_barray_control)); 1.604 + 1.605 + result->mem_buffer = NULL; /* marks array not yet realized */ 1.606 + result->rows_in_array = numrows; 1.607 + result->blocksperrow = blocksperrow; 1.608 + result->maxaccess = maxaccess; 1.609 + result->pre_zero = pre_zero; 1.610 + result->b_s_open = FALSE; /* no associated backing-store object */ 1.611 + result->next = mem->virt_barray_list; /* add to list of virtual arrays */ 1.612 + mem->virt_barray_list = result; 1.613 + 1.614 + return result; 1.615 +} 1.616 + 1.617 + 1.618 +METHODDEF(void) 1.619 +realize_virt_arrays (j_common_ptr cinfo) 1.620 +/* Allocate the in-memory buffers for any unrealized virtual arrays */ 1.621 +{ 1.622 + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; 1.623 + size_t space_per_minheight, maximum_space, avail_mem; 1.624 + size_t minheights, max_minheights; 1.625 + jvirt_sarray_ptr sptr; 1.626 + jvirt_barray_ptr bptr; 1.627 + 1.628 + /* Compute the minimum space needed (maxaccess rows in each buffer) 1.629 + * and the maximum space needed (full image height in each buffer). 1.630 + * These may be of use to the system-dependent jpeg_mem_available routine. 1.631 + */ 1.632 + space_per_minheight = 0; 1.633 + maximum_space = 0; 1.634 + for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) { 1.635 + if (sptr->mem_buffer == NULL) { /* if not realized yet */ 1.636 + space_per_minheight += (long) sptr->maxaccess * 1.637 + (long) sptr->samplesperrow * SIZEOF(JSAMPLE); 1.638 + maximum_space += (long) sptr->rows_in_array * 1.639 + (long) sptr->samplesperrow * SIZEOF(JSAMPLE); 1.640 + } 1.641 + } 1.642 + for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) { 1.643 + if (bptr->mem_buffer == NULL) { /* if not realized yet */ 1.644 + space_per_minheight += (long) bptr->maxaccess * 1.645 + (long) bptr->blocksperrow * SIZEOF(JBLOCK); 1.646 + maximum_space += (long) bptr->rows_in_array * 1.647 + (long) bptr->blocksperrow * SIZEOF(JBLOCK); 1.648 + } 1.649 + } 1.650 + 1.651 + if (space_per_minheight <= 0) 1.652 + return; /* no unrealized arrays, no work */ 1.653 + 1.654 + /* Determine amount of memory to actually use; this is system-dependent. */ 1.655 + avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space, 1.656 + mem->total_space_allocated); 1.657 + 1.658 + /* If the maximum space needed is available, make all the buffers full 1.659 + * height; otherwise parcel it out with the same number of minheights 1.660 + * in each buffer. 1.661 + */ 1.662 + if (avail_mem >= maximum_space) 1.663 + max_minheights = 1000000000L; 1.664 + else { 1.665 + max_minheights = avail_mem / space_per_minheight; 1.666 + /* If there doesn't seem to be enough space, try to get the minimum 1.667 + * anyway. This allows a "stub" implementation of jpeg_mem_available(). 1.668 + */ 1.669 + if (max_minheights <= 0) 1.670 + max_minheights = 1; 1.671 + } 1.672 + 1.673 + /* Allocate the in-memory buffers and initialize backing store as needed. */ 1.674 + 1.675 + for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) { 1.676 + if (sptr->mem_buffer == NULL) { /* if not realized yet */ 1.677 + minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L; 1.678 + if (minheights <= max_minheights) { 1.679 + /* This buffer fits in memory */ 1.680 + sptr->rows_in_mem = sptr->rows_in_array; 1.681 + } else { 1.682 + /* It doesn't fit in memory, create backing store. */ 1.683 + sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess); 1.684 + jpeg_open_backing_store(cinfo, & sptr->b_s_info, 1.685 + (long) sptr->rows_in_array * 1.686 + (long) sptr->samplesperrow * 1.687 + (long) SIZEOF(JSAMPLE)); 1.688 + sptr->b_s_open = TRUE; 1.689 + } 1.690 + sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE, 1.691 + sptr->samplesperrow, sptr->rows_in_mem); 1.692 + sptr->rowsperchunk = mem->last_rowsperchunk; 1.693 + sptr->cur_start_row = 0; 1.694 + sptr->first_undef_row = 0; 1.695 + sptr->dirty = FALSE; 1.696 + } 1.697 + } 1.698 + 1.699 + for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) { 1.700 + if (bptr->mem_buffer == NULL) { /* if not realized yet */ 1.701 + minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L; 1.702 + if (minheights <= max_minheights) { 1.703 + /* This buffer fits in memory */ 1.704 + bptr->rows_in_mem = bptr->rows_in_array; 1.705 + } else { 1.706 + /* It doesn't fit in memory, create backing store. */ 1.707 + bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess); 1.708 + jpeg_open_backing_store(cinfo, & bptr->b_s_info, 1.709 + (long) bptr->rows_in_array * 1.710 + (long) bptr->blocksperrow * 1.711 + (long) SIZEOF(JBLOCK)); 1.712 + bptr->b_s_open = TRUE; 1.713 + } 1.714 + bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE, 1.715 + bptr->blocksperrow, bptr->rows_in_mem); 1.716 + bptr->rowsperchunk = mem->last_rowsperchunk; 1.717 + bptr->cur_start_row = 0; 1.718 + bptr->first_undef_row = 0; 1.719 + bptr->dirty = FALSE; 1.720 + } 1.721 + } 1.722 +} 1.723 + 1.724 + 1.725 +LOCAL(void) 1.726 +do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing) 1.727 +/* Do backing store read or write of a virtual sample array */ 1.728 +{ 1.729 + long bytesperrow, file_offset, byte_count, rows, thisrow, i; 1.730 + 1.731 + bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE); 1.732 + file_offset = ptr->cur_start_row * bytesperrow; 1.733 + /* Loop to read or write each allocation chunk in mem_buffer */ 1.734 + for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) { 1.735 + /* One chunk, but check for short chunk at end of buffer */ 1.736 + rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i); 1.737 + /* Transfer no more than is currently defined */ 1.738 + thisrow = (long) ptr->cur_start_row + i; 1.739 + rows = MIN(rows, (long) ptr->first_undef_row - thisrow); 1.740 + /* Transfer no more than fits in file */ 1.741 + rows = MIN(rows, (long) ptr->rows_in_array - thisrow); 1.742 + if (rows <= 0) /* this chunk might be past end of file! */ 1.743 + break; 1.744 + byte_count = rows * bytesperrow; 1.745 + if (writing) 1.746 + (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info, 1.747 + (void FAR *) ptr->mem_buffer[i], 1.748 + file_offset, byte_count); 1.749 + else 1.750 + (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info, 1.751 + (void FAR *) ptr->mem_buffer[i], 1.752 + file_offset, byte_count); 1.753 + file_offset += byte_count; 1.754 + } 1.755 +} 1.756 + 1.757 + 1.758 +LOCAL(void) 1.759 +do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing) 1.760 +/* Do backing store read or write of a virtual coefficient-block array */ 1.761 +{ 1.762 + long bytesperrow, file_offset, byte_count, rows, thisrow, i; 1.763 + 1.764 + bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK); 1.765 + file_offset = ptr->cur_start_row * bytesperrow; 1.766 + /* Loop to read or write each allocation chunk in mem_buffer */ 1.767 + for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) { 1.768 + /* One chunk, but check for short chunk at end of buffer */ 1.769 + rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i); 1.770 + /* Transfer no more than is currently defined */ 1.771 + thisrow = (long) ptr->cur_start_row + i; 1.772 + rows = MIN(rows, (long) ptr->first_undef_row - thisrow); 1.773 + /* Transfer no more than fits in file */ 1.774 + rows = MIN(rows, (long) ptr->rows_in_array - thisrow); 1.775 + if (rows <= 0) /* this chunk might be past end of file! */ 1.776 + break; 1.777 + byte_count = rows * bytesperrow; 1.778 + if (writing) 1.779 + (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info, 1.780 + (void FAR *) ptr->mem_buffer[i], 1.781 + file_offset, byte_count); 1.782 + else 1.783 + (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info, 1.784 + (void FAR *) ptr->mem_buffer[i], 1.785 + file_offset, byte_count); 1.786 + file_offset += byte_count; 1.787 + } 1.788 +} 1.789 + 1.790 + 1.791 +METHODDEF(JSAMPARRAY) 1.792 +access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr, 1.793 + JDIMENSION start_row, JDIMENSION num_rows, 1.794 + boolean writable) 1.795 +/* Access the part of a virtual sample array starting at start_row */ 1.796 +/* and extending for num_rows rows. writable is true if */ 1.797 +/* caller intends to modify the accessed area. */ 1.798 +{ 1.799 + JDIMENSION end_row = start_row + num_rows; 1.800 + JDIMENSION undef_row; 1.801 + 1.802 + /* debugging check */ 1.803 + if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess || 1.804 + ptr->mem_buffer == NULL) 1.805 + ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); 1.806 + 1.807 + /* Make the desired part of the virtual array accessible */ 1.808 + if (start_row < ptr->cur_start_row || 1.809 + end_row > ptr->cur_start_row+ptr->rows_in_mem) { 1.810 + if (! ptr->b_s_open) 1.811 + ERREXIT(cinfo, JERR_VIRTUAL_BUG); 1.812 + /* Flush old buffer contents if necessary */ 1.813 + if (ptr->dirty) { 1.814 + do_sarray_io(cinfo, ptr, TRUE); 1.815 + ptr->dirty = FALSE; 1.816 + } 1.817 + /* Decide what part of virtual array to access. 1.818 + * Algorithm: if target address > current window, assume forward scan, 1.819 + * load starting at target address. If target address < current window, 1.820 + * assume backward scan, load so that target area is top of window. 1.821 + * Note that when switching from forward write to forward read, will have 1.822 + * start_row = 0, so the limiting case applies and we load from 0 anyway. 1.823 + */ 1.824 + if (start_row > ptr->cur_start_row) { 1.825 + ptr->cur_start_row = start_row; 1.826 + } else { 1.827 + /* use long arithmetic here to avoid overflow & unsigned problems */ 1.828 + long ltemp; 1.829 + 1.830 + ltemp = (long) end_row - (long) ptr->rows_in_mem; 1.831 + if (ltemp < 0) 1.832 + ltemp = 0; /* don't fall off front end of file */ 1.833 + ptr->cur_start_row = (JDIMENSION) ltemp; 1.834 + } 1.835 + /* Read in the selected part of the array. 1.836 + * During the initial write pass, we will do no actual read 1.837 + * because the selected part is all undefined. 1.838 + */ 1.839 + do_sarray_io(cinfo, ptr, FALSE); 1.840 + } 1.841 + /* Ensure the accessed part of the array is defined; prezero if needed. 1.842 + * To improve locality of access, we only prezero the part of the array 1.843 + * that the caller is about to access, not the entire in-memory array. 1.844 + */ 1.845 + if (ptr->first_undef_row < end_row) { 1.846 + if (ptr->first_undef_row < start_row) { 1.847 + if (writable) /* writer skipped over a section of array */ 1.848 + ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); 1.849 + undef_row = start_row; /* but reader is allowed to read ahead */ 1.850 + } else { 1.851 + undef_row = ptr->first_undef_row; 1.852 + } 1.853 + if (writable) 1.854 + ptr->first_undef_row = end_row; 1.855 + if (ptr->pre_zero) { 1.856 + size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE); 1.857 + undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */ 1.858 + end_row -= ptr->cur_start_row; 1.859 + while (undef_row < end_row) { 1.860 + jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow); 1.861 + undef_row++; 1.862 + } 1.863 + } else { 1.864 + if (! writable) /* reader looking at undefined data */ 1.865 + ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); 1.866 + } 1.867 + } 1.868 + /* Flag the buffer dirty if caller will write in it */ 1.869 + if (writable) 1.870 + ptr->dirty = TRUE; 1.871 + /* Return address of proper part of the buffer */ 1.872 + return ptr->mem_buffer + (start_row - ptr->cur_start_row); 1.873 +} 1.874 + 1.875 + 1.876 +METHODDEF(JBLOCKARRAY) 1.877 +access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr, 1.878 + JDIMENSION start_row, JDIMENSION num_rows, 1.879 + boolean writable) 1.880 +/* Access the part of a virtual block array starting at start_row */ 1.881 +/* and extending for num_rows rows. writable is true if */ 1.882 +/* caller intends to modify the accessed area. */ 1.883 +{ 1.884 + JDIMENSION end_row = start_row + num_rows; 1.885 + JDIMENSION undef_row; 1.886 + 1.887 + /* debugging check */ 1.888 + if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess || 1.889 + ptr->mem_buffer == NULL) 1.890 + ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); 1.891 + 1.892 + /* Make the desired part of the virtual array accessible */ 1.893 + if (start_row < ptr->cur_start_row || 1.894 + end_row > ptr->cur_start_row+ptr->rows_in_mem) { 1.895 + if (! ptr->b_s_open) 1.896 + ERREXIT(cinfo, JERR_VIRTUAL_BUG); 1.897 + /* Flush old buffer contents if necessary */ 1.898 + if (ptr->dirty) { 1.899 + do_barray_io(cinfo, ptr, TRUE); 1.900 + ptr->dirty = FALSE; 1.901 + } 1.902 + /* Decide what part of virtual array to access. 1.903 + * Algorithm: if target address > current window, assume forward scan, 1.904 + * load starting at target address. If target address < current window, 1.905 + * assume backward scan, load so that target area is top of window. 1.906 + * Note that when switching from forward write to forward read, will have 1.907 + * start_row = 0, so the limiting case applies and we load from 0 anyway. 1.908 + */ 1.909 + if (start_row > ptr->cur_start_row) { 1.910 + ptr->cur_start_row = start_row; 1.911 + } else { 1.912 + /* use long arithmetic here to avoid overflow & unsigned problems */ 1.913 + long ltemp; 1.914 + 1.915 + ltemp = (long) end_row - (long) ptr->rows_in_mem; 1.916 + if (ltemp < 0) 1.917 + ltemp = 0; /* don't fall off front end of file */ 1.918 + ptr->cur_start_row = (JDIMENSION) ltemp; 1.919 + } 1.920 + /* Read in the selected part of the array. 1.921 + * During the initial write pass, we will do no actual read 1.922 + * because the selected part is all undefined. 1.923 + */ 1.924 + do_barray_io(cinfo, ptr, FALSE); 1.925 + } 1.926 + /* Ensure the accessed part of the array is defined; prezero if needed. 1.927 + * To improve locality of access, we only prezero the part of the array 1.928 + * that the caller is about to access, not the entire in-memory array. 1.929 + */ 1.930 + if (ptr->first_undef_row < end_row) { 1.931 + if (ptr->first_undef_row < start_row) { 1.932 + if (writable) /* writer skipped over a section of array */ 1.933 + ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); 1.934 + undef_row = start_row; /* but reader is allowed to read ahead */ 1.935 + } else { 1.936 + undef_row = ptr->first_undef_row; 1.937 + } 1.938 + if (writable) 1.939 + ptr->first_undef_row = end_row; 1.940 + if (ptr->pre_zero) { 1.941 + size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK); 1.942 + undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */ 1.943 + end_row -= ptr->cur_start_row; 1.944 + while (undef_row < end_row) { 1.945 + jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow); 1.946 + undef_row++; 1.947 + } 1.948 + } else { 1.949 + if (! writable) /* reader looking at undefined data */ 1.950 + ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS); 1.951 + } 1.952 + } 1.953 + /* Flag the buffer dirty if caller will write in it */ 1.954 + if (writable) 1.955 + ptr->dirty = TRUE; 1.956 + /* Return address of proper part of the buffer */ 1.957 + return ptr->mem_buffer + (start_row - ptr->cur_start_row); 1.958 +} 1.959 + 1.960 + 1.961 +/* 1.962 + * Release all objects belonging to a specified pool. 1.963 + */ 1.964 + 1.965 +METHODDEF(void) 1.966 +free_pool (j_common_ptr cinfo, int pool_id) 1.967 +{ 1.968 + my_mem_ptr mem = (my_mem_ptr) cinfo->mem; 1.969 + small_pool_ptr shdr_ptr; 1.970 + large_pool_ptr lhdr_ptr; 1.971 + size_t space_freed; 1.972 + 1.973 + if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS) 1.974 + ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ 1.975 + 1.976 +#ifdef MEM_STATS 1.977 + if (cinfo->err->trace_level > 1) 1.978 + print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */ 1.979 +#endif 1.980 + 1.981 + /* If freeing IMAGE pool, close any virtual arrays first */ 1.982 + if (pool_id == JPOOL_IMAGE) { 1.983 + jvirt_sarray_ptr sptr; 1.984 + jvirt_barray_ptr bptr; 1.985 + 1.986 + for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) { 1.987 + if (sptr->b_s_open) { /* there may be no backing store */ 1.988 + sptr->b_s_open = FALSE; /* prevent recursive close if error */ 1.989 + (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info); 1.990 + } 1.991 + } 1.992 + mem->virt_sarray_list = NULL; 1.993 + for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) { 1.994 + if (bptr->b_s_open) { /* there may be no backing store */ 1.995 + bptr->b_s_open = FALSE; /* prevent recursive close if error */ 1.996 + (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info); 1.997 + } 1.998 + } 1.999 + mem->virt_barray_list = NULL; 1.1000 + } 1.1001 + 1.1002 + /* Release large objects */ 1.1003 + lhdr_ptr = mem->large_list[pool_id]; 1.1004 + mem->large_list[pool_id] = NULL; 1.1005 + 1.1006 + while (lhdr_ptr != NULL) { 1.1007 + large_pool_ptr next_lhdr_ptr = lhdr_ptr->next; 1.1008 + space_freed = lhdr_ptr->bytes_used + 1.1009 + lhdr_ptr->bytes_left + 1.1010 + SIZEOF(large_pool_hdr); 1.1011 + jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed); 1.1012 + mem->total_space_allocated -= space_freed; 1.1013 + lhdr_ptr = next_lhdr_ptr; 1.1014 + } 1.1015 + 1.1016 + /* Release small objects */ 1.1017 + shdr_ptr = mem->small_list[pool_id]; 1.1018 + mem->small_list[pool_id] = NULL; 1.1019 + 1.1020 + while (shdr_ptr != NULL) { 1.1021 + small_pool_ptr next_shdr_ptr = shdr_ptr->next; 1.1022 + space_freed = shdr_ptr->bytes_used + 1.1023 + shdr_ptr->bytes_left + 1.1024 + SIZEOF(small_pool_hdr); 1.1025 + jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed); 1.1026 + mem->total_space_allocated -= space_freed; 1.1027 + shdr_ptr = next_shdr_ptr; 1.1028 + } 1.1029 +} 1.1030 + 1.1031 + 1.1032 +/* 1.1033 + * Close up shop entirely. 1.1034 + * Note that this cannot be called unless cinfo->mem is non-NULL. 1.1035 + */ 1.1036 + 1.1037 +METHODDEF(void) 1.1038 +self_destruct (j_common_ptr cinfo) 1.1039 +{ 1.1040 + int pool; 1.1041 + 1.1042 + /* Close all backing store, release all memory. 1.1043 + * Releasing pools in reverse order might help avoid fragmentation 1.1044 + * with some (brain-damaged) malloc libraries. 1.1045 + */ 1.1046 + for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) { 1.1047 + free_pool(cinfo, pool); 1.1048 + } 1.1049 + 1.1050 + /* Release the memory manager control block too. */ 1.1051 + jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr)); 1.1052 + cinfo->mem = NULL; /* ensures I will be called only once */ 1.1053 + 1.1054 + jpeg_mem_term(cinfo); /* system-dependent cleanup */ 1.1055 +} 1.1056 + 1.1057 + 1.1058 +/* 1.1059 + * Memory manager initialization. 1.1060 + * When this is called, only the error manager pointer is valid in cinfo! 1.1061 + */ 1.1062 + 1.1063 +GLOBAL(void) 1.1064 +jinit_memory_mgr (j_common_ptr cinfo) 1.1065 +{ 1.1066 + my_mem_ptr mem; 1.1067 + long max_to_use; 1.1068 + int pool; 1.1069 + size_t test_mac; 1.1070 + 1.1071 + cinfo->mem = NULL; /* for safety if init fails */ 1.1072 + 1.1073 + /* Check for configuration errors. 1.1074 + * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably 1.1075 + * doesn't reflect any real hardware alignment requirement. 1.1076 + * The test is a little tricky: for X>0, X and X-1 have no one-bits 1.1077 + * in common if and only if X is a power of 2, ie has only one one-bit. 1.1078 + * Some compilers may give an "unreachable code" warning here; ignore it. 1.1079 + */ 1.1080 + if ((ALIGN_SIZE & (ALIGN_SIZE-1)) != 0) 1.1081 + ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE); 1.1082 + /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be 1.1083 + * a multiple of ALIGN_SIZE. 1.1084 + * Again, an "unreachable code" warning may be ignored here. 1.1085 + * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK. 1.1086 + */ 1.1087 + test_mac = (size_t) MAX_ALLOC_CHUNK; 1.1088 + if ((long) test_mac != MAX_ALLOC_CHUNK || 1.1089 + (MAX_ALLOC_CHUNK % ALIGN_SIZE) != 0) 1.1090 + ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK); 1.1091 + 1.1092 + max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */ 1.1093 + 1.1094 + /* Attempt to allocate memory manager's control block */ 1.1095 + mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr)); 1.1096 + 1.1097 + if (mem == NULL) { 1.1098 + jpeg_mem_term(cinfo); /* system-dependent cleanup */ 1.1099 + ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0); 1.1100 + } 1.1101 + 1.1102 + /* OK, fill in the method pointers */ 1.1103 + mem->pub.alloc_small = alloc_small; 1.1104 + mem->pub.alloc_large = alloc_large; 1.1105 + mem->pub.alloc_sarray = alloc_sarray; 1.1106 + mem->pub.alloc_barray = alloc_barray; 1.1107 + mem->pub.request_virt_sarray = request_virt_sarray; 1.1108 + mem->pub.request_virt_barray = request_virt_barray; 1.1109 + mem->pub.realize_virt_arrays = realize_virt_arrays; 1.1110 + mem->pub.access_virt_sarray = access_virt_sarray; 1.1111 + mem->pub.access_virt_barray = access_virt_barray; 1.1112 + mem->pub.free_pool = free_pool; 1.1113 + mem->pub.self_destruct = self_destruct; 1.1114 + 1.1115 + /* Make MAX_ALLOC_CHUNK accessible to other modules */ 1.1116 + mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK; 1.1117 + 1.1118 + /* Initialize working state */ 1.1119 + mem->pub.max_memory_to_use = max_to_use; 1.1120 + 1.1121 + for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) { 1.1122 + mem->small_list[pool] = NULL; 1.1123 + mem->large_list[pool] = NULL; 1.1124 + } 1.1125 + mem->virt_sarray_list = NULL; 1.1126 + mem->virt_barray_list = NULL; 1.1127 + 1.1128 + mem->total_space_allocated = SIZEOF(my_memory_mgr); 1.1129 + 1.1130 + /* Declare ourselves open for business */ 1.1131 + cinfo->mem = & mem->pub; 1.1132 + 1.1133 + /* Check for an environment variable JPEGMEM; if found, override the 1.1134 + * default max_memory setting from jpeg_mem_init. Note that the 1.1135 + * surrounding application may again override this value. 1.1136 + * If your system doesn't support getenv(), define NO_GETENV to disable 1.1137 + * this feature. 1.1138 + */ 1.1139 +#ifndef NO_GETENV 1.1140 + { char * memenv; 1.1141 + 1.1142 + if ((memenv = getenv("JPEGMEM")) != NULL) { 1.1143 + char ch = 'x'; 1.1144 + 1.1145 + if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) { 1.1146 + if (ch == 'm' || ch == 'M') 1.1147 + max_to_use *= 1000L; 1.1148 + mem->pub.max_memory_to_use = max_to_use * 1000L; 1.1149 + } 1.1150 + } 1.1151 + } 1.1152 +#endif 1.1153 + 1.1154 +}