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
#![feature(allocator_api)]
#![feature(const_trait_impl)]
#![feature(maybe_uninit_uninit_array)]
#![feature(never_type)]
#![cfg_attr(feature="logging", feature(panic_abort))]
#![feature(slice_ptr_get)]
#![feature(strict_provenance)]

#![deny(warnings)]
#![doc(test(attr(deny(warnings))))]
#![doc(test(attr(allow(dead_code))))]
#![doc(test(attr(allow(unused_variables))))]
#![allow(clippy::collapsible_else_if)]

#![cfg_attr(not(feature="logging"), no_std)]

#[cfg(feature="logging")]
extern crate core;

#[cfg(feature="logging")]
extern crate panic_abort;

extern crate alloc;

mod base;
pub use base::*;

pub mod fallbacked;

pub mod limited_up_to;

mod global;
pub use global::*;

mod as_global;
pub use as_global::*;

#[cfg(feature="logging")]
mod logging;
#[cfg(feature="logging")]
pub use logging::*;

#[cfg(all(not(target_os="dos"), windows))]
mod winapi;

#[cfg(all(not(target_os="dos"), not(windows)))]
mod posix;

#[cfg(not(target_os="dos"))]
mod system;

#[cfg(not(target_os="dos"))]
pub use system::*;

mod impossible;
pub use impossible::*;

mod non_working;
pub use non_working::*;

pub mod stacked;

pub mod freelist;

#[doc(hidden)]
pub use core::alloc::Layout as std_alloc_Layout;
#[doc(hidden)]
pub use core::mem::MaybeUninit as std_mem_MaybeUninit;
#[doc(hidden)]
pub use core::ptr::addr_of_mut as std_ptr_addr_of_mut;

#[macro_export]
macro_rules! global_freelist_allocator_128_KiB_align_8 {
    () => {
        const MEM_SIZE: usize = 131072;

        static mut MEM: [$crate::std_mem_MaybeUninit<u8>; MEM_SIZE] =
            [$crate::std_mem_MaybeUninit::uninit(); MEM_SIZE]
        ;

        static STACKED: $crate::stacked::Stacked =
            $crate::stacked::Stacked::from_static_array(unsafe { &mut *$crate::std_ptr_addr_of_mut!(MEM) })
        ;

        type Freelist8B = $crate::AsGlobal<$crate::fallbacked::Fallbacked<$crate::limited_up_to::LimitedUpTo<
            $crate::freelist::Freelist<$crate::freelist::NoLimit, &'static $crate::stacked::Stacked>
        >, &'static Freelist16B>>;

        #[global_allocator]
        static FREELIST_8_B: Freelist8B = $crate::AsGlobal($crate::fallbacked::Fallbacked(
            $crate::limited_up_to::LimitedUpTo::new(
                unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(8, 8) },
                $crate::freelist::Freelist::new(
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(8, 8) },
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(1, 1) },
                    $crate::freelist::NoLimit,
                    &STACKED
                )
            ),
            &FREELIST_16_B
        ));

        type Freelist16B = $crate::fallbacked::Fallbacked<$crate::limited_up_to::LimitedUpTo<
            $crate::freelist::Freelist<$crate::freelist::NoLimit, &'static $crate::stacked::Stacked>
        >, &'static Freelist32B>;

        static FREELIST_16_B: Freelist16B = $crate::fallbacked::Fallbacked(
            $crate::limited_up_to::LimitedUpTo::new(
                unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(16, 8) },
                $crate::freelist::Freelist::new(
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(16, 8) },
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(9, 1) },
                    $crate::freelist::NoLimit,
                    &STACKED
                )
            ),
            &FREELIST_32_B
        );

        type Freelist32B = $crate::fallbacked::Fallbacked<$crate::limited_up_to::LimitedUpTo<
            $crate::freelist::Freelist<$crate::freelist::NoLimit, &'static $crate::stacked::Stacked>
        >, &'static Freelist64B>;

        static FREELIST_32_B: Freelist32B = $crate::fallbacked::Fallbacked(
            $crate::limited_up_to::LimitedUpTo::new(
                unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(32, 8) },
                $crate::freelist::Freelist::new(
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(32, 8) },
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(17, 1) },
                    $crate::freelist::NoLimit,
                    &STACKED
                )
            ),
            &FREELIST_64_B
        );

        type Freelist64B = $crate::fallbacked::Fallbacked<$crate::limited_up_to::LimitedUpTo<
            $crate::freelist::Freelist<$crate::freelist::NoLimit, &'static $crate::stacked::Stacked>
        >, &'static Freelist128B>;

        static FREELIST_64_B: Freelist64B = $crate::fallbacked::Fallbacked(
            $crate::limited_up_to::LimitedUpTo::new(
                unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(64, 8) },
                $crate::freelist::Freelist::new(
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(64, 8) },
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(33, 1) },
                    $crate::freelist::NoLimit,
                    &STACKED
                )
            ),
            &FREELIST_128_B
        );

        type Freelist128B = $crate::fallbacked::Fallbacked<$crate::limited_up_to::LimitedUpTo<
            $crate::freelist::Freelist<$crate::freelist::NoLimit, &'static $crate::stacked::Stacked>
        >, &'static Freelist256B>;

        static FREELIST_128_B: Freelist128B = $crate::fallbacked::Fallbacked(
            $crate::limited_up_to::LimitedUpTo::new(
                unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(128, 8) },
                $crate::freelist::Freelist::new(
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(128, 8) },
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(65, 1) },
                    $crate::freelist::NoLimit,
                    &STACKED
                )
            ),
            &FREELIST_256_B
        );

        type Freelist256B = $crate::fallbacked::Fallbacked<$crate::limited_up_to::LimitedUpTo<
            $crate::freelist::Freelist<$crate::freelist::NoLimit, &'static $crate::stacked::Stacked>
        >, &'static Freelist512B>;

        static FREELIST_256_B: Freelist256B = $crate::fallbacked::Fallbacked(
            $crate::limited_up_to::LimitedUpTo::new(
                unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(256, 8) },
                $crate::freelist::Freelist::new(
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(256, 8) },
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(129, 1) },
                    $crate::freelist::NoLimit,
                    &STACKED
                )
            ),
            &FREELIST_512_B
        );

        type Freelist512B = $crate::fallbacked::Fallbacked<$crate::limited_up_to::LimitedUpTo<
            $crate::freelist::Freelist<$crate::freelist::NoLimit, &'static $crate::stacked::Stacked>
        >, &'static Freelist1KiB>;

        static FREELIST_512_B: Freelist512B = $crate::fallbacked::Fallbacked(
            $crate::limited_up_to::LimitedUpTo::new(
                unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(512, 8) },
                $crate::freelist::Freelist::new(
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(512, 8) },
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(257, 1) },
                    $crate::freelist::NoLimit,
                    &STACKED
                )
            ),
            &FREELIST_1_KIB
        );

        type Freelist1KiB = $crate::fallbacked::Fallbacked<$crate::limited_up_to::LimitedUpTo<
            $crate::freelist::Freelist<$crate::freelist::NoLimit, &'static $crate::stacked::Stacked>
        >, &'static Freelist2KiB>;

        static FREELIST_1_KIB: Freelist1KiB = $crate::fallbacked::Fallbacked(
            $crate::limited_up_to::LimitedUpTo::new(
                unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(1024, 8) },
                $crate::freelist::Freelist::new(
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(1024, 8) },
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(513, 1) },
                    $crate::freelist::NoLimit,
                    &STACKED
                )
            ),
            &FREELIST_2_KIB
        );

        type Freelist2KiB = $crate::fallbacked::Fallbacked<$crate::limited_up_to::LimitedUpTo<
            $crate::freelist::Freelist<$crate::freelist::NoLimit, &'static $crate::stacked::Stacked>
        >, &'static Freelist4KiB>;

        static FREELIST_2_KIB: Freelist2KiB = $crate::fallbacked::Fallbacked(
            $crate::limited_up_to::LimitedUpTo::new(
                unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(2048, 8) },
                $crate::freelist::Freelist::new(
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(2048, 8) },
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(1025, 1) },
                    $crate::freelist::NoLimit,
                    &STACKED
                )
            ),
            &FREELIST_4_KIB
        );

        type Freelist4KiB = $crate::fallbacked::Fallbacked<$crate::limited_up_to::LimitedUpTo<
            $crate::freelist::Freelist<$crate::freelist::NoLimit, &'static $crate::stacked::Stacked>
        >, &'static Freelist8KiB>;

        static FREELIST_4_KIB: Freelist4KiB = $crate::fallbacked::Fallbacked(
            $crate::limited_up_to::LimitedUpTo::new(
                unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(4096, 8) },
                $crate::freelist::Freelist::new(
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(4096, 8) },
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(2049, 1) },
                    $crate::freelist::NoLimit,
                    &STACKED
                )
            ),
            &FREELIST_8_KIB
        );

        type Freelist8KiB = $crate::fallbacked::Fallbacked<$crate::limited_up_to::LimitedUpTo<
            $crate::freelist::Freelist<$crate::freelist::NoLimit, &'static $crate::stacked::Stacked>
        >, &'static Freelist16KiB>;

        static FREELIST_8_KIB: Freelist8KiB = $crate::fallbacked::Fallbacked(
            $crate::limited_up_to::LimitedUpTo::new(
                unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(8192, 8) },
                $crate::freelist::Freelist::new(
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(8192, 8) },
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(4097, 1) },
                    $crate::freelist::NoLimit,
                    &STACKED
                )
            ),
            &FREELIST_16_KIB
        );

        type Freelist16KiB = $crate::fallbacked::Fallbacked<$crate::limited_up_to::LimitedUpTo<
            $crate::freelist::Freelist<$crate::freelist::NoLimit, &'static $crate::stacked::Stacked>
        >, &'static Freelist32KiB>;

        static FREELIST_16_KIB: Freelist16KiB = $crate::fallbacked::Fallbacked(
            $crate::limited_up_to::LimitedUpTo::new(
                unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(16384, 8) },
                $crate::freelist::Freelist::new(
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(16384, 8) },
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(8193, 1) },
                    $crate::freelist::NoLimit,
                    &STACKED
                )
            ),
            &FREELIST_32_KIB
        );
    
        type Freelist32KiB = $crate::fallbacked::Fallbacked<$crate::limited_up_to::LimitedUpTo<
            $crate::freelist::Freelist<$crate::freelist::NoLimit, &'static $crate::stacked::Stacked>
        >, &'static Freelist64KiB>;

        static FREELIST_32_KIB: Freelist32KiB = $crate::fallbacked::Fallbacked(
            $crate::limited_up_to::LimitedUpTo::new(
                unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(32768, 8) },
                $crate::freelist::Freelist::new(
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(32768, 8) },
                    unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(16385, 1) },
                    $crate::freelist::NoLimit,
                    &STACKED
                )
            ),
            &FREELIST_64_KIB
        );

        type Freelist64KiB = $crate::limited_up_to::LimitedUpTo<
            $crate::freelist::Freelist<$crate::freelist::NoLimit, &'static $crate::stacked::Stacked>
        >;

        static FREELIST_64_KIB: Freelist64KiB = $crate::limited_up_to::LimitedUpTo::new(
            unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(65536, 8) },
            $crate::freelist::Freelist::new(
                unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(65536, 8) },
                unsafe { $crate::std_alloc_Layout::from_size_align_unchecked(32769, 1) },
                $crate::freelist::NoLimit,
                &STACKED
            )
        );
    };
}