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
//! # Buddy system allocator
//!
//! Buddy system is a memory allocation algorithm, designed to reduce
//! external fragmentation but can still achieve high performance.
//! It has been widely used in modern operating systems such as Linux for
//! dynamic allocation and deallocation of memory chunks.
//!
//! ## Complexity
//!
//! Allocation and deallocation are both guaranteed to finish within O(log n),
//! where n is the size of memory handled by this buddy system.
//!

use crate::{rawlist::Rawlist, to_order};
use core::{
    alloc::{GlobalAlloc, Layout},
    cmp::min,
    mem::{self, size_of},
    ptr,
};
use spin::Mutex;
use typenum::{marker_traits::PowerOfTwo, Unsigned};

/// Round down to the nearest multiple of n.
#[inline]
fn round_down(x: usize, n: usize) -> usize {
    x - (x % n)
}
/// Round up to the nearest multiple of n.
#[inline]
fn round_up(x: usize, n: usize) -> usize {
    round_down(x + n - 1, n)
}
#[inline]
fn left(i: usize) -> usize {
    i * 2
}
#[inline]
fn right(i: usize) -> usize {
    left(i) + 1
}
#[inline]
fn father(i: usize) -> usize {
    i / 2
}
#[inline]
fn buddy_idx(i: usize) -> usize {
    i ^ 1
}

/// Buddy System Allocator Structure.
///
/// # Memory Layout
///
/// ```text
///          bitmap_begin          page_begin          page_end
///               |                    |                   |
///               v                    v                   v
/// +-------------+--------+-----------+-------------------+
/// | this struct | bitmap | (padding) | 2^max_order pages |
/// +-------------+--------+-----------+-------------------+
///                                    ^
///                                    |      
///                    page_begin is a multiple of P
/// ```
#[repr(C)]
pub struct BuddySystem<P> {
    /// Should be less than 32.
    max_order: usize,

    /// Should be equal to 2^max_order.
    npages: usize,

    bitmap_begin: usize,

    /// Address of the first page. Should be a multiple of page_size.
    page_begin: usize,

    page_end: usize,

    /// freelist[i] is a list of free blocks of size 2^i.
    /// Maximum order is 31, only support area of 2^31 pages.
    freelist: [Rawlist; 32],

    /// Pointer to next buddy system, used by MultiBuddySystem.
    next: *mut BuddySystem<P>,
}

impl<P: Unsigned + PowerOfTwo> BuddySystem<P> {
    #[inline]
    unsafe fn set_bit(&mut self, i: usize) {
        let p = self.bitmap_begin + i / 8;
        debug_assert!(self.bitmap_begin <= p && p < self.page_begin);
        *(p as *mut u8) |= 1 << (i % 8);
    }

    #[inline]
    unsafe fn unset_bit(&mut self, i: usize) {
        let p = self.bitmap_begin + i / 8;
        debug_assert!(self.bitmap_begin <= p && p < self.page_begin);
        *(p as *mut u8) &= !(1 << (i % 8));
    }

    #[inline]
    unsafe fn get_bit(&self, i: usize) -> bool {
        let p = self.bitmap_begin + i / 8;
        debug_assert!(self.bitmap_begin <= p && p < self.page_begin);
        let b = *(p as *mut u8);
        if ((b >> (i % 8)) & 1) == 0 {
            false
        } else {
            true
        }
    }

    #[inline]
    fn bitmap_idx(&self, p: usize, order: usize) -> usize {
        (1 << (self.max_order - order)) + (((p - self.page_begin) / P::to_usize()) >> order)
    }

    /// Construct a buddy system allocator at memory [begin, end) with specific page size.
    ///
    /// Notice that it guarantees safety only if the access to [begin, end) is safe
    /// and `self` is a static variable.
    pub unsafe fn build(mut begin: usize, end: usize) -> Result<&'static mut Self, ()> {
        assert!(P::to_usize() >= mem::size_of::<Rawlist>());
        begin = round_up(begin, mem::align_of::<Self>());

        let b = &mut (*(begin as *mut Self));
        begin += size_of::<Self>();
        if begin >= end {
            return Err(());
        }
        b.max_order = b.freelist.len();
        b.bitmap_begin = begin;
        b.next = ptr::null_mut();

