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
//
// Copyright 2023, Colias Group, LLC
//
// SPDX-License-Identifier: BSD-2-Clause
//

use core::alloc::Layout;

use crate::{AbstractBounceBufferAllocator, Offset, Size};

pub struct Bump {
    watermark: Offset,
    end: Offset,
}

impl Bump {
    pub fn new(size: Size) -> Self {
        Self {
            watermark: 0,
            end: size,
        }
    }
}

impl AbstractBounceBufferAllocator for Bump {
    type Error = ();

    fn allocate(&mut self, layout: Layout) -> Result<Offset, Self::Error> {
        let offset = self.watermark.next_multiple_of(layout.align());
        let new_watermark = offset + layout.size();
        assert!(new_watermark <= self.end);
        self.watermark = new_watermark;
        Ok(offset)
    }

    fn deallocate(&mut self, _offset: Offset, _size: Size) {}
}