Skip to content

AFNI/NIfTI Server

Sections
Personal tools
You are here: Home » AFNI » Documentation

Doxygen Source Code Documentation


Main Page   Alphabetical List   Data Structures   File List   Data Fields   Globals   Search  

jmemmgr.c File Reference

#include "jinclude.h"
#include "jpeglib.h"
#include "jmemsys.h"

Go to the source code of this file.


Data Structures

struct  jvirt_barray_control
struct  jvirt_sarray_control
union  large_pool_struct
struct  my_memory_mgr
union  small_pool_struct

Defines

#define JPEG_INTERNALS
#define AM_MEMORY_MANAGER
#define ALIGN_TYPE   double
#define MIN_SLOP   50

Typedefs

typedef small_pool_structsmall_pool_ptr
typedef small_pool_struct small_pool_hdr
typedef large_pool_struct
FAR * 
large_pool_ptr
typedef large_pool_struct large_pool_hdr
typedef my_memory_mgrmy_mem_ptr

Functions

char *getenv JPP ((const char *name))
 out_of_memory (j_common_ptr cinfo, int which)
 alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
 alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
 alloc_sarray (j_common_ptr cinfo, int pool_id, JDIMENSION samplesperrow, JDIMENSION numrows)
 alloc_barray (j_common_ptr cinfo, int pool_id, JDIMENSION blocksperrow, JDIMENSION numrows)
 request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero, JDIMENSION samplesperrow, JDIMENSION numrows, JDIMENSION maxaccess)
 request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero, JDIMENSION blocksperrow, JDIMENSION numrows, JDIMENSION maxaccess)
 realize_virt_arrays (j_common_ptr cinfo)
 do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
 do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
 access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr, JDIMENSION start_row, JDIMENSION num_rows, boolean writable)
 access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr, JDIMENSION start_row, JDIMENSION num_rows, boolean writable)
 free_pool (j_common_ptr cinfo, int pool_id)
 self_destruct (j_common_ptr cinfo)
 jinit_memory_mgr (j_common_ptr cinfo)

Variables

const size_t first_pool_slop [JPOOL_NUMPOOLS]
const size_t extra_pool_slop [JPOOL_NUMPOOLS]

Define Documentation

#define ALIGN_TYPE   double
 

Definition at line 73 of file jmemmgr.c.

Referenced by alloc_large(), alloc_small(), and jinit_memory_mgr().

#define AM_MEMORY_MANAGER
 

Definition at line 28 of file jmemmgr.c.

#define JPEG_INTERNALS
 

Definition at line 27 of file jmemmgr.c.

#define MIN_SLOP   50
 

Definition at line 253 of file jmemmgr.c.

Referenced by alloc_small().


Typedef Documentation

typedef union large_pool_struct large_pool_hdr
 

typedef union large_pool_struct FAR* large_pool_ptr
 

Definition at line 100 of file jmemmgr.c.

typedef my_memory_mgr* my_mem_ptr
 

Definition at line 140 of file jmemmgr.c.

typedef union small_pool_struct small_pool_hdr
 

typedef union small_pool_struct* small_pool_ptr
 

Definition at line 89 of file jmemmgr.c.


Function Documentation

access_virt_barray j_common_ptr    cinfo,
jvirt_barray_ptr    ptr,
JDIMENSION    start_row,
JDIMENSION    num_rows,
boolean    writable
 

Definition at line 841 of file jmemmgr.c.

References jvirt_barray_control::b_s_open, jvirt_barray_control::blocksperrow, jvirt_barray_control::cur_start_row, jvirt_barray_control::dirty, do_barray_io(), ERREXIT, jvirt_barray_control::first_undef_row, JBLOCK, JDIMENSION, jzero_far(), jvirt_barray_control::maxaccess, jvirt_barray_control::mem_buffer, num_rows, jvirt_barray_control::pre_zero, jvirt_barray_control::rows_in_array, jvirt_barray_control::rows_in_mem, and SIZEOF.

Referenced by jinit_memory_mgr().