        for i in 0..b.freelist.len() {
            let n: usize = 1 << i;
            let bitmap_end = begin + round_up(2 * n, 8) / 8;
            let page_begin = round_up(bitmap_end, P::to_usize());
            let page_end = page_begin + n * P::to_usize();
            if page_end <= end {
                b.max_order = i;
                b.npages = n;
                b.page_begin = page_begin;
                b.page_end = page_end;
            } else {
                break;
            }
        }
        if b.max_order >= b.freelist.len() {
            return Err(());
        }
        for head in b.freelist.iter_mut() {
            Rawlist::init(head);
        }
        Rawlist::push_front(&mut b.freelist[b.max_order], b.page_begin as *mut Rawlist);
        ptr::write_bytes(begin as *mut u8, 0, round_up(2 * b.npages, 8) / 8);
        Ok(b)
    }

    /// Alloccate memory by order.
    pub unsafe fn alloc1(&mut self, order: usize) -> *mut u8 {
        let mut d = order;
        let mut p = ptr::null_mut();
        while d <= self.max_order {
            let head = &mut self.freelist[d] as *mut Rawlist;
            if !Rawlist::is_empty(head) {
                p = Rawlist::pop_front(head) as *mut u8;
                let mut i = self.bitmap_idx(p as usize, d);
                self.set_bit(i);
                while d > order {
                    d -= 1;
                    Rawlist::push_front(
                        &mut self.freelist[d],
                        (p as usize + (1 << d) * P::to_usize()) as *mut Rawlist,
                    );
                    i = left(i);
                    self.set_bit(i);
                }
                break;
            }
            d += 1;
        }

        #[cfg(any(test, debug_assertions))]
        self.check();

        p
    }

    /// Free the block of memory starting from `ptr` with specific order.
    /// Return the order of the freed page after merging possible buddy pages.
    pub unsafe fn free(&mut self, ptr: *mut u8, mut order: usize) -> usize {
        let mut i = self.bitmap_idx(ptr as usize, order);
        let mut p = ptr as usize;
        while i != 1 && self.get_bit(buddy_idx(i)) == false {
            let bp = if i < buddy_idx(i) {
                p + (1 << order) * P::to_usize()
            } else {
                p - (1 << order) * P::to_usize()
            };
            Rawlist::drop(bp as *mut Rawlist);
            self.unset_bit(i);

            order += 1;
            i = father(i);
            p = min(p, bp);
        }
        self.unset_bit(i);
        Rawlist::push_front(&mut self.freelist[order], p as *mut Rawlist);

        #[cfg(any(test, debug_assertions))]
        self.check();

        order
    }

    /// Allocate memory specified by `layout`.
    pub unsafe fn alloc(&mut self, layout: Layout) -> *mut u8 {
        self.alloc1(to_order(P::to_usize(), &layout))
    }

    /// Free the block of memory starting from `ptr` with specific `layout`.
    pub unsafe fn dealloc(&mut self, ptr: *mut u8, layout: Layout) {
        debug_assert_eq!(ptr.is_null(), false);
        debug_assert!(self.page_begin <= (ptr as usize) && (ptr as usize) < self.page_end);
        self.free(ptr, to_order(P::to_usize(), &layout));
    }

    /// Check the properties maintained by buddy system.
    ///
    /// - 1-nodes that don't have any 1-node child are allocated chunks.
    /// - Root 0-nodes and 0-nodes whose father is 1-node and buddy is 1-node are free chunks.
    /// - Children of any 0-node must be also 0-nodes.
    #[cfg(any(test, debug_assertions))]
    pub unsafe fn check(&self) {
        let mut nalloc = 0;
        let mut nfree = 0;

        // BFS starting from root node 1.
        for u in 1..(self.npages * 2) {
            let tag = self.get_bit(u);
            let d = self.max_order - ((u + 1).next_power_of_two().trailing_zeros() as usize - 1);
            let l = left(u);
            let r = right(u);

            let npages = 1 << d;
            let addr = self.page_begin + npages * P::to_usize() * (u - (1 << (self.max_order - d)));

            // 1-nodes that don't have any 1-node child are allocated chunks.
            if tag == true && (d == 0 || (self.get_bit(l) == false && self.get_bit(r) == false)) {
                nalloc += npages;
            }

            // Root 0-nodes and 0-nodes whose father is 1-node and buddy is 1-node are free chunks.
            if tag == false
                && (u == 1
                    || (self.get_bit(father(u)) == true) && self.get_bit(buddy_idx(u)) == true)
            {
                let mut found = false;
                let head = &self.freelist[d] as *const Rawlist;
                let mut p = self.freelist[d].next as *const Rawlist;
                while p != head {
                    if p as usize == addr {
                        found = true;
                        break;
                    }
                    p = (*p).next;
                }
                assert_eq!(found, true);
                nfree += npages;
            }

            // Children of any 0-node must be also 0-nodes.
            if d != 0 && tag == false {
                assert_eq!(self.get_bit(l), false);
                assert_eq!(self.get_bit(r), false);
            }
        }

        let mut nfreelist = 0;
        for i in 0..=self.max_order {
            let head = &self.freelist[i] as *const Rawlist;
            let mut p = self.freelist[i].next as *const Rawlist;
            // let mut free_ptrs = vec![];
            while p != head {
                nfreelist += 1 << i;
                // free_ptrs.push(self.bitmap_idx(p as usize, i));
                p = (*p).next;
            }
        }
        assert_eq!(nfree, nfreelist);
        assert_eq!(nalloc + nfree, self.npages);
    }
}

/// Allocator that holds multiple buddy systems.
pub struct MultiBuddySystem<P> {
    head: *mut BuddySystem<P>,
}

impl<P: Unsigned + PowerOfTwo + 'static> MultiBuddySystem<P> {
    /// Create an empty multi-buddy system allocator.
    pub const fn new() -> Self {
        Self {
            head: ptr::null_mut(),
        }
    }

    /// Add zone [begin, end) with page size into this allocator.
    ///
    /// Note that size of the memory handled by one buddy system is requied to be a power of two.
    /// But this may waste a large amount of memory if it's slightly smaller than a power of two.
    /// Thus, to make full use of the memory, we recursively build buddy systems on the
    /// memory left by previous buddy system and terminate when it fails to build.
    ///
    /// This may add O(logN) buddy systems, where N = end - begin.
    pub unsafe fn add_zone(&mut self, mut begin: usize, end: usize) {
        #[cfg(any(test, debug_assertions))]
        assert_eq!(self.overlap(begin, end, 0 as *mut BuddySystem<P>), false);

        while let Ok(p) = BuddySystem::<P>::build(begin, end) {
            p.next = self.head;
            self.head = p;
            begin = p.page_end;
        }

        #[cfg(any(test, debug_assertions))]
        self.check();
    }

    /// Allocate memory of `2^order` pages.
    ///
    /// Loop through every registered zone and try allocating.
    /// O(logN) per zone.
    pub unsafe fn alloc1(&self, order: usize) -> *mut u8 {
        let mut b = self.head;
        let mut p: *mut u8 = ptr::null_mut();
        while !b.is_null() && p.is_null() {
            p = (*b).alloc1(order);
            b = (*b).next;
        }
        p
    }

    /// Allocate memory according to layout.
    pub unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        self.alloc1(to_order(P::to_usize(), &layout))
    }

    /// Free memory pointed by ptr with specific layout.
    ///
    /// Return the order of the freed page after merging possible buddy pages.
    /// Loop through every registered buddy system and try deallocating.
    /// O(logN) per zone.
    pub unsafe fn free(&self, ptr: *mut u8, order: usize) -> usize {
        let mut b = self.head;
        while !b.is_null() {
            if (*b).page_begin <= ptr as usize && (ptr as usize) < (*b).page_end {
                return (*b).free(ptr, order);
            }
            b = (*b).next;
        }
        panic!("pointer doesn't fall into any buddy system");
    }

    /// Free memory of specific layout.
    pub unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        self.free(ptr, to_order(P::to_usize(), &layout));
    }

    /// Check that buddy systems donot overlap with each other.
    #[cfg(any(test, debug_assertions))]
    pub unsafe fn check(&self) {
        let mut b = self.head;
        while !b.is_null() {
            // This buddy system should not overlap with other buddy systems.
            assert_eq!(self.overlap(b as usize, (*b).page_end, b), false);
            b = (*b).next;
        }
    }

    /// Check that if memory [begin, end) is overlapped with any zone in
    /// the buddy system except the ignored one.
    #[cfg(any(test, debug_assertions))]
    pub unsafe fn overlap(&self, begin: usize, end: usize, ignore: *mut BuddySystem<P>) -> bool {
        let mut b = self.head;
        while !b.is_null() {
            if b != ignore {
                if !((*b).page_end <= begin || end <= (b as usize)) {
                    return true;
                }
            }
            b = (*b).next;
        }
        false
    }
}

