Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(66)

Side by Side Diff: jmemmgr.c

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

Powered by Google App Engine
This is Rietveld 408576698