00847 {
00848   JDIMENSION end_row = start_row + num_rows;
00849   JDIMENSION undef_row;
00850 
00851   /* debugging check */
00852   if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
00853       ptr->mem_buffer == NULL)
00854     ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
00855 
00856   /* Make the desired part of the virtual array accessible */
00857   if (start_row < ptr->cur_start_row ||
00858       end_row > ptr->cur_start_row+ptr->rows_in_mem) {
00859     if (! ptr->b_s_open)
00860       ERREXIT(cinfo, JERR_VIRTUAL_BUG);
00861     /* Flush old buffer contents if necessary */
00862     if (ptr->dirty) {
00863       do_barray_io(cinfo, ptr, TRUE);
00864       ptr->dirty = FALSE;
00865     }
00866     /* Decide what part of virtual array to access.
00867      * Algorithm: if target address > current window, assume forward scan,
00868      * load starting at target address.  If target address < current window,
00869      * assume backward scan, load so that target area is top of window.
00870      * Note that when switching from forward write to forward read, will have
00871      * start_row = 0, so the limiting case applies and we load from 0 anyway.
00872      */
00873     if (start_row > ptr->cur_start_row) {
00874       ptr->cur_start_row = start_row;
00875     } else {
00876       /* use long arithmetic here to avoid overflow & unsigned problems */
00877       long ltemp;
00878 
00879       ltemp = (long) end_row - (long) ptr->rows_in_mem;
00880       if (ltemp < 0)
00881         ltemp = 0;              /* don't fall off front end of file */
00882       ptr->cur_start_row = (JDIMENSION) ltemp;
00883     }
00884     /* Read in the selected part of the array.
00885      * During the initial write pass, we will do no actual read
00886      * because the selected part is all undefined.
00887      */
00888     do_barray_io(cinfo, ptr, FALSE);
00889   }
00890   /* Ensure the accessed part of the array is defined; prezero if needed.
00891    * To improve locality of access, we only prezero the part of the array
00892    * that the caller is about to access, not the entire in-memory array.
00893    */
00894   if (ptr->first_undef_row < end_row) {
00895     if (ptr->first_undef_row < start_row) {
00896       if (writable)             /* writer skipped over a section of array */
00897         ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
00898       undef_row = start_row;    /* but reader is allowed to read ahead */
00899     } else {
00900       undef_row = ptr->first_undef_row;
00901     }
00902     if (writable)
00903       ptr->first_undef_row = end_row;
00904     if (ptr->pre_zero) {
00905       size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK);
00906       undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
00907       end_row -= ptr->cur_start_row;
00908       while (undef_row < end_row) {
00909         jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
00910         undef_row++;
00911       }
00912     } else {
00913       if (! writable)           /* reader looking at undefined data */
00914         ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
00915     }
00916   }
00917   /* Flag the buffer dirty if caller will write in it */
00918   if (writable)
00919     ptr->dirty = TRUE;
00920   /* Return address of proper part of the buffer */
00921   return ptr->mem_buffer + (start_row - ptr->cur_start_row);
00922 }

access_virt_sarray j_common_ptr    cinfo,
jvirt_sarray_ptr    ptr,
JDIMENSION    start_row,
JDIMENSION    num_rows,
boolean    writable
 

Definition at line 756 of file jmemmgr.c.

References jvirt_sarray_control::b_s_open, jvirt_sarray_control::cur_start_row, jvirt_sarray_control::dirty, do_sarray_io(), ERREXIT, jvirt_sarray_control::first_undef_row, JDIMENSION, JSAMPLE, jzero_far(), jvirt_sarray_control::maxaccess, jvirt_sarray_control::mem_buffer, num_rows, jvirt_sarray_control::pre_zero, jvirt_sarray_control::rows_in_array, jvirt_sarray_control::rows_in_mem, jvirt_sarray_control::samplesperrow, and SIZEOF.

Referenced by jinit_memory_mgr().

00762 {
00763   JDIMENSION end_row = start_row + num_rows;
00764   JDIMENSION undef_row;
00765 
00766   /* debugging check */
00767   if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
00768       ptr->mem_buffer == NULL)
00769     ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
00770 
00771   /* Make the desired part of the virtual array accessible */
00772   if (start_row < ptr->cur_start_row ||
00773       end_row > ptr->cur_start_row+ptr->rows_in_mem) {
00774     if (! ptr->b_s_open)
00775       ERREXIT(cinfo, JERR_VIRTUAL_BUG);
00776     /* Flush old buffer contents if necessary */
00777     if (ptr->dirty) {
00778       do_sarray_io(cinfo, ptr, TRUE);
00779       ptr->dirty = FALSE;
00780     }
00781     /* Decide what part of virtual array to access.
00782      * Algorithm: if target address > current window, assume forward scan,
00783      * load starting at target address.  If target address < current window,
00784      * assume backward scan, load so that target area is top of window.
00785      * Note that when switching from forward write to forward read, will have
00786      * start_row = 0, so the limiting case applies and we load from 0 anyway.
00787      */
00788     if (start_row > ptr->cur_start_row) {
00789       ptr->cur_start_row = start_row;
00790     } else {
00791       /* use long arithmetic here to avoid overflow & unsigned problems */
00792       long ltemp;
00793 
00794       ltemp = (long) end_row - (long) ptr->rows_in_mem;
00795       if (ltemp < 0)
00796         ltemp = 0;              /* don't fall off front end of file */
00797       ptr->cur_start_row = (JDIMENSION) ltemp;
00798     }
00799     /* Read in the selected part of the array.
00800      * During the initial write pass, we will do no actual read
00801      * because the selected part is all undefined.
00802      */
00803     do_sarray_io(cinfo, ptr, FALSE);
00804   }
00805   /* Ensure the accessed part of the array is defined; prezero if needed.
00806    * To improve locality of access, we only prezero the part of the array
00807    * that the caller is about to access, not the entire in-memory array.
00808    */
00809   if (ptr->first_undef_row < end_row) {
00810     if (ptr->first_undef_row < start_row) {
00811       if (writable)             /* writer skipped over a section of array */
00812         ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
00813       undef_row = start_row;    /* but reader is allowed to read ahead */
00814     } else {
00815       undef_row = ptr->first_undef_row;
00816     }
00817     if (writable)
00818       ptr->first_undef_row = end_row;
00819     if (ptr->pre_zero) {
00820       size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE);
00821       undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
00822       end_row -= ptr->cur_start_row;
00823       while (undef_row < end_row) {
00824         jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
00825         undef_row++;
00826       }
00827     } else {
00828       if (! writable)           /* reader looking at undefined data */
00829         ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
00830     }
00831   }
00832   /* Flag the buffer dirty if caller will write in it */
00833   if (writable)
00834     ptr->dirty = TRUE;
00835   /* Return address of proper part of the buffer */
00836   return ptr->mem_buffer + (start_row - ptr->cur_start_row);
00837 }

alloc_barray j_common_ptr    cinfo,
int    pool_id,
JDIMENSION    blocksperrow,
JDIMENSION    numrows
 

Definition at line 443 of file jmemmgr.c.

References alloc_large(), alloc_small(), ERREXIT, i, JBLOCK, JBLOCKARRAY, JBLOCKROW, JDIMENSION, my_memory_mgr::last_rowsperchunk, MAX_ALLOC_CHUNK, MIN, and SIZEOF.

Referenced by jinit_memory_mgr(), and realize_virt_arrays().

00446 {
00447   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00448   JBLOCKARRAY result;
00449   JBLOCKROW workspace;
00450   JDIMENSION rowsperchunk, currow, i;
00451   long ltemp;
00452 
00453   /* Calculate max # of rows allowed in one allocation chunk */
00454   ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
00455           ((long) blocksperrow * SIZEOF(JBLOCK));
00456   if (ltemp <= 0)
00457     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
00458   if (ltemp < (long) numrows)
00459     rowsperchunk = (JDIMENSION) ltemp;
00460   else
00461     rowsperchunk = numrows;
00462   mem->last_rowsperchunk = rowsperchunk;
00463 
00464   /* Get space for row pointers (small object) */
00465   result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
00466                                      (size_t) (numrows * SIZEOF(JBLOCKROW)));
00467 
00468   /* Get the rows themselves (large objects) */
00469   currow = 0;
00470   while (currow < numrows) {
00471     rowsperchunk = MIN(rowsperchunk, numrows - currow);
00472     workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
00473         (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
00474                   * SIZEOF(JBLOCK)));
00475     for (i = rowsperchunk; i > 0; i--) {
00476       result[currow++] = workspace;
00477       workspace += blocksperrow;
00478     }
00479   }
00480 
00481   return result;
00482 }

alloc_large j_common_ptr    cinfo,
int    pool_id,
size_t    sizeofobject
 

Definition at line 342 of file jmemmgr.c.

References ALIGN_TYPE, ERREXIT1, jpeg_get_large(), JPOOL_NUMPOOLS, my_memory_mgr::large_list, MAX_ALLOC_CHUNK, out_of_memory(), SIZEOF, sizeofobject, and my_memory_mgr::total_space_allocated.

Referenced by alloc_barray(), alloc_sarray(), and jinit_memory_mgr().

00344 {
00345   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00346   large_pool_ptr hdr_ptr;
00347   size_t odd_bytes;
00348 
00349   /* Check for unsatisfiable request (do now to ensure no overflow below) */
00350   if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)))
00351     out_of_memory(cinfo, 3);    /* request exceeds malloc's ability */
00352 
00353   /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
00354   odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
00355   if (odd_bytes > 0)
00356     sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
00357 
00358   /* Always make a new pool */
00359   if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
00360     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
00361 
00362   hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
00363                                             SIZEOF(large_pool_hdr));
00364   if (hdr_ptr == NULL)
00365     out_of_memory(cinfo, 4);    /* jpeg_get_large failed */
00366   mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr);
00367 
00368   /* Success, initialize the new pool header and add to list */
00369   hdr_ptr->hdr.next = mem->large_list[pool_id];
00370   /* We maintain space counts in each pool header for statistical purposes,
00371    * even though they are not needed for allocation.
00372    */
00373   hdr_ptr->hdr.bytes_used = sizeofobject;
00374   hdr_ptr->hdr.bytes_left = 0;
00375   mem->large_list[pool_id] = hdr_ptr;
00376 
00377   return (void FAR *) (hdr_ptr + 1); /* point to first data byte in pool */
00378 }

alloc_sarray j_common_ptr    cinfo,
int    pool_id,
JDIMENSION    samplesperrow,
JDIMENSION    numrows
 

Definition at line 395 of file jmemmgr.c.

References alloc_large(), alloc_small(), ERREXIT, i, JDIMENSION, JSAMPARRAY, JSAMPLE, JSAMPROW, my_memory_mgr::last_rowsperchunk, MAX_ALLOC_CHUNK, MIN, and SIZEOF.

Referenced by jinit_memory_mgr(), and realize_virt_arrays().

