SAMV71 Xplained Ultra Software Package 1.5

jmemmgr.c

00001 /*
00002  * jmemmgr.c
00003  *
00004  * Copyright (C) 1991-1997, Thomas G. Lane.
00005  * This file is part of the Independent JPEG Group's software.
00006  * For conditions of distribution and use, see the accompanying README file.
00007  *
00008  * This file contains the JPEG system-independent memory management
00009  * routines.  This code is usable across a wide variety of machines; most
00010  * of the system dependencies have been isolated in a separate file.
00011  * The major functions provided here are:
00012  *   * pool-based allocation and freeing of memory;
00013  *   * policy decisions about how to divide available memory among the
00014  *     virtual arrays;
00015  *   * control logic for swapping virtual arrays between main memory and
00016  *     backing storage.
00017  * The separate system-dependent file provides the actual backing-storage
00018  * access code, and it contains the policy decision about how much total
00019  * main memory to use.
00020  * This file is system-dependent in the sense that some of its functions
00021  * are unnecessary in some systems.  For example, if there is enough virtual
00022  * memory so that backing storage will never be used, much of the virtual
00023  * array control logic could be removed.  (Of course, if you have that much
00024  * memory then you shouldn't care about a little bit of unused code...)
00025  */
00026 
00027 #define JPEG_INTERNALS
00028 #define AM_MEMORY_MANAGER   /* we define jvirt_Xarray_control structs */
00029 #include "jinclude.h"
00030 #include "jpeglib.h"
00031 #include "jmemsys.h"        /* import the system-dependent declarations */
00032 
00033 #ifndef NO_GETENV
00034 #error "tgghh"
00035 #ifndef HAVE_STDLIB_H       /* <stdlib.h> should declare getenv() */
00036 extern char * getenv JPP((const char * name));
00037 #endif
00038 #endif
00039 
00040 
00041 /*
00042  * Some important notes:
00043  *   The allocation routines provided here must never return NULL.
00044  *   They should exit to error_exit if unsuccessful.
00045  *
00046  *   It's not a good idea to try to merge the sarray and barray routines,
00047  *   even though they are textually almost the same, because samples are
00048  *   usually stored as bytes while coefficients are shorts or ints.  Thus,
00049  *   in machines where byte pointers have a different representation from
00050  *   word pointers, the resulting machine code could not be the same.
00051  */
00052 
00053 
00054 /*
00055  * Many machines require storage alignment: longs must start on 4-byte
00056  * boundaries, doubles on 8-byte boundaries, etc.  On such machines, malloc()
00057  * always returns pointers that are multiples of the worst-case alignment
00058  * requirement, and we had better do so too.
00059  * There isn't any really portable way to determine the worst-case alignment
00060  * requirement.  This module assumes that the alignment requirement is
00061  * multiples of sizeof(ALIGN_TYPE).
00062  * By default, we define ALIGN_TYPE as double.  This is necessary on some
00063  * workstations (where doubles really do need 8-byte alignment) and will work
00064  * fine on nearly everything.  If your machine has lesser alignment needs,
00065  * you can save a few bytes by making ALIGN_TYPE smaller.
00066  * The only place I know of where this will NOT work is certain Macintosh
00067  * 680x0 compilers that define double as a 10-byte IEEE extended float.
00068  * Doing 10-byte alignment is counterproductive because longwords won't be
00069  * aligned well.  Put "#define ALIGN_TYPE long" in jconfig.h if you have
00070  * such a compiler.
00071  */
00072 
00073 #ifndef ALIGN_TYPE      /* so can override from jconfig.h */
00074 #define ALIGN_TYPE  double
00075 #endif
00076 
00077 
00078 /*
00079  * We allocate objects from "pools", where each pool is gotten with a single
00080  * request to jpeg_get_small() or jpeg_get_large().  There is no per-object
00081  * overhead within a pool, except for alignment padding.  Each pool has a
00082  * header with a link to the next pool of the same class.
00083  * Small and large pool headers are identical except that the latter's
00084  * link pointer must be FAR on 80x86 machines.
00085  * Notice that the "real" header fields are union'ed with a dummy ALIGN_TYPE
00086  * field.  This forces the compiler to make SIZEOF(small_pool_hdr) a multiple
00087  * of the alignment requirement of ALIGN_TYPE.
00088  */
00089 
00090 typedef union small_pool_struct * small_pool_ptr;
00091 
00092 typedef union small_pool_struct {
00093   struct {
00094     small_pool_ptr next;    /* next in list of pools */
00095     size_t bytes_used;      /* how many bytes already used within pool */
00096     size_t bytes_left;      /* bytes still available in this pool */
00097   } hdr;
00098   ALIGN_TYPE dummy;     /* included in union to ensure alignment */
00099 } small_pool_hdr;
00100 
00101 typedef union large_pool_struct FAR * large_pool_ptr;
00102 
00103 typedef union large_pool_struct {
00104   struct {
00105     large_pool_ptr next;    /* next in list of pools */
00106     size_t bytes_used;      /* how many bytes already used within pool */
00107     size_t bytes_left;      /* bytes still available in this pool */
00108   } hdr;
00109   ALIGN_TYPE dummy;     /* included in union to ensure alignment */
00110 } large_pool_hdr;
00111 
00112 
00113 /*
00114  * Here is the full definition of a memory manager object.
00115  */
00116 
00117 typedef struct {
00118   struct jpeg_memory_mgr pub;   /* public fields */
00119 
00120   /* Each pool identifier (lifetime class) names a linked list of pools. */
00121   small_pool_ptr small_list[JPOOL_NUMPOOLS];
00122   large_pool_ptr large_list[JPOOL_NUMPOOLS];
00123 
00124   /* Since we only have one lifetime class of virtual arrays, only one
00125    * linked list is necessary (for each datatype).  Note that the virtual
00126    * array control blocks being linked together are actually stored somewhere
00127    * in the small-pool list.
00128    */
00129   jvirt_sarray_ptr virt_sarray_list;
00130   jvirt_barray_ptr virt_barray_list;
00131 
00132   /* This counts total space obtained from jpeg_get_small/large */
00133   long total_space_allocated;
00134 
00135   /* alloc_sarray and alloc_barray set this value for use by virtual
00136    * array routines.
00137    */
00138   JDIMENSION last_rowsperchunk; /* from most recent alloc_sarray/barray */
00139 } my_memory_mgr;
00140 
00141 typedef my_memory_mgr * my_mem_ptr;
00142 
00143 
00144 /*
00145  * The control blocks for virtual arrays.
00146  * Note that these blocks are allocated in the "small" pool area.
00147  * System-dependent info for the associated backing store (if any) is hidden
00148  * inside the backing_store_info struct.
00149  */
00150 
00151 struct jvirt_sarray_control {
00152   JSAMPARRAY mem_buffer;    /* => the in-memory buffer */
00153   JDIMENSION rows_in_array; /* total virtual array height */
00154   JDIMENSION samplesperrow; /* width of array (and of memory buffer) */
00155   JDIMENSION maxaccess;     /* max rows accessed by access_virt_sarray */
00156   JDIMENSION rows_in_mem;   /* height of memory buffer */
00157   JDIMENSION rowsperchunk;  /* allocation chunk size in mem_buffer */
00158   JDIMENSION cur_start_row; /* first logical row # in the buffer */
00159   JDIMENSION first_undef_row;   /* row # of first uninitialized row */
00160   boolean pre_zero;     /* pre-zero mode requested? */
00161   boolean dirty;        /* do current buffer contents need written? */
00162   boolean b_s_open;     /* is backing-store data valid? */
00163   jvirt_sarray_ptr next;    /* link to next virtual sarray control block */
00164   backing_store_info b_s_info;  /* System-dependent control info */
00165 };
00166 
00167 struct jvirt_barray_control {
00168   JBLOCKARRAY mem_buffer;   /* => the in-memory buffer */
00169   JDIMENSION rows_in_array; /* total virtual array height */
00170   JDIMENSION blocksperrow;  /* width of array (and of memory buffer) */
00171   JDIMENSION maxaccess;     /* max rows accessed by access_virt_barray */
00172   JDIMENSION rows_in_mem;   /* height of memory buffer */
00173   JDIMENSION rowsperchunk;  /* allocation chunk size in mem_buffer */
00174   JDIMENSION cur_start_row; /* first logical row # in the buffer */
00175   JDIMENSION first_undef_row;   /* row # of first uninitialized row */
00176   boolean pre_zero;     /* pre-zero mode requested? */
00177   boolean dirty;        /* do current buffer contents need written? */
00178   boolean b_s_open;     /* is backing-store data valid? */
00179   jvirt_barray_ptr next;    /* link to next virtual barray control block */
00180   backing_store_info b_s_info;  /* System-dependent control info */
00181 };
00182 
00183 
00184 #ifdef MEM_STATS        /* optional extra stuff for statistics */
00185 
00186 LOCAL(void)
00187 print_mem_stats (j_common_ptr cinfo, int pool_id)
00188 {
00189   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00190   small_pool_ptr shdr_ptr;
00191   large_pool_ptr lhdr_ptr;
00192 
00193   /* Since this is only a debugging stub, we can cheat a little by using
00194    * fprintf directly rather than going through the trace message code.
00195    * This is helpful because message parm array can't handle longs.
00196    */
00197   fprintf(stderr, "Freeing pool %d, total space = %ld\n",
00198       pool_id, mem->total_space_allocated);
00199 
00200   for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
00201        lhdr_ptr = lhdr_ptr->hdr.next) {
00202     fprintf(stderr, "  Large chunk used %ld\n",
00203         (long) lhdr_ptr->hdr.bytes_used);
00204   }
00205 
00206   for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;
00207        shdr_ptr = shdr_ptr->hdr.next) {
00208     fprintf(stderr, "  Small chunk used %ld free %ld\n",
00209         (long) shdr_ptr->hdr.bytes_used,
00210         (long) shdr_ptr->hdr.bytes_left);
00211   }
00212 }
00213 
00214 #endif /* MEM_STATS */
00215 
00216 
00217 LOCAL(void)
00218 out_of_memory (j_common_ptr cinfo, int which)
00219 /* Report an out-of-memory error and stop execution */
00220 /* If we compiled MEM_STATS support, report alloc requests before dying */
00221 {
00222 #ifdef MEM_STATS
00223   cinfo->err->trace_level = 2;  /* force self_destruct to report stats */
00224 #endif
00225   ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
00226 }
00227 
00228 
00229 /*
00230  * Allocation of "small" objects.
00231  *
00232  * For these, we use pooled storage.  When a new pool must be created,
00233  * we try to get enough space for the current request plus a "slop" factor,
00234  * where the slop will be the amount of leftover space in the new pool.
00235  * The speed vs. space tradeoff is largely determined by the slop values.
00236  * A different slop value is provided for each pool class (lifetime),
00237  * and we also distinguish the first pool of a class from later ones.
00238  * NOTE: the values given work fairly well on both 16- and 32-bit-int
00239  * machines, but may be too small if longs are 64 bits or more.
00240  */
00241 
00242 static const size_t first_pool_slop[JPOOL_NUMPOOLS] =
00243 {
00244     1600,           /* first PERMANENT pool */
00245     16000           /* first IMAGE pool */
00246 };
00247 
00248 static const size_t extra_pool_slop[JPOOL_NUMPOOLS] =
00249 {
00250     0,          /* additional PERMANENT pools */
00251     5000            /* additional IMAGE pools */
00252 };
00253 
00254 #define MIN_SLOP  50        /* greater than 0 to avoid futile looping */
00255 
00256 
00257 METHODDEF(void *)
00258 alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
00259 /* Allocate a "small" object */
00260 {
00261   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00262   small_pool_ptr hdr_ptr, prev_hdr_ptr;
00263   char * data_ptr;
00264   size_t odd_bytes, min_request, slop;
00265 
00266   /* Check for unsatisfiable request (do now to ensure no overflow below) */
00267   if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr)))
00268     out_of_memory(cinfo, 1);    /* request exceeds malloc's ability */
00269 
00270   /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
00271   odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
00272   if (odd_bytes > 0)
00273     sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
00274 
00275   /* See if space is available in any existing pool */
00276   if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
00277     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
00278   prev_hdr_ptr = NULL;
00279   hdr_ptr = mem->small_list[pool_id];
00280   while (hdr_ptr != NULL) {
00281     if (hdr_ptr->hdr.bytes_left >= sizeofobject)
00282       break;            /* found pool with enough space */
00283     prev_hdr_ptr = hdr_ptr;
00284     hdr_ptr = hdr_ptr->hdr.next;
00285   }
00286 
00287   /* Time to make a new pool? */
00288   if (hdr_ptr == NULL) {
00289     /* min_request is what we need now, slop is what will be leftover */
00290     min_request = sizeofobject + SIZEOF(small_pool_hdr);
00291     if (prev_hdr_ptr == NULL)   /* first pool in class? */
00292       slop = first_pool_slop[pool_id];
00293     else
00294       slop = extra_pool_slop[pool_id];
00295     /* Don't ask for more than MAX_ALLOC_CHUNK */
00296     if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
00297       slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
00298     /* Try to get space, if fail reduce slop and try again */
00299     for (;;) {
00300       hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
00301       if (hdr_ptr != NULL)
00302     break;
00303       slop /= 2;
00304       if (slop < MIN_SLOP)  /* give up when it gets real small */
00305     out_of_memory(cinfo, 2); /* jpeg_get_small failed */
00306     }
00307     mem->total_space_allocated += min_request + slop;
00308     /* Success, initialize the new pool header and add to end of list */
00309     hdr_ptr->hdr.next = NULL;
00310     hdr_ptr->hdr.bytes_used = 0;
00311     hdr_ptr->hdr.bytes_left = sizeofobject + slop;
00312     if (prev_hdr_ptr == NULL)   /* first pool in class? */
00313       mem->small_list[pool_id] = hdr_ptr;
00314     else
00315       prev_hdr_ptr->hdr.next = hdr_ptr;
00316   }
00317 
00318   /* OK, allocate the object from the current pool */
00319   data_ptr = (char *) (hdr_ptr + 1); /* point to first data byte in pool */
00320   data_ptr += hdr_ptr->hdr.bytes_used; /* point to place for object */
00321   hdr_ptr->hdr.bytes_used += sizeofobject;
00322   hdr_ptr->hdr.bytes_left -= sizeofobject;
00323 
00324   return (void *) data_ptr;
00325 }
00326 
00327 
00328 /*
00329  * Allocation of "large" objects.
00330  *
00331  * The external semantics of these are the same as "small" objects,
00332  * except that FAR pointers are used on 80x86.  However the pool
00333  * management heuristics are quite different.  We assume that each
00334  * request is large enough that it may as well be passed directly to
00335  * jpeg_get_large; the pool management just links everything together
00336  * so that we can free it all on demand.
00337  * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY
00338  * structures.  The routines that create these structures (see below)
00339  * deliberately bunch rows together to ensure a large request size.
00340  */
00341 
00342 METHODDEF(void FAR *)
00343 alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
00344 /* Allocate a "large" object */
00345 {
00346   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00347   large_pool_ptr hdr_ptr;
00348   size_t odd_bytes;
00349 
00350   /* Check for unsatisfiable request (do now to ensure no overflow below) */
00351   if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)))
00352     out_of_memory(cinfo, 3);    /* request exceeds malloc's ability */
00353 
00354   /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
00355   odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
00356   if (odd_bytes > 0)
00357     sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
00358 
00359   /* Always make a new pool */
00360   if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
00361     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
00362 
00363   hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
00364                         SIZEOF(large_pool_hdr));
00365   if (hdr_ptr == NULL)
00366     out_of_memory(cinfo, 4);    /* jpeg_get_large failed */
00367   mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr);
00368 
00369   /* Success, initialize the new pool header and add to list */
00370   hdr_ptr->hdr.next = mem->large_list[pool_id];
00371   /* We maintain space counts in each pool header for statistical purposes,
00372    * even though they are not needed for allocation.
00373    */
00374   hdr_ptr->hdr.bytes_used = sizeofobject;
00375   hdr_ptr->hdr.bytes_left = 0;
00376   mem->large_list[pool_id] = hdr_ptr;
00377 
00378   return (void FAR *) (hdr_ptr + 1); /* point to first data byte in pool */
00379 }
00380 
00381 
00382 /*
00383  * Creation of 2-D sample arrays.
00384  * The pointers are in near heap, the samples themselves in FAR heap.
00385  *
00386  * To minimize allocation overhead and to allow I/O of large contiguous
00387  * blocks, we allocate the sample rows in groups of as many rows as possible
00388  * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.
00389  * NB: the virtual array control routines, later in this file, know about
00390  * this chunking of rows.  The rowsperchunk value is left in the mem manager
00391  * object so that it can be saved away if this sarray is the workspace for
00392  * a virtual array.
00393  */
00394 
00395 METHODDEF(JSAMPARRAY)
00396 alloc_sarray (j_common_ptr cinfo, int pool_id,
00397           JDIMENSION samplesperrow, JDIMENSION numrows)
00398 /* Allocate a 2-D sample array */
00399 {
00400   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00401   JSAMPARRAY result;
00402   JSAMPROW workspace;
00403   JDIMENSION rowsperchunk, currow, i;
00404   long ltemp;
00405 
00406   /* Calculate max # of rows allowed in one allocation chunk */
00407   ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
00408       ((long) samplesperrow * SIZEOF(JSAMPLE));
00409   if (ltemp <= 0)
00410     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
00411   if (ltemp < (long) numrows)
00412     rowsperchunk = (JDIMENSION) ltemp;
00413   else
00414     rowsperchunk = numrows;
00415   mem->last_rowsperchunk = rowsperchunk;
00416 
00417   /* Get space for row pointers (small object) */
00418   result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
00419                     (size_t) (numrows * SIZEOF(JSAMPROW)));
00420 
00421   /* Get the rows themselves (large objects) */
00422   currow = 0;
00423   while (currow < numrows) {
00424     rowsperchunk = MIN(rowsperchunk, numrows - currow);
00425     workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
00426     (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
00427           * SIZEOF(JSAMPLE)));
00428     for (i = rowsperchunk; i > 0; i--) {
00429       result[currow++] = workspace;
00430       workspace += samplesperrow;
00431     }
00432   }
00433 
00434   return result;
00435 }
00436 
00437 
00438 /*
00439  * Creation of 2-D coefficient-block arrays.
00440  * This is essentially the same as the code for sample arrays, above.
00441  */
00442 
00443 METHODDEF(JBLOCKARRAY)
00444 alloc_barray (j_common_ptr cinfo, int pool_id,
00445           JDIMENSION blocksperrow, JDIMENSION numrows)
00446 /* Allocate a 2-D coefficient-block array */
00447 {
00448   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00449   JBLOCKARRAY result;
00450   JBLOCKROW workspace;
00451   JDIMENSION rowsperchunk, currow, i;
00452   long ltemp;
00453 
00454   /* Calculate max # of rows allowed in one allocation chunk */
00455   ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
00456       ((long) blocksperrow * SIZEOF(JBLOCK));
00457   if (ltemp <= 0)
00458     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
00459   if (ltemp < (long) numrows)
00460     rowsperchunk = (JDIMENSION) ltemp;
00461   else
00462     rowsperchunk = numrows;
00463   mem->last_rowsperchunk = rowsperchunk;
00464 
00465   /* Get space for row pointers (small object) */
00466   result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
00467                      (size_t) (numrows * SIZEOF(JBLOCKROW)));
00468 
00469   /* Get the rows themselves (large objects) */
00470   currow = 0;
00471   while (currow < numrows) {
00472     rowsperchunk = MIN(rowsperchunk, numrows - currow);
00473     workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
00474     (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
00475           * SIZEOF(JBLOCK)));
00476     for (i = rowsperchunk; i > 0; i--) {
00477       result[currow++] = workspace;
00478       workspace += blocksperrow;
00479     }
00480   }
00481 
00482   return result;
00483 }
00484 
00485 
00486 /*
00487  * About virtual array management:
00488  *
00489  * The above "normal" array routines are only used to allocate strip buffers
00490  * (as wide as the image, but just a few rows high).  Full-image-sized buffers
00491  * are handled as "virtual" arrays.  The array is still accessed a strip at a
00492  * time, but the memory manager must save the whole array for repeated
00493  * accesses.  The intended implementation is that there is a strip buffer in
00494  * memory (as high as is possible given the desired memory limit), plus a
00495  * backing file that holds the rest of the array.
00496  *
00497  * The request_virt_array routines are told the total size of the image and
00498  * the maximum number of rows that will be accessed at once.  The in-memory
00499  * buffer must be at least as large as the maxaccess value.
00500  *
00501  * The request routines create control blocks but not the in-memory buffers.
00502  * That is postponed until realize_virt_arrays is called.  At that time the
00503  * total amount of space needed is known (approximately, anyway), so free
00504  * memory can be divided up fairly.
00505  *
00506  * The access_virt_array routines are responsible for making a specific strip
00507  * area accessible (after reading or writing the backing file, if necessary).
00508  * Note that the access routines are told whether the caller intends to modify
00509  * the accessed strip; during a read-only pass this saves having to rewrite
00510  * data to disk.  The access routines are also responsible for pre-zeroing
00511  * any newly accessed rows, if pre-zeroing was requested.
00512  *
00513  * In current usage, the access requests are usually for nonoverlapping
00514  * strips; that is, successive access start_row numbers differ by exactly
00515  * num_rows = maxaccess.  This means we can get good performance with simple
00516  * buffer dump/reload logic, by making the in-memory buffer be a multiple
00517  * of the access height; then there will never be accesses across bufferload
00518  * boundaries.  The code will still work with overlapping access requests,
00519  * but it doesn't handle bufferload overlaps very efficiently.
00520  */
00521 
00522 
00523 METHODDEF(jvirt_sarray_ptr)
00524 request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
00525              JDIMENSION samplesperrow, JDIMENSION numrows,
00526              JDIMENSION maxaccess)
00527 /* Request a virtual 2-D sample array */
00528 {
00529   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00530   jvirt_sarray_ptr result;
00531 
00532   /* Only IMAGE-lifetime virtual arrays are currently supported */
00533   if (pool_id != JPOOL_IMAGE)
00534     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
00535 
00536   /* get control block */
00537   result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
00538                       SIZEOF(struct jvirt_sarray_control));
00539 
00540   result->mem_buffer = NULL;    /* marks array not yet realized */
00541   result->rows_in_array = numrows;
00542   result->samplesperrow = samplesperrow;
00543   result->maxaccess = maxaccess;
00544   result->pre_zero = pre_zero;
00545   result->b_s_open = FALSE; /* no associated backing-store object */
00546   result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
00547   mem->virt_sarray_list = result;
00548 
00549   return result;
00550 }
00551 
00552 
00553 METHODDEF(jvirt_barray_ptr)
00554 request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
00555              JDIMENSION blocksperrow, JDIMENSION numrows,
00556              JDIMENSION maxaccess)
00557 /* Request a virtual 2-D coefficient-block array */
00558 {
00559   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00560   jvirt_barray_ptr result;
00561 
00562   /* Only IMAGE-lifetime virtual arrays are currently supported */
00563   if (pool_id != JPOOL_IMAGE)
00564     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
00565 
00566   /* get control block */
00567   result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
00568                       SIZEOF(struct jvirt_barray_control));
00569 
00570   result->mem_buffer = NULL;    /* marks array not yet realized */
00571   result->rows_in_array = numrows;
00572   result->blocksperrow = blocksperrow;
00573   result->maxaccess = maxaccess;
00574   result->pre_zero = pre_zero;
00575   result->b_s_open = FALSE; /* no associated backing-store object */
00576   result->next = mem->virt_barray_list; /* add to list of virtual arrays */
00577   mem->virt_barray_list = result;
00578 
00579   return result;
00580 }
00581 
00582 
00583 METHODDEF(void)
00584 realize_virt_arrays (j_common_ptr cinfo)
00585 /* Allocate the in-memory buffers for any unrealized virtual arrays */
00586 {
00587   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00588   long space_per_minheight, maximum_space, avail_mem;
00589   long minheights, max_minheights;
00590   jvirt_sarray_ptr sptr;
00591   jvirt_barray_ptr bptr;
00592 
00593   /* Compute the minimum space needed (maxaccess rows in each buffer)
00594    * and the maximum space needed (full image height in each buffer).
00595    * These may be of use to the system-dependent jpeg_mem_available routine.
00596    */
00597   space_per_minheight = 0;
00598   maximum_space = 0;
00599   for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
00600     if (sptr->mem_buffer == NULL) { /* if not realized yet */
00601       space_per_minheight += (long) sptr->maxaccess *
00602                  (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
00603       maximum_space += (long) sptr->rows_in_array *
00604                (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
00605     }
00606   }
00607   for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
00608     if (bptr->mem_buffer == NULL) { /* if not realized yet */
00609       space_per_minheight += (long) bptr->maxaccess *
00610                  (long) bptr->blocksperrow * SIZEOF(JBLOCK);
00611       maximum_space += (long) bptr->rows_in_array *
00612                (long) bptr->blocksperrow * SIZEOF(JBLOCK);
00613     }
00614   }
00615 
00616   if (space_per_minheight <= 0)
00617     return;         /* no unrealized arrays, no work */
00618 
00619   /* Determine amount of memory to actually use; this is system-dependent. */
00620   avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
00621                  mem->total_space_allocated);
00622 
00623   /* If the maximum space needed is available, make all the buffers full
00624    * height; otherwise parcel it out with the same number of minheights
00625    * in each buffer.
00626    */
00627   if (avail_mem >= maximum_space)
00628     max_minheights = 1000000000L;
00629   else {
00630     max_minheights = avail_mem / space_per_minheight;
00631     /* If there doesn't seem to be enough space, try to get the minimum
00632      * anyway.  This allows a "stub" implementation of jpeg_mem_available().
00633      */
00634     if (max_minheights <= 0)
00635       max_minheights = 1;
00636   }
00637 
00638   /* Allocate the in-memory buffers and initialize backing store as needed. */
00639 
00640   for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
00641     if (sptr->mem_buffer == NULL) { /* if not realized yet */
00642       minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
00643       if (minheights <= max_minheights) {
00644     /* This buffer fits in memory */
00645     sptr->rows_in_mem = sptr->rows_in_array;
00646       } else {
00647     /* It doesn't fit in memory, create backing store. */
00648     sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
00649     jpeg_open_backing_store(cinfo, & sptr->b_s_info,
00650                 (long) sptr->rows_in_array *
00651                 (long) sptr->samplesperrow *
00652                 (long) SIZEOF(JSAMPLE));
00653     sptr->b_s_open = TRUE;
00654       }
00655       sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
00656                       sptr->samplesperrow, sptr->rows_in_mem);
00657       sptr->rowsperchunk = mem->last_rowsperchunk;
00658       sptr->cur_start_row = 0;
00659       sptr->first_undef_row = 0;
00660       sptr->dirty = FALSE;
00661     }
00662   }
00663 
00664   for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
00665     if (bptr->mem_buffer == NULL) { /* if not realized yet */
00666       minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
00667       if (minheights <= max_minheights) {
00668     /* This buffer fits in memory */
00669     bptr->rows_in_mem = bptr->rows_in_array;
00670       } else {
00671     /* It doesn't fit in memory, create backing store. */
00672     bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
00673     jpeg_open_backing_store(cinfo, & bptr->b_s_info,
00674                 (long) bptr->rows_in_array *
00675                 (long) bptr->blocksperrow *
00676                 (long) SIZEOF(JBLOCK));
00677     bptr->b_s_open = TRUE;
00678       }
00679       bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
00680                       bptr->blocksperrow, bptr->rows_in_mem);
00681       bptr->rowsperchunk = mem->last_rowsperchunk;
00682       bptr->cur_start_row = 0;
00683       bptr->first_undef_row = 0;
00684       bptr->dirty = FALSE;
00685     }
00686   }
00687 }
00688 
00689 
00690 LOCAL(void)
00691 do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
00692 /* Do backing store read or write of a virtual sample array */
00693 {
00694   long bytesperrow, file_offset, byte_count, rows, thisrow, i;
00695 
00696   bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE);
00697   file_offset = ptr->cur_start_row * bytesperrow;
00698   /* Loop to read or write each allocation chunk in mem_buffer */
00699   for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
00700     /* One chunk, but check for short chunk at end of buffer */
00701     rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
00702     /* Transfer no more than is currently defined */
00703     thisrow = (long) ptr->cur_start_row + i;
00704     rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
00705     /* Transfer no more than fits in file */
00706     rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
00707     if (rows <= 0)      /* this chunk might be past end of file! */
00708       break;
00709     byte_count = rows * bytesperrow;
00710     if (writing)
00711       (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
00712                         (void FAR *) ptr->mem_buffer[i],
00713                         file_offset, byte_count);
00714     else
00715       (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
00716                        (void FAR *) ptr->mem_buffer[i],
00717                        file_offset, byte_count);
00718     file_offset += byte_count;
00719   }
00720 }
00721 
00722 
00723 LOCAL(void)
00724 do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
00725 /* Do backing store read or write of a virtual coefficient-block array */
00726 {
00727   long bytesperrow, file_offset, byte_count, rows, thisrow, i;
00728 
00729   bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK);
00730   file_offset = ptr->cur_start_row * bytesperrow;
00731   /* Loop to read or write each allocation chunk in mem_buffer */
00732   for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
00733     /* One chunk, but check for short chunk at end of buffer */
00734     rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
00735     /* Transfer no more than is currently defined */
00736     thisrow = (long) ptr->cur_start_row + i;
00737     rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
00738     /* Transfer no more than fits in file */
00739     rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
00740     if (rows <= 0)      /* this chunk might be past end of file! */
00741       break;
00742     byte_count = rows * bytesperrow;
00743     if (writing)
00744       (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
00745                         (void FAR *) ptr->mem_buffer[i],
00746                         file_offset, byte_count);
00747     else
00748       (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
00749                        (void FAR *) ptr->mem_buffer[i],
00750                        file_offset, byte_count);
00751     file_offset += byte_count;
00752   }
00753 }
00754 
00755 
00756 METHODDEF(JSAMPARRAY)
00757 access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
00758             JDIMENSION start_row, JDIMENSION num_rows,
00759             boolean writable)
00760 /* Access the part of a virtual sample array starting at start_row */
00761 /* and extending for num_rows rows.  writable is true if  */
00762 /* caller intends to modify the accessed area. */
00763 {
00764   JDIMENSION end_row = start_row + num_rows;
00765   JDIMENSION undef_row;
00766 
00767   /* debugging check */
00768   if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
00769       ptr->mem_buffer == NULL)
00770     ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
00771 
00772   /* Make the desired part of the virtual array accessible */
00773   if (start_row < ptr->cur_start_row ||
00774       end_row > ptr->cur_start_row+ptr->rows_in_mem) {
00775     if (! ptr->b_s_open)
00776       ERREXIT(cinfo, JERR_VIRTUAL_BUG);
00777     /* Flush old buffer contents if necessary */
00778     if (ptr->dirty) {
00779       do_sarray_io(cinfo, ptr, TRUE);
00780       ptr->dirty = FALSE;
00781     }
00782     /* Decide what part of virtual array to access.
00783      * Algorithm: if target address > current window, assume forward scan,
00784      * load starting at target address.  If target address < current window,
00785      * assume backward scan, load so that target area is top of window.
00786      * Note that when switching from forward write to forward read, will have
00787      * start_row = 0, so the limiting case applies and we load from 0 anyway.
00788      */
00789     if (start_row > ptr->cur_start_row) {
00790       ptr->cur_start_row = start_row;
00791     } else {
00792       /* use long arithmetic here to avoid overflow & unsigned problems */
00793       long ltemp;
00794 
00795       ltemp = (long) end_row - (long) ptr->rows_in_mem;
00796       if (ltemp < 0)
00797     ltemp = 0;      /* don't fall off front end of file */
00798       ptr->cur_start_row = (JDIMENSION) ltemp;
00799     }
00800     /* Read in the selected part of the array.
00801      * During the initial write pass, we will do no actual read
00802      * because the selected part is all undefined.
00803      */
00804     do_sarray_io(cinfo, ptr, FALSE);
00805   }
00806   /* Ensure the accessed part of the array is defined; prezero if needed.
00807    * To improve locality of access, we only prezero the part of the array
00808    * that the caller is about to access, not the entire in-memory array.
00809    */
00810   if (ptr->first_undef_row < end_row) {
00811     if (ptr->first_undef_row < start_row) {
00812       if (writable)     /* writer skipped over a section of array */
00813     ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
00814       undef_row = start_row;    /* but reader is allowed to read ahead */
00815     } else {
00816       undef_row = ptr->first_undef_row;
00817     }
00818     if (writable)
00819       ptr->first_undef_row = end_row;
00820     if (ptr->pre_zero) {
00821       size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE);
00822       undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
00823       end_row -= ptr->cur_start_row;
00824       while (undef_row < end_row) {
00825     jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
00826     undef_row++;
00827       }
00828     } else {
00829       if (! writable)       /* reader looking at undefined data */
00830     ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
00831     }
00832   }
00833   /* Flag the buffer dirty if caller will write in it */
00834   if (writable)
00835     ptr->dirty = TRUE;
00836   /* Return address of proper part of the buffer */
00837   return ptr->mem_buffer + (start_row - ptr->cur_start_row);
00838 }
00839 
00840 
00841 METHODDEF(JBLOCKARRAY)
00842 access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
00843             JDIMENSION start_row, JDIMENSION num_rows,
00844             boolean writable)
00845 /* Access the part of a virtual block array starting at start_row */
00846 /* and extending for num_rows rows.  writable is true if  */
00847 /* caller intends to modify the accessed area. */
00848 {
00849   JDIMENSION end_row = start_row + num_rows;
00850   JDIMENSION undef_row;
00851 
00852   /* debugging check */
00853   if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
00854       ptr->mem_buffer == NULL)
00855     ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
00856 
00857   /* Make the desired part of the virtual array accessible */
00858   if (start_row < ptr->cur_start_row ||
00859       end_row > ptr->cur_start_row+ptr->rows_in_mem) {
00860     if (! ptr->b_s_open)
00861       ERREXIT(cinfo, JERR_VIRTUAL_BUG);
00862     /* Flush old buffer contents if necessary */
00863     if (ptr->dirty) {
00864       do_barray_io(cinfo, ptr, TRUE);
00865       ptr->dirty = FALSE;
00866     }
00867     /* Decide what part of virtual array to access.
00868      * Algorithm: if target address > current window, assume forward scan,
00869      * load starting at target address.  If target address < current window,
00870      * assume backward scan, load so that target area is top of window.
00871      * Note that when switching from forward write to forward read, will have
00872      * start_row = 0, so the limiting case applies and we load from 0 anyway.
00873      */
00874     if (start_row > ptr->cur_start_row) {
00875       ptr->cur_start_row = start_row;
00876     } else {
00877       /* use long arithmetic here to avoid overflow & unsigned problems */
00878       long ltemp;
00879 
00880       ltemp = (long) end_row - (long) ptr->rows_in_mem;
00881       if (ltemp < 0)
00882     ltemp = 0;      /* don't fall off front end of file */
00883       ptr->cur_start_row = (JDIMENSION) ltemp;
00884     }
00885     /* Read in the selected part of the array.
00886      * During the initial write pass, we will do no actual read
00887      * because the selected part is all undefined.
00888      */
00889     do_barray_io(cinfo, ptr, FALSE);
00890   }
00891   /* Ensure the accessed part of the array is defined; prezero if needed.
00892    * To improve locality of access, we only prezero the part of the array
00893    * that the caller is about to access, not the entire in-memory array.
00894    */
00895   if (ptr->first_undef_row < end_row) {
00896     if (ptr->first_undef_row < start_row) {
00897       if (writable)     /* writer skipped over a section of array */
00898     ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
00899       undef_row = start_row;    /* but reader is allowed to read ahead */
00900     } else {
00901       undef_row = ptr->first_undef_row;
00902     }
00903     if (writable)
00904       ptr->first_undef_row = end_row;
00905     if (ptr->pre_zero) {
00906       size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK);
00907       undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
00908       end_row -= ptr->cur_start_row;
00909       while (undef_row < end_row) {
00910     jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
00911     undef_row++;
00912       }
00913     } else {
00914       if (! writable)       /* reader looking at undefined data */
00915     ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
00916     }
00917   }
00918   /* Flag the buffer dirty if caller will write in it */
00919   if (writable)
00920     ptr->dirty = TRUE;
00921   /* Return address of proper part of the buffer */
00922   return ptr->mem_buffer + (start_row - ptr->cur_start_row);
00923 }
00924 
00925 
00926 /*
00927  * Release all objects belonging to a specified pool.
00928  */
00929 
00930 METHODDEF(void)
00931 free_pool (j_common_ptr cinfo, int pool_id)
00932 {
00933   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00934   small_pool_ptr shdr_ptr;
00935   large_pool_ptr lhdr_ptr;
00936   size_t space_freed;
00937 
00938   if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
00939     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
00940 
00941 #ifdef MEM_STATS
00942   if (cinfo->err->trace_level > 1)
00943     print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
00944 #endif
00945 
00946   /* If freeing IMAGE pool, close any virtual arrays first */
00947   if (pool_id == JPOOL_IMAGE) {
00948     jvirt_sarray_ptr sptr;
00949     jvirt_barray_ptr bptr;
00950 
00951     for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
00952       if (sptr->b_s_open) { /* there may be no backing store */
00953     sptr->b_s_open = FALSE; /* prevent recursive close if error */
00954     (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
00955       }
00956     }
00957     mem->virt_sarray_list = NULL;
00958     for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
00959       if (bptr->b_s_open) { /* there may be no backing store */
00960     bptr->b_s_open = FALSE; /* prevent recursive close if error */
00961     (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
00962       }
00963     }
00964     mem->virt_barray_list = NULL;
00965   }
00966 
00967   /* Release large objects */
00968   lhdr_ptr = mem->large_list[pool_id];
00969   mem->large_list[pool_id] = NULL;
00970 
00971   while (lhdr_ptr != NULL) {
00972     large_pool_ptr next_lhdr_ptr = lhdr_ptr->hdr.next;
00973     space_freed = lhdr_ptr->hdr.bytes_used +
00974           lhdr_ptr->hdr.bytes_left +
00975           SIZEOF(large_pool_hdr);
00976     jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed);
00977     mem->total_space_allocated -= space_freed;
00978     lhdr_ptr = next_lhdr_ptr;
00979   }
00980 
00981   /* Release small objects */
00982   shdr_ptr = mem->small_list[pool_id];
00983   mem->small_list[pool_id] = NULL;
00984 
00985   while (shdr_ptr != NULL) {
00986     small_pool_ptr next_shdr_ptr = shdr_ptr->hdr.next;
00987     space_freed = shdr_ptr->hdr.bytes_used +
00988           shdr_ptr->hdr.bytes_left +
00989           SIZEOF(small_pool_hdr);
00990     jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
00991     mem->total_space_allocated -= space_freed;
00992     shdr_ptr = next_shdr_ptr;
00993   }
00994 }
00995 
00996 
00997 /*
00998  * Close up shop entirely.
00999  * Note that this cannot be called unless cinfo->mem is non-NULL.
01000  */
01001 
01002 METHODDEF(void)
01003 self_destruct (j_common_ptr cinfo)
01004 {
01005   int pool;
01006 
01007   /* Close all backing store, release all memory.
01008    * Releasing pools in reverse order might help avoid fragmentation
01009    * with some (brain-damaged) malloc libraries.
01010    */
01011   for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
01012     free_pool(cinfo, pool);
01013   }
01014 
01015   /* Release the memory manager control block too. */
01016   jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr));
01017   cinfo->mem = NULL;        /* ensures I will be called only once */
01018 
01019   jpeg_mem_term(cinfo);     /* system-dependent cleanup */
01020 }
01021 
01022 
01023 /*
01024  * Memory manager initialization.
01025  * When this is called, only the error manager pointer is valid in cinfo!
01026  */
01027 
01028 GLOBAL(void)
01029 jinit_memory_mgr (j_common_ptr cinfo)
01030 {
01031   my_mem_ptr mem;
01032   long max_to_use;
01033   int pool;
01034   size_t test_mac;
01035 
01036   cinfo->mem = NULL;        /* for safety if init fails */
01037 
01038   /* Check for configuration errors.
01039    * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably
01040    * doesn't reflect any real hardware alignment requirement.
01041    * The test is a little tricky: for X>0, X and X-1 have no one-bits
01042    * in common if and only if X is a power of 2, ie has only one one-bit.
01043    * Some compilers may give an "unreachable code" warning here; ignore it.
01044    */
01045   if ((SIZEOF(ALIGN_TYPE) & (SIZEOF(ALIGN_TYPE)-1)) != 0)
01046     ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
01047   /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
01048    * a multiple of SIZEOF(ALIGN_TYPE).
01049    * Again, an "unreachable code" warning may be ignored here.
01050    * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
01051    */
01052   test_mac = (size_t) MAX_ALLOC_CHUNK;
01053   if ((long) test_mac != MAX_ALLOC_CHUNK ||
01054       (MAX_ALLOC_CHUNK % SIZEOF(ALIGN_TYPE)) != 0)
01055     ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
01056 
01057   max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */
01058 
01059   /* Attempt to allocate memory manager's control block */
01060   mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr));
01061 
01062   if (mem == NULL) {
01063     jpeg_mem_term(cinfo);   /* system-dependent cleanup */
01064     ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
01065   }
01066 
01067   /* OK, fill in the method pointers */
01068   mem->pub.alloc_small = alloc_small;
01069   mem->pub.alloc_large = alloc_large;
01070   mem->pub.alloc_sarray = alloc_sarray;
01071   mem->pub.alloc_barray = alloc_barray;
01072   mem->pub.request_virt_sarray = request_virt_sarray;
01073   mem->pub.request_virt_barray = request_virt_barray;
01074   mem->pub.realize_virt_arrays = realize_virt_arrays;
01075   mem->pub.access_virt_sarray = access_virt_sarray;
01076   mem->pub.access_virt_barray = access_virt_barray;
01077   mem->pub.free_pool = free_pool;
01078   mem->pub.self_destruct = self_destruct;
01079 
01080   /* Make MAX_ALLOC_CHUNK accessible to other modules */
01081   mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK;
01082 
01083   /* Initialize working state */
01084   mem->pub.max_memory_to_use = max_to_use;
01085 
01086   for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
01087     mem->small_list[pool] = NULL;
01088     mem->large_list[pool] = NULL;
01089   }
01090   mem->virt_sarray_list = NULL;
01091   mem->virt_barray_list = NULL;
01092 
01093   mem->total_space_allocated = SIZEOF(my_memory_mgr);
01094 
01095   /* Declare ourselves open for business */
01096   cinfo->mem = & mem->pub;
01097 
01098   /* Check for an environment variable JPEGMEM; if found, override the
01099    * default max_memory setting from jpeg_mem_init.  Note that the
01100    * surrounding application may again override this value.
01101    * If your system doesn't support getenv(), define NO_GETENV to disable
01102    * this feature.
01103    */
01104 #ifndef NO_GETENV
01105   { char * memenv;
01106 
01107     if ((memenv = getenv("JPEGMEM")) != NULL) {
01108       char ch = 'x';
01109 
01110       if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
01111     if (ch == 'm' || ch == 'M')
01112       max_to_use *= 1000L;
01113     mem->pub.max_memory_to_use = max_to_use * 1000L;
01114       }
01115     }
01116   }
01117 #endif
01118 
01119 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines