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
//! Implements the general purpose allocator.

use std::ffi::c_void;

#[cfg(feature = "vk-dedicated-allocation")]
use ash::version::DeviceV1_1;
use ash::version::InstanceV1_0;
use ash::vk;
use slotmap::{new_key_type, SlotMap};
#[cfg(feature = "tracing")]
use tracing::debug;

#[cfg(feature = "tracing")]
use crate::debug_memory_types;
use crate::{
    align_up, find_memory_type_index, AllocationType, AllocatorError, AllocatorStatistic,
    MemoryUsage, Result,
};
use crate::{AllocationInfo, MemoryBlock};

// For a minimal bucket size of 256b as log2.
const MINIMAL_BUCKET_SIZE_LOG2: u32 = 8;

/// The general purpose memory allocator. Implemented as a segregated list allocator.
pub struct Allocator {
    device: ash::Device,
    buffer_pools: Vec<MemoryPool>,
    image_pools: Vec<MemoryPool>,
    block_size: u64,
    memory_types_size: u32,
    memory_properties: vk::PhysicalDeviceMemoryProperties,
}

impl Allocator {
    /// Creates a new allocator.
    #[cfg_attr(feature = "profiling", profiling::function)]
    pub fn new(
        instance: &ash::Instance,
        physical_device: vk::PhysicalDevice,
        logical_device: &ash::Device,
        descriptor: &AllocatorDescriptor,
    ) -> Self {
        let memory_properties =
            unsafe { instance.get_physical_device_memory_properties(physical_device) };

        let memory_types =
            &memory_properties.memory_types[..memory_properties.memory_type_count as usize];

        #[cfg(feature = "tracing")]
        debug_memory_types(memory_properties, memory_types);

        let memory_types_size = memory_types.len() as u32;

        let block_size = (2u64).pow(descriptor.block_size as u32);

        let buffer_pools = memory_types
            .iter()
            .enumerate()
            .map(|(i, memory_type)| {
                MemoryPool::new(
                    i as u32,
                    block_size,
                    i,
                    memory_type
                        .property_flags
                        .contains(vk::MemoryPropertyFlags::HOST_VISIBLE),
                )
            })
            .collect();

        let image_pools = memory_types
            .iter()
            .enumerate()
            .map(|(i, memory_type)| {
                MemoryPool::new(
                    memory_types_size + i as u32,
                    (2u64).pow(descriptor.block_size as u32),
                    i,
                    memory_type
                        .property_flags
                        .contains(vk::MemoryPropertyFlags::HOST_VISIBLE),
                )
            })
            .collect();

        Self {
            device: logical_device.clone(),
            buffer_pools,
            image_pools,
            block_size,
            memory_types_size,
            memory_properties,
        }
    }

    /// Allocates memory for a buffer.
    ///
    /// Required the following Vulkan extensions:
    ///  - VK_KHR_get_memory_requirements2
    ///  - VK_KHR_dedicated_allocation
    #[cfg(feature = "vk-dedicated-allocation")]
    pub fn allocate_memory_for_buffer(
        &mut self,
        buffer: vk::Buffer,
        location: MemoryUsage,
    ) -> Result<Allocation> {
        let info = vk::BufferMemoryRequirementsInfo2::builder().buffer(buffer);
        let mut dedicated_requirements = vk::MemoryDedicatedRequirements::builder();
        let mut requirements =
            vk::MemoryRequirements2::builder().push_next(&mut dedicated_requirements);

        unsafe {
            self.device
                .get_buffer_memory_requirements2(&info, &mut requirements);
        }

        let memory_requirements = requirements.memory_requirements;

        let is_dedicated = dedicated_requirements.prefers_dedicated_allocation == 1
            || dedicated_requirements.requires_dedicated_allocation == 1;

        let alloc_decs = AllocationDescriptor {
            requirements: memory_requirements,
            location,
            allocation_type: AllocationType::Buffer,
            is_dedicated,
        };

        self.allocate(&alloc_decs)
    }

    /// Allocates memory for an image.
    ///
    /// Required the following Vulkan extensions:
    ///  - VK_KHR_get_memory_requirements2
    ///  - VK_KHR_dedicated_allocation
    #[cfg(feature = "vk-dedicated-allocation")]
    pub fn allocate_memory_for_image(
        &mut self,
        image: vk::Image,
        location: MemoryUsage,
    ) -> Result<Allocation> {
        let info = vk::ImageMemoryRequirementsInfo2::builder().image(image);
        let mut dedicated_requirements = vk::MemoryDedicatedRequirements::builder();
        let mut requirements =
            vk::MemoryRequirements2::builder().push_next(&mut dedicated_requirements);

        unsafe {
            self.device
                .get_image_memory_requirements2(&info, &mut requirements);
        }

        let memory_requirements = requirements.memory_requirements;

        let is_dedicated = dedicated_requirements.prefers_dedicated_allocation == 1
            || dedicated_requirements.requires_dedicated_allocation == 1;

        let alloc_decs = AllocationDescriptor {
            requirements: memory_requirements,
            location,
            allocation_type: AllocationType::OptimalImage,
            is_dedicated,
        };

        self.allocate(&alloc_decs)
    }

    /// Allocates memory on the allocator.
    //
    // For each memory type we have two memory pools: For linear resources and for optimal textures.
    // This removes the need to check for the granularity between them and the idea is, that
    // buffers/textures have different lifetimes and internal fragmentation is smaller this way.
    //
    // Dedicated blocks still exists in their respective pools. They are de-allocated when
    // they are freed. Normal blocks are not de-allocated.
    //
    // Each pool has fixed sized blocks that need to be of power two size. Each block has at
    // least one chunk.
    //
    // Free chunks are saved in a segregated list with buckets of power of two sizes.
    // The biggest bucket size is the block size.
    #[cfg_attr(feature = "profiling", profiling::function)]
    pub fn allocate(&mut self, descriptor: &AllocationDescriptor) -> Result<Allocation> {
        let size = descriptor.requirements.size;
        let alignment = descriptor.requirements.alignment;

        #[cfg(feature = "tracing")]
        debug!(
            "Allocating {} bytes with an alignment of {}.",
            size, alignment
        );

        if size == 0 || !alignment.is_power_of_two() {
            return Err(AllocatorError::InvalidAlignment);
        }

        let memory_type_index = find_memory_type_index(
            &self.memory_properties,
            descriptor.location,
            descriptor.requirements.memory_type_bits,
        )?;

        let pool = match descriptor.allocation_type {
            AllocationType::Buffer | AllocationType::LinearImage => {
                &mut self.buffer_pools[memory_type_index]
            }
            AllocationType::OptimalImage => &mut self.image_pools[memory_type_index],
        };

        if descriptor.is_dedicated || size >= self.block_size {
            pool.allocate_dedicated(&self.device, size)
        } else {
            pool.allocate(&self.device, size, alignment)
        }
    }

    /// Frees the allocation.
    #[cfg_attr(feature = "profiling", profiling::function)]
    pub fn free(&mut self, allocation: Allocation) -> Result<()> {
        let memory_pool = if allocation.pool_index > self.memory_types_size {
            &mut self.image_pools[(allocation.pool_index - self.memory_types_size) as usize]
        } else {
            &mut self.buffer_pools[allocation.pool_index as usize]
        };

        if let Some(chunk_key) = allocation.chunk_key {
            memory_pool.free(chunk_key)?;
        } else {
            // Dedicated block
            let mut block = memory_pool
                .blocks
                .remove(allocation.block_key)
                .ok_or_else(|| {
                    AllocatorError::Internal("can't find block key in block slotmap".to_owned())
                })?;
            block.destroy(&self.device);
        }

        Ok(())
    }
}

impl Drop for Allocator {
    fn drop(&mut self) {
        let device = self.device.clone();
        self.buffer_pools.iter_mut().for_each(|pool| {
            pool.blocks
                .iter_mut()
                .for_each(|(_, block)| block.destroy(&device))
        });
        self.image_pools.iter_mut().for_each(|pool| {
            pool.blocks
                .iter_mut()
                .for_each(|(_, block)| block.destroy(&device))
        });
    }
}

impl AllocatorStatistic for Allocator {
    fn allocation_count(&self) -> usize {
        let buffer_count: usize = self
            .buffer_pools
            .iter()
            .flat_map(|buffer| &buffer.chunks)
            .filter(|(_, chunk)| !chunk.is_free)
            .count();
        let image_count: usize = self
            .image_pools
            .iter()
            .flat_map(|buffer| &buffer.chunks)
            .filter(|(_, chunk)| !chunk.is_free)
            .count();
        let dedicated_buffer_count = self
            .buffer_pools
            .iter()
            .flat_map(|pool| &pool.blocks)
            .filter(|(_, block)| block.is_dedicated)
            .count();
        let dedicated_image_count = self
            .image_pools
            .iter()
            .flat_map(|pool| &pool.blocks)
            .filter(|(_, block)| block.is_dedicated)
            .count();

        buffer_count + image_count + dedicated_buffer_count + dedicated_image_count
    }

    fn unused_range_count(&self) -> usize {
        count_unused_ranges(&self.buffer_pools) + count_unused_ranges(&self.image_pools)
    }

    fn used_bytes(&self) -> u64 {
        let buffer_bytes: u64 = self
            .buffer_pools
            .iter()
            .flat_map(|buffer| &buffer.chunks)
            .filter(|(_, chunk)| !chunk.is_free)
            .map(|(_, chunk)| chunk.size)
            .sum();
        let image_bytes: u64 = self
            .image_pools
            .iter()
            .flat_map(|buffer| &buffer.chunks)
            .filter(|(_, chunk)| !chunk.is_free)
            .map(|(_, chunk)| chunk.size)
            .sum();
        let dedicated_buffer_bytes: u64 = self
            .buffer_pools
            .iter()
            .flat_map(|buffer| &buffer.blocks)
            .filter(|(_, block)| block.is_dedicated)
            .map(|(_, chunk)| chunk.size)
            .sum();
        let dedicated_image_bytes: u64 = self
            .image_pools
            .iter()
            .flat_map(|buffer| &buffer.blocks)
            .filter(|(_, block)| block.is_dedicated)
            .map(|(_, chunk)| chunk.size)
            .sum();

        buffer_bytes + image_bytes + dedicated_buffer_bytes + dedicated_image_bytes
    }

    fn unused_bytes(&self) -> u64 {
        count_unused_bytes(&self.buffer_pools) + count_unused_bytes(&self.image_pools)
    }

    fn block_count(&self) -> usize {
        let buffer_sum: usize = self.buffer_pools.iter().map(|pool| pool.blocks.len()).sum();
        let image_sum: usize = self.image_pools.iter().map(|pool| pool.blocks.len()).sum();

        buffer_sum + image_sum
    }
}

/// Describes the configuration of an `Allocator`.
#[derive(Debug, Clone)]
pub struct AllocatorDescriptor {
    /// The size of the blocks that are allocated. Defined as log2(size in bytes). Default: 64 MiB.
    pub block_size: u8,
}

impl Default for AllocatorDescriptor {
    fn default() -> Self {
        Self { block_size: 26 }
    }
}

/// The descriptor for an allocation on the allocator.
#[derive(Debug, Clone)]
pub struct AllocationDescriptor {
    /// Location where the memory allocation should be stored.
    pub location: MemoryUsage,
    /// Vulkan memory requirements for an allocation.
    pub requirements: vk::MemoryRequirements,
    /// The type of the allocation.
    pub allocation_type: AllocationType,
    /// If the allocation should be dedicated.
    pub is_dedicated: bool,
}

/// An allocation of the `Allocator`.
#[derive(Clone, Debug)]
pub struct Allocation {
    pool_index: u32,
    block_key: BlockKey,
    chunk_key: Option<ChunkKey>,
    device_memory: vk::DeviceMemory,
    offset: u64,
    size: u64,
    mapped_ptr: Option<std::ptr::NonNull<c_void>>,
}

impl AllocationInfo for Allocation {
    /// The `vk::DeviceMemory` of the allocation. Managed by the allocator.
    fn memory(&self) -> vk::DeviceMemory {
        self.device_memory
    }

    /// The offset inside the `vk::DeviceMemory`.
    fn offset(&self) -> u64 {
        self.offset
    }

    /// The size of the allocation.
    fn size(&self) -> u64 {
        self.size
    }

    /// Returns a pointer into the mapped memory if it is host visible, otherwise returns None.
    fn mapped_ptr(&self) -> Option<std::ptr::NonNull<c_void>> {
        self.mapped_ptr
    }
}

new_key_type! {
    struct BlockKey;
    struct ChunkKey;
}

struct BestFitCandidate {
    key: ChunkKey,
    free_list_index: usize,
    free_size: u64,
}

/// A managed memory region of a specific memory type.
///
/// Used to separate buffer (linear) and texture (optimal) memory regions,
/// so that internal memory fragmentation is kept low.
struct MemoryPool {
    pool_index: u32,
    block_size: u64,
    memory_type_index: usize,
    is_mappable: bool,
    blocks: SlotMap<BlockKey, MemoryBlock>,
    chunks: SlotMap<ChunkKey, MemoryChunk>,
    free_chunks: Vec<Vec<ChunkKey>>,
    max_bucket_index: u32,
}

impl MemoryPool {
    fn new(pool_index: u32, block_size: u64, memory_type_index: usize, is_mappable: bool) -> Self {
        let blocks = SlotMap::with_capacity_and_key(1024);
        let chunks = SlotMap::with_capacity_and_key(1024);

        // The smallest bucket size is 256b, which is log2(256) = 8. So the maximal bucket size is
        // "64 - 8 - log2(block_size - 1)". We can't have a free chunk that is bigger than a block.
        let num_buckets = 64 - MINIMAL_BUCKET_SIZE_LOG2 - (block_size - 1u64).leading_zeros();

        // We preallocate only a reasonable amount of entries for each bucket.
        // The highest bucket for example can only hold two values at most.
        let free_chunks = (0..num_buckets)
            .into_iter()
            .map(|i| {
                let min_bucket_element_size = if i == 0 {
                    512
                } else {
                    2u64.pow(MINIMAL_BUCKET_SIZE_LOG2 - 1 + i)
                };
                let max_elements = (block_size / min_bucket_element_size) as usize;
                Vec::with_capacity(512.min(max_elements))
            })
            .collect();

        Self {
            pool_index,
            block_size,
            memory_type_index,
            is_mappable,
            blocks,
            chunks,
            free_chunks,
            max_bucket_index: num_buckets - 1,
        }
    }

    fn allocate_dedicated(&mut self, device: &ash::Device, size: u64) -> Result<Allocation> {
        let block = MemoryBlock::new(device, size, self.memory_type_index, self.is_mappable, true)?;

        let device_memory = block.device_memory;
        let mapped_ptr = std::ptr::NonNull::new(block.mapped_ptr);

        Ok(Allocation {
            pool_index: self.pool_index,
            block_key: self.blocks.insert(block),
            chunk_key: None,
            device_memory,
            offset: 0,
            size,
            mapped_ptr,
        })
    }

    fn allocate(&mut self, device: &ash::Device, size: u64, alignment: u64) -> Result<Allocation> {
        let mut bucket_index = calculate_bucket_index(size);

        // Make sure that we don't try to allocate a chunk bigger than the block.
        debug_assert!(bucket_index <= self.max_bucket_index);

        loop {
            // We couldn't find an empty block, so we will allocate a new one.
            if bucket_index > self.max_bucket_index {
                self.allocate_new_block(device)?;
                bucket_index = self.max_bucket_index;
            }

            let free_list = &self.free_chunks[bucket_index as usize];

            // Find best fit in this bucket.
            let mut best_fit_candidate: Option<BestFitCandidate> = None;
            for (index, key) in free_list.iter().enumerate() {
                let chunk = &mut self.chunks[*key];
                debug_assert!(chunk.is_free);

                if chunk.size < size {
                    continue;
                }

                let offset = align_up(chunk.offset, alignment);
                let padding = offset - chunk.offset;
                let aligned_size = padding + size;

                // Try to find the best fitting chunk.
                if chunk.size >= aligned_size {
                    let free_size = chunk.size - aligned_size;

                    let best_fit_size = if let Some(best_fit) = &best_fit_candidate {
                        best_fit.free_size
                    } else {
                        u64::MAX
                    };

                    if free_size < best_fit_size {
                        best_fit_candidate = Some(BestFitCandidate {
                            key: *key,
                            free_list_index: index,
                            free_size,
                        })
                    }
                }
            }

            // Allocate using the best fit candidate.
            if let Some(candidate) = &best_fit_candidate {
                self.free_chunks[bucket_index as usize].remove(candidate.free_list_index);

                // Split the lhs chunk and register the rhs as a new free chunk.
                let rhs_chunk_key = if candidate.free_size != 0 {
                    let lhs = self.chunks[candidate.key].clone();

                    let lhs_aligned_offset = align_up(lhs.offset, alignment);
                    let lhs_padding = lhs_aligned_offset - lhs.offset;
                    let rhs_offset = lhs.offset + size + lhs_padding;
                    let rhs_size = lhs.size - (lhs_padding + size);

                    let rhs_chunk_key = self.chunks.insert(MemoryChunk {
                        block_key: lhs.block_key,
                        size: rhs_size,
                        offset: rhs_offset,
                        previous: Some(candidate.key),
                        next: lhs.next,
                        is_free: true,
                    });

                    let rhs_bucket_index = calculate_bucket_index(rhs_size);
                    self.free_chunks[rhs_bucket_index as usize].push(rhs_chunk_key);

                    Some(rhs_chunk_key)
                } else {
                    None
                };

                let lhs = &mut self.chunks[candidate.key];
                lhs.is_free = false;
                lhs.offset = align_up(lhs.offset, alignment);
                lhs.size = size;

                if let Some(new_chunk_key) = rhs_chunk_key {
                    lhs.next = Some(new_chunk_key);
                }

                let block = &self.blocks[lhs.block_key];

                return Ok(Allocation {
                    pool_index: self.pool_index,
                    block_key: lhs.block_key,
                    chunk_key: Some(candidate.key),
                    device_memory: block.device_memory,
                    offset: lhs.offset,
                    size: lhs.size,
                    mapped_ptr: std::ptr::NonNull::new(block.mapped_ptr),
                });
            }

            bucket_index += 1;
        }
    }

    fn allocate_new_block(&mut self, device: &ash::Device) -> Result<()> {
        let block = MemoryBlock::new(
            device,
            self.block_size,
            self.memory_type_index,
            self.is_mappable,
            false,
        )?;
        let block_key = self.blocks.insert(block);
        let chunk_key = self.chunks.insert(MemoryChunk {
            block_key,
            size: self.block_size,
            offset: 0,
            previous: None,
            next: None,
            is_free: true,
        });

        self.free_chunks[self.max_bucket_index as usize].push(chunk_key);

        Ok(())
    }

    fn free(&mut self, chunk_key: ChunkKey) -> Result<()> {
        let (previous_key, next_key) = {
            let chunk = &mut self.chunks[chunk_key];
            chunk.is_free = true;
            (chunk.previous, chunk.next)
        };

        if let Some(next_key) = next_key {
            if self.chunks[next_key].is_free {
                self.merge_rhs_into_lhs_chunk(chunk_key, next_key, true)?;
            }
        }

        let mut is_chunk_merged = false;

        if let Some(previous_key) = previous_key {
            if self.chunks[previous_key].is_free {
                is_chunk_merged = true;
                self.merge_rhs_into_lhs_chunk(previous_key, chunk_key, false)?;
            }
        }

        if !is_chunk_merged {
            let chunk = &mut self.chunks[chunk_key];
            let chunk_bucket_index = calculate_bucket_index(chunk.size);
            self.free_chunks[chunk_bucket_index as usize].push(chunk_key);
        }

        Ok(())
    }

