ocas-core 0.14.0

Core runtime, memory management, and numerical backend glue for oCAS
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
//! Arena allocator for expression nodes.
//!
//! oCAS uses a bump allocator to store expression sub-nodes. This avoids the
//! per-node allocation overhead of `Box` or `Rc` and improves cache locality.
//! When the arena is dropped, the entire tree is freed at once.
//!
//! # Current limitations
//!
//! The 0.1.0 `Arena` does **not** run destructors for allocated values. It is
//! therefore only safe to store `Copy` types or types that do not own resources
//! requiring explicit cleanup. This restriction will be lifted once expression
//! trees need to store owned strings or other `Drop` types.

use std::alloc::{self, Layout};
use std::cell::RefCell;
use std::marker::PhantomData;
use std::mem;
use std::ptr::NonNull;

/// Default block size for new arena chunks.
const DEFAULT_BLOCK_SIZE: usize = 64 * 1024;

/// A bump-allocated region of memory.
///
/// Values allocated in an `Arena` are tied to its lifetime and must not
/// outlive it. The public API enforces this with borrow checker lifetimes.
///
/// # Type safety note
///
/// `Arena` does not run destructors. Only store `Copy` or otherwise
/// non-owning values until drop support is added.
///
/// # Example
///
/// ```
/// use ocas_core::arena::Arena;
///
/// let arena = Arena::new();
/// let value = arena.allocate_with(|| 42);
/// assert_eq!(*value, 42);
/// ```
pub struct Arena {
    chunks: RefCell<Vec<Chunk>>,
    block_size: usize,
}

impl Arena {
    /// Create a new arena with the default block size.
    ///
    /// # Example
    ///
    /// ```
    /// use ocas_core::arena::Arena;
    ///
    /// let arena = Arena::new();
    /// let n = arena.allocate_with(|| 7);
    /// assert_eq!(*n, 7);
    /// ```
    pub fn new() -> Self {
        Self {
            chunks: RefCell::new(Vec::new()),
            block_size: DEFAULT_BLOCK_SIZE,
        }
    }

    /// Create a new arena with a custom initial block size.
    pub fn with_capacity(block_size: usize) -> Self {
        Self {
            chunks: RefCell::new(Vec::new()),
            block_size,
        }
    }

    /// Allocate a value in the arena, constructing it inside `init`, and return
    /// a mutable reference tied to `self`.
    ///
    /// The closure form avoids any ambiguity about when mutation of the arena
    /// occurs. The returned `&mut T` is unique because `alloc_raw` advances the
    /// arena offset for each allocation via interior mutability.
    ///
    /// # Panics
    ///
    /// Panics if the requested layout has size zero.
    ///
    /// # Example
    ///
    /// ```
    /// use ocas_core::arena::Arena;
    ///
    /// let arena = Arena::new();
    /// let value = arena.allocate_with(|| "hello");
    /// assert_eq!(*value, "hello");
    /// ```
    #[allow(clippy::mut_from_ref)]
    pub fn allocate_with<T>(&self, init: impl FnOnce() -> T) -> &mut T {
        let layout = Layout::new::<T>();
        assert!(
            layout.size() > 0,
            "cannot allocate zero-sized types in Arena"
        );
        let ptr = self.alloc_raw(layout);

        // SAFETY: `ptr` is non-null and properly aligned for `T`.
        unsafe {
            let typed = ptr.as_ptr().cast::<T>();
            typed.write(init());
            &mut *typed
        }
    }

    /// Allocate a contiguous slice of `T` values in the arena.
    ///
    /// The returned slice is tied to the arena lifetime. Because the arena does
    /// not run destructors, `T` must be `Copy` so that dropping the arena does
    /// not leak resources owned by the slice elements.
    ///
    /// # Panics
    ///
    /// Panics if `T` has zero size or if the total allocation size overflows.
    ///
    /// # Example
    ///
    /// ```
    /// use ocas_core::arena::Arena;
    ///
    /// let arena = Arena::new();
    /// let slice = arena.allocate_slice(&[1, 2, 3, 4, 5]);
    /// assert_eq!(slice, &[1, 2, 3, 4, 5]);
    /// ```
    pub fn allocate_slice<T: Copy>(&self, values: &[T]) -> &[T] {
        if values.is_empty() {
            return &[];
        }

        let layout = Layout::from_size_align(mem::size_of_val(values), mem::align_of::<T>())
            .expect("invalid slice layout");
        assert!(
            layout.size() > 0,
            "cannot allocate zero-sized types in Arena"
        );

        let ptr = self.alloc_raw(layout);

        // SAFETY: `ptr` is non-null, properly aligned, and points to a block
        // large enough to hold `values.len()` elements of type `T`.
        unsafe {
            let typed = ptr.as_ptr().cast::<T>();
            std::ptr::copy_nonoverlapping(values.as_ptr(), typed, values.len());
            std::slice::from_raw_parts(typed, values.len())
        }
    }

