7 * Copyright (C) 1991-1995, Thomas G. Lane.
\r
9 * This file is part of the Independent JPEG Group's software.
\r
11 * For conditions of distribution and use, see the accompanying README file.
\r
15 * This file contains the JPEG system-independent memory management
\r
17 * routines. This code is usable across a wide variety of machines; most
\r
19 * of the system dependencies have been isolated in a separate file.
\r
21 * The major functions provided here are:
\r
23 * * pool-based allocation and freeing of memory;
\r
25 * * policy decisions about how to divide available memory among the
\r
29 * * control logic for swapping virtual arrays between main memory and
\r
33 * The separate system-dependent file provides the actual backing-storage
\r
35 * access code, and it contains the policy decision about how much total
\r
37 * main memory to use.
\r
39 * This file is system-dependent in the sense that some of its functions
\r
41 * are unnecessary in some systems. For example, if there is enough virtual
\r
43 * memory so that backing storage will never be used, much of the virtual
\r
45 * array control logic could be removed. (Of course, if you have that much
\r
47 * memory then you shouldn't care about a little bit of unused code...)
\r
53 #define JPEG_INTERNALS
\r
55 #define AM_MEMORY_MANAGER /* we define jvirt_Xarray_control structs */
\r
57 #include "jinclude.h"
\r
59 #include "radiant_jpeglib.h"
\r
61 #include "jmemsys.h" /* import the system-dependent declarations */
\r
67 #ifndef HAVE_STDLIB_H /* <stdlib.h> should declare getenv() */
\r
69 extern char * getenv JPP((const char * name));
\r
81 * Some important notes:
\r
83 * The allocation routines provided here must never return NULL.
\r
85 * They should exit to error_exit if unsuccessful.
\r
89 * It's not a good idea to try to merge the sarray and barray routines,
\r
91 * even though they are textually almost the same, because samples are
\r
93 * usually stored as bytes while coefficients are shorts or ints. Thus,
\r
95 * in machines where byte pointers have a different representation from
\r
97 * word pointers, the resulting machine code could not be the same.
\r
107 * Many machines require storage alignment: longs must start on 4-byte
\r
109 * boundaries, doubles on 8-byte boundaries, etc. On such machines, malloc()
\r
111 * always returns pointers that are multiples of the worst-case alignment
\r
113 * requirement, and we had better do so too.
\r
115 * There isn't any really portable way to determine the worst-case alignment
\r
117 * requirement. This module assumes that the alignment requirement is
\r
119 * multiples of sizeof(ALIGN_TYPE).
\r
121 * By default, we define ALIGN_TYPE as double. This is necessary on some
\r
123 * workstations (where doubles really do need 8-byte alignment) and will work
\r
125 * fine on nearly everything. If your machine has lesser alignment needs,
\r
127 * you can save a few bytes by making ALIGN_TYPE smaller.
\r
129 * The only place I know of where this will NOT work is certain Macintosh
\r
131 * 680x0 compilers that define double as a 10-byte IEEE extended float.
\r
133 * Doing 10-byte alignment is counterproductive because longwords won't be
\r
135 * aligned well. Put "#define ALIGN_TYPE long" in jconfig.h if you have
\r
143 #ifndef ALIGN_TYPE /* so can override from jconfig.h */
\r
145 #define ALIGN_TYPE double
\r
155 * We allocate objects from "pools", where each pool is gotten with a single
\r
157 * request to jpeg_get_small() or jpeg_get_large(). There is no per-object
\r
159 * overhead within a pool, except for alignment padding. Each pool has a
\r
161 * header with a link to the next pool of the same class.
\r
163 * Small and large pool headers are identical except that the latter's
\r
165 * link pointer must be FAR on 80x86 machines.
\r
167 * Notice that the "real" header fields are union'ed with a dummy ALIGN_TYPE
\r
169 * field. This forces the compiler to make SIZEOF(small_pool_hdr) a multiple
\r
171 * of the alignment requirement of ALIGN_TYPE.
\r
177 typedef union small_pool_struct * small_pool_ptr;
\r
181 typedef union small_pool_struct {
\r
185 small_pool_ptr next; /* next in list of pools */
\r
187 size_t bytes_used; /* how many bytes already used within pool */
\r
189 size_t bytes_left; /* bytes still available in this pool */
\r
193 ALIGN_TYPE dummy; /* included in union to ensure alignment */
\r
199 typedef union large_pool_struct FAR * large_pool_ptr;
\r
203 typedef union large_pool_struct {
\r
207 large_pool_ptr next; /* next in list of pools */
\r
209 size_t bytes_used; /* how many bytes already used within pool */
\r
211 size_t bytes_left; /* bytes still available in this pool */
\r
215 ALIGN_TYPE dummy; /* included in union to ensure alignment */
\r
225 * Here is the full definition of a memory manager object.
\r
233 struct jpeg_memory_mgr pub; /* public fields */
\r
237 /* Each pool identifier (lifetime class) names a linked list of pools. */
\r
239 small_pool_ptr small_list[JPOOL_NUMPOOLS];
\r
241 large_pool_ptr large_list[JPOOL_NUMPOOLS];
\r
245 /* Since we only have one lifetime class of virtual arrays, only one
\r
247 * linked list is necessary (for each datatype). Note that the virtual
\r
249 * array control blocks being linked together are actually stored somewhere
\r
251 * in the small-pool list.
\r
255 jvirt_sarray_ptr virt_sarray_list;
\r
257 jvirt_barray_ptr virt_barray_list;
\r
261 /* This counts total space obtained from jpeg_get_small/large */
\r
263 long total_space_allocated;
\r
267 /* alloc_sarray and alloc_barray set this value for use by virtual
\r
273 JDIMENSION last_rowsperchunk; /* from most recent alloc_sarray/barray */
\r
279 typedef my_memory_mgr * my_mem_ptr;
\r
287 * The control blocks for virtual arrays.
\r
289 * Note that these blocks are allocated in the "small" pool area.
\r
291 * System-dependent info for the associated backing store (if any) is hidden
\r
293 * inside the backing_store_info struct.
\r
299 struct jvirt_sarray_control {
\r
301 JSAMPARRAY mem_buffer; /* => the in-memory buffer */
\r
303 JDIMENSION rows_in_array; /* total virtual array height */
\r
305 JDIMENSION samplesperrow; /* width of array (and of memory buffer) */
\r
307 JDIMENSION maxaccess; /* max rows accessed by access_virt_sarray */
\r
309 JDIMENSION rows_in_mem; /* height of memory buffer */
\r
311 JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
\r
313 JDIMENSION cur_start_row; /* first logical row # in the buffer */
\r
315 JDIMENSION first_undef_row; /* row # of first uninitialized row */
\r
317 boolean pre_zero; /* pre-zero mode requested? */
\r
319 boolean dirty; /* do current buffer contents need written? */
\r
321 boolean b_s_open; /* is backing-store data valid? */
\r
323 jvirt_sarray_ptr next; /* link to next virtual sarray control block */
\r
325 backing_store_info b_s_info; /* System-dependent control info */
\r
331 struct jvirt_barray_control {
\r
333 JBLOCKARRAY mem_buffer; /* => the in-memory buffer */
\r
335 JDIMENSION rows_in_array; /* total virtual array height */
\r
337 JDIMENSION blocksperrow; /* width of array (and of memory buffer) */
\r
339 JDIMENSION maxaccess; /* max rows accessed by access_virt_barray */
\r
341 JDIMENSION rows_in_mem; /* height of memory buffer */
\r
343 JDIMENSION rowsperchunk; /* allocation chunk size in mem_buffer */
\r
345 JDIMENSION cur_start_row; /* first logical row # in the buffer */
\r
347 JDIMENSION first_undef_row; /* row # of first uninitialized row */
\r
349 boolean pre_zero; /* pre-zero mode requested? */
\r
351 boolean dirty; /* do current buffer contents need written? */
\r
353 boolean b_s_open; /* is backing-store data valid? */
\r
355 jvirt_barray_ptr next; /* link to next virtual barray control block */
\r
357 backing_store_info b_s_info; /* System-dependent control info */
\r
365 #ifdef MEM_STATS /* optional extra stuff for statistics */
\r
371 print_mem_stats (j_common_ptr cinfo, int pool_id)
\r
375 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
\r
377 small_pool_ptr shdr_ptr;
\r
379 large_pool_ptr lhdr_ptr;
\r
383 /* Since this is only a debugging stub, we can cheat a little by using
\r
385 * fprintf directly rather than going through the trace message code.
\r
387 * This is helpful because message parm array can't handle longs.
\r
391 fprintf(stderr, "Freeing pool %d, total space = %ld\n",
\r
393 pool_id, mem->total_space_allocated);
\r
397 for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr != NULL;
\r
399 lhdr_ptr = lhdr_ptr->hdr.next) {
\r
401 fprintf(stderr, " Large chunk used %ld\n",
\r
403 (long) lhdr_ptr->hdr.bytes_used);
\r
409 for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr != NULL;
\r
411 shdr_ptr = shdr_ptr->hdr.next) {
\r
413 fprintf(stderr, " Small chunk used %ld free %ld\n",
\r
415 (long) shdr_ptr->hdr.bytes_used,
\r
417 (long) shdr_ptr->hdr.bytes_left);
\r
425 #endif /* MEM_STATS */
\r
433 out_of_memory (j_common_ptr cinfo, int which)
\r
435 /* Report an out-of-memory error and stop execution */
\r
437 /* If we compiled MEM_STATS support, report alloc requests before dying */
\r
443 cinfo->err->trace_level = 2; /* force self_destruct to report stats */
\r
447 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
\r
457 * Allocation of "small" objects.
\r
461 * For these, we use pooled storage. When a new pool must be created,
\r
463 * we try to get enough space for the current request plus a "slop" factor,
\r
465 * where the slop will be the amount of leftover space in the new pool.
\r
467 * The speed vs. space tradeoff is largely determined by the slop values.
\r
469 * A different slop value is provided for each pool class (lifetime),
\r
471 * and we also distinguish the first pool of a class from later ones.
\r
473 * NOTE: the values given work fairly well on both 16- and 32-bit-int
\r
475 * machines, but may be too small if longs are 64 bits or more.
\r
481 static const size_t first_pool_slop[JPOOL_NUMPOOLS] =
\r
485 1600, /* first PERMANENT pool */
\r
487 16000 /* first IMAGE pool */
\r
493 static const size_t extra_pool_slop[JPOOL_NUMPOOLS] =
\r
497 0, /* additional PERMANENT pools */
\r
499 5000 /* additional IMAGE pools */
\r
505 #define MIN_SLOP 50 /* greater than 0 to avoid futile looping */
\r
513 alloc_small (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
\r
515 /* Allocate a "small" object */
\r
519 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
\r
521 small_pool_ptr hdr_ptr, prev_hdr_ptr;
\r
525 size_t odd_bytes, min_request, slop;
\r
529 /* Check for unsatisfiable request (do now to ensure no overflow below) */
\r
531 if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(small_pool_hdr)))
\r
533 out_of_memory(cinfo, 1); /* request exceeds malloc's ability */
\r
537 /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
\r
539 odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
\r
543 sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
\r
547 /* See if space is available in any existing pool */
\r
549 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
\r
551 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
\r
553 prev_hdr_ptr = NULL;
\r
555 hdr_ptr = mem->small_list[pool_id];
\r
557 while (hdr_ptr != NULL) {
\r
559 if (hdr_ptr->hdr.bytes_left >= sizeofobject)
\r
561 break; /* found pool with enough space */
\r
563 prev_hdr_ptr = hdr_ptr;
\r
565 hdr_ptr = hdr_ptr->hdr.next;
\r
571 /* Time to make a new pool? */
\r
573 if (hdr_ptr == NULL) {
\r
575 /* min_request is what we need now, slop is what will be leftover */
\r
577 min_request = sizeofobject + SIZEOF(small_pool_hdr);
\r
579 if (prev_hdr_ptr == NULL) /* first pool in class? */
\r
581 slop = first_pool_slop[pool_id];
\r
585 slop = extra_pool_slop[pool_id];
\r
587 /* Don't ask for more than MAX_ALLOC_CHUNK */
\r
589 if (slop > (size_t) (MAX_ALLOC_CHUNK-min_request))
\r
591 slop = (size_t) (MAX_ALLOC_CHUNK-min_request);
\r
593 /* Try to get space, if fail reduce slop and try again */
\r
597 hdr_ptr = (small_pool_ptr) jpeg_get_small(cinfo, min_request + slop);
\r
599 if (hdr_ptr != NULL)
\r
605 if (slop < MIN_SLOP) /* give up when it gets real small */
\r
607 out_of_memory(cinfo, 2); /* jpeg_get_small failed */
\r
611 mem->total_space_allocated += min_request + slop;
\r
613 /* Success, initialize the new pool header and add to end of list */
\r
615 hdr_ptr->hdr.next = NULL;
\r
617 hdr_ptr->hdr.bytes_used = 0;
\r
619 hdr_ptr->hdr.bytes_left = sizeofobject + slop;
\r
621 if (prev_hdr_ptr == NULL) /* first pool in class? */
\r
623 mem->small_list[pool_id] = hdr_ptr;
\r
627 prev_hdr_ptr->hdr.next = hdr_ptr;
\r
633 /* OK, allocate the object from the current pool */
\r
635 data_ptr = (char *) (hdr_ptr + 1); /* point to first data byte in pool */
\r
637 data_ptr += hdr_ptr->hdr.bytes_used; /* point to place for object */
\r
639 hdr_ptr->hdr.bytes_used += sizeofobject;
\r
641 hdr_ptr->hdr.bytes_left -= sizeofobject;
\r
645 return (void *) data_ptr;
\r
655 * Allocation of "large" objects.
\r
659 * The external semantics of these are the same as "small" objects,
\r
661 * except that FAR pointers are used on 80x86. However the pool
\r
663 * management heuristics are quite different. We assume that each
\r
665 * request is large enough that it may as well be passed directly to
\r
667 * jpeg_get_large; the pool management just links everything together
\r
669 * so that we can free it all on demand.
\r
671 * Note: the major use of "large" objects is in JSAMPARRAY and JBLOCKARRAY
\r
673 * structures. The routines that create these structures (see below)
\r
675 * deliberately bunch rows together to ensure a large request size.
\r
681 METHODDEF void FAR *
\r
683 alloc_large (j_common_ptr cinfo, int pool_id, size_t sizeofobject)
\r
685 /* Allocate a "large" object */
\r
689 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
\r
691 large_pool_ptr hdr_ptr;
\r
697 /* Check for unsatisfiable request (do now to ensure no overflow below) */
\r
699 if (sizeofobject > (size_t) (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)))
\r
701 out_of_memory(cinfo, 3); /* request exceeds malloc's ability */
\r
705 /* Round up the requested size to a multiple of SIZEOF(ALIGN_TYPE) */
\r
707 odd_bytes = sizeofobject % SIZEOF(ALIGN_TYPE);
\r
711 sizeofobject += SIZEOF(ALIGN_TYPE) - odd_bytes;
\r
715 /* Always make a new pool */
\r
717 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
\r
719 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
\r
723 hdr_ptr = (large_pool_ptr) jpeg_get_large(cinfo, sizeofobject +
\r
725 SIZEOF(large_pool_hdr));
\r
727 if (hdr_ptr == NULL)
\r
729 out_of_memory(cinfo, 4); /* jpeg_get_large failed */
\r
731 mem->total_space_allocated += sizeofobject + SIZEOF(large_pool_hdr);
\r
735 /* Success, initialize the new pool header and add to list */
\r
737 hdr_ptr->hdr.next = mem->large_list[pool_id];
\r
739 /* We maintain space counts in each pool header for statistical purposes,
\r
741 * even though they are not needed for allocation.
\r
745 hdr_ptr->hdr.bytes_used = sizeofobject;
\r
747 hdr_ptr->hdr.bytes_left = 0;
\r
749 mem->large_list[pool_id] = hdr_ptr;
\r
753 return (void FAR *) (hdr_ptr + 1); /* point to first data byte in pool */
\r
763 * Creation of 2-D sample arrays.
\r
765 * The pointers are in near heap, the samples themselves in FAR heap.
\r
769 * To minimize allocation overhead and to allow I/O of large contiguous
\r
771 * blocks, we allocate the sample rows in groups of as many rows as possible
\r
773 * without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.
\r
775 * NB: the virtual array control routines, later in this file, know about
\r
777 * this chunking of rows. The rowsperchunk value is left in the mem manager
\r
779 * object so that it can be saved away if this sarray is the workspace for
\r
787 METHODDEF JSAMPARRAY
\r
789 alloc_sarray (j_common_ptr cinfo, int pool_id,
\r
791 JDIMENSION samplesperrow, JDIMENSION numrows)
\r
793 /* Allocate a 2-D sample array */
\r
797 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
\r
801 JSAMPROW workspace;
\r
803 JDIMENSION rowsperchunk, currow, i;
\r
809 /* Calculate max # of rows allowed in one allocation chunk */
\r
811 ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
\r
813 ((long) samplesperrow * SIZEOF(JSAMPLE));
\r
817 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
\r
819 if (ltemp < (long) numrows)
\r
821 rowsperchunk = (JDIMENSION) ltemp;
\r
825 rowsperchunk = numrows;
\r
827 mem->last_rowsperchunk = rowsperchunk;
\r
831 /* Get space for row pointers (small object) */
\r
833 result = (JSAMPARRAY) alloc_small(cinfo, pool_id,
\r
835 (size_t) (numrows * SIZEOF(JSAMPROW)));
\r
839 /* Get the rows themselves (large objects) */
\r
843 while (currow < numrows) {
\r
845 rowsperchunk = MIN(rowsperchunk, numrows - currow);
\r
847 workspace = (JSAMPROW) alloc_large(cinfo, pool_id,
\r
849 (size_t) ((size_t) rowsperchunk * (size_t) samplesperrow
\r
851 * SIZEOF(JSAMPLE)));
\r
853 for (i = rowsperchunk; i > 0; i--) {
\r
855 result[currow++] = workspace;
\r
857 workspace += samplesperrow;
\r
875 * Creation of 2-D coefficient-block arrays.
\r
877 * This is essentially the same as the code for sample arrays, above.
\r
883 METHODDEF JBLOCKARRAY
\r
885 alloc_barray (j_common_ptr cinfo, int pool_id,
\r
887 JDIMENSION blocksperrow, JDIMENSION numrows)
\r
889 /* Allocate a 2-D coefficient-block array */
\r
893 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
\r
895 JBLOCKARRAY result;
\r
897 JBLOCKROW workspace;
\r
899 JDIMENSION rowsperchunk, currow, i;
\r
905 /* Calculate max # of rows allowed in one allocation chunk */
\r
907 ltemp = (MAX_ALLOC_CHUNK-SIZEOF(large_pool_hdr)) /
\r
909 ((long) blocksperrow * SIZEOF(JBLOCK));
\r
913 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
\r
915 if (ltemp < (long) numrows)
\r
917 rowsperchunk = (JDIMENSION) ltemp;
\r
921 rowsperchunk = numrows;
\r
923 mem->last_rowsperchunk = rowsperchunk;
\r
927 /* Get space for row pointers (small object) */
\r
929 result = (JBLOCKARRAY) alloc_small(cinfo, pool_id,
\r
931 (size_t) (numrows * SIZEOF(JBLOCKROW)));
\r
935 /* Get the rows themselves (large objects) */
\r
939 while (currow < numrows) {
\r
941 rowsperchunk = MIN(rowsperchunk, numrows - currow);
\r
943 workspace = (JBLOCKROW) alloc_large(cinfo, pool_id,
\r
945 (size_t) ((size_t) rowsperchunk * (size_t) blocksperrow
\r
947 * SIZEOF(JBLOCK)));
\r
949 for (i = rowsperchunk; i > 0; i--) {
\r
951 result[currow++] = workspace;
\r
953 workspace += blocksperrow;
\r
971 * About virtual array management:
\r
975 * The above "normal" array routines are only used to allocate strip buffers
\r
977 * (as wide as the image, but just a few rows high). Full-image-sized buffers
\r
979 * are handled as "virtual" arrays. The array is still accessed a strip at a
\r
981 * time, but the memory manager must save the whole array for repeated
\r
983 * accesses. The intended implementation is that there is a strip buffer in
\r
985 * memory (as high as is possible given the desired memory limit), plus a
\r
987 * backing file that holds the rest of the array.
\r
991 * The request_virt_array routines are told the total size of the image and
\r
993 * the maximum number of rows that will be accessed at once. The in-memory
\r
995 * buffer must be at least as large as the maxaccess value.
\r
999 * The request routines create control blocks but not the in-memory buffers.
\r
1001 * That is postponed until realize_virt_arrays is called. At that time the
\r
1003 * total amount of space needed is known (approximately, anyway), so free
\r
1005 * memory can be divided up fairly.
\r
1009 * The access_virt_array routines are responsible for making a specific strip
\r
1011 * area accessible (after reading or writing the backing file, if necessary).
\r
1013 * Note that the access routines are told whether the caller intends to modify
\r
1015 * the accessed strip; during a read-only pass this saves having to rewrite
\r
1017 * data to disk. The access routines are also responsible for pre-zeroing
\r
1019 * any newly accessed rows, if pre-zeroing was requested.
\r
1023 * In current usage, the access requests are usually for nonoverlapping
\r
1025 * strips; that is, successive access start_row numbers differ by exactly
\r
1027 * num_rows = maxaccess. This means we can get good performance with simple
\r
1029 * buffer dump/reload logic, by making the in-memory buffer be a multiple
\r
1031 * of the access height; then there will never be accesses across bufferload
\r
1033 * boundaries. The code will still work with overlapping access requests,
\r
1035 * but it doesn't handle bufferload overlaps very efficiently.
\r
1043 METHODDEF jvirt_sarray_ptr
\r
1045 request_virt_sarray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
\r
1047 JDIMENSION samplesperrow, JDIMENSION numrows,
\r
1049 JDIMENSION maxaccess)
\r
1051 /* Request a virtual 2-D sample array */
\r
1055 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
\r
1057 jvirt_sarray_ptr result;
\r
1061 /* Only IMAGE-lifetime virtual arrays are currently supported */
\r
1063 if (pool_id != JPOOL_IMAGE)
\r
1065 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
\r
1069 /* get control block */
\r
1071 result = (jvirt_sarray_ptr) alloc_small(cinfo, pool_id,
\r
1073 SIZEOF(struct jvirt_sarray_control));
\r
1077 result->mem_buffer = NULL; /* marks array not yet realized */
\r
1079 result->rows_in_array = numrows;
\r
1081 result->samplesperrow = samplesperrow;
\r
1083 result->maxaccess = maxaccess;
\r
1085 result->pre_zero = pre_zero;
\r
1087 result->b_s_open = FALSE; /* no associated backing-store object */
\r
1089 result->next = mem->virt_sarray_list; /* add to list of virtual arrays */
\r
1091 mem->virt_sarray_list = result;
\r
1103 METHODDEF jvirt_barray_ptr
\r
1105 request_virt_barray (j_common_ptr cinfo, int pool_id, boolean pre_zero,
\r
1107 JDIMENSION blocksperrow, JDIMENSION numrows,
\r
1109 JDIMENSION maxaccess)
\r
1111 /* Request a virtual 2-D coefficient-block array */
\r
1115 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
\r
1117 jvirt_barray_ptr result;
\r
1121 /* Only IMAGE-lifetime virtual arrays are currently supported */
\r
1123 if (pool_id != JPOOL_IMAGE)
\r
1125 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
\r
1129 /* get control block */
\r
1131 result = (jvirt_barray_ptr) alloc_small(cinfo, pool_id,
\r
1133 SIZEOF(struct jvirt_barray_control));
\r
1137 result->mem_buffer = NULL; /* marks array not yet realized */
\r
1139 result->rows_in_array = numrows;
\r
1141 result->blocksperrow = blocksperrow;
\r
1143 result->maxaccess = maxaccess;
\r
1145 result->pre_zero = pre_zero;
\r
1147 result->b_s_open = FALSE; /* no associated backing-store object */
\r
1149 result->next = mem->virt_barray_list; /* add to list of virtual arrays */
\r
1151 mem->virt_barray_list = result;
\r
1165 realize_virt_arrays (j_common_ptr cinfo)
\r
1167 /* Allocate the in-memory buffers for any unrealized virtual arrays */
\r
1171 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
\r
1173 long space_per_minheight, maximum_space, avail_mem;
\r
1175 long minheights, max_minheights;
\r
1177 jvirt_sarray_ptr sptr;
\r
1179 jvirt_barray_ptr bptr;
\r
1183 /* Compute the minimum space needed (maxaccess rows in each buffer)
\r
1185 * and the maximum space needed (full image height in each buffer).
\r
1187 * These may be of use to the system-dependent jpeg_mem_available routine.
\r
1191 space_per_minheight = 0;
\r
1193 maximum_space = 0;
\r
1195 for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
\r
1197 if (sptr->mem_buffer == NULL) { /* if not realized yet */
\r
1199 space_per_minheight += (long) sptr->maxaccess *
\r
1201 (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
\r
1203 maximum_space += (long) sptr->rows_in_array *
\r
1205 (long) sptr->samplesperrow * SIZEOF(JSAMPLE);
\r
1211 for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
\r
1213 if (bptr->mem_buffer == NULL) { /* if not realized yet */
\r
1215 space_per_minheight += (long) bptr->maxaccess *
\r
1217 (long) bptr->blocksperrow * SIZEOF(JBLOCK);
\r
1219 maximum_space += (long) bptr->rows_in_array *
\r
1221 (long) bptr->blocksperrow * SIZEOF(JBLOCK);
\r
1229 if (space_per_minheight <= 0)
\r
1231 return; /* no unrealized arrays, no work */
\r
1235 /* Determine amount of memory to actually use; this is system-dependent. */
\r
1237 avail_mem = jpeg_mem_available(cinfo, space_per_minheight, maximum_space,
\r
1239 mem->total_space_allocated);
\r
1243 /* If the maximum space needed is available, make all the buffers full
\r
1245 * height; otherwise parcel it out with the same number of minheights
\r
1251 if (avail_mem >= maximum_space)
\r
1253 max_minheights = 1000000000L;
\r
1257 max_minheights = avail_mem / space_per_minheight;
\r
1259 /* If there doesn't seem to be enough space, try to get the minimum
\r
1261 * anyway. This allows a "stub" implementation of jpeg_mem_available().
\r
1265 if (max_minheights <= 0)
\r
1267 max_minheights = 1;
\r
1273 /* Allocate the in-memory buffers and initialize backing store as needed. */
\r
1277 for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
\r
1279 if (sptr->mem_buffer == NULL) { /* if not realized yet */
\r
1281 minheights = ((long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
\r
1283 if (minheights <= max_minheights) {
\r
1285 /* This buffer fits in memory */
\r
1287 sptr->rows_in_mem = sptr->rows_in_array;
\r
1291 /* It doesn't fit in memory, create backing store. */
\r
1293 sptr->rows_in_mem = (JDIMENSION) (max_minheights * sptr->maxaccess);
\r
1295 jpeg_open_backing_store(cinfo, & sptr->b_s_info,
\r
1297 (long) sptr->rows_in_array *
\r
1299 (long) sptr->samplesperrow *
\r
1301 (long) SIZEOF(JSAMPLE));
\r
1303 sptr->b_s_open = TRUE;
\r
1307 sptr->mem_buffer = alloc_sarray(cinfo, JPOOL_IMAGE,
\r
1309 sptr->samplesperrow, sptr->rows_in_mem);
\r
1311 sptr->rowsperchunk = mem->last_rowsperchunk;
\r
1313 sptr->cur_start_row = 0;
\r
1315 sptr->first_undef_row = 0;
\r
1317 sptr->dirty = FALSE;
\r
1325 for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
\r
1327 if (bptr->mem_buffer == NULL) { /* if not realized yet */
\r
1329 minheights = ((long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
\r
1331 if (minheights <= max_minheights) {
\r
1333 /* This buffer fits in memory */
\r
1335 bptr->rows_in_mem = bptr->rows_in_array;
\r
1339 /* It doesn't fit in memory, create backing store. */
\r
1341 bptr->rows_in_mem = (JDIMENSION) (max_minheights * bptr->maxaccess);
\r
1343 jpeg_open_backing_store(cinfo, & bptr->b_s_info,
\r
1345 (long) bptr->rows_in_array *
\r
1347 (long) bptr->blocksperrow *
\r
1349 (long) SIZEOF(JBLOCK));
\r
1351 bptr->b_s_open = TRUE;
\r
1355 bptr->mem_buffer = alloc_barray(cinfo, JPOOL_IMAGE,
\r
1357 bptr->blocksperrow, bptr->rows_in_mem);
\r
1359 bptr->rowsperchunk = mem->last_rowsperchunk;
\r
1361 bptr->cur_start_row = 0;
\r
1363 bptr->first_undef_row = 0;
\r
1365 bptr->dirty = FALSE;
\r
1379 do_sarray_io (j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
\r
1381 /* Do backing store read or write of a virtual sample array */
\r
1385 long bytesperrow, file_offset, byte_count, rows, thisrow, i;
\r
1389 bytesperrow = (long) ptr->samplesperrow * SIZEOF(JSAMPLE);
\r
1391 file_offset = ptr->cur_start_row * bytesperrow;
\r
1393 /* Loop to read or write each allocation chunk in mem_buffer */
\r
1395 for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
\r
1397 /* One chunk, but check for short chunk at end of buffer */
\r
1399 rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
\r
1401 /* Transfer no more than is currently defined */
\r
1403 thisrow = (long) ptr->cur_start_row + i;
\r
1405 rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
\r
1407 /* Transfer no more than fits in file */
\r
1409 rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
\r
1411 if (rows <= 0) /* this chunk might be past end of file! */
\r
1415 byte_count = rows * bytesperrow;
\r
1419 (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
\r
1421 (void FAR *) ptr->mem_buffer[i],
\r
1423 file_offset, byte_count);
\r
1427 (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
\r
1429 (void FAR *) ptr->mem_buffer[i],
\r
1431 file_offset, byte_count);
\r
1433 file_offset += byte_count;
\r
1445 do_barray_io (j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
\r
1447 /* Do backing store read or write of a virtual coefficient-block array */
\r
1451 long bytesperrow, file_offset, byte_count, rows, thisrow, i;
\r
1455 bytesperrow = (long) ptr->blocksperrow * SIZEOF(JBLOCK);
\r
1457 file_offset = ptr->cur_start_row * bytesperrow;
\r
1459 /* Loop to read or write each allocation chunk in mem_buffer */
\r
1461 for (i = 0; i < (long) ptr->rows_in_mem; i += ptr->rowsperchunk) {
\r
1463 /* One chunk, but check for short chunk at end of buffer */
\r
1465 rows = MIN((long) ptr->rowsperchunk, (long) ptr->rows_in_mem - i);
\r
1467 /* Transfer no more than is currently defined */
\r
1469 thisrow = (long) ptr->cur_start_row + i;
\r
1471 rows = MIN(rows, (long) ptr->first_undef_row - thisrow);
\r
1473 /* Transfer no more than fits in file */
\r
1475 rows = MIN(rows, (long) ptr->rows_in_array - thisrow);
\r
1477 if (rows <= 0) /* this chunk might be past end of file! */
\r
1481 byte_count = rows * bytesperrow;
\r
1485 (*ptr->b_s_info.write_backing_store) (cinfo, & ptr->b_s_info,
\r
1487 (void FAR *) ptr->mem_buffer[i],
\r
1489 file_offset, byte_count);
\r
1493 (*ptr->b_s_info.read_backing_store) (cinfo, & ptr->b_s_info,
\r
1495 (void FAR *) ptr->mem_buffer[i],
\r
1497 file_offset, byte_count);
\r
1499 file_offset += byte_count;
\r
1509 METHODDEF JSAMPARRAY
\r
1511 access_virt_sarray (j_common_ptr cinfo, jvirt_sarray_ptr ptr,
\r
1513 JDIMENSION start_row, JDIMENSION num_rows,
\r
1517 /* Access the part of a virtual sample array starting at start_row */
\r
1519 /* and extending for num_rows rows. writable is true if */
\r
1521 /* caller intends to modify the accessed area. */
\r
1525 JDIMENSION end_row = start_row + num_rows;
\r
1527 JDIMENSION undef_row;
\r
1531 /* debugging check */
\r
1533 if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
\r
1535 ptr->mem_buffer == NULL)
\r
1537 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
\r
1541 /* Make the desired part of the virtual array accessible */
\r
1543 if (start_row < ptr->cur_start_row ||
\r
1545 end_row > ptr->cur_start_row+ptr->rows_in_mem) {
\r
1547 if (! ptr->b_s_open)
\r
1549 ERREXIT(cinfo, JERR_VIRTUAL_BUG);
\r
1551 /* Flush old buffer contents if necessary */
\r
1555 do_sarray_io(cinfo, ptr, TRUE);
\r
1557 ptr->dirty = FALSE;
\r
1561 /* Decide what part of virtual array to access.
\r
1563 * Algorithm: if target address > current window, assume forward scan,
\r
1565 * load starting at target address. If target address < current window,
\r
1567 * assume backward scan, load so that target area is top of window.
\r
1569 * Note that when switching from forward write to forward read, will have
\r
1571 * start_row = 0, so the limiting case applies and we load from 0 anyway.
\r
1575 if (start_row > ptr->cur_start_row) {
\r
1577 ptr->cur_start_row = start_row;
\r
1581 /* use long arithmetic here to avoid overflow & unsigned problems */
\r
1587 ltemp = (long) end_row - (long) ptr->rows_in_mem;
\r
1591 ltemp = 0; /* don't fall off front end of file */
\r
1593 ptr->cur_start_row = (JDIMENSION) ltemp;
\r
1597 /* Read in the selected part of the array.
\r
1599 * During the initial write pass, we will do no actual read
\r
1601 * because the selected part is all undefined.
\r
1605 do_sarray_io(cinfo, ptr, FALSE);
\r
1609 /* Ensure the accessed part of the array is defined; prezero if needed.
\r
1611 * To improve locality of access, we only prezero the part of the array
\r
1613 * that the caller is about to access, not the entire in-memory array.
\r
1617 if (ptr->first_undef_row < end_row) {
\r
1619 if (ptr->first_undef_row < start_row) {
\r
1621 if (writable) /* writer skipped over a section of array */
\r
1623 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
\r
1625 undef_row = start_row; /* but reader is allowed to read ahead */
\r
1629 undef_row = ptr->first_undef_row;
\r
1635 ptr->first_undef_row = end_row;
\r
1637 if (ptr->pre_zero) {
\r
1639 size_t bytesperrow = (size_t) ptr->samplesperrow * SIZEOF(JSAMPLE);
\r
1641 undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
\r
1643 end_row -= ptr->cur_start_row;
\r
1645 while (undef_row < end_row) {
\r
1647 jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
\r
1655 if (! writable) /* reader looking at undefined data */
\r
1657 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
\r
1663 /* Flag the buffer dirty if caller will write in it */
\r
1667 ptr->dirty = TRUE;
\r
1669 /* Return address of proper part of the buffer */
\r
1671 return ptr->mem_buffer + (start_row - ptr->cur_start_row);
\r
1679 METHODDEF JBLOCKARRAY
\r
1681 access_virt_barray (j_common_ptr cinfo, jvirt_barray_ptr ptr,
\r
1683 JDIMENSION start_row, JDIMENSION num_rows,
\r
1687 /* Access the part of a virtual block array starting at start_row */
\r
1689 /* and extending for num_rows rows. writable is true if */
\r
1691 /* caller intends to modify the accessed area. */
\r
1695 JDIMENSION end_row = start_row + num_rows;
\r
1697 JDIMENSION undef_row;
\r
1701 /* debugging check */
\r
1703 if (end_row > ptr->rows_in_array || num_rows > ptr->maxaccess ||
\r
1705 ptr->mem_buffer == NULL)
\r
1707 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
\r
1711 /* Make the desired part of the virtual array accessible */
\r
1713 if (start_row < ptr->cur_start_row ||
\r
1715 end_row > ptr->cur_start_row+ptr->rows_in_mem) {
\r
1717 if (! ptr->b_s_open)
\r
1719 ERREXIT(cinfo, JERR_VIRTUAL_BUG);
\r
1721 /* Flush old buffer contents if necessary */
\r
1725 do_barray_io(cinfo, ptr, TRUE);
\r
1727 ptr->dirty = FALSE;
\r
1731 /* Decide what part of virtual array to access.
\r
1733 * Algorithm: if target address > current window, assume forward scan,
\r
1735 * load starting at target address. If target address < current window,
\r
1737 * assume backward scan, load so that target area is top of window.
\r
1739 * Note that when switching from forward write to forward read, will have
\r
1741 * start_row = 0, so the limiting case applies and we load from 0 anyway.
\r
1745 if (start_row > ptr->cur_start_row) {
\r
1747 ptr->cur_start_row = start_row;
\r
1751 /* use long arithmetic here to avoid overflow & unsigned problems */
\r
1757 ltemp = (long) end_row - (long) ptr->rows_in_mem;
\r
1761 ltemp = 0; /* don't fall off front end of file */
\r
1763 ptr->cur_start_row = (JDIMENSION) ltemp;
\r
1767 /* Read in the selected part of the array.
\r
1769 * During the initial write pass, we will do no actual read
\r
1771 * because the selected part is all undefined.
\r
1775 do_barray_io(cinfo, ptr, FALSE);
\r
1779 /* Ensure the accessed part of the array is defined; prezero if needed.
\r
1781 * To improve locality of access, we only prezero the part of the array
\r
1783 * that the caller is about to access, not the entire in-memory array.
\r
1787 if (ptr->first_undef_row < end_row) {
\r
1789 if (ptr->first_undef_row < start_row) {
\r
1791 if (writable) /* writer skipped over a section of array */
\r
1793 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
\r
1795 undef_row = start_row; /* but reader is allowed to read ahead */
\r
1799 undef_row = ptr->first_undef_row;
\r
1805 ptr->first_undef_row = end_row;
\r
1807 if (ptr->pre_zero) {
\r
1809 size_t bytesperrow = (size_t) ptr->blocksperrow * SIZEOF(JBLOCK);
\r
1811 undef_row -= ptr->cur_start_row; /* make indexes relative to buffer */
\r
1813 end_row -= ptr->cur_start_row;
\r
1815 while (undef_row < end_row) {
\r
1817 jzero_far((void FAR *) ptr->mem_buffer[undef_row], bytesperrow);
\r
1825 if (! writable) /* reader looking at undefined data */
\r
1827 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
\r
1833 /* Flag the buffer dirty if caller will write in it */
\r
1837 ptr->dirty = TRUE;
\r
1839 /* Return address of proper part of the buffer */
\r
1841 return ptr->mem_buffer + (start_row - ptr->cur_start_row);
\r
1851 * Release all objects belonging to a specified pool.
\r
1859 free_pool (j_common_ptr cinfo, int pool_id)
\r
1863 my_mem_ptr mem = (my_mem_ptr) cinfo->mem;
\r
1865 small_pool_ptr shdr_ptr;
\r
1867 large_pool_ptr lhdr_ptr;
\r
1869 size_t space_freed;
\r
1873 if (pool_id < 0 || pool_id >= JPOOL_NUMPOOLS)
\r
1875 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id); /* safety check */
\r
1881 if (cinfo->err->trace_level > 1)
\r
1883 print_mem_stats(cinfo, pool_id); /* print pool's memory usage statistics */
\r
1889 /* If freeing IMAGE pool, close any virtual arrays first */
\r
1891 if (pool_id == JPOOL_IMAGE) {
\r
1893 jvirt_sarray_ptr sptr;
\r
1895 jvirt_barray_ptr bptr;
\r
1899 for (sptr = mem->virt_sarray_list; sptr != NULL; sptr = sptr->next) {
\r
1901 if (sptr->b_s_open) { /* there may be no backing store */
\r
1903 sptr->b_s_open = FALSE; /* prevent recursive close if error */
\r
1905 (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
\r
1911 mem->virt_sarray_list = NULL;
\r
1913 for (bptr = mem->virt_barray_list; bptr != NULL; bptr = bptr->next) {
\r
1915 if (bptr->b_s_open) { /* there may be no backing store */
\r
1917 bptr->b_s_open = FALSE; /* prevent recursive close if error */
\r
1919 (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
\r
1925 mem->virt_barray_list = NULL;
\r
1931 /* Release large objects */
\r
1933 lhdr_ptr = mem->large_list[pool_id];
\r
1935 mem->large_list[pool_id] = NULL;
\r
1939 while (lhdr_ptr != NULL) {
\r
1941 large_pool_ptr next_lhdr_ptr = lhdr_ptr->hdr.next;
\r
1943 space_freed = lhdr_ptr->hdr.bytes_used +
\r
1945 lhdr_ptr->hdr.bytes_left +
\r
1947 SIZEOF(large_pool_hdr);
\r
1949 jpeg_free_large(cinfo, (void FAR *) lhdr_ptr, space_freed);
\r
1951 mem->total_space_allocated -= space_freed;
\r
1953 lhdr_ptr = next_lhdr_ptr;
\r
1959 /* Release small objects */
\r
1961 shdr_ptr = mem->small_list[pool_id];
\r
1963 mem->small_list[pool_id] = NULL;
\r
1967 while (shdr_ptr != NULL) {
\r
1969 small_pool_ptr next_shdr_ptr = shdr_ptr->hdr.next;
\r
1971 space_freed = shdr_ptr->hdr.bytes_used +
\r
1973 shdr_ptr->hdr.bytes_left +
\r
1975 SIZEOF(small_pool_hdr);
\r
1977 jpeg_free_small(cinfo, (void *) shdr_ptr, space_freed);
\r
1979 mem->total_space_allocated -= space_freed;
\r
1981 shdr_ptr = next_shdr_ptr;
\r
1993 * Close up shop entirely.
\r
1995 * Note that this cannot be called unless cinfo->mem is non-NULL.
\r
2003 self_destruct (j_common_ptr cinfo)
\r
2011 /* Close all backing store, release all memory.
\r
2013 * Releasing pools in reverse order might help avoid fragmentation
\r
2015 * with some (brain-damaged) malloc libraries.
\r
2019 for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
\r
2021 free_pool(cinfo, pool);
\r
2027 /* Release the memory manager control block too. */
\r
2029 jpeg_free_small(cinfo, (void *) cinfo->mem, SIZEOF(my_memory_mgr));
\r
2031 cinfo->mem = NULL; /* ensures I will be called only once */
\r
2035 jpeg_mem_term(cinfo); /* system-dependent cleanup */
\r
2045 * Memory manager initialization.
\r
2047 * When this is called, only the error manager pointer is valid in cinfo!
\r
2055 jinit_memory_mgr (j_common_ptr cinfo)
\r
2069 cinfo->mem = NULL; /* for safety if init fails */
\r
2073 /* Check for configuration errors.
\r
2075 * SIZEOF(ALIGN_TYPE) should be a power of 2; otherwise, it probably
\r
2077 * doesn't reflect any real hardware alignment requirement.
\r
2079 * The test is a little tricky: for X>0, X and X-1 have no one-bits
\r
2081 * in common if and only if X is a power of 2, ie has only one one-bit.
\r
2083 * Some compilers may give an "unreachable code" warning here; ignore it.
\r
2087 if ((SIZEOF(ALIGN_TYPE) & (SIZEOF(ALIGN_TYPE)-1)) != 0)
\r
2089 ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
\r
2091 /* MAX_ALLOC_CHUNK must be representable as type size_t, and must be
\r
2093 * a multiple of SIZEOF(ALIGN_TYPE).
\r
2095 * Again, an "unreachable code" warning may be ignored here.
\r
2097 * But a "constant too large" warning means you need to fix MAX_ALLOC_CHUNK.
\r
2101 test_mac = (size_t) MAX_ALLOC_CHUNK;
\r
2103 if ((long) test_mac != MAX_ALLOC_CHUNK ||
\r
2105 (MAX_ALLOC_CHUNK % SIZEOF(ALIGN_TYPE)) != 0)
\r
2107 ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
\r
2111 max_to_use = jpeg_mem_init(cinfo); /* system-dependent initialization */
\r
2115 /* Attempt to allocate memory manager's control block */
\r
2117 mem = (my_mem_ptr) jpeg_get_small(cinfo, SIZEOF(my_memory_mgr));
\r
2121 if (mem == NULL) {
\r
2123 jpeg_mem_term(cinfo); /* system-dependent cleanup */
\r
2125 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
\r
2131 /* OK, fill in the method pointers */
\r
2133 mem->pub.alloc_small = alloc_small;
\r
2135 mem->pub.alloc_large = alloc_large;
\r
2137 mem->pub.alloc_sarray = alloc_sarray;
\r
2139 mem->pub.alloc_barray = alloc_barray;
\r
2141 mem->pub.request_virt_sarray = request_virt_sarray;
\r
2143 mem->pub.request_virt_barray = request_virt_barray;
\r
2145 mem->pub.realize_virt_arrays = realize_virt_arrays;
\r
2147 mem->pub.access_virt_sarray = access_virt_sarray;
\r
2149 mem->pub.access_virt_barray = access_virt_barray;
\r
2151 mem->pub.free_pool = free_pool;
\r
2153 mem->pub.self_destruct = self_destruct;
\r
2157 /* Initialize working state */
\r
2159 mem->pub.max_memory_to_use = max_to_use;
\r
2163 for (pool = JPOOL_NUMPOOLS-1; pool >= JPOOL_PERMANENT; pool--) {
\r
2165 mem->small_list[pool] = NULL;
\r
2167 mem->large_list[pool] = NULL;
\r
2171 mem->virt_sarray_list = NULL;
\r
2173 mem->virt_barray_list = NULL;
\r
2177 mem->total_space_allocated = SIZEOF(my_memory_mgr);
\r
2181 /* Declare ourselves open for business */
\r
2183 cinfo->mem = & mem->pub;
\r
2187 /* Check for an environment variable JPEGMEM; if found, override the
\r
2189 * default max_memory setting from jpeg_mem_init. Note that the
\r
2191 * surrounding application may again override this value.
\r
2193 * If your system doesn't support getenv(), define NO_GETENV to disable
\r
2205 if ((memenv = getenv("JPEGMEM")) != NULL) {
\r
2211 if (sscanf(memenv, "%ld%c", &max_to_use, &ch) > 0) {
\r
2213 if (ch == 'm' || ch == 'M')
\r
2215 max_to_use *= 1000L;
\r
2217 mem->pub.max_memory_to_use = max_to_use * 1000L;
\r