    fn merge_rhs_into_lhs_chunk(
        &mut self,
        lhs_chunk_key: ChunkKey,
        rhs_chunk_key: ChunkKey,
        cleanup_free_list: bool,
    ) -> Result<()> {
        let (rhs_size, rhs_offset, rhs_next) = {
            let chunk = self.chunks.remove(rhs_chunk_key).ok_or_else(|| {
                AllocatorError::Internal("chunk key not present in chunk slotmap".to_owned())
            })?;
            if cleanup_free_list {
                self.remove_from_free_list(rhs_chunk_key, chunk.size)?;
            }

            (chunk.size, chunk.offset, chunk.next)
        };

        let lhs_chunk = &mut self.chunks[lhs_chunk_key];
        lhs_chunk.next = rhs_next;
        lhs_chunk.size = (rhs_offset + rhs_size) - lhs_chunk.offset;

        if let Some(rhs_next) = rhs_next {
            let chunk = &mut self.chunks[rhs_next];
            chunk.previous = Some(lhs_chunk_key);
        }

        Ok(())
    }

    fn remove_from_free_list(&mut self, chunk_key: ChunkKey, chunk_size: u64) -> Result<()> {
        let bucket_index = calculate_bucket_index(chunk_size);
        let free_list_index = self.free_chunks[bucket_index as usize]
            .iter()
            .enumerate()
            .find(|(_, key)| **key == chunk_key)
            .map(|(index, _)| index)
            .ok_or_else(|| {
                AllocatorError::Internal(
                    "can't find chunk key in expected free list bucket".to_owned(),
                )
            })?;
        self.free_chunks[bucket_index as usize].remove(free_list_index);
        Ok(())
    }
}

/// A chunk inside a memory block. Next = None is the start chunk. Previous = None is the end chunk.
#[derive(Clone)]
struct MemoryChunk {
    block_key: BlockKey,
    size: u64,
    offset: u64,
    previous: Option<ChunkKey>,
    next: Option<ChunkKey>,
    is_free: bool,
}

#[inline]
fn calculate_bucket_index(size: u64) -> u32 {
    if size <= 256 {
        0
    } else {
        64 - MINIMAL_BUCKET_SIZE_LOG2 - (size - 1u64).leading_zeros() - 1
    }
}

fn count_unused_ranges(pools: &[MemoryPool]) -> usize {
    let mut unused_count: usize = 0;
    pools.iter().for_each(|buffer| {
        collect_start_chunks(buffer).iter().for_each(|key| {
            let mut next_key: ChunkKey = *key;
            let mut previous_size: u64 = 0;
            let mut previous_offset: u64 = 0;
            loop {
                let chunk = &buffer.chunks[next_key];
                if chunk.offset != previous_offset + previous_size {
                    unused_count += 1;
                }

                if let Some(key) = chunk.next {
                    next_key = key
                } else {
                    break;
                }

                previous_size = chunk.size;
                previous_offset = chunk.offset
            }
        });
    });
    unused_count
}

fn count_unused_bytes(pools: &[MemoryPool]) -> u64 {
    let mut unused_bytes: u64 = 0;
    pools.iter().for_each(|buffer| {
        collect_start_chunks(buffer).iter().for_each(|key| {
            let mut next_key: ChunkKey = *key;
            let mut previous_size: u64 = 0;
            let mut previous_offset: u64 = 0;
            loop {
                let chunk = &buffer.chunks[next_key];
                if chunk.offset != previous_offset + previous_size {
                    unused_bytes += chunk.offset - (previous_offset + previous_size);
                }

                if let Some(key) = chunk.next {
                    next_key = key
                } else {
                    break;
                }

                previous_size = chunk.size;
                previous_offset = chunk.offset
            }
        });
    });
    unused_bytes
}

#[inline]
fn collect_start_chunks(buffer: &MemoryPool) -> Vec<ChunkKey> {
    buffer
        .chunks
        .iter()
        .filter(|(_, chunk)| chunk.previous.is_none())
        .map(|(key, _)| key)
        .collect()
}