bitena 0.1.2

Simple lock-free Thread-safe Bump Arena for extemely fast memory allocations
Documentation
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
//! # Bitena
//!
//! A small, extremely fast, lock-free thread-safe arena bump allocator that can hand out multiple
//! mutable elements, structs, slices, or read-only &strs from a single pre-allocated block.
//!
//! ## What is an Arena?
//!
//! An arena allocator is a memory allocation strategy that pre-allocates a large block
//! of memory once, and can then hand out sub-allocations from that block sequentially.
//! Bitena is are much faster than normal memory allocations because:
//!
//!   - **Bulk Allocation**: The entire arena is allocated all at once
//!   - **No Fragmentation**: Memory is allocated sequentially
//!   - **Fast Bookkeeping**: No complex tracking/reallocations
//!   - **Simplified Deallocation**: The entire arena is freed simultaneously
//!
//! `Bitena` is special in that, because of its design, it is not subject
//! to the same use after free or overlapped memory bugs that are possible with
//! some other bump allocators.
//!
//! ## Quick Start
//!
//! Add the following to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! bitena = "0.1"
//! ```
//!
//! ```rust
//!   use bitena::Bitena;
//!   fn main() {
//!     let mut bitena = Bitena::new(1024).expect("Failed to allocate memory");
//!     let num = bitena.alloc(42u32);
//!     let stnum = format!("Num: {}", *num);
//!     let s = bitena.alloc_str(&stnum);
//!     println!("{}  {:?}", *num, s);
//!   }
//! ```
//!
//! # The API
//!
//! ## new(byte_capacity)
//! Allocate a new Arena with a specified capacity.
//!
//! ## alloc(item) or try_alloc(item)
//! Allocate an element or structure in the Arena
//!
//! ## alloc_slice(init_value, items) or try_alloc_slice(init_value, items)
//! Allocate a slice vector of elements
//!
//! ## alloc_str(&str) or try_alloc_str(&str)
//! Store a &str in the Arena
//!
//! ## reset()
//! Reset the arena. This requires that all allocations are vacated, and
//! re-initializes the Arena to it's brand new state.
//!
//! ## Tradeoffs
//!
//!   - Individual Items are not resizeable. Each element or item allocated from
//!     the arena is a fixed size. You need to individually Box<T> any items, 
//!     (Strings, Vecs, Fat Pointers, file handles, etc) to avoid leaking memory.
//!
//!   - The entire arena will be dropped in a single operation. Individual Drop
//!     operations will not be performed on the Arena's contents. This then will
//!     leak any memory separately allocated as with Strings and Vecs.
//!
//!   - **No item Reclamation**: Any unused allocations are stuck until
//!     the whole arena is dropped or reset().
//!
//!   - **Fixed Size**: The arena has a set fixed size that doesn't grow.
//!
//! # MIRI to the Rescue
//!
//! Miri detected attempted memory leaks with String, and Vec in our tests.
//!
//! ```ignore
//!   cargo +nightly miri run
//! ```
//!
//!
//! # Use Cases:
//!
//!  - **Long-lived Data**: Perform one alloc from the system, and break that into
//!    all the allocations your need for the life of your program
//!
//!  - **Short-lived Processing**: Temporary allocations for a process... encoding,
//!    parsing, compiling, translation, etc. All the memory can be reused with reset()
//!    or set or returned/deallocated at the end of processing.
//!
//!  - **Saving Space**: Many system allocation schemes allocate in page sized blocks
//!    so freed memory can be more efficiently managed for reallocation. Arena fills
//!    every byte it can given alignment requirements.
//!
//!
//! # Design Choices
//!
//! There are hundreds of possible improvements...  A lot of them are very
//! useful:
//!
//!  - Chunk Size, Statistics, Diagnostics, Memory Trimming, Snapshots - See arena-b
//!  - Generation Counter and Key reservation - See atomic-arena
//!  - Growable - See blink-alloc
//!  - Memory Paging and Arena Growth - See arena-allocator
//!  - Memory Reclamation from Individual Items - See drop-arena
//!  - Scoped Allocator, so you can restore memory in stages - See bump-scope
//!  - Memory Pools - See shared-arena
//!  - Boxed Allocations or Collections so you CAN use an arena with strings
//!       and vecs. See Rodeo and Bumpalo
//!  - Memory Layout Control, Rewinding, Thread-Local memory lakes, etc (See lake)
//!  - Detect Use after free - See arena-allocator
//!
//! Bitena is the Simple, Fast, and Multi-threaded solution.
//!
//! # What NOT to do:
//!
//! ❌ - Don't do this:
//! ```ignore
//!      let v = arena.try_alloc("Hello".to_string())?;    <== Still allocates from the heap
//! ```
//!
//! ✅ - Do this instead:
//! ```ignore
//!      let v = bitena.try_alloc("Hello")?;   <==  Arena based READ ONLY str
//! ```
//!
//! ✅ - Do this instead: allocate a Box in the Arena, the string data from the heap.
//! ```ignore
//!      let v = bitena.try_alloc(Box("Hello".to_string()))?; <== St from heap, Box handles drop
//! ```
//!
//!
//! ❌ - Don't do this:  
//! ```ignore
//!      let v = bitena.try_alloc(vec![42u32; 10])?;  <== Allocates data on the heap
//! ```
//!
//! ✅ - Do this instead:
//! ```ignore
//!      let v = bitena.try_alloc_slice(42u32, 10)?;   <==  Returns a 10 element MUTABLE slice
//! ```
//!
//! ✅ - Do this instead, allocate a Box in the Arena, Box allocates/deallocates Vec from the heap.
//! ```ignore
//!      let v = bitena.try_alloc(Box(vec![42u32; 10]))?;  <==  Vec on heap, Box handles drop
//! ```
//! In both cases of the Don't do this, a fat pointer will be stored in the arena,
//! and memory for the data or string will be allocated and LEAKED on the heap. In 
//!
//! ## License
//! MIT
//!
//! ## Contributions
//!
//! All contributions intentionally submitted for inclusion in this work by you will
//! be governed by the MIT License without consideration of any additional terms or
//! conditions. By contributing to this project, you agree to license your
//! contributions under the MIT License.
//!
//! ## Credits
//! Everyone who's been part of the Open Source Movement. Thank you.
//! Reverse allocations inspired by:
//!   https://fitzgen.com/2019/11/01/always-bump-downwards.html

