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
use crate::allocators::allocator::Allocator;
use crate::allocators::bit_set::bit_set_allocator::BitSetAllocator;
use crate::allocators::bump_allocator::BumpAllocator;
use crate::allocators::global::local_allocator::LocalAllocator;
use crate::allocators::global::memory_range::MemoryRange;
use crate::allocators::multiple_binary_search_tree_allocator::MultipleBinarySearchTreeAllocator;

use crate::memory_address::MemoryAddress;
use crate::memory_sources::memory_source::MemorySource;
use std::alloc::AllocErr;
use std::fmt::Debug;
use std::num::NonZeroUsize;

/// An allocator designed for contexts with different lifetimes.
///
/// This allocator NEVER grows or shrinks its memory region.
///
/// This allocator is not thread-safe.
#[derive(Debug)]
pub enum ContextAllocator<MS: MemorySource> {
    /// Use this variant for contexts with short-lived lifetimes.
    ///
    /// Very fast allocation and almost costless deallocation, at the expense of the strong likelihood of running out of memory.
    ///
    /// Reallocation is very expensive when growing unless reallocating the most recently made allocation.
    ShortLived(BumpAllocator<MS>),

    /// Use this variant for contexts with slightly longer than short-lived lifetimes.
    ///
    /// Slower allocation and deallocation but reallocation is less expensive than for `ShortLived`.
    MediumLived(BitSetAllocator<MS>),

    /// Use this variant for contexts with long-lived lifetimes.
    LongLived(MultipleBinarySearchTreeAllocator<MS>),
}

impl<MS: MemorySource> Allocator for ContextAllocator<MS> {
    #[inline(always)]
    fn allocate(
        &self,
        non_zero_size: NonZeroUsize,
        non_zero_power_of_two_alignment: NonZeroUsize,
    ) -> Result<MemoryAddress, AllocErr> {
        use self::ContextAllocator::*;

        match *self {
            ShortLived(ref allocator) => {
                allocator.allocate(non_zero_size, non_zero_power_of_two_alignment)
            }

            MediumLived(ref allocator) => {
                allocator.allocate(non_zero_size, non_zero_power_of_two_alignment)
            }

            LongLived(ref allocator) => {
                allocator.allocate(non_zero_size, non_zero_power_of_two_alignment)
            }
        }
    }

    #[inline(always)]
    fn deallocate(
        &self,
        non_zero_size: NonZeroUsize,
        non_zero_power_of_two_alignment: NonZeroUsize,
        current_memory: MemoryAddress,
    ) {
        use self::ContextAllocator::*;

        match *self {
            ShortLived(ref allocator) => allocator.deallocate(
                non_zero_size,
                non_zero_power_of_two_alignment,
                current_memory,
            ),

            MediumLived(ref allocator) => allocator.deallocate(
                non_zero_size,
                non_zero_power_of_two_alignment,
                current_memory,
            ),

            LongLived(ref allocator) => allocator.deallocate(
                non_zero_size,
                non_zero_power_of_two_alignment,
                current_memory,
            ),
        }
    }

    #[inline(always)]
    fn growing_reallocate(
        &self,
        non_zero_new_size: NonZeroUsize,
        non_zero_power_of_two_alignment: NonZeroUsize,
        non_zero_current_size: NonZeroUsize,
        current_memory: MemoryAddress,
    ) -> Result<MemoryAddress, AllocErr> {
        use self::ContextAllocator::*;

        match *self {
            ShortLived(ref allocator) => allocator.growing_reallocate(
                non_zero_new_size,
                non_zero_power_of_two_alignment,
                non_zero_current_size,
                current_memory,
            ),

            MediumLived(ref allocator) => allocator.growing_reallocate(
                non_zero_new_size,
                non_zero_power_of_two_alignment,
                non_zero_current_size,
                current_memory,
            ),

            LongLived(ref allocator) => allocator.growing_reallocate(
                non_zero_new_size,
                non_zero_power_of_two_alignment,
                non_zero_current_size,
                current_memory,
            ),
        }
    }

    #[inline(always)]
    fn shrinking_reallocate(
        &self,
        non_zero_new_size: NonZeroUsize,
        non_zero_power_of_two_alignment: NonZeroUsize,
        non_zero_current_size: NonZeroUsize,
        current_memory: MemoryAddress,
    ) -> Result<MemoryAddress, AllocErr> {
        use self::ContextAllocator::*;

        match *self {
            ShortLived(ref allocator) => allocator.shrinking_reallocate(
                non_zero_new_size,
                non_zero_power_of_two_alignment,
                non_zero_current_size,
                current_memory,
            ),

            MediumLived(ref allocator) => allocator.shrinking_reallocate(
                non_zero_new_size,
                non_zero_power_of_two_alignment,
                non_zero_current_size,
                current_memory,
            ),

            LongLived(ref allocator) => allocator.shrinking_reallocate(
                non_zero_new_size,
                non_zero_power_of_two_alignment,
                non_zero_current_size,
                current_memory,
            ),
        }
    }
}

impl<MS: MemorySource> LocalAllocator for ContextAllocator<MS> {
    #[inline(always)]
    fn memory_range(&self) -> MemoryRange {
        use self::ContextAllocator::*;

        match *self {
            ShortLived(ref allocator) => allocator.memory_range(),

            MediumLived(ref allocator) => allocator.memory_range(),

            LongLived(ref allocator) => allocator.memory_range(),
        }
    }
}