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
|
/**
* @file llmemory.cpp
* @brief Very special memory allocation/deallocation stuff here
*
* $LicenseInfo:firstyear=2002&license=viewerlgpl$
* Second Life Viewer Source Code
* Copyright (C) 2010, Linden Research, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation;
* version 2.1 of the License only.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
* $/LicenseInfo$
*/
#include "linden_common.h"
#include "llthread.h"
#if defined(LL_WINDOWS)
//# include <windows.h>
# include <psapi.h>
#elif defined(LL_DARWIN)
# include <sys/types.h>
# include <mach/task.h>
# include <mach/mach_init.h>
#elif LL_LINUX || LL_SOLARIS
# include <unistd.h>
#endif
#include "llmemory.h"
#include "llsys.h"
//----------------------------------------------------------------------------
//static
char* LLMemory::reserveMem = 0;
U32 LLMemory::sAvailPhysicalMemInKB = U32_MAX ;
U32 LLMemory::sMaxPhysicalMemInKB = 0;
U32 LLMemory::sAllocatedMemInKB = 0;
U32 LLMemory::sAllocatedPageSizeInKB = 0 ;
U32 LLMemory::sMaxHeapSizeInKB = U32_MAX ;
BOOL LLMemory::sEnableMemoryFailurePrevention = FALSE;
//static
void LLMemory::initClass()
{
if (!reserveMem)
{
reserveMem = new char[16*1024]; // reserve 16K for out of memory error handling
}
}
//static
void LLMemory::cleanupClass()
{
delete [] reserveMem;
reserveMem = NULL;
}
//static
void LLMemory::freeReserve()
{
delete [] reserveMem;
reserveMem = NULL;
}
//static
void LLMemory::initMaxHeapSizeGB(F32 max_heap_size_gb, BOOL prevent_heap_failure)
{
sMaxHeapSizeInKB = (U32)(max_heap_size_gb * 1024 * 1024) ;
sEnableMemoryFailurePrevention = prevent_heap_failure ;
}
//static
void LLMemory::updateMemoryInfo()
{
#if LL_WINDOWS
HANDLE self = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS counters;
if (!GetProcessMemoryInfo(self, &counters, sizeof(counters)))
{
llwarns << "GetProcessMemoryInfo failed" << llendl;
return ;
}
sAllocatedMemInKB = (U32)(counters.WorkingSetSize / 1024) ;
sAllocatedPageSizeInKB = (U32)(counters.PagefileUsage / 1024) ;
sMaxPhysicalMemInKB = llmin(LLMemoryInfo::getAvailableMemoryKB() + sAllocatedMemInKB, sMaxHeapSizeInKB);
if(sMaxPhysicalMemInKB > sAllocatedMemInKB)
{
sAvailPhysicalMemInKB = sMaxPhysicalMemInKB - sAllocatedMemInKB ;
}
else
{
sAvailPhysicalMemInKB = 0 ;
}
#else
//not valid for other systems for now.
sAllocatedMemInKB = (U32)(LLMemory::getCurrentRSS() / 1024) ;
sMaxPhysicalMemInKB = U32_MAX ;
sAvailPhysicalMemInKB = U32_MAX ;
#endif
return ;
}
//
//this function is to test if there is enough space with the size in the virtual address space.
//it does not do any real allocation
//if success, it returns the address where the memory chunk can fit in;
//otherwise it returns NULL.
//
//static
void* LLMemory::tryToAlloc(void* address, U32 size)
{
#if LL_WINDOWS
address = VirtualAlloc(address, size, MEM_RESERVE | MEM_TOP_DOWN, PAGE_NOACCESS) ;
if(address)
{
if(!VirtualFree(address, 0, MEM_RELEASE))
{
llerrs << "error happens when free some memory reservation." << llendl ;
}
}
#else
#endif
return address ;
}
//static
void LLMemory::logMemoryInfo(BOOL update)
{
if(update)
{
updateMemoryInfo() ;
}
llinfos << "Current allocated physical memory(KB): " << sAllocatedMemInKB << llendl ;
llinfos << "Current allocated page size (KB): " << sAllocatedPageSizeInKB << llendl ;
llinfos << "Current availabe physical memory(KB): " << sAvailPhysicalMemInKB << llendl ;
llinfos << "Current max usable memory(KB): " << sMaxPhysicalMemInKB << llendl ;
}
//return 0: everything is normal;
//return 1: the memory pool is low, but not in danger;
//return -1: the memory pool is in danger, is about to crash.
//static
S32 LLMemory::isMemoryPoolLow()
{
static const U32 LOW_MEMEOY_POOL_THRESHOLD_KB = 64 * 1024 ; //64 MB for emergency use
if(!sEnableMemoryFailurePrevention)
{
return 0 ; //no memory failure prevention.
}
if(sAvailPhysicalMemInKB < (LOW_MEMEOY_POOL_THRESHOLD_KB >> 2)) //out of physical memory
{
return -1 ;
}
if(sAllocatedPageSizeInKB + (LOW_MEMEOY_POOL_THRESHOLD_KB >> 2) > sMaxHeapSizeInKB) //out of virtual address space.
{
return -1 ;
}
return (S32)(sAvailPhysicalMemInKB < LOW_MEMEOY_POOL_THRESHOLD_KB ||
sAllocatedPageSizeInKB + LOW_MEMEOY_POOL_THRESHOLD_KB > sMaxHeapSizeInKB) ;
}
//static
U32 LLMemory::getAvailableMemKB()
{
return sAvailPhysicalMemInKB ;
}
//static
U32 LLMemory::getMaxMemKB()
{
return sMaxPhysicalMemInKB ;
}
//static
U32 LLMemory::getAllocatedMemKB()
{
return sAllocatedMemInKB ;
}
void* ll_allocate (size_t size)
{
if (size == 0)
{
llwarns << "Null allocation" << llendl;
}
void *p = malloc(size);
if (p == NULL)
{
LLMemory::freeReserve();
llerrs << "Out of memory Error" << llendl;
}
return p;
}
void ll_release (void *p)
{
free(p);
}
//----------------------------------------------------------------------------
#if defined(LL_WINDOWS)
U64 LLMemory::getCurrentRSS()
{
HANDLE self = GetCurrentProcess();
PROCESS_MEMORY_COUNTERS counters;
if (!GetProcessMemoryInfo(self, &counters, sizeof(counters)))
{
llwarns << "GetProcessMemoryInfo failed" << llendl;
return 0;
}
return counters.WorkingSetSize;
}
#elif defined(LL_DARWIN)
/*
The API used here is not capable of dealing with 64-bit memory sizes, but is available before 10.4.
Once we start requiring 10.4, we can use the updated API, which looks like this:
task_basic_info_64_data_t basicInfo;
mach_msg_type_number_t basicInfoCount = TASK_BASIC_INFO_64_COUNT;
if (task_info(mach_task_self(), TASK_BASIC_INFO_64, (task_info_t)&basicInfo, &basicInfoCount) == KERN_SUCCESS)
Of course, this doesn't gain us anything unless we start building the viewer as a 64-bit executable, since that's the only way
for our memory allocation to exceed 2^32.
*/
// if (sysctl(ctl, 2, &page_size, &size, NULL, 0) == -1)
// {
// llwarns << "Couldn't get page size" << llendl;
// return 0;
// } else {
// return page_size;
// }
// }
U64 LLMemory::getCurrentRSS()
{
U64 residentSize = 0;
task_basic_info_data_t basicInfo;
mach_msg_type_number_t basicInfoCount = TASK_BASIC_INFO_COUNT;
if (task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&basicInfo, &basicInfoCount) == KERN_SUCCESS)
{
residentSize = basicInfo.resident_size;
// If we ever wanted it, the process virtual size is also available as:
// virtualSize = basicInfo.virtual_size;
// llinfos << "resident size is " << residentSize << llendl;
}
else
{
llwarns << "task_info failed" << llendl;
}
return residentSize;
}
#elif defined(LL_LINUX)
U64 LLMemory::getCurrentRSS()
{
static const char statPath[] = "/proc/self/stat";
LLFILE *fp = LLFile::fopen(statPath, "r");
U64 rss = 0;
if (fp == NULL)
{
llwarns << "couldn't open " << statPath << llendl;
goto bail;
}
// Eee-yew! See Documentation/filesystems/proc.txt in your
// nearest friendly kernel tree for details.
{
int ret = fscanf(fp, "%*d (%*[^)]) %*c %*d %*d %*d %*d %*d %*d %*d "
"%*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %*d %Lu",
&rss);
if (ret != 1)
{
llwarns << "couldn't parse contents of " << statPath << llendl;
rss = 0;
}
}
fclose(fp);
bail:
return rss;
}
#elif LL_SOLARIS
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#define _STRUCTURED_PROC 1
#include <sys/procfs.h>
U64 LLMemory::getCurrentRSS()
{
char path [LL_MAX_PATH]; /* Flawfinder: ignore */
sprintf(path, "/proc/%d/psinfo", (int)getpid());
int proc_fd = -1;
if((proc_fd = open(path, O_RDONLY)) == -1){
llwarns << "LLmemory::getCurrentRSS() unable to open " << path << ". Returning 0 RSS!" << llendl;
return 0;
}
psinfo_t proc_psinfo;
if(read(proc_fd, &proc_psinfo, sizeof(psinfo_t)) != sizeof(psinfo_t)){
llwarns << "LLmemory::getCurrentRSS() Unable to read from " << path << ". Returning 0 RSS!" << llendl;
close(proc_fd);
return 0;
}
close(proc_fd);
return((U64)proc_psinfo.pr_rssize * 1024);
}
#else
U64 LLMemory::getCurrentRSS()
{
return 0;
}
#endif
//-------------------------------------------------------------
//class LLPrivateMemoryPool::LLMemoryBlock
//-------------------------------------------------------------
//
//each memory block could fit for two page sizes: 0.75 * mSlotSize, which starts from the beginning of the memory chunk and grow towards the end of the
//the block; another is mSlotSize, which starts from the end of the block and grows towards the beginning of the block.
//
LLPrivateMemoryPool::LLMemoryBlock::LLMemoryBlock()
{
//empty
}
LLPrivateMemoryPool::LLMemoryBlock::~LLMemoryBlock()
{
//empty
}
void LLPrivateMemoryPool::LLMemoryBlock::init(char* buffer, U32 buffer_size, U32 slot_size)
{
mBuffer = buffer ;
mBufferSize = buffer_size ;
mSlotSize = slot_size ;
mTotalSlots = buffer_size / mSlotSize ;
llassert_always(mTotalSlots < 256) ; //max number is 256
mAllocatedSlots = 0 ;
//mark free bits
S32 usage_bit_len = (mTotalSlots + 31) / 32 ;
mDummySize = usage_bit_len - 1 ;
if(mDummySize > 0) //extra space to store mUsageBits
{
mTotalSlots -= (mDummySize * sizeof(mUsageBits) + mSlotSize - 1) / mSlotSize ;
usage_bit_len = (mTotalSlots + 31) / 32 ;
mDummySize = usage_bit_len - 1 ;
if(mDummySize > 0)
{
mUsageBits = 0 ;
for(S32 i = 0 ; i < mDummySize ; i++)
{
*((U32*)mBuffer + i) = 0 ;
}
if(mTotalSlots & 31)
{
*((U32*)mBuffer + mDummySize - 1) = (0xffffffff << (mTotalSlots & 31)) ;
}
}
}
if(mDummySize < 1)
{
mUsageBits = 0 ;
if(mTotalSlots & 31)
{
mUsageBits = (0xffffffff << (mTotalSlots & 31)) ;
}
}
mSelf = NULL ;
mNext = NULL ;
}
void LLPrivateMemoryPool::LLMemoryBlock::setBuffer(char* buffer, U32 buffer_size)
{
mBuffer = buffer ;
mBufferSize = buffer_size ;
mTotalSlots = 0 ; //set the block is free.
}
char* LLPrivateMemoryPool::LLMemoryBlock::allocate()
{
llassert_always(mAllocatedSlots < mTotalSlots) ;
//find a free slot
U32* bits = NULL ;
U32 k = 0 ;
if(mUsageBits != 0xffffffff)
{
bits = &mUsageBits ;
}
else if(mDummySize > 0)//go to extra space
{
for(S32 i = 0 ; i < mDummySize; i++)
{
if(*((U32*)mBuffer + i) != 0xffffffff)
{
bits = (U32*)mBuffer + i ;
k = i + 1 ;
break ;
}
}
}
S32 idx = 0 ;
U32 tmp = *bits ;
for(; tmp & 1 ; tmp >>= 1, idx++) ;
//set the slot reserved
if(!idx)
{
*bits |= 1 ;
}
else
{
*bits |= (1 << idx) ;
}
mAllocatedSlots++ ;
return mBuffer + mDummySize * sizeof(U32) + (k * 32 + idx) * mSlotSize ;
}
void LLPrivateMemoryPool::LLMemoryBlock::free(void* addr)
{
U32 idx = ((char*) addr - mBuffer - mDummySize * sizeof(U32)) / mSlotSize ;
U32* bits = &mUsageBits ;
if(idx > 32)
{
bits = (U32*)mBuffer + (idx - 32) / 32 ;
}
if(idx & 31)
{
*bits &= ~(1 << (idx & 31)) ;
}
else
{
*bits &= ~1 ;
}
mAllocatedSlots-- ;
}
//-------------------------------------------------------------------
//class LLMemoryChunk
//--------------------------------------------------------------------
LLPrivateMemoryPool::LLMemoryChunk::LLMemoryChunk()
{
//empty
}
LLPrivateMemoryPool::LLMemoryChunk::~LLMemoryChunk()
{
//empty
}
void LLPrivateMemoryPool::LLMemoryChunk::init(char* buffer, U32 buffer_size, U32 min_slot_size, U32 max_slot_size, U32 min_block_size, U32 max_block_size)
{
mBuffer = buffer ;
mBufferSize = buffer_size ;
mMetaBuffer = mBuffer + sizeof(LLMemoryChunk) ;
mMinBlockSize = min_block_size;
mMaxBlockSize = max_block_size;
mMinSlotSize = min_slot_size;
mBlockLevels = max_block_size / min_block_size ;
mPartitionLevels = mMaxBlockSize / mMinBlockSize + 1 ;
S32 max_num_blocks = (buffer_size - sizeof(LLMemoryChunk) - mBlockLevels * sizeof(LLMemoryBlock*) - mPartitionLevels * sizeof(LLMemoryBlock*)) /
(mMinBlockSize + sizeof(LLMemoryBlock)) ;
//meta data space
mBlocks = (LLMemoryBlock*)mMetaBuffer ;
mAvailBlockList = (LLMemoryBlock**)((char*)mBlocks + sizeof(LLMemoryBlock) * max_num_blocks) ;
mFreeSpaceList = (LLMemoryBlock**)((char*)mAvailBlockList + sizeof(LLMemoryBlock*) * mBlockLevels) ;
//data buffer
mDataBuffer = (char*)mFreeSpaceList + sizeof(LLMemoryBlock*) * mPartitionLevels ;
//init
for(U32 i = 0 ; i < mBlockLevels; i++)
{
mAvailBlockList[i] = NULL ;
}
for(U32 i = 0 ; i < mPartitionLevels ; i++)
{
mFreeSpaceList[i] = NULL ;
}
mBlocks[0].setBuffer(mDataBuffer, buffer_size - (mDataBuffer - mBuffer)) ;
addToFreeSpace(&mBlocks[0]) ;
mKey = (U32)mBuffer ;
mNext = NULL ;
mPrev = NULL ;
}
//static
U32 LLPrivateMemoryPool::LLMemoryChunk::getMaxOverhead(U32 data_buffer_size, U32 min_page_size)
{
return 2048 +
sizeof(LLMemoryBlock) * (data_buffer_size / min_page_size) ;
}
char* LLPrivateMemoryPool::LLMemoryChunk::allocate(U32 size)
{
char* p = NULL ;
U32 blk_idx = size / mMinSlotSize ;
if(mMinSlotSize * blk_idx < size)
{
blk_idx++ ;
}
//check if there is free block available
if(mAvailBlockList[blk_idx])
{
LLMemoryBlock* blk = mAvailBlockList[blk_idx] ;
p = blk->allocate() ;
if(blk->isFull())
{
//removeFromFreelist
popAvailBlockList(blk_idx) ;
}
}
//ask for a new block
if(!p)
{
LLMemoryBlock* blk = addBlock(blk_idx) ;
if(blk)
{
p = blk->allocate() ;
if(blk->isFull())
{
//removeFromFreelist
popAvailBlockList(blk_idx) ;
}
}
}
//ask for space from higher level blocks
if(!p)
{
for(S32 i = blk_idx + 1 ; i < mBlockLevels; i++)
{
if(mAvailBlockList[i])
{
LLMemoryBlock* blk = mAvailBlockList[i] ;
p = blk->allocate() ;
if(blk->isFull())
{
//removeFromFreelist
popAvailBlockList(i) ;
}
break ;
}
}
}
return p ;
}
void LLPrivateMemoryPool::LLMemoryChunk::free(void* addr)
{
LLMemoryBlock* blk = (LLMemoryBlock*)(mMetaBuffer + (((char*)addr - mDataBuffer) / mMinBlockSize) * sizeof(LLMemoryBlock)) ;
blk = blk->mSelf ;
bool was_full = blk->isFull() ;
blk->free(addr) ;
if(blk->empty())
{
removeBlock(blk) ;
}
else if(was_full)
{
addToAvailBlockList(blk) ;
}
}
LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::addBlock(U32 blk_idx)
{
U32 slot_size = mMinSlotSize * (blk_idx + 1) ;
U32 preferred_block_size = llmax(mMinBlockSize, slot_size * 32) ;
preferred_block_size = llmin(preferred_block_size, mMaxBlockSize) ;
U32 idx = preferred_block_size / mMinBlockSize ;
preferred_block_size = idx * mMinBlockSize ; //round to integer times of mMinBlockSize.
LLMemoryBlock* blk = NULL ;
if(mFreeSpaceList[idx])//if there is free slot for blk_idx
{
blk = createNewBlock(&mFreeSpaceList[idx], preferred_block_size, slot_size, blk_idx) ;
}
else if(mFreeSpaceList[mPartitionLevels - 1]) //search free pool
{
blk = createNewBlock(&mFreeSpaceList[mPartitionLevels - 1], preferred_block_size, slot_size, blk_idx) ;
}
else //search for other non-preferred but enough space slot.
{
for(U32 i = idx - 1 ; i >= 0 ; i--) //search the small slots first
{
if(mFreeSpaceList[i])
{
//create a NEW BLOCK THERE.
if(mFreeSpaceList[i]->getBufferSize() >= slot_size) //at least there is space for one slot.
{
blk = createNewBlock(&mFreeSpaceList[i], preferred_block_size, slot_size, blk_idx) ;
}
break ;
}
}
if(!blk)
{
for(U16 i = idx + 1 ; i < mPartitionLevels - 1; i++) //search the large slots
{
if(mFreeSpaceList[i])
{
//create a NEW BLOCK THERE.
blk = createNewBlock(&mFreeSpaceList[i], preferred_block_size, slot_size, blk_idx) ;
break ;
}
}
}
}
return blk ;
}
LLPrivateMemoryPool::LLMemoryBlock* LLPrivateMemoryPool::LLMemoryChunk::createNewBlock(LLMemoryBlock** cur_idxp, U32 buffer_size, U32 slot_size, U32 blk_idx)
{
LLMemoryBlock* blk = *cur_idxp ;
buffer_size = llmin(buffer_size, blk->getBufferSize()) ;
U32 new_free_blk_size = blk->getBufferSize() - buffer_size ;
if(new_free_blk_size < mMinBlockSize) //can not partition the memory into size smaller than mMinBlockSize
{
buffer_size += new_free_blk_size ;
new_free_blk_size = 0 ;
}
blk->init(blk->getBuffer(), buffer_size, slot_size) ;
if(new_free_blk_size > 0) //cur_idx still has free space
{
LLMemoryBlock* next_blk = blk + (buffer_size / mMinBlockSize) ;
next_blk->setBuffer(blk->getBuffer() + buffer_size, new_free_blk_size) ;
if(new_free_blk_size > mMaxBlockSize) //stays in the free pool
{
next_blk->mPrev = NULL ;
next_blk->mNext = blk->mNext ;
if(next_blk->mNext)
{
next_blk->mNext->mPrev = next_blk ;
}
*cur_idxp = next_blk ;
}
else
{
*cur_idxp = blk->mNext ; //move to the next slot
(*cur_idxp)->mPrev = NULL ;
addToFreeSpace(next_blk) ;
}
}
else //move to the next block
{
*cur_idxp = blk->mNext ;
(*cur_idxp)->mPrev = NULL ;
}
//insert to the available block list...
blk->mNext = NULL ;
blk->mPrev = NULL ;
blk->mSelf = blk ;
mAvailBlockList[blk_idx] = blk ;
//mark the address map
U32 end = (buffer_size / mMinBlockSize) ;
for(U32 i = 1 ; i < end ; i++)
{
(blk + i)->mSelf = blk ;
}
return blk ;
}
void LLPrivateMemoryPool::LLMemoryChunk::removeBlock(LLMemoryBlock* blk)
{
//remove from the available block list
if(blk->mPrev)
{
blk->mPrev->mNext = blk->mNext ;
}
if(blk->mNext)
{
blk->mNext->mPrev = blk->mPrev ;
}
//mark it free
blk->setBuffer(blk->getBuffer(), blk->getBufferSize()) ;
//merge blk with neighbors if possible
if(blk->getBuffer() > mDataBuffer) //has the left neighbor
{
if((blk - 1)->mSelf->isFree())
{
removeFromFreeSpace((blk - 1)->mSelf);
(blk - 1)->mSelf->setBuffer((blk-1)->mSelf->getBuffer(), (blk-1)->mSelf->getBufferSize() + blk->getBufferSize()) ;
blk = (blk - 1)->mSelf ;
}
}
if(blk->getBuffer() + blk->getBufferSize() < mBuffer + mBufferSize) //has the right neighbor
{
U32 d = blk->getBufferSize() / mMinBlockSize ;
if((blk + d)->isFree())
{
removeFromFreeSpace(blk + d) ;
blk->setBuffer(blk->getBuffer(), blk->getBufferSize() + (blk + d)->getBufferSize()) ;
}
}
addToFreeSpace(blk) ;
return ;
}
//the top block in the list is full, pop it out of the list
void LLPrivateMemoryPool::LLMemoryChunk::popAvailBlockList(U32 blk_idx)
{
if(mAvailBlockList[blk_idx])
{
LLMemoryBlock* next = mAvailBlockList[blk_idx]->mNext ;
next->mPrev = NULL ;
mAvailBlockList[blk_idx]->mNext = NULL ;
mAvailBlockList[blk_idx] = next ;
}
}
void LLPrivateMemoryPool::LLMemoryChunk::addToFreeSpace(LLMemoryBlock* blk)
{
U16 free_idx = blk->getBufferSize() / mMinBlockSize ;
(blk + free_idx)->mSelf = blk ; //mark the end pointing back to the head.
free_idx = llmin(free_idx, (U16)(mPartitionLevels - 1)) ;
blk->mNext = mFreeSpaceList[free_idx] ;
if(mFreeSpaceList[free_idx])
{
mFreeSpaceList[free_idx]->mPrev = blk ;
}
mFreeSpaceList[free_idx] = blk ;
blk->mPrev = NULL ;
blk->mSelf = blk ;
return ;
}
void LLPrivateMemoryPool::LLMemoryChunk::removeFromFreeSpace(LLMemoryBlock* blk)
{
U16 free_idx = blk->getBufferSize() / mMinBlockSize ;
free_idx = llmin(free_idx, (U16)(mPartitionLevels - 1)) ;
if(mFreeSpaceList[free_idx] == blk)
{
mFreeSpaceList[free_idx] = blk->mNext ;
}
if(blk->mPrev)
{
blk->mPrev->mNext = blk->mNext ;
}
if(blk->mNext)
{
blk->mNext->mPrev = blk->mPrev ;
}
return ;
}
void LLPrivateMemoryPool::LLMemoryChunk::addToAvailBlockList(LLMemoryBlock* blk)
{
U32 blk_idx = blk->getSlotSize() / mMinSlotSize ;
blk->mNext = mAvailBlockList[blk_idx] ;
if(blk->mNext)
{
blk->mNext->mPrev = blk ;
}
blk->mPrev = NULL ;
return ;
}
//-------------------------------------------------------------------
//class LLPrivateMemoryPool
//--------------------------------------------------------------------
LLPrivateMemoryPool::LLPrivateMemoryPool(U32 max_size, bool threaded) :
mMutexp(NULL),
mMaxPoolSize(max_size),
mReservedPoolSize(0)
{
if(threaded)
{
mMutexp = new LLMutex(NULL) ;
}
for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++)
{
mChunkList[i] = NULL ;
}
mChunkVectorCapacity = 128 ;
mChunks.resize(mChunkVectorCapacity) ; //at most 128 chunks
mNumOfChunks = 0 ;
}
LLPrivateMemoryPool::~LLPrivateMemoryPool()
{
destroyPool();
delete mMutexp ;
}
char* LLPrivateMemoryPool::allocate(U32 size)
{
const static U32 MAX_BLOCK_SIZE = 4 * 1024 * 1024 ; //4MB
//if the asked size larger than MAX_BLOCK_SIZE, fetch from heap directly, the pool does not manage it
if(size >= MAX_BLOCK_SIZE)
{
return new char[size] ;
}
char* p = NULL ;
//find the appropriate chunk
S32 chunk_idx = getChunkIndex(size) ;
lock() ;
LLMemoryChunk* chunk = mChunkList[chunk_idx];
while(chunk)
{
if(p = chunk->allocate(size))
{
break ;
}
chunk = chunk->mNext ;
}
//fetch new memory chunk
if(!p)
{
chunk = addChunk(chunk_idx) ;
p = chunk->allocate(size) ;
}
unlock() ;
return p ;
}
void LLPrivateMemoryPool::free(void* addr)
{
lock() ;
LLMemoryChunk* chunk = mChunks[findChunk((char*)addr)] ;
if(!chunk)
{
delete[] (char*)addr ; //release from heap
}
else
{
chunk->free(addr) ;
if(chunk->empty())
{
removeChunk(chunk) ;
}
}
unlock() ;
}
void LLPrivateMemoryPool::dump()
{
}
void LLPrivateMemoryPool::lock()
{
if(mMutexp)
{
mMutexp->lock() ;
}
}
void LLPrivateMemoryPool::unlock()
{
if(mMutexp)
{
mMutexp->unlock() ;
}
}
S32 LLPrivateMemoryPool::getChunkIndex(U32 size)
{
if(size < 2048)
{
return 0 ;
}
else if(size < (512 << 10))
{
return 1 ;
}
else
{
return 2 ;
}
}
//destroy the entire pool
void LLPrivateMemoryPool::destroyPool()
{
for(U16 i = 0 ; i < mNumOfChunks ; i++)
{
delete[] mChunks[i]->getBuffer() ;
}
mNumOfChunks = 0 ;
for(S32 i = 0 ; i < SUPER_ALLOCATION ; i++)
{
mChunkList[i] = NULL ;
}
}
LLPrivateMemoryPool::LLMemoryChunk* LLPrivateMemoryPool::addChunk(S32 chunk_index)
{
static const U32 MIN_BLOCK_SIZES[SUPER_ALLOCATION] = {2 << 10, 32 << 10, 64 << 10} ;
static const U32 MAX_BLOCK_SIZES[SUPER_ALLOCATION] = {64 << 10, 1 << 20, 4 << 20} ;
static const U32 MIN_SLOT_SIZES[SUPER_ALLOCATION] = {8, 2 << 10, 512 << 10};
static const U32 MAX_SLOT_SIZES[SUPER_ALLOCATION] = {(2 << 10) - 8, (512 - 2) << 10, 4 << 20};
U32 preferred_size ;
U32 overhead ;
if(chunk_index < LARGE_ALLOCATION)
{
preferred_size = (4 << 20) ; //4MB
overhead = LLMemoryChunk::getMaxOverhead(preferred_size, MIN_BLOCK_SIZES[chunk_index]) ;
}
else
{
preferred_size = (16 << 20) ; //16MB
overhead = LLMemoryChunk::getMaxOverhead(preferred_size, MIN_BLOCK_SIZES[chunk_index]) ;
}
char* buffer = new(std::nothrow) char[preferred_size + overhead] ;
if(!buffer)
{
return NULL ;
}
LLMemoryChunk* chunk = new (buffer) LLMemoryChunk() ;
chunk->init(buffer, preferred_size + overhead, MIN_SLOT_SIZES[chunk_index],
MAX_SLOT_SIZES[chunk_index], MIN_BLOCK_SIZES[chunk_index], MAX_BLOCK_SIZES[chunk_index]) ;
//add to the head of the linked list
chunk->mNext = mChunkList[chunk_index] ;
if(mChunkList[chunk_index])
{
mChunkList[chunk_index]->mPrev = chunk ;
}
chunk->mPrev = NULL ;
mChunkList[chunk_index] = chunk ;
//insert into the array
llassert_always(mNumOfChunks + 1 < mChunkVectorCapacity) ;
if(!mNumOfChunks)
{
mChunks[0] = chunk ;
}
else
{
U16 k ;
if(mChunks[0]->getBuffer() > chunk->getBuffer())
{
k = 0 ;
}
else
{
k = findChunk(chunk->getBuffer()) + 1 ;
}
for(U16 i = mNumOfChunks ; i > k ; i++)
{
mChunks[i] = mChunks[i-1] ;
}
mChunks[k] = chunk ;
}
mNumOfChunks++;
return chunk ;
}
void LLPrivateMemoryPool::removeChunk(LLMemoryChunk* chunk)
{
//remove from the linked list
if(chunk->mPrev)
{
chunk->mPrev->mNext = chunk->mNext ;
}
if(chunk->mNext)
{
chunk->mNext->mPrev = chunk->mPrev ;
}
//remove from the array
U16 k = findChunk(chunk->getBuffer()) ;
mNumOfChunks--;
for(U16 i = k ; i < mNumOfChunks ; i++)
{
mChunks[i] = mChunks[i+1] ;
}
//release memory
delete[] chunk->getBuffer() ;
}
U16 LLPrivateMemoryPool::findChunk(const char* addr)
{
llassert_always(mNumOfChunks > 0) ;
U16 s = 0, e = mNumOfChunks;
U16 k = (s + e) / 2 ;
while(s < e)
{
if(mChunks[k]->mKey > (U32)addr)
{
e = k ;
}
else if(k < mNumOfChunks - 1 && mChunks[k+1]->mKey < (U32)addr)
{
s = k ;
}
else
{
break ;
}
k = (s + e) / 2 ;
}
return k ;
}
//--------------------------------------------------------------------
//class LLPrivateMemoryPoolTester
LLPrivateMemoryPoolTester* LLPrivateMemoryPoolTester::sInstance = NULL ;
LLPrivateMemoryPool* LLPrivateMemoryPoolTester::sPool = NULL ;
LLPrivateMemoryPoolTester::LLPrivateMemoryPoolTester()
{
}
LLPrivateMemoryPoolTester::~LLPrivateMemoryPoolTester()
{
}
//static
LLPrivateMemoryPoolTester* LLPrivateMemoryPoolTester::getInstance()
{
if(!sInstance)
{
sInstance = new LLPrivateMemoryPoolTester() ;
}
return sInstance ;
}
//static
void LLPrivateMemoryPoolTester::destroy()
{
if(sInstance)
{
delete sInstance ;
sInstance = NULL ;
}
if(sPool)
{
delete sPool ;
sPool = NULL ;
}
}
void LLPrivateMemoryPoolTester::run()
{
const U32 max_pool_size = 16 << 20 ;
const bool threaded = false ;
if(!sPool)
{
sPool = new LLPrivateMemoryPool(max_pool_size, threaded) ;
}
//run the test
correctnessTest() ;
reliabilityTest() ;
performanceTest() ;
fragmentationtest() ;
}
void LLPrivateMemoryPoolTester::correctnessTest()
{
//try many different sized allocation, fill the memory fully to see if allocation is right.
}
void LLPrivateMemoryPoolTester::reliabilityTest()
void LLPrivateMemoryPoolTester::performanceTest()
void LLPrivateMemoryPoolTester::fragmentationtest()
void* LLPrivateMemoryPoolTester::operator new(size_t size)
{
return (void*)sPool->allocate(size) ;
}
void LLPrivateMemoryPoolTester::operator delete(void* addr)
{
sPool->free(addr) ;
}
//--------------------------------------------------------------------
|