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