/// A thread-safe allocator with multiple buddy systems.
pub struct Allocator<P> {
    inner: Mutex<MultiBuddySystem<P>>,
}

impl<P: Unsigned + PowerOfTwo + 'static> Allocator<P> {
    /// Create a allocator with empty memory.
    pub const fn new() -> Self {
        Self {
            inner: Mutex::new(MultiBuddySystem::new()),
        }
    }

    /// Add free memory [begin, end) to this allocator.
    pub unsafe fn add_zone(&mut self, begin: usize, end: usize) {
        self.inner.lock().add_zone(begin, end);
    }
}

unsafe impl<P: Unsigned + PowerOfTwo + 'static> GlobalAlloc for Allocator<P> {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        self.inner.lock().alloc(layout)
    }
    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        self.inner.lock().dealloc(ptr, layout);
    }
}

#[cfg(test)]
pub mod tests {
    use core::ops::Range;

    use rand::{prelude::SliceRandom, Rng};
    use std::vec::Vec;
    use typenum::U64;

    use super::*;

    const NBUF: usize = 10 * 4096;
    pub type PGSIZE = U64; // Should be large enough to store the Rawlist structure.

    fn to_layout(order: usize) -> Layout {
        let sz = (1 << order) * PGSIZE::to_usize();
        Layout::from_size_align(sz, sz).unwrap()
    }

    fn alloc_some<A: GlobalAlloc>(a: &A, order_rg: Range<usize>) -> Vec<(*mut u8, Layout)> {
        let mut rng = rand::thread_rng();
        let mut result = Vec::new();
        unsafe {
            loop {
                let ly = to_layout(rng.gen_range(order_rg.clone()));
                let p = a.alloc(ly.clone());
                if p.is_null() {
                    break;
                }
                // Blur with random stuff.
                ptr::write_bytes(p, rng.gen_range(0xAC..0xFF), ly.size());
                result.push((p, ly));
            }
        }
        result
    }
    fn free_some<A: GlobalAlloc>(a: &A, mut pg: Vec<(*mut u8, Layout)>) {
        let mut rng = rand::thread_rng();
        pg.shuffle(&mut rng);
        for (p, ly) in pg {
            unsafe {
                a.dealloc(p, ly);
            }
        }
    }

    fn test_range<A: GlobalAlloc>(a: &A, order_rg: Range<usize>) {
        let mut pg = alloc_some(a, order_rg.clone());
        let pg1 = pg.drain(pg.len() / 2..).collect();
        free_some(a, pg1);
        pg.append(&mut alloc_some(a, order_rg));
        free_some(a, pg);
    }

    pub fn test1<A: GlobalAlloc>(a: &A) {
        test_range(a, 0..2);
        test_range(a, 0..10);
    }

    #[test]
    fn test_allocator() {
        let n: usize = PGSIZE::to_usize() * 1000;
        let mut v: Vec<u8> = Vec::new();
        v.reserve(n);
        let mut rng = rand::thread_rng();
        for _ in 0..n {
            v.push(rng.gen_range(0..0xFF));
        }
        let ptr = v.as_mut_ptr() as usize;
        unsafe {
            let mut a: Allocator<PGSIZE> = Allocator::new();
            a.add_zone(ptr, ptr + n);
            test1(&a);
        }
    }

    #[test]
    fn test_utils() {
        assert_eq!(left(1), 2);
        assert_eq!(left(2), 4);
        assert_eq!(left(3), 6);

        assert_eq!(right(1), 3);
        assert_eq!(right(2), 5);
        assert_eq!(right(3), 7);

        assert_eq!(2usize.next_power_of_two(), 2);
    }

