Coverage Report

Created: 2025-06-22 06:33

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