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