00398 {
00399   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00400   JSAMPARRAY result;
00401   JSAMPROW workspace;
00402   JDIMENSION rowsperchunk, currow, i;
00403   long ltemp;
00404 
00405   /* Calculate max # of rows allowed in one allocation chunk */
00406   ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
00407           ((long) samplesperrow * SIZEOF(JSAMPLE));
00408   if (ltemp <= 0)
00409     ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
00410   if (ltemp < (long) numrows)
00411     rowsperchunk = (JDIMENSION) ltemp;
00412   else
00413     rowsperchunk = numrows;
00414   mem->last_rowsperchunk = rowsperchunk;
00415 
00416   /* Get space for row pointers (small object) */
00417   result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
00418                                     (size_t) (numrows * SIZEOF(JSAMPROW)));
00419 
00420   /* Get the rows themselves (large objects) */
00421   currow = 0;
00422   while (currow < numrows) {
00423     rowsperchunk = MIN(rowsperchunk, numrows - currow);
00424     workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
00425         (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
00426                   * SIZEOF(JSAMPLE)));
00427     for (i = rowsperchunk; i > 0; i--) {
00428       result[currow++] = workspace;
00429       workspace += samplesperrow;
00430     }
00431   }
00432 
00433   return result;
00434 }

alloc_small j_common_ptr    cinfo,
int    pool_id,
size_t    sizeofobject
 

Definition at line 257 of file jmemmgr.c.

References ALIGN_TYPE, ERREXIT1, extra_pool_slop, first_pool_slop, small_pool_struct::hdr, jpeg_get_small(), JPOOL_NUMPOOLS, MAX_ALLOC_CHUNK, MIN_SLOP, out_of_memory(), SIZEOF, sizeofobject, my_memory_mgr::small_list, and my_memory_mgr::total_space_allocated.

Referenced by alloc_barray(), alloc_sarray(), jinit_memory_mgr(), request_virt_barray(), and request_virt_sarray().

00259 {
00260   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00261   small_pool_ptr hdr_ptr, prev_hdr_ptr;
00262   char * data_ptr;
00263   size_t odd_bytes, min_request, slop;
00264 
00265   /* Check for unsatisfiable request (do now to ensure no overflow below) */
00266   if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr)))
00267     out_of_memory(cinfo, 1);    /* request exceeds malloc's ability */
00268 
00269   /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
00270   odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
00271   if (odd_bytes > 0)
00272     sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
00273 
00274   /* See if space is available in any existing pool */
00275   if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
00276     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
00277   prev_hdr_ptr = NULL;
00278   hdr_ptr = mem->small_list[pool_id];
00279   while (hdr_ptr != NULL) {
00280     if (hdr_ptr->hdr.bytes_left >= sizeofobject)
00281       break;                    /* found pool with enough space */
00282     prev_hdr_ptr = hdr_ptr;
00283     hdr_ptr = hdr_ptr->hdr.next;
00284   }
00285 
00286   /* Time to make a new pool? */
00287   if (hdr_ptr == NULL) {
00288     /* min_request is what we need now, slop is what will be leftover */
00289     min_request = sizeofobject + SIZEOF(small_pool_hdr);
00290     if (prev_hdr_ptr == NULL)   /* first pool in class? */
00291       slop = first_pool_slop[pool_id];
00292     else
00293       slop = extra_pool_slop[pool_id];
00294     /* Don't ask for more than MAX_ALLOC_CHUNK */
00295     if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
00296       slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
00297     /* Try to get space, if fail reduce slop and try again */
00298     for (;;) {
00299       hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
00300       if (hdr_ptr != NULL)
00301         break;
00302       slop /= 2;
00303       if (slop < MIN_SLOP)      /* give up when it gets real small */
00304         out_of_memory(cinfo, 2); /* jpeg_get_small failed */
00305     }
00306     mem->total_space_allocated += min_request + slop;
00307     /* Success, initialize the new pool header and add to end of list */
00308     hdr_ptr->hdr.next = NULL;
00309     hdr_ptr->hdr.bytes_used = 0;
00310     hdr_ptr->hdr.bytes_left = sizeofobject + slop;
00311     if (prev_hdr_ptr == NULL)   /* first pool in class? */
00312       mem->small_list[pool_id] = hdr_ptr;
00313     else
00314       prev_hdr_ptr->hdr.next = hdr_ptr;
00315   }
00316 
00317   /* OK, allocate the object from the current pool */
00318   data_ptr = (char *) (hdr_ptr + 1); /* point to first data byte in pool */
00319   data_ptr += hdr_ptr->hdr.bytes_used; /* point to place for object */
00320   hdr_ptr->hdr.bytes_used += sizeofobject;
00321   hdr_ptr->hdr.bytes_left -= sizeofobject;
00322 
00323   return (void *) data_ptr;
00324 }

do_barray_io j_common_ptr    cinfo,
jvirt_barray_ptr    ptr,
boolean    writing
 

Definition at line 723 of file jmemmgr.c.

References jvirt_barray_control::b_s_info, jvirt_barray_control::blocksperrow, jvirt_barray_control::cur_start_row, jvirt_barray_control::first_undef_row, i, JBLOCK, jvirt_barray_control::mem_buffer, MIN, rows, jvirt_barray_control::rows_in_array, jvirt_barray_control::rows_in_mem, jvirt_barray_control::rowsperchunk, and SIZEOF.

Referenced by access_virt_barray().

