27 #define JPEG_INTERNALS 28 #define AM_MEMORY_MANAGER 35 extern char * getenv
JPP((
const char *
name));
73 #define ALIGN_TYPE double 91 typedef union small_pool_struct {
102 typedef union large_pool_struct {
132 long total_space_allocated;
150 struct jvirt_sarray_control {
166 struct jvirt_barray_control {
189 small_pool_ptr shdr_ptr;
190 large_pool_ptr lhdr_ptr;
196 fprintf(stderr,
"Freeing pool %d, total space = %ld\n",
197 pool_id, mem->total_space_allocated);
199 for (lhdr_ptr = mem->large_list[pool_id]; lhdr_ptr !=
NULL;
200 lhdr_ptr = lhdr_ptr->hdr.next) {
201 fprintf(stderr,
" Large chunk used %ld\n",
202 (
long) lhdr_ptr->hdr.bytes_used);
205 for (shdr_ptr = mem->small_list[pool_id]; shdr_ptr !=
NULL;
206 shdr_ptr = shdr_ptr->hdr.next) {
207 fprintf(stderr,
" Small chunk used %ld free %ld\n",
208 (
long) shdr_ptr->hdr.bytes_used,
209 (
long) shdr_ptr->hdr.bytes_left);
222 cinfo->err->trace_level = 2;
224 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, which);
263 size_t odd_bytes, min_request, slop;
276 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);
278 hdr_ptr = mem->small_list[pool_id];
279 while (hdr_ptr !=
NULL) {
280 if (hdr_ptr->hdr.bytes_left >= sizeofobject)
282 prev_hdr_ptr = hdr_ptr;
283 hdr_ptr = hdr_ptr->hdr.next;
287 if (hdr_ptr ==
NULL) {
290 if (prev_hdr_ptr ==
NULL)
291 slop = first_pool_slop[pool_id];
293 slop = extra_pool_slop[pool_id];
306 mem->total_space_allocated += min_request + slop;
308 hdr_ptr->hdr.next =
NULL;
309 hdr_ptr->hdr.bytes_used = 0;
310 hdr_ptr->hdr.bytes_left = sizeofobject + slop;
311 if (prev_hdr_ptr ==
NULL)
312 mem->small_list[pool_id] = hdr_ptr;
314 prev_hdr_ptr->hdr.next = hdr_ptr;
318 data_ptr = (
char *) (hdr_ptr + 1);
319 data_ptr += hdr_ptr->hdr.bytes_used;
323 return (
void *) data_ptr;
360 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);
369 hdr_ptr->hdr.next = mem->large_list[pool_id];
374 hdr_ptr->hdr.bytes_left = 0;
375 mem->large_list[pool_id] = hdr_ptr;
377 return (
void FAR *) (hdr_ptr + 1);
409 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
410 if (ltemp < (
long) numrows)
413 rowsperchunk = numrows;
414 mem->last_rowsperchunk = rowsperchunk;
422 while (currow < numrows) {
423 rowsperchunk =
MIN(rowsperchunk, numrows - currow);
425 (
size_t) ((size_t) rowsperchunk * (
size_t) samplesperrow
427 for (
i = rowsperchunk;
i > 0;
i--) {
428 result[currow++] = workspace;
429 workspace += samplesperrow;
457 ERREXIT(cinfo, JERR_WIDTH_OVERFLOW);
458 if (ltemp < (
long) numrows)
461 rowsperchunk = numrows;
462 mem->last_rowsperchunk = rowsperchunk;
470 while (currow < numrows) {
471 rowsperchunk =
MIN(rowsperchunk, numrows - currow);
473 (
size_t) ((size_t) rowsperchunk * (
size_t) blocksperrow
475 for (
i = rowsperchunk;
i > 0;
i--) {
476 result[currow++] = workspace;
477 workspace += blocksperrow;
533 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);
537 SIZEOF(
struct jvirt_sarray_control));
540 result->rows_in_array = numrows;
541 result->samplesperrow = samplesperrow;
542 result->maxaccess = maxaccess;
543 result->pre_zero = pre_zero;
545 result->next = mem->virt_sarray_list;
546 mem->virt_sarray_list =
result;
563 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);
567 SIZEOF(
struct jvirt_barray_control));
570 result->rows_in_array = numrows;
571 result->blocksperrow = blocksperrow;
572 result->maxaccess = maxaccess;
573 result->pre_zero = pre_zero;
575 result->next = mem->virt_barray_list;
576 mem->virt_barray_list =
result;
587 long space_per_minheight, maximum_space, avail_mem;
588 long minheights, max_minheights;
596 space_per_minheight = 0;
598 for (sptr = mem->virt_sarray_list; sptr !=
NULL; sptr = sptr->next) {
599 if (sptr->mem_buffer ==
NULL) {
600 space_per_minheight += (
long) sptr->maxaccess *
602 maximum_space += (
long) sptr->rows_in_array *
606 for (bptr = mem->virt_barray_list; bptr !=
NULL; bptr = bptr->next) {
607 if (bptr->mem_buffer ==
NULL) {
608 space_per_minheight += (
long) bptr->maxaccess *
610 maximum_space += (
long) bptr->rows_in_array *
615 if (space_per_minheight <= 0)
620 mem->total_space_allocated);
626 if (avail_mem >= maximum_space)
627 max_minheights = 1000000000L;
629 max_minheights = avail_mem / space_per_minheight;
633 if (max_minheights <= 0)
639 for (sptr = mem->virt_sarray_list; sptr !=
NULL; sptr = sptr->next) {
640 if (sptr->mem_buffer ==
NULL) {
641 minheights = ((
long) sptr->rows_in_array - 1L) / sptr->maxaccess + 1L;
642 if (minheights <= max_minheights) {
644 sptr->rows_in_mem = sptr->rows_in_array;
647 sptr->rows_in_mem = (
JDIMENSION) (max_minheights * sptr->maxaccess);
649 (
long) sptr->rows_in_array *
650 (
long) sptr->samplesperrow *
652 sptr->b_s_open =
TRUE;
655 sptr->samplesperrow, sptr->rows_in_mem);
656 sptr->rowsperchunk = mem->last_rowsperchunk;
657 sptr->cur_start_row = 0;
658 sptr->first_undef_row = 0;
663 for (bptr = mem->virt_barray_list; bptr !=
NULL; bptr = bptr->next) {
664 if (bptr->mem_buffer ==
NULL) {
665 minheights = ((
long) bptr->rows_in_array - 1L) / bptr->maxaccess + 1L;
666 if (minheights <= max_minheights) {
668 bptr->rows_in_mem = bptr->rows_in_array;
671 bptr->rows_in_mem = (
JDIMENSION) (max_minheights * bptr->maxaccess);
673 (
long) bptr->rows_in_array *
674 (
long) bptr->blocksperrow *
676 bptr->b_s_open =
TRUE;
679 bptr->blocksperrow, bptr->rows_in_mem);
680 bptr->rowsperchunk = mem->last_rowsperchunk;
681 bptr->cur_start_row = 0;
682 bptr->first_undef_row = 0;
693 long bytesperrow, file_offset, byte_count, rows, thisrow,
i;
696 file_offset =
ptr->cur_start_row * bytesperrow;
698 for (
i = 0;
i < (
long)
ptr->rows_in_mem;
i +=
ptr->rowsperchunk) {
700 rows =
MIN((
long)
ptr->rowsperchunk, (
long)
ptr->rows_in_mem -
i);
702 thisrow = (
long)
ptr->cur_start_row +
i;
703 rows =
MIN(rows, (
long)
ptr->first_undef_row - thisrow);
705 rows =
MIN(rows, (
long)
ptr->rows_in_array - thisrow);
708 byte_count = rows * bytesperrow;
710 (*
ptr->b_s_info.write_backing_store) (cinfo, &
ptr->b_s_info,
711 (
void FAR *)
ptr->mem_buffer[
i],
712 file_offset, byte_count);
714 (*
ptr->b_s_info.read_backing_store) (cinfo, &
ptr->b_s_info,
715 (
void FAR *)
ptr->mem_buffer[
i],
716 file_offset, byte_count);
717 file_offset += byte_count;
726 long bytesperrow, file_offset, byte_count, rows, thisrow,
i;
729 file_offset =
ptr->cur_start_row * bytesperrow;
731 for (
i = 0;
i < (
long)
ptr->rows_in_mem;
i +=
ptr->rowsperchunk) {
733 rows =
MIN((
long)
ptr->rowsperchunk, (
long)
ptr->rows_in_mem -
i);
735 thisrow = (
long)
ptr->cur_start_row +
i;
736 rows =
MIN(rows, (
long)
ptr->first_undef_row - thisrow);
738 rows =
MIN(rows, (
long)
ptr->rows_in_array - thisrow);
741 byte_count = rows * bytesperrow;
743 (*
ptr->b_s_info.write_backing_store) (cinfo, &
ptr->b_s_info,
744 (
void FAR *)
ptr->mem_buffer[
i],
745 file_offset, byte_count);
747 (*
ptr->b_s_info.read_backing_store) (cinfo, &
ptr->b_s_info,
748 (
void FAR *)
ptr->mem_buffer[
i],
749 file_offset, byte_count);
750 file_offset += byte_count;
769 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
772 if (start_row < ptr->cur_start_row ||
773 end_row >
ptr->cur_start_row+
ptr->rows_in_mem) {
775 ERREXIT(cinfo, JERR_VIRTUAL_BUG);
788 if (start_row >
ptr->cur_start_row) {
789 ptr->cur_start_row = start_row;
794 ltemp = (
long) end_row - (
long)
ptr->rows_in_mem;
809 if (
ptr->first_undef_row < end_row) {
810 if (
ptr->first_undef_row < start_row) {
812 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
813 undef_row = start_row;
815 undef_row =
ptr->first_undef_row;
818 ptr->first_undef_row = end_row;
821 undef_row -=
ptr->cur_start_row;
822 end_row -=
ptr->cur_start_row;
823 while (undef_row < end_row) {
829 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
836 return ptr->mem_buffer + (start_row -
ptr->cur_start_row);
854 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
857 if (start_row < ptr->cur_start_row ||
858 end_row >
ptr->cur_start_row+
ptr->rows_in_mem) {
860 ERREXIT(cinfo, JERR_VIRTUAL_BUG);
873 if (start_row >
ptr->cur_start_row) {
874 ptr->cur_start_row = start_row;
879 ltemp = (
long) end_row - (
long)
ptr->rows_in_mem;
894 if (
ptr->first_undef_row < end_row) {
895 if (
ptr->first_undef_row < start_row) {
897 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
898 undef_row = start_row;
900 undef_row =
ptr->first_undef_row;
903 ptr->first_undef_row = end_row;
906 undef_row -=
ptr->cur_start_row;
907 end_row -=
ptr->cur_start_row;
908 while (undef_row < end_row) {
914 ERREXIT(cinfo, JERR_BAD_VIRTUAL_ACCESS);
921 return ptr->mem_buffer + (start_row -
ptr->cur_start_row);
938 ERREXIT1(cinfo, JERR_BAD_POOL_ID, pool_id);
941 if (cinfo->err->trace_level > 1)
942 print_mem_stats(cinfo, pool_id);
950 for (sptr = mem->virt_sarray_list; sptr !=
NULL; sptr = sptr->next) {
951 if (sptr->b_s_open) {
952 sptr->b_s_open =
FALSE;
953 (*sptr->b_s_info.close_backing_store) (cinfo, & sptr->b_s_info);
956 mem->virt_sarray_list =
NULL;
957 for (bptr = mem->virt_barray_list; bptr !=
NULL; bptr = bptr->next) {
958 if (bptr->b_s_open) {
959 bptr->b_s_open =
FALSE;
960 (*bptr->b_s_info.close_backing_store) (cinfo, & bptr->b_s_info);
963 mem->virt_barray_list =
NULL;
967 lhdr_ptr = mem->large_list[pool_id];
968 mem->large_list[pool_id] =
NULL;
970 while (lhdr_ptr !=
NULL) {
972 space_freed = lhdr_ptr->hdr.bytes_used +
973 lhdr_ptr->hdr.bytes_left +
976 mem->total_space_allocated -= space_freed;
977 lhdr_ptr = next_lhdr_ptr;
981 shdr_ptr = mem->small_list[pool_id];
982 mem->small_list[pool_id] =
NULL;
984 while (shdr_ptr !=
NULL) {
986 space_freed = shdr_ptr->hdr.bytes_used +
987 shdr_ptr->hdr.bytes_left +
990 mem->total_space_allocated -= space_freed;
991 shdr_ptr = next_shdr_ptr;
1045 ERREXIT(cinfo, JERR_BAD_ALIGN_TYPE);
1054 ERREXIT(cinfo, JERR_BAD_ALLOC_CHUNK);
1063 ERREXIT1(cinfo, JERR_OUT_OF_MEMORY, 0);
1083 mem->pub.max_memory_to_use = max_to_use;
1086 mem->small_list[pool] =
NULL;
1087 mem->large_list[pool] =
NULL;
1089 mem->virt_sarray_list =
NULL;
1090 mem->virt_barray_list =
NULL;
1092 mem->total_space_allocated =
SIZEOF(my_memory_mgr);
1095 cinfo->mem = & mem->pub;
1106 if ((memenv = getenv(
"JPEGMEM")) !=
NULL) {
1109 if (sscanf(memenv,
"%ld%c", &max_to_use, &ch) > 0) {
1110 if (ch ==
'm' || ch ==
'M')
1111 max_to_use *= 1000L;
1112 mem->pub.max_memory_to_use = max_to_use * 1000L;
request_virt_barray(j_common_ptr cinfo, int pool_id, boolean pre_zero, JDIMENSION blocksperrow, JDIMENSION numrows, JDIMENSION maxaccess)
for(n=1;n< outline->n_points;n++)
jpeg_get_small(j_common_ptr cinfo, size_t sizeofobject)
jpeg_get_large(j_common_ptr cinfo, size_t sizeofobject)
alloc_small(j_common_ptr cinfo, int pool_id, size_t sizeofobject)
alloc_sarray(j_common_ptr cinfo, int pool_id, JDIMENSION samplesperrow, JDIMENSION numrows)
jpeg_mem_term(j_common_ptr cinfo)
do_barray_io(j_common_ptr cinfo, jvirt_barray_ptr ptr, boolean writing)
out_of_memory(j_common_ptr cinfo, int which)
#define ERREXIT(cinfo, code)
my_memory_mgr * my_mem_ptr
alloc_large(j_common_ptr cinfo, int pool_id, size_t sizeofobject)
free_pool(j_common_ptr cinfo, int pool_id)
self_destruct(j_common_ptr cinfo)
union large_pool_struct large_pool_hdr
union large_pool_struct FAR * large_pool_ptr
jpeg_open_backing_store(j_common_ptr cinfo, backing_store_ptr info, long total_bytes_needed)
do_sarray_io(j_common_ptr cinfo, jvirt_sarray_ptr ptr, boolean writing)
union small_pool_struct * small_pool_ptr
int JSAMPARRAY int int num_rows
union small_pool_struct small_pool_hdr
jpeg_free_large(j_common_ptr cinfo, void FAR *object, size_t sizeofobject)
jinit_memory_mgr(j_common_ptr cinfo)
typedef long(ZCALLBACK *tell_file_func) OF((voidpf opaque
jzero_far(void FAR *target, size_t bytestozero)
access_virt_sarray(j_common_ptr cinfo, jvirt_sarray_ptr ptr, JDIMENSION start_row, JDIMENSION num_rows, boolean writable)
#define ERREXIT1(cinfo, code, p1)
jpeg_mem_available(j_common_ptr cinfo, long min_bytes_needed, long max_bytes_needed, long already_allocated)
struct jvirt_barray_control * jvirt_barray_ptr
GLuint const GLchar * name
if(!abbox) return FT_THROW(Invalid_Argument)
jpeg_mem_init(j_common_ptr cinfo)
jpeg_free_small(j_common_ptr cinfo, void *object, size_t sizeofobject)
realize_virt_arrays(j_common_ptr cinfo)
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)
struct jvirt_sarray_control * jvirt_sarray_ptr
access_virt_barray(j_common_ptr cinfo, jvirt_barray_ptr ptr, JDIMENSION start_row, JDIMENSION num_rows, boolean writable)