aboutsummaryrefslogtreecommitdiff
path: root/src/mem/alloc.rs
blob: 514f34fac3128550b0b0c106343d372c630039f1 (plain)
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
//! # Allocate crate
//!
//! Provides the Global allocator and methods
//! to create special purpose allocators.
use super::types::*;
use crate::sync::interface::Mutex;
use crate::sync::NullLock;
use crate::serial_vprintln;
use alloc::alloc::{GlobalAlloc, Layout};
use os_pic::init_lifo_queue;
use os_pic::util::lifo_queue::LifoQueue;
use os_pic::util::node::Node;
use os_pic::util::queue::Queue;

/// # Grand Allocator
///
/// The structure that uses different sized pools and allocates memory chunks
pub struct GrandAllocator {}

/// # The number of elements of each size
const GRAND_ALLOC_SIZE: usize = 64;

init_lifo_queue!(U8_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u8);
init_lifo_queue!(U16_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u16);
init_lifo_queue!(U32_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u32);
init_lifo_queue!(U64_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u64);
init_lifo_queue!(U128_GRAND_ALLOC, GRAND_ALLOC_SIZE, 0, u128);
init_lifo_queue!(U256_GRAND_ALLOC, GRAND_ALLOC_SIZE, { U256::new() }, U256);
init_lifo_queue!(U512_GRAND_ALLOC, GRAND_ALLOC_SIZE, { U512::new() }, U512);
init_lifo_queue!(U1024_GRAND_ALLOC, GRAND_ALLOC_SIZE, { U1024::new() }, U1024);
init_lifo_queue!(U2048_GRAND_ALLOC, GRAND_ALLOC_SIZE, { U2048::new() }, U2048);
init_lifo_queue!(U4096_GRAND_ALLOC, GRAND_ALLOC_SIZE, { U4096::new() }, U4096);

impl GrandAllocator {
    pub fn init(&self) -> Result<(), &'static str> {
        serial_vprintln!("GA: \x1b[93mInit U8 Pool\x1b[0m");
        U8_GRAND_ALLOC.init();
        serial_vprintln!("GA: \x1b[93mInit U16 Pool\x1b[0m");
        U16_GRAND_ALLOC.init();
        serial_vprintln!("GA: \x1b[93mInit U32 Pool\x1b[0m");
        U32_GRAND_ALLOC.init();
        serial_vprintln!("GA: \x1b[93mInit U64 Pool\x1b[0m");
        U64_GRAND_ALLOC.init();
        serial_vprintln!("GA: \x1b[93mInit U128 Pool\x1b[0m");
        U128_GRAND_ALLOC.init();
        serial_vprintln!("GA: \x1b[93mInit U256 Pool\x1b[0m");
        U256_GRAND_ALLOC.init();
        serial_vprintln!("GA: \x1b[93mInit U512 Pool\x1b[0m");
        U512_GRAND_ALLOC.init();
        serial_vprintln!("GA: \x1b[93mInit U1024 Pool\x1b[0m");
        U1024_GRAND_ALLOC.init();
        serial_vprintln!("GA: \x1b[93mInit U2048 Pool\x1b[0m");
        U2048_GRAND_ALLOC.init();
        serial_vprintln!("GA: \x1b[93mInit U4096 Pool\x1b[0m");
        U4096_GRAND_ALLOC.init();
        serial_vprintln!("GA: \x1b[94mPools Initialized!\x1b[0m");
        Ok(())
    }
}

