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
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "alloc_cache.hxx"
#include "alloc_impl.hxx"
#include "alloc_arena.hxx"
#include "internal/rtllifecycle.h"
#include "sal/macros.h"
#include "osl/diagnose.h"
#include <osl/thread.hxx>
#include <cassert>
#include <string.h>
#include <stdio.h>
extern AllocMode alloc_mode;
/* ================================================================= *
*
* cache internals.
*
* ================================================================= */
/** g_cache_list
* @internal
*/
struct rtl_cache_list_st
{
rtl_memory_lock_type m_lock;
rtl_cache_type m_cache_head;
#if defined(SAL_UNX)
pthread_t m_update_thread;
pthread_cond_t m_update_cond;
#elif defined(SAL_W32)
HANDLE m_update_thread;
HANDLE m_update_cond;
#endif /* SAL_UNX || SAL_W32 */
int m_update_done;
};
static rtl_cache_list_st g_cache_list;
/** gp_cache_arena
* provided for cache_type allocations, and hash_table resizing.
*
* @internal
*/
static rtl_arena_type * gp_cache_arena = 0;
/** gp_cache_magazine_cache
* @internal
*/
static rtl_cache_type * gp_cache_magazine_cache = 0;
/** gp_cache_slab_cache
* @internal
*/
static rtl_cache_type * gp_cache_slab_cache = 0;
/** gp_cache_bufctl_cache
* @internal
*/
static rtl_cache_type * gp_cache_bufctl_cache = 0;
/* ================================================================= */
/** RTL_CACHE_HASH_INDEX()
*/
#define RTL_CACHE_HASH_INDEX_IMPL(a, s, q, m) \
((((a) + ((a) >> (s)) + ((a) >> ((s) << 1))) >> (q)) & (m))
#define RTL_CACHE_HASH_INDEX(cache, addr) \
RTL_CACHE_HASH_INDEX_IMPL((addr), (cache)->m_hash_shift, (cache)->m_type_shift, ((cache)->m_hash_size - 1))
namespace
{
void
rtl_cache_hash_rescale (
rtl_cache_type * cache,
sal_Size new_size
)
{
rtl_cache_bufctl_type ** new_table;
sal_Size new_bytes;
new_bytes = new_size * sizeof(rtl_cache_bufctl_type*);
new_table = (rtl_cache_bufctl_type**)rtl_arena_alloc(gp_cache_arena, &new_bytes);
if (new_table != 0)
{
rtl_cache_bufctl_type ** old_table;
sal_Size old_size, i;
memset (new_table, 0, new_bytes);
RTL_MEMORY_LOCK_ACQUIRE(&(cache->m_slab_lock));
old_table = cache->m_hash_table;
old_size = cache->m_hash_size;
// SAL_INFO(
// "sal.rtl",
// "rtl_cache_hash_rescale(" << cache->m_name << "): nbuf: "
// << (cache->m_slab_stats.m_alloc - cache->m_slab_stats.m_free)
// << " (ave: "
// << ((cache->m_slab_stats.m_alloc - cache->m_slab_stats.m_free)
// >> cache->m_hash_shift)
// << "), frees: " << cache->m_slab_stats.m_free << " [old_size: "
// << old_size << ", new_size: " << new_size << ']');
cache->m_hash_table = new_table;
cache->m_hash_size = new_size;
cache->m_hash_shift = highbit(cache->m_hash_size) - 1;
for (i = 0; i < old_size; i++)
{
rtl_cache_bufctl_type * curr = old_table[i];
while (curr != 0)
{
rtl_cache_bufctl_type * next = curr->m_next;
rtl_cache_bufctl_type ** head;
// coverity[negative_shift]
head = &(cache->m_hash_table[RTL_CACHE_HASH_INDEX(cache, curr->m_addr)]);
curr->m_next = (*head);
(*head) = curr;
curr = next;
}
old_table[i] = 0;
}
RTL_MEMORY_LOCK_RELEASE(&(cache->m_slab_lock));
if (old_table != cache->m_hash_table_0)
{
sal_Size old_bytes = old_size * sizeof(rtl_cache_bufctl_type*);
rtl_arena_free (gp_cache_arena, old_table, old_bytes);
}
}
}
inline sal_uIntPtr
rtl_cache_hash_insert (
rtl_cache_type * cache,
rtl_cache_bufctl_type * bufctl
)
{
rtl_cache_bufctl_type ** ppHead;
ppHead = &(cache->m_hash_table[RTL_CACHE_HASH_INDEX(cache, bufctl->m_addr)]);
bufctl->m_next = (*ppHead);
(*ppHead) = bufctl;
return (bufctl->m_addr);
}
/** rtl_cache_hash_remove()
*/
rtl_cache_bufctl_type *
rtl_cache_hash_remove (
rtl_cache_type * cache,
sal_uIntPtr addr
)
{
rtl_cache_bufctl_type ** ppHead;
rtl_cache_bufctl_type * bufctl;
sal_Size lookups = 0;
ppHead = &(cache->m_hash_table[RTL_CACHE_HASH_INDEX(cache, addr)]);
while ((bufctl = *ppHead) != 0)
{
if (bufctl->m_addr == addr)
{
*ppHead = bufctl->m_next, bufctl->m_next = 0;
break;
}
lookups += 1;
ppHead = &(bufctl->m_next);
}
assert(bufctl != 0); // bad free
if (lookups > 1)
{
sal_Size nbuf = (sal_Size)(cache->m_slab_stats.m_alloc - cache->m_slab_stats.m_free);
if (nbuf > 4 * cache->m_hash_size)
{
if (!(cache->m_features & RTL_CACHE_FEATURE_RESCALE))
{
sal_Size ave = nbuf >> cache->m_hash_shift;
// coverity[negative_shift]
sal_Size new_size = cache->m_hash_size << (highbit(ave) - 1);
cache->m_features |= RTL_CACHE_FEATURE_RESCALE;
RTL_MEMORY_LOCK_RELEASE(&(cache->m_slab_lock));
rtl_cache_hash_rescale (cache, new_size);
RTL_MEMORY_LOCK_ACQUIRE(&(cache->m_slab_lock));
cache->m_features &= ~RTL_CACHE_FEATURE_RESCALE;
}
}
}
return (bufctl);
}
/* ================================================================= */
/** RTL_CACHE_SLAB()
*/
#define RTL_CACHE_SLAB(addr, size) \
(((rtl_cache_slab_type*)(RTL_MEMORY_P2END((sal_uIntPtr)(addr), (size)))) - 1)
/** rtl_cache_slab_constructor()
*/
int
rtl_cache_slab_constructor (void * obj, SAL_UNUSED_PARAMETER void *)
{
rtl_cache_slab_type * slab = (rtl_cache_slab_type*)(obj);
QUEUE_START_NAMED(slab, slab_);
slab->m_ntypes = 0;
return (1);
}
/** rtl_cache_slab_destructor()
*/
void
rtl_cache_slab_destructor (void * obj, SAL_UNUSED_PARAMETER void *)
{
rtl_cache_slab_type * slab = static_cast< rtl_cache_slab_type * >(obj);
assert(QUEUE_STARTED_NAMED(slab, slab_)); // assure removed from queue(s)
assert(slab->m_ntypes == 0); // assure no longer referenced
(void) slab; // avoid warnings
}
/** rtl_cache_slab_create()
*
* @precond cache->m_slab_lock released.
*/
rtl_cache_slab_type *
rtl_cache_slab_create (
rtl_cache_type * cache
)
{
rtl_cache_slab_type * slab = 0;
void * addr;
sal_Size size;
size = cache->m_slab_size;
addr = rtl_arena_alloc (cache->m_source, &size);
if (addr != 0)
{
assert(size >= cache->m_slab_size);
if (cache->m_features & RTL_CACHE_FEATURE_HASH)
{
/* allocate slab struct from slab cache */
assert(cache != gp_cache_slab_cache);
slab = (rtl_cache_slab_type*)rtl_cache_alloc (gp_cache_slab_cache);
}
else
{
/* construct embedded slab struct */
slab = RTL_CACHE_SLAB(addr, cache->m_slab_size);
(void) rtl_cache_slab_constructor (slab, 0);
}
if (slab != 0)
{
slab->m_data = (sal_uIntPtr)(addr);
/* dynamic freelist initialization */
slab->m_bp = slab->m_data;
slab->m_sp = 0;
}
else
{
rtl_arena_free (cache->m_source, addr, size);
}
}
return (slab);
}
/** rtl_cache_slab_destroy()
*
* @precond cache->m_slab_lock released.
*/
void
rtl_cache_slab_destroy (
rtl_cache_type * cache,
rtl_cache_slab_type * slab
)
{
void * addr = (void*)(slab->m_data);
sal_Size refcnt = slab->m_ntypes; slab->m_ntypes = 0;
if (cache->m_features & RTL_CACHE_FEATURE_HASH)
{
/* cleanup bufctl(s) for free buffer(s) */
sal_Size ntypes = (slab->m_bp - slab->m_data) / cache->m_type_size;
for (ntypes -= refcnt; slab->m_sp != 0; ntypes--)
{
rtl_cache_bufctl_type * bufctl = slab->m_sp;
/* pop from freelist */
slab->m_sp = bufctl->m_next, bufctl->m_next = 0;
/* return bufctl struct to bufctl cache */
rtl_cache_free (gp_cache_bufctl_cache, bufctl);
}
assert(ntypes == 0);
/* return slab struct to slab cache */
rtl_cache_free (gp_cache_slab_cache, slab);
}
else
{
/* destruct embedded slab struct */
rtl_cache_slab_destructor (slab, 0);
}
if ((refcnt == 0) || (cache->m_features & RTL_CACHE_FEATURE_BULKDESTROY))
{
/* free memory */
rtl_arena_free (cache->m_source, addr, cache->m_slab_size);
}
}
/** rtl_cache_slab_populate()
*
* @precond cache->m_slab_lock acquired.
*/
bool
rtl_cache_slab_populate (
rtl_cache_type * cache
)
{
rtl_cache_slab_type * slab;
RTL_MEMORY_LOCK_RELEASE(&(cache->m_slab_lock));
slab = rtl_cache_slab_create (cache);
RTL_MEMORY_LOCK_ACQUIRE(&(cache->m_slab_lock));
if (slab != 0)
{
/* update buffer start addr w/ current color */
slab->m_bp += cache->m_ncolor;
/* update color for next slab */
cache->m_ncolor += cache->m_type_align;
if (cache->m_ncolor > cache->m_ncolor_max)
cache->m_ncolor = 0;
/* update stats */
cache->m_slab_stats.m_mem_total += cache->m_slab_size;
/* insert onto 'free' queue */
QUEUE_INSERT_HEAD_NAMED(&(cache->m_free_head), slab, slab_);
}
return (slab != 0);
}
/* ================================================================= */
/** rtl_cache_slab_alloc()
*
* Allocate a buffer from slab layer; used by magazine layer.
*/
void *
rtl_cache_slab_alloc (
rtl_cache_type * cache
)
{
void * addr = 0;
rtl_cache_slab_type * head;
RTL_MEMORY_LOCK_ACQUIRE(&(cache->m_slab_lock));
head = &(cache->m_free_head);
if ((head->m_slab_next != head) || rtl_cache_slab_populate (cache))
{
rtl_cache_slab_type * slab;
rtl_cache_bufctl_type * bufctl;
slab = head->m_slab_next;
assert(slab->m_ntypes < cache->m_ntypes);
if (slab->m_sp == 0)
{
/* initialize bufctl w/ current 'slab->m_bp' */
assert(slab->m_bp < slab->m_data + cache->m_ntypes * cache->m_type_size + cache->m_ncolor_max);
if (cache->m_features & RTL_CACHE_FEATURE_HASH)
{
/* allocate bufctl */
assert(cache != gp_cache_bufctl_cache);
bufctl = (rtl_cache_bufctl_type*)rtl_cache_alloc (gp_cache_bufctl_cache);
if (bufctl == 0)
{
/* out of memory */
RTL_MEMORY_LOCK_RELEASE(&(cache->m_slab_lock));
return (0);
}
bufctl->m_addr = slab->m_bp;
bufctl->m_slab = (sal_uIntPtr)(slab);
}
else
{
/* embedded bufctl */
bufctl = (rtl_cache_bufctl_type*)(slab->m_bp);
}
bufctl->m_next = 0;
/* update 'slab->m_bp' to next free buffer */
slab->m_bp += cache->m_type_size;
/* assign bufctl to freelist */
slab->m_sp = bufctl;
}
/* pop front */
bufctl = slab->m_sp;
slab->m_sp = bufctl->m_next;
/* increment usage, check for full slab */
if ((slab->m_ntypes += 1) == cache->m_ntypes)
{
/* remove from 'free' queue */
QUEUE_REMOVE_NAMED(slab, slab_);
/* insert onto 'used' queue (tail) */
QUEUE_INSERT_TAIL_NAMED(&(cache->m_used_head), slab, slab_);
}
/* update stats */
cache->m_slab_stats.m_alloc += 1;
cache->m_slab_stats.m_mem_alloc += cache->m_type_size;
if (cache->m_features & RTL_CACHE_FEATURE_HASH)
addr = (void*)rtl_cache_hash_insert (cache, bufctl);
else
addr = bufctl;
}
RTL_MEMORY_LOCK_RELEASE(&(cache->m_slab_lock));
return (addr);
}
/** rtl_cache_slab_free()
*
* Return a buffer to slab layer; used by magazine layer.
*/
void
rtl_cache_slab_free (
rtl_cache_type * cache,
void * addr
)
{
rtl_cache_bufctl_type * bufctl;
rtl_cache_slab_type * slab;
RTL_MEMORY_LOCK_ACQUIRE(&(cache->m_slab_lock));
/* determine slab from addr */
if (cache->m_features & RTL_CACHE_FEATURE_HASH)
{
bufctl = rtl_cache_hash_remove (cache, (sal_uIntPtr)(addr));
slab = (bufctl != 0) ? (rtl_cache_slab_type*)(bufctl->m_slab) : 0;
}
else
{
/* embedded slab struct */
bufctl = (rtl_cache_bufctl_type*)(addr);
slab = RTL_CACHE_SLAB(addr, cache->m_slab_size);
}
if (slab != 0)
{
/* check for full slab */
if (slab->m_ntypes == cache->m_ntypes)
{
/* remove from 'used' queue */
QUEUE_REMOVE_NAMED(slab, slab_);
/* insert onto 'free' queue (head) */
QUEUE_INSERT_HEAD_NAMED(&(cache->m_free_head), slab, slab_);
}
/* push front */
bufctl->m_next = slab->m_sp;
slab->m_sp = bufctl;
/* update stats */
cache->m_slab_stats.m_free += 1;
cache->m_slab_stats.m_mem_alloc -= cache->m_type_size;
/* decrement usage, check for empty slab */
if ((slab->m_ntypes -= 1) == 0)
{
/* remove from 'free' queue */
QUEUE_REMOVE_NAMED(slab, slab_);
/* update stats */
cache->m_slab_stats.m_mem_total -= cache->m_slab_size;
/* free 'empty' slab */
RTL_MEMORY_LOCK_RELEASE(&(cache->m_slab_lock));
rtl_cache_slab_destroy (cache, slab);
return;
}
}
RTL_MEMORY_LOCK_RELEASE(&(cache->m_slab_lock));
}
/* ================================================================= */
/** rtl_cache_magazine_constructor()
*/
int
rtl_cache_magazine_constructor (void * obj, SAL_UNUSED_PARAMETER void *)
{
rtl_cache_magazine_type * mag = (rtl_cache_magazine_type*)(obj);
/* @@@ sal_Size size = (sal_Size)(arg); @@@ */
mag->m_mag_next = 0;
mag->m_mag_size = RTL_CACHE_MAGAZINE_SIZE;
mag->m_mag_used = 0;
return (1);
}
/** rtl_cache_magazine_destructor()
*/
void
rtl_cache_magazine_destructor (void * obj, SAL_UNUSED_PARAMETER void *)
{
rtl_cache_magazine_type * mag = static_cast< rtl_cache_magazine_type * >(
obj);
assert(mag->m_mag_next == 0); // assure removed from queue(s)
assert(mag->m_mag_used == 0); // assure no longer referenced
(void) mag; // avoid warnings
}
/** rtl_cache_magazine_clear()
*/
void
rtl_cache_magazine_clear (
rtl_cache_type * cache,
rtl_cache_magazine_type * mag
)
{
for (; mag->m_mag_used > 0; --mag->m_mag_used)
{
void * obj = mag->m_objects[mag->m_mag_used - 1];
mag->m_objects[mag->m_mag_used - 1] = 0;
if (cache->m_destructor != 0)
{
/* destruct object */
(cache->m_destructor)(obj, cache->m_userarg);
}
/* return buffer to slab layer */
rtl_cache_slab_free (cache, obj);
}
}
/* ================================================================= */
/** rtl_cache_depot_enqueue()
*
* @precond cache->m_depot_lock acquired.
*/
inline void
rtl_cache_depot_enqueue (
rtl_cache_depot_type * depot,
rtl_cache_magazine_type * mag
)
{
/* enqueue empty magazine */
mag->m_mag_next = depot->m_mag_next;
depot->m_mag_next = mag;
/* update depot stats */
depot->m_mag_count++;
}
/** rtl_cache_depot_dequeue()
*
* @precond cache->m_depot_lock acquired.
*/
inline rtl_cache_magazine_type *
rtl_cache_depot_dequeue (
rtl_cache_depot_type * depot
)
{
rtl_cache_magazine_type * mag = 0;
if (depot->m_mag_count > 0)
{
/* dequeue magazine */
assert(depot->m_mag_next != 0);
mag = depot->m_mag_next;
depot->m_mag_next = mag->m_mag_next;
mag->m_mag_next = 0;
/* update depot stats */
depot->m_mag_count--;
if(depot->m_curr_min > depot->m_mag_count)
{
depot->m_curr_min = depot->m_mag_count;
}
}
return (mag);
}
/** rtl_cache_depot_exchange_alloc()
*
* @precond cache->m_depot_lock acquired.
*/
inline rtl_cache_magazine_type *
rtl_cache_depot_exchange_alloc (
rtl_cache_type * cache,
rtl_cache_magazine_type * empty
)
{
rtl_cache_magazine_type * full;
assert((empty == 0) || (empty->m_mag_used == 0));
/* dequeue full magazine */
full = rtl_cache_depot_dequeue (&(cache->m_depot_full));
if ((full != 0) && (empty != 0))
{
/* enqueue empty magazine */
rtl_cache_depot_enqueue (&(cache->m_depot_empty), empty);
}
assert((full == 0) || (full->m_mag_used > 0));
return (full);
}
/** rtl_cache_depot_exchange_free()
*
* @precond cache->m_depot_lock acquired.
*/
inline rtl_cache_magazine_type *
rtl_cache_depot_exchange_free (
rtl_cache_type * cache,
rtl_cache_magazine_type * full
)
{
rtl_cache_magazine_type * empty;
assert((full == 0) || (full->m_mag_used > 0));
/* dequeue empty magazine */
empty = rtl_cache_depot_dequeue (&(cache->m_depot_empty));
if ((empty != 0) && (full != 0))
{
/* enqueue full magazine */
rtl_cache_depot_enqueue (&(cache->m_depot_full), full);
}
assert((empty == 0) || (empty->m_mag_used == 0));
return (empty);
}
/** rtl_cache_depot_populate()
*
* @precond cache->m_depot_lock acquired.
*/
bool
rtl_cache_depot_populate (
rtl_cache_type * cache
)
{
rtl_cache_magazine_type * empty = 0;
if (cache->m_magazine_cache != 0)
{
/* allocate new empty magazine */
RTL_MEMORY_LOCK_RELEASE(&(cache->m_depot_lock));
empty = (rtl_cache_magazine_type*)rtl_cache_alloc (cache->m_magazine_cache);
RTL_MEMORY_LOCK_ACQUIRE(&(cache->m_depot_lock));
if (empty != 0)
{
/* enqueue (new) empty magazine */
rtl_cache_depot_enqueue (&(cache->m_depot_empty), empty);
}
}
return (empty != 0);
}
/* ================================================================= */
/** rtl_cache_constructor()
*/
int
rtl_cache_constructor (void * obj)
{
rtl_cache_type * cache = (rtl_cache_type*)(obj);
memset (cache, 0, sizeof(rtl_cache_type));
/* linkage */
QUEUE_START_NAMED(cache, cache_);
/* slab layer */
(void)RTL_MEMORY_LOCK_INIT(&(cache->m_slab_lock));
QUEUE_START_NAMED(&(cache->m_free_head), slab_);
QUEUE_START_NAMED(&(cache->m_used_head), slab_);
cache->m_hash_table = cache->m_hash_table_0;
cache->m_hash_size = RTL_CACHE_HASH_SIZE;
cache->m_hash_shift = highbit(cache->m_hash_size) - 1;
/* depot layer */
(void)RTL_MEMORY_LOCK_INIT(&(cache->m_depot_lock));
return (1);
}
/** rtl_cache_destructor()
*/
void
rtl_cache_destructor (void * obj)
{
rtl_cache_type * cache = (rtl_cache_type*)(obj);
/* linkage */
assert(QUEUE_STARTED_NAMED(cache, cache_));
/* slab layer */
(void)RTL_MEMORY_LOCK_DESTROY(&(cache->m_slab_lock));
assert(QUEUE_STARTED_NAMED(&(cache->m_free_head), slab_));
assert(QUEUE_STARTED_NAMED(&(cache->m_used_head), slab_));
assert(cache->m_hash_table == cache->m_hash_table_0);
assert(cache->m_hash_size == RTL_CACHE_HASH_SIZE);
assert(cache->m_hash_shift == (sal_Size)(highbit(cache->m_hash_size) - 1));
/* depot layer */
(void)RTL_MEMORY_LOCK_DESTROY(&(cache->m_depot_lock));
}
/* ================================================================= */
/** rtl_cache_activate()
*/
rtl_cache_type *
rtl_cache_activate (
rtl_cache_type * cache,
const char * name,
size_t objsize,
size_t objalign,
int (SAL_CALL * constructor)(void * obj, void * userarg),
void (SAL_CALL * destructor) (void * obj, void * userarg),
void (SAL_CALL * reclaim) (void * userarg),
void * userarg,
rtl_arena_type * source,
int flags
)
{
assert(cache != 0);
if (cache != 0)
{
sal_Size slabsize;
snprintf (cache->m_name, sizeof(cache->m_name), "%s", name);
/* ensure minimum size (embedded bufctl linkage) */
if(objsize < sizeof(rtl_cache_bufctl_type*))
{
objsize = sizeof(rtl_cache_bufctl_type*);
}
if (objalign == 0)
{
/* determine default alignment */
if (objsize >= RTL_MEMORY_ALIGNMENT_8)
objalign = RTL_MEMORY_ALIGNMENT_8;
else
objalign = RTL_MEMORY_ALIGNMENT_4;
}
else
{
/* ensure minimum alignment */
if(objalign < RTL_MEMORY_ALIGNMENT_4)
{
objalign = RTL_MEMORY_ALIGNMENT_4;
}
}
assert(RTL_MEMORY_ISP2(objalign));
cache->m_type_size = objsize = RTL_MEMORY_P2ROUNDUP(objsize, objalign);
cache->m_type_align = objalign;
cache->m_type_shift = highbit(cache->m_type_size) - 1;
cache->m_constructor = constructor;
cache->m_destructor = destructor;
cache->m_reclaim = reclaim;
cache->m_userarg = userarg;
/* slab layer */
cache->m_source = source;
slabsize = source->m_quantum; /* minimum slab size */
if (flags & RTL_CACHE_FLAG_QUANTUMCACHE)
{
/* next power of 2 above 3 * qcache_max */
if(slabsize < (((sal_Size)1) << highbit(3 * source->m_qcache_max)))
{
slabsize = (((sal_Size)1) << highbit(3 * source->m_qcache_max));
}
}
else
{
/* waste at most 1/8 of slab */
if(slabsize < cache->m_type_size * 8)
{
slabsize = cache->m_type_size * 8;
}
}
slabsize = RTL_MEMORY_P2ROUNDUP(slabsize, source->m_quantum);
if (!RTL_MEMORY_ISP2(slabsize))
slabsize = (((sal_Size)1) << highbit(slabsize));
cache->m_slab_size = slabsize;
if (cache->m_slab_size > source->m_quantum)
{
assert(gp_cache_slab_cache != 0);
assert(gp_cache_bufctl_cache != 0);
cache->m_features |= RTL_CACHE_FEATURE_HASH;
cache->m_ntypes = cache->m_slab_size / cache->m_type_size;
cache->m_ncolor_max = cache->m_slab_size % cache->m_type_size;
}
else
{
/* embedded slab struct */
cache->m_ntypes = (cache->m_slab_size - sizeof(rtl_cache_slab_type)) / cache->m_type_size;
cache->m_ncolor_max = (cache->m_slab_size - sizeof(rtl_cache_slab_type)) % cache->m_type_size;
}
assert(cache->m_ntypes > 0);
cache->m_ncolor = 0;
if (flags & RTL_CACHE_FLAG_BULKDESTROY)
{
/* allow bulk slab delete upon cache deactivation */
cache->m_features |= RTL_CACHE_FEATURE_BULKDESTROY;
}
/* magazine layer */
if (!(flags & RTL_CACHE_FLAG_NOMAGAZINE))
{
assert(gp_cache_magazine_cache != 0);
cache->m_magazine_cache = gp_cache_magazine_cache;
}
/* insert into cache list */
RTL_MEMORY_LOCK_ACQUIRE(&(g_cache_list.m_lock));
QUEUE_INSERT_TAIL_NAMED(&(g_cache_list.m_cache_head), cache, cache_);
RTL_MEMORY_LOCK_RELEASE(&(g_cache_list.m_lock));
}
return (cache);
}
/** rtl_cache_deactivate()
*/
void
rtl_cache_deactivate (
rtl_cache_type * cache
)
{
/* remove from cache list */
RTL_MEMORY_LOCK_ACQUIRE(&(g_cache_list.m_lock));
bool active = !QUEUE_STARTED_NAMED(cache, cache_);
QUEUE_REMOVE_NAMED(cache, cache_);
RTL_MEMORY_LOCK_RELEASE(&(g_cache_list.m_lock));
assert(active); // orphaned cache
(void)active;
/* cleanup magazine layer */
if (cache->m_magazine_cache != 0)
{
rtl_cache_type * mag_cache;
rtl_cache_magazine_type * mag;
/* prevent recursion */
mag_cache = cache->m_magazine_cache, cache->m_magazine_cache = 0;
/* cleanup cpu layer */
if ((mag = cache->m_cpu_curr) != 0)
{
// coverity[missing_lock]
cache->m_cpu_curr = 0;
rtl_cache_magazine_clear (cache, mag);
rtl_cache_free (mag_cache, mag);
}
if ((mag = cache->m_cpu_prev) != 0)
{
// coverity[missing_lock]
cache->m_cpu_prev = 0;
rtl_cache_magazine_clear (cache, mag);
rtl_cache_free (mag_cache, mag);
}
/* cleanup depot layer */
while ((mag = rtl_cache_depot_dequeue(&(cache->m_depot_full))) != 0)
{
rtl_cache_magazine_clear (cache, mag);
rtl_cache_free (mag_cache, mag);
}
while ((mag = rtl_cache_depot_dequeue(&(cache->m_depot_empty))) != 0)
{
rtl_cache_magazine_clear (cache, mag);
rtl_cache_free (mag_cache, mag);
}
}
// SAL_INFO(
// "sal.rtl",
// "rtl_cache_deactivate(" << cache->m_name << "): [slab]: allocs: "
// << cache->m_slab_stats.m_alloc << ", frees: "
// << cache->m_slab_stats.m_free << "; total: "
// << cache->m_slab_stats.m_mem_total << ", used: "
// << cache->m_slab_stats.m_mem_alloc << "; [cpu]: allocs: "
// << cache->m_cpu_stats.m_alloc << ", frees: "
// << cache->m_cpu_stats.m_free << "; [total]: allocs: "
// << (cache->m_slab_stats.m_alloc + cache->m_cpu_stats.m_alloc)
// << ", frees: "
// << (cache->m_slab_stats.m_free + cache->m_cpu_stats.m_free));
/* cleanup slab layer */
if (cache->m_slab_stats.m_alloc > cache->m_slab_stats.m_free)
{
// SAL_INFO(
// "sal.rtl",
// "rtl_cache_deactivate(" << cache->m_name << "): cleaning up "
// << (cache->m_slab_stats.m_alloc - cache->m_slab_stats.m_free)
// << " leaked buffer(s) [" << cache->m_slab_stats.m_mem_alloc
// << " bytes] [" << cache->m_slab_stats.m_mem_total << " total]");
if (cache->m_features & RTL_CACHE_FEATURE_HASH)
{
/* cleanup bufctl(s) for leaking buffer(s) */
sal_Size i, n = cache->m_hash_size;
for (i = 0; i < n; i++)
{
rtl_cache_bufctl_type * bufctl;
while ((bufctl = cache->m_hash_table[i]) != 0)
{
/* pop from hash table */
cache->m_hash_table[i] = bufctl->m_next, bufctl->m_next = 0;
/* return to bufctl cache */
rtl_cache_free (gp_cache_bufctl_cache, bufctl);
}
}
}
{
/* force cleanup of remaining slabs */
rtl_cache_slab_type *head, *slab;
head = &(cache->m_used_head);
for (slab = head->m_slab_next; slab != head; slab = head->m_slab_next)
{
/* remove from 'used' queue */
QUEUE_REMOVE_NAMED(slab, slab_);
/* update stats */
cache->m_slab_stats.m_mem_total -= cache->m_slab_size;
/* free slab */
rtl_cache_slab_destroy (cache, slab);
}
head = &(cache->m_free_head);
for (slab = head->m_slab_next; slab != head; slab = head->m_slab_next)
{
/* remove from 'free' queue */
QUEUE_REMOVE_NAMED(slab, slab_);
/* update stats */
cache->m_slab_stats.m_mem_total -= cache->m_slab_size;
/* free slab */
rtl_cache_slab_destroy (cache, slab);
}
}
}
if (cache->m_hash_table != cache->m_hash_table_0)
{
rtl_arena_free (
gp_cache_arena,
cache->m_hash_table,
cache->m_hash_size * sizeof(rtl_cache_bufctl_type*));
cache->m_hash_table = cache->m_hash_table_0;
cache->m_hash_size = RTL_CACHE_HASH_SIZE;
cache->m_hash_shift = highbit(cache->m_hash_size) - 1;
}
}
} //namespace
/* ================================================================= *
*
* cache implementation.
*
* ================================================================= */
/** rtl_cache_create()
*/
rtl_cache_type *
SAL_CALL rtl_cache_create (
const char * name,
sal_Size objsize,
sal_Size objalign,
int (SAL_CALL * constructor)(void * obj, void * userarg),
void (SAL_CALL * destructor) (void * obj, void * userarg),
void (SAL_CALL * reclaim) (void * userarg),
void * userarg,
rtl_arena_type * source,
int flags
) SAL_THROW_EXTERN_C()
{
rtl_cache_type * result = 0;
sal_Size size = sizeof(rtl_cache_type);
try_alloc:
result = (rtl_cache_type*)rtl_arena_alloc (gp_cache_arena, &size);
if (result != 0)
{
rtl_cache_type * cache = result;
(void) rtl_cache_constructor (cache);
if (!source)
{
/* use default arena */
assert(gp_default_arena != 0);
source = gp_default_arena;
}
result = rtl_cache_activate (
cache,
name,
objsize,
objalign,
constructor,
destructor,
reclaim,
userarg,
source,
flags
);
if (result == 0)
{
/* activation failed */
rtl_cache_deactivate (cache);
rtl_cache_destructor (cache);
rtl_arena_free (gp_cache_arena, cache, size);
}
}
else if (gp_cache_arena == 0)
{
ensureCacheSingleton();
if (gp_cache_arena)
{
/* try again */
goto try_alloc;
}
}
return (result);
}
/** rtl_cache_destroy()
*/
void SAL_CALL rtl_cache_destroy (
rtl_cache_type * cache
) SAL_THROW_EXTERN_C()
{
if (cache != 0)
{
rtl_cache_deactivate (cache);
rtl_cache_destructor (cache);
rtl_arena_free (gp_cache_arena, cache, sizeof(rtl_cache_type));
}
}
/** rtl_cache_alloc()
*/
void *
SAL_CALL rtl_cache_alloc (
rtl_cache_type * cache
) SAL_THROW_EXTERN_C()
{
void * obj = 0;
if (cache == 0)
return (0);
if (alloc_mode == AMode_SYSTEM)
{
obj = rtl_allocateMemory(cache->m_type_size);
if ((obj != 0) && (cache->m_constructor != 0))
{
if (!((cache->m_constructor)(obj, cache->m_userarg)))
{
/* construction failure */
rtl_freeMemory(obj), obj = 0;
}
}
return obj;
}
RTL_MEMORY_LOCK_ACQUIRE(&(cache->m_depot_lock));
if (cache->m_cpu_curr != 0)
{
for (;;)
{
/* take object from magazine layer */
rtl_cache_magazine_type *curr, *prev, *temp;
curr = cache->m_cpu_curr;
if ((curr != 0) && (curr->m_mag_used > 0))
{
obj = curr->m_objects[--curr->m_mag_used];
cache->m_cpu_stats.m_alloc += 1;
RTL_MEMORY_LOCK_RELEASE(&(cache->m_depot_lock));
return (obj);
}
prev = cache->m_cpu_prev;
if ((prev != 0) && (prev->m_mag_used > 0))
{
temp = cache->m_cpu_curr;
cache->m_cpu_curr = cache->m_cpu_prev;
cache->m_cpu_prev = temp;
continue;
}
temp = rtl_cache_depot_exchange_alloc (cache, prev);
if (temp != 0)
{
cache->m_cpu_prev = cache->m_cpu_curr;
cache->m_cpu_curr = temp;
continue;
}
/* no full magazine: fall through to slab layer */
break;
}
}
RTL_MEMORY_LOCK_RELEASE(&(cache->m_depot_lock));
/* alloc buffer from slab layer */
obj = rtl_cache_slab_alloc (cache);
if ((obj != 0) && (cache->m_constructor != 0))
{
/* construct object */
if (!((cache->m_constructor)(obj, cache->m_userarg)))
{
/* construction failure */
rtl_cache_slab_free (cache, obj), obj = 0;
}
}
return (obj);
}
/** rtl_cache_free()
*/
void
SAL_CALL rtl_cache_free (
rtl_cache_type * cache,
void * obj
) SAL_THROW_EXTERN_C()
{
if ((obj != 0) && (cache != 0))
{
if (alloc_mode == AMode_SYSTEM)
{
if (cache->m_destructor != 0)
{
/* destruct object */
(cache->m_destructor)(obj, cache->m_userarg);
}
rtl_freeMemory(obj);
return;
}
RTL_MEMORY_LOCK_ACQUIRE(&(cache->m_depot_lock));
for (;;)
{
/* return object to magazine layer */
rtl_cache_magazine_type *curr, *prev, *temp;
curr = cache->m_cpu_curr;
if ((curr != 0) && (curr->m_mag_used < curr->m_mag_size))
{
curr->m_objects[curr->m_mag_used++] = obj;
cache->m_cpu_stats.m_free += 1;
RTL_MEMORY_LOCK_RELEASE(&(cache->m_depot_lock));
return;
}
prev = cache->m_cpu_prev;
if ((prev != 0) && (prev->m_mag_used == 0))
{
temp = cache->m_cpu_curr;
cache->m_cpu_curr = cache->m_cpu_prev;
cache->m_cpu_prev = temp;
continue;
}
temp = rtl_cache_depot_exchange_free (cache, prev);
if (temp != 0)
{
cache->m_cpu_prev = cache->m_cpu_curr;
cache->m_cpu_curr = temp;
continue;
}
if (rtl_cache_depot_populate(cache))
{
continue;
}
/* no empty magazine: fall through to slab layer */
break;
}
RTL_MEMORY_LOCK_RELEASE(&(cache->m_depot_lock));
/* no space for constructed object in magazine layer */
if (cache->m_destructor != 0)
{
/* destruct object */
(cache->m_destructor)(obj, cache->m_userarg);
}
/* return buffer to slab layer */
rtl_cache_slab_free (cache, obj);
}
}
/* ================================================================= *
*
* cache wsupdate (machdep) internals.
*
* ================================================================= */
/** rtl_cache_wsupdate_init()
*
* @precond g_cache_list.m_lock initialized
*/
static void
rtl_cache_wsupdate_init();
/** rtl_cache_wsupdate_wait()
*
* @precond g_cache_list.m_lock acquired
*/
static void
rtl_cache_wsupdate_wait (
unsigned int seconds
);
/** rtl_cache_wsupdate_fini()
*
*/
static void
rtl_cache_wsupdate_fini();
/* ================================================================= */
#if defined(SAL_UNX)
#include <sys/time.h>
static void *
rtl_cache_wsupdate_all (void * arg);
static void
rtl_cache_wsupdate_init()
{
RTL_MEMORY_LOCK_ACQUIRE(&(g_cache_list.m_lock));
g_cache_list.m_update_done = 0;
(void) pthread_cond_init (&(g_cache_list.m_update_cond), NULL);
if (pthread_create (
&(g_cache_list.m_update_thread), NULL, rtl_cache_wsupdate_all, (void*)(10)) != 0)
{
/* failure */
g_cache_list.m_update_thread = (pthread_t)(0);
}
RTL_MEMORY_LOCK_RELEASE(&(g_cache_list.m_lock));
}
static void
rtl_cache_wsupdate_wait (unsigned int seconds)
{
if (seconds > 0)
{
timeval now;
timespec wakeup;
gettimeofday(&now, 0);
wakeup.tv_sec = now.tv_sec + (seconds);
wakeup.tv_nsec = now.tv_usec * 1000;
(void) pthread_cond_timedwait (
&(g_cache_list.m_update_cond),
&(g_cache_list.m_lock),
&wakeup);
}
}
static void
rtl_cache_wsupdate_fini()
{
RTL_MEMORY_LOCK_ACQUIRE(&(g_cache_list.m_lock));
g_cache_list.m_update_done = 1;
pthread_cond_signal (&(g_cache_list.m_update_cond));
RTL_MEMORY_LOCK_RELEASE(&(g_cache_list.m_lock));
if (g_cache_list.m_update_thread != (pthread_t)(0))
pthread_join (g_cache_list.m_update_thread, NULL);
}
/* ================================================================= */
#elif defined(SAL_W32)
static DWORD WINAPI
rtl_cache_wsupdate_all (void * arg);
static void
rtl_cache_wsupdate_init()
{
DWORD dwThreadId;
RTL_MEMORY_LOCK_ACQUIRE(&(g_cache_list.m_lock));
g_cache_list.m_update_done = 0;
g_cache_list.m_update_cond = CreateEvent (0, TRUE, FALSE, 0);
g_cache_list.m_update_thread =
CreateThread (NULL, 0, rtl_cache_wsupdate_all, (LPVOID)(10), 0, &dwThreadId);
RTL_MEMORY_LOCK_RELEASE(&(g_cache_list.m_lock));
}
static void
rtl_cache_wsupdate_wait (unsigned int seconds)
{
if (seconds > 0)
{
RTL_MEMORY_LOCK_RELEASE(&(g_cache_list.m_lock));
WaitForSingleObject (g_cache_list.m_update_cond, (DWORD)(seconds * 1000));
RTL_MEMORY_LOCK_ACQUIRE(&(g_cache_list.m_lock));
}
}
static void
rtl_cache_wsupdate_fini()
{
RTL_MEMORY_LOCK_ACQUIRE(&(g_cache_list.m_lock));
g_cache_list.m_update_done = 1;
SetEvent (g_cache_list.m_update_cond);
RTL_MEMORY_LOCK_RELEASE(&(g_cache_list.m_lock));
WaitForSingleObject (g_cache_list.m_update_thread, INFINITE);
}
#endif /* SAL_UNX || SAL_W32 */
/* ================================================================= */
/** rtl_cache_depot_wsupdate()
* update depot stats and purge excess magazines.
*
* @precond cache->m_depot_lock acquired
*/
static void
rtl_cache_depot_wsupdate (
rtl_cache_type * cache,
rtl_cache_depot_type * depot
)
{
sal_Size npurge;
depot->m_prev_min = depot->m_curr_min;
depot->m_curr_min = depot->m_mag_count;
npurge = depot->m_curr_min < depot->m_prev_min ? depot->m_curr_min : depot->m_prev_min;
for (; npurge > 0; npurge--)
{
rtl_cache_magazine_type * mag = rtl_cache_depot_dequeue (depot);
if (mag != 0)
{
RTL_MEMORY_LOCK_RELEASE(&(cache->m_depot_lock));
rtl_cache_magazine_clear (cache, mag);
rtl_cache_free (cache->m_magazine_cache, mag);
RTL_MEMORY_LOCK_ACQUIRE(&(cache->m_depot_lock));
}
}
// coverity[missing_unlock]
}
/** rtl_cache_wsupdate()
*
* @precond cache->m_depot_lock released
*/
static void
rtl_cache_wsupdate (
rtl_cache_type * cache
)
{
if (cache->m_magazine_cache != 0)
{
RTL_MEMORY_LOCK_ACQUIRE(&(cache->m_depot_lock));
// SAL_INFO(
// "sal.rtl",
// "rtl_cache_wsupdate(" << cache->m_name
// << ") [depot: count, curr_min, prev_min] full: "
// << cache->m_depot_full.m_mag_count << ", "
// << cache->m_depot_full.m_curr_min << ", "
// << cache->m_depot_full.m_prev_min << "; empty: "
// << cache->m_depot_empty.m_mag_count << ", "
// << cache->m_depot_empty.m_curr_min << ", "
// << cache->m_depot_empty.m_prev_min);
rtl_cache_depot_wsupdate (cache, &(cache->m_depot_full));
rtl_cache_depot_wsupdate (cache, &(cache->m_depot_empty));
RTL_MEMORY_LOCK_RELEASE(&(cache->m_depot_lock));
}
}
/** rtl_cache_wsupdate_all()
*
*/
#if defined(SAL_UNX)
static void *
#elif defined(SAL_W32)
static DWORD WINAPI
#endif /* SAL_UNX || SAL_W32 */
rtl_cache_wsupdate_all (void * arg)
{
osl::Thread::setName("rtl_cache_wsupdate_all");
unsigned int seconds = sal::static_int_cast< unsigned int >(
reinterpret_cast< sal_uIntPtr >(arg));
RTL_MEMORY_LOCK_ACQUIRE(&(g_cache_list.m_lock));
while (!g_cache_list.m_update_done)
{
rtl_cache_wsupdate_wait (seconds);
if (!g_cache_list.m_update_done)
{
rtl_cache_type * head, * cache;
head = &(g_cache_list.m_cache_head);
for (cache = head->m_cache_next;
cache != head;
cache = cache->m_cache_next)
{
rtl_cache_wsupdate (cache);
}
}
}
RTL_MEMORY_LOCK_RELEASE(&(g_cache_list.m_lock));
return (0);
}
/* ================================================================= *
*
* cache initialization.
*
* ================================================================= */
void
rtl_cache_init()
{
{
/* list of caches */
RTL_MEMORY_LOCK_INIT(&(g_cache_list.m_lock));
(void) rtl_cache_constructor (&(g_cache_list.m_cache_head));
}
{
/* cache: internal arena */
assert(gp_cache_arena == 0);
gp_cache_arena = rtl_arena_create (
"rtl_cache_internal_arena",
64, /* quantum */
0, /* no quantum caching */
NULL, /* default source */
rtl_arena_alloc,
rtl_arena_free,
0 /* flags */
);
assert(gp_cache_arena != 0);
/* check 'gp_default_arena' initialization */
assert(gp_default_arena != 0);
}
{
/* cache: magazine cache */
static rtl_cache_type g_cache_magazine_cache;
assert(gp_cache_magazine_cache == 0);
(void) rtl_cache_constructor (&g_cache_magazine_cache);
gp_cache_magazine_cache = rtl_cache_activate (
&g_cache_magazine_cache,
"rtl_cache_magazine_cache",
sizeof(rtl_cache_magazine_type), /* objsize */
0, /* objalign */
rtl_cache_magazine_constructor,
rtl_cache_magazine_destructor,
0, /* reclaim */
0, /* userarg: NYI */
gp_default_arena, /* source */
RTL_CACHE_FLAG_NOMAGAZINE /* during bootstrap; activated below */
);
assert(gp_cache_magazine_cache != 0);
/* activate magazine layer */
g_cache_magazine_cache.m_magazine_cache = gp_cache_magazine_cache;
}
{
/* cache: slab (struct) cache */
static rtl_cache_type g_cache_slab_cache;
assert(gp_cache_slab_cache == 0);
(void) rtl_cache_constructor (&g_cache_slab_cache);
gp_cache_slab_cache = rtl_cache_activate (
&g_cache_slab_cache,
"rtl_cache_slab_cache",
sizeof(rtl_cache_slab_type), /* objsize */
0, /* objalign */
rtl_cache_slab_constructor,
rtl_cache_slab_destructor,
0, /* reclaim */
0, /* userarg: none */
gp_default_arena, /* source */
0 /* flags: none */
);
assert(gp_cache_slab_cache != 0);
}
{
/* cache: bufctl cache */
static rtl_cache_type g_cache_bufctl_cache;
assert(gp_cache_bufctl_cache == 0);
(void) rtl_cache_constructor (&g_cache_bufctl_cache);
gp_cache_bufctl_cache = rtl_cache_activate (
&g_cache_bufctl_cache,
"rtl_cache_bufctl_cache",
sizeof(rtl_cache_bufctl_type), /* objsize */
0, /* objalign */
0, /* constructor */
0, /* destructor */
0, /* reclaim */
0, /* userarg */
gp_default_arena, /* source */
0 /* flags: none */
);
assert(gp_cache_bufctl_cache != 0);
}
rtl_cache_wsupdate_init();
// SAL_INFO("sal.rtl", "rtl_cache_init completed");
}
/* ================================================================= */
void
rtl_cache_fini()
{
if (gp_cache_arena != 0)
{
rtl_cache_type * cache, * head;
rtl_cache_wsupdate_fini();
if (gp_cache_bufctl_cache != 0)
{
cache = gp_cache_bufctl_cache, gp_cache_bufctl_cache = 0;
rtl_cache_deactivate (cache);
rtl_cache_destructor (cache);
}
if (gp_cache_slab_cache != 0)
{
cache = gp_cache_slab_cache, gp_cache_slab_cache = 0;
rtl_cache_deactivate (cache);
rtl_cache_destructor (cache);
}
if (gp_cache_magazine_cache != 0)
{
cache = gp_cache_magazine_cache, gp_cache_magazine_cache = 0;
rtl_cache_deactivate (cache);
rtl_cache_destructor (cache);
}
if (gp_cache_arena != 0)
{
rtl_arena_destroy (gp_cache_arena);
gp_cache_arena = 0;
}
RTL_MEMORY_LOCK_ACQUIRE(&(g_cache_list.m_lock));
head = &(g_cache_list.m_cache_head);
for (cache = head->m_cache_next; cache != head; cache = cache->m_cache_next)
{
// SAL_INFO(
// "sal.rtl",
// "rtl_cache_fini(" << cache->m_name << ") [slab]: allocs: "
// << cache->m_slab_stats.m_alloc << ", frees: "
// << cache->m_slab_stats.m_free << "; total: "
// << cache->m_slab_stats.m_mem_total << ", used: "
// << cache->m_slab_stats.m_mem_alloc << "; [cpu]: allocs: "
// << cache->m_cpu_stats.m_alloc << ", frees: "
// << cache->m_cpu_stats.m_free << "; [total]: allocs: "
// << (cache->m_slab_stats.m_alloc
// + cache->m_cpu_stats.m_alloc)
// << ", frees: "
// << (cache->m_slab_stats.m_free
// + cache->m_cpu_stats.m_free));
}
RTL_MEMORY_LOCK_RELEASE(&(g_cache_list.m_lock));
}
// SAL_INFO("sal.rtl", "rtl_cache_fini completed");
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|