1111//===----------------------------------------------------------------------===//
1212
1313/// A ``BumpPtrAllocator`` that allocates `slabSize` at a time.
14+ /// `slabSize` initiates with `initialSlabSize` and doubles periodically as allocations occur.
1415/// Once all memory in a slab has been used, it allocates a new slab and no
1516/// memory allocations are necessary until that slab is completely filled up.
1617@_spi ( BumpPtrAllocator) @_spi ( Testing)
@@ -20,8 +21,7 @@ public class BumpPtrAllocator {
2021 static private let GROWTH_DELAY : Int = 128
2122 static private let SLAB_ALIGNMENT : Int = 8
2223
23- /// Initial slab size.
24- private var slabSize : Int
24+ private let initialSlabSize : Int
2525
2626 private var slabs : [ Slab ]
2727 /// Pair of pointers in the current slab.
@@ -38,8 +38,8 @@ public class BumpPtrAllocator {
3838 private var _totalBytesAllocated : Int
3939
4040 /// Construct a new ``BumpPtrAllocator``.
41- public init ( slabSize : Int ) {
42- self . slabSize = slabSize
41+ public init ( initialSlabSize : Int ) {
42+ self . initialSlabSize = initialSlabSize
4343 slabs = [ ]
4444 current = nil
4545 customSizeSlabs = [ ]
@@ -48,8 +48,6 @@ public class BumpPtrAllocator {
4848
4949 deinit {
5050 /// Deallocate all memory.
51- _totalBytesAllocated = 0
52- current = nil
5351 while let slab = slabs. popLast ( ) {
5452 slab. deallocate ( )
5553 }
@@ -61,7 +59,7 @@ public class BumpPtrAllocator {
6159 /// Calculate the size of the slab at the index.
6260 private func slabSize( at index: Int ) -> Int {
6361 // Double the slab size every 'GROWTH_DELAY' slabs.
64- return self . slabSize * ( 1 << min ( 30 , index / Self. GROWTH_DELAY) )
62+ return self . initialSlabSize * ( 1 << min ( 30 , index / Self. GROWTH_DELAY) )
6563 }
6664
6765 private func startNewSlab( ) {
@@ -114,7 +112,7 @@ public class BumpPtrAllocator {
114112 }
115113
116114 // If the size is too big, allocate a dedicated slab for it.
117- if byteCount >= self . slabSize {
115+ if byteCount >= self . initialSlabSize {
118116 let customSlab = Slab . allocate (
119117 byteCount: byteCount,
120118 alignment: alignment
0 commit comments