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
use core::alloc::{Allocator, Layout};
use core::ops::{Deref, DerefMut};
use core::ptr::NonNull;

pub(crate) mod block_allocator;
pub(crate) mod bump_allocator;

use block_allocator::BlockAllocator;

use self::bump_allocator::StartEnd;

struct SendNonNull<T>(NonNull<T>);
unsafe impl<T> Send for SendNonNull<T> {}

impl<T> Clone for SendNonNull<T> {
    fn clone(&self) -> Self {
        SendNonNull(self.0)
    }
}
impl<T> Copy for SendNonNull<T> {}

impl<T> Deref for SendNonNull<T> {
    type Target = NonNull<T>;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T> DerefMut for SendNonNull<T> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

const EWRAM_END: usize = 0x0204_0000;
const IWRAM_END: usize = 0x0300_8000;

#[global_allocator]
static GLOBAL_ALLOC: BlockAllocator = unsafe {
    BlockAllocator::new(StartEnd {
        start: data_end,
        end: || EWRAM_END,
    })
};

macro_rules! impl_zst_allocator {
    ($name_of_struct: ty, $name_of_static: ident) => {
        unsafe impl Allocator for $name_of_struct {
            fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, core::alloc::AllocError> {
                $name_of_static.allocate(layout)
            }

            unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
                $name_of_static.deallocate(ptr, layout)
            }
        }
    };
}

/// This is the allocator for the External Working Ram. This is currently
/// equivalent to the Global Allocator (where things are allocated if no allocator is provided). This implements the allocator trait, so
/// is meant to be used in specifying where certain structures should be
/// allocated.
///
/// ```rust,no_run
/// #![feature(allocator_api)]
/// # #![no_std]
/// # #![no_main]
/// # use agb::ExternalAllocator;
/// # extern crate alloc;
/// # use alloc::vec::Vec;
/// # fn foo(gba: &mut agb::Gba) {
/// let mut v = Vec::new_in(ExternalAllocator);
/// v.push("hello, world");
/// assert!(
///     (0x0200_0000..0x0204_0000).contains(&(v.as_ptr() as usize)),
///     "the address of the vector is inside ewram"
/// );
/// # }
/// ```
#[derive(Clone)]
pub struct ExternalAllocator;

impl_zst_allocator!(ExternalAllocator, GLOBAL_ALLOC);

/// This is the allocator for the Internal Working Ram. This implements the
/// allocator trait, so is meant to be used in specifying where certain
/// structures should be allocated.
///
/// ```rust,no_run
/// #![feature(allocator_api)]
/// # #![no_std]
/// # #![no_main]
/// # use agb::InternalAllocator;
/// # extern crate alloc;
/// # use alloc::vec::Vec;
/// # fn foo(gba: &mut agb::Gba) {
/// let mut v = Vec::new_in(InternalAllocator);
/// v.push("hello, world");
/// assert!(
///     (0x0300_0000..0x0300_8000).contains(&(v.as_ptr() as usize)),
///     "the address of the vector is inside iwram"
/// );
/// # }
/// ```
#[derive(Clone)]
pub struct InternalAllocator;

impl_zst_allocator!(InternalAllocator, __IWRAM_ALLOC);

static __IWRAM_ALLOC: BlockAllocator = unsafe {
    BlockAllocator::new(StartEnd {
        start: iwram_data_end,
        end: || IWRAM_END,
    })
};

#[cfg(any(test, feature = "testing"))]
pub(crate) unsafe fn number_of_blocks() -> u32 {
    GLOBAL_ALLOC.number_of_blocks()
}

fn iwram_data_end() -> usize {
    extern "C" {
        static __iwram_end: usize;
    }

    // TODO: This seems completely wrong, but without the &, rust generates
    // a double dereference :/. Maybe a bug in nightly?
    (unsafe { &__iwram_end }) as *const _ as usize
}

fn data_end() -> usize {
    extern "C" {
        static __ewram_data_end: usize;
    }

    // TODO: This seems completely wrong, but without the &, rust generates
    // a double dereference :/. Maybe a bug in nightly?
    (unsafe { &__ewram_data_end }) as *const _ as usize
}

#[cfg(test)]
mod test {
    const EWRAM_START: usize = 0x0200_0000;

    use super::*;
    use alloc::boxed::Box;
    use alloc::vec;
    use alloc::vec::Vec;

    #[test_case]
    fn test_box(_gba: &mut crate::Gba) {
        let first_box = Box::new(1);
        let second_box = Box::new(2);

        assert!(&*first_box as *const _ < &*second_box as *const _);
        assert_eq!(*first_box, 1);
        assert_eq!(*second_box, 2);

        let address = &*first_box as *const _ as usize;
        assert!(
            (EWRAM_START..EWRAM_END).contains(&address),
            "ewram is located between 0x0200_0000 and 0x0204_0000, address was actually found to be {address:#010X}"
        );
    }

    #[test_case]
    fn test_vec(_gba: &mut crate::Gba) {
        let mut v = Vec::with_capacity(5);

        for i in 0..100 {
            v.push(i);
        }

        for (i, &e) in v.iter().enumerate() {
            assert_eq!(e, i);
        }
    }

    #[test_case]
    fn test_creating_and_removing_things(_gba: &mut crate::Gba) {
        let item = Box::new(1);
        for i in 0..1_000 {
            let x = Box::new(i);
            assert_eq!(*x, i);
            let address = &*x as *const _ as usize;
            assert!(
                (EWRAM_START..EWRAM_END).contains(&address),
                "ewram is located between 0x0200_0000 and 0x0204_0000, address was actually found to be {address:#010X}"
            );
        }

        assert_eq!(*item, 1);
    }

    #[test_case]
    fn test_adding_to_2_different_vectors(_gba: &mut crate::Gba) {
        let mut v1 = vec![1, 2, 3];
        let mut v2 = vec![4, 5, 6];

        for i in 0..100 {
            v1.push(i + 100);
            v2.push(i + 1000);
        }

        assert_eq!(v1[40], 137);
        assert_eq!(v2[78], 1075);
    }

    #[test_case]
    fn should_return_data_end_somewhere_in_ewram(_gba: &mut crate::Gba) {
        let data_end = data_end();

        assert!(
            0x0200_0000 <= data_end,
            "data end should be bigger than 0x0200_0000, got {data_end}"
        );
        assert!(
            0x0204_0000 > data_end,
            "data end should be smaller than 0x0203_0000"
        );
    }

    #[test_case]
    fn should_return_data_end_somewhere_in_iwram(_gba: &mut crate::Gba) {
        let data_end = iwram_data_end();

        assert!(
            (0x0300_0000..0x0300_8000).contains(&data_end),
            "iwram data end should be in iwram, instead was {data_end}"
        );
    }

    #[test_case]
    fn allocate_to_iwram_works(_gba: &mut crate::Gba) {
        let a = Box::new_in(1, InternalAllocator);
        let p = &*a as *const i32;
        let addr = p as usize;
        assert!(
            (0x0300_0000..0x0300_8000).contains(&addr),
            "address of allocation should be within iwram, instead at {p:?}"
        );
    }

    #[test_case]
    fn benchmark_allocation(_gba: &mut crate::Gba) {
        let mut stored: Vec<Vec<u8>> = Vec::new();

        let mut rng = crate::rng::RandomNumberGenerator::new();

        const MAX_VEC_LENGTH: usize = 100;

        enum Action {
            Add { size: usize },
            Remove { index: usize },
        }

        let next_action = |rng: &mut crate::rng::RandomNumberGenerator, stored: &[Vec<u8>]| {
            if stored.len() >= MAX_VEC_LENGTH {
                Action::Remove {
                    index: (rng.gen() as usize) % stored.len(),
                }
            } else if stored.is_empty() || rng.gen() as usize % 4 != 0 {
                Action::Add {
                    size: rng.gen() as usize % 32,
                }
            } else {
                Action::Remove {
                    index: (rng.gen() as usize) % stored.len(),
                }
            }
        };

        for _ in 0..10000 {
            match next_action(&mut rng, &stored) {
                Action::Add { size } => {
                    stored.push(Vec::with_capacity(size));
                }
                Action::Remove { index } => {
                    stored.swap_remove(index);
                }
            }
        }
    }

    #[test_case]
    fn growth_works(_gba: &mut crate::Gba) {
        let mut growing_vector = Vec::with_capacity(1);

        for i in 0..1000 {
            growing_vector.push(i);
            growing_vector.reserve_exact(i + 2);

            for (idx, elem) in growing_vector.iter().enumerate() {
                assert_eq!(idx, *elem);
            }
        }
    }
}