use std::alloc::{Layout, dealloc};
use std::marker::PhantomData;
use std::mem;
use std::num::NonZero;
use std::ptr::{copy_nonoverlapping, NonNull};
use std::sync::atomic::{AtomicUsize, Ordering};

mod error;
pub use self::error::{Error, Result};


/// Bitena
///
/// A small, extremely fast, lock-free thread-safe arena bump allocator that can hand out multiple
/// mutable elements, structs, slices, or read-only &strs from a single pre-allocated block.
///
/// This crate is optimized for performance-critical applications
///
///  - FIXED SIZED ITEMS
///
///  - ALLOCATED ITEMS DON'T DROP
///
///  - FIXED SIZE ARENA
///
/// any element types that reserver memory or resources, file handles,
/// vecs, and strings will leak memory if allocated on Bitena because 
/// the arena drops in one operation, without dropping each individual
/// item as appropriate.
///
/// # Example
///
/// ```
/// use bitena::*;
///
/// fn main() -> Result<()> {
///     let bitena = Bitena::new(1024)?;
///     let slice = bitena.try_alloc_slice(0u32, 4)?;
///     for i in 0..4 {
///         slice[i] = i as u32;
///     }
///     println!("{:?}", slice);
///     Ok(())
/// }
/// ```
pub struct Bitena<'a> {
    buf: NonNull<u8>,
    end_byte_idx: AtomicUsize, // Allows for interior mutability without Mutex, RefCells, Arcs
    layout: Layout,            // Stores byte_capacity
    _marker: PhantomData<&'a ()>,
}

impl<'a> Bitena<'a> {
    /// Creates a new Arena with the specified byte capacity.
    ///
    /// # Example
    ///
    /// ```rust
    /// use bitena::*;
    ///
    /// fn main() -> Result<()> {
    ///     let bitena = Bitena::new(1024)?;
    ///
    ///     Ok(())
    /// }
    /// ```
    pub fn new(byte_capacity: usize) -> Result<Self> {
        assert!(byte_capacity > 0, "Capacity must be greater than zero.");

        let layout = Layout::from_size_align(byte_capacity, mem::align_of::<u8>())?;
        let buf = unsafe {
            let ptr = std::alloc::alloc(layout);
            if ptr.is_null() {
                return Err(Error::OutOfMemory);
            }
            ptr as *mut u8
        };
        Ok(Self {
            buf: NonNull::new(buf).ok_or(Error::PointerUnderflow)?,
            end_byte_idx: AtomicUsize::new(byte_capacity),
            layout,
            _marker: PhantomData,
        })
    }

    /// Allocates space for a single element and returns a mutable reference to it.
    ///
    /// # Safety
    ///
    /// The caller must ensure:
    /// - The arena has enough remaining memory
    /// - The reference is not used after the arena is reset or dropped
    ///
    /// # Example
    ///
    /// ```
    /// use bitena::*;
    ///
    /// fn main() -> Result<()> {
    ///     let bitena = Bitena::new(1024)?;
    ///     let value = bitena.try_alloc(42u32)?;
    ///     let num = bitena.try_alloc(-234i32)?;
    ///     *num = 100;
    ///     println!("{}", *num);
    ///     Ok(())
    /// }
    /// ```
    #[inline]
    pub fn alloc<T>(&self, val: T) -> &mut T {
        self.try_alloc(val)
            .unwrap_or_else(|e| panic!("Bitena Failed: {}", e))
    }

    pub fn try_alloc<T>(&self, val: T) -> Result<&mut T> {
        let sizet = std::mem::size_of::<T>();
        let align = std::mem::align_of::<T>();
        debug_assert!(sizet > 0, "Can't alloc 0 bytes");
        debug_assert!(align.is_power_of_two(), "Alignment must be a power of two");

        unsafe {
            loop {
                let end_byte_idx = self.end_byte_idx.load(Ordering::Relaxed);
                let ptr_num = (self.buf.as_ptr().add(end_byte_idx as usize) as usize)
                    .checked_sub(sizet)
                    .ok_or(Error::PointerUnderflow)?;

                //let ptr = (ptr as usize & !(align - 1)) as *mut u8;  // Align Ptr pre-Miri
                let ptr = self
                    .buf
                    .with_addr(NonZero::new(ptr_num & !(align - 1)).ok_or(Error::PointerUnderflow)?)
                    .as_ptr() as *mut u8;

                if (ptr as usize) < self.buf.as_ptr() as usize {
                    return Err(Error::OutOfMemory);
                }
                let new_end_byte_idx =
                    (ptr as usize).saturating_sub(self.buf.as_ptr() as usize) as usize;

                if let Ok(_) = self.end_byte_idx.compare_exchange_weak(
                    end_byte_idx,     // Expected value
                    new_end_byte_idx, // New value
                    Ordering::Relaxed,
                    Ordering::Relaxed,
                ) {
                    std::ptr::write(ptr as *mut T, val);
                    return Ok(&mut *(ptr as *mut T));
                }
            }
        }
    }

    /// Allocates space for a slice and returns a mutable slice reference.
    ///
    /// # Safety
    ///
    /// The caller must ensure:
    /// - The arena has enough remaining memory
    /// - The reference is not used after the arena is reset or dropped
    ///
    /// # Example
    ///
    /// ```
    /// use bitena::*;
    ///
    /// fn main() -> Result<()> {
    ///     let bitena = Bitena::new(1024)?;
    ///         let slice = bitena.try_alloc_slice(0u32, 4)?;
    ///         for i in 0..4 {
    ///             slice[i] = i as u32;
    ///         }
    ///         println!("{:?}", slice);
    ///     Ok(())
    /// }
    /// ```
    #[inline]
    pub fn alloc_slice<T>(&self, initial_value: T, len: usize) -> &mut [T] {
        self.try_alloc_slice(initial_value, len)
            .unwrap_or_else(|e| panic!("Bitena Failed: {}", e))
    }

    pub fn try_alloc_slice<T>(&self, initial_value: T, len: usize) -> Result<&mut [T]> {
        let sizet = std::mem::size_of::<T>();
        let align = std::mem::align_of::<T>();
        debug_assert!(sizet > 0, "Can't alloc 0 bytes");
        debug_assert!(align.is_power_of_two(), "Alignment must be a power of two");

        // This performs a compare and exchange loop on atomicUsize for the end_byte_idx value...
        // Making this algorithm safe for multi-thread apps
        unsafe {
            loop {
                let end_byte_idx = self.end_byte_idx.load(Ordering::Relaxed);
                let ptr_num = (self.buf.as_ptr().add(end_byte_idx) as usize)
                    .checked_sub(len * sizet)
                    .ok_or(Error::PointerUnderflow)?;

                //let ptr = (ptr as usize & !(align - 1)) as *mut u8;  // Align Ptr pre-Miri
                let ptr = self
                    .buf
                    .with_addr(NonZero::new(ptr_num & !(align - 1)).ok_or(Error::PointerUnderflow)?)
                    .as_ptr() as *mut u8;

                if (ptr as *mut u8 as usize) < self.buf.as_ptr() as usize {
                    return Err(Error::OutOfMemory);
                }
                let new_end_byte_idx =
                    (ptr as usize).saturating_sub(self.buf.as_ptr() as usize) as usize;

                if let Ok(_) = self.end_byte_idx.compare_exchange_weak(
                    end_byte_idx,     // Expected value
                    new_end_byte_idx, // New value
                    Ordering::Relaxed,
                    Ordering::Relaxed,
                ) {
                    // Initialize New Slice
                    if sizet == 1 {
                        // Bytes are VERY FAST to initialize
                        let byte_ptr = &initial_value as *const T as *const u8;
                        std::ptr::write_bytes(ptr, *byte_ptr, len * sizet);
                    } else if is_all_zeros(&initial_value) {
                        // Zeroed Memory is too
                        std::ptr::write_bytes(ptr, 0, len * sizet);
                    } else {
                        // Not so fast!!!
                        let initial_value_ptr = &initial_value as *const T as *const u8;
                        for i in 0..len {
                            copy_nonoverlapping(
                                initial_value_ptr,
                                (ptr as *mut u8).add(i * sizet),
                                sizet,
                            );
                        }
                    }
                    return Ok(std::slice::from_raw_parts_mut(ptr as *mut T, len));
                }
            }
        }
    }

    /// Allocates space for a str and returns a read-only reference, &str.
    ///
    /// # Safety
    ///
    /// The caller must ensure:
    /// - The arena has enough remaining memory
    /// - The reference is not used after the arena is reset or dropped
    ///
    /// # Example
    ///
    /// ```
    /// use bitena::*;
    ///
    /// fn main() -> Result<()> {
    ///     let bitena = Bitena::new(1024)?;
    ///         let num = bitena.try_alloc(42u32)?;
    ///         *num = 100;
    ///         let stnum = format!("Num: {}", *num);
    ///         let s = bitena.try_alloc_str(&stnum)?;
    ///         println!("{}  {:?}", *num, s);
    ///     Ok(())
    /// }
    /// ```
    #[inline]
    pub fn alloc_str(&self, st: &str) -> &str {
        self.try_alloc_str(st)
            .unwrap_or_else(|e| panic!("Bitena Failed: {}", e))
    }

    pub fn try_alloc_str(&self, st: &str) -> Result<&str> {
        let sizet = st.len();
        let align = std::mem::align_of::<u8>();
        if sizet == 0 {
            return Ok::<&str, Error>("");
        }
        debug_assert!(align.is_power_of_two(), "Alignment must be a power of two");

        unsafe {
            loop {
                let end_byte_idx = self.end_byte_idx.load(Ordering::Relaxed);
                let ptr_num = (self.buf.as_ptr().add(end_byte_idx as usize) as usize)
                    .checked_sub(sizet)
                    .ok_or(Error::PointerUnderflow)?;

                //let ptr = (ptr as usize & !(align - 1)) as *mut u8;  // Align Ptr pre-Miri
                let ptr = self
                    .buf
                    .with_addr(NonZero::new(ptr_num & !(align - 1)).ok_or(Error::PointerUnderflow)?)
                    .as_ptr() as *mut u8;

                if (ptr as usize) < self.buf.as_ptr() as usize {
                    return Err(Error::OutOfMemory);
                }
                let new_end_byte_idx =
                    (ptr as usize).saturating_sub(self.buf.as_ptr() as usize) as usize;

                if let Ok(_) = self.end_byte_idx.compare_exchange_weak(
                    end_byte_idx,     // Expected value
                    new_end_byte_idx, // New value
                    Ordering::Relaxed,
                    Ordering::Relaxed,
                ) {
                    copy_nonoverlapping(st.as_ptr(), ptr, sizet);
                    // Unchecked is Ok since the bytes came from a valid str
                    return Ok(std::str::from_utf8_unchecked(std::slice::from_raw_parts(
                        ptr, sizet,
                    )));
                }
            }
        }
    }

    /// Returns the number of bytes remaining in the arena.
    ///
    /// # Example
    ///
    /// ```rust
    /// use bitena::*;
    ///
    /// fn main() {
    ///     let bitena = Bitena::new(1024).unwrap();
    ///     assert_eq!(bitena.remaining(), 1024);
    /// }
    /// ```
    #[inline]
    pub fn remaining(&self) -> usize {
        self.end_byte_idx.load(Ordering::Relaxed)
    }

    /// Resets the arena, making all previously allocated memory available again.
    ///
    /// # Example
    ///
    /// ```rust
    /// use bitena::*;
    ///
    /// fn main() -> Result<()> {
    ///     let mut bitena = Bitena::new(1024)?;
    ///     let slice = bitena.try_alloc_slice(1u8, 100)?;
    ///     assert_eq!(bitena.remaining(), 924);
    ///     bitena.reset();
    ///     assert_eq!(bitena.remaining(), 1024);
    ///     Ok(())
    /// }
    /// ```
    pub fn reset(&mut self) {
        loop {
            let end_byte_idx = self.end_byte_idx.load(Ordering::Relaxed);

            if let Ok(_) = self.end_byte_idx.compare_exchange_weak(
                end_byte_idx,       // Expected value
                self.layout.size(), // New value
                Ordering::Relaxed,
                Ordering::Relaxed,
            ) {
                return ();
            }
        }
    }
}

impl Drop for Bitena<'_> {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            dealloc(self.buf.as_ptr(), self.layout);
        }
    }
}

unsafe impl Send for Bitena<'_> {}
unsafe impl Sync for Bitena<'_> {}

/// Returns IF value is comprised of all zeros.
#[inline]
fn is_all_zeros<T>(value: &T) -> bool {
    let num_bytes = std::mem::size_of::<T>();
    unsafe {
        let ptr = value as *const T as *const u8;
        for i in 0..num_bytes {
            if *ptr.add(i) != 0 {
                return false;
            }
        }
    }
    true
}

#[cfg(test)]
mod test {
    use super::*;
    use sysinfo::{Pid, System};

    #[test]
    fn test_try_alignment() -> Result<()> {
        let bitena = Bitena::new(1024)?;

        let _a = bitena.alloc_slice(0i8, 1); // 1-byte aligned
        let _a = bitena.alloc_slice(0i8, 1); // 1-byte aligned
        let a = bitena.alloc_slice(0i8, 1); // 1-byte aligned
        assert_eq!(a.as_ptr() as usize % 1, 0);

        let b = bitena.alloc(0u16); // 2-byte aligned
        assert_eq!(b as *const u16 as usize % 2, 0);

        let c = bitena.alloc_slice(0i32, 1); // 4-byte aligned
        assert_eq!(c.as_ptr() as usize % 4, 0);

        let d = bitena.alloc_slice(0i64, 1); // 8-byte aligned
        assert_eq!(d.as_ptr() as usize % 8, 0);

        // try_
        let a = bitena.try_alloc_slice(0i8, 1)?; // 1-byte aligned
        assert_eq!(a.as_ptr() as usize % 1, 0);

        let b = bitena.try_alloc(0u16)?; // 2-byte aligned
        assert_eq!(b as *const u16 as usize % 2, 0);

        let c = bitena.try_alloc_slice(0i32, 1)?; // 4-byte aligned
        assert_eq!(c.as_ptr() as usize % 4, 0);

        let d = bitena.try_alloc_slice(0i64, 1)?; // 8-byte aligned
        assert_eq!(d.as_ptr() as usize % 8, 0);
        Ok(())
    }

    #[test]
    fn test_try_bitena() -> Result<()> {
        let mut bitena = Bitena::new(1024)?;

        assert_eq!(bitena.remaining(), 1024, "Bitena should report 1024 bytes");

        let u8_ptr: &mut u8 = bitena.alloc(41u8);
        assert_eq!(bitena.remaining(), 1023, "Bitena should report 1023 bytes");

        let u32_ptr: &mut u32 = bitena.alloc(42u32);
        assert_eq!(
            u32_ptr as *mut u32 as usize % 4,
            0,
            "Pointer should be aligned"
        );

        *u32_ptr += *u8_ptr as u32;

        let u8_ptr: &mut [u8] = bitena.alloc_slice(43u8, 5);
        assert_eq!(u8_ptr, vec![43u8, 43, 43, 43, 43]);

        bitena.reset();

        assert_eq!(bitena.remaining(), 1024, "Bitena should report 1024 bytes");

        let _u8_ptr: &mut u8 = bitena.alloc(44u8);
        assert_eq!(bitena.remaining(), 1023, "Bitena should report 1023 bytes");

        let u64_ptr = bitena.alloc_slice(0u64, 4);
        assert_eq!(
            u64_ptr.as_ptr() as usize % 8,
            0,
            "u64 pointer should be 8-byte aligned"
        );

        let _u8_ptr: &mut u8 = bitena.alloc(46u8);

        let u128_ptr = bitena.alloc_slice(0u128, 5);
        assert_eq!(
            u128_ptr.as_ptr() as usize % 16,
            0,
            "u128 pointer should be 8-byte aligned"
        );

        // try_ testing:
        bitena.reset();
        let u8_ptr: &mut u8 = bitena.try_alloc(41u8)?;
        assert_eq!(bitena.remaining(), 1023, "Bitena should report 1023 bytes");

        let u32_ptr: &mut u32 = bitena.try_alloc(42u32)?;
        assert_eq!(
            u32_ptr as *mut u32 as usize % 4,
            0,
            "Pointer should be aligned"
        );

        *u32_ptr += *u8_ptr as u32;

        let u8_ptr: &mut [u8] = bitena.try_alloc_slice(43u8, 5)?;
        assert_eq!(u8_ptr, vec![43u8, 43, 43, 43, 43]);

        bitena.reset();

        assert_eq!(bitena.remaining(), 1024, "Bitena should report 1024 bytes");

        let _u8_ptr: &mut u8 = bitena.try_alloc(44u8)?;
        assert_eq!(bitena.remaining(), 1023, "Bitena should report 1023 bytes");

        let st = bitena.try_alloc_str("Test")?;
        assert_eq!(bitena.remaining(), 1019, "Bitena should report 1023 bytes");
        assert_eq!(st, "Test");

        let st = bitena.try_alloc_str("")?;
        assert_eq!(bitena.remaining(), 1019, "Bitena should report 1023 bytes");
        assert_eq!(st, "");

        let u64_ptr = bitena.try_alloc_slice(0u64, 4)?;
        assert_eq!(
            u64_ptr.as_ptr() as usize % 8,
            0,
            "u64 pointer should be 8-byte aligned"
        );

        let _u8_ptr: &mut u8 = bitena.try_alloc(46u8)?;

        let u128_ptr = bitena.try_alloc_slice(0u128, 5)?;
        assert_eq!(
            u128_ptr.as_ptr() as usize % 16,
            0,
            "u128 pointer should be 8-byte aligned"
        );

        Ok(())
    }

    #[test]
    #[should_panic(expected = "Layout Error: invalid parameters to Layout::from_size_align")]
    fn test_failed_to_allocate_panic() {
        let _bitena = Bitena::new(usize::MAX).unwrap_or_else(|e| panic!("Bitena Failed: {}", e));
    }

    #[test]
    fn test_try_failed_to_allocate() -> Result<()> {
        assert!(matches!(Bitena::new(usize::MAX), Err(Error::Layout(_))));
        Ok(())
    }

    #[test]
    #[should_panic(expected = "Bitena Failed: Out of Memory")]
    fn test_out_of_memory_panic() {
        let bitena = Bitena::new(1024).unwrap_or_else(|e| panic!("Should work Arena Failed: {}", e));
        let _large_slice: &mut [u64] = bitena.alloc_slice(0u64, 150);
    }

    #[test]
    fn test_try_out_of_memory() -> Result<()> {
        let bitena = Bitena::new(1024)?;
        // Check that alloc_slice returns Err(Error::OutOfMemory)
        assert!(matches!(
            bitena.try_alloc_slice(0u64, 150),
            Err(Error::OutOfMemory)
        ));
        Ok(())
    }

    fn format_number(n: u64) -> String {
        let s = n.to_string();
        let mut result = String::new();
        let mut count = 0;

        for c in s.chars().rev() {
            if count == 3 {
                result.push(',');
                count = 0;
            }
            result.push(c);
            count += 1;
        }

        result.chars().rev().collect()
    }

    fn get_system_available_memory() -> u64 {
        let sys = System::new_all();
        sys.available_memory()
    }

    fn get_process_memory_usage() -> u64 {
        let sys = System::new_all();
        let pid = Pid::from(std::process::id() as usize); // Convert to sysinfo's Pid type
        sys.process(pid)
            .map(|process: &sysinfo::Process| process.memory())
            .unwrap_or(0)
    }

    fn test_lg_alloc(size: usize) -> Result<()> {
        let bitena = Bitena::new(size)?;
        let j = bitena.try_alloc_slice(0u8, size)?;
        j.fill(15u8);
        Ok(())
    }


    // Note, Miri fails the sysconf(_SC_CLK_TCK) call.
    #[cfg_attr(miri, cfg(miri_skip))]
    #[test]
    fn test_try_large_allocation() -> Result<()> {
        const TARGET_SIZE: usize = 2 * 1024 * 1024 * 1024; // 2GB
        const NUM_ALLOCS: usize = 1000;
        const ALLOC_SIZE: usize = TARGET_SIZE / NUM_ALLOCS;

        //let bitena = Bitena::new(TARGET_SIZE);
        let start_sys = get_system_available_memory();
        let start_proc = get_process_memory_usage();

        println!(
            "Available memory: {} bytes",
            format_number(get_system_available_memory())
        );
        println!(
            "Process memory usage: {} bytes",
            format_number(get_process_memory_usage())
        );

        for _ in 0..NUM_ALLOCS {
            test_lg_alloc(ALLOC_SIZE).unwrap();
        }

        let used_sys = start_sys.saturating_sub(get_system_available_memory());
        let used_proc = get_process_memory_usage().saturating_sub(start_proc);

        println!(
            "Available memory: {} bytes, used: {}",
            format_number(get_system_available_memory()),
            format_number(used_sys)
        );
        println!(
            "Process memory usage: {} bytes, Additional proc mem used: {}",
            format_number(get_process_memory_usage()),
            format_number(used_proc)
        );

        assert!(used_sys < 50_000_000); // Arbitrary 50 MB Limit
        assert!(used_proc < 10_000_000); // Arbitrary 10 MB Limit
        Ok(())
    }
}