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
use core::mem;

use crate::{is_aligned, HeapStats, PageAllocatorProvider};

use super::align_up;

const HEAP_MAGIC: u32 = 0xF0B0CAFE;

#[repr(C, align(16))]
struct AllocatedHeapBlockInfo {
    magic: u32,
    size: usize,
    pre_padding: usize,
}

const KERNEL_HEAP_BLOCK_INFO_SIZE: usize = mem::size_of::<AllocatedHeapBlockInfo>();

#[derive(Debug)]
struct HeapFreeBlock {
    prev: *mut HeapFreeBlock,
    next: *mut HeapFreeBlock,
    // including this header
    size: usize,
}

pub struct HeapAllocator<const PAGE_SIZE: usize, T: PageAllocatorProvider<PAGE_SIZE>> {
    heap_start: usize,
    total_heap_size: usize,
    free_list_addr: *mut HeapFreeBlock,
    free_size: usize,
    used_size: usize,
    page_allocator: T,
}

unsafe impl<const PAGE_SIZE: usize, T: PageAllocatorProvider<PAGE_SIZE>> Send
    for HeapAllocator<PAGE_SIZE, T>
{
}

impl<const PAGE_SIZE: usize, T> HeapAllocator<PAGE_SIZE, T>
where
    T: PageAllocatorProvider<PAGE_SIZE>,
{
    fn is_free_blocks_in_cycle(&self) -> bool {
        // use floyd algorithm to detect if we are in cycle
        let mut slow = self.free_list_addr;
        let mut fast = self.free_list_addr;

        // advance fast first
        if fast.is_null() {
            return false;
        } else {
            fast = unsafe { (*fast).next };
        }

        while fast != slow {
            if fast.is_null() {
                return false;
            } else {
                fast = unsafe { (*fast).next };
            }
            if fast.is_null() {
                return false;
            } else {
                fast = unsafe { (*fast).next };
            }

            if slow.is_null() {
                return false;
            } else {
                slow = unsafe { (*slow).next };
            }
        }

        true
    }

    fn check_free_blocks(&self) -> bool {
        let mut forward_count = 0;
        let mut last: *mut HeapFreeBlock = core::ptr::null_mut();
        for block in self.iter_free_blocks() {
            forward_count += 1;
            last = block as _;
        }

        let mut backward_count = 0;
        if !last.is_null() {
            // go back to the first block
            while !last.is_null() {
                backward_count += 1;
                last = unsafe { (*last).prev };
            }
        }

        forward_count != backward_count
    }

    fn check_issues(&self) -> bool {
        self.is_free_blocks_in_cycle() || self.check_free_blocks()
    }

    fn get_free_block(&mut self, size: usize) -> *mut HeapFreeBlock {
        if self.total_heap_size == 0 {
            let size = align_up(size, PAGE_SIZE);
            self.allocate_more_pages(size / PAGE_SIZE);
            // call recursively
            return self.get_free_block(size);
        }
        // find best block
        let mut best_block: *mut HeapFreeBlock = core::ptr::null_mut();
        for block in self.iter_free_blocks() {
            if block.size >= size
                && (best_block.is_null() || block.size < unsafe { (*best_block).size })
            {
                best_block = block as _;
            }
        }

        if best_block.is_null() {
            // no block found, allocate more pages
            let size = align_up(size, PAGE_SIZE);
            self.allocate_more_pages(size / PAGE_SIZE);
            // call recursively
            return self.get_free_block(size);
        }

        best_block
    }

    fn iter_free_blocks(&self) -> impl Iterator<Item = &mut HeapFreeBlock> {
        let mut current_block = self.free_list_addr;
        core::iter::from_fn(move || {
            if current_block.is_null() {
                None
            } else {
                let block = current_block;
                current_block = unsafe { (*current_block).next };
                Some(unsafe { &mut *block })
            }
        })
    }

    /// Allocates more pages and add them to the free list
    fn allocate_more_pages(&mut self, pages: usize) {
        assert!(pages > 0);

        let new_heap_start = if self.total_heap_size == 0 {
            // first allocation
            self.heap_start = self.page_allocator.allocate_pages(pages).unwrap() as usize;
            self.heap_start
        } else {
            // allocate more pages
            self.page_allocator.allocate_pages(pages).unwrap() as usize
        };

        self.total_heap_size += pages * PAGE_SIZE;

        // add to the free list (fast path)
        if self.free_list_addr.is_null() {
            // no free list for now, add this as the very first free entry
            let free_block = new_heap_start as *mut HeapFreeBlock;

            unsafe {
                (*free_block).prev = core::ptr::null_mut();
                (*free_block).next = core::ptr::null_mut();
                (*free_block).size = pages * PAGE_SIZE;
            }

            self.free_list_addr = free_block;
        } else {
            unsafe {
                self.free_block(new_heap_start as _, pages * PAGE_SIZE);
            }
        }
        self.free_size += pages * PAGE_SIZE;
    }

    unsafe fn free_block(&mut self, freeing_block: usize, size: usize) {
        assert!(freeing_block <= self.heap_start + self.total_heap_size);
        assert!(freeing_block + size <= self.heap_start + self.total_heap_size);

        let freeing_block = freeing_block as *mut HeapFreeBlock;
        let freeing_block_start = freeing_block as usize;
        let freeing_block_end = freeing_block_start + size;

        // find blocks that are either before or after this block
        let mut prev_block: *mut HeapFreeBlock = core::ptr::null_mut();
        let mut next_block: *mut HeapFreeBlock = core::ptr::null_mut();
        let mut closest_prev_block: *mut HeapFreeBlock = core::ptr::null_mut();
        for block in self.iter_free_blocks() {
            let block_addr = block as *mut _ as usize;
            let block_end = block_addr + block.size;

            if block_addr == freeing_block_start {
                // our block should not be in the free list
                panic!("double free");
            }

            // assert that we are not in the middle of a block
            assert!(
                (freeing_block_end <= block_addr) || (freeing_block_start >= block_end),
                "Free block at {:x}..{:x} is in the middle of another block at {:x}..{:x}",
                freeing_block_start,
                freeing_block_end,
                block_addr,
                block_end
            );

            if block_end == freeing_block_start {
                // this block is before the freeing block
                prev_block = block as _;
            } else if freeing_block_end == block_addr {
                // this block is after the freeing block
                next_block = block as _;
            }

            if block_addr < freeing_block_start {
                // this block is before the freeing block
                if closest_prev_block.is_null() || block_addr > (closest_prev_block as usize) {
                    closest_prev_block = block as _;
                }
            }
        }

        if !prev_block.is_null() && !next_block.is_null() {
            let new_block = prev_block;
            // both are not null, so we are in the middle
            // merge the blocks
            (*new_block).size += size + (*next_block).size;

            // update the previous block to point to this new subblock instead
            if !(*next_block).next.is_null() {
                (*(*next_block).next).prev = new_block;
            }

            if !(*next_block).prev.is_null() {
                (*(*next_block).prev).next = new_block;
            } else {
                // this is the first block
                self.free_list_addr = new_block;
            }

            (*new_block).next = (*next_block).next;
        } else if !prev_block.is_null() {
            // no blocks after this
            // merge the blocks easily, we only need to change the size
            (*prev_block).size += size;
        } else if !next_block.is_null() {
            let new_block = freeing_block;

            // replace next with a new size
            (*new_block).size = (*next_block).size + size;
            (*new_block).prev = (*next_block).prev;
            (*new_block).next = (*next_block).next;

            // update references
            // update the next block to point to this new subblock instead
            if !(*next_block).next.is_null() {
                (*(*next_block).next).prev = new_block;
            }
            // update the previous block to point to this new subblock instead
            if !(*next_block).prev.is_null() {
                (*(*next_block).prev).next = new_block;
            } else {
                // this is the first block
                self.free_list_addr = new_block;
            }
        } else {
            // no blocks around this
            // add this to the free list in the correct order
            if closest_prev_block.is_null() {
                // this is the first block
                (*freeing_block).prev = core::ptr::null_mut();
                (*freeing_block).next = self.free_list_addr;
                (*freeing_block).size = size;

                // update the next block to point to this new subblock instead
                if !(*freeing_block).next.is_null() {
                    (*(*freeing_block).next).prev = freeing_block;
                }

                self.free_list_addr = freeing_block;
            } else {
                // put this after the closest previous block
                let closest_next_block = (*closest_prev_block).next;
                (*freeing_block).prev = closest_prev_block;
                (*freeing_block).next = closest_next_block;
                (*freeing_block).size = size;

                (*closest_prev_block).next = freeing_block;
                if !closest_next_block.is_null() {
                    (*closest_next_block).prev = freeing_block;
                }
            }
        }
    }
}