unsafe impl GlobalAlloc for GrandAllocator {
    /// # Allocator
    ///
    /// Allocate the fixed size chunks
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        serial_vprintln!("GA: Allocating chunk of size {}!", layout.size());
        match layout.size() {
            1 => match U8_GRAND_ALLOC.pop() {
                None => {
                    panic!("No cells to allocate!");
                }
                Some(elem) => {
                    return (*elem).ptr();
                }
            },
            2 => match U16_GRAND_ALLOC.pop() {
                None => {
                    panic!("No cells to allocate!");
                }
                Some(elem) => {
                    return (*elem).ptr();
                }
            },
            3..=4 => match U32_GRAND_ALLOC.pop() {
                None => {
                    panic!("No cells to allocate!");
                }
                Some(elem) => {
                    return (*elem).ptr();
                }
            },
            5..=8 => match U64_GRAND_ALLOC.pop() {
                None => {
                    panic!("No cells to allocate!");
                }
                Some(elem) => {
                    return (*elem).ptr();
                }
            },
            9..=16 => match U128_GRAND_ALLOC.pop() {
                None => {
                    panic!("No cells to allocate!");
                }
                Some(elem) => {
                    return (*elem).ptr();
                }
            },
            17..=32 => match U256_GRAND_ALLOC.pop() {
                None => {
                    panic!("No cells to allocate!");
                }
                Some(elem) => {
                    return (*elem).ptr();
                }
            },
            33..=64 => match U512_GRAND_ALLOC.pop() {
                None => {
                    panic!("No cells to allocate!");
                }
                Some(elem) => {
                    return (*elem).ptr();
                }
            },
            65..=128 => match U1024_GRAND_ALLOC.pop() {
                None => {
                    panic!("No cells to allocate!");
                }
                Some(elem) => {
                    return (*elem).ptr();
                }
            },
            129..=256 => match U2048_GRAND_ALLOC.pop() {
                None => {
                    panic!("No cells to allocate!");
                }
                Some(elem) => {
                    return (*elem).ptr();
                }
            },
            257..=512 => match U4096_GRAND_ALLOC.pop() {
                None => {
                    panic!("No cells to allocate!");
                }
                Some(elem) => {
                    return (*elem).ptr();
                }
            },
            _ => {
                panic!("No allocators for size {}!", layout.size());
            }
        }
    }

    /// # Deallocate
    ///
    /// Deallocate the fixed size chunks by searching for them
    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        serial_vprintln!("GA: Deallocating chunk of size {}!", layout.size());
        match layout.size() {
            1 => {
                U8_GRAND_ALLOC.inner.lock(|pool| {
					let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize);
					let diff: usize = (ptr as usize) - (pool[1].ptr() as usize);
					let index: usize = diff/spacing;
					assert!(index < GRAND_ALLOC_SIZE, "{} is out of the allocation bounds ({})", index, GRAND_ALLOC_SIZE);
					assert_eq!(diff % spacing, 0, "{} is not aligned with the spacings and so it must not have been allocated by the Grand Allocator", diff % spacing);
					U8_GRAND_ALLOC.push(&mut pool[index+1]);
					serial_vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing);
				});
            }
            2 => {
                U16_GRAND_ALLOC.inner.lock(|pool| {
					let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize);
					let diff: usize = (ptr as usize) - (pool[1].ptr() as usize);
					let index: usize = diff/spacing;
					assert!(index < GRAND_ALLOC_SIZE, "{} is out of the allocation bounds ({})", index, GRAND_ALLOC_SIZE);
					assert_eq!(diff % spacing, 0, "{} is not aligned with the spacings and so it must not have been allocated by the Grand Allocator", diff % spacing);
					U16_GRAND_ALLOC.push(&mut pool[index+1]);
					serial_vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing);
				});
            }
            3..=4 => {
                U32_GRAND_ALLOC.inner.lock(|pool| {
					let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize);
					let diff: usize = (ptr as usize) - (pool[1].ptr() as usize);
					let index: usize = diff/spacing;
					assert!(index < GRAND_ALLOC_SIZE, "{} is out of the allocation bounds ({})", index, GRAND_ALLOC_SIZE);
					assert_eq!(diff % spacing, 0, "{} is not aligned with the spacings and so it must not have been allocated by the Grand Allocator", diff % spacing);
					U32_GRAND_ALLOC.push(&mut pool[index+1]);
					serial_vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing);
				});
            }
            5..=8 => {
                U64_GRAND_ALLOC.inner.lock(|pool| {
					let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize);
					let diff: usize = (ptr as usize) - (pool[1].ptr() as usize);
					let index: usize = diff/spacing;
					assert!(index < GRAND_ALLOC_SIZE, "{} is out of the allocation bounds ({})", index, GRAND_ALLOC_SIZE);
					assert_eq!(diff % spacing, 0, "{} is not aligned with the spacings and so it must not have been allocated by the Grand Allocator", diff % spacing);
					U64_GRAND_ALLOC.push(&mut pool[index+1]);
					serial_vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing);
				});
            }
            9..=16 => {
                U128_GRAND_ALLOC.inner.lock(|pool| {
					let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize);
					let diff: usize = (ptr as usize) - (pool[1].ptr() as usize);
					let index: usize = diff/spacing;
					assert!(index < GRAND_ALLOC_SIZE, "{} is out of the allocation bounds ({})", index, GRAND_ALLOC_SIZE);
					assert_eq!(diff % spacing, 0, "{} is not aligned with the spacings and so it must not have been allocated by the Grand Allocator", diff % spacing);
					U128_GRAND_ALLOC.push(&mut pool[index+1]);
					serial_vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing);
				});
            }
            17..=32 => {
                U256_GRAND_ALLOC.inner.lock(|pool| {
					let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize);
					let diff: usize = (ptr as usize) - (pool[1].ptr() as usize);
					let index: usize = diff/spacing;
					assert!(index < GRAND_ALLOC_SIZE, "{} is out of the allocation bounds ({})", index, GRAND_ALLOC_SIZE);
					assert_eq!(diff % spacing, 0, "{} is not aligned with the spacings and so it must not have been allocated by the Grand Allocator", diff % spacing);
					U256_GRAND_ALLOC.push(&mut pool[index+1]);
					serial_vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing);
				});
            }
            33..=64 => {
                U512_GRAND_ALLOC.inner.lock(|pool| {
					let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize);
					let diff: usize = (ptr as usize) - (pool[1].ptr() as usize);
					let index: usize = diff/spacing;
					assert!(index < GRAND_ALLOC_SIZE, "{} is out of the allocation bounds ({})", index, GRAND_ALLOC_SIZE);
					assert_eq!(diff % spacing, 0, "{} is not aligned with the spacings and so it must not have been allocated by the Grand Allocator", diff % spacing);
					U512_GRAND_ALLOC.push(&mut pool[index+1]);
					serial_vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing);
				});
            }
            65..=128 => {
                U1024_GRAND_ALLOC.inner.lock(|pool| {
					let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize);
					let diff: usize = (ptr as usize) - (pool[1].ptr() as usize);
					let index: usize = diff/spacing;
					assert!(index < GRAND_ALLOC_SIZE, "{} is out of the allocation bounds ({})", index, GRAND_ALLOC_SIZE);
					assert_eq!(diff % spacing, 0, "{} is not aligned with the spacings and so it must not have been allocated by the Grand Allocator", diff % spacing);
					U1024_GRAND_ALLOC.push(&mut pool[index+1]);
					serial_vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing);
				});
            }
            129..=256 => {
                U2048_GRAND_ALLOC.inner.lock(|pool| {
					let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize);
					let diff: usize = (ptr as usize) - (pool[1].ptr() as usize);
					let index: usize = diff/spacing;
					assert!(index < GRAND_ALLOC_SIZE, "{} is out of the allocation bounds ({})", index, GRAND_ALLOC_SIZE);
					assert_eq!(diff % spacing, 0, "{} is not aligned with the spacings and so it must not have been allocated by the Grand Allocator", diff % spacing);
					U2048_GRAND_ALLOC.push(&mut pool[index+1]);
					serial_vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing);
				});
            }
            257..=512 => {
                U4096_GRAND_ALLOC.inner.lock(|pool| {
					let spacing: usize = (pool[2].ptr() as usize) - (pool[1].ptr() as usize);
					let diff: usize = (ptr as usize) - (pool[1].ptr() as usize);
					let index: usize = diff/spacing;
					assert!(index < GRAND_ALLOC_SIZE, "{} is out of the allocation bounds ({})", index, GRAND_ALLOC_SIZE);
					assert_eq!(diff % spacing, 0, "{} is not aligned with the spacings and so it must not have been allocated by the Grand Allocator", diff % spacing);
					U4096_GRAND_ALLOC.push(&mut pool[index+1]);
					serial_vprintln!("GA: Freeing ({}, {}, {})", index, diff, spacing);
				});
            }
            _ => {
                panic!("No deallocators for size {}!", layout.size());
            }
        }
    }
}

/// # Grand Allocator
///
/// The allocator of allocators. It hands out fixed sized memory chunks.
#[global_allocator]
pub static ALLOCATOR: GrandAllocator = GrandAllocator {};

/// # Global Allocator
///
/// Returns a borrow for the Global Allocator
pub fn allocator() -> &'static crate::mem::alloc::GrandAllocator {
    serial_vprintln!("AL: Getting global allocator!");
    &crate::mem::alloc::ALLOCATOR
}