1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
|
/* $XConsortium: jmemmgr.c,v 1.3 94/01/12 20:20:03 rws Exp $ */
/* Module jmemmgr.c */
/****************************************************************************
Copyright (c) 1993, 1994 X Consortium
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of the X Consortium shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from the X Consortium.
NOTICE
This software is being provided by AGE Logic, Inc. under the
following license. By obtaining, using and/or copying this software,
you agree that you have read, understood, and will comply with these
terms and conditions:
Permission to use, copy, modify, distribute and sell this
software and its documentation for any purpose and without
fee or royalty and to grant others any or all rights granted
herein is hereby granted, provided that you agree to comply
with the following copyright notice and statements, including
the disclaimer, and that the same appears on all copies and
derivative works of the software and documentation you make.
"Copyright 1993, 1994 by AGE Logic, Inc."
THIS SOFTWARE IS PROVIDED "AS IS". AGE LOGIC MAKES NO
REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED. By way of
example, but not limitation, AGE LOGIC MAKE NO
REPRESENTATIONS OR WARRANTIES OF MERCHANTABILITY OR FITNESS
FOR ANY PARTICULAR PURPOSE OR THAT THE SOFTWARE DOES NOT
INFRINGE THIRD-PARTY PROPRIETARY RIGHTS. AGE LOGIC
SHALL BEAR NO LIABILITY FOR ANY USE OF THIS SOFTWARE. IN NO
EVENT SHALL EITHER PARTY BE LIABLE FOR ANY INDIRECT,
INCIDENTAL, SPECIAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOSS
OF PROFITS, REVENUE, DATA OR USE, INCURRED BY EITHER PARTY OR
ANY THIRD PARTY, WHETHER IN AN ACTION IN CONTRACT OR TORT OR
BASED ON A WARRANTY, EVEN IF AGE LOGIC LICENSEES
HEREUNDER HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
DAMAGES.
The name of AGE Logic, Inc. may not be used in
advertising or publicity pertaining to this software without
specific, written prior permission from AGE Logic.
Title to this software shall at all times remain with AGE
Logic, Inc.
*****************************************************************************
Gary Rogers, AGE Logic, Inc., October 1993
Gary Rogers, AGE Logic, Inc., January 1994
****************************************************************************/
/*
* jmemmgr.c
*
* Copyright (C) 1991, 1992, Thomas G. Lane.
* This file is part of the Independent JPEG Group's software.
* For conditions of distribution and use, see the accompanying README file.
*
* This file provides the standard system-independent memory management
* routines. This code is usable across a wide variety of machines; most
* of the system dependencies have been isolated in a separate file.
* The major functions provided here are:
* * bookkeeping to allow all allocated memory to be freed upon exit;
* * policy decisions about how to divide available memory among the
* various large arrays;
* * control logic for swapping virtual arrays between main memory and
* backing storage.
* The separate system-dependent file provides the actual backing-storage
* access code, and it contains the policy decision about how much total
* main memory to use.
* This file is system-dependent in the sense that some of its functions
* are unnecessary in some systems. For example, if there is enough virtual
* memory so that backing storage will never be used, much of the big-array
* control logic could be removed. (Of course, if you have that much memory
* then you shouldn't care about a little bit of unused code...)
*
* These routines are invoked via the methods alloc_small, free_small,
* alloc_medium, free_medium, alloc_small_sarray, free_small_sarray,
* alloc_small_barray, free_small_barray, request_big_sarray,
* request_big_barray, alloc_big_arrays, access_big_sarray, access_big_barray,
* free_big_sarray, free_big_barray, and free_all.
*/
#define AM_MEMORY_MANAGER /* we define big_Xarray_control structs */
#include "jinclude.h"
#include "jmemsys.h" /* import the system-dependent declarations */
#ifndef XIE_SUPPORTED
#ifndef NO_GETENV
#ifdef INCLUDES_ARE_ANSI
#include <stdlib.h> /* to declare getenv() */
#else
extern char * getenv PP((const char * name));
#endif
#endif
#endif /* XIE_SUPPORTED */
/*
* On many systems it is not necessary to distinguish alloc_small from
* alloc_medium; the main case where they must be distinguished is when
* FAR pointers are distinct from regular pointers. However, you might
* want to keep them separate if you have different system-dependent logic
* for small and large memory requests (i.e., jget_small and jget_large
* do different things).
*/
#ifdef NEED_FAR_POINTERS
#define NEED_ALLOC_MEDIUM /* flags alloc_medium really exists */
#endif
#ifndef XIE_SUPPORTED
/*
* Many machines require storage alignment: longs must start on 4-byte
* boundaries, doubles on 8-byte boundaries, etc. On such machines, malloc()
* always returns pointers that are multiples of the worst-case alignment
* requirement, and we had better do so too. This means the headers that
* we tack onto allocated structures had better have length a multiple of
* the alignment requirement.
* There isn't any really portable way to determine the worst-case alignment
* requirement. In this code we assume that the alignment requirement is
* multiples of sizeof(align_type). Here we define align_type as double;
* with this definition, the code will run on all machines known to me.
* If your machine has lesser alignment needs, you can save a few bytes
* by making align_type smaller.
*/
typedef double align_type;
/*
* Some important notes:
* The allocation routines provided here must never return NULL.
* They should exit to error_exit if unsuccessful.
*
* It's not a good idea to try to merge the sarray and barray routines,
* even though they are textually almost the same, because samples are
* usually stored as bytes while coefficients are shorts. Thus, in machines
* where byte pointers have a different representation from word pointers,
* the resulting machine code could not be the same.
*/
static external_methods_ptr methods; /* saved for access to error_exit */
#ifdef MEM_STATS /* optional extra stuff for statistics */
/* These macros are the assumed overhead per block for malloc().
* They don't have to be accurate, but the printed statistics will be
* off a little bit if they are not.
*/
#define MALLOC_OVERHEAD (SIZEOF(pointer )) /* overhead for jget_small() */
#define MALLOC_FAR_OVERHEAD (SIZEOF(pointer)) /* for jget_large() */
static long total_num_small = 0; /* total # of small objects alloced */
static long total_bytes_small = 0; /* total bytes requested */
static long cur_num_small = 0; /* # currently alloced */
static long max_num_small = 0; /* max simultaneously alloced */
#ifdef NEED_ALLOC_MEDIUM
static long total_num_medium = 0; /* total # of medium objects alloced */
static long total_bytes_medium = 0; /* total bytes requested */
static long cur_num_medium = 0; /* # currently alloced */
static long max_num_medium = 0; /* max simultaneously alloced */
#endif
static long total_num_sarray = 0; /* total # of sarray objects alloced */
static long total_bytes_sarray = 0; /* total bytes requested */
static long cur_num_sarray = 0; /* # currently alloced */
static long max_num_sarray = 0; /* max simultaneously alloced */
static long total_num_barray = 0; /* total # of barray objects alloced */
static long total_bytes_barray = 0; /* total bytes requested */
static long cur_num_barray = 0; /* # currently alloced */
static long max_num_barray = 0; /* max simultaneously alloced */
LOCAL void
print_mem_stats (void)
{
/* since this is only a debugging stub, we can cheat a little on the
* trace message mechanism... helpful 'cuz trace_message can't handle longs.
*/
fprintf(stderr, "total_num_small = %ld\n", total_num_small);
fprintf(stderr, "total_bytes_small = %ld\n", total_bytes_small);
if (cur_num_small)
fprintf(stderr, "cur_num_small = %ld\n", cur_num_small);
fprintf(stderr, "max_num_small = %ld\n", max_num_small);
#ifdef NEED_ALLOC_MEDIUM
fprintf(stderr, "total_num_medium = %ld\n", total_num_medium);
fprintf(stderr, "total_bytes_medium = %ld\n", total_bytes_medium);
if (cur_num_medium)
fprintf(stderr, "cur_num_medium = %ld\n", cur_num_medium);
fprintf(stderr, "max_num_medium = %ld\n", max_num_medium);
#endif
fprintf(stderr, "total_num_sarray = %ld\n", total_num_sarray);
fprintf(stderr, "total_bytes_sarray = %ld\n", total_bytes_sarray);
if (cur_num_sarray)
fprintf(stderr, "cur_num_sarray = %ld\n", cur_num_sarray);
fprintf(stderr, "max_num_sarray = %ld\n", max_num_sarray);
fprintf(stderr, "total_num_barray = %ld\n", total_num_barray);
fprintf(stderr, "total_bytes_barray = %ld\n", total_bytes_barray);
if (cur_num_barray)
fprintf(stderr, "cur_num_barray = %ld\n", cur_num_barray);
fprintf(stderr, "max_num_barray = %ld\n", max_num_barray);
}
#endif /* MEM_STATS */
LOCAL void
out_of_memory (int which)
/* Report an out-of-memory error and stop execution */
/* If we compiled MEM_STATS support, report alloc requests before dying */
{
#ifdef MEM_STATS
if (methods->trace_level <= 0) /* don't do it if free_all() will */
print_mem_stats(); /* print optional memory usage statistics */
#endif
ERREXIT1(methods, "Insufficient memory (case %d)", which);
}
#endif /* XIE_SUPPORTED */
/*
* Management of "small" objects.
* These are all-in-memory, and are in near-heap space on an 80x86.
*/
#ifdef XIE_SUPPORTED
METHODDEF pointer
#if NeedFunctionPrototypes
c_alloc_small (compress_info_ptr cinfo, size_t sizeofobject)
#else
c_alloc_small (cinfo, sizeofobject)
compress_info_ptr cinfo;
size_t sizeofobject;
#endif /* NeedFunctionPrototypes */
/* Allocate a "small" object */
{
small_ptr result;
sizeofobject += SIZEOF(small_hdr); /* add space for header */
result = (small_ptr) jget_small(sizeofobject);
if (result == NULL)
return (pointer ) result;
result->next = cinfo->small_list;
cinfo->small_list = result;
result++; /* advance past header */
return (pointer ) result;
}
METHODDEF pointer
#if NeedFunctionPrototypes
d_alloc_small (decompress_info_ptr cinfo, size_t sizeofobject)
#else
d_alloc_small (cinfo, sizeofobject)
decompress_info_ptr cinfo;
size_t sizeofobject;
#endif /* NeedFunctionPrototypes */
/* Allocate a "small" object */
{
small_ptr result;
sizeofobject += SIZEOF(small_hdr); /* add space for header */
result = (small_ptr) jget_small(sizeofobject);
if (result == NULL)
return (pointer ) result;
result->next = cinfo->small_list;
cinfo->small_list = result;
result++; /* advance past header */
return (pointer ) result;
}
#else
typedef union small_struct * small_ptr;
typedef union small_struct {
small_ptr next; /* next in list of allocated objects */
align_type dummy; /* ensures alignment of following storage */
} small_hdr;
static small_ptr small_list; /* head of list */
METHODDEF pointer
alloc_small (size_t sizeofobject)
/* Allocate a "small" object */
{
small_ptr result;
sizeofobject += SIZEOF(small_hdr); /* add space for header */
#ifdef MEM_STATS
total_num_small++;
total_bytes_small += sizeofobject + MALLOC_OVERHEAD;
cur_num_small++;
if (cur_num_small > max_num_small) max_num_small = cur_num_small;
#endif
result = (small_ptr) jget_small(sizeofobject);
if (result == NULL)
out_of_memory(1);
result->next = small_list;
small_list = result;
result++; /* advance past header */
return (pointer ) result;
}
#endif /* XIE_SUPPORTED */
#ifdef XIE_SUPPORTED
METHODDEF int
#if NeedFunctionPrototypes
c_free_small (compress_info_ptr cinfo, pointer ptr)
#else
c_free_small (cinfo, ptr)
compress_info_ptr cinfo;
pointer ptr;
#endif /* NeedFunctionPrototypes */
/* Free a "small" object */
{
small_ptr hdr;
small_ptr * llink;
hdr = (small_ptr) ptr;
hdr--; /* point back to header */
/* Remove item from list -- linear search is fast enough */
llink = &cinfo->small_list;
while (*llink != hdr) {
if (*llink == NULL)
return(XIE_ERR);
llink = &( (*llink)->next );
}
*llink = hdr->next;
jfree_small((pointer ) hdr);
return(0);
}
METHODDEF int
#if NeedFunctionPrototypes
d_free_small (decompress_info_ptr cinfo, pointer ptr)
#else
d_free_small (cinfo, ptr)
decompress_info_ptr cinfo;
pointer ptr;
#endif /* NeedFunctionPrototypes */
/* Free a "small" object */
{
small_ptr hdr;
small_ptr * llink;
hdr = (small_ptr) ptr;
hdr--; /* point back to header */
/* Remove item from list -- linear search is fast enough */
llink = &cinfo->small_list;
while (*llink != hdr) {
if (*llink == NULL)
return(XIE_ERR);
llink = &( (*llink)->next );
}
*llink = hdr->next;
jfree_small((pointer ) hdr);
return(0);
}
#else
METHODDEF void
free_small (pointer ptr)
/* Free a "small" object */
{
small_ptr hdr;
small_ptr * llink;
hdr = (small_ptr) ptr;
hdr--; /* point back to header */
/* Remove item from list -- linear search is fast enough */
llink = &small_list;
while (*llink != hdr) {
if (*llink == NULL)
ERREXIT(methods, "Bogus free_small request");
llink = &( (*llink)->next );
}
*llink = hdr->next;
jfree_small((pointer ) hdr);
#ifdef MEM_STATS
cur_num_small--;
#endif
}
#endif /* XIE_SUPPORTED */
/*
* Management of "medium-size" objects.
* These are just like small objects except they are in the FAR heap.
*/
#ifndef XIE_SUPPORTED
#ifdef NEED_ALLOC_MEDIUM
typedef union medium_struct FAR * medium_ptr;
typedef union medium_struct {
medium_ptr next; /* next in list of allocated objects */
align_type dummy; /* ensures alignment of following storage */
} medium_hdr;
static medium_ptr medium_list; /* head of list */
METHODDEF pointer
alloc_medium (size_t sizeofobject)
/* Allocate a "medium-size" object */
{
medium_ptr result;
sizeofobject += SIZEOF(medium_hdr); /* add space for header */
#ifdef MEM_STATS
total_num_medium++;
total_bytes_medium += sizeofobject + MALLOC_FAR_OVERHEAD;
cur_num_medium++;
if (cur_num_medium > max_num_medium) max_num_medium = cur_num_medium;
#endif
result = (medium_ptr) jget_large(sizeofobject);
if (result == NULL)
out_of_memory(2);
result->next = medium_list;
medium_list = result;
result++; /* advance past header */
return (pointer) result;
}
METHODDEF void
free_medium (pointer ptr)
/* Free a "medium-size" object */
{
medium_ptr hdr;
medium_ptr FAR * llink;
hdr = (medium_ptr) ptr;
hdr--; /* point back to header */
/* Remove item from list -- linear search is fast enough */
llink = &medium_list;
while (*llink != hdr) {
if (*llink == NULL)
ERREXIT(methods, "Bogus free_medium request");
llink = &( (*llink)->next );
}
*llink = hdr->next;
jfree_large((pointer) hdr);
#ifdef MEM_STATS
cur_num_medium--;
#endif
}
#endif /* NEED_ALLOC_MEDIUM */
#endif /* XIE_SUPPORTED */
/*
* Management of "small" (all-in-memory) 2-D sample arrays.
* The pointers are in near heap, the samples themselves in FAR heap.
* The header structure is adjacent to the row pointers.
* To minimize allocation overhead and to allow I/O of large contiguous
* blocks, we allocate the sample rows in groups of as many rows as possible
* without exceeding MAX_ALLOC_CHUNK total bytes per allocation request.
* Note that the big-array control routines, later in this file, know about
* this chunking of rows ... and also how to get the rowsperchunk value!
*/
#ifdef XIE_SUPPORTED
METHODDEF JSAMPARRAY
#if NeedFunctionPrototypes
c_alloc_small_sarray (compress_info_ptr cinfo,
long samplesperrow, long numrows)
#else
c_alloc_small_sarray (cinfo, samplesperrow, numrows)
compress_info_ptr cinfo;
long samplesperrow;
long numrows;
#endif /* NeedFunctionPrototypes */
/* Allocate a "small" (all-in-memory) 2-D sample array */
{
small_sarray_ptr hdr;
JSAMPARRAY result;
JSAMPROW workspace;
long rowsperchunk, currow, i;
/* Calculate max # of rows allowed in one allocation chunk */
rowsperchunk = MAX_ALLOC_CHUNK / (samplesperrow * SIZEOF(JSAMPLE));
if (rowsperchunk <= 0)
return (JSAMPARRAY) NULL;
/* Get space for header and row pointers; this is always "near" on 80x86 */
hdr = (small_sarray_ptr) c_alloc_small(cinfo,
(size_t) (numrows * SIZEOF(JSAMPROW) + SIZEOF(small_sarray_hdr)));
result = (JSAMPARRAY) (hdr+1); /* advance past header */
/* Insert into list now so free_all does right thing if I fail */
/* after allocating only some of the rows... */
hdr->next = cinfo->small_sarray_list;
hdr->numrows = 0;
hdr->rowsperchunk = rowsperchunk;
cinfo->small_sarray_list = hdr;
/* Get the rows themselves; on 80x86 these are "far" */
currow = 0;
while (currow < numrows) {
rowsperchunk = MIN(rowsperchunk, numrows - currow);
workspace = (JSAMPROW) jget_large((size_t) (rowsperchunk * samplesperrow
* SIZEOF(JSAMPLE)));
if (workspace == NULL)
return (JSAMPARRAY) workspace;
for (i = rowsperchunk; i > 0; i--) {
result[currow++] = workspace;
workspace += samplesperrow;
}
hdr->numrows = currow;
}
return result;
}
METHODDEF JSAMPARRAY
#if NeedFunctionPrototypes
d_alloc_small_sarray (decompress_info_ptr cinfo,
long samplesperrow, long numrows)
#else
d_alloc_small_sarray (cinfo, samplesperrow, numrows)
decompress_info_ptr cinfo;
long samplesperrow;
long numrows;
#endif /* NeedFunctionPrototypes */
/* Allocate a "small" (all-in-memory) 2-D sample array */
{
small_sarray_ptr hdr;
JSAMPARRAY result;
JSAMPROW workspace;
long rowsperchunk, currow, i;
/* Calculate max # of rows allowed in one allocation chunk */
rowsperchunk = MAX_ALLOC_CHUNK / (samplesperrow * SIZEOF(JSAMPLE));
if (rowsperchunk <= 0)
return (JSAMPARRAY) NULL;
/* Get space for header and row pointers; this is always "near" on 80x86 */
hdr = (small_sarray_ptr) d_alloc_small(cinfo,
(size_t) (numrows * SIZEOF(JSAMPROW) + SIZEOF(small_sarray_hdr)));
result = (JSAMPARRAY) (hdr+1); /* advance past header */
/* Insert into list now so free_all does right thing if I fail */
/* after allocating only some of the rows... */
hdr->next = cinfo->small_sarray_list;
hdr->numrows = 0;
hdr->rowsperchunk = rowsperchunk;
cinfo->small_sarray_list = hdr;
/* Get the rows themselves; on 80x86 these are "far" */
currow = 0;
while (currow < numrows) {
rowsperchunk = MIN(rowsperchunk, numrows - currow);
workspace = (JSAMPROW) jget_large((size_t) (rowsperchunk * samplesperrow
* SIZEOF(JSAMPLE)));
if (workspace == NULL)
return (JSAMPARRAY) workspace;
for (i = rowsperchunk; i > 0; i--) {
result[currow++] = workspace;
workspace += samplesperrow;
}
hdr->numrows = currow;
}
return result;
}
#else
typedef struct small_sarray_struct * small_sarray_ptr;
typedef struct small_sarray_struct {
small_sarray_ptr next; /* next in list of allocated sarrays */
long numrows; /* # of rows in this array */
long rowsperchunk; /* max # of rows per allocation chunk */
JSAMPROW dummy; /* ensures alignment of following storage */
} small_sarray_hdr;
static small_sarray_ptr small_sarray_list; /* head of list */
METHODDEF JSAMPARRAY
alloc_small_sarray (long samplesperrow, long numrows)
/* Allocate a "small" (all-in-memory) 2-D sample array */
{
small_sarray_ptr hdr;
JSAMPARRAY result;
JSAMPROW workspace;
long rowsperchunk, currow, i;
#ifdef MEM_STATS
total_num_sarray++;
cur_num_sarray++;
if (cur_num_sarray > max_num_sarray) max_num_sarray = cur_num_sarray;
#endif
/* Calculate max # of rows allowed in one allocation chunk */
rowsperchunk = MAX_ALLOC_CHUNK / (samplesperrow * SIZEOF(JSAMPLE));
if (rowsperchunk <= 0)
ERREXIT(methods, "Image too wide for this implementation");
/* Get space for header and row pointers; this is always "near" on 80x86 */
hdr = (small_sarray_ptr) alloc_small((size_t) (numrows * SIZEOF(JSAMPROW)
+ SIZEOF(small_sarray_hdr)));
result = (JSAMPARRAY) (hdr+1); /* advance past header */
/* Insert into list now so free_all does right thing if I fail */
/* after allocating only some of the rows... */
hdr->next = small_sarray_list;
hdr->numrows = 0;
hdr->rowsperchunk = rowsperchunk;
small_sarray_list = hdr;
/* Get the rows themselves; on 80x86 these are "far" */
currow = 0;
while (currow < numrows) {
rowsperchunk = MIN(rowsperchunk, numrows - currow);
#ifdef MEM_STATS
total_bytes_sarray += rowsperchunk * samplesperrow * SIZEOF(JSAMPLE)
+ MALLOC_FAR_OVERHEAD;
#endif
workspace = (JSAMPROW) jget_large((size_t) (rowsperchunk * samplesperrow
* SIZEOF(JSAMPLE)));
if (workspace == NULL)
out_of_memory(3);
for (i = rowsperchunk; i > 0; i--) {
result[currow++] = workspace;
workspace += samplesperrow;
}
hdr->numrows = currow;
}
return result;
}
#endif /* XIE_SUPPORTED */
#ifdef XIE_SUPPORTED
METHODDEF int
#if NeedFunctionPrototypes
c_free_small_sarray (compress_info_ptr cinfo, JSAMPARRAY ptr)
#else
c_free_small_sarray (cinfo, ptr)
compress_info_ptr cinfo;
JSAMPARRAY ptr;
#endif /* NeedFunctionPrototypes */
/* Free a "small" (all-in-memory) 2-D sample array */
{
small_sarray_ptr hdr;
small_sarray_ptr * llink;
long i;
hdr = (small_sarray_ptr) ptr;
hdr--; /* point back to header */
/* Remove item from list -- linear search is fast enough */
llink = &cinfo->small_sarray_list;
while (*llink != hdr) {
if (*llink == NULL)
return(XIE_ERR);
llink = &( (*llink)->next );
}
*llink = hdr->next;
/* Free the rows themselves; on 80x86 these are "far" */
/* Note we only free the row-group headers! */
for (i = 0; i < hdr->numrows; i += hdr->rowsperchunk) {
jfree_large((pointer) ptr[i]);
}
/* Free header and row pointers */
return(c_free_small(cinfo, (pointer ) hdr));
}
METHODDEF int
#if NeedFunctionPrototypes
d_free_small_sarray (decompress_info_ptr cinfo, JSAMPARRAY ptr)
#else
d_free_small_sarray (cinfo, ptr)
decompress_info_ptr cinfo;
JSAMPARRAY ptr;
#endif /* NeedFunctionPrototypes */
/* Free a "small" (all-in-memory) 2-D sample array */
{
small_sarray_ptr hdr;
small_sarray_ptr * llink;
long i;
hdr = (small_sarray_ptr) ptr;
hdr--; /* point back to header */
/* Remove item from list -- linear search is fast enough */
llink = &cinfo->small_sarray_list;
while (*llink != hdr) {
if (*llink == NULL)
return(XIE_ERR);
llink = &( (*llink)->next );
}
*llink = hdr->next;
/* Free the rows themselves; on 80x86 these are "far" */
/* Note we only free the row-group headers! */
for (i = 0; i < hdr->numrows; i += hdr->rowsperchunk) {
jfree_large((pointer) ptr[i]);
}
/* Free header and row pointers */
return(d_free_small(cinfo, (pointer ) hdr));
}
#else
METHODDEF void
free_small_sarray (JSAMPARRAY ptr)
/* Free a "small" (all-in-memory) 2-D sample array */
{
small_sarray_ptr hdr;
small_sarray_ptr * llink;
long i;
hdr = (small_sarray_ptr) ptr;
hdr--; /* point back to header */
/* Remove item from list -- linear search is fast enough */
llink = &small_sarray_list;
while (*llink != hdr) {
if (*llink == NULL)
ERREXIT(methods, "Bogus free_small_sarray request");
llink = &( (*llink)->next );
}
*llink = hdr->next;
/* Free the rows themselves; on 80x86 these are "far" */
/* Note we only free the row-group headers! */
for (i = 0; i < hdr->numrows; i += hdr->rowsperchunk) {
jfree_large((pointer) ptr[i]);
}
/* Free header and row pointers */
free_small((pointer ) hdr);
#ifdef MEM_STATS
cur_num_sarray--;
#endif
}
#endif /* XIE_SUPPORTED */
/*
* Management of "small" (all-in-memory) 2-D coefficient-block arrays.
* This is essentially the same as the code for sample arrays, above.
*/
#ifdef XIE_SUPPORTED
METHODDEF JBLOCKARRAY
#if NeedFunctionPrototypes
d_alloc_small_barray (decompress_info_ptr cinfo,
long blocksperrow, long numrows)
#else
d_alloc_small_barray (cinfo, blocksperrow, numrows)
decompress_info_ptr cinfo;
long blocksperrow;
long numrows;
#endif /* NeedFunctionPrototypes */
/* Allocate a "small" (all-in-memory) 2-D coefficient-block array */
{
small_barray_ptr hdr;
JBLOCKARRAY result;
JBLOCKROW workspace;
long rowsperchunk, currow, i;
/* Calculate max # of rows allowed in one allocation chunk */
rowsperchunk = MAX_ALLOC_CHUNK / (blocksperrow * SIZEOF(JBLOCK));
if (rowsperchunk <= 0)
return (JBLOCKARRAY) NULL;
/* Get space for header and row pointers; this is always "near" on 80x86 */
hdr = (small_barray_ptr) d_alloc_small(cinfo,
(size_t) (numrows * SIZEOF(JBLOCKROW) + SIZEOF(small_barray_hdr)));
result = (JBLOCKARRAY) (hdr+1); /* advance past header */
/* Insert into list now so free_all does right thing if I fail */
/* after allocating only some of the rows... */
hdr->next = cinfo->small_barray_list;
hdr->numrows = 0;
hdr->rowsperchunk = rowsperchunk;
cinfo->small_barray_list = hdr;
/* Get the rows themselves; on 80x86 these are "far" */
currow = 0;
while (currow < numrows) {
rowsperchunk = MIN(rowsperchunk, numrows - currow);
workspace = (JBLOCKROW) jget_large((size_t) (rowsperchunk * blocksperrow
* SIZEOF(JBLOCK)));
if (workspace == NULL)
return (JBLOCKARRAY) workspace;
for (i = rowsperchunk; i > 0; i--) {
result[currow++] = workspace;
workspace += blocksperrow;
}
hdr->numrows = currow;
}
return result;
}
#else
typedef struct small_barray_struct * small_barray_ptr;
typedef struct small_barray_struct {
small_barray_ptr next; /* next in list of allocated barrays */
long numrows; /* # of rows in this array */
long rowsperchunk; /* max # of rows per allocation chunk */
JBLOCKROW dummy; /* ensures alignment of following storage */
} small_barray_hdr;
static small_barray_ptr small_barray_list; /* head of list */
METHODDEF JBLOCKARRAY
alloc_small_barray (long blocksperrow, long numrows)
/* Allocate a "small" (all-in-memory) 2-D coefficient-block array */
{
small_barray_ptr hdr;
JBLOCKARRAY result;
JBLOCKROW workspace;
long rowsperchunk, currow, i;
#ifdef MEM_STATS
total_num_barray++;
cur_num_barray++;
if (cur_num_barray > max_num_barray) max_num_barray = cur_num_barray;
#endif
/* Calculate max # of rows allowed in one allocation chunk */
rowsperchunk = MAX_ALLOC_CHUNK / (blocksperrow * SIZEOF(JBLOCK));
if (rowsperchunk <= 0)
ERREXIT(methods, "Image too wide for this implementation");
/* Get space for header and row pointers; this is always "near" on 80x86 */
hdr = (small_barray_ptr) alloc_small((size_t) (numrows * SIZEOF(JBLOCKROW)
+ SIZEOF(small_barray_hdr)));
result = (JBLOCKARRAY) (hdr+1); /* advance past header */
/* Insert into list now so free_all does right thing if I fail */
/* after allocating only some of the rows... */
hdr->next = small_barray_list;
hdr->numrows = 0;
hdr->rowsperchunk = rowsperchunk;
small_barray_list = hdr;
/* Get the rows themselves; on 80x86 these are "far" */
currow = 0;
while (currow < numrows) {
rowsperchunk = MIN(rowsperchunk, numrows - currow);
#ifdef MEM_STATS
total_bytes_barray += rowsperchunk * blocksperrow * SIZEOF(JBLOCK)
+ MALLOC_FAR_OVERHEAD;
#endif
workspace = (JBLOCKROW) jget_large((size_t) (rowsperchunk * blocksperrow
* SIZEOF(JBLOCK)));
if (workspace == NULL)
out_of_memory(4);
for (i = rowsperchunk; i > 0; i--) {
result[currow++] = workspace;
workspace += blocksperrow;
}
hdr->numrows = currow;
}
return result;
}
#endif /* XIE_SUPPORTED */
#ifdef XIE_SUPPORTED
METHODDEF int
#if NeedFunctionPrototypes
d_free_small_barray (decompress_info_ptr cinfo, JBLOCKARRAY ptr)
#else
d_free_small_barray (cinfo, ptr)
decompress_info_ptr cinfo;
JBLOCKARRAY ptr;
#endif /* NeedFunctionPrototypes */
/* Free a "small" (all-in-memory) 2-D coefficient-block array */
{
small_barray_ptr hdr;
small_barray_ptr * llink;
long i;
hdr = (small_barray_ptr) ptr;
hdr--; /* point back to header */
/* Remove item from list -- linear search is fast enough */
llink = &cinfo->small_barray_list;
while (*llink != hdr) {
if (*llink == NULL)
return(XIE_ERR);
llink = &( (*llink)->next );
}
*llink = hdr->next;
/* Free the rows themselves; on 80x86 these are "far" */
/* Note we only free the row-group headers! */
for (i = 0; i < hdr->numrows; i += hdr->rowsperchunk) {
jfree_large((pointer) ptr[i]);
}
/* Free header and row pointers */
return(d_free_small(cinfo, (pointer ) hdr));
}
#else
METHODDEF void
free_small_barray (JBLOCKARRAY ptr)
/* Free a "small" (all-in-memory) 2-D coefficient-block array */
{
small_barray_ptr hdr;
small_barray_ptr * llink;
long i;
hdr = (small_barray_ptr) ptr;
hdr--; /* point back to header */
/* Remove item from list -- linear search is fast enough */
llink = &small_barray_list;
while (*llink != hdr) {
if (*llink == NULL)
ERREXIT(methods, "Bogus free_small_barray request");
llink = &( (*llink)->next );
}
*llink = hdr->next;
/* Free the rows themselves; on 80x86 these are "far" */
/* Note we only free the row-group headers! */
for (i = 0; i < hdr->numrows; i += hdr->rowsperchunk) {
jfree_large((pointer) ptr[i]);
}
/* Free header and row pointers */
free_small((pointer ) hdr);
#ifdef MEM_STATS
cur_num_barray--;
#endif
}
#endif /* XIE_SUPPORTED */
/*
* About "big" array management:
*
* To allow machines with limited memory to handle large images,
* all processing in the JPEG system is done a few pixel or block rows
* at a time. The above "small" array routines are only used to allocate
* strip buffers (as wide as the image, but just a few rows high).
* In some cases multiple passes must be made over the data. In these
* cases the "big" array routines are used. The array is still accessed
* a strip at a time, but the memory manager must save the whole array
* for repeated accesses. The intended implementation is that there is
* a strip buffer in memory (as high as is possible given the desired memory
* limit), plus a backing file that holds the rest of the array.
*
* The request_big_array routines are told the total size of the image (in case
* it is useful to know the total file size that will be needed). They are
* also given the unit height, which is the number of rows that will be
* accessed at once; the in-memory buffer should be made a multiple of
* this height for best efficiency.
*
* The request routines create control blocks (and may open backing files),
* but they don't create the in-memory buffers. This is postponed until
* alloc_big_arrays is called. At that time the total amount of space needed
* is known (approximately, anyway), so free memory can be divided up fairly.
*
* The access_big_array routines are responsible for making a specific strip
* area accessible (after reading or writing the backing file, if necessary).
* Note that the access routines are told whether the caller intends to modify
* the accessed strip; during a read-only pass this saves having to rewrite
* data to disk.
*
* The typical access pattern is one top-to-bottom pass to write the data,
* followed by one or more read-only top-to-bottom passes. However, other
* access patterns may occur while reading. For example, translation of image
* formats that use bottom-to-top scan order will require bottom-to-top read
* passes. The memory manager need not support multiple write passes nor
* funny write orders (meaning that rearranging rows must be handled while
* reading data out of the big array, not while putting it in).
*
* In current usage, the access requests are always for nonoverlapping strips;
* that is, successive access start_row numbers always differ by exactly the
* unitheight. This allows fairly simple buffer dump/reload logic if the
* in-memory buffer is made a multiple of the unitheight. It would be
* possible to keep downsampled rather than fullsize data in the "big" arrays,
* thus reducing temp file size, if we supported overlapping strip access
* (access requests differing by less than the unitheight). At the moment
* I don't believe this is worth the extra complexity.
*/
/* The control blocks for virtual arrays.
* System-dependent info for the associated backing store is hidden inside
* the backing_store_info struct.
*/
#ifndef XIE_SUPPORTED
struct big_sarray_control {
long rows_in_array; /* total virtual array height */
long samplesperrow; /* width of array (and of memory buffer) */
long unitheight; /* # of rows accessed by access_big_sarray() */
JSAMPARRAY mem_buffer; /* the in-memory buffer */
long rows_in_mem; /* height of memory buffer */
long rowsperchunk; /* allocation chunk size in mem_buffer */
long cur_start_row; /* first logical row # in the buffer */
boolean dirty; /* do current buffer contents need written? */
boolean b_s_open; /* is backing-store data valid? */
big_sarray_ptr next; /* link to next big sarray control block */
backing_store_info b_s_info; /* System-dependent control info */
};
static big_sarray_ptr big_sarray_list; /* head of list */
struct big_barray_control {
long rows_in_array; /* total virtual array height */
long blocksperrow; /* width of array (and of memory buffer) */
long unitheight; /* # of rows accessed by access_big_barray() */
JBLOCKARRAY mem_buffer; /* the in-memory buffer */
long rows_in_mem; /* height of memory buffer */
long rowsperchunk; /* allocation chunk size in mem_buffer */
long cur_start_row; /* first logical row # in the buffer */
boolean dirty; /* do current buffer contents need written? */
boolean b_s_open; /* is backing-store data valid? */
big_barray_ptr next; /* link to next big barray control block */
backing_store_info b_s_info; /* System-dependent control info */
};
static big_barray_ptr big_barray_list; /* head of list */
METHODDEF big_sarray_ptr
request_big_sarray (long samplesperrow, long numrows, long unitheight)
/* Request a "big" (virtual-memory) 2-D sample array */
{
big_sarray_ptr result;
/* get control block */
result = (big_sarray_ptr) alloc_small(SIZEOF(struct big_sarray_control));
result->rows_in_array = numrows;
result->samplesperrow = samplesperrow;
result->unitheight = unitheight;
result->mem_buffer = NULL; /* marks array not yet realized */
result->b_s_open = FALSE; /* no associated backing-store object */
result->next = big_sarray_list; /* add to list of big arrays */
big_sarray_list = result;
return result;
}
METHODDEF big_barray_ptr
request_big_barray (long blocksperrow, long numrows, long unitheight)
/* Request a "big" (virtual-memory) 2-D coefficient-block array */
{
big_barray_ptr result;
/* get control block */
result = (big_barray_ptr) alloc_small(SIZEOF(struct big_barray_control));
result->rows_in_array = numrows;
result->blocksperrow = blocksperrow;
result->unitheight = unitheight;
result->mem_buffer = NULL; /* marks array not yet realized */
result->b_s_open = FALSE; /* no associated backing-store object */
result->next = big_barray_list; /* add to list of big arrays */
big_barray_list = result;
return result;
}
METHODDEF void
alloc_big_arrays (long extra_small_samples, long extra_small_blocks,
long extra_medium_space)
/* Allocate the in-memory buffers for any unrealized "big" arrays */
/* 'extra' values are upper bounds for total future small-array requests */
/* and far-heap requests */
{
long total_extra_space = extra_small_samples * SIZEOF(JSAMPLE)
+ extra_small_blocks * SIZEOF(JBLOCK)
+ extra_medium_space;
long space_per_unitheight, maximum_space, avail_mem;
long unitheights, max_unitheights;
big_sarray_ptr sptr;
big_barray_ptr bptr;
/* Compute the minimum space needed (unitheight rows in each buffer)
* and the maximum space needed (full image height in each buffer).
* These may be of use to the system-dependent jmem_available routine.
*/
space_per_unitheight = 0;
maximum_space = total_extra_space;
for (sptr = big_sarray_list; sptr != NULL; sptr = sptr->next) {
if (sptr->mem_buffer == NULL) { /* if not realized yet */
space_per_unitheight += sptr->unitheight *
sptr->samplesperrow * SIZEOF(JSAMPLE);
maximum_space += sptr->rows_in_array *
sptr->samplesperrow * SIZEOF(JSAMPLE);
}
}
for (bptr = big_barray_list; bptr != NULL; bptr = bptr->next) {
if (bptr->mem_buffer == NULL) { /* if not realized yet */
space_per_unitheight += bptr->unitheight *
bptr->blocksperrow * SIZEOF(JBLOCK);
maximum_space += bptr->rows_in_array *
bptr->blocksperrow * SIZEOF(JBLOCK);
}
}
if (space_per_unitheight <= 0)
return; /* no unrealized arrays, no work */
/* Determine amount of memory to actually use; this is system-dependent. */
avail_mem = jmem_available(space_per_unitheight + total_extra_space,
maximum_space);
/* If the maximum space needed is available, make all the buffers full
* height; otherwise parcel it out with the same number of unitheights
* in each buffer.
*/
if (avail_mem >= maximum_space)
max_unitheights = 1000000000L;
else {
max_unitheights = (avail_mem - total_extra_space) / space_per_unitheight;
/* If there doesn't seem to be enough space, try to get the minimum
* anyway. This allows a "stub" implementation of jmem_available().
*/
if (max_unitheights <= 0)
max_unitheights = 1;
}
/* Allocate the in-memory buffers and initialize backing store as needed. */
for (sptr = big_sarray_list; sptr != NULL; sptr = sptr->next) {
if (sptr->mem_buffer == NULL) { /* if not realized yet */
unitheights = (sptr->rows_in_array + sptr->unitheight - 1L)
/ sptr->unitheight;
if (unitheights <= max_unitheights) {
/* This buffer fits in memory */
sptr->rows_in_mem = sptr->rows_in_array;
} else {
/* It doesn't fit in memory, create backing store. */
sptr->rows_in_mem = max_unitheights * sptr->unitheight;
jopen_backing_store(& sptr->b_s_info,
(long) (sptr->rows_in_array *
sptr->samplesperrow * SIZEOF(JSAMPLE)));
sptr->b_s_open = TRUE;
}
sptr->mem_buffer = alloc_small_sarray(sptr->samplesperrow,
sptr->rows_in_mem);
/* Reach into the small_sarray header and get the rowsperchunk field.
* Yes, I know, this is horrible coding practice.
*/
sptr->rowsperchunk =
((small_sarray_ptr) sptr->mem_buffer)[-1].rowsperchunk;
sptr->cur_start_row = 0;
sptr->dirty = FALSE;
}
}
for (bptr = big_barray_list; bptr != NULL; bptr = bptr->next) {
if (bptr->mem_buffer == NULL) { /* if not realized yet */
unitheights = (bptr->rows_in_array + bptr->unitheight - 1L)
/ bptr->unitheight;
if (unitheights <= max_unitheights) {
/* This buffer fits in memory */
bptr->rows_in_mem = bptr->rows_in_array;
} else {
/* It doesn't fit in memory, create backing store. */
bptr->rows_in_mem = max_unitheights * bptr->unitheight;
jopen_backing_store(& bptr->b_s_info,
(long) (bptr->rows_in_array *
bptr->blocksperrow * SIZEOF(JBLOCK)));
bptr->b_s_open = TRUE;
}
bptr->mem_buffer = alloc_small_barray(bptr->blocksperrow,
bptr->rows_in_mem);
/* Reach into the small_barray header and get the rowsperchunk field. */
bptr->rowsperchunk =
((small_barray_ptr) bptr->mem_buffer)[-1].rowsperchunk;
bptr->cur_start_row = 0;
bptr->dirty = FALSE;
}
}
}
LOCAL void
do_sarray_io (big_sarray_ptr ptr, boolean writing)
/* Do backing store read or write of a "big" sample array */
{
long bytesperrow, file_offset, byte_count, rows, i;
bytesperrow = ptr->samplesperrow * SIZEOF(JSAMPLE);
file_offset = ptr->cur_start_row * bytesperrow;
/* Loop to read or write each allocation chunk in mem_buffer */
for (i = 0; i < ptr->rows_in_mem; i += ptr->rowsperchunk) {
/* One chunk, but check for short chunk at end of buffer */
rows = MIN(ptr->rowsperchunk, ptr->rows_in_mem - i);
/* Transfer no more than fits in file */
rows = MIN(rows, ptr->rows_in_array - (ptr->cur_start_row + i));
if (rows <= 0) /* this chunk might be past end of file! */
break;
byte_count = rows * bytesperrow;
if (writing)
(*ptr->b_s_info.write_backing_store) (& ptr->b_s_info,
(pointer) ptr->mem_buffer[i],
file_offset, byte_count);
else
(*ptr->b_s_info.read_backing_store) (& ptr->b_s_info,
(pointer) ptr->mem_buffer[i],
file_offset, byte_count);
file_offset += byte_count;
}
}
LOCAL void
do_barray_io (big_barray_ptr ptr, boolean writing)
/* Do backing store read or write of a "big" coefficient-block array */
{
long bytesperrow, file_offset, byte_count, rows, i;
bytesperrow = ptr->blocksperrow * SIZEOF(JBLOCK);
file_offset = ptr->cur_start_row * bytesperrow;
/* Loop to read or write each allocation chunk in mem_buffer */
for (i = 0; i < ptr->rows_in_mem; i += ptr->rowsperchunk) {
/* One chunk, but check for short chunk at end of buffer */
rows = MIN(ptr->rowsperchunk, ptr->rows_in_mem - i);
/* Transfer no more than fits in file */
rows = MIN(rows, ptr->rows_in_array - (ptr->cur_start_row + i));
if (rows <= 0) /* this chunk might be past end of file! */
break;
byte_count = rows * bytesperrow;
if (writing)
(*ptr->b_s_info.write_backing_store) (& ptr->b_s_info,
(pointer) ptr->mem_buffer[i],
file_offset, byte_count);
else
(*ptr->b_s_info.read_backing_store) (& ptr->b_s_info,
(pointer) ptr->mem_buffer[i],
file_offset, byte_count);
file_offset += byte_count;
}
}
METHODDEF JSAMPARRAY
access_big_sarray (big_sarray_ptr ptr, long start_row, boolean writable)
/* Access the part of a "big" sample array starting at start_row */
/* and extending for ptr->unitheight rows. writable is true if */
/* caller intends to modify the accessed area. */
{
/* debugging check */
if (start_row < 0 || start_row+ptr->unitheight > ptr->rows_in_array ||
ptr->mem_buffer == NULL)
ERREXIT(methods, "Bogus access_big_sarray request");
/* Make the desired part of the virtual array accessible */
if (start_row < ptr->cur_start_row ||
start_row+ptr->unitheight > ptr->cur_start_row+ptr->rows_in_mem) {
if (! ptr->b_s_open)
ERREXIT(methods, "Virtual array controller messed up");
/* Flush old buffer contents if necessary */
if (ptr->dirty) {
do_sarray_io(ptr, TRUE);
ptr->dirty = FALSE;
}
/* Decide what part of virtual array to access.
* Algorithm: if target address > current window, assume forward scan,
* load starting at target address. If target address < current window,
* assume backward scan, load so that target address is top of window.
* Note that when switching from forward write to forward read, will have
* start_row = 0, so the limiting case applies and we load from 0 anyway.
*/
if (start_row > ptr->cur_start_row) {
ptr->cur_start_row = start_row;
} else {
ptr->cur_start_row = start_row + ptr->unitheight - ptr->rows_in_mem;
if (ptr->cur_start_row < 0)
ptr->cur_start_row = 0; /* don't fall off front end of file */
}
/* If reading, read in the selected part of the array.
* If we are writing, we need not pre-read the selected portion,
* since the access sequence constraints ensure it would be garbage.
*/
if (! writable) {
do_sarray_io(ptr, FALSE);
}
}
/* Flag the buffer dirty if caller will write in it */
if (writable)
ptr->dirty = TRUE;
/* Return address of proper part of the buffer */
return ptr->mem_buffer + (start_row - ptr->cur_start_row);
}
METHODDEF JBLOCKARRAY
access_big_barray (big_barray_ptr ptr, long start_row, boolean writable)
/* Access the part of a "big" coefficient-block array starting at start_row */
/* and extending for ptr->unitheight rows. writable is true if */
/* caller intends to modify the accessed area. */
{
/* debugging check */
if (start_row < 0 || start_row+ptr->unitheight > ptr->rows_in_array ||
ptr->mem_buffer == NULL)
ERREXIT(methods, "Bogus access_big_barray request");
/* Make the desired part of the virtual array accessible */
if (start_row < ptr->cur_start_row ||
start_row+ptr->unitheight > ptr->cur_start_row+ptr->rows_in_mem) {
if (! ptr->b_s_open)
ERREXIT(methods, "Virtual array controller messed up");
/* Flush old buffer contents if necessary */
if (ptr->dirty) {
do_barray_io(ptr, TRUE);
ptr->dirty = FALSE;
}
/* Decide what part of virtual array to access.
* Algorithm: if target address > current window, assume forward scan,
* load starting at target address. If target address < current window,
* assume backward scan, load so that target address is top of window.
* Note that when switching from forward write to forward read, will have
* start_row = 0, so the limiting case applies and we load from 0 anyway.
*/
if (start_row > ptr->cur_start_row) {
ptr->cur_start_row = start_row;
} else {
ptr->cur_start_row = start_row + ptr->unitheight - ptr->rows_in_mem;
if (ptr->cur_start_row < 0)
ptr->cur_start_row = 0; /* don't fall off front end of file */
}
/* If reading, read in the selected part of the array.
* If we are writing, we need not pre-read the selected portion,
* since the access sequence constraints ensure it would be garbage.
*/
if (! writable) {
do_barray_io(ptr, FALSE);
}
}
/* Flag the buffer dirty if caller will write in it */
if (writable)
ptr->dirty = TRUE;
/* Return address of proper part of the buffer */
return ptr->mem_buffer + (start_row - ptr->cur_start_row);
}
METHODDEF void
free_big_sarray (big_sarray_ptr ptr)
/* Free a "big" (virtual-memory) 2-D sample array */
{
big_sarray_ptr * llink;
/* Remove item from list -- linear search is fast enough */
llink = &big_sarray_list;
while (*llink != ptr) {
if (*llink == NULL)
ERREXIT(methods, "Bogus free_big_sarray request");
llink = &( (*llink)->next );
}
*llink = ptr->next;
if (ptr->b_s_open) /* there may be no backing store */
(*ptr->b_s_info.close_backing_store) (& ptr->b_s_info);
if (ptr->mem_buffer != NULL) /* just in case never realized */
free_small_sarray(ptr->mem_buffer);
free_small((pointer ) ptr); /* free the control block too */
}
METHODDEF void
free_big_barray (big_barray_ptr ptr)
/* Free a "big" (virtual-memory) 2-D coefficient-block array */
{
big_barray_ptr * llink;
/* Remove item from list -- linear search is fast enough */
llink = &big_barray_list;
while (*llink != ptr) {
if (*llink == NULL)
ERREXIT(methods, "Bogus free_big_barray request");
llink = &( (*llink)->next );
}
*llink = ptr->next;
if (ptr->b_s_open) /* there may be no backing store */
(*ptr->b_s_info.close_backing_store) (& ptr->b_s_info);
if (ptr->mem_buffer != NULL) /* just in case never realized */
free_small_barray(ptr->mem_buffer);
free_small((pointer ) ptr); /* free the control block too */
}
#endif /* XIE_SUPPORTED */
/*
* Cleanup: free anything that's been allocated since jselmemmgr().
*/
#ifdef XIE_SUPPORTED
METHODDEF int
#if NeedFunctionPrototypes
c_free_all (compress_info_ptr cinfo)
#else
c_free_all (cinfo)
compress_info_ptr cinfo;
#endif /* NeedFunctionPrototypes */
{
/* Free any open small arrays -- these may release small objects */
/* +1's are because we must pass a pointer to the data, not the header */
while (cinfo->small_sarray_list != NULL) {
if ((c_free_small_sarray(cinfo,
(JSAMPARRAY) (cinfo->small_sarray_list + 1))) < 0)
return(XIE_ERR);
}
/* Free any remaining small objects */
while (cinfo->small_list != NULL) {
if ((c_free_small(cinfo,
(pointer ) (cinfo->small_list + 1))) < 0)
return(XIE_ERR);
}
jmem_term(); /* system-dependent cleanup */
return(XIE_NRML);
}
METHODDEF int
#if NeedFunctionPrototypes
d_free_all (decompress_info_ptr cinfo)
#else
d_free_all (cinfo)
decompress_info_ptr cinfo;
#endif /* NeedFunctionPrototypes */
{
/* Free any open small arrays -- these may release small objects */
/* +1's are because we must pass a pointer to the data, not the header */
while (cinfo->small_sarray_list != NULL) {
if ((d_free_small_sarray(cinfo,
(JSAMPARRAY) (cinfo->small_sarray_list + 1))) < 0)
return(XIE_ERR);
}
while (cinfo->small_barray_list != NULL) {
if ((d_free_small_barray(cinfo,
(JBLOCKARRAY) (cinfo->small_barray_list + 1))) < 0)
return(XIE_ERR);
}
/* Free any remaining small objects */
while (cinfo->small_list != NULL) {
if ((d_free_small(cinfo,
(pointer ) (cinfo->small_list + 1))) < 0)
return(XIE_ERR);
}
jmem_term(); /* system-dependent cleanup */
return(XIE_NRML);
}
#else
METHODDEF void
free_all (void)
{
/* First free any open "big" arrays -- these may release small arrays */
while (big_sarray_list != NULL)
free_big_sarray(big_sarray_list);
while (big_barray_list != NULL)
free_big_barray(big_barray_list);
/* Free any open small arrays -- these may release small objects */
/* +1's are because we must pass a pointer to the data, not the header */
while (small_sarray_list != NULL)
free_small_sarray((JSAMPARRAY) (small_sarray_list + 1));
while (small_barray_list != NULL)
free_small_barray((JBLOCKARRAY) (small_barray_list + 1));
/* Free any remaining small objects */
while (small_list != NULL)
free_small((pointer ) (small_list + 1));
#ifdef NEED_ALLOC_MEDIUM
while (medium_list != NULL)
free_medium((pointer) (medium_list + 1));
#endif
jmem_term(); /* system-dependent cleanup */
#ifdef MEM_STATS
if (methods->trace_level > 0)
print_mem_stats(); /* print optional memory usage statistics */
#endif
}
#endif /* XIE_SUPPORTED */
/*
* The method selection routine for virtual memory systems.
* The system-dependent setup routine should call this routine
* to install the necessary method pointers in the supplied struct.
*/
#ifdef XIE_SUPPORTED
GLOBAL void
#if NeedFunctionPrototypes
jcselmemmgr (compress_info_ptr cinfo, external_methods_ptr emethods)
#else
jcselmemmgr (cinfo, emethods)
compress_info_ptr cinfo;
external_methods_ptr emethods;
#endif /* NeedFunctionPrototypes */
{
emethods->c_alloc_small = c_alloc_small;
emethods->c_free_small = c_free_small;
emethods->c_alloc_small_sarray = c_alloc_small_sarray;
emethods->c_free_small_sarray = c_free_small_sarray;
emethods->c_free_all = c_free_all;
/* Initialize list headers to empty */
cinfo->small_list = NULL;
cinfo->small_sarray_list = NULL;
cinfo->small_barray_list = NULL;
emethods->max_memory_to_use = 0;
}
GLOBAL void
#if NeedFunctionPrototypes
jdselmemmgr (decompress_info_ptr cinfo, external_methods_ptr emethods)
#else
jdselmemmgr (cinfo, emethods)
decompress_info_ptr cinfo;
external_methods_ptr emethods;
#endif /* NeedFunctionPrototypes */
{
emethods->d_alloc_small = d_alloc_small;
emethods->d_free_small = d_free_small;
emethods->d_alloc_small_sarray = d_alloc_small_sarray;
emethods->d_free_small_sarray = d_free_small_sarray;
emethods->d_alloc_small_barray = d_alloc_small_barray;
emethods->d_free_small_barray = d_free_small_barray;
emethods->d_free_all = d_free_all;
/* Initialize list headers to empty */
cinfo->small_list = NULL;
cinfo->small_sarray_list = NULL;
cinfo->small_barray_list = NULL;
emethods->max_memory_to_use = 0;
}
#else
GLOBAL void
jselmemmgr (external_methods_ptr emethods)
{
methods = emethods; /* save struct addr for error exit access */
emethods->alloc_small = alloc_small;
emethods->free_small = free_small;
#ifdef NEED_ALLOC_MEDIUM
emethods->alloc_medium = alloc_medium;
emethods->free_medium = free_medium;
#else
emethods->alloc_medium = alloc_small;
emethods->free_medium = free_small;
#endif
emethods->alloc_small_sarray = alloc_small_sarray;
emethods->free_small_sarray = free_small_sarray;
emethods->alloc_small_barray = alloc_small_barray;
emethods->free_small_barray = free_small_barray;
emethods->request_big_sarray = request_big_sarray;
emethods->request_big_barray = request_big_barray;
emethods->alloc_big_arrays = alloc_big_arrays;
emethods->access_big_sarray = access_big_sarray;
emethods->access_big_barray = access_big_barray;
emethods->free_big_sarray = free_big_sarray;
emethods->free_big_barray = free_big_barray;
emethods->free_all = free_all;
/* Initialize list headers to empty */
small_list = NULL;
#ifdef NEED_ALLOC_MEDIUM
medium_list = NULL;
#endif
small_sarray_list = NULL;
small_barray_list = NULL;
big_sarray_list = NULL;
big_barray_list = NULL;
jmem_init(emethods); /* system-dependent initialization */
/* Check for an environment variable JPEGMEM; if found, override the
* default max_memory setting from jmem_init. Note that a command line
* -m argument may again override this value.
* If your system doesn't support getenv(), define NO_GETENV to disable
* this feature.
*/
#ifndef NO_GETENV
{ char * memenv;
if ((memenv = getenv("JPEGMEM")) != NULL) {
long lval;
char ch = 'x';
if (sscanf(memenv, "%ld%c", &lval, &ch) > 0) {
if (ch == 'm' || ch == 'M')
lval *= 1000L;
emethods->max_memory_to_use = lval * 1000L;
}
}
}
#endif
}
#endif /* XIE_SUPPORTED */
|