00725 {
00726   long bytesperrow, file_offset, byte_count, rows, thisrow, i;
00727 
00728   bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK);
00729   file_offset = ptr->cur_start_row * bytesperrow;
00730   /* Loop to read or write each allocation chunk in mem_buffer */
00731   for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
00732     /* One chunk, but check for short chunk at end of buffer */
00733     rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
00734     /* Transfer no more than is currently defined */
00735     thisrow = (long) ptr->cur_start_row + i;
00736     rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
00737     /* Transfer no more than fits in file */
00738     rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
00739     if (rows <= 0)              /* this chunk might be past end of file! */
00740       break;
00741     byte_count = rows * bytesperrow;
00742     if (writing)
00743       (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
00744                                             (void FAR *) ptr->mem_buffer[i],
00745                                             file_offset, byte_count);
00746     else
00747       (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
00748                                            (void FAR *) ptr->mem_buffer[i],
00749                                            file_offset, byte_count);
00750     file_offset += byte_count;
00751   }
00752 }

do_sarray_io j_common_ptr    cinfo,
jvirt_sarray_ptr    ptr,
boolean    writing
 

Definition at line 690 of file jmemmgr.c.

References jvirt_sarray_control::b_s_info, jvirt_sarray_control::cur_start_row, jvirt_sarray_control::first_undef_row, i, JSAMPLE, jvirt_sarray_control::mem_buffer, MIN, rows, jvirt_sarray_control::rows_in_array, jvirt_sarray_control::rows_in_mem, jvirt_sarray_control::rowsperchunk, jvirt_sarray_control::samplesperrow, and SIZEOF.

Referenced by access_virt_sarray().

00692 {
00693   long bytesperrow, file_offset, byte_count, rows, thisrow, i;
00694 
00695   bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE);
00696   file_offset = ptr->cur_start_row * bytesperrow;
00697   /* Loop to read or write each allocation chunk in mem_buffer */
00698   for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
00699     /* One chunk, but check for short chunk at end of buffer */
00700     rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
00701     /* Transfer no more than is currently defined */
00702     thisrow = (long) ptr->cur_start_row + i;
00703     rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
00704     /* Transfer no more than fits in file */
00705     rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
00706     if (rows <= 0)              /* this chunk might be past end of file! */
00707       break;
00708     byte_count = rows * bytesperrow;
00709     if (writing)
00710       (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
00711                                             (void FAR *) ptr->mem_buffer[i],
00712                                             file_offset, byte_count);
00713     else
00714       (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
00715                                            (void FAR *) ptr->mem_buffer[i],
00716                                            file_offset, byte_count);
00717     file_offset += byte_count;
00718   }
00719 }

free_pool j_common_ptr    cinfo,
int    pool_id
 

Definition at line 930 of file jmemmgr.c.

References jvirt_barray_control::b_s_info, jvirt_sarray_control::b_s_info, jvirt_barray_control::b_s_open, jvirt_sarray_control::b_s_open, ERREXIT1, small_pool_struct::hdr, large_pool_struct::hdr, jpeg_free_large(), jpeg_free_small(), JPOOL_IMAGE, JPOOL_NUMPOOLS, my_memory_mgr::large_list, jvirt_barray_control::next, jvirt_sarray_control::next, SIZEOF, my_memory_mgr::small_list, my_memory_mgr::total_space_allocated, my_memory_mgr::virt_barray_list, and my_memory_mgr::virt_sarray_list.

Referenced by jinit_memory_mgr(), and self_destruct().

00931 {
00932   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00933   small_pool_ptr shdr_ptr;
00934   large_pool_ptr lhdr_ptr;
00935   size_t space_freed;
00936 
00937   if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
00938     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
00939 
00940 #ifdef MEM_STATS
00941   if (cinfo->err->trace_level > 1)
00942     print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
00943 #endif
00944 
00945   /* If freeing IMAGE pool, close any virtual arrays first */
00946   if (pool_id == JPOOL_IMAGE) {
00947     jvirt_sarray_ptr sptr;
00948     jvirt_barray_ptr bptr;
00949 
00950     for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
00951       if (sptr->b_s_open) {     /* there may be no backing store */
00952         sptr->b_s_open = FALSE; /* prevent recursive close if error */
00953         (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
00954       }
00955     }
00956     mem->virt_sarray_list = NULL;
00957     for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
00958       if (bptr->b_s_open) {     /* there may be no backing store */
00959         bptr->b_s_open = FALSE; /* prevent recursive close if error */
00960         (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
00961       }
00962     }
00963     mem->virt_barray_list = NULL;
00964   }
00965 
00966   /* Release large objects */
00967   lhdr_ptr = mem->large_list[pool_id];
00968   mem->large_list[pool_id] = NULL;
00969 
00970   while (lhdr_ptr != NULL) {
00971     large_pool_ptr next_lhdr_ptr = lhdr_ptr->hdr.next;
00972     space_freed = lhdr_ptr->hdr.bytes_used +
00973                   lhdr_ptr->hdr.bytes_left +
00974                   SIZEOF(large_pool_hdr);
00975     jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed);
00976     mem->total_space_allocated -= space_freed;
00977     lhdr_ptr = next_lhdr_ptr;
00978   }
00979 
00980   /* Release small objects */
00981   shdr_ptr = mem->small_list[pool_id];
00982   mem->small_list[pool_id] = NULL;
00983 
00984   while (shdr_ptr != NULL) {
00985     small_pool_ptr next_shdr_ptr = shdr_ptr->hdr.next;
00986     space_freed = shdr_ptr->hdr.bytes_used +
00987                   shdr_ptr->hdr.bytes_left +
00988                   SIZEOF(small_pool_hdr);
00989     jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
00990     mem->total_space_allocated -= space_freed;
00991     shdr_ptr = next_shdr_ptr;
00992   }
00993 }