// public interface
impl<const PAGE_SIZE: usize, T> HeapAllocator<PAGE_SIZE, T>
where
    T: PageAllocatorProvider<PAGE_SIZE>,
{
    pub fn new(page_allocator: T) -> Self {
        Self {
            heap_start: 0,
            free_list_addr: core::ptr::null_mut(),
            total_heap_size: 0,
            free_size: 0,
            used_size: 0,
            page_allocator,
        }
    }

    pub fn stats(&self) -> HeapStats {
        HeapStats {
            allocated: self.used_size,
            free_size: self.free_size,
            heap_size: self.total_heap_size,
        }
    }

    pub fn debug_free_blocks(&self) -> impl Iterator<Item = (usize, usize)> + '_ {
        self.iter_free_blocks()
            .map(|block| (block as *mut _ as usize, block.size))
    }

    /// # Safety
    /// Check [`core::alloc::GlobalAlloc::alloc`] for more info
    pub unsafe fn alloc(&mut self, layout: core::alloc::Layout) -> *mut u8 {
        // info header
        let block_info_layout = core::alloc::Layout::new::<AllocatedHeapBlockInfo>();

        let (whole_layout, whole_block_offset) = block_info_layout
            .extend(layout.align_to(16).unwrap())
            .unwrap();
        // at least align to 16 bytes
        let size_to_allocate = whole_layout.pad_to_align().size();

        let free_block = self.get_free_block(size_to_allocate);

        if free_block.is_null() {
            return core::ptr::null_mut();
        }

        let free_block_size = (*free_block).size;
        let free_block_end = free_block as usize + size_to_allocate;
        let new_free_block = free_block_end as *mut HeapFreeBlock;

        // we have to make sure that the block after us has enough space to write the metadat
        // and we won't corrupt the block that comes after (if there is anys)
        let whole_size = size_to_allocate + mem::size_of::<HeapFreeBlock>();

        // store the actual size of the block
        // if we needed to extend (since the next free block is to small)
        // this will include the whole size and not just the size that
        // we were asked to allocate
        let mut this_allocation_size = size_to_allocate;

        // do we have empty space left?
        if free_block_size > whole_size {
            // update the previous block to point to this new subblock instead
            (*new_free_block).prev = (*free_block).prev;
            (*new_free_block).next = (*free_block).next;
            (*new_free_block).size = free_block_size - size_to_allocate;

            // update the next block to point to this new subblock instead
            if !(*new_free_block).next.is_null() {
                (*(*new_free_block).next).prev = new_free_block;
            }

            // update the previous block to point to this new subblock instead
            if !(*new_free_block).prev.is_null() {
                (*(*new_free_block).prev).next = new_free_block;
            } else {
                // this is the first block
                self.free_list_addr = new_free_block;
            }
        } else {
            // exact size
            this_allocation_size = free_block_size;

            // update the previous block to point to the next block instead
            if !(*free_block).prev.is_null() {
                (*(*free_block).prev).next = (*free_block).next;
            } else {
                // this is the first block
                self.free_list_addr = (*free_block).next;
            }
            if !(*free_block).next.is_null() {
                (*(*free_block).next).prev = (*free_block).prev;
            }
        }
        self.free_size -= this_allocation_size;
        self.used_size += this_allocation_size;

        // TODO: add flag to control when to enable this runtime checking
        if self.check_issues() {
            panic!("Found issues in `alloc`");
        }

        // work on the pointer and add the info of the block before it
        // so we can use it to deallocate later
        let base = free_block as usize;
        let possible_next_offset = align_up(base, layout.align()) - base;
        let allocated_block_offset = if possible_next_offset < KERNEL_HEAP_BLOCK_INFO_SIZE {
            possible_next_offset + KERNEL_HEAP_BLOCK_INFO_SIZE.max(layout.align())
        } else {
            possible_next_offset
        };
        assert!(allocated_block_offset >= KERNEL_HEAP_BLOCK_INFO_SIZE);
        assert!(allocated_block_offset <= whole_block_offset);
        let allocated_ptr = (free_block as *mut u8).add(allocated_block_offset);
        let allocated_block_info =
            allocated_ptr.sub(KERNEL_HEAP_BLOCK_INFO_SIZE) as *mut AllocatedHeapBlockInfo;
        // make sure we are aligned
        assert!(is_aligned(allocated_ptr as _, layout.align()),
            "base_block={allocated_block_info:p}, offset={allocated_block_offset}, ptr={allocated_ptr:?}, layout={layout:?}, should_be_addr={:x}",
            align_up(allocated_block_info as usize, layout.align()));

        // write the info header
        (*allocated_block_info).magic = HEAP_MAGIC;
        (*allocated_block_info).size = this_allocation_size;
        (*allocated_block_info).pre_padding = allocated_block_offset;

        allocated_ptr
    }

    /// # Safety
    /// Check [`core::alloc::GlobalAlloc::dealloc`] for more info
    pub unsafe fn dealloc(&mut self, ptr: *mut u8, layout: core::alloc::Layout) {
        assert!(!ptr.is_null());

        // info header
        let base_layout = core::alloc::Layout::new::<AllocatedHeapBlockInfo>();

        let (whole_layout, _) = base_layout.extend(layout.align_to(16).unwrap()).unwrap();
        let size_to_free_from_layout = whole_layout.pad_to_align().size();

        let allocated_block_info =
            ptr.sub(KERNEL_HEAP_BLOCK_INFO_SIZE) as *mut AllocatedHeapBlockInfo;

        assert_eq!((*allocated_block_info).magic, HEAP_MAGIC);
        // This could be more than the layout size, because
        // we might increase the size of the block a bit to not leave
        // free blocks that are too small (see `alloc``)
        assert!((*allocated_block_info).size >= size_to_free_from_layout);
        assert!((*allocated_block_info).pre_padding >= KERNEL_HEAP_BLOCK_INFO_SIZE);
        let this_allocation_size = (*allocated_block_info).size;

        let freeing_block = ptr.sub((*allocated_block_info).pre_padding) as usize;

        self.free_block(freeing_block, this_allocation_size);
        self.used_size -= this_allocation_size;
        self.free_size += this_allocation_size;

        // TODO: add flag to control when to enable this runtime checking
        if self.check_issues() {
            panic!("Found issues in `dealloc`");
        }
    }
}