    fn alloc_raw(&self, layout: Layout) -> NonNull<u8> {
        let mut chunks = self.chunks.borrow_mut();

        // Try to allocate from the current chunk.
        if let Some(chunk) = chunks.last_mut()
            && let Some(ptr) = chunk.try_alloc(layout)
        {
            return ptr;
        }

        // Need a new chunk. Use at least the requested size and alignment so the
        // first allocation in the chunk is correctly aligned.
        let size = layout.size().max(self.block_size);
        let align = layout.align();
        let mut new_chunk = Chunk::new(size, align);
        let ptr = new_chunk
            .try_alloc(layout)
            .expect("new chunk should fit any layout up to its size");
        chunks.push(new_chunk);
        ptr
    }
}

impl Default for Arena {
    fn default() -> Self {
        Self::new()
    }
}

impl Drop for Arena {
    fn drop(&mut self) {
        // 0.1.0: destructors for allocated values are intentionally not called.
        // Only `Copy`/non-owning values may be stored.
    }
}

struct Chunk {
    memory: NonNull<u8>,
    size: usize,
    align: usize,
    offset: usize,
}

impl Chunk {
    fn new(size: usize, align: usize) -> Self {
        let layout = Layout::from_size_align(size, align).expect("invalid chunk layout");
        // SAFETY: layout is non-zero and properly aligned.
        let memory = unsafe { NonNull::new_unchecked(alloc::alloc(layout)) };
        Self {
            memory,
            size,
            align,
            offset: 0,
        }
    }

    fn try_alloc(&mut self, layout: Layout) -> Option<NonNull<u8>> {
        let aligned_offset = align_up(self.offset, layout.align());
        let end = aligned_offset.checked_add(layout.size())?;
        if end > self.size {
            return None;
        }

        // SAFETY: offset is within the allocated block and aligned.
        let ptr = unsafe { NonNull::new_unchecked(self.memory.as_ptr().add(aligned_offset)) };
        self.offset = end;
        Some(ptr)
    }
}

impl Drop for Chunk {
    fn drop(&mut self) {
        // Deallocation requires the same size and an alignment that is at least
        // as large as the original allocation. The chunk was allocated with the
        // maximum alignment requested by any layout served from this chunk, so
        // that alignment is stored alongside the chunk.
        let layout = Layout::from_size_align(self.size, self.align).expect("invalid chunk layout");
        // SAFETY: `memory` was allocated with this layout.
        unsafe {
            alloc::dealloc(self.memory.as_ptr(), layout);
        }
    }
}

fn align_up(offset: usize, align: usize) -> usize {
    assert!(align.is_power_of_two(), "alignment must be a power of two");
    (offset + align - 1) & !(align - 1)
}

/// An owned expression that keeps its arena alive.
pub struct OwnedExpr<T> {
    #[allow(dead_code)]
    arena: Box<Arena>,
    root: *mut T,
    _marker: PhantomData<T>,
}

impl<T> OwnedExpr<T> {
    /// Create an owned expression from an arena and a root pointer.
    ///
    /// # Safety
    ///
    /// `root` must point to a value allocated in `arena` and must be valid
    /// for the lifetime of `arena`.
    pub unsafe fn new(arena: Box<Arena>, root: *mut T) -> Self {
        Self {
            arena,
            root,
            _marker: PhantomData,
        }
    }

    /// Access the root expression.
    pub fn root(&self) -> &T {
        // SAFETY: root is valid as long as arena is alive.
        unsafe { &*self.root }
    }
}

unsafe impl<T: Send> Send for OwnedExpr<T> {}
unsafe impl<T: Sync> Sync for OwnedExpr<T> {}

#[cfg(test)]
mod tests {
    use super::*;
    use proptest::prelude::*;

    mod simple {
        use super::*;

        #[test]
        fn allocate_single_integer() {
            let arena = Arena::new();
            let value = arena.allocate_with(|| 42);
            assert_eq!(*value, 42);
        }

        #[test]
        fn allocate_two_integers() {
            let arena = Arena::new();
            let a = arena.allocate_with(|| 1);
            let b = arena.allocate_with(|| 2);
            assert_eq!(*a, 1);
            assert_eq!(*b, 2);
        }

        #[test]
        fn allocate_empty_slice() {
            let arena = Arena::new();
            let slice: &[i32] = arena.allocate_slice(&[]);
            assert!(slice.is_empty());
        }

        #[test]
        fn allocate_small_slice() {
            let arena = Arena::new();
            let data = [10, 20, 30];
            let slice = arena.allocate_slice(&data);
            assert_eq!(slice, &data[..]);
        }

        #[test]
        fn arena_default_matches_new() {
            let arena: Arena = Default::default();
            let value = arena.allocate_with(|| "x");
            assert_eq!(*value, "x");
        }
    }

    mod medium {
        use super::*;

        #[test]
        fn allocate_larger_than_block() {
            let arena = Arena::with_capacity(16);
            let data = [0u8; 128];
            let ptr = arena.allocate_with(|| data);
            assert_eq!(*ptr, data);
        }

        #[test]
        fn allocate_slice_larger_than_block() {
            let arena = Arena::with_capacity(16);
            let values: Vec<u8> = (0..=255).collect();
            let slice = arena.allocate_slice(&values);
            assert_eq!(slice, &values[..]);
        }

        #[test]
        fn multiple_chunks_for_many_values() {
            let arena = Arena::with_capacity(32);
            let mut sum = 0i64;
            for i in 0..100 {
                let value = arena.allocate_with(|| i);
                sum += *value;
            }
            assert_eq!(sum, 4950);
        }

        #[test]
        fn multiple_chunks_for_many_slices() {
            let arena = Arena::with_capacity(64);
            let mut total = 0i64;
            for i in 0..50 {
                let values: Vec<i64> = (0..10).map(|j| i * 10 + j).collect();
                let slice = arena.allocate_slice(&values);
                total += slice.iter().sum::<i64>();
            }
            assert_eq!(total, 124_750);
        }

        #[test]
        fn owned_expr_keeps_arena_alive() {
            let arena = Box::new(Arena::new());
            let root = arena.allocate_with(|| 123);
            let root_ptr: *mut i32 = root;
            // SAFETY: root was allocated in arena, and arena outlives OwnedExpr.
            let owned = unsafe { OwnedExpr::new(arena, root_ptr) };
            assert_eq!(*owned.root(), 123);
        }
    }

    mod complex {
        use super::*;

        #[test]
        fn copy_values_survive_arena_drop() {
            let value = {
                let arena = Arena::new();
                let ptr = arena.allocate_with(|| 42i32);
                *ptr
            };
            assert_eq!(value, 42);
        }

        #[test]
        fn alignment_of_large_type() {
            #[derive(Clone, Copy)]
            #[repr(C, align(64))]
            struct BigAlign(u64);

            // The first allocation in a fresh chunk must respect the requested
            // alignment. Use a single-element slice so the layout alignment is
            // dominated by BigAlign.
            let arena = Arena::with_capacity(4096);
            let values = [BigAlign(7)];
            let slice = arena.allocate_slice(&values);
            assert!((slice.as_ptr() as usize).is_multiple_of(64));
            assert_eq!(slice[0].0, 7);
        }

        #[test]
        fn alignment_of_single_value() {
            #[derive(Clone, Copy)]
            #[repr(C, align(64))]
            struct BigAlign(u64);

            let arena = Arena::with_capacity(4096);
            let value = arena.allocate_with(|| BigAlign(7));
            assert_eq!((value as *const BigAlign) as usize % 64, 0);
            assert_eq!(value.0, 7);
        }

        #[test]
        #[should_panic(expected = "cannot allocate zero-sized types in Arena")]
        fn zero_sized_type_panics() {
            let arena = Arena::new();
            let _: &mut () = arena.allocate_with(|| ());
        }

        #[test]
        #[should_panic(expected = "cannot allocate zero-sized types in Arena")]
        fn zero_sized_slice_panics() {
            let arena = Arena::new();
            let _: &[()] = arena.allocate_slice(&[()]);
        }

        #[test]
        fn owned_expr_is_send_sync() {
            fn assert_send_sync<T: Send + Sync>() {}
            assert_send_sync::<OwnedExpr<u8>>();
        }
    }

    mod extreme {
        use super::*;

        #[test]
        fn stress_mixed_allocations() {
            let arena = Arena::with_capacity(256);
            let mut total = 0usize;
            for size in (1usize..=1000).step_by(7) {
                let data: Vec<u8> = (0..size).map(|i| (i % 256) as u8).collect();
                let ptr = arena.allocate_with(|| data.clone());
                total += ptr.iter().map(|&x| x as usize).sum::<usize>();
            }
            assert!(total > 0);
        }

        proptest! {
            #[test]
            fn allocate_random_sizes(size in 1usize..10_000) {
                let arena = Arena::with_capacity(256);
                let data: Vec<u8> = (0..size).map(|i| (i % 256) as u8).collect();
                let ptr = arena.allocate_with(|| data.clone());
                prop_assert_eq!(&ptr[..], &data[..]);
            }

            #[test]
            fn allocate_many_random_values(sizes in prop::collection::vec(1usize..512, 1..50)) {
                let arena = Arena::with_capacity(256);
                let mut total = 0usize;
                for (idx, size) in sizes.iter().enumerate() {
                    let expected: Vec<u8> = (0..*size).map(|i| (i.wrapping_add(idx)) as u8).collect();
                    let ptr = arena.allocate_with(|| expected.clone());
                    prop_assert_eq!(&ptr[..], &expected[..]);
                    total += size;
                }
                prop_assert!(total > 0);
            }

            #[test]
            fn slice_roundtrip(values in prop::collection::vec(0i32..100, 0..512)) {
                let arena = Arena::with_capacity(256);
                let slice = arena.allocate_slice(&values);
                prop_assert_eq!(slice, &values[..]);
            }
        }
    }
}