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