jinit_memory_mgr j_common_ptr    cinfo
 

Definition at line 1028 of file jmemmgr.c.

References access_virt_barray(), access_virt_sarray(), ALIGN_TYPE, alloc_barray(), alloc_large(), alloc_sarray(), alloc_small(), ERREXIT, ERREXIT1, free_pool(), getenv(), JERR_OUT_OF_MEMORY, jpeg_get_small(), jpeg_mem_init(), jpeg_mem_term(), JPOOL_NUMPOOLS, JPOOL_PERMANENT, L, my_memory_mgr::large_list, jpeg_memory_mgr::max_alloc_chunk, MAX_ALLOC_CHUNK, jpeg_memory_mgr::max_memory_to_use, my_memory_mgr::pub, realize_virt_arrays(), request_virt_barray(), request_virt_sarray(), self_destruct(), SIZEOF, my_memory_mgr::small_list, my_memory_mgr::total_space_allocated, my_memory_mgr::virt_barray_list, and my_memory_mgr::virt_sarray_list.

Referenced by jpeg_CreateCompress(), and jpeg_CreateDecompress().

01029 {
01030   my_mem_ptr mem;
01031   long max_to_use;
01032   int pool;
01033   size_t test_mac;
01034 
01035   cinfo->mem = NULL;            /* for safety if init fails */
01036 
01037   /* Check for configuration errors.
01038    * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably
01039    * doesn't reflect any real hardware alignment requirement.
01040    * The test is a little tricky: for X>0, X and X-1 have no one-bits
01041    * in common if and only if X is a power of 2, ie has only one one-bit.
01042    * Some compilers may give an "unreachable code" warning here; ignore it.
01043    */
01044   if ((SIZEOF(ALIGN_TYPE) & (SIZEOF(ALIGN_TYPE)-1)) != 0)
01045     ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
01046   /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
01047    * a multiple of SIZEOF(ALIGN_TYPE).
01048    * Again, an "unreachable code" warning may be ignored here.
01049    * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
01050    */
01051   test_mac = (size_t) MAX_ALLOC_CHUNK;
01052   if ((long) test_mac != MAX_ALLOC_CHUNK ||
01053       (MAX_ALLOC_CHUNK % SIZEOF(ALIGN_TYPE)) != 0)
01054     ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
01055 
01056   max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */
01057 
01058   /* Attempt to allocate memory manager's control block */
01059   mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr));
01060 
01061   if (mem == NULL) {
01062     jpeg_mem_term(cinfo);       /* system-dependent cleanup */
01063     ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
01064   }
01065 
01066   /* OK, fill in the method pointers */
01067   mem->pub.alloc_small = alloc_small;
01068   mem->pub.alloc_large = alloc_large;
01069   mem->pub.alloc_sarray = alloc_sarray;
01070   mem->pub.alloc_barray = alloc_barray;
01071   mem->pub.request_virt_sarray = request_virt_sarray;
01072   mem->pub.request_virt_barray = request_virt_barray;
01073   mem->pub.realize_virt_arrays = realize_virt_arrays;
01074   mem->pub.access_virt_sarray = access_virt_sarray;
01075   mem->pub.access_virt_barray = access_virt_barray;
01076   mem->pub.free_pool = free_pool;
01077   mem->pub.self_destruct = self_destruct;
01078 
01079   /* Make MAX_ALLOC_CHUNK accessible to other modules */
01080   mem->pub.max_alloc_chunk = MAX_ALLOC_CHUNK;
01081 
01082   /* Initialize working state */
01083   mem->pub.max_memory_to_use = max_to_use;
01084 
01085   for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
01086     mem->small_list[pool] = NULL;
01087     mem->large_list[pool] = NULL;
01088   }
01089   mem->virt_sarray_list = NULL;
01090   mem->virt_barray_list = NULL;
01091 
01092   mem->total_space_allocated = SIZEOF(my_memory_mgr);
01093 
01094   /* Declare ourselves open for business */
01095   cinfo->mem = & mem->pub;
01096 
01097   /* Check for an environment variable JPEGMEM; if found, override the
01098    * default max_memory setting from jpeg_mem_init.  Note that the
01099    * surrounding application may again override this value.
01100    * If your system doesn't support getenv(), define NO_GETENV to disable
01101    * this feature.
01102    */
01103 #ifndef NO_GETENV
01104   { char * memenv;
01105 
01106     if ((memenv = getenv("JPEGMEM")) != NULL) {
01107       char ch = 'x';
01108 
01109       if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
01110         if (ch == 'm' || ch == 'M')
01111           max_to_use *= 1000L;
01112         mem->pub.max_memory_to_use = max_to_use * 1000L;
01113       }
01114     }
01115   }
01116 #endif
01117 
01118 }

