]> de.git.xonotic.org Git - xonotic/netradiant.git/blobdiff - libs/jpeg6/jmemmgr.cpp
more eol-style
[xonotic/netradiant.git] / libs / jpeg6 / jmemmgr.cpp
index 9204b324326846cf42992db849ee02acc2d33f37..375108abebd5fab97707077ff531e0b9e1b6d4a2 100644 (file)
-/*\r
-\r
- * jmemmgr.c\r
-\r
- *\r
-\r
- * Copyright (C) 1991-1995, Thomas G. Lane.\r
-\r
- * This file is part of the Independent JPEG Group's software.\r
-\r
- * For conditions of distribution and use, see the accompanying README file.\r
-\r
- *\r
-\r
- * This file contains the JPEG system-independent memory management\r
-\r
- * routines.  This code is usable across a wide variety of machines; most\r
-\r
- * of the system dependencies have been isolated in a separate file.\r
-\r
- * The major functions provided here are:\r
-\r
- *   * pool-based allocation and freeing of memory;\r
-\r
- *   * policy decisions about how to divide available memory among the\r
-\r
- *     virtual arrays;\r
-\r
- *   * control logic for swapping virtual arrays between main memory and\r
-\r
- *     backing storage.\r
-\r
- * The separate system-dependent file provides the actual backing-storage\r
-\r
- * access code, and it contains the policy decision about how much total\r
-\r
- * main memory to use.\r
-\r
- * This file is system-dependent in the sense that some of its functions\r
-\r
- * are unnecessary in some systems.  For example, if there is enough virtual\r
-\r
- * memory so that backing storage will never be used, much of the virtual\r
-\r
- * array control logic could be removed.  (Of course, if you have that much\r
-\r
- * memory then you shouldn't care about a little bit of unused code...)\r
-\r
- */\r
-\r
-\r
-\r
-#define JPEG_INTERNALS\r
-\r
-#define AM_MEMORY_MANAGER      /* we define jvirt_Xarray_control structs */\r
-\r
-#include "jinclude.h"\r
-\r
-#include "radiant_jpeglib.h"\r
-\r
-#include "jmemsys.h"           /* import the system-dependent declarations */\r
-\r
-\r
-\r
-#ifndef NO_GETENV\r
-\r
-#ifndef HAVE_STDLIB_H          /* <stdlib.h> should declare getenv() */\r
-\r
-extern char * getenv JPP((const char * name));\r
-\r
-#endif\r
-\r
-#endif\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Some important notes:\r
-\r
- *   The allocation routines provided here must never return NULL.\r
-\r
- *   They should exit to error_exit if unsuccessful.\r
-\r
- *\r
-\r
- *   It's not a good idea to try to merge the sarray and barray routines,\r
-\r
- *   even though they are textually almost the same, because samples are\r
-\r
- *   usually stored as bytes while coefficients are shorts or ints.  Thus,\r
-\r
- *   in machines where byte pointers have a different representation from\r
-\r
- *   word pointers, the resulting machine code could not be the same.\r
-\r
- */\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Many machines require storage alignment: longs must start on 4-byte\r
-\r
- * boundaries, doubles on 8-byte boundaries, etc.  On such machines, malloc()\r
-\r
- * always returns pointers that are multiples of the worst-case alignment\r
-\r
- * requirement, and we had better do so too.\r
-\r
- * There isn't any really portable way to determine the worst-case alignment\r
-\r
- * requirement.  This module assumes that the alignment requirement is\r
-\r
- * multiples of sizeof(ALIGN_TYPE).\r
-\r
- * By default, we define ALIGN_TYPE as double.  This is necessary on some\r
-\r
- * workstations (where doubles really do need 8-byte alignment) and will work\r
-\r
- * fine on nearly everything.  If your machine has lesser alignment needs,\r
-\r
- * you can save a few bytes by making ALIGN_TYPE smaller.\r
-\r
- * The only place I know of where this will NOT work is certain Macintosh\r
-\r
- * 680x0 compilers that define double as a 10-byte IEEE extended float.\r
-\r
- * Doing 10-byte alignment is counterproductive because longwords won't be\r
-\r
- * aligned well.  Put "#define ALIGN_TYPE long" in jconfig.h if you have\r
-\r
- * such a compiler.\r
-\r
- */\r
-\r
-\r
-\r
-#ifndef ALIGN_TYPE             /* so can override from jconfig.h */\r
-\r
-#define ALIGN_TYPE  double\r
-\r
-#endif\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * We allocate objects from "pools", where each pool is gotten with a single\r
-\r
- * request to jpeg_get_small() or jpeg_get_large().  There is no per-object\r
-\r
- * overhead within a pool, except for alignment padding.  Each pool has a\r
-\r
- * header with a link to the next pool of the same class.\r
-\r
- * Small and large pool headers are identical except that the latter's\r
-\r
- * link pointer must be FAR on 80x86 machines.\r
-\r
- * Notice that the "real" header fields are union'ed with a dummy ALIGN_TYPE\r
-\r
- * field.  This forces the compiler to make SIZEOF(small_pool_hdr) a multiple\r
-\r
- * of the alignment requirement of ALIGN_TYPE.\r
-\r
- */\r
-\r
-\r
-\r
-typedef union small_pool_struct * small_pool_ptr;\r
-\r
-\r
-\r
-typedef union small_pool_struct {\r
-\r
-  struct {\r
-\r
-    small_pool_ptr next;       /* next in list of pools */\r
-\r
-    size_t bytes_used;         /* how many bytes already used within pool */\r
-\r
-    size_t bytes_left;         /* bytes still available in this pool */\r
-\r
-  } hdr;\r
-\r
-  ALIGN_TYPE dummy;            /* included in union to ensure alignment */\r
-\r
-} small_pool_hdr;\r
-\r
-\r
-\r
-typedef union large_pool_struct FAR * large_pool_ptr;\r
-\r
-\r
-\r
-typedef union large_pool_struct {\r
-\r
-  struct {\r
-\r
-    large_pool_ptr next;       /* next in list of pools */\r
-\r
-    size_t bytes_used;         /* how many bytes already used within pool */\r
-\r
-    size_t bytes_left;         /* bytes still available in this pool */\r
-\r
-  } hdr;\r
-\r
-  ALIGN_TYPE dummy;            /* included in union to ensure alignment */\r
-\r
-} large_pool_hdr;\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Here is the full definition of a memory manager object.\r
-\r
- */\r
-\r
-\r
-\r
-typedef struct {\r
-\r
-  struct jpeg_memory_mgr pub;  /* public fields */\r
-\r
-\r
-\r
-  /* Each pool identifier (lifetime class) names a linked list of pools. */\r
-\r
-  small_pool_ptr small_list[JPOOL_NUMPOOLS];\r
-\r
-  large_pool_ptr large_list[JPOOL_NUMPOOLS];\r
-\r
-\r
-\r
-  /* Since we only have one lifetime class of virtual arrays, only one\r
-\r
-   * linked list is necessary (for each datatype).  Note that the virtual\r
-\r
-   * array control blocks being linked together are actually stored somewhere\r
-\r
-   * in the small-pool list.\r
-\r
-   */\r
-\r
-  jvirt_sarray_ptr virt_sarray_list;\r
-\r
-  jvirt_barray_ptr virt_barray_list;\r
-\r
-\r
-\r
-  /* This counts total space obtained from jpeg_get_small/large */\r
-\r
-  long total_space_allocated;\r
-\r
-\r
-\r
-  /* alloc_sarray and alloc_barray set this value for use by virtual\r
-\r
-   * array routines.\r
-\r
-   */\r
-\r
-  JDIMENSION last_rowsperchunk;        /* from most recent alloc_sarray/barray */\r
-\r
-} my_memory_mgr;\r
-\r
-\r
-\r
-typedef my_memory_mgr * my_mem_ptr;\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * The control blocks for virtual arrays.\r
-\r
- * Note that these blocks are allocated in the "small" pool area.\r
-\r
- * System-dependent info for the associated backing store (if any) is hidden\r
-\r
- * inside the backing_store_info struct.\r
-\r
- */\r
-\r
-\r
-\r
-struct jvirt_sarray_control {\r
-\r
-  JSAMPARRAY mem_buffer;       /* => the in-memory buffer */\r
-\r
-  JDIMENSION rows_in_array;    /* total virtual array height */\r
-\r
-  JDIMENSION samplesperrow;    /* width of array (and of memory buffer) */\r
-\r
-  JDIMENSION maxaccess;                /* max rows accessed by access_virt_sarray */\r
-\r
-  JDIMENSION rows_in_mem;      /* height of memory buffer */\r
-\r
-  JDIMENSION rowsperchunk;     /* allocation chunk size in mem_buffer */\r
-\r
-  JDIMENSION cur_start_row;    /* first logical row # in the buffer */\r
-\r
-  JDIMENSION first_undef_row;  /* row # of first uninitialized row */\r
-\r
-  boolean pre_zero;            /* pre-zero mode requested? */\r
-\r
-  boolean dirty;               /* do current buffer contents need written? */\r
-\r
-  boolean b_s_open;            /* is backing-store data valid? */\r
-\r
-  jvirt_sarray_ptr next;       /* link to next virtual sarray control block */\r
-\r
-  backing_store_info b_s_info; /* System-dependent control info */\r
-\r
-};\r
-\r
-\r
-\r
-struct jvirt_barray_control {\r
-\r
-  JBLOCKARRAY mem_buffer;      /* => the in-memory buffer */\r
-\r
-  JDIMENSION rows_in_array;    /* total virtual array height */\r
-\r
-  JDIMENSION blocksperrow;     /* width of array (and of memory buffer) */\r
-\r
-  JDIMENSION maxaccess;                /* max rows accessed by access_virt_barray */\r
-\r
-  JDIMENSION rows_in_mem;      /* height of memory buffer */\r
-\r
-  JDIMENSION rowsperchunk;     /* allocation chunk size in mem_buffer */\r
-\r
-  JDIMENSION cur_start_row;    /* first logical row # in the buffer */\r
-\r
-  JDIMENSION first_undef_row;  /* row # of first uninitialized row */\r
-\r
-  boolean pre_zero;            /* pre-zero mode requested? */\r
-\r
-  boolean dirty;               /* do current buffer contents need written? */\r
-\r
-  boolean b_s_open;            /* is backing-store data valid? */\r
-\r
-  jvirt_barray_ptr next;       /* link to next virtual barray control block */\r
-\r
-  backing_store_info b_s_info; /* System-dependent control info */\r
-\r
-};\r
-\r
-\r
-\r
-\r
-\r
-#ifdef MEM_STATS               /* optional extra stuff for statistics */\r
-\r
-\r
-\r
-LOCAL void\r
-\r
-print_mem_stats (j_common_ptr cinfo, int pool_id)\r
-\r
-{\r
-\r
-  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;\r
-\r
-  small_pool_ptr shdr_ptr;\r
-\r
-  large_pool_ptr lhdr_ptr;\r
-\r
-\r
-\r
-  /* Since this is only a debugging stub, we can cheat a little by using\r
-\r
-   * fprintf directly rather than going through the trace message code.\r
-\r
-   * This is helpful because message parm array can't handle longs.\r
-\r
-   */\r
-\r
-  fprintf(stderr, "Freeing pool %d, total space = %ld\n",\r
-\r
-         pool_id, mem->total_space_allocated);\r
-\r
-\r
-\r
-  for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;\r
-\r
-       lhdr_ptr = lhdr_ptr->hdr.next) {\r
-\r
-    fprintf(stderr, "  Large chunk used %ld\n",\r
-\r
-           (long) lhdr_ptr->hdr.bytes_used);\r
-\r
-  }\r
-\r
-\r
-\r
-  for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;\r
-\r
-       shdr_ptr = shdr_ptr->hdr.next) {\r
-\r
-    fprintf(stderr, "  Small chunk used %ld free %ld\n",\r
-\r
-           (long) shdr_ptr->hdr.bytes_used,\r
-\r
-           (long) shdr_ptr->hdr.bytes_left);\r
-\r
-  }\r
-\r
-}\r
-\r
-\r
-\r
-#endif /* MEM_STATS */\r
-\r
-\r
-\r
-\r
-\r
-LOCAL void\r
-\r
-out_of_memory (j_common_ptr cinfo, int which)\r
-\r
-/* Report an out-of-memory error and stop execution */\r
-\r
-/* If we compiled MEM_STATS support, report alloc requests before dying */\r
-\r
-{\r
-\r
-#ifdef MEM_STATS\r
-\r
-  cinfo->err->trace_level = 2; /* force self_destruct to report stats */\r
-\r
-#endif\r
-\r
-  ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Allocation of "small" objects.\r
-\r
- *\r
-\r
- * For these, we use pooled storage.  When a new pool must be created,\r
-\r
- * we try to get enough space for the current request plus a "slop" factor,\r
-\r
- * where the slop will be the amount of leftover space in the new pool.\r
-\r
- * The speed vs. space tradeoff is largely determined by the slop values.\r
-\r
- * A different slop value is provided for each pool class (lifetime),\r
-\r
- * and we also distinguish the first pool of a class from later ones.\r
-\r
- * NOTE: the values given work fairly well on both 16- and 32-bit-int\r
-\r
- * machines, but may be too small if longs are 64 bits or more.\r
-\r
- */\r
-\r
-\r
-\r
-static const size_t first_pool_slop[JPOOL_NUMPOOLS] = \r
-\r
-{\r
-\r
-       1600,                   /* first PERMANENT pool */\r
-\r
-       16000                   /* first IMAGE pool */\r
-\r
-};\r
-\r
-\r
-\r
-static const size_t extra_pool_slop[JPOOL_NUMPOOLS] = \r
-\r
-{\r
-\r
-       0,                      /* additional PERMANENT pools */\r
-\r
-       5000                    /* additional IMAGE pools */\r
-\r
-};\r
-\r
-\r
-\r
-#define MIN_SLOP  50           /* greater than 0 to avoid futile looping */\r
-\r
-\r
-\r
-\r
-\r
-METHODDEF void *\r
-\r
-alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)\r
-\r
-/* Allocate a "small" object */\r
-\r
-{\r
-\r
-  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;\r
-\r
-  small_pool_ptr hdr_ptr, prev_hdr_ptr;\r
-\r
-  char * data_ptr;\r
-\r
-  size_t odd_bytes, min_request, slop;\r
-\r
-\r
-\r
-  /* Check for unsatisfiable request (do now to ensure no overflow below) */\r
-\r
-  if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr)))\r
-\r
-    out_of_memory(cinfo, 1);   /* request exceeds malloc's ability */\r
-\r
-\r
-\r
-  /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */\r
-\r
-  odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);\r
-\r
-  if (odd_bytes > 0)\r
-\r
-    sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;\r
-\r
-\r
-\r
-  /* See if space is available in any existing pool */\r
-\r
-  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)\r
-\r
-    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);        /* safety check */\r
-\r
-  prev_hdr_ptr = NULL;\r
-\r
-  hdr_ptr = mem->small_list[pool_id];\r
-\r
-  while (hdr_ptr != NULL) {\r
-\r
-    if (hdr_ptr->hdr.bytes_left >= sizeofobject)\r
-\r
-      break;                   /* found pool with enough space */\r
-\r
-    prev_hdr_ptr = hdr_ptr;\r
-\r
-    hdr_ptr = hdr_ptr->hdr.next;\r
-\r
-  }\r
-\r
-\r
-\r
-  /* Time to make a new pool? */\r
-\r
-  if (hdr_ptr == NULL) {\r
-\r
-    /* min_request is what we need now, slop is what will be leftover */\r
-\r
-    min_request = sizeofobject + SIZEOF(small_pool_hdr);\r
-\r
-    if (prev_hdr_ptr == NULL)  /* first pool in class? */\r
-\r
-      slop = first_pool_slop[pool_id];\r
-\r
-    else\r
-\r
-      slop = extra_pool_slop[pool_id];\r
-\r
-    /* Don't ask for more than MAX_ALLOC_CHUNK */\r
-\r
-    if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))\r
-\r
-      slop = (size_t) (MAX_ALLOC_CHUNK-min_request);\r
-\r
-    /* Try to get space, if fail reduce slop and try again */\r
-\r
-    for (;;) {\r
-\r
-      hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);\r
-\r
-      if (hdr_ptr != NULL)\r
-\r
-       break;\r
-\r
-      slop /= 2;\r
-\r
-      if (slop < MIN_SLOP)     /* give up when it gets real small */\r
-\r
-       out_of_memory(cinfo, 2); /* jpeg_get_small failed */\r
-\r
-    }\r
-\r
-    mem->total_space_allocated += min_request + slop;\r
-\r
-    /* Success, initialize the new pool header and add to end of list */\r
-\r
-    hdr_ptr->hdr.next = NULL;\r
-\r
-    hdr_ptr->hdr.bytes_used = 0;\r
-\r
-    hdr_ptr->hdr.bytes_left = sizeofobject + slop;\r
-\r
-    if (prev_hdr_ptr == NULL)  /* first pool in class? */\r
-\r
-      mem->small_list[pool_id] = hdr_ptr;\r
-\r
-    else\r
-\r
-      prev_hdr_ptr->hdr.next = hdr_ptr;\r
-\r
-  }\r
-\r
-\r
-\r
-  /* OK, allocate the object from the current pool */\r
-\r
-  data_ptr = (char *) (hdr_ptr + 1); /* point to first data byte in pool */\r
-\r
-  data_ptr += hdr_ptr->hdr.bytes_used; /* point to place for object */\r
-\r
-  hdr_ptr->hdr.bytes_used += sizeofobject;\r
-\r
-  hdr_ptr->hdr.bytes_left -= sizeofobject;\r
-\r
-\r
-\r
-  return (void *) data_ptr;\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Allocation of "large" objects.\r
-\r
- *\r
-\r
- * The external semantics of these are the same as "small" objects,\r
-\r
- * except that FAR pointers are used on 80x86.  However the pool\r
-\r
- * management heuristics are quite different.  We assume that each\r
-\r
- * request is large enough that it may as well be passed directly to\r
-\r
- * jpeg_get_large; the pool management just links everything together\r
-\r
- * so that we can free it all on demand.\r
-\r
- * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY\r
-\r
- * structures.  The routines that create these structures (see below)\r
-\r
- * deliberately bunch rows together to ensure a large request size.\r
-\r
- */\r
-\r
-\r
-\r
-METHODDEF void FAR *\r
-\r
-alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)\r
-\r
-/* Allocate a "large" object */\r
-\r
-{\r
-\r
-  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;\r
-\r
-  large_pool_ptr hdr_ptr;\r
-\r
-  size_t odd_bytes;\r
-\r
-\r
-\r
-  /* Check for unsatisfiable request (do now to ensure no overflow below) */\r
-\r
-  if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)))\r
-\r
-    out_of_memory(cinfo, 3);   /* request exceeds malloc's ability */\r
-\r
-\r
-\r
-  /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */\r
-\r
-  odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);\r
-\r
-  if (odd_bytes > 0)\r
-\r
-    sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;\r
-\r
-\r
-\r
-  /* Always make a new pool */\r
-\r
-  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)\r
-\r
-    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);        /* safety check */\r
-\r
-\r
-\r
-  hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +\r
-\r
-                                           SIZEOF(large_pool_hdr));\r
-\r
-  if (hdr_ptr == NULL)\r
-\r
-    out_of_memory(cinfo, 4);   /* jpeg_get_large failed */\r
-\r
-  mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr);\r
-\r
-\r
-\r
-  /* Success, initialize the new pool header and add to list */\r
-\r
-  hdr_ptr->hdr.next = mem->large_list[pool_id];\r
-\r
-  /* We maintain space counts in each pool header for statistical purposes,\r
-\r
-   * even though they are not needed for allocation.\r
-\r
-   */\r
-\r
-  hdr_ptr->hdr.bytes_used = sizeofobject;\r
-\r
-  hdr_ptr->hdr.bytes_left = 0;\r
-\r
-  mem->large_list[pool_id] = hdr_ptr;\r
-\r
-\r
-\r
-  return (void FAR *) (hdr_ptr + 1); /* point to first data byte in pool */\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Creation of 2-D sample arrays.\r
-\r
- * The pointers are in near heap, the samples themselves in FAR heap.\r
-\r
- *\r
-\r
- * To minimize allocation overhead and to allow I/O of large contiguous\r
-\r
- * blocks, we allocate the sample rows in groups of as many rows as possible\r
-\r
- * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.\r
-\r
- * NB: the virtual array control routines, later in this file, know about\r
-\r
- * this chunking of rows.  The rowsperchunk value is left in the mem manager\r
-\r
- * object so that it can be saved away if this sarray is the workspace for\r
-\r
- * a virtual array.\r
-\r
- */\r
-\r
-\r
-\r
-METHODDEF JSAMPARRAY\r
-\r
-alloc_sarray (j_common_ptr cinfo, int pool_id,\r
-\r
-             JDIMENSION samplesperrow, JDIMENSION numrows)\r
-\r
-/* Allocate a 2-D sample array */\r
-\r
-{\r
-\r
-  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;\r
-\r
-  JSAMPARRAY result;\r
-\r
-  JSAMPROW workspace;\r
-\r
-  JDIMENSION rowsperchunk, currow, i;\r
-\r
-  long ltemp;\r
-\r
-\r
-\r
-  /* Calculate max # of rows allowed in one allocation chunk */\r
-\r
-  ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /\r
-\r
-         ((long) samplesperrow * SIZEOF(JSAMPLE));\r
-\r
-  if (ltemp <= 0)\r
-\r
-    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);\r
-\r
-  if (ltemp < (long) numrows)\r
-\r
-    rowsperchunk = (JDIMENSION) ltemp;\r
-\r
-  else\r
-\r
-    rowsperchunk = numrows;\r
-\r
-  mem->last_rowsperchunk = rowsperchunk;\r
-\r
-\r
-\r
-  /* Get space for row pointers (small object) */\r
-\r
-  result = (JSAMPARRAY) alloc_small(cinfo, pool_id,\r
-\r
-                                   (size_t) (numrows * SIZEOF(JSAMPROW)));\r
-\r
-\r
-\r
-  /* Get the rows themselves (large objects) */\r
-\r
-  currow = 0;\r
-\r
-  while (currow < numrows) {\r
-\r
-    rowsperchunk = MIN(rowsperchunk, numrows - currow);\r
-\r
-    workspace = (JSAMPROW) alloc_large(cinfo, pool_id,\r
-\r
-       (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow\r
-\r
-                 * SIZEOF(JSAMPLE)));\r
-\r
-    for (i = rowsperchunk; i > 0; i--) {\r
-\r
-      result[currow++] = workspace;\r
-\r
-      workspace += samplesperrow;\r
-\r
-    }\r
-\r
-  }\r
-\r
-\r
-\r
-  return result;\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Creation of 2-D coefficient-block arrays.\r
-\r
- * This is essentially the same as the code for sample arrays, above.\r
-\r
- */\r
-\r
-\r
-\r
-METHODDEF JBLOCKARRAY\r
-\r
-alloc_barray (j_common_ptr cinfo, int pool_id,\r
-\r
-             JDIMENSION blocksperrow, JDIMENSION numrows)\r
-\r
-/* Allocate a 2-D coefficient-block array */\r
-\r
-{\r
-\r
-  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;\r
-\r
-  JBLOCKARRAY result;\r
-\r
-  JBLOCKROW workspace;\r
-\r
-  JDIMENSION rowsperchunk, currow, i;\r
-\r
-  long ltemp;\r
-\r
-\r
-\r
-  /* Calculate max # of rows allowed in one allocation chunk */\r
-\r
-  ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /\r
-\r
-         ((long) blocksperrow * SIZEOF(JBLOCK));\r
-\r
-  if (ltemp <= 0)\r
-\r
-    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);\r
-\r
-  if (ltemp < (long) numrows)\r
-\r
-    rowsperchunk = (JDIMENSION) ltemp;\r
-\r
-  else\r
-\r
-    rowsperchunk = numrows;\r
-\r
-  mem->last_rowsperchunk = rowsperchunk;\r
-\r
-\r
-\r
-  /* Get space for row pointers (small object) */\r
-\r
-  result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,\r
-\r
-                                    (size_t) (numrows * SIZEOF(JBLOCKROW)));\r
-\r
-\r
-\r
-  /* Get the rows themselves (large objects) */\r
-\r
-  currow = 0;\r
-\r
-  while (currow < numrows) {\r
-\r
-    rowsperchunk = MIN(rowsperchunk, numrows - currow);\r
-\r
-    workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,\r
-\r
-       (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow\r
-\r
-                 * SIZEOF(JBLOCK)));\r
-\r
-    for (i = rowsperchunk; i > 0; i--) {\r
-\r
-      result[currow++] = workspace;\r
-\r
-      workspace += blocksperrow;\r
-\r
-    }\r
-\r
-  }\r
-\r
-\r
-\r
-  return result;\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * About virtual array management:\r
-\r
- *\r
-\r
- * The above "normal" array routines are only used to allocate strip buffers\r
-\r
- * (as wide as the image, but just a few rows high).  Full-image-sized buffers\r
-\r
- * are handled as "virtual" arrays.  The array is still accessed a strip at a\r
-\r
- * time, but the memory manager must save the whole array for repeated\r
-\r
- * accesses.  The intended implementation is that there is a strip buffer in\r
-\r
- * memory (as high as is possible given the desired memory limit), plus a\r
-\r
- * backing file that holds the rest of the array.\r
-\r
- *\r
-\r
- * The request_virt_array routines are told the total size of the image and\r
-\r
- * the maximum number of rows that will be accessed at once.  The in-memory\r
-\r
- * buffer must be at least as large as the maxaccess value.\r
-\r
- *\r
-\r
- * The request routines create control blocks but not the in-memory buffers.\r
-\r
- * That is postponed until realize_virt_arrays is called.  At that time the\r
-\r
- * total amount of space needed is known (approximately, anyway), so free\r
-\r
- * memory can be divided up fairly.\r
-\r
- *\r
-\r
- * The access_virt_array routines are responsible for making a specific strip\r
-\r
- * area accessible (after reading or writing the backing file, if necessary).\r
-\r
- * Note that the access routines are told whether the caller intends to modify\r
-\r
- * the accessed strip; during a read-only pass this saves having to rewrite\r
-\r
- * data to disk.  The access routines are also responsible for pre-zeroing\r
-\r
- * any newly accessed rows, if pre-zeroing was requested.\r
-\r
- *\r
-\r
- * In current usage, the access requests are usually for nonoverlapping\r
-\r
- * strips; that is, successive access start_row numbers differ by exactly\r
-\r
- * num_rows = maxaccess.  This means we can get good performance with simple\r
-\r
- * buffer dump/reload logic, by making the in-memory buffer be a multiple\r
-\r
- * of the access height; then there will never be accesses across bufferload\r
-\r
- * boundaries.  The code will still work with overlapping access requests,\r
-\r
- * but it doesn't handle bufferload overlaps very efficiently.\r
-\r
- */\r
-\r
-\r
-\r
-\r
-\r
-METHODDEF jvirt_sarray_ptr\r
-\r
-request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,\r
-\r
-                    JDIMENSION samplesperrow, JDIMENSION numrows,\r
-\r
-                    JDIMENSION maxaccess)\r
-\r
-/* Request a virtual 2-D sample array */\r
-\r
-{\r
-\r
-  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;\r
-\r
-  jvirt_sarray_ptr result;\r
-\r
-\r
-\r
-  /* Only IMAGE-lifetime virtual arrays are currently supported */\r
-\r
-  if (pool_id != JPOOL_IMAGE)\r
-\r
-    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);        /* safety check */\r
-\r
-\r
-\r
-  /* get control block */\r
-\r
-  result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,\r
-\r
-                                         SIZEOF(struct jvirt_sarray_control));\r
-\r
-\r
-\r
-  result->mem_buffer = NULL;   /* marks array not yet realized */\r
-\r
-  result->rows_in_array = numrows;\r
-\r
-  result->samplesperrow = samplesperrow;\r
-\r
-  result->maxaccess = maxaccess;\r
-\r
-  result->pre_zero = pre_zero;\r
-\r
-  result->b_s_open = FALSE;    /* no associated backing-store object */\r
-\r
-  result->next = mem->virt_sarray_list; /* add to list of virtual arrays */\r
-\r
-  mem->virt_sarray_list = result;\r
-\r
-\r
-\r
-  return result;\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-METHODDEF jvirt_barray_ptr\r
-\r
-request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,\r
-\r
-                    JDIMENSION blocksperrow, JDIMENSION numrows,\r
-\r
-                    JDIMENSION maxaccess)\r
-\r
-/* Request a virtual 2-D coefficient-block array */\r
-\r
-{\r
-\r
-  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;\r
-\r
-  jvirt_barray_ptr result;\r
-\r
-\r
-\r
-  /* Only IMAGE-lifetime virtual arrays are currently supported */\r
-\r
-  if (pool_id != JPOOL_IMAGE)\r
-\r
-    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);        /* safety check */\r
-\r
-\r
-\r
-  /* get control block */\r
-\r
-  result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,\r
-\r
-                                         SIZEOF(struct jvirt_barray_control));\r
-\r
-\r
-\r
-  result->mem_buffer = NULL;   /* marks array not yet realized */\r
-\r
-  result->rows_in_array = numrows;\r
-\r
-  result->blocksperrow = blocksperrow;\r
-\r
-  result->maxaccess = maxaccess;\r
-\r
-  result->pre_zero = pre_zero;\r
-\r
-  result->b_s_open = FALSE;    /* no associated backing-store object */\r
-\r
-  result->next = mem->virt_barray_list; /* add to list of virtual arrays */\r
-\r
-  mem->virt_barray_list = result;\r
-\r
-\r
-\r
-  return result;\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-METHODDEF void\r
-\r
-realize_virt_arrays (j_common_ptr cinfo)\r
-\r
-/* Allocate the in-memory buffers for any unrealized virtual arrays */\r
-\r
-{\r
-\r
-  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;\r
-\r
-  long space_per_minheight, maximum_space, avail_mem;\r
-\r
-  long minheights, max_minheights;\r
-\r
-  jvirt_sarray_ptr sptr;\r
-\r
-  jvirt_barray_ptr bptr;\r
-\r
-\r
-\r
-  /* Compute the minimum space needed (maxaccess rows in each buffer)\r
-\r
-   * and the maximum space needed (full image height in each buffer).\r
-\r
-   * These may be of use to the system-dependent jpeg_mem_available routine.\r
-\r
-   */\r
-\r
-  space_per_minheight = 0;\r
-\r
-  maximum_space = 0;\r
-\r
-  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {\r
-\r
-    if (sptr->mem_buffer == NULL) { /* if not realized yet */\r
-\r
-      space_per_minheight += (long) sptr->maxaccess *\r
-\r
-                            (long) sptr->samplesperrow * SIZEOF(JSAMPLE);\r
-\r
-      maximum_space += (long) sptr->rows_in_array *\r
-\r
-                      (long) sptr->samplesperrow * SIZEOF(JSAMPLE);\r
-\r
-    }\r
-\r
-  }\r
-\r
-  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {\r
-\r
-    if (bptr->mem_buffer == NULL) { /* if not realized yet */\r
-\r
-      space_per_minheight += (long) bptr->maxaccess *\r
-\r
-                            (long) bptr->blocksperrow * SIZEOF(JBLOCK);\r
-\r
-      maximum_space += (long) bptr->rows_in_array *\r
-\r
-                      (long) bptr->blocksperrow * SIZEOF(JBLOCK);\r
-\r
-    }\r
-\r
-  }\r
-\r
-\r
-\r
-  if (space_per_minheight <= 0)\r
-\r
-    return;                    /* no unrealized arrays, no work */\r
-\r
-\r
-\r
-  /* Determine amount of memory to actually use; this is system-dependent. */\r
-\r
-  avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,\r
-\r
-                                mem->total_space_allocated);\r
-\r
-\r
-\r
-  /* If the maximum space needed is available, make all the buffers full\r
-\r
-   * height; otherwise parcel it out with the same number of minheights\r
-\r
-   * in each buffer.\r
-\r
-   */\r
-\r
-  if (avail_mem >= maximum_space)\r
-\r
-    max_minheights = 1000000000L;\r
-\r
-  else {\r
-\r
-    max_minheights = avail_mem / space_per_minheight;\r
-\r
-    /* If there doesn't seem to be enough space, try to get the minimum\r
-\r
-     * anyway.  This allows a "stub" implementation of jpeg_mem_available().\r
-\r
-     */\r
-\r
-    if (max_minheights <= 0)\r
-\r
-      max_minheights = 1;\r
-\r
-  }\r
-\r
-\r
-\r
-  /* Allocate the in-memory buffers and initialize backing store as needed. */\r
-\r
-\r
-\r
-  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {\r
-\r
-    if (sptr->mem_buffer == NULL) { /* if not realized yet */\r
-\r
-      minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;\r
-\r
-      if (minheights <= max_minheights) {\r
-\r
-       /* This buffer fits in memory */\r
-\r
-       sptr->rows_in_mem = sptr->rows_in_array;\r
-\r
-      } else {\r
-\r
-       /* It doesn't fit in memory, create backing store. */\r
-\r
-       sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);\r
-\r
-       jpeg_open_backing_store(cinfo, & sptr->b_s_info,\r
-\r
-                               (long) sptr->rows_in_array *\r
-\r
-                               (long) sptr->samplesperrow *\r
-\r
-                               (long) SIZEOF(JSAMPLE));\r
-\r
-       sptr->b_s_open = TRUE;\r
-\r
-      }\r
-\r
-      sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,\r
-\r
-                                     sptr->samplesperrow, sptr->rows_in_mem);\r
-\r
-      sptr->rowsperchunk = mem->last_rowsperchunk;\r
-\r
-      sptr->cur_start_row = 0;\r
-\r
-      sptr->first_undef_row = 0;\r
-\r
-      sptr->dirty = FALSE;\r
-\r
-    }\r
-\r
-  }\r
-\r
-\r
-\r
-  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {\r
-\r
-    if (bptr->mem_buffer == NULL) { /* if not realized yet */\r
-\r
-      minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;\r
-\r
-      if (minheights <= max_minheights) {\r
-\r
-       /* This buffer fits in memory */\r
-\r
-       bptr->rows_in_mem = bptr->rows_in_array;\r
-\r
-      } else {\r
-\r
-       /* It doesn't fit in memory, create backing store. */\r
-\r
-       bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);\r
-\r
-       jpeg_open_backing_store(cinfo, & bptr->b_s_info,\r
-\r
-                               (long) bptr->rows_in_array *\r
-\r
-                               (long) bptr->blocksperrow *\r
-\r
-                               (long) SIZEOF(JBLOCK));\r
-\r
-       bptr->b_s_open = TRUE;\r
-\r
-      }\r
-\r
-      bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,\r
-\r
-                                     bptr->blocksperrow, bptr->rows_in_mem);\r
-\r
-      bptr->rowsperchunk = mem->last_rowsperchunk;\r
-\r
-      bptr->cur_start_row = 0;\r
-\r
-      bptr->first_undef_row = 0;\r
-\r
-      bptr->dirty = FALSE;\r
-\r
-    }\r
-\r
-  }\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-LOCAL void\r
-\r
-do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)\r
-\r
-/* Do backing store read or write of a virtual sample array */\r
-\r
-{\r
-\r
-  long bytesperrow, file_offset, byte_count, rows, thisrow, i;\r
-\r
-\r
-\r
-  bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE);\r
-\r
-  file_offset = ptr->cur_start_row * bytesperrow;\r
-\r
-  /* Loop to read or write each allocation chunk in mem_buffer */\r
-\r
-  for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {\r
-\r
-    /* One chunk, but check for short chunk at end of buffer */\r
-\r
-    rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);\r
-\r
-    /* Transfer no more than is currently defined */\r
-\r
-    thisrow = (long) ptr->cur_start_row + i;\r
-\r
-    rows = MIN(rows, (long) ptr->first_undef_row - thisrow);\r
-\r
-    /* Transfer no more than fits in file */\r
-\r
-    rows = MIN(rows, (long) ptr->rows_in_array - thisrow);\r
-\r
-    if (rows <= 0)             /* this chunk might be past end of file! */\r
-\r
-      break;\r
-\r
-    byte_count = rows * bytesperrow;\r
-\r
-    if (writing)\r
-\r
-      (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,\r
-\r
-                                           (void FAR *) ptr->mem_buffer[i],\r
-\r
-                                           file_offset, byte_count);\r
-\r
-    else\r
-\r
-      (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,\r
-\r
-                                          (void FAR *) ptr->mem_buffer[i],\r
-\r
-                                          file_offset, byte_count);\r
-\r
-    file_offset += byte_count;\r
-\r
-  }\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-LOCAL void\r
-\r
-do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)\r
-\r
-/* Do backing store read or write of a virtual coefficient-block array */\r
-\r
-{\r
-\r
-  long bytesperrow, file_offset, byte_count, rows, thisrow, i;\r
-\r
-\r
-\r
-  bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK);\r
-\r
-  file_offset = ptr->cur_start_row * bytesperrow;\r
-\r
-  /* Loop to read or write each allocation chunk in mem_buffer */\r
-\r
-  for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {\r
-\r
-    /* One chunk, but check for short chunk at end of buffer */\r
-\r
-    rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);\r
-\r
-    /* Transfer no more than is currently defined */\r
-\r
-    thisrow = (long) ptr->cur_start_row + i;\r
-\r
-    rows = MIN(rows, (long) ptr->first_undef_row - thisrow);\r
-\r
-    /* Transfer no more than fits in file */\r
-\r
-    rows = MIN(rows, (long) ptr->rows_in_array - thisrow);\r
-\r
-    if (rows <= 0)             /* this chunk might be past end of file! */\r
-\r
-      break;\r
-\r
-    byte_count = rows * bytesperrow;\r
-\r
-    if (writing)\r
-\r
-      (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,\r
-\r
-                                           (void FAR *) ptr->mem_buffer[i],\r
-\r
-                                           file_offset, byte_count);\r
-\r
-    else\r
-\r
-      (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,\r
-\r
-                                          (void FAR *) ptr->mem_buffer[i],\r
-\r
-                                          file_offset, byte_count);\r
-\r
-    file_offset += byte_count;\r
-\r
-  }\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-METHODDEF JSAMPARRAY\r
-\r
-access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,\r
-\r
-                   JDIMENSION start_row, JDIMENSION num_rows,\r
-\r
-                   boolean writable)\r
-\r
-/* Access the part of a virtual sample array starting at start_row */\r
-\r
-/* and extending for num_rows rows.  writable is true if  */\r
-\r
-/* caller intends to modify the accessed area. */\r
-\r
-{\r
-\r
-  JDIMENSION end_row = start_row + num_rows;\r
-\r
-  JDIMENSION undef_row;\r
-\r
-\r
-\r
-  /* debugging check */\r
-\r
-  if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||\r
-\r
-      ptr->mem_buffer == NULL)\r
-\r
-    ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);\r
-\r
-\r
-\r
-  /* Make the desired part of the virtual array accessible */\r
-\r
-  if (start_row < ptr->cur_start_row ||\r
-\r
-      end_row > ptr->cur_start_row+ptr->rows_in_mem) {\r
-\r
-    if (! ptr->b_s_open)\r
-\r
-      ERREXIT(cinfo, JERR_VIRTUAL_BUG);\r
-\r
-    /* Flush old buffer contents if necessary */\r
-\r
-    if (ptr->dirty) {\r
-\r
-      do_sarray_io(cinfo, ptr, TRUE);\r
-\r
-      ptr->dirty = FALSE;\r
-\r
-    }\r
-\r
-    /* Decide what part of virtual array to access.\r
-\r
-     * Algorithm: if target address > current window, assume forward scan,\r
-\r
-     * load starting at target address.  If target address < current window,\r
-\r
-     * assume backward scan, load so that target area is top of window.\r
-\r
-     * Note that when switching from forward write to forward read, will have\r
-\r
-     * start_row = 0, so the limiting case applies and we load from 0 anyway.\r
-\r
-     */\r
-\r
-    if (start_row > ptr->cur_start_row) {\r
-\r
-      ptr->cur_start_row = start_row;\r
-\r
-    } else {\r
-\r
-      /* use long arithmetic here to avoid overflow & unsigned problems */\r
-\r
-      long ltemp;\r
-\r
-\r
-\r
-      ltemp = (long) end_row - (long) ptr->rows_in_mem;\r
-\r
-      if (ltemp < 0)\r
-\r
-       ltemp = 0;              /* don't fall off front end of file */\r
-\r
-      ptr->cur_start_row = (JDIMENSION) ltemp;\r
-\r
-    }\r
-\r
-    /* Read in the selected part of the array.\r
-\r
-     * During the initial write pass, we will do no actual read\r
-\r
-     * because the selected part is all undefined.\r
-\r
-     */\r
-\r
-    do_sarray_io(cinfo, ptr, FALSE);\r
-\r
-  }\r
-\r
-  /* Ensure the accessed part of the array is defined; prezero if needed.\r
-\r
-   * To improve locality of access, we only prezero the part of the array\r
-\r
-   * that the caller is about to access, not the entire in-memory array.\r
-\r
-   */\r
-\r
-  if (ptr->first_undef_row < end_row) {\r
-\r
-    if (ptr->first_undef_row < start_row) {\r
-\r
-      if (writable)            /* writer skipped over a section of array */\r
-\r
-       ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);\r
-\r
-      undef_row = start_row;   /* but reader is allowed to read ahead */\r
-\r
-    } else {\r
-\r
-      undef_row = ptr->first_undef_row;\r
-\r
-    }\r
-\r
-    if (writable)\r
-\r
-      ptr->first_undef_row = end_row;\r
-\r
-    if (ptr->pre_zero) {\r
-\r
-      size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE);\r
-\r
-      undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */\r
-\r
-      end_row -= ptr->cur_start_row;\r
-\r
-      while (undef_row < end_row) {\r
-\r
-       jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);\r
-\r
-       undef_row++;\r
-\r
-      }\r
-\r
-    } else {\r
-\r
-      if (! writable)          /* reader looking at undefined data */\r
-\r
-       ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);\r
-\r
-    }\r
-\r
-  }\r
-\r
-  /* Flag the buffer dirty if caller will write in it */\r
-\r
-  if (writable)\r
-\r
-    ptr->dirty = TRUE;\r
-\r
-  /* Return address of proper part of the buffer */\r
-\r
-  return ptr->mem_buffer + (start_row - ptr->cur_start_row);\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-METHODDEF JBLOCKARRAY\r
-\r
-access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,\r
-\r
-                   JDIMENSION start_row, JDIMENSION num_rows,\r
-\r
-                   boolean writable)\r
-\r
-/* Access the part of a virtual block array starting at start_row */\r
-\r
-/* and extending for num_rows rows.  writable is true if  */\r
-\r
-/* caller intends to modify the accessed area. */\r
-\r
-{\r
-\r
-  JDIMENSION end_row = start_row + num_rows;\r
-\r
-  JDIMENSION undef_row;\r
-\r
-\r
-\r
-  /* debugging check */\r
-\r
-  if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||\r
-\r
-      ptr->mem_buffer == NULL)\r
-\r
-    ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);\r
-\r
-\r
-\r
-  /* Make the desired part of the virtual array accessible */\r
-\r
-  if (start_row < ptr->cur_start_row ||\r
-\r
-      end_row > ptr->cur_start_row+ptr->rows_in_mem) {\r
-\r
-    if (! ptr->b_s_open)\r
-\r
-      ERREXIT(cinfo, JERR_VIRTUAL_BUG);\r
-\r
-    /* Flush old buffer contents if necessary */\r
-\r
-    if (ptr->dirty) {\r
-\r
-      do_barray_io(cinfo, ptr, TRUE);\r
-\r
-      ptr->dirty = FALSE;\r
-\r
-    }\r
-\r
-    /* Decide what part of virtual array to access.\r
-\r
-     * Algorithm: if target address > current window, assume forward scan,\r
-\r
-     * load starting at target address.  If target address < current window,\r
-\r
-     * assume backward scan, load so that target area is top of window.\r
-\r
-     * Note that when switching from forward write to forward read, will have\r
-\r
-     * start_row = 0, so the limiting case applies and we load from 0 anyway.\r
-\r
-     */\r
-\r
-    if (start_row > ptr->cur_start_row) {\r
-\r
-      ptr->cur_start_row = start_row;\r
-\r
-    } else {\r
-\r
-      /* use long arithmetic here to avoid overflow & unsigned problems */\r
-\r
-      long ltemp;\r
-\r
-\r
-\r
-      ltemp = (long) end_row - (long) ptr->rows_in_mem;\r
-\r
-      if (ltemp < 0)\r
-\r
-       ltemp = 0;              /* don't fall off front end of file */\r
-\r
-      ptr->cur_start_row = (JDIMENSION) ltemp;\r
-\r
-    }\r
-\r
-    /* Read in the selected part of the array.\r
-\r
-     * During the initial write pass, we will do no actual read\r
-\r
-     * because the selected part is all undefined.\r
-\r
-     */\r
-\r
-    do_barray_io(cinfo, ptr, FALSE);\r
-\r
-  }\r
-\r
-  /* Ensure the accessed part of the array is defined; prezero if needed.\r
-\r
-   * To improve locality of access, we only prezero the part of the array\r
-\r
-   * that the caller is about to access, not the entire in-memory array.\r
-\r
-   */\r
-\r
-  if (ptr->first_undef_row < end_row) {\r
-\r
-    if (ptr->first_undef_row < start_row) {\r
-\r
-      if (writable)            /* writer skipped over a section of array */\r
-\r
-       ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);\r
-\r
-      undef_row = start_row;   /* but reader is allowed to read ahead */\r
-\r
-    } else {\r
-\r
-      undef_row = ptr->first_undef_row;\r
-\r
-    }\r
-\r
-    if (writable)\r
-\r
-      ptr->first_undef_row = end_row;\r
-\r
-    if (ptr->pre_zero) {\r
-\r
-      size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK);\r
-\r
-      undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */\r
-\r
-      end_row -= ptr->cur_start_row;\r
-\r
-      while (undef_row < end_row) {\r
-\r
-       jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);\r
-\r
-       undef_row++;\r
-\r
-      }\r
-\r
-    } else {\r
-\r
-      if (! writable)          /* reader looking at undefined data */\r
-\r
-       ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);\r
-\r
-    }\r
-\r
-  }\r
-\r
-  /* Flag the buffer dirty if caller will write in it */\r
-\r
-  if (writable)\r
-\r
-    ptr->dirty = TRUE;\r
-\r
-  /* Return address of proper part of the buffer */\r
-\r
-  return ptr->mem_buffer + (start_row - ptr->cur_start_row);\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Release all objects belonging to a specified pool.\r
-\r
- */\r
-\r
-\r
-\r
-METHODDEF void\r
-\r
-free_pool (j_common_ptr cinfo, int pool_id)\r
-\r
-{\r
-\r
-  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;\r
-\r
-  small_pool_ptr shdr_ptr;\r
-\r
-  large_pool_ptr lhdr_ptr;\r
-\r
-  size_t space_freed;\r
-\r
-\r
-\r
-  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)\r
-\r
-    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);        /* safety check */\r
-\r
-\r
-\r
-#ifdef MEM_STATS\r
-\r
-  if (cinfo->err->trace_level > 1)\r
-\r
-    print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */\r
-\r
-#endif\r
-\r
-\r
-\r
-  /* If freeing IMAGE pool, close any virtual arrays first */\r
-\r
-  if (pool_id == JPOOL_IMAGE) {\r
-\r
-    jvirt_sarray_ptr sptr;\r
-\r
-    jvirt_barray_ptr bptr;\r
-\r
-\r
-\r
-    for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {\r
-\r
-      if (sptr->b_s_open) {    /* there may be no backing store */\r
-\r
-       sptr->b_s_open = FALSE; /* prevent recursive close if error */\r
-\r
-       (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);\r
-\r
-      }\r
-\r
-    }\r
-\r
-    mem->virt_sarray_list = NULL;\r
-\r
-    for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {\r
-\r
-      if (bptr->b_s_open) {    /* there may be no backing store */\r
-\r
-       bptr->b_s_open = FALSE; /* prevent recursive close if error */\r
-\r
-       (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);\r
-\r
-      }\r
-\r
-    }\r
-\r
-    mem->virt_barray_list = NULL;\r
-\r
-  }\r
-\r
-\r
-\r
-  /* Release large objects */\r
-\r
-  lhdr_ptr = mem->large_list[pool_id];\r
-\r
-  mem->large_list[pool_id] = NULL;\r
-\r
-\r
-\r
-  while (lhdr_ptr != NULL) {\r
-\r
-    large_pool_ptr next_lhdr_ptr = lhdr_ptr->hdr.next;\r
-\r
-    space_freed = lhdr_ptr->hdr.bytes_used +\r
-\r
-                 lhdr_ptr->hdr.bytes_left +\r
-\r
-                 SIZEOF(large_pool_hdr);\r
-\r
-    jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed);\r
-\r
-    mem->total_space_allocated -= space_freed;\r
-\r
-    lhdr_ptr = next_lhdr_ptr;\r
-\r
-  }\r
-\r
-\r
-\r
-  /* Release small objects */\r
-\r
-  shdr_ptr = mem->small_list[pool_id];\r
-\r
-  mem->small_list[pool_id] = NULL;\r
-\r
-\r
-\r
-  while (shdr_ptr != NULL) {\r
-\r
-    small_pool_ptr next_shdr_ptr = shdr_ptr->hdr.next;\r
-\r
-    space_freed = shdr_ptr->hdr.bytes_used +\r
-\r
-                 shdr_ptr->hdr.bytes_left +\r
-\r
-                 SIZEOF(small_pool_hdr);\r
-\r
-    jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);\r
-\r
-    mem->total_space_allocated -= space_freed;\r
-\r
-    shdr_ptr = next_shdr_ptr;\r
-\r
-  }\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Close up shop entirely.\r
-\r
- * Note that this cannot be called unless cinfo->mem is non-NULL.\r
-\r
- */\r
-\r
-\r
-\r
-METHODDEF void\r
-\r
-self_destruct (j_common_ptr cinfo)\r
-\r
-{\r
-\r
-  int pool;\r
-\r
-\r
-\r
-  /* Close all backing store, release all memory.\r
-\r
-   * Releasing pools in reverse order might help avoid fragmentation\r
-\r
-   * with some (brain-damaged) malloc libraries.\r
-\r
-   */\r
-\r
-  for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {\r
-\r
-    free_pool(cinfo, pool);\r
-\r
-  }\r
-\r
-\r
-\r
-  /* Release the memory manager control block too. */\r
-\r
-  jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr));\r
-\r
-  cinfo->mem = NULL;           /* ensures I will be called only once */\r
-\r
-\r
-\r
-  jpeg_mem_term(cinfo);                /* system-dependent cleanup */\r
-\r
-}\r
-\r
-\r
-\r
-\r
-\r
-/*\r
-\r
- * Memory manager initialization.\r
-\r
- * When this is called, only the error manager pointer is valid in cinfo!\r
-\r
- */\r
-\r
-\r
-\r
-GLOBAL void\r
-\r
-jinit_memory_mgr (j_common_ptr cinfo)\r
-\r
-{\r
-\r
-  my_mem_ptr mem;\r
-\r
-  long max_to_use;\r
-\r
-  int pool;\r
-\r
-  size_t test_mac;\r
-\r
-\r
-\r
-  cinfo->mem = NULL;           /* for safety if init fails */\r
-\r
-\r
-\r
-  /* Check for configuration errors.\r
-\r
-   * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably\r
-\r
-   * doesn't reflect any real hardware alignment requirement.\r
-\r
-   * The test is a little tricky: for X>0, X and X-1 have no one-bits\r
-\r
-   * in common if and only if X is a power of 2, ie has only one one-bit.\r
-\r
-   * Some compilers may give an "unreachable code" warning here; ignore it.\r
-\r
-   */\r
-\r
-  if ((SIZEOF(ALIGN_TYPE) & (SIZEOF(ALIGN_TYPE)-1)) != 0)\r
-\r
-    ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);\r
-\r
-  /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be\r
-\r
-   * a multiple of SIZEOF(ALIGN_TYPE).\r
-\r
-   * Again, an "unreachable code" warning may be ignored here.\r
-\r
-   * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.\r
-\r
-   */\r
-\r
-  test_mac = (size_t) MAX_ALLOC_CHUNK;\r
-\r
-  if ((long) test_mac != MAX_ALLOC_CHUNK ||\r
-\r
-      (MAX_ALLOC_CHUNK % SIZEOF(ALIGN_TYPE)) != 0)\r
-\r
-    ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);\r
-\r
-\r
-\r
-  max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */\r
-\r
-\r
-\r
-  /* Attempt to allocate memory manager's control block */\r
-\r
-  mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr));\r
-\r
-\r
-\r
-  if (mem == NULL) {\r
-\r
-    jpeg_mem_term(cinfo);      /* system-dependent cleanup */\r
-\r
-    ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);\r
-\r
-  }\r
-\r
-\r
-\r
-  /* OK, fill in the method pointers */\r
-\r
-  mem->pub.alloc_small = alloc_small;\r
-\r
-  mem->pub.alloc_large = alloc_large;\r
-\r
-  mem->pub.alloc_sarray = alloc_sarray;\r
-\r
-  mem->pub.alloc_barray = alloc_barray;\r
-\r
-  mem->pub.request_virt_sarray = request_virt_sarray;\r
-\r
-  mem->pub.request_virt_barray = request_virt_barray;\r
-\r
-  mem->pub.realize_virt_arrays = realize_virt_arrays;\r
-\r
-  mem->pub.access_virt_sarray = access_virt_sarray;\r
-\r
-  mem->pub.access_virt_barray = access_virt_barray;\r
-\r
-  mem->pub.free_pool = free_pool;\r
-\r
-  mem->pub.self_destruct = self_destruct;\r
-\r
-\r
-\r
-  /* Initialize working state */\r
-\r
-  mem->pub.max_memory_to_use = max_to_use;\r
-\r
-\r
-\r
-  for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {\r
-\r
-    mem->small_list[pool] = NULL;\r
-\r
-    mem->large_list[pool] = NULL;\r
-\r
-  }\r
-\r
-  mem->virt_sarray_list = NULL;\r
-\r
-  mem->virt_barray_list = NULL;\r
-\r
-\r
-\r
-  mem->total_space_allocated = SIZEOF(my_memory_mgr);\r
-\r
-\r
-\r
-  /* Declare ourselves open for business */\r
-\r
-  cinfo->mem = & mem->pub;\r
-\r
-\r
-\r
-  /* Check for an environment variable JPEGMEM; if found, override the\r
-\r
-   * default max_memory setting from jpeg_mem_init.  Note that the\r
-\r
-   * surrounding application may again override this value.\r
-\r
-   * If your system doesn't support getenv(), define NO_GETENV to disable\r
-\r
-   * this feature.\r
-\r
-   */\r
-\r
-#ifndef NO_GETENV\r
-\r
-  { char * memenv;\r
-\r
-\r
-\r
-    if ((memenv = getenv("JPEGMEM")) != NULL) {\r
-\r
-      char ch = 'x';\r
-\r
-\r
-\r
-      if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {\r
-\r
-       if (ch == 'm' || ch == 'M')\r
-\r
-         max_to_use *= 1000L;\r
-\r
-       mem->pub.max_memory_to_use = max_to_use * 1000L;\r
-\r
-      }\r
-\r
-    }\r
-\r
-  }\r
-\r
-#endif\r
-\r
-\r
-\r
-}\r
-\r
+/*
+
+ * jmemmgr.c
+
+ *
+
+ * Copyright (C) 1991-1995, Thomas G. Lane.
+
+ * This file is part of the Independent JPEG Group's software.
+
+ * For conditions of distribution and use, see the accompanying README file.
+
+ *
+
+ * This file contains the JPEG system-independent memory management
+
+ * routines.  This code is usable across a wide variety of machines; most
+
+ * of the system dependencies have been isolated in a separate file.
+
+ * The major functions provided here are:
+
+ *   * pool-based allocation and freeing of memory;
+
+ *   * policy decisions about how to divide available memory among the
+
+ *     virtual arrays;
+
+ *   * control logic for swapping virtual arrays between main memory and
+
+ *     backing storage.
+
+ * The separate system-dependent file provides the actual backing-storage
+
+ * access code, and it contains the policy decision about how much total
+
+ * main memory to use.
+
+ * This file is system-dependent in the sense that some of its functions
+
+ * are unnecessary in some systems.  For example, if there is enough virtual
+
+ * memory so that backing storage will never be used, much of the virtual
+
+ * array control logic could be removed.  (Of course, if you have that much
+
+ * memory then you shouldn't care about a little bit of unused code...)
+
+ */
+
+
+
+#define JPEG_INTERNALS
+
+#define AM_MEMORY_MANAGER      /* we define jvirt_Xarray_control structs */
+
+#include "jinclude.h"
+
+#include "radiant_jpeglib.h"
+
+#include "jmemsys.h"           /* import the system-dependent declarations */
+
+
+
+#ifndef NO_GETENV
+
+#ifndef HAVE_STDLIB_H          /* <stdlib.h> should declare getenv() */
+
+extern char * getenv JPP((const char * name));
+
+#endif
+
+#endif
+
+
+
+
+
+/*
+
+ * Some important notes:
+
+ *   The allocation routines provided here must never return NULL.
+
+ *   They should exit to error_exit if unsuccessful.
+
+ *
+
+ *   It's not a good idea to try to merge the sarray and barray routines,
+
+ *   even though they are textually almost the same, because samples are
+
+ *   usually stored as bytes while coefficients are shorts or ints.  Thus,
+
+ *   in machines where byte pointers have a different representation from
+
+ *   word pointers, the resulting machine code could not be the same.
+
+ */
+
+
+
+
+
+/*
+
+ * Many machines require storage alignment: longs must start on 4-byte
+
+ * boundaries, doubles on 8-byte boundaries, etc.  On such machines, malloc()
+
+ * always returns pointers that are multiples of the worst-case alignment
+
+ * requirement, and we had better do so too.
+
+ * There isn't any really portable way to determine the worst-case alignment
+
+ * requirement.  This module assumes that the alignment requirement is
+
+ * multiples of sizeof(ALIGN_TYPE).
+
+ * By default, we define ALIGN_TYPE as double.  This is necessary on some
+
+ * workstations (where doubles really do need 8-byte alignment) and will work
+
+ * fine on nearly everything.  If your machine has lesser alignment needs,
+
+ * you can save a few bytes by making ALIGN_TYPE smaller.
+
+ * The only place I know of where this will NOT work is certain Macintosh
+
+ * 680x0 compilers that define double as a 10-byte IEEE extended float.
+
+ * Doing 10-byte alignment is counterproductive because longwords won't be
+
+ * aligned well.  Put "#define ALIGN_TYPE long" in jconfig.h if you have
+
+ * such a compiler.
+
+ */
+
+
+
+#ifndef ALIGN_TYPE             /* so can override from jconfig.h */
+
+#define ALIGN_TYPE  double
+
+#endif
+
+
+
+
+
+/*
+
+ * We allocate objects from "pools", where each pool is gotten with a single
+
+ * request to jpeg_get_small() or jpeg_get_large().  There is no per-object
+
+ * overhead within a pool, except for alignment padding.  Each pool has a
+
+ * header with a link to the next pool of the same class.
+
+ * Small and large pool headers are identical except that the latter's
+
+ * link pointer must be FAR on 80x86 machines.
+
+ * Notice that the "real" header fields are union'ed with a dummy ALIGN_TYPE
+
+ * field.  This forces the compiler to make SIZEOF(small_pool_hdr) a multiple
+
+ * of the alignment requirement of ALIGN_TYPE.
+
+ */
+
+
+
+typedef union small_pool_struct * small_pool_ptr;
+
+
+
+typedef union small_pool_struct {
+
+  struct {
+
+    small_pool_ptr next;       /* next in list of pools */
+
+    size_t bytes_used;         /* how many bytes already used within pool */
+
+    size_t bytes_left;         /* bytes still available in this pool */
+
+  } hdr;
+
+  ALIGN_TYPE dummy;            /* included in union to ensure alignment */
+
+} small_pool_hdr;
+
+
+
+typedef union large_pool_struct FAR * large_pool_ptr;
+
+
+
+typedef union large_pool_struct {
+
+  struct {
+
+    large_pool_ptr next;       /* next in list of pools */
+
+    size_t bytes_used;         /* how many bytes already used within pool */
+
+    size_t bytes_left;         /* bytes still available in this pool */
+
+  } hdr;
+
+  ALIGN_TYPE dummy;            /* included in union to ensure alignment */
+
+} large_pool_hdr;
+
+
+
+
+
+/*
+
+ * Here is the full definition of a memory manager object.
+
+ */
+
+
+
+typedef struct {
+
+  struct jpeg_memory_mgr pub;  /* public fields */
+
+
+
+  /* Each pool identifier (lifetime class) names a linked list of pools. */
+
+  small_pool_ptr small_list[JPOOL_NUMPOOLS];
+
+  large_pool_ptr large_list[JPOOL_NUMPOOLS];
+
+
+
+  /* Since we only have one lifetime class of virtual arrays, only one
+
+   * linked list is necessary (for each datatype).  Note that the virtual
+
+   * array control blocks being linked together are actually stored somewhere
+
+   * in the small-pool list.
+
+   */
+
+  jvirt_sarray_ptr virt_sarray_list;
+
+  jvirt_barray_ptr virt_barray_list;
+
+
+
+  /* This counts total space obtained from jpeg_get_small/large */
+
+  long total_space_allocated;
+
+
+
+  /* alloc_sarray and alloc_barray set this value for use by virtual
+
+   * array routines.
+
+   */
+
+  JDIMENSION last_rowsperchunk;        /* from most recent alloc_sarray/barray */
+
+} my_memory_mgr;
+
+
+
+typedef my_memory_mgr * my_mem_ptr;
+
+
+
+
+
+/*
+
+ * The control blocks for virtual arrays.
+
+ * Note that these blocks are allocated in the "small" pool area.
+
+ * System-dependent info for the associated backing store (if any) is hidden
+
+ * inside the backing_store_info struct.
+
+ */
+
+
+
+struct jvirt_sarray_control {
+
+  JSAMPARRAY mem_buffer;       /* => the in-memory buffer */
+
+  JDIMENSION rows_in_array;    /* total virtual array height */
+
+  JDIMENSION samplesperrow;    /* width of array (and of memory buffer) */
+
+  JDIMENSION maxaccess;                /* max rows accessed by access_virt_sarray */
+
+  JDIMENSION rows_in_mem;      /* height of memory buffer */
+
+  JDIMENSION rowsperchunk;     /* allocation chunk size in mem_buffer */
+
+  JDIMENSION cur_start_row;    /* first logical row # in the buffer */
+
+  JDIMENSION first_undef_row;  /* row # of first uninitialized row */
+
+  boolean pre_zero;            /* pre-zero mode requested? */
+
+  boolean dirty;               /* do current buffer contents need written? */
+
+  boolean b_s_open;            /* is backing-store data valid? */
+
+  jvirt_sarray_ptr next;       /* link to next virtual sarray control block */
+
+  backing_store_info b_s_info; /* System-dependent control info */
+
+};
+
+
+
+struct jvirt_barray_control {
+
+  JBLOCKARRAY mem_buffer;      /* => the in-memory buffer */
+
+  JDIMENSION rows_in_array;    /* total virtual array height */
+
+  JDIMENSION blocksperrow;     /* width of array (and of memory buffer) */
+
+  JDIMENSION maxaccess;                /* max rows accessed by access_virt_barray */
+
+  JDIMENSION rows_in_mem;      /* height of memory buffer */
+
+  JDIMENSION rowsperchunk;     /* allocation chunk size in mem_buffer */
+
+  JDIMENSION cur_start_row;    /* first logical row # in the buffer */
+
+  JDIMENSION first_undef_row;  /* row # of first uninitialized row */
+
+  boolean pre_zero;            /* pre-zero mode requested? */
+
+  boolean dirty;               /* do current buffer contents need written? */
+
+  boolean b_s_open;            /* is backing-store data valid? */
+
+  jvirt_barray_ptr next;       /* link to next virtual barray control block */
+
+  backing_store_info b_s_info; /* System-dependent control info */
+
+};
+
+
+
+
+
+#ifdef MEM_STATS               /* optional extra stuff for statistics */
+
+
+
+LOCAL void
+
+print_mem_stats (j_common_ptr cinfo, int pool_id)
+
+{
+
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+
+  small_pool_ptr shdr_ptr;
+
+  large_pool_ptr lhdr_ptr;
+
+
+
+  /* Since this is only a debugging stub, we can cheat a little by using
+
+   * fprintf directly rather than going through the trace message code.
+
+   * This is helpful because message parm array can't handle longs.
+
+   */
+
+  fprintf(stderr, "Freeing pool %d, total space = %ld\n",
+
+         pool_id, mem->total_space_allocated);
+
+
+
+  for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
+
+       lhdr_ptr = lhdr_ptr->hdr.next) {
+
+    fprintf(stderr, "  Large chunk used %ld\n",
+
+           (long) lhdr_ptr->hdr.bytes_used);
+
+  }
+
+
+
+  for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;
+
+       shdr_ptr = shdr_ptr->hdr.next) {
+
+    fprintf(stderr, "  Small chunk used %ld free %ld\n",
+
+           (long) shdr_ptr->hdr.bytes_used,
+
+           (long) shdr_ptr->hdr.bytes_left);
+
+  }
+
+}
+
+
+
+#endif /* MEM_STATS */
+
+
+
+
+
+LOCAL void
+
+out_of_memory (j_common_ptr cinfo, int which)
+
+/* Report an out-of-memory error and stop execution */
+
+/* If we compiled MEM_STATS support, report alloc requests before dying */
+
+{
+
+#ifdef MEM_STATS
+
+  cinfo->err->trace_level = 2; /* force self_destruct to report stats */
+
+#endif
+
+  ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
+
+}
+
+
+
+
+
+/*
+
+ * Allocation of "small" objects.
+
+ *
+
+ * For these, we use pooled storage.  When a new pool must be created,
+
+ * we try to get enough space for the current request plus a "slop" factor,
+
+ * where the slop will be the amount of leftover space in the new pool.
+
+ * The speed vs. space tradeoff is largely determined by the slop values.
+
+ * A different slop value is provided for each pool class (lifetime),
+
+ * and we also distinguish the first pool of a class from later ones.
+
+ * NOTE: the values given work fairly well on both 16- and 32-bit-int
+
+ * machines, but may be too small if longs are 64 bits or more.
+
+ */
+
+
+
+static const size_t first_pool_slop[JPOOL_NUMPOOLS] = 
+
+{
+
+       1600,                   /* first PERMANENT pool */
+
+       16000                   /* first IMAGE pool */
+
+};
+
+
+
+static const size_t extra_pool_slop[JPOOL_NUMPOOLS] = 
+
+{
+
+       0,                      /* additional PERMANENT pools */
+
+       5000                    /* additional IMAGE pools */
+
+};
+
+
+
+#define MIN_SLOP  50           /* greater than 0 to avoid futile looping */
+
+
+
+
+
+METHODDEF void *
+
+alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
+
+/* Allocate a "small" object */
+
+{
+
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+
+  small_pool_ptr hdr_ptr, prev_hdr_ptr;
+
+  char * data_ptr;
+
+  size_t odd_bytes, min_request, slop;
+
+
+
+  /* Check for unsatisfiable request (do now to ensure no overflow below) */
+
+  if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr)))
+
+    out_of_memory(cinfo, 1);   /* request exceeds malloc's ability */
+
+
+
+  /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
+
+  odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
+
+  if (odd_bytes > 0)
+
+    sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
+
+
+
+  /* See if space is available in any existing pool */
+
+  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
+
+    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);        /* safety check */
+
+  prev_hdr_ptr = NULL;
+
+  hdr_ptr = mem->small_list[pool_id];
+
+  while (hdr_ptr != NULL) {
+
+    if (hdr_ptr->hdr.bytes_left >= sizeofobject)
+
+      break;                   /* found pool with enough space */
+
+    prev_hdr_ptr = hdr_ptr;
+
+    hdr_ptr = hdr_ptr->hdr.next;
+
+  }
+
+
+
+  /* Time to make a new pool? */
+
+  if (hdr_ptr == NULL) {
+
+    /* min_request is what we need now, slop is what will be leftover */
+
+    min_request = sizeofobject + SIZEOF(small_pool_hdr);
+
+    if (prev_hdr_ptr == NULL)  /* first pool in class? */
+
+      slop = first_pool_slop[pool_id];
+
+    else
+
+      slop = extra_pool_slop[pool_id];
+
+    /* Don't ask for more than MAX_ALLOC_CHUNK */
+
+    if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
+
+      slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
+
+    /* Try to get space, if fail reduce slop and try again */
+
+    for (;;) {
+
+      hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
+
+      if (hdr_ptr != NULL)
+
+       break;
+
+      slop /= 2;
+
+      if (slop < MIN_SLOP)     /* give up when it gets real small */
+
+       out_of_memory(cinfo, 2); /* jpeg_get_small failed */
+
+    }
+
+    mem->total_space_allocated += min_request + slop;
+
+    /* Success, initialize the new pool header and add to end of list */
+
+    hdr_ptr->hdr.next = NULL;
+
+    hdr_ptr->hdr.bytes_used = 0;
+
+    hdr_ptr->hdr.bytes_left = sizeofobject + slop;
+
+    if (prev_hdr_ptr == NULL)  /* first pool in class? */
+
+      mem->small_list[pool_id] = hdr_ptr;
+
+    else
+
+      prev_hdr_ptr->hdr.next = hdr_ptr;
+
+  }
+
+
+
+  /* OK, allocate the object from the current pool */
+
+  data_ptr = (char *) (hdr_ptr + 1); /* point to first data byte in pool */
+
+  data_ptr += hdr_ptr->hdr.bytes_used; /* point to place for object */
+
+  hdr_ptr->hdr.bytes_used += sizeofobject;
+
+  hdr_ptr->hdr.bytes_left -= sizeofobject;
+
+
+
+  return (void *) data_ptr;
+
+}
+
+
+
+
+
+/*
+
+ * Allocation of "large" objects.
+
+ *
+
+ * The external semantics of these are the same as "small" objects,
+
+ * except that FAR pointers are used on 80x86.  However the pool
+
+ * management heuristics are quite different.  We assume that each
+
+ * request is large enough that it may as well be passed directly to
+
+ * jpeg_get_large; the pool management just links everything together
+
+ * so that we can free it all on demand.
+
+ * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY
+
+ * structures.  The routines that create these structures (see below)
+
+ * deliberately bunch rows together to ensure a large request size.
+
+ */
+
+
+
+METHODDEF void FAR *
+
+alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
+
+/* Allocate a "large" object */
+
+{
+
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+
+  large_pool_ptr hdr_ptr;
+
+  size_t odd_bytes;
+
+
+
+  /* Check for unsatisfiable request (do now to ensure no overflow below) */
+
+  if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)))
+
+    out_of_memory(cinfo, 3);   /* request exceeds malloc's ability */
+
+
+
+  /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
+
+  odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
+
+  if (odd_bytes > 0)
+
+    sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
+
+
+
+  /* Always make a new pool */
+
+  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
+
+    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);        /* safety check */
+
+
+
+  hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
+
+                                           SIZEOF(large_pool_hdr));
+
+  if (hdr_ptr == NULL)
+
+    out_of_memory(cinfo, 4);   /* jpeg_get_large failed */
+
+  mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr);
+
+
+
+  /* Success, initialize the new pool header and add to list */
+
+  hdr_ptr->hdr.next = mem->large_list[pool_id];
+
+  /* We maintain space counts in each pool header for statistical purposes,
+
+   * even though they are not needed for allocation.
+
+   */
+
+  hdr_ptr->hdr.bytes_used = sizeofobject;
+
+  hdr_ptr->hdr.bytes_left = 0;
+
+  mem->large_list[pool_id] = hdr_ptr;
+
+
+
+  return (void FAR *) (hdr_ptr + 1); /* point to first data byte in pool */
+
+}
+
+
+
+
+
+/*
+
+ * Creation of 2-D sample arrays.
+
+ * The pointers are in near heap, the samples themselves in FAR heap.
+
+ *
+
+ * To minimize allocation overhead and to allow I/O of large contiguous
+
+ * blocks, we allocate the sample rows in groups of as many rows as possible
+
+ * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.
+
+ * NB: the virtual array control routines, later in this file, know about
+
+ * this chunking of rows.  The rowsperchunk value is left in the mem manager
+
+ * object so that it can be saved away if this sarray is the workspace for
+
+ * a virtual array.
+
+ */
+
+
+
+METHODDEF JSAMPARRAY
+
+alloc_sarray (j_common_ptr cinfo, int pool_id,
+
+             JDIMENSION samplesperrow, JDIMENSION numrows)
+
+/* Allocate a 2-D sample array */
+
+{
+
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+
+  JSAMPARRAY result;
+
+  JSAMPROW workspace;
+
+  JDIMENSION rowsperchunk, currow, i;
+
+  long ltemp;
+
+
+
+  /* Calculate max # of rows allowed in one allocation chunk */
+
+  ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
+
+         ((long) samplesperrow * SIZEOF(JSAMPLE));
+
+  if (ltemp <= 0)
+
+    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
+
+  if (ltemp < (long) numrows)
+
+    rowsperchunk = (JDIMENSION) ltemp;
+
+  else
+
+    rowsperchunk = numrows;
+
+  mem->last_rowsperchunk = rowsperchunk;
+
+
+
+  /* Get space for row pointers (small object) */
+
+  result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
+
+                                   (size_t) (numrows * SIZEOF(JSAMPROW)));
+
+
+
+  /* Get the rows themselves (large objects) */
+
+  currow = 0;
+
+  while (currow < numrows) {
+
+    rowsperchunk = MIN(rowsperchunk, numrows - currow);
+
+    workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
+
+       (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
+
+                 * SIZEOF(JSAMPLE)));
+
+    for (i = rowsperchunk; i > 0; i--) {
+
+      result[currow++] = workspace;
+
+      workspace += samplesperrow;
+
+    }
+
+  }
+
+
+
+  return result;
+
+}
+
+
+
+
+
+/*
+
+ * Creation of 2-D coefficient-block arrays.
+
+ * This is essentially the same as the code for sample arrays, above.
+
+ */
+
+
+
+METHODDEF JBLOCKARRAY
+
+alloc_barray (j_common_ptr cinfo, int pool_id,
+
+             JDIMENSION blocksperrow, JDIMENSION numrows)
+
+/* Allocate a 2-D coefficient-block array */
+
+{
+
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+
+  JBLOCKARRAY result;
+
+  JBLOCKROW workspace;
+
+  JDIMENSION rowsperchunk, currow, i;
+
+  long ltemp;
+
+
+
+  /* Calculate max # of rows allowed in one allocation chunk */
+
+  ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
+
+         ((long) blocksperrow * SIZEOF(JBLOCK));
+
+  if (ltemp <= 0)
+
+    ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
+
+  if (ltemp < (long) numrows)
+
+    rowsperchunk = (JDIMENSION) ltemp;
+
+  else
+
+    rowsperchunk = numrows;
+
+  mem->last_rowsperchunk = rowsperchunk;
+
+
+
+  /* Get space for row pointers (small object) */
+
+  result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
+
+                                    (size_t) (numrows * SIZEOF(JBLOCKROW)));
+
+
+
+  /* Get the rows themselves (large objects) */
+
+  currow = 0;
+
+  while (currow < numrows) {
+
+    rowsperchunk = MIN(rowsperchunk, numrows - currow);
+
+    workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
+
+       (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
+
+                 * SIZEOF(JBLOCK)));
+
+    for (i = rowsperchunk; i > 0; i--) {
+
+      result[currow++] = workspace;
+
+      workspace += blocksperrow;
+
+    }
+
+  }
+
+
+
+  return result;
+
+}
+
+
+
+
+
+/*
+
+ * About virtual array management:
+
+ *
+
+ * The above "normal" array routines are only used to allocate strip buffers
+
+ * (as wide as the image, but just a few rows high).  Full-image-sized buffers
+
+ * are handled as "virtual" arrays.  The array is still accessed a strip at a
+
+ * time, but the memory manager must save the whole array for repeated
+
+ * accesses.  The intended implementation is that there is a strip buffer in
+
+ * memory (as high as is possible given the desired memory limit), plus a
+
+ * backing file that holds the rest of the array.
+
+ *
+
+ * The request_virt_array routines are told the total size of the image and
+
+ * the maximum number of rows that will be accessed at once.  The in-memory
+
+ * buffer must be at least as large as the maxaccess value.
+
+ *
+
+ * The request routines create control blocks but not the in-memory buffers.
+
+ * That is postponed until realize_virt_arrays is called.  At that time the
+
+ * total amount of space needed is known (approximately, anyway), so free
+
+ * memory can be divided up fairly.
+
+ *
+
+ * The access_virt_array routines are responsible for making a specific strip
+
+ * area accessible (after reading or writing the backing file, if necessary).
+
+ * Note that the access routines are told whether the caller intends to modify
+
+ * the accessed strip; during a read-only pass this saves having to rewrite
+
+ * data to disk.  The access routines are also responsible for pre-zeroing
+
+ * any newly accessed rows, if pre-zeroing was requested.
+
+ *
+
+ * In current usage, the access requests are usually for nonoverlapping
+
+ * strips; that is, successive access start_row numbers differ by exactly
+
+ * num_rows = maxaccess.  This means we can get good performance with simple
+
+ * buffer dump/reload logic, by making the in-memory buffer be a multiple
+
+ * of the access height; then there will never be accesses across bufferload
+
+ * boundaries.  The code will still work with overlapping access requests,
+
+ * but it doesn't handle bufferload overlaps very efficiently.
+
+ */
+
+
+
+
+
+METHODDEF jvirt_sarray_ptr
+
+request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
+
+                    JDIMENSION samplesperrow, JDIMENSION numrows,
+
+                    JDIMENSION maxaccess)
+
+/* Request a virtual 2-D sample array */
+
+{
+
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+
+  jvirt_sarray_ptr result;
+
+
+
+  /* Only IMAGE-lifetime virtual arrays are currently supported */
+
+  if (pool_id != JPOOL_IMAGE)
+
+    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);        /* safety check */
+
+
+
+  /* get control block */
+
+  result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
+
+                                         SIZEOF(struct jvirt_sarray_control));
+
+
+
+  result->mem_buffer = NULL;   /* marks array not yet realized */
+
+  result->rows_in_array = numrows;
+
+  result->samplesperrow = samplesperrow;
+
+  result->maxaccess = maxaccess;
+
+  result->pre_zero = pre_zero;
+
+  result->b_s_open = FALSE;    /* no associated backing-store object */
+
+  result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
+
+  mem->virt_sarray_list = result;
+
+
+
+  return result;
+
+}
+
+
+
+
+
+METHODDEF jvirt_barray_ptr
+
+request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
+
+                    JDIMENSION blocksperrow, JDIMENSION numrows,
+
+                    JDIMENSION maxaccess)
+
+/* Request a virtual 2-D coefficient-block array */
+
+{
+
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+
+  jvirt_barray_ptr result;
+
+
+
+  /* Only IMAGE-lifetime virtual arrays are currently supported */
+
+  if (pool_id != JPOOL_IMAGE)
+
+    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);        /* safety check */
+
+
+
+  /* get control block */
+
+  result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
+
+                                         SIZEOF(struct jvirt_barray_control));
+
+
+
+  result->mem_buffer = NULL;   /* marks array not yet realized */
+
+  result->rows_in_array = numrows;
+
+  result->blocksperrow = blocksperrow;
+
+  result->maxaccess = maxaccess;
+
+  result->pre_zero = pre_zero;
+
+  result->b_s_open = FALSE;    /* no associated backing-store object */
+
+  result->next = mem->virt_barray_list; /* add to list of virtual arrays */
+
+  mem->virt_barray_list = result;
+
+
+
+  return result;
+
+}
+
+
+
+
+
+METHODDEF void
+
+realize_virt_arrays (j_common_ptr cinfo)
+
+/* Allocate the in-memory buffers for any unrealized virtual arrays */
+
+{
+
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+
+  long space_per_minheight, maximum_space, avail_mem;
+
+  long minheights, max_minheights;
+
+  jvirt_sarray_ptr sptr;
+
+  jvirt_barray_ptr bptr;
+
+
+
+  /* Compute the minimum space needed (maxaccess rows in each buffer)
+
+   * and the maximum space needed (full image height in each buffer).
+
+   * These may be of use to the system-dependent jpeg_mem_available routine.
+
+   */
+
+  space_per_minheight = 0;
+
+  maximum_space = 0;
+
+  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
+
+    if (sptr->mem_buffer == NULL) { /* if not realized yet */
+
+      space_per_minheight += (long) sptr->maxaccess *
+
+                            (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
+
+      maximum_space += (long) sptr->rows_in_array *
+
+                      (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
+
+    }
+
+  }
+
+  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
+
+    if (bptr->mem_buffer == NULL) { /* if not realized yet */
+
+      space_per_minheight += (long) bptr->maxaccess *
+
+                            (long) bptr->blocksperrow * SIZEOF(JBLOCK);
+
+      maximum_space += (long) bptr->rows_in_array *
+
+                      (long) bptr->blocksperrow * SIZEOF(JBLOCK);
+
+    }
+
+  }
+
+
+
+  if (space_per_minheight <= 0)
+
+    return;                    /* no unrealized arrays, no work */
+
+
+
+  /* Determine amount of memory to actually use; this is system-dependent. */
+
+  avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
+
+                                mem->total_space_allocated);
+
+
+
+  /* If the maximum space needed is available, make all the buffers full
+
+   * height; otherwise parcel it out with the same number of minheights
+
+   * in each buffer.
+
+   */
+
+  if (avail_mem >= maximum_space)
+
+    max_minheights = 1000000000L;
+
+  else {
+
+    max_minheights = avail_mem / space_per_minheight;
+
+    /* If there doesn't seem to be enough space, try to get the minimum
+
+     * anyway.  This allows a "stub" implementation of jpeg_mem_available().
+
+     */
+
+    if (max_minheights <= 0)
+
+      max_minheights = 1;
+
+  }
+
+
+
+  /* Allocate the in-memory buffers and initialize backing store as needed. */
+
+
+
+  for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
+
+    if (sptr->mem_buffer == NULL) { /* if not realized yet */
+
+      minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
+
+      if (minheights <= max_minheights) {
+
+       /* This buffer fits in memory */
+
+       sptr->rows_in_mem = sptr->rows_in_array;
+
+      } else {
+
+       /* It doesn't fit in memory, create backing store. */
+
+       sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
+
+       jpeg_open_backing_store(cinfo, & sptr->b_s_info,
+
+                               (long) sptr->rows_in_array *
+
+                               (long) sptr->samplesperrow *
+
+                               (long) SIZEOF(JSAMPLE));
+
+       sptr->b_s_open = TRUE;
+
+      }
+
+      sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
+
+                                     sptr->samplesperrow, sptr->rows_in_mem);
+
+      sptr->rowsperchunk = mem->last_rowsperchunk;
+
+      sptr->cur_start_row = 0;
+
+      sptr->first_undef_row = 0;
+
+      sptr->dirty = FALSE;
+
+    }
+
+  }
+
+
+
+  for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
+
+    if (bptr->mem_buffer == NULL) { /* if not realized yet */
+
+      minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
+
+      if (minheights <= max_minheights) {
+
+       /* This buffer fits in memory */
+
+       bptr->rows_in_mem = bptr->rows_in_array;
+
+      } else {
+
+       /* It doesn't fit in memory, create backing store. */
+
+       bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
+
+       jpeg_open_backing_store(cinfo, & bptr->b_s_info,
+
+                               (long) bptr->rows_in_array *
+
+                               (long) bptr->blocksperrow *
+
+                               (long) SIZEOF(JBLOCK));
+
+       bptr->b_s_open = TRUE;
+
+      }
+
+      bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
+
+                                     bptr->blocksperrow, bptr->rows_in_mem);
+
+      bptr->rowsperchunk = mem->last_rowsperchunk;
+
+      bptr->cur_start_row = 0;
+
+      bptr->first_undef_row = 0;
+
+      bptr->dirty = FALSE;
+
+    }
+
+  }
+
+}
+
+
+
+
+
+LOCAL void
+
+do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
+
+/* Do backing store read or write of a virtual sample array */
+
+{
+
+  long bytesperrow, file_offset, byte_count, rows, thisrow, i;
+
+
+
+  bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE);
+
+  file_offset = ptr->cur_start_row * bytesperrow;
+
+  /* Loop to read or write each allocation chunk in mem_buffer */
+
+  for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
+
+    /* One chunk, but check for short chunk at end of buffer */
+
+    rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
+
+    /* Transfer no more than is currently defined */
+
+    thisrow = (long) ptr->cur_start_row + i;
+
+    rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
+
+    /* Transfer no more than fits in file */
+
+    rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
+
+    if (rows <= 0)             /* this chunk might be past end of file! */
+
+      break;
+
+    byte_count = rows * bytesperrow;
+
+    if (writing)
+
+      (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
+
+                                           (void FAR *) ptr->mem_buffer[i],
+
+                                           file_offset, byte_count);
+
+    else
+
+      (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
+
+                                          (void FAR *) ptr->mem_buffer[i],
+
+                                          file_offset, byte_count);
+
+    file_offset += byte_count;
+
+  }
+
+}
+
+
+
+
+
+LOCAL void
+
+do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
+
+/* Do backing store read or write of a virtual coefficient-block array */
+
+{
+
+  long bytesperrow, file_offset, byte_count, rows, thisrow, i;
+
+
+
+  bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK);
+
+  file_offset = ptr->cur_start_row * bytesperrow;
+
+  /* Loop to read or write each allocation chunk in mem_buffer */
+
+  for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
+
+    /* One chunk, but check for short chunk at end of buffer */
+
+    rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
+
+    /* Transfer no more than is currently defined */
+
+    thisrow = (long) ptr->cur_start_row + i;
+
+    rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
+
+    /* Transfer no more than fits in file */
+
+    rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
+
+    if (rows <= 0)             /* this chunk might be past end of file! */
+
+      break;
+
+    byte_count = rows * bytesperrow;
+
+    if (writing)
+
+      (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
+
+                                           (void FAR *) ptr->mem_buffer[i],
+
+                                           file_offset, byte_count);
+
+    else
+
+      (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
+
+                                          (void FAR *) ptr->mem_buffer[i],
+
+                                          file_offset, byte_count);
+
+    file_offset += byte_count;
+
+  }
+
+}
+
+
+
+
+
+METHODDEF JSAMPARRAY
+
+access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
+
+                   JDIMENSION start_row, JDIMENSION num_rows,
+
+                   boolean writable)
+
+/* Access the part of a virtual sample array starting at start_row */
+
+/* and extending for num_rows rows.  writable is true if  */
+
+/* caller intends to modify the accessed area. */
+
+{
+
+  JDIMENSION end_row = start_row + num_rows;
+
+  JDIMENSION undef_row;
+
+
+
+  /* debugging check */
+
+  if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
+
+      ptr->mem_buffer == NULL)
+
+    ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+
+
+
+  /* Make the desired part of the virtual array accessible */
+
+  if (start_row < ptr->cur_start_row ||
+
+      end_row > ptr->cur_start_row+ptr->rows_in_mem) {
+
+    if (! ptr->b_s_open)
+
+      ERREXIT(cinfo, JERR_VIRTUAL_BUG);
+
+    /* Flush old buffer contents if necessary */
+
+    if (ptr->dirty) {
+
+      do_sarray_io(cinfo, ptr, TRUE);
+
+      ptr->dirty = FALSE;
+
+    }
+
+    /* Decide what part of virtual array to access.
+
+     * Algorithm: if target address > current window, assume forward scan,
+
+     * load starting at target address.  If target address < current window,
+
+     * assume backward scan, load so that target area is top of window.
+
+     * Note that when switching from forward write to forward read, will have
+
+     * start_row = 0, so the limiting case applies and we load from 0 anyway.
+
+     */
+
+    if (start_row > ptr->cur_start_row) {
+
+      ptr->cur_start_row = start_row;
+
+    } else {
+
+      /* use long arithmetic here to avoid overflow & unsigned problems */
+
+      long ltemp;
+
+
+
+      ltemp = (long) end_row - (long) ptr->rows_in_mem;
+
+      if (ltemp < 0)
+
+       ltemp = 0;              /* don't fall off front end of file */
+
+      ptr->cur_start_row = (JDIMENSION) ltemp;
+
+    }
+
+    /* Read in the selected part of the array.
+
+     * During the initial write pass, we will do no actual read
+
+     * because the selected part is all undefined.
+
+     */
+
+    do_sarray_io(cinfo, ptr, FALSE);
+
+  }
+
+  /* Ensure the accessed part of the array is defined; prezero if needed.
+
+   * To improve locality of access, we only prezero the part of the array
+
+   * that the caller is about to access, not the entire in-memory array.
+
+   */
+
+  if (ptr->first_undef_row < end_row) {
+
+    if (ptr->first_undef_row < start_row) {
+
+      if (writable)            /* writer skipped over a section of array */
+
+       ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+
+      undef_row = start_row;   /* but reader is allowed to read ahead */
+
+    } else {
+
+      undef_row = ptr->first_undef_row;
+
+    }
+
+    if (writable)
+
+      ptr->first_undef_row = end_row;
+
+    if (ptr->pre_zero) {
+
+      size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE);
+
+      undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
+
+      end_row -= ptr->cur_start_row;
+
+      while (undef_row < end_row) {
+
+       jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
+
+       undef_row++;
+
+      }
+
+    } else {
+
+      if (! writable)          /* reader looking at undefined data */
+
+       ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+
+    }
+
+  }
+
+  /* Flag the buffer dirty if caller will write in it */
+
+  if (writable)
+
+    ptr->dirty = TRUE;
+
+  /* Return address of proper part of the buffer */
+
+  return ptr->mem_buffer + (start_row - ptr->cur_start_row);
+
+}
+
+
+
+
+
+METHODDEF JBLOCKARRAY
+
+access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
+
+                   JDIMENSION start_row, JDIMENSION num_rows,
+
+                   boolean writable)
+
+/* Access the part of a virtual block array starting at start_row */
+
+/* and extending for num_rows rows.  writable is true if  */
+
+/* caller intends to modify the accessed area. */
+
+{
+
+  JDIMENSION end_row = start_row + num_rows;
+
+  JDIMENSION undef_row;
+
+
+
+  /* debugging check */
+
+  if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
+
+      ptr->mem_buffer == NULL)
+
+    ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+
+
+
+  /* Make the desired part of the virtual array accessible */
+
+  if (start_row < ptr->cur_start_row ||
+
+      end_row > ptr->cur_start_row+ptr->rows_in_mem) {
+
+    if (! ptr->b_s_open)
+
+      ERREXIT(cinfo, JERR_VIRTUAL_BUG);
+
+    /* Flush old buffer contents if necessary */
+
+    if (ptr->dirty) {
+
+      do_barray_io(cinfo, ptr, TRUE);
+
+      ptr->dirty = FALSE;
+
+    }
+
+    /* Decide what part of virtual array to access.
+
+     * Algorithm: if target address > current window, assume forward scan,
+
+     * load starting at target address.  If target address < current window,
+
+     * assume backward scan, load so that target area is top of window.
+
+     * Note that when switching from forward write to forward read, will have
+
+     * start_row = 0, so the limiting case applies and we load from 0 anyway.
+
+     */
+
+    if (start_row > ptr->cur_start_row) {
+
+      ptr->cur_start_row = start_row;
+
+    } else {
+
+      /* use long arithmetic here to avoid overflow & unsigned problems */
+
+      long ltemp;
+
+
+
+      ltemp = (long) end_row - (long) ptr->rows_in_mem;
+
+      if (ltemp < 0)
+
+       ltemp = 0;              /* don't fall off front end of file */
+
+      ptr->cur_start_row = (JDIMENSION) ltemp;
+
+    }
+
+    /* Read in the selected part of the array.
+
+     * During the initial write pass, we will do no actual read
+
+     * because the selected part is all undefined.
+
+     */
+
+    do_barray_io(cinfo, ptr, FALSE);
+
+  }
+
+  /* Ensure the accessed part of the array is defined; prezero if needed.
+
+   * To improve locality of access, we only prezero the part of the array
+
+   * that the caller is about to access, not the entire in-memory array.
+
+   */
+
+  if (ptr->first_undef_row < end_row) {
+
+    if (ptr->first_undef_row < start_row) {
+
+      if (writable)            /* writer skipped over a section of array */
+
+       ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+
+      undef_row = start_row;   /* but reader is allowed to read ahead */
+
+    } else {
+
+      undef_row = ptr->first_undef_row;
+
+    }
+
+    if (writable)
+
+      ptr->first_undef_row = end_row;
+
+    if (ptr->pre_zero) {
+
+      size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK);
+
+      undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
+
+      end_row -= ptr->cur_start_row;
+
+      while (undef_row < end_row) {
+
+       jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
+
+       undef_row++;
+
+      }
+
+    } else {
+
+      if (! writable)          /* reader looking at undefined data */
+
+       ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
+
+    }
+
+  }
+
+  /* Flag the buffer dirty if caller will write in it */
+
+  if (writable)
+
+    ptr->dirty = TRUE;
+
+  /* Return address of proper part of the buffer */
+
+  return ptr->mem_buffer + (start_row - ptr->cur_start_row);
+
+}
+
+
+
+
+
+/*
+
+ * Release all objects belonging to a specified pool.
+
+ */
+
+
+
+METHODDEF void
+
+free_pool (j_common_ptr cinfo, int pool_id)
+
+{
+
+  my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
+
+  small_pool_ptr shdr_ptr;
+
+  large_pool_ptr lhdr_ptr;
+
+  size_t space_freed;
+
+
+
+  if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
+
+    ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);        /* safety check */
+
+
+
+#ifdef MEM_STATS
+
+  if (cinfo->err->trace_level > 1)
+
+    print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
+
+#endif
+
+
+
+  /* If freeing IMAGE pool, close any virtual arrays first */
+
+  if (pool_id == JPOOL_IMAGE) {
+
+    jvirt_sarray_ptr sptr;
+
+    jvirt_barray_ptr bptr;
+
+
+
+    for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
+
+      if (sptr->b_s_open) {    /* there may be no backing store */
+
+       sptr->b_s_open = FALSE; /* prevent recursive close if error */
+
+       (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
+
+      }
+
+    }
+
+    mem->virt_sarray_list = NULL;
+
+    for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
+
+      if (bptr->b_s_open) {    /* there may be no backing store */
+
+       bptr->b_s_open = FALSE; /* prevent recursive close if error */
+
+       (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
+
+      }
+
+    }
+
+    mem->virt_barray_list = NULL;
+
+  }
+
+
+
+  /* Release large objects */
+
+  lhdr_ptr = mem->large_list[pool_id];
+
+  mem->large_list[pool_id] = NULL;
+
+
+
+  while (lhdr_ptr != NULL) {
+
+    large_pool_ptr next_lhdr_ptr = lhdr_ptr->hdr.next;
+
+    space_freed = lhdr_ptr->hdr.bytes_used +
+
+                 lhdr_ptr->hdr.bytes_left +
+
+                 SIZEOF(large_pool_hdr);
+
+    jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed);
+
+    mem->total_space_allocated -= space_freed;
+
+    lhdr_ptr = next_lhdr_ptr;
+
+  }
+
+
+
+  /* Release small objects */
+
+  shdr_ptr = mem->small_list[pool_id];
+
+  mem->small_list[pool_id] = NULL;
+
+
+
+  while (shdr_ptr != NULL) {
+
+    small_pool_ptr next_shdr_ptr = shdr_ptr->hdr.next;
+
+    space_freed = shdr_ptr->hdr.bytes_used +
+
+                 shdr_ptr->hdr.bytes_left +
+
+                 SIZEOF(small_pool_hdr);
+
+    jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
+
+    mem->total_space_allocated -= space_freed;
+
+    shdr_ptr = next_shdr_ptr;
+
+  }
+
+}
+
+
+
+
+
+/*
+
+ * Close up shop entirely.
+
+ * Note that this cannot be called unless cinfo->mem is non-NULL.
+
+ */
+
+
+
+METHODDEF void
+
+self_destruct (j_common_ptr cinfo)
+
+{
+
+  int pool;
+
+
+
+  /* Close all backing store, release all memory.
+
+   * Releasing pools in reverse order might help avoid fragmentation
+
+   * with some (brain-damaged) malloc libraries.
+
+   */
+
+  for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
+
+    free_pool(cinfo, pool);
+
+  }
+
+
+
+  /* Release the memory manager control block too. */
+
+  jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr));
+
+  cinfo->mem = NULL;           /* ensures I will be called only once */
+
+
+
+  jpeg_mem_term(cinfo);                /* system-dependent cleanup */
+
+}
+
+
+
+
+
+/*
+
+ * Memory manager initialization.
+
+ * When this is called, only the error manager pointer is valid in cinfo!
+
+ */
+
+
+
+GLOBAL void
+
+jinit_memory_mgr (j_common_ptr cinfo)
+
+{
+
+  my_mem_ptr mem;
+
+  long max_to_use;
+
+  int pool;
+
+  size_t test_mac;
+
+
+
+  cinfo->mem = NULL;           /* for safety if init fails */
+
+
+
+  /* Check for configuration errors.
+
+   * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably
+
+   * doesn't reflect any real hardware alignment requirement.
+
+   * The test is a little tricky: for X>0, X and X-1 have no one-bits
+
+   * in common if and only if X is a power of 2, ie has only one one-bit.
+
+   * Some compilers may give an "unreachable code" warning here; ignore it.
+
+   */
+
+  if ((SIZEOF(ALIGN_TYPE) & (SIZEOF(ALIGN_TYPE)-1)) != 0)
+
+    ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
+
+  /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
+
+   * a multiple of SIZEOF(ALIGN_TYPE).
+
+   * Again, an "unreachable code" warning may be ignored here.
+
+   * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
+
+   */
+
+  test_mac = (size_t) MAX_ALLOC_CHUNK;
+
+  if ((long) test_mac != MAX_ALLOC_CHUNK ||
+
+      (MAX_ALLOC_CHUNK % SIZEOF(ALIGN_TYPE)) != 0)
+
+    ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
+
+
+
+  max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */
+
+
+
+  /* Attempt to allocate memory manager's control block */
+
+  mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr));
+
+
+
+  if (mem == NULL) {
+
+    jpeg_mem_term(cinfo);      /* system-dependent cleanup */
+
+    ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
+
+  }
+
+
+
+  /* OK, fill in the method pointers */
+
+  mem->pub.alloc_small = alloc_small;
+
+  mem->pub.alloc_large = alloc_large;
+
+  mem->pub.alloc_sarray = alloc_sarray;
+
+  mem->pub.alloc_barray = alloc_barray;
+
+  mem->pub.request_virt_sarray = request_virt_sarray;
+
+  mem->pub.request_virt_barray = request_virt_barray;
+
+  mem->pub.realize_virt_arrays = realize_virt_arrays;
+
+  mem->pub.access_virt_sarray = access_virt_sarray;
+
+  mem->pub.access_virt_barray = access_virt_barray;
+
+  mem->pub.free_pool = free_pool;
+
+  mem->pub.self_destruct = self_destruct;
+
+
+
+  /* Initialize working state */
+
+  mem->pub.max_memory_to_use = max_to_use;
+
+
+
+  for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
+
+    mem->small_list[pool] = NULL;
+
+    mem->large_list[pool] = NULL;
+
+  }
+
+  mem->virt_sarray_list = NULL;
+
+  mem->virt_barray_list = NULL;
+
+
+
+  mem->total_space_allocated = SIZEOF(my_memory_mgr);
+
+
+
+  /* Declare ourselves open for business */
+
+  cinfo->mem = & mem->pub;
+
+
+
+  /* Check for an environment variable JPEGMEM; if found, override the
+
+   * default max_memory setting from jpeg_mem_init.  Note that the
+
+   * surrounding application may again override this value.
+
+   * If your system doesn't support getenv(), define NO_GETENV to disable
+
+   * this feature.
+
+   */
+
+#ifndef NO_GETENV
+
+  { char * memenv;
+
+
+
+    if ((memenv = getenv("JPEGMEM")) != NULL) {
+
+      char ch = 'x';
+
+
+
+      if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
+
+       if (ch == 'm' || ch == 'M')
+
+         max_to_use *= 1000L;
+
+       mem->pub.max_memory_to_use = max_to_use * 1000L;
+
+      }
+
+    }
+
+  }
+
+#endif
+
+
+
+}
+