Repository navigation
Expand file tree
/
Copy pathMsgVBHeap.cpp
More file actions
5497 lines (5227 loc) · 241 KB
/
Copy pathMsgVBHeap.cpp
File metadata and controls
5497 lines (5227 loc) · 241 KB
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
// Copyright © 2005-2015, 2023, 2026 Ivyware Pty Ltd, Khrustal & Mann
// MELBOURNE, VICTORIA, AUSTRALIA, 3000
//
// Licensed 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
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
//
// P2Pmsg Virtual-Blocked-Heap utitities
// NOTES: Manage the VBHeap data structures
//
#include "stdafx.h"
#include "P2PmsgVBLock.h"
#include "MsgVBHeap.h"
#include "Msgexception.h"
#include <ASSERT.h>
#include <atomic>
// Sizing VBLockHdr's, eliminates need to contract empty objects
const VBLockHdr soHdr = { 0 };
const VBLockName soName = { 0 };
const VBLockData soData = { 0 };
const VBLockList soList = { 0 };
const VBLockVect soVect = { 0 };
//const VBLockNode soNode = { 0 };
const VBLockAttr soAttr = { 0 };
const VBLockDesc soDesc = { 0 };
#define XCtrl_Hdr
///////////////////////////////////////////////////////////////////////
// VBHeap container, definitions and helpers
// NOTES: Fundamental unit of memory allocation and subsequent
// fragmentation. De-fragmentaion occurs through collation
// of free'd adjacent VBHeapBlock's
#pragma pack(push,1)
template < typename SIZE__ >
struct VBHeapNN__
{ // VBHeap blocks, minimum unit of allocation
SIZE__ nPrev;
SIZE__ nNext;
char cData;
};
typedef VBHeapNN__ <UINT08> VBHeap08;
typedef VBHeapNN__ <UINT16> VBHeap16;
typedef VBHeapNN__ <UINT32> VBHeap32;
typedef VBHeapNN__ <UINT64> VBHeap64;
typedef struct VBHeap_
{ // Universal header
VBLockHdr oHdr; // VBLockHdr is common to both VBHeap & VBLock's
union
{ // Heap management
VBHeap08 oHeap08; // According to VBHeap address mode
VBHeap16 oHeap16;
VBHeap32 oHeap32;
VBHeap64 oHeap64;
} ud; // VBLock equivalent is VBLock::ud
} VBHeap;
#pragma pack(pop)
#define VBHeap_IsLinked(pVBHeap) VBLock_IsLinked((VBLock*)pVBHeap)
#define VBHeap_IsFree(pVBHeap) VBLock_IsFree((VBLock*)pVBHeap)
#define VBHeap_IsAddr(pVBHeap,uAddrType) VBLock_IsAddr((VBLock*)pVBHeap,uAddrType)
#define VBHeap_IsAlloc(pVBHeap) VBLock_IsAlloc((VBLock*)pVBHeap)
#define VBHeap_Sizenn(pVBHeap) VBLock_Hdr_u_SizeNN((VBLock*)pVBHeap)
#define VBList2PhysVBHeap(hVBList,aVBHeap) ((VBHeap*)P2PmsgHeap_Block2Phys(hVBList,aVBHeap))
void*
VBHeap_pud ( VBHeap *pVBHeap )
{
char *pud = (char *)pVBHeap + sizeof(pVBHeap->oHdr) - sizeof(pVBHeap->oHdr.u);
UCHAR uVBLockAddr = pVBHeap->oHdr.uVBLockDefs & VBLock_AddrMask;
if ( uVBLockAddr == VBLock_Addr64 )
return pud += sizeof(pVBHeap->oHdr.u.nSize64);
if ( uVBLockAddr == VBLock_Addr32 )
return pud += sizeof(pVBHeap->oHdr.u.nSize32);
if ( uVBLockAddr == VBLock_Addr16 )
return pud += sizeof(pVBHeap->oHdr.u.nSize16);
if ( uVBLockAddr == VBLock_Addr08 )
return pud += sizeof(pVBHeap->oHdr.u.nSize08);
ASSERT(0);
return nullptr;
}
void
VBHeap_Init ( VBHeap *pVBHeap, UCHAR uVBLockDefs, VBLaddr nSizenn )
{
// Observe addressing model
pVBHeap -> oHdr.uVBLockDefs = uVBLockDefs | VBLock_Alloc;
VBLaddr uAddr = uVBLockDefs & VBLock_AddrMask;
if ( uAddr == VBLock_Addr32 &&
nSizenn == (nSizenn&0xFFFFFFFF) )
{
auto *pVBHeap32 = static_cast<VBHeap32*>( VBHeap_pud(pVBHeap) );
pVBHeap -> oHdr.u.nSize32 = UINT32(nSizenn);
pVBHeap32 -> nPrev = 0;
pVBHeap32 -> nNext = 0;
}
else if ( uAddr == VBLock_Addr64 )
{
auto *pVBHeap64 = static_cast<VBHeap64*>(VBHeap_pud(pVBHeap));
pVBHeap -> oHdr.u.nSize64 = UINT64(nSizenn);
pVBHeap64 -> nPrev = 0;
pVBHeap64 -> nNext = 0;
}
else if ( uAddr == VBLock_Addr16 &&
nSizenn == (nSizenn&0xFFFF) )
{
auto *pVBHeap16 = static_cast<VBHeap16*>( VBHeap_pud(pVBHeap) );
pVBHeap -> oHdr.u.nSize16 = UINT16(nSizenn);
pVBHeap16 -> nPrev = 0;
pVBHeap16 -> nNext = 0;
}
else if (uAddr == VBLock_Addr08 &&
nSizenn == (nSizenn & 0xFF) )
{
auto* pVBHeap08 = static_cast<VBHeap08*>(VBHeap_pud(pVBHeap));
pVBHeap->oHdr.u.nSize08 = UINT08(nSizenn);
pVBHeap08->nPrev = 0;
pVBHeap08->nNext = 0;
}
else EVERR->Module ( "%s(%x, %llu)", __FUNCTION__
, uAddr, nSizenn )
->Message("Internal VBLock.uVBLock corruption" )
->Throw ( );
}
VBLaddr
VBHeap_GetPrev ( VBHeap *pVBHeap, VBLelem *pnItem )
{
// Observe addressing model
UCHAR uVBLockAddr = pVBHeap->oHdr.uVBLockDefs & VBLock_AddrMask;
if ( pnItem )
(*pnItem)--;
if ( uVBLockAddr == VBLock_Addr64 )
return (VBLaddr)((VBHeap64 *)VBHeap_pud(pVBHeap)) -> nPrev;
if ( uVBLockAddr == VBLock_Addr32 )
return ((VBHeap32 *)VBHeap_pud(pVBHeap)) -> nPrev;
if ( uVBLockAddr == VBLock_Addr16 )
return ((VBHeap16 *)VBHeap_pud(pVBHeap)) -> nPrev;
if ( uVBLockAddr == VBLock_Addr08 )
return ((VBHeap08 *)VBHeap_pud(pVBHeap)) -> nPrev;
EVERR->Module ( __FUNCTION__)
->Message("Internal VBLock.uVBLockAddr=%i corruption"
, uVBLockAddr )
->Throw ( );
return (VBLaddr)~0;
}
VBHeap*
VBHeap_SetPrev ( VBHeap *pVBHeap, VBLaddr aItemPrev )
{
// Observe addressing model
UCHAR uVBLockAddr = pVBHeap->oHdr.uVBLockDefs & VBLock_AddrMask;
if ( uVBLockAddr == VBLock_Addr32 &&
aItemPrev == (aItemPrev&(UINT32)~0) )
{
((VBHeap32 *)VBHeap_pud(pVBHeap))->nPrev = static_cast<UINT32>(aItemPrev);
return pVBHeap;
}
if ( uVBLockAddr == VBLock_Addr64 &&
aItemPrev == (aItemPrev&(UINT64)~0) )
{
((VBHeap64 *)VBHeap_pud(pVBHeap))->nPrev = static_cast<UINT64>(aItemPrev);
return pVBHeap;
}
if ( uVBLockAddr == VBLock_Addr16 &&
aItemPrev == (aItemPrev&(UINT16)~0) )
{
((VBHeap16 *)VBHeap_pud(pVBHeap))->nPrev = static_cast<UINT16>(aItemPrev);
return pVBHeap;
}
if ( uVBLockAddr == VBLock_Addr08 &&
aItemPrev == (aItemPrev&(UINT08)~0) )
{
((VBHeap08 *)VBHeap_pud(pVBHeap))->nPrev = static_cast<UINT08>(aItemPrev);
return pVBHeap;
}
EVERR->Module ( "%s(%llu)", __FUNCTION__)
->Message("Internal VBLock.uVBLockAddr=%i corruption"
, uVBLockAddr )
->Throw ( );
return nullptr;
}
VBLaddr
VBHeap_GetNext ( VBHeap *pVBHeap, VBLelem *pnItem )
{
// Observe addressing model
UCHAR uVBLockAddr = pVBHeap->oHdr.uVBLockDefs & VBLock_AddrMask;
if ( pnItem )
(*pnItem)++;
if ( uVBLockAddr == VBLock_Addr32 )
return ((VBHeap32 *)VBHeap_pud(pVBHeap)) -> nNext;
if ( uVBLockAddr == VBLock_Addr64 )
return (VBLaddr)((VBHeap64 *)VBHeap_pud(pVBHeap)) -> nNext;
if ( uVBLockAddr == VBLock_Addr16 )
return ((VBHeap16 *)VBHeap_pud(pVBHeap)) -> nNext;
if ( uVBLockAddr == VBLock_Addr08 )
return ((VBHeap08 *)VBHeap_pud(pVBHeap)) -> nNext;
EVERR->Module ( __FUNCTION__ )
->Message("Internal VBLockItem.uVBLockAddr=%llu corruption"
, uVBLockAddr )
->Throw ( );
return (UINT)~0;
}
VBHeap*
VBHeap_SetNext ( VBHeap *pVBHeap, VBLaddr aItemNext )
{
// Observe addressing model
UCHAR uVBLockAddr = pVBHeap->oHdr.uVBLockDefs & VBLock_AddrMask;
if ( uVBLockAddr == VBLock_Addr32 &&
aItemNext == (aItemNext&(UINT32)~0) )
{
((VBHeap32 *)VBHeap_pud(pVBHeap)) -> nNext = static_cast<UINT32>(aItemNext);
return pVBHeap;
}
if ( uVBLockAddr == VBLock_Addr64 &&
aItemNext == (aItemNext&(UINT64)~0) )
{
((VBHeap64 *)VBHeap_pud(pVBHeap)) -> nNext = static_cast<UINT64>(aItemNext);
return pVBHeap;
}
if ( uVBLockAddr == VBLock_Addr16 &&
aItemNext == (aItemNext&(UINT16)~0) )
{
((VBHeap16 *)VBHeap_pud(pVBHeap)) -> nNext = static_cast<UINT16>(aItemNext);
return pVBHeap;
}
if ( uVBLockAddr == VBLock_Addr08 &&
aItemNext == (aItemNext&(UINT08)~0) )
{
((VBHeap08 *)VBHeap_pud(pVBHeap)) -> nNext = static_cast<UINT08>(aItemNext);
return pVBHeap;
}
EVERR->Module ( __FUNCTION__ )
->Message("Internal VBLockNode.uVBLockAddr=%llu corruption"
, uVBLockAddr )
->Throw ( );
return pVBHeap;
}
///////////////////////////////////////////////////////////////////////
// VBHeapRoot container, definitions and helpers
// NOTES: Fundamental unit of memory allocation and subsequent
// fragmentation. De-fragmentaion occurs through collation
// of free'd adjacent VBHeapBlock's
#pragma pack(push,1)
template < typename SIZE__ >
struct VBHeap_CtrlKeysNN__
{ // Serialised VBHeap control keys and counters
SIZE__ aAlloc; // Address of first allocated VBLock
SIZE__ aAllocSize; // Cummulative size of all allocated VBLock's
SIZE__ nAllocItems; // Number of allocated entries
SIZE__ aFree; // Address of first free VBLock
SIZE__ aFreeLast; // Address of last free VBLock
SIZE__ aFreeSize; // Cummulative size of all free VBLock's
SIZE__ nFreeItems; // Number of free items
SIZE__ aSpare8;
};
typedef VBHeap_CtrlKeysNN__ <UINT08> VBHeap_CtrlKeys08;
typedef VBHeap_CtrlKeysNN__ <UINT16> VBHeap_CtrlKeys16;
typedef VBHeap_CtrlKeysNN__ <UINT32> VBHeap_CtrlKeys32;
typedef VBHeap_CtrlKeysNN__ <UINT64> VBHeap_CtrlKeys64;
typedef VBHeap_CtrlKeysNN__ <VBLaddr> VBHeap_CtrlKeys;
typedef struct VBHeap_CtrlKeys_
{
UINT16 nSize; // Size of this header
UINT16 uVBLock; // Addressing mode
union
{
VBHeap_CtrlKeys08 oRoot08; // Control keys
VBHeap_CtrlKeys16 oRoot16;
VBHeap_CtrlKeys32 oRoot32;
VBHeap_CtrlKeys64 oRoot64;
VBHeap_CtrlKeys oRoot; // Compiled bit size (default)
} ud; // Contents, refer uVBLock
} VBHeapRoot;
#pragma pack(pop)
//
// VBHeapIOMAGE containers and definitions
// NOTES: Private root for VBHeapIOMAGE structure, header mimics VBHeapIOmage
// : Dynamic size dependant upon addressing mode. Optimised for remote
// exchanges and P2PeerMsg refections etc.
#pragma pack(push,1)
typedef struct
{
struct
{
UINT32 uiSync1; // size | addressing | endian sentinel
UINT32 uiSync2; // Compliment of above
} oSync; // Mimics VBListIOmage (see P2PmsgBSTR.h)
VBHeapRoot oRoot; // Root header
} VBHeapIOMAGE;
//#define VBHeapIOmage VBHeapIOMAGE
#pragma pack(pop)
//
// Static manipulators
// NOTES: Provide address sensitive VBLockHeap manipulation
void
P2PmsgHeap_InitIOMAGE ( VBHeapIOMAGE *pIOMAGE, VBLsize nSizeofIOMAGE, UCHAR uAddrType );
bool
P2PmsgHeap_AssertValidFree ( P2PmsgHANDLE hP2PmsgHeap, VBLaddr aVBLock );
void
VBHeapRoot_Init ( VBHeapRoot *pRoot, UCHAR uVBLock, VBLsize nSizenn );
VBLsize
VBHeapRoot_Sizeof ( VBHeapRoot *pRoot ) { return pRoot->nSize; }
// The image bytes a root header occupies under a given addressing mode
// NOTES: VBHeapRoot_Sizeof reads nSize out of the root itself, which is the
// wrong source when the root has not yet been shown to BE there -- the
// field sits inside the very bytes in question. This derives the same
// figure from the addressing width, which arrives in the sync word and
// is range-checked before anything else is read. Same arithmetic as
// VBHeapRoot_Init, which is what writes nSize in the first place.
static VBLsize
VBHeapRoot_Sizeof_Addrnn ( UCHAR uAddrType ) noexcept
{
const VBLsize nHdr = sizeof(VBHeapRoot) - sizeof(VBHeapRoot::ud);
uAddrType = uAddrType & VBLock_AddrMask;
if ( uAddrType == VBLock_Addr08 ) return nHdr + sizeof(VBHeapRoot::ud.oRoot08);
if ( uAddrType == VBLock_Addr16 ) return nHdr + sizeof(VBHeapRoot::ud.oRoot16);
if ( uAddrType == VBLock_Addr32 ) return nHdr + sizeof(VBHeapRoot::ud.oRoot32);
if ( uAddrType == VBLock_Addr64 ) return nHdr + sizeof(VBHeapRoot::ud.oRoot64);
return nHdr + sizeof(VBHeapRoot::ud); // unknown mode: demand the widest
}
VBLaddr
VBHeapRoot_GetAlloc ( const VBHeapRoot *pRoot, int *pnItem = 0 );
VBHeapRoot*
VBHeapRoot_SetAlloc ( VBHeapRoot *pRoot, VBLaddr aAlloc );
VBLsize
VBHeapRoot_GetAllocSize ( const VBHeapRoot *pRoot );
VBLsize
VBHeapRoot_SetAllocSize ( VBHeapRoot *pRoot, VBLsize aAllocSize, bool bAbsolute = false );
VBLelem
VBHeapRoot_GetAllocItems( const VBHeapRoot *pRoot );
VBLelem
VBHeapRoot_SetAllocItems( VBHeapRoot *pRoot, VBLelem nItems, bool bAbsolute = false );
VBLaddr
VBHeapRoot_GetFree ( const VBHeapRoot *pRoot, VBLelem *pnItem = 0 );
VBHeapRoot*
VBHeapRoot_SetFree ( VBHeapRoot *pRoot, VBLaddr aFree );
VBLaddr
VBHeapRoot_GetFreeLast ( const VBHeapRoot *pRoot, VBLelem *pnItem = 0 );
VBHeapRoot*
VBHeapRoot_SetFreeLast ( VBHeapRoot *pRoot, VBLaddr aFreeLast );
VBLsize
VBHeapRoot_GetFreeSize ( const VBHeapRoot *pRoot );
VBLsize
VBHeapRoot_SetFreeSize ( VBHeapRoot *pRoot, VBLaddr aFreeSize, bool bAbsolute = false );
VBLelem
VBHeapRoot_GetFreeItems( const VBHeapRoot *pRoot );
VBLelem
VBHeapRoot_SetFreeItems( VBHeapRoot *pRoot, VBLelem nItems, bool bAbsolute = false );
void
VBHeapRoot_Init ( VBHeapRoot *pRoot, UCHAR uVBLock, VBLsize nSizenn )
{
pRoot -> uVBLock = uVBLock;
uVBLock = uVBLock & VBLock_AddrMask;
pRoot -> uVBLock = uVBLock;
pRoot -> nSize = sizeof(VBHeapRoot) - sizeof(VBHeapRoot::ud);
if ( uVBLock == VBLock_Addr32 &&
nSizenn == (nSizenn&0xFFFFFFFF) )
{
ZeroMemory( &pRoot->ud.oRoot32, sizeof(pRoot->ud.oRoot32) );
pRoot -> nSize += sizeof(VBHeapRoot::ud.oRoot32);
return;
}
if ( uVBLock == VBLock_Addr64 &&
nSizenn == (nSizenn&0xFFFFFFFFFFFF) )
{
ZeroMemory( &pRoot->ud.oRoot64, sizeof(pRoot->ud.oRoot64) );
pRoot -> nSize += sizeof(VBHeapRoot::ud.oRoot64);
return;
}
if ( uVBLock == VBLock_Addr16 &&
nSizenn == (nSizenn&0xFFFF) )
{
ZeroMemory( &pRoot->ud.oRoot16, sizeof(pRoot->ud.oRoot16) );
pRoot -> nSize += sizeof(VBHeapRoot::ud.oRoot16);
return;
}
if ( uVBLock == VBLock_Addr08 &&
nSizenn == (nSizenn&0xFF) )
{
ASSERT(0);
ZeroMemory( &pRoot->ud.oRoot08, sizeof(pRoot->ud.oRoot08) );
pRoot -> nSize += sizeof(VBHeapRoot::ud.oRoot08);
return;
}
EVERR->Module ("%s(pRoot,uVBLock=%02x,nSizenn=%i", __FUNCTION__
, uVBLock, nSizenn )
->Message("Invalid addressing parameters")
->Throw();
}
VBLsize
VBHeapRoot_GetAlloc ( const VBHeapRoot *pRoot, int *pnItem )
{
// Observe addressing model
UINT16 uVBLock = pRoot->uVBLock & VBLock_AddrMask;
if ( pnItem )
*pnItem = 0;
if ( uVBLock == VBLock_Addr32 )
return pRoot->ud.oRoot32.aAlloc;
if ( uVBLock == VBLock_Addr16 )
return pRoot->ud.oRoot16.aAlloc;
if ( uVBLock == VBLock_Addr64 )
return pRoot->ud.oRoot64.aAlloc;
if ( uVBLock == VBLock_Addr08 )
return pRoot->ud.oRoot08.aAlloc;
EVERR->Module ( __FUNCTION__)
->Message("Internal VBHeapRoot.uVBLockAddr=%i corruption"
, uVBLock )
->Throw ( );
return (VBLsize)~0;
}
VBHeapRoot*
VBHeapRoot_SetAlloc ( VBHeapRoot *pRoot, VBLaddr aAlloc )
{
// Observe addressing model
UINT16 uVBLock = pRoot->uVBLock & VBLock_AddrMask;
if ( uVBLock == VBLock_Addr32 &&
aAlloc == (aAlloc&0xFFFFFFFF) )
{ pRoot->ud.oRoot32.aAlloc = static_cast<UINT32>(aAlloc); return pRoot; }
if ( uVBLock == VBLock_Addr16 &&
aAlloc == (aAlloc&0xFFFF) )
{ pRoot->ud.oRoot16.aAlloc = static_cast<UINT16>(aAlloc); return pRoot; }
if ( uVBLock == VBLock_Addr08 &&
aAlloc == (aAlloc&0xFF) )
{ pRoot->ud.oRoot08.aAlloc = static_cast<UINT08>(aAlloc); return pRoot; }
if ( uVBLock == VBLock_Addr64 &&
aAlloc == (aAlloc&0xFFFFFFFFFFFFFFFF) )
{ pRoot->ud.oRoot64.aAlloc = static_cast<UINT64>(aAlloc); return pRoot; }
ASSERT (0);
EVERR->Module ( "%s(pRoot=%lp, aAlloc=%Ii)", __FUNCTION__
, pRoot, aAlloc )
->Message("Internal VBHeapRoot.uVBLockAddr=%i corruption"
, uVBLock )
->Throw ( );
return 0;
}
VBLsize
VBHeapRoot_GetAllocSize ( const VBHeapRoot *pRoot )
{
// Observe addressing model
UINT16 uVBLock = pRoot->uVBLock & VBLock_AddrMask;
if ( uVBLock == VBLock_Addr32 )
return pRoot->ud.oRoot32.aAllocSize;
if ( uVBLock == VBLock_Addr16 )
return pRoot->ud.oRoot16.aAllocSize;
if ( uVBLock == VBLock_Addr64 )
return pRoot->ud.oRoot64.aAllocSize;
if ( uVBLock == VBLock_Addr08 )
return pRoot->ud.oRoot08.aAllocSize;
EVERR->Module ( __FUNCTION__)
->Message("Internal VBHeapRoot.uVBLockAddr=%i corruption"
, uVBLock )
->Throw ( );
return ~0;
}
VBLsize
VBHeapRoot_SetAllocSize ( VBHeapRoot *pRoot, VBLsize aAllocSize, bool bAbsolute )
{
if ( bAbsolute && aAllocSize < 0 )
EVERR->Module ( "%s(pRoot,aAllocSize=%i,bAbsolute=%i)", __FUNCTION__
, aAllocSize, bAbsolute )
->Message("Invalid parameter (nItems<0)" )
->Throw ( );
else if ( !bAbsolute )
aAllocSize += VBHeapRoot_GetAllocSize ( pRoot );
// Observe addressing model
UINT16 uVBLock = pRoot->uVBLock & VBLock_AddrMask;
if ( uVBLock == VBLock_Addr32 &&
aAllocSize == (aAllocSize&0xFFFFFFFF) )
{ pRoot->ud.oRoot32.aAllocSize = (UINT32)aAllocSize; return pRoot->ud.oRoot32.aAllocSize; }
if ( uVBLock == VBLock_Addr16 &&
aAllocSize == (aAllocSize&0xFFFF) )
{ pRoot->ud.oRoot16.aAllocSize = (UINT16)aAllocSize; return pRoot->ud.oRoot16.aAllocSize; }
if ( uVBLock == VBLock_Addr08 &&
aAllocSize == (aAllocSize&0xFF) )
{ pRoot->ud.oRoot08.aAllocSize = (UINT08)aAllocSize; return pRoot->ud.oRoot08.aAllocSize; }
if ( uVBLock == VBLock_Addr64 )
{ pRoot->ud.oRoot64.aAllocSize = (UINT64)aAllocSize; return (VBLsize)pRoot->ud.oRoot64.aAllocSize; }
ASSERT (0);
EVERR->Module ( "%s(pRoot=%i, aAllocSize=%i)", __FUNCTION__
, (VBLaddr)pRoot, aAllocSize )
->Message("Internal VBHeapRoot.uVBLockAddr=%i corruption"
, uVBLock )
->Throw ( );
return (VBLsize)~0;
}
VBLelem
VBHeapRoot_GetAllocItems ( const VBHeapRoot *pRoot )
{
// Observe addressing model
UINT16 uVBLock = pRoot->uVBLock & VBLock_AddrMask;
if ( uVBLock == VBLock_Addr32 )
return pRoot->ud.oRoot32.nAllocItems;
if ( uVBLock == VBLock_Addr16 )
return pRoot->ud.oRoot16.nAllocItems;
if ( uVBLock == VBLock_Addr64 )
return (VBLelem)pRoot->ud.oRoot64.nAllocItems;
if ( uVBLock == VBLock_Addr08 )
return pRoot->ud.oRoot08.nAllocItems;
EVERR->Module ( __FUNCTION__)
->Message("Internal VBHeapRoot.uVBLockAddr=%i corruption"
, uVBLock )
->Throw ( );
return (VBLelem)~0;
}
VBLelem
VBHeapRoot_SetAllocItems ( VBHeapRoot *pRoot, VBLelem nItems, bool bAbsolute )
{
if ( bAbsolute && nItems < 0 )
EVERR->Module ( "%s(pRoot,nItems=%i,bAbsolute=%i)", __FUNCTION__
, nItems, bAbsolute )
->Message("Invalid parameter (nItems<0)" )
->Throw ( );
else if ( !bAbsolute )
nItems += VBHeapRoot_GetAllocItems ( pRoot );
// Observe addressing model
UINT uItems = static_cast<UINT>(nItems);
UCHAR uVBLock = pRoot->uVBLock & VBLock_AddrMask;
if ( uVBLock == VBLock_Addr32 &&
uItems == (uItems&0xFFFFFFFF) )
{
pRoot->ud.oRoot32.nAllocItems = static_cast<UINT32>(uItems);
return nItems;
}
if ( uVBLock == VBLock_Addr16 &&
uItems == (uItems&0xFFFF) )
{
pRoot->ud.oRoot16.nAllocItems = static_cast<UINT16>(uItems);
return nItems;
}
if ( uVBLock == VBLock_Addr08 &&
uItems == (uItems&0xFF) )
{
pRoot->ud.oRoot08.nAllocItems = static_cast<UINT08>(uItems);
return nItems;
}
if ( uVBLock == VBLock_Addr64 )
{
pRoot->ud.oRoot64.nAllocItems = static_cast<UINT64>(uItems);
return nItems;
}
EVERR->Module ( "%s(pRoot,nItems=%i,bAbsolute=%i)", __FUNCTION__
, nItems, bAbsolute )
->Message("Internal VBHeapRoot.uVBLockAddr=%i corruption"
, uVBLock )
->Throw ( );
return nItems;
}
VBLaddr
VBHeapRoot_GetFree ( const VBHeapRoot *pRoot, int *pnItem )
{
// Observe addressing model
UINT16 uVBLock = pRoot->uVBLock & VBLock_AddrMask;
if ( pnItem )
*pnItem = 0;
if ( uVBLock == VBLock_Addr32 )
return pRoot->ud.oRoot32.aFree;
if ( uVBLock == VBLock_Addr16 )
return pRoot->ud.oRoot16.aFree;
if ( uVBLock == VBLock_Addr64 )
return pRoot->ud.oRoot64.aFree;
if ( uVBLock == VBLock_Addr08 )
return pRoot->ud.oRoot08.aFree;
EVERR->Module ( __FUNCTION__)
->Message("Internal VBHeapRoot.uVBLockAddr=%i corruption"
, uVBLock )
->Throw ( );
return (VBLaddr)~0;
}
VBHeapRoot*
VBHeapRoot_SetFree ( VBHeapRoot *pRoot, VBLaddr aFree )
{
// Observe addressing model
UINT16 uVBLock = pRoot->uVBLock & VBLock_AddrMask;
if ( uVBLock == VBLock_Addr32 &&
aFree == (aFree&0xFFFFFFFF) )
{ pRoot->ud.oRoot32.aFree = (UINT32)aFree; return pRoot; }
if ( uVBLock == VBLock_Addr16 &&
aFree == (aFree&0xFFFF) )
{ pRoot->ud.oRoot16.aFree = (UINT16)aFree; return pRoot; }
if ( uVBLock == VBLock_Addr08 &&
aFree == (aFree&0xFF) )
{ pRoot->ud.oRoot08.aFree = (UINT08)aFree; return pRoot; }
if ( uVBLock == VBLock_Addr64 )
{ pRoot->ud.oRoot64.aFree = (UINT64)aFree; return pRoot; }
ASSERT (0);
EVERR->Module ( "%s(pRoot=%i, aFree=%i)", __FUNCTION__
, (VBLaddr)pRoot, aFree )
->Message("Internal VBHeapRoot.uVBLockAddr=%i corruption"
, uVBLock )
->Throw ( );
return 0;
}
VBLaddr
VBHeapRoot_GetFreeLast ( const VBHeapRoot *pRoot, int *pnItem )
{
// Observe addressing model
UINT16 uVBLock = pRoot->uVBLock & VBLock_AddrMask;
if ( pnItem )
*pnItem = 0;
if ( uVBLock == VBLock_Addr32 )
return pRoot->ud.oRoot32.aFreeLast;
if ( uVBLock == VBLock_Addr16 )
return pRoot->ud.oRoot16.aFreeLast;
if ( uVBLock == VBLock_Addr64 )
return pRoot->ud.oRoot64.aFreeLast;
if ( uVBLock == VBLock_Addr08 )
return pRoot->ud.oRoot08.aFreeLast;
EVERR->Module ( __FUNCTION__)
->Message("Internal VBHeapRoot.uVBLockAddr=%i corruption"
, uVBLock )
->Throw ( );
return ~0;
}
VBHeapRoot*
VBHeapRoot_SetFreeLast ( VBHeapRoot *pRoot, VBLaddr aFreeLast )
{
// Observe addressing model
UINT16 uVBLock = pRoot->uVBLock & VBLock_AddrMask;
if ( uVBLock == VBLock_Addr32 &&
aFreeLast == (aFreeLast&0xFFFFFFFF) )
{ pRoot->ud.oRoot32.aFreeLast = (UINT32)aFreeLast; return pRoot; }
if ( uVBLock == VBLock_Addr16 &&
aFreeLast == (aFreeLast&0xFFFF) )
{ pRoot->ud.oRoot16.aFreeLast = (UINT16)aFreeLast; return pRoot; }
if ( uVBLock == VBLock_Addr08 &&
aFreeLast == (aFreeLast&0xFF) )
{ pRoot->ud.oRoot08.aFreeLast = (UINT08)aFreeLast; return pRoot; }
if ( uVBLock == VBLock_Addr64 )
{ pRoot->ud.oRoot64.aFreeLast = (UINT64)aFreeLast; return pRoot; }
ASSERT (0);
EVERR->Module ( "%s(pRoot=%Ii, aFreeLast=%Ii)", __FUNCTION__
, (VBLaddr)pRoot, aFreeLast )
->Message("Internal VBHeapRoot.uVBLockAddr=%i corruption"
, uVBLock )
->Throw ( );
return 0;
}
VBLsize
VBHeapRoot_GetFreeSize ( const VBHeapRoot *pRoot )
{
// Observe addressing model
UINT16 uVBLock = pRoot->uVBLock & VBLock_AddrMask;
if ( uVBLock == VBLock_Addr32 )
return pRoot->ud.oRoot32.aFreeSize;
if ( uVBLock == VBLock_Addr16 )
return pRoot->ud.oRoot16.aFreeSize;
if ( uVBLock == VBLock_Addr64 )
return (VBLsize)pRoot->ud.oRoot64.aFreeSize;
if ( uVBLock == VBLock_Addr08 )
return pRoot->ud.oRoot08.aFreeSize;
EVERR->Module ( __FUNCTION__)
->Message("Internal VBHeapRoot.uVBLockAddr=%i corruption"
, uVBLock )
->Throw ( );
return ~0u;
}
VBLaddr
VBHeapRoot_SetFreeSize ( VBHeapRoot *pRoot, VBLsize aFreeSize, bool bAbsolute )
{
if ( bAbsolute && aFreeSize < 0 )
EVERR->Module ( "%s(pRoot,aFreeSize=%i,bAbsolute=%i)", __FUNCTION__
, aFreeSize, bAbsolute )
->Message("Invalid parameter (nItems<0)" )
->Throw ( );
else if ( !bAbsolute )
aFreeSize += VBHeapRoot_GetFreeSize ( pRoot );
// Observe addressing model
UINT16 uVBLock = pRoot->uVBLock & VBLock_AddrMask;
if ( uVBLock == VBLock_Addr32 &&
aFreeSize == (aFreeSize&0xFFFFFFFF) )
{ pRoot->ud.oRoot32.aFreeSize = (UINT32)aFreeSize; return pRoot->ud.oRoot32.aFreeSize; }
if ( uVBLock == VBLock_Addr16 &&
aFreeSize == (aFreeSize&0xFFFF) )
{ pRoot->ud.oRoot16.aFreeSize = (UINT16)aFreeSize; return pRoot->ud.oRoot16.aFreeSize; }
if ( uVBLock == VBLock_Addr08 &&
aFreeSize == (aFreeSize&0xFF) )
{ pRoot->ud.oRoot08.aFreeSize = (UINT08)aFreeSize; return pRoot->ud.oRoot08.aFreeSize; }
if ( uVBLock == VBLock_Addr64 )
{ pRoot->ud.oRoot64.aFreeSize = (UINT64)aFreeSize; return (VBLaddr)pRoot->ud.oRoot64.aFreeSize; }
ASSERT (0);
EVERR->Module ( "%s(pRoot=%i, aFreeSize=%i)", __FUNCTION__
, (VBLaddr)pRoot, aFreeSize )
->Message("Internal VBHeapRoot.uVBLockAddr=%i corruption"
, uVBLock )
->Throw ( );
return (VBLaddr)~0;
}
VBLelem
VBHeapRoot_GetFreeItems ( const VBHeapRoot *pRoot )
{
// Observe addressing model
UINT16 uVBLock = pRoot->uVBLock & VBLock_AddrMask;
if ( uVBLock == VBLock_Addr32 )
return pRoot->ud.oRoot32.nFreeItems;
if ( uVBLock == VBLock_Addr16 )
return pRoot->ud.oRoot16.nFreeItems;
if ( uVBLock == VBLock_Addr64 )
return (VBLelem)pRoot->ud.oRoot64.nFreeItems;
if ( uVBLock == VBLock_Addr08 )
return pRoot->ud.oRoot08.nFreeItems;
EVERR->Module ( __FUNCTION__)
->Message("Internal VBHeapRoot.uVBLockAddr=%i corruption"
, uVBLock )
->Throw ( );
return (VBLelem)~0;
}
VBLelem
VBHeapRoot_SetFreeItems ( VBHeapRoot *pRoot, VBLelem nItems, bool bAbsolute )
{
if ( bAbsolute && nItems < 0 )
EVERR->Module ( "%s(pRoot,nItems=%i,bAbsolute=%i)", __FUNCTION__
, nItems, bAbsolute )
->Message("Invalid parameter (nItems<0)" )
->Throw ( );
else if ( !bAbsolute )
nItems += VBHeapRoot_GetFreeItems ( pRoot );
// Observe addressing model
UINT uItems = static_cast<UINT>(nItems);
UCHAR uVBLock = pRoot->uVBLock & VBLock_AddrMask;
if ( uVBLock == VBLock_Addr32 &&
uItems == (uItems&0xFFFFFFFF) )
{
pRoot->ud.oRoot32.nFreeItems = static_cast<UINT32>(uItems);
return nItems;
}
if ( uVBLock == VBLock_Addr16 &&
uItems == (uItems&0xFFFF) )
{
pRoot->ud.oRoot16.nFreeItems = static_cast<UINT16>(uItems);
return nItems;
}
if ( uVBLock == VBLock_Addr08 &&
uItems == (uItems&0xFF) )
{
pRoot->ud.oRoot08.nFreeItems = static_cast<UINT08>(uItems);
return nItems;
}
if ( uVBLock == VBLock_Addr64 )
{
pRoot->ud.oRoot64.nFreeItems = static_cast<UINT64>(uItems);
return nItems;
}
EVERR->Module ( "%s(pRoot,nItems=%i,bAbsolute=%i)", __FUNCTION__
, nItems, bAbsolute )
->Message("Internal VBHeapRoot.uVBLockAddr=%i corruption"
, uVBLock )
->Throw ( );
return nItems;
}
///////////////////////////////////////////////////////////////////////
// P2PmsgHeap containers and definitions
// NOTES: Fundamental unit of memory allocation and subsequent
#define P2PmsgHeap_IOMAGE 1
#define P2PmsgHeap_SYSTEM 2
#define P2PmsgHeap_CHUNK 3
#define P2PmsgHeap_BSTRio 4
#define P2PmsgHeap_BSTRio64 5
typedef struct tagSTrigger
{
UINT nType; // INSERT, UPDATE or DELETE
HWND hWnd; // Notification window
UINT uiWM_APP_Trigger;
// WPARAM is always P2Pos address
LPARAM lParam; // LPARAM for notification
} STrigger;
typedef CList<STrigger> CListRegTriggers;
typedef CMap<VBLaddr,VBLaddr,CListRegTriggers*,CListRegTriggers*> CMapTriggers;
CMapTriggers* // Pre-definition
P2PmsgHeap_DropTriggers ( CMapTriggers *pCMapTriggers );
typedef CMap<VBLaddr,VBLaddr,VBLaddr,VBLaddr> CMapAlloc;
typedef struct
{
std::atomic<int> nRefCount; // atomic: AddRef/Close race across pump threads (msg IOMAGE handle share)
UINT08 uVBListType;
UINT08 uAddrType;
VBLsize nSizeofUsed;
VBLsize nSizeofInit;
VBLsize nSizeofAlloc;
VBLsize nSizeofMax;
VBLsize nVBLockMin;
char *pVBListBuffer;
//UINT aVBListBuffer;
VBLelem nFreeEntries;
BOOL bEoD;
BOOL bDirty;
// Headless trigger sink (runtime-only; NOT part of the persisted image, which
// is *pBSTRio/*pIOMAGE). Zero-initialised by the ZeroMemory in every handle
// allocation below, so a manager with no sink installed leaves this nullptr.
P2PmsgTriggerSink pfnTriggerSink;
void* pTriggerSinkUser;
// Inode generations (runtime-only, like the sink above, and zero until the
// first P2PmsgHeap_InodeStamp). Refer that function for the whole of it.
CMapAlloc *pGenerations;
UINT32 nInodeStamp;
// Report budgets for the two collation refusals. Runtime-only, like the
// sink above, and zero-initialised by the ZeroMemory in every handle
// allocation below.
//
// WHY BUDGETS AND NOT PLAIN REPORTS: both refusals sit inside a per-block
// loop walking structure that came off the wire, so ONE malformed image
// reaches them once per block. Reporting each occurrence let a remote peer
// drive an unbounded volume of log writes with a single bad image -- the
// same shape of defect as the F11 hang itself: attacker-chosen input
// consuming an unbounded amount of something.
//
// MEASURED, NOT SUPPOSED. Fuzz run 32043591227 (recv_image, 600s, 58,514
// runs) emitted 157,875 report lines and a 27MB step log:
//
// 152,692 "Internal address corruption" 96.7%
// 5,183 "free neighbour declares zero size" 3.3%
//
// Note which one dominates. The zero-size refusal is the new one, and it is
// the small half; the address-corruption report has been emitting per block
// all along and was simply never measured under fuzzing. Budgeting only the
// new line -- the first thing tried here -- cut 3% of the volume, and cut
// none of it in practice, because these arrive across many short-lived
// heaps rather than many hits on one heap.
//
// So the REFUSALS stay unconditional -- each one is what makes its loop
// terminate or stops it acting on damaged structure, and both must run
// every time -- while the REPORTS are budgeted to the first per heap, with
// P2PmsgHeap_Close emitting the tallies. A malformed image costs a bounded
// number of lines, and no occurrence goes unrecorded.
VBLsize nCollateZeroSize;
VBLsize nCollateCorrupt;
union
{
struct
{
VBLaddr uVBLock0;
VBLaddr uVBLock1;
VBLaddr uVBLock2;
CMapAlloc *pCMapAlloc;
} SYS;
struct
{
//#ifndef XCtrlKeys
VBLockRoot oRoot; // Deprecated by oCtrlKeys
//#else
VBHeapRoot *pRoot; // VBHeapRoot control keys
//#endif
VBLaddr uHiWM;
VBLaddr aIOmage;
VBListIOmage *pIOmage;
VBHeapIOMAGE *pIOMAGE; // Raw image pointer
} IOMAGE;
struct
{
//VBLockRoot oRoot;
//VBListBSTRio oBSTRio;
CMapTriggers *pCMapTriggers;
VBHeap_CtrlKeys oCtrlKeys; // VBHeap control keys
VBLaddr uHiWM;
VBLaddr aBSTRio;
VBListBSTRio *pBSTRio; // Raw image pointer
} BSTRio;
struct
{
//VBLockRoot oRoot;
//VBListBSTRio oBSTRio;
CMapTriggers *pCMapTriggers;
VBHeap_CtrlKeys oCtrlKeys; // VBHeap control keys
VBLaddr uHiWM;
VBLaddr aBSTRio;
VBListBSTRio *pBSTRio; // Raw image pointer
} BSTRio64;
struct
{
VBLockRoot oRoot;
VBLaddr uHiWM;
VBLaddr aIOmage;
VBListIOmage *pIOmage;
} CHUNK;
} u;
} VBListHANDLE;
//
// Ceiling on how many blocks a heap could possibly contain: every block
// carries at least a header, so the image size divided by the header size
// bounds the count. Used to terminate the free-list walks, whose links come
// off the wire and can be made to form a cycle. A count suffices -- a cycle
// must revisit an address, so it must exceed the number of distinct blocks
// that can exist, and no set of visited addresses need be remembered.
//
static VBLsize
P2PmsgHeap_MaxBlocks ( P2PmsgHANDLE hVBHeap ) noexcept
{
const VBListHANDLE *pHandle = static_cast<VBListHANDLE *>(hVBHeap);
const VBLsize nHdr = P2PmsgHeap_Sizeof_Hdr ( hVBHeap );
if ( nHdr == 0 )
return 0;
return pHandle->nSizeofAlloc / nHdr + 1;
}
// Sizing VBLockHdr's, eliminates need to contract empty objects
// const VBLockHdr soHdr = { 0 };
const VBLock soVBLock = { 0 };
// F10. How many grow-and-retry passes an allocation gets before the free list
// is declared not to describe the image. One is the legitimate answer -- a
// resize that honours the requested size creates a block that satisfies it --
// so this is one more than necessary and still bounds the geometric runaway
// that a corrupt free list otherwise produces. See P2PmsgHeap_AllocBSTRio.
static const int kMaxAllocResizes = 2;
// How many blocks that ALREADY FIT the request the allocator will look at
// before settling for the best of them.
//
// This allocator was first-fit from the head of a LIFO free list, and the two
// together are worse than either: a freed block goes to the head, so the head
// is whichever block was freed last, and first-fit takes it whatever its size.
// Free an 800-byte block and then a 50-byte one, ask for 50, and the 800 is
// split -- the 50 sits on the list untouched, and the next 800-byte request
// cannot be served by what is left of the block that used to serve it. The
// boundary tag (refer the note above VBHeap_FootMagic) repairs the case where
// two such blocks are ADJACENT, by merging them back into one. It cannot
// repair this one: live data sits between them and no merge is possible, so
// the only repair left is to choose better among the blocks that exist.
//
// BOUNDED, because "best fit" across a whole free list is a walk of the whole
// free list on every single allocation, and this list can be long. Eight is
// chosen against the list's own order rather than as a round number: the list
// is LIFO, so the blocks nearest the head are the most recently freed, which
// in a container being emptied and refilled -- the workload that produced the
// 824-bytes-a-round drift this and the boundary tag were found by -- are
// exactly the blocks about to be asked for again. A walk of the whole list
// would spend most of its time on the part least likely to help.
//
// Blocks too small to serve the request do NOT count against this budget:
// they were walked past before this change and are walked past after it. What
// is budgeted is only the walking this change ADDS.
static const VBLsize kMaxFitWalk = 8;
///////////////////////////////////////////////////////////////////////
// VBHeap private operations
// NOTES: Used to expose VBHeap size etc
UINT
VBList_VBHeapMin ( UCHAR uVBLaddr )
{
//VBLock oVBLock;
uVBLaddr = uVBLaddr & VBLock_AddrMask;
if ( uVBLaddr == VBLock_Addr32 )
return sizeof(VBHeap::oHdr) - sizeof(VBHeap::oHdr.u) + sizeof(VBHeap::oHdr.u.nSize32)
+ sizeof(VBHeap::ud.oHeap32);
if ( uVBLaddr == VBLock_Addr64 )
return sizeof(VBHeap::oHdr) - sizeof(VBHeap::oHdr.u) + sizeof(VBHeap::oHdr.u.nSize64)
+ sizeof(VBHeap::ud.oHeap64);
if ( uVBLaddr == VBLock_Addr16 )
return sizeof(VBHeap::oHdr) - sizeof(VBHeap::oHdr.u) + sizeof(VBHeap::oHdr.u.nSize16)
+ sizeof(VBHeap::ud.oHeap16);
if ( uVBLaddr == VBLock_Addr08 )
return sizeof(VBHeap::oHdr) - sizeof(VBHeap::oHdr.u) + sizeof(VBHeap::oHdr.u.nSize08)
+ sizeof(VBHeap::ud.oHeap08);
ASSERT(0);
return 0;
}
///////////////////////////////////////////////////////////////////////
// Boundary tags
// NOTES: This heap could only ever coalesce FORWARDS. Collate merges a freed
// block with its next physical neighbour, and there was no way to go
// the other way: a block is found by its address, its header sits at
// the front, and nothing in the image says how far back the previous
// block began. So a run of frees reclaimed its space only if it ran
// high address to low. Low to high -- which is what almost everything
// does, P3PmsgDesc::Truncate deleting child 0 over and over being the