char* getenv JPP (const char *name  
 

out_of_memory j_common_ptr    cinfo,
int    which
 

Definition at line 217 of file jmemmgr.c.

References ERREXIT1, and JERR_OUT_OF_MEMORY.

Referenced by alloc_large(), and alloc_small().

00220 {
00221 #ifdef MEM_STATS
00222   cinfo->err->trace_level = 2;  /* force self_destruct to report stats */
00223 #endif
00224   ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
00225 }

realize_virt_arrays j_common_ptr    cinfo
 

Definition at line 583 of file jmemmgr.c.

References alloc_barray(), alloc_sarray(), jvirt_barray_control::b_s_info, jvirt_sarray_control::b_s_info, jvirt_barray_control::b_s_open, jvirt_sarray_control::b_s_open, jvirt_barray_control::blocksperrow, jvirt_barray_control::cur_start_row, jvirt_sarray_control::cur_start_row, jvirt_barray_control::dirty, jvirt_sarray_control::dirty, jvirt_barray_control::first_undef_row, jvirt_sarray_control::first_undef_row, JBLOCK, jpeg_mem_available(), jpeg_open_backing_store(), JPOOL_IMAGE, JSAMPLE, L, my_memory_mgr::last_rowsperchunk, jvirt_barray_control::maxaccess, jvirt_sarray_control::maxaccess, jvirt_barray_control::mem_buffer, jvirt_sarray_control::mem_buffer, jvirt_barray_control::next, jvirt_sarray_control::next, jvirt_barray_control::rows_in_array, jvirt_sarray_control::rows_in_array, jvirt_barray_control::rows_in_mem, jvirt_sarray_control::rows_in_mem, jvirt_barray_control::rowsperchunk, jvirt_sarray_control::rowsperchunk, jvirt_sarray_control::samplesperrow, SIZEOF, my_memory_mgr::total_space_allocated, my_memory_mgr::virt_barray_list, and my_memory_mgr::virt_sarray_list.

Referenced by jinit_memory_mgr().

00585 {
00586   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00587   long space_per_minheight, maximum_space, avail_mem;
00588   long minheights, max_minheights;
00589   jvirt_sarray_ptr sptr;
00590   jvirt_barray_ptr bptr;
00591 
00592   /* Compute the minimum space needed (maxaccess rows in each buffer)
00593    * and the maximum space needed (full image height in each buffer).
00594    * These may be of use to the system-dependent jpeg_mem_available routine.
00595    */
00596   space_per_minheight = 0;
00597   maximum_space = 0;
00598   for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
00599     if (sptr->mem_buffer == NULL) { /* if not realized yet */
00600       space_per_minheight += (long) sptr->maxaccess *
00601                              (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
00602       maximum_space += (long) sptr->rows_in_array *
00603                        (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
00604     }
00605   }
00606   for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
00607     if (bptr->mem_buffer == NULL) { /* if not realized yet */
00608       space_per_minheight += (long) bptr->maxaccess *
00609                              (long) bptr->blocksperrow * SIZEOF(JBLOCK);
00610       maximum_space += (long) bptr->rows_in_array *
00611                        (long) bptr->blocksperrow * SIZEOF(JBLOCK);
00612     }
00613   }
00614 
00615   if (space_per_minheight <= 0)
00616     return;                     /* no unrealized arrays, no work */
00617 
00618   /* Determine amount of memory to actually use; this is system-dependent. */
00619   avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
00620                                  mem->total_space_allocated);
00621 
00622   /* If the maximum space needed is available, make all the buffers full
00623    * height; otherwise parcel it out with the same number of minheights
00624    * in each buffer.
00625    */
00626   if (avail_mem >= maximum_space)
00627     max_minheights = 1000000000L;
00628   else {
00629     max_minheights = avail_mem / space_per_minheight;
00630     /* If there doesn't seem to be enough space, try to get the minimum
00631      * anyway.  This allows a "stub" implementation of jpeg_mem_available().
00632      */
00633     if (max_minheights <= 0)
00634       max_minheights = 1;
00635   }
00636 
00637   /* Allocate the in-memory buffers and initialize backing store as needed. */
00638 
00639   for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
00640     if (sptr->mem_buffer == NULL) { /* if not realized yet */
00641       minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
00642       if (minheights <= max_minheights) {
00643         /* This buffer fits in memory */
00644         sptr->rows_in_mem = sptr->rows_in_array;
00645       } else {
00646         /* It doesn't fit in memory, create backing store. */
00647         sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
00648         jpeg_open_backing_store(cinfo, & sptr->b_s_info,
00649                                 (long) sptr->rows_in_array *
00650                                 (long) sptr->samplesperrow *
00651                                 (long) SIZEOF(JSAMPLE));
00652         sptr->b_s_open = TRUE;
00653       }
00654       sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
00655                                       sptr->samplesperrow, sptr->rows_in_mem);
00656       sptr->rowsperchunk = mem->last_rowsperchunk;
00657       sptr->cur_start_row = 0;
00658       sptr->first_undef_row = 0;
00659       sptr->dirty = FALSE;
00660     }
00661   }
00662 
00663   for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
00664     if (bptr->mem_buffer == NULL) { /* if not realized yet */
00665       minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
00666       if (minheights <= max_minheights) {
00667         /* This buffer fits in memory */
00668         bptr->rows_in_mem = bptr->rows_in_array;
00669       } else {
00670         /* It doesn't fit in memory, create backing store. */
00671         bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
00672         jpeg_open_backing_store(cinfo, & bptr->b_s_info,
00673                                 (long) bptr->rows_in_array *
00674                                 (long) bptr->blocksperrow *
00675                                 (long) SIZEOF(JBLOCK));
00676         bptr->b_s_open = TRUE;
00677       }
00678       bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
00679                                       bptr->blocksperrow, bptr->rows_in_mem);
00680       bptr->rowsperchunk = mem->last_rowsperchunk;
00681       bptr->cur_start_row = 0;
00682       bptr->first_undef_row = 0;
00683       bptr->dirty = FALSE;
00684     }
00685   }
00686 }

