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