    #[test]
    fn test_buddy() {
        let buf = [0u8; NBUF];
        let mem_begin = buf.as_ptr() as usize;
        let mem_end = buf.as_ptr() as usize + NBUF;

        unsafe {
            let b: &mut BuddySystem<PGSIZE> = BuddySystem::build(mem_begin, mem_end).unwrap();
            assert!(mem_begin < b.page_end && b.page_end <= mem_end);
            b.check();
            let layouts = [
                Layout::from_size_align(4, 2).unwrap(),
                Layout::from_size_align(5, 4).unwrap(),
                Layout::from_size_align(2 * PGSIZE::to_usize(), PGSIZE::to_usize()).unwrap(),
                Layout::from_size_align(PGSIZE::to_usize(), PGSIZE::to_usize()).unwrap(),
            ];

            let mut to_dealloc = vec![];
            for x in layouts.iter() {
                let ptr = b.alloc(x.clone());
                assert_ne!(ptr, ptr::null_mut());
                ptr::write_bytes(ptr, 0xFF, x.size());

                to_dealloc.push((ptr, x.clone()));
                b.check();
            }

            loop {
                let ly = Layout::from_size_align(PGSIZE::to_usize(), PGSIZE::to_usize()).unwrap();
                let ptr = b.alloc(ly.clone());
                if ptr.is_null() {
                    break;
                }
                ptr::write_bytes(ptr, 0xFF, ly.size());
                to_dealloc.push((ptr, ly.clone()));
                b.check();
            }

            for (ptr, layout) in to_dealloc {
                b.dealloc(ptr, layout);
                b.check();
            }
        }
    }

    #[test]
    fn test_misaligned_build() {
        // x86 won't fail even if misaligned.
        let buf = [0u8; NBUF];
        let mem_begin = buf.as_ptr() as usize;
        let mem_end = buf.as_ptr() as usize + NBUF;
        for i in 0..8 {
            let mut b: MultiBuddySystem<PGSIZE> = MultiBuddySystem::new();
            unsafe { b.add_zone(mem_begin + i, mem_end) }
        }
    }

    #[test]
    #[should_panic]
    fn test_add_zone() {
        let buf = [0u8; NBUF];
        let mem_begin = buf.as_ptr() as usize;
        let mem_end = buf.as_ptr() as usize + NBUF;
        let mut b: MultiBuddySystem<PGSIZE> = MultiBuddySystem::new();

        unsafe {
            b.add_zone(mem_begin, mem_end);
            b.add_zone(mem_begin, mem_end);
        }
    }

    #[test]
    fn test_multi_buddy() {
        let buf = [0u8; NBUF];
        let mem_begin = buf.as_ptr() as usize;
        let mem_end = buf.as_ptr() as usize + NBUF;
        let mut b: MultiBuddySystem<PGSIZE> = MultiBuddySystem::new();

        unsafe {
            b.add_zone(mem_begin, mem_end);

            let layouts = [
                Layout::from_size_align(4, 2).unwrap(),
                Layout::from_size_align(5, 4).unwrap(),
                Layout::from_size_align(2 * PGSIZE::to_usize(), PGSIZE::to_usize()).unwrap(),
                Layout::from_size_align(PGSIZE::to_usize(), PGSIZE::to_usize()).unwrap(),
            ];

            let mut to_dealloc = vec![];
            for x in layouts.iter() {
                let ptr = b.alloc(x.clone());
                assert_ne!(ptr, ptr::null_mut());
                ptr::write_bytes(ptr, 0xFF, x.size());
                to_dealloc.push((ptr, x.clone()));
            }

            loop {
                let ly = Layout::from_size_align(PGSIZE::to_usize(), PGSIZE::to_usize()).unwrap();
                let ptr = b.alloc(ly.clone());
                if ptr.is_null() {
                    break;
                }
                ptr::write_bytes(ptr, 0xFF, ly.size());
                to_dealloc.push((ptr, ly.clone()));
            }

            for (ptr, layout) in to_dealloc {
                b.dealloc(ptr, layout);
            }
        }
    }
}