OLD | NEW |
1 /* | 1 /* |
2 * jmemmgr.c | 2 * jmemmgr.c |
3 * | 3 * |
4 * Copyright (C) 1991-1997, Thomas G. Lane. | 4 * Copyright (C) 1991-1997, Thomas G. Lane. |
5 * This file is part of the Independent JPEG Group's software. | 5 * This file is part of the Independent JPEG Group's software. |
6 * For conditions of distribution and use, see the accompanying README file. | 6 * For conditions of distribution and use, see the accompanying README file. |
7 * | 7 * |
8 * This file contains the JPEG system-independent memory management | 8 * This file contains the JPEG system-independent memory management |
9 * routines. This code is usable across a wide variety of machines; most | 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. | 10 * of the system dependencies have been isolated in a separate file. |
(...skipping 19 matching lines...) Expand all Loading... |
30 #include "jpeglib.h" | 30 #include "jpeglib.h" |
31 #include "jmemsys.h" /* import the system-dependent declarations */ | 31 #include "jmemsys.h" /* import the system-dependent declarations */ |
32 | 32 |
33 #ifndef NO_GETENV | 33 #ifndef NO_GETENV |
34 #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare getenv() */ | 34 #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare getenv() */ |
35 extern char * getenv JPP((const char * name)); | 35 extern char * getenv JPP((const char * name)); |
36 #endif | 36 #endif |
37 #endif | 37 #endif |
38 | 38 |
39 | 39 |
| 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 |
40 /* | 49 /* |
41 * Some important notes: | 50 * Some important notes: |
42 * The allocation routines provided here must never return NULL. | 51 * The allocation routines provided here must never return NULL. |
43 * They should exit to error_exit if unsuccessful. | 52 * They should exit to error_exit if unsuccessful. |
44 * | 53 * |
45 * It's not a good idea to try to merge the sarray and barray routines, | 54 * It's not a good idea to try to merge the sarray and barray routines, |
46 * even though they are textually almost the same, because samples are | 55 * even though they are textually almost the same, because samples are |
47 * usually stored as bytes while coefficients are shorts or ints. Thus, | 56 * usually stored as bytes while coefficients are shorts or ints. Thus, |
48 * in machines where byte pointers have a different representation from | 57 * in machines where byte pointers have a different representation from |
49 * word pointers, the resulting machine code could not be the same. | 58 * word pointers, the resulting machine code could not be the same. |
(...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
258 small_pool_ptr hdr_ptr, prev_hdr_ptr; | 267 small_pool_ptr hdr_ptr, prev_hdr_ptr; |
259 char * data_ptr; | 268 char * data_ptr; |
260 size_t min_request, slop; | 269 size_t min_request, slop; |
261 | 270 |
262 /* | 271 /* |
263 * Round up the requested size to a multiple of ALIGN_SIZE in order | 272 * Round up the requested size to a multiple of ALIGN_SIZE in order |
264 * to assure alignment for the next object allocated in the same pool | 273 * to assure alignment for the next object allocated in the same pool |
265 * and so that algorithms can straddle outside the proper area up | 274 * and so that algorithms can straddle outside the proper area up |
266 * to the next alignment. | 275 * to the next alignment. |
267 */ | 276 */ |
268 sizeofobject = jround_up(sizeofobject, ALIGN_SIZE); | 277 sizeofobject = round_up_pow2(sizeofobject, ALIGN_SIZE); |
269 | 278 |
270 /* Check for unsatisfiable request (do now to ensure no overflow below) */ | 279 /* Check for unsatisfiable request (do now to ensure no overflow below) */ |
271 if ((SIZEOF(small_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) > MAX_ALLOC_CHUNK
) | 280 if ((SIZEOF(small_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) > MAX_ALLOC_CHUNK
) |
272 out_of_memory(cinfo, 1); /* request exceeds malloc's ability */ | 281 out_of_memory(cinfo, 1); /* request exceeds malloc's ability */ |
273 | 282 |
274 /* See if space is available in any existing pool */ | 283 /* See if space is available in any existing pool */ |
275 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS) | 284 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS) |
276 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ | 285 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ |
277 prev_hdr_ptr = NULL; | 286 prev_hdr_ptr = NULL; |
278 hdr_ptr = mem->small_list[pool_id]; | 287 hdr_ptr = mem->small_list[pool_id]; |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
347 { | 356 { |
348 my_mem_ptr mem = (my_mem_ptr) cinfo->mem; | 357 my_mem_ptr mem = (my_mem_ptr) cinfo->mem; |
349 large_pool_ptr hdr_ptr; | 358 large_pool_ptr hdr_ptr; |
350 char FAR * data_ptr; | 359 char FAR * data_ptr; |
351 | 360 |
352 /* | 361 /* |
353 * Round up the requested size to a multiple of ALIGN_SIZE so that | 362 * Round up the requested size to a multiple of ALIGN_SIZE so that |
354 * algorithms can straddle outside the proper area up to the next | 363 * algorithms can straddle outside the proper area up to the next |
355 * alignment. | 364 * alignment. |
356 */ | 365 */ |
357 sizeofobject = jround_up(sizeofobject, ALIGN_SIZE); | 366 sizeofobject = round_up_pow2(sizeofobject, ALIGN_SIZE); |
358 | 367 |
359 /* Check for unsatisfiable request (do now to ensure no overflow below) */ | 368 /* Check for unsatisfiable request (do now to ensure no overflow below) */ |
360 if ((SIZEOF(large_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) > MAX_ALLOC_CHUNK
) | 369 if ((SIZEOF(large_pool_hdr) + sizeofobject + ALIGN_SIZE - 1) > MAX_ALLOC_CHUNK
) |
361 out_of_memory(cinfo, 3); /* request exceeds malloc's ability */ | 370 out_of_memory(cinfo, 3); /* request exceeds malloc's ability */ |
362 | 371 |
363 /* Always make a new pool */ | 372 /* Always make a new pool */ |
364 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS) | 373 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS) |
365 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ | 374 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */ |
366 | 375 |
367 hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject + | 376 hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject + |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
413 { | 422 { |
414 my_mem_ptr mem = (my_mem_ptr) cinfo->mem; | 423 my_mem_ptr mem = (my_mem_ptr) cinfo->mem; |
415 JSAMPARRAY result; | 424 JSAMPARRAY result; |
416 JSAMPROW workspace; | 425 JSAMPROW workspace; |
417 JDIMENSION rowsperchunk, currow, i; | 426 JDIMENSION rowsperchunk, currow, i; |
418 long ltemp; | 427 long ltemp; |
419 | 428 |
420 /* Make sure each row is properly aligned */ | 429 /* Make sure each row is properly aligned */ |
421 if ((ALIGN_SIZE % SIZEOF(JSAMPLE)) != 0) | 430 if ((ALIGN_SIZE % SIZEOF(JSAMPLE)) != 0) |
422 out_of_memory(cinfo, 5); /* safety check */ | 431 out_of_memory(cinfo, 5); /* safety check */ |
423 samplesperrow = (JDIMENSION)jround_up(samplesperrow, (2 * ALIGN_SIZE) / SIZEOF
(JSAMPLE)); | 432 samplesperrow = (JDIMENSION)round_up_pow2(samplesperrow, (2 * ALIGN_SIZE) / SI
ZEOF(JSAMPLE)); |
424 | 433 |
425 /* Calculate max # of rows allowed in one allocation chunk */ | 434 /* Calculate max # of rows allowed in one allocation chunk */ |
426 ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) / | 435 ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) / |
427 ((long) samplesperrow * SIZEOF(JSAMPLE)); | 436 ((long) samplesperrow * SIZEOF(JSAMPLE)); |
428 if (ltemp <= 0) | 437 if (ltemp <= 0) |
429 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); | 438 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW); |
430 if (ltemp < (long) numrows) | 439 if (ltemp < (long) numrows) |
431 rowsperchunk = (JDIMENSION) ltemp; | 440 rowsperchunk = (JDIMENSION) ltemp; |
432 else | 441 else |
433 rowsperchunk = numrows; | 442 rowsperchunk = numrows; |
(...skipping 699 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1133 if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) { | 1142 if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) { |
1134 if (ch == 'm' || ch == 'M') | 1143 if (ch == 'm' || ch == 'M') |
1135 max_to_use *= 1000L; | 1144 max_to_use *= 1000L; |
1136 mem->pub.max_memory_to_use = max_to_use * 1000L; | 1145 mem->pub.max_memory_to_use = max_to_use * 1000L; |
1137 } | 1146 } |
1138 } | 1147 } |
1139 } | 1148 } |
1140 #endif | 1149 #endif |
1141 | 1150 |
1142 } | 1151 } |
OLD | NEW |