request_virt_barray j_common_ptr    cinfo,
int    pool_id,
boolean    pre_zero,
JDIMENSION    blocksperrow,
JDIMENSION    numrows,
JDIMENSION    maxaccess
 

Definition at line 553 of file jmemmgr.c.

References alloc_small(), jvirt_barray_control::b_s_open, jvirt_barray_control::blocksperrow, ERREXIT1, JDIMENSION, JPOOL_IMAGE, jvirt_barray_control::maxaccess, jvirt_barray_control::mem_buffer, jvirt_barray_control::next, jvirt_barray_control::pre_zero, jvirt_barray_control::rows_in_array, SIZEOF, and my_memory_mgr::virt_barray_list.

Referenced by jinit_memory_mgr().

00557 {
00558   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00559   jvirt_barray_ptr result;
00560 
00561   /* Only IMAGE-lifetime virtual arrays are currently supported */
00562   if (pool_id != JPOOL_IMAGE)
00563     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
00564 
00565   /* get control block */
00566   result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
00567                                           SIZEOF(struct jvirt_barray_control));
00568 
00569   result->mem_buffer = NULL;    /* marks array not yet realized */
00570   result->rows_in_array = numrows;
00571   result->blocksperrow = blocksperrow;
00572   result->maxaccess = maxaccess;
00573   result->pre_zero = pre_zero;
00574   result->b_s_open = FALSE;     /* no associated backing-store object */
00575   result->next = mem->virt_barray_list; /* add to list of virtual arrays */
00576   mem->virt_barray_list = result;
00577 
00578   return result;
00579 }

request_virt_sarray j_common_ptr    cinfo,
int    pool_id,
boolean    pre_zero,
JDIMENSION    samplesperrow,
JDIMENSION    numrows,
JDIMENSION    maxaccess
 

Definition at line 523 of file jmemmgr.c.

References alloc_small(), jvirt_sarray_control::b_s_open, ERREXIT1, JDIMENSION, JPOOL_IMAGE, jvirt_sarray_control::maxaccess, jvirt_sarray_control::mem_buffer, jvirt_sarray_control::next, jvirt_sarray_control::pre_zero, jvirt_sarray_control::rows_in_array, jvirt_sarray_control::samplesperrow, SIZEOF, and my_memory_mgr::virt_sarray_list.

Referenced by jinit_memory_mgr().

00527 {
00528   my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
00529   jvirt_sarray_ptr result;
00530 
00531   /* Only IMAGE-lifetime virtual arrays are currently supported */
00532   if (pool_id != JPOOL_IMAGE)
00533     ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
00534 
00535   /* get control block */
00536   result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
00537                                           SIZEOF(struct jvirt_sarray_control));
00538 
00539   result->mem_buffer = NULL;    /* marks array not yet realized */
00540   result->rows_in_array = numrows;
00541   result->samplesperrow = samplesperrow;
00542   result->maxaccess = maxaccess;
00543   result->pre_zero = pre_zero;
00544   result->b_s_open = FALSE;     /* no associated backing-store object */
00545   result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
00546   mem->virt_sarray_list = result;
00547 
00548   return result;
00549 }

self_destruct j_common_ptr    cinfo
 

Definition at line 1002 of file jmemmgr.c.

References free_pool(), jpeg_free_small(), jpeg_mem_term(), JPOOL_NUMPOOLS, JPOOL_PERMANENT, and SIZEOF.

Referenced by jinit_memory_mgr().

01003 {
01004   int pool;
01005 
01006   /* Close all backing store, release all memory.
01007    * Releasing pools in reverse order might help avoid fragmentation
01008    * with some (brain-damaged) malloc libraries.
01009    */
01010   for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
01011     free_pool(cinfo, pool);
01012   }
01013 
01014   /* Release the memory manager control block too. */
01015   jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr));
01016   cinfo->mem = NULL;            /* ensures I will be called only once */
01017 
01018   jpeg_mem_term(cinfo);         /* system-dependent cleanup */
01019 }

Variable Documentation

const size_t extra_pool_slop[JPOOL_NUMPOOLS] [static]
 

Initial value:

 
{
        0,                      
        5000                    
}

Definition at line 247 of file jmemmgr.c.

Referenced by alloc_small().

const size_t first_pool_slop[JPOOL_NUMPOOLS] [static]
 

Initial value:

 
{
        1600,                   
        16000                   
}

Definition at line 241 of file jmemmgr.c.

Referenced by alloc_small().

 

Powered by Plone

This site conforms to the following standards: