summaryrefslogblamecommitdiffstats
path: root/private/mvdm/suballoc/suballoc.c
blob: c2021ec4c25417f8d2f49305bed2bd21003069fb (plain) (tree)
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


































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                               
/*++

Copyright (c) 1992  Microsoft Corporation

Module Name:

    suballoc.c

Abstract:

    This module contains code for managing a paritially commited address
    space.  It handles allocation of chunks of memory smaller than the 
    commit granularity.  It commits and decommits memory as needed using
    the supplied function for committing and decommitting memory.  The 
    structures used for tracking the address space are allocated outside
    of the specified addresss space.

Author:

    Dave Hastings (daveh) creation-date 21-Jan-1994
    
Notes:

    Since this package does not actually access memory in the address space
    it is managing, it will work as well with real linear addresses or 
    "Intel Addresses" such as would be encountered with the Insignia Emulator
    on risc.

Revision History:


--*/
#include "suballcp.h"

PVOID
SAInitialize(
    ULONG BaseAddress,
    ULONG Size,
    PSACOMMITROUTINE CommitRoutine,
    PSACOMMITROUTINE DecommitRoutine,
    PSAMEMORYMOVEROUTINE MemoryMoveRoutine
    )
/*++

Routine Description:

    This function performs initialization of the sub allocation package
    for the specified addresss range.  It allocates the data structures
    necessary to track the allocations

Arguments:

    BaseAddress -- Supplies the base address of the address space to
        sub allocate.
    Size -- Supplies the size in bytes of the address space to sub allocate.
    CommitRoutine -- Supplies a pointer to the routine used to commit regions
        of the address space.

Return Value:

    If the function was successful it returns a pointer to the sub-allocation
    data structures.  Otherwise it returns NULL.
    
--*/
{
    PSUBALLOCATIONDATA SubAlloc;
    ULONG SASize;

    ASSERT_STEPTHROUGH;
    //
    // Asserts to insure that everything is as we expect it to be
    //
    ASSERT(((COMMIT_GRANULARITY % SUBALLOC_GRANULARITY) == 0));
        
    //
    // Allocate the tracking structure
    // 
    // SUBALLOCATIONDATA is declared with 1 uchar for the bitmap.
    // this is the reason for subtracting one from the total size
    // calculation.
    //
    SASize = sizeof(SUBALLOCATIONDATA) 
        + (Size / SUBALLOC_GRANULARITY) / sizeof(UCHAR) - 1;

    SubAlloc = malloc(SASize);
    
    if (SubAlloc == NULL) {
        return NULL;
    }
    
    //
    // Initialize the structure
    //
    RtlZeroMemory(SubAlloc, SASize);
    
    INIT_SUBALLOC_SIGNATURE(SubAlloc);
    SubAlloc->BaseAddress = BaseAddress;
    SubAlloc->Size = Size / SUBALLOC_GRANULARITY;
    SubAlloc->CommitRoutine = CommitRoutine;
    SubAlloc->DecommitRoutine = DecommitRoutine;
    SubAlloc->MoveMemRoutine = MemoryMoveRoutine;

    return SubAlloc;    
}

BOOL 
SAQueryFree(
    PVOID SubAllocation,
    PULONG FreeBytes,
    PULONG LargestFreeBlock
    )    
/*++

Routine Description:

    This routine returns the number of free bytes in the 
    sub allocated address space.

Arguments:
    
    SubAllocation -- Supplies the pointer returned by SAInitialize
    FreeBytes -- Returns the number of free bytes
   
Return Value:

    TRUE -- if successful, and FreeBytes contains the number of free bytes.
    FALSE otherwise
    
--*/
{
    ULONG i, FreeCount;
    PSUBALLOCATIONDATA SubAlloc;
    ULONG TempLargest, LargestBlock;
    
    ASSERT_STEPTHROUGH;
    
    //
    // Get a typed pointer
    //
    SubAlloc = SubAllocation;
    
    //
    // Make sure that we have what we think we do
    //
    ASSERT_SUBALLOC(SubAlloc);
    
    //
    // Count the free chunks and find largest block
    //
    FreeCount = 0;
    LargestBlock = 0;
    i = 0;
    while (i < SubAlloc->Size) {
        
        TempLargest = 0;
        while (
            (i < SubAlloc->Size) && 
            (GET_BIT_FROM_CHAR_ARRAY(SubAlloc->Allocated, i) == 0) 
        ){
            FreeCount++;
            TempLargest++;
            i++;
        }
    
        if (TempLargest > LargestBlock) {
            LargestBlock = TempLargest;
        }
        
        //
        // Skip allocated blocks
        //
        while (
            (i < SubAlloc->Size) && 
            (GET_BIT_FROM_CHAR_ARRAY(SubAlloc->Allocated, i) == 1)
        ) {
            i++;
        }
    }
    
    *FreeBytes = FreeCount * SUBALLOC_GRANULARITY;
    *LargestFreeBlock = LargestBlock * SUBALLOC_GRANULARITY;
    return TRUE;
}

BOOL
SAAllocate(
    PVOID SubAllocation,
    ULONG Size,
    PULONG Address
    )
/*++

Routine Description:

    This function allocates a portion of the address space described by 
    SubAllocation.  If necessary, it will commit additional blocks. 
    Size is rounded up to the next higher multiple of SUBALLOC_GRANULARITY.

Arguments:

    SubAllocation -- Supplies the pointer returned by SAInitialize.
    Size -- Supplies the size in bytes of the region to allocate.
    Address -- Returns the address of the region allocated.

Return Value:

    TRUE if successful.  If false is returned, no address is returned.
    
Notes:

    Zero is a valid value for the returned address.

--*/
{
    ULONG AllocateSize, i, CurrentChunk;
    BOOL Done = FALSE;
    PSUBALLOCATIONDATA SubAlloc;
    BOOL Success;
    
    ASSERT_STEPTHROUGH;
    
    //
    // Get a typed pointer.  This allows us to avoid
    // casting every time we access the pointer.
    //
    SubAlloc = SubAllocation;

    ASSERT_SUBALLOC(SubAlloc);
    
    //
    // Round size and make into number of blocks
    //
    AllocateSize = ALLOC_ROUND(Size);

    //
    // Find a chunk that is free
    //
    // We need this loop in spite of the fact that we 
    // are keeping an index to the first free block.
    // We update this pointer somewhat heuristically.
    // If we allocate the first free block, we update
    // the index to point past the block we allocated.
    // We don't repeat the free scan however, so the 
    // index may actually point to an allocated block.
    //
    CurrentChunk = SubAlloc->FirstFree;
    while (CurrentChunk < SubAlloc->Size) {
        if (GET_BIT_FROM_CHAR_ARRAY(SubAlloc->Allocated, CurrentChunk) == 0) {
            SubAlloc->FirstFree = CurrentChunk;
            break;
        }
        CurrentChunk++;
    }

    //
    // Find a block that is big enough
    //
    while (!Done && (CurrentChunk < SubAlloc->Size)){
    
        //
        // Search for a contiguous block large enough
        //
        for (i = 0; i < AllocateSize; i++){
            //
            // Insure we don't walk off the end of the data structure
            //
            if ((i + CurrentChunk) >= SubAlloc->Size){
                CurrentChunk += i; // Satisfy termination condition
                break;
            }
            
            //
            // Check to see if this chunk is free
            //
            if (
                GET_BIT_FROM_CHAR_ARRAY(
                    SubAlloc->Allocated, 
                    i + CurrentChunk
                    ) 
                    == 0
            ){
                continue;
            } else {
                //
                // Chunk is not free, so advance the search
                //
                CurrentChunk += i + 1;
                break;
            }
        }
        
        //
        // Check to see if we found a chunk 
        //
        if (i == AllocateSize) {
            Done = TRUE;
        } 
    }
    
    //
    // If we found the chunk, commit it (if necessary) and mark it allocated
    //
    // N.B.  It is important to commit it first, and mark it allocated last, 
    //       because we use the allocated bits to determine if the chunk is 
    //       committed.  If all of the allocated bits are clear, the chunk 
    //       is not commited yet.
    //
    if (Done) {

        //
        // Allocate and commit the memory
        //
        Success = AllocateChunkAt(
            SubAlloc,
            AllocateSize,
            CurrentChunk,
            FALSE
            );
        
        if (!Success) {
            return FALSE;
        }
        
        *Address = BLOCK_INDEX_TO_ADDRESS(SubAlloc, CurrentChunk);

        ASSERT((SubAlloc->BaseAddress <= *Address) && 
            ((SubAlloc->BaseAddress + SubAlloc->Size * SUBALLOC_GRANULARITY)
            > *Address));
#if 0            
        {
            char Buffer[80];
            
            sprintf(Buffer, "SAAllocate: Allocating at address %lx\n", *Address);
            OutputDebugString(Buffer);
        }
#endif        
        return TRUE;
        
    } else {
        return FALSE;
    }
}

BOOL
SAFree(
    PVOID SubAllocation,
    ULONG Size,
    ULONG Address
    )
/*++

Routine Description:

    This routine frees a sub-allocated chunk of memory.  If the 
    entire commited block (or blocks) that the specified chunk
    belongs to are free, the chunks are decommitted.  Address is 
    rounded down to the next lower SUBALLOC_GRANULARITY boundary.
    size is rounded up to the next higher multiple of SUBALLOC_GRANULARITY.

Arguments:

    SubAllocation -- Supplies the pointer returned by SAInitialize.
    Size -- Supplies the size in bytes of the region to free.
    Address -- Supplies the address of the region to free.

Return Value:

    TRUE if successful.
    
Notes:

    It is possible to free a different size at a particular 
    address than was allocated.  This will not cause the 
    SubAllocation package any problems.

    BUGBUG decommit error handling?    
--*/
{
    PSUBALLOCATIONDATA SubAlloc;
    ULONG AllocatedSize, BaseBlock;
    
    SubAlloc = SubAllocation;
    ASSERT_SUBALLOC(SubAlloc);
    
    //
    // Make sure that the space to free is really ours
    // (Entire block within range, and correctly aligned)
    if (
        (Address < SubAlloc->BaseAddress) || 
        (Address >= (SubAlloc->BaseAddress + SubAlloc->Size * SUBALLOC_GRANULARITY)) ||
        ((Address + Size) > (SubAlloc->BaseAddress + SubAlloc->Size * SUBALLOC_GRANULARITY)) ||
        (Address % SUBALLOC_GRANULARITY)
    ) {
        return FALSE;
    }
    
    //
    // Turn Address into Block #
    //
    BaseBlock = ADDRESS_TO_BLOCK_INDEX(SubAlloc, Address);
        
    //
    // Round up the size    
    //
    AllocatedSize = ALLOC_ROUND(Size);

    return FreeChunk(
        SubAlloc,
        AllocatedSize,
        BaseBlock
        );
}

BOOL
SAReallocate(
    PVOID SubAllocation,
    ULONG OriginalSize,
    ULONG OriginalAddress,
    ULONG NewSize,
    PULONG NewAddress
    )
/*++

Routine Description:

    This routine reallocates a sub allocated block of memory.
    The sizes are rounded up to the next SUBALLOC_GRANULARITY.
    The Original address is rounded down to the next SUBALLOC_GRANULARITY
    boundary.  Only min(OriginalSize, NewSize) bytes of data are copied to
    the new block.  The block changed in place if possible.
    
    The following is an enumation of the possible successful reallocs.
    
    1.  NewSize < OriginalSize
        free block tail
    2.  NewSize > OriginalSize
        a.)  Sufficient freespace at OriginalAddress + OriginalSize
                Allocate the space at the tail of the block
        b.)  Sufficient free space at OriginalAddress - size delta
                Allocate the space at the beginning of the block, and
                copy the data.
        c.)  Sufficient space elsewhere in the address space
                Allocate the space, and copy the block.
                
    If none of the above is true, the realloc fails.  The above are 
    in order of preference.

Arguments:

    SubAllocation -- Supplies the pointer returned by SAInitialize.
    OriginalSize -- Supplies the old size in bytes of the block.
    OriginalAddress -- Supplies the old address of the block.
    NewSize -- Supplies the new size in bytes of the block.
    NewAddress -- Returns the new address of the block.

Return Value:

    True if successful.  If unsucessful, no allocation is changed.
    
Notes:

    If the caller does not supply the correct original size for the block,
    some memory may be lost, and the block may be moved unnecessarily.
    
--*/
{

    ULONG OriginalSizeBlock, NewSizeBlock, OriginalIndex;
    ULONG AdditionalBlocks, Address;
    BOOL Success;
    PSUBALLOCATIONDATA SubAlloc;

    SubAlloc = SubAllocation;
    ASSERT_SUBALLOC(SubAlloc);
    
    //
    // Convert Sizes and address to blocks
    //
    OriginalSizeBlock = ALLOC_ROUND(OriginalSize);
    NewSizeBlock = ALLOC_ROUND(NewSize);
    OriginalIndex = ADDRESS_TO_BLOCK_INDEX(SubAlloc, OriginalAddress);
    
    //
    // Check to see if we are changing the size of the block
    //
    // N.B.  Because we have rounded the numbers to an allocation
    //       boundary, the following test may succeed (correctly)
    //       even though OriginalSize != NewSize
    //
    if (OriginalSizeBlock == NewSizeBlock) {
        *NewAddress = OriginalAddress;
        return TRUE;
    }
    
    //
    // Check to see if the block is getting smaller
    //
    if (OriginalSizeBlock > NewSizeBlock) {
    
        //
        // Free the tail of the block
        //
        Success = FreeChunk(
            SubAlloc, 
            OriginalSizeBlock - NewSizeBlock,
            OriginalIndex + NewSizeBlock
            );
            
        if (Success) {
            *NewAddress = OriginalAddress;
            return TRUE;
        } else {
            return FALSE;
        }
    }
    
    //
    // Try to allocate the space at the end of the block
    //
    AdditionalBlocks = NewSizeBlock - OriginalSizeBlock;
    
    Success = AllocateChunkAt(
        SubAlloc,
        AdditionalBlocks,
        OriginalIndex + OriginalSizeBlock,
        TRUE
        );
        
    //
    // If there was space, return success
    //
    if (Success) {
        *NewAddress = OriginalAddress;
        return TRUE;
    }
    
    //
    // Try to allocate space at the beginning of the block
    //
    Success = AllocateChunkAt(
        SubAlloc,
        AdditionalBlocks,
        OriginalIndex - AdditionalBlocks,
        TRUE
        );
        
    if (Success) {
        //
        // Move the data
        //
        // N.B.  We can't just call RtlMoveMemory, 
        //       because we don't know the correspondence
        //       between the address space we manage, and 
        //       real linear addresses.  In addition, for
        //       risc NTVDM, some additional work may have
        //       to be done (such as flushing caches).
        //
        SubAlloc->MoveMemRoutine(
            BLOCK_INDEX_TO_ADDRESS(
                SubAlloc, 
                (OriginalIndex - AdditionalBlocks)
                ),
            OriginalAddress,
            OriginalSize
            );
            
        *NewAddress = BLOCK_INDEX_TO_ADDRESS(
            SubAlloc,
            (OriginalIndex - AdditionalBlocks)
            );
            
        return TRUE;
    }
    
    //
    // Attempt to allocate a new block
    //
    Success = SAAllocate(
        SubAlloc,
        NewSize,
        &Address
        );
        
    if (Success) {
        //
        // Move the data
        // 
        // N.B. We could copy the data, but it would
        //      require one more function pointer.
        //
        SubAlloc->MoveMemRoutine(
            Address,
            OriginalAddress,
            OriginalSize
            );
            
        SAFree(
            SubAlloc,
            OriginalSize,
            OriginalAddress
            );
            
        //
        // Indicate success
        //
        *NewAddress = Address;
        return TRUE;
    }
    
    //
    // All reallocation strategies failed.  
    //
    return FALSE;
}

BOOL
AllocateChunkAt(
    PSUBALLOCATIONDATA SubAlloc,
    ULONG Size,
    ULONG BlockIndex,
    BOOLEAN CheckFree
    )
/*++

Routine Description:

    This routine attempts to allocate the specified chunk
    of memory.  It first checks to make sure that it is 
    free.

Arguments:

    SubAlloc -- Supplies a pointer to the suballocation data
    Size -- Supplies the size of the chunk to allocate
    BlockIndex -- Supplies the index of the beginning of the block
        to allocate
    CheckFree -- Supplies an indication of whether to check and see
        if the memory is free.  If this routine is called from 
        SAAllocate, we know the memory is free.
        
Return Value:

    True if successful
    
--*/
{
    ULONG i;

    if (CheckFree) {
        //
        // Verify that the memory is free
        //
        for (i = 0; i < Size; i++){
            //
            // Insure we don't walk off the end of the data structure
            //
            if ((i + BlockIndex) >= SubAlloc->Size){
                break;
            }
            
            //
            // Check to see if this chunk is free
            //
            if (
                GET_BIT_FROM_CHAR_ARRAY(
                    SubAlloc->Allocated, 
                    i + BlockIndex
                    ) 
                == 0
            ){
                continue;
            } else {
                //
                // Chunk is not free
                //
                break;
            }
        }
 
        //
        // If the chunk is not free
        //   
        if (i != Size) {
            return FALSE;
        }
    }

    //
    // Commit the chunk
    //
    if (!CommitChunk(SubAlloc, BlockIndex, Size, SACommit)) {
        return FALSE;
    }
    
    //
    // Mark it as allocated
    //    
    for (i = BlockIndex; i < BlockIndex + Size; i++) {
        SET_BIT_IN_CHAR_ARRAY(SubAlloc->Allocated, i);
    }
    
    //
    // Update the pointer to the first free block
    //
    if (BlockIndex == SubAlloc->FirstFree) {
        SubAlloc->FirstFree += Size;
    }
    
    return TRUE;
}


BOOL
FreeChunk(
    PSUBALLOCATIONDATA SubAlloc,
    ULONG Size,
    ULONG BlockIndex
    )
/*++

Routine Description:

    This routine actually marks the memory as free
    and decommits it as necessary.

Arguments:

    SubAlloc -- Supplies a pointer to the suballocation data
    Size -- Supplies the size (in SUBALLOC_GRANULARITY) of the 
        region to free
    BlockIndex -- Supplies the index of the begining of the region
        (in SUBALLOC_GRANULARITY)
        
Return Value:

    TRUE if successful.

--*/
{
    SUBALLOCATIONDATA LocalSubAlloc;
    ULONG CurrentBlock;
    BOOL Success;
    
    //
    // Save a copy of the suballoc data
    //
    LocalSubAlloc = *SubAlloc;
    
    //
    // reset free pointer
    //
    if (BlockIndex < SubAlloc->FirstFree) {
        SubAlloc->FirstFree = BlockIndex;
    }
    //    
    // Mark the region as free
    //
    // N.B.  We mark the block as free before decommitting it, because
    //       the decommit code will use the allocated bits to determine which
    //       parts can be decommitted.
    //
    for (CurrentBlock = BlockIndex; 
        CurrentBlock < BlockIndex + Size; 
        CurrentBlock++
    ) {
        CLEAR_BIT_IN_CHAR_ARRAY(SubAlloc->Allocated, CurrentBlock);
    }
    
    //
    // Decommit the memory
    //
    Success = CommitChunk(SubAlloc, BlockIndex, Size, SADecommit);
    
    if (!Success) {
        *SubAlloc = LocalSubAlloc;
    }
    
    return Success;
}

BOOL
CommitChunk(
    PSUBALLOCATIONDATA SubAlloc,
    ULONG StartChunk,
    ULONG Size,
    COMMIT_ACTION Action
    )
/*++

Routine Description:

    This routine commits a chunk of memory.  Part or all
    of the specified chunk may already be commited.

Arguments:

    SubAllocation -- Supplies a pointer to the suballocation data
    StartChunk -- Supplies the relative start of the region to be  
        committed (in SUBALLOCATION_GRANULARITY)
    Size -- Supplies the size of the chunk to be commited 
        (in SUBALLOCATION_GRANULARITY)
    
Return Value:

    TRUE -- If the block was successfully committed (or already committed)
    FALSE -- Otherwise

Notes:
    
    This routine depends on the allocated bits in SubAlloc to determine
    whether memory is committed.  When memory is to be committed, CommitBlock
    must be called before the Allocated bits are modified.  When memory is 
    decommitted, the Allocated bits must be modified before CommitBlock is
    called.
           
--*/
{
    ULONG FirstBlock, LastBlock, CurrentBlock;
    NTSTATUS Status;
    
    ASSERT_STEPTHROUGH;

    ASSERT_SUBALLOC(SubAlloc);
    
    //
    // Round Start down to next COMMIT_GRANULARITY and convert to block #
    //
    FirstBlock = (StartChunk * SUBALLOC_GRANULARITY) / COMMIT_GRANULARITY;
    
    //
    // Round StartChunk + size up to next COMMIT_GRANULARITY
    //
    LastBlock = ((StartChunk + Size) * SUBALLOC_GRANULARITY + 
        (COMMIT_GRANULARITY - 1)) / COMMIT_GRANULARITY;
    
    for (
        CurrentBlock = FirstBlock; 
        CurrentBlock < LastBlock; 
        CurrentBlock++
    ) {
        
        //
        // If the block is not committed, either commit it or decommit it, 
        // depending on the value of Action.
        //
        if (!IsBlockCommitted(SubAlloc, CurrentBlock)) {
            if (Action == SACommit) {
            
                Status = (SubAlloc->CommitRoutine)(
                    CurrentBlock * COMMIT_GRANULARITY + SubAlloc->BaseAddress,
                    COMMIT_GRANULARITY
                    );
                
            } else if (Action == SADecommit) {

                Status = (SubAlloc->DecommitRoutine)(
                    CurrentBlock * COMMIT_GRANULARITY + SubAlloc->BaseAddress,
                    COMMIT_GRANULARITY
                    );

            }
            if (Status != STATUS_SUCCESS) {
            //
            // Bugbug -- decommit any blocks committed here
            //
                return FALSE;
            }
        }
    }
    return TRUE;
}

BOOL
IsBlockCommitted(
    PSUBALLOCATIONDATA SubAlloc,
    ULONG Block
    )
/*++

Routine Description:

    This routine checks to see if a particular block of the
    suballocation is committed.  

Arguments:

    SubAlloc -- Supplies a pointer to the suballocation data
    Block -- Supplies the number of the block to check
    
Return Value:

    TRUE -- if the block is committed
    FALSE -- if the block is not committed

Notes:

    The return value is based on the state of the bits in the 
    suballocation data, not on information from the NT memory 
    manager.    
    
--*/
{
    BOOL Committed = FALSE;
    ULONG i;
    
    ASSERT_STEPTHROUGH;
    ASSERT_SUBALLOC(SubAlloc);
    
    //
    // Check the bits for each of the suballoc blocks in the 
    // commit block
    //
    for (i = 0; i < COMMIT_GRANULARITY / SUBALLOC_GRANULARITY; i++) {
        
        //
        // Check to see if this suballoc block is allocated
        //    
        if (
            GET_BIT_FROM_CHAR_ARRAY(
                SubAlloc->Allocated, 
                i + Block * COMMIT_GRANULARITY / SUBALLOC_GRANULARITY
                )
        ) {
            Committed = TRUE;
            break;
        }
    }
    
    return Committed;
}