oxigdal-embedded 0.1.4

Embedded systems support for OxiGDAL - no_std compatible geospatial processing for ARM, RISC-V, and ESP32
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
//! Static memory pool implementations for no_std environments
//!
//! Provides memory pool abstractions that allow predictable allocation behavior
//! in embedded systems without heap allocation.

use core::cell::UnsafeCell;
use core::ptr::NonNull;
use core::sync::atomic::{AtomicBool, AtomicUsize, Ordering};

use crate::error::{EmbeddedError, Result};

/// Memory pool trait for unified pool interface
pub trait MemoryPool {
    /// Allocate memory from the pool
    ///
    /// # Errors
    ///
    /// Returns `PoolExhausted` if no memory is available
    /// Returns `InvalidAlignment` if alignment requirements cannot be met
    fn allocate(&self, size: usize, align: usize) -> Result<NonNull<u8>>;

    /// Deallocate memory back to the pool
    ///
    /// # Safety
    ///
    /// The pointer must have been allocated from this pool
    unsafe fn deallocate(&self, ptr: NonNull<u8>, size: usize, align: usize) -> Result<()>;

    /// Get the total capacity of the pool
    fn capacity(&self) -> usize;

    /// Get the currently used bytes
    fn used(&self) -> usize;

    /// Get the available bytes
    fn available(&self) -> usize {
        self.capacity().saturating_sub(self.used())
    }

    /// Reset the pool (deallocate all)
    ///
    /// # Safety
    ///
    /// All pointers allocated from this pool must not be used after reset
    unsafe fn reset(&self) -> Result<()>;
}

/// Static memory pool with compile-time size
///
/// Uses a simple bump allocator strategy suitable for embedded systems
pub struct StaticPool<const N: usize> {
    buffer: UnsafeCell<[u8; N]>,
    offset: AtomicUsize,
    locked: AtomicBool,
}

impl<const N: usize> StaticPool<N> {
    /// Create a new static pool
    pub const fn new() -> Self {
        Self {
            buffer: UnsafeCell::new([0u8; N]),
            offset: AtomicUsize::new(0),
            locked: AtomicBool::new(false),
        }
    }

    /// Get the base address of the pool
    fn base_addr(&self) -> usize {
        self.buffer.get() as usize
    }

    /// Try to acquire lock for allocation
    fn try_lock(&self) -> Result<()> {
        match self
            .locked
            .compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
        {
            Ok(_) => Ok(()),
            Err(_) => Err(EmbeddedError::ResourceBusy),
        }
    }

    /// Release the lock
    fn unlock(&self) {
        self.locked.store(false, Ordering::Release);
    }
}

impl<const N: usize> Default for StaticPool<N> {
    fn default() -> Self {
        Self::new()
    }
}

impl<const N: usize> MemoryPool for StaticPool<N> {
    fn allocate(&self, size: usize, align: usize) -> Result<NonNull<u8>> {
        if size == 0 {
            return Err(EmbeddedError::InvalidParameter);
        }

        if !align.is_power_of_two() {
            return Err(EmbeddedError::InvalidAlignment {
                required: align,
                actual: 0,
            });
        }

        self.try_lock()?;

        let current_offset = self.offset.load(Ordering::Relaxed);
        let base = self.base_addr();

        // Calculate aligned offset considering the base address
        // We need (base + aligned_offset) % align == 0
        // So aligned_offset = align_up(base + current_offset, align) - base
        let current_addr = base.wrapping_add(current_offset);
        let aligned_addr =
            (current_addr.wrapping_add(align.wrapping_sub(1))) & !align.wrapping_sub(1);
        let aligned_offset = aligned_addr.wrapping_sub(base);

        let new_offset = match aligned_offset.checked_add(size) {
            Some(offset) if offset <= N => offset,
            _ => {
                self.unlock();
                return Err(EmbeddedError::PoolExhausted);
            }
        };

        self.offset.store(new_offset, Ordering::Release);
        self.unlock();

        // SAFETY: We've verified the pointer is within our buffer and properly aligned
        let ptr = unsafe { NonNull::new_unchecked(aligned_addr as *mut u8) };
        Ok(ptr)
    }

    unsafe fn deallocate(&self, _ptr: NonNull<u8>, _size: usize, _align: usize) -> Result<()> {
        // Bump allocator doesn't support individual deallocation
        // Use reset() to reclaim all memory
        Ok(())
    }

    fn capacity(&self) -> usize {
        N
    }

    fn used(&self) -> usize {
        self.offset.load(Ordering::Relaxed)
    }

    unsafe fn reset(&self) -> Result<()> {
        self.try_lock()?;
        self.offset.store(0, Ordering::Release);
        self.unlock();
        Ok(())
    }
}

// SAFETY: StaticPool uses atomic operations for thread-safe access
unsafe impl<const N: usize> Sync for StaticPool<N> {}
unsafe impl<const N: usize> Send for StaticPool<N> {}

/// Block-based memory pool with fixed-size blocks
///
/// More efficient for frequent allocations/deallocations of similar sizes
pub struct BlockPool<const BLOCK_SIZE: usize, const NUM_BLOCKS: usize, const BITMAP_SIZE: usize> {
    blocks: UnsafeCell<[[u8; BLOCK_SIZE]; NUM_BLOCKS]>,
    free_bitmap: UnsafeCell<[u8; BITMAP_SIZE]>,
    free_count: AtomicUsize,
    locked: AtomicBool,
}

impl<const BLOCK_SIZE: usize, const NUM_BLOCKS: usize, const BITMAP_SIZE: usize>
    BlockPool<BLOCK_SIZE, NUM_BLOCKS, BITMAP_SIZE>
{
    /// Create a new block pool
    ///
    /// # Panics
    ///
    /// Panics if BITMAP_SIZE is not sufficient for NUM_BLOCKS
    pub const fn new() -> Self {
        // We need at least (NUM_BLOCKS + 7) / 8 bytes for the bitmap
        // This check will be evaluated at compile time
        assert!(
            BITMAP_SIZE * 8 >= NUM_BLOCKS,
            "BITMAP_SIZE is too small for NUM_BLOCKS"
        );

        Self {
            blocks: UnsafeCell::new([[0u8; BLOCK_SIZE]; NUM_BLOCKS]),
            free_bitmap: UnsafeCell::new([0xFF; BITMAP_SIZE]),
            free_count: AtomicUsize::new(NUM_BLOCKS),
            locked: AtomicBool::new(false),
        }
    }

    /// Try to acquire lock
    fn try_lock(&self) -> Result<()> {
        match self
            .locked
            .compare_exchange(false, true, Ordering::Acquire, Ordering::Relaxed)
        {
            Ok(_) => Ok(()),
            Err(_) => Err(EmbeddedError::ResourceBusy),
        }
    }

    /// Release the lock
    fn unlock(&self) {
        self.locked.store(false, Ordering::Release);
    }

    /// Find and allocate a free block
    fn find_free_block(&self) -> Option<usize> {
        // SAFETY: We hold the lock
        let bitmap = unsafe { &mut *self.free_bitmap.get() };

        for (byte_idx, byte) in bitmap.iter_mut().enumerate() {
            if *byte == 0 {
                continue;
            }

            // Find first set bit
            for bit_idx in 0..8 {
                let block_idx = byte_idx * 8 + bit_idx;
                if block_idx >= NUM_BLOCKS {
                    return None;
                }

                if (*byte >> bit_idx) & 1 != 0 {
                    // Mark as allocated
                    *byte &= !(1 << bit_idx);
                    return Some(block_idx);
                }
            }
        }

        None
    }

    /// Mark a block as free
    ///
    /// # Safety
    ///
    /// block_idx must be valid and currently allocated
    unsafe fn free_block(&self, block_idx: usize) {
        // SAFETY: Caller guarantees block_idx is valid and we hold the lock
        let bitmap = unsafe { &mut *self.free_bitmap.get() };
        let byte_idx = block_idx / 8;
        let bit_idx = block_idx % 8;

        bitmap[byte_idx] |= 1 << bit_idx;
    }
}

impl<const BLOCK_SIZE: usize, const NUM_BLOCKS: usize, const BITMAP_SIZE: usize> Default
    for BlockPool<BLOCK_SIZE, NUM_BLOCKS, BITMAP_SIZE>
{
    fn default() -> Self {
        Self::new()
    }
}

impl<const BLOCK_SIZE: usize, const NUM_BLOCKS: usize, const BITMAP_SIZE: usize> MemoryPool
    for BlockPool<BLOCK_SIZE, NUM_BLOCKS, BITMAP_SIZE>
{
    fn allocate(&self, size: usize, align: usize) -> Result<NonNull<u8>> {
        if size > BLOCK_SIZE {
            return Err(EmbeddedError::BufferTooSmall {
                required: size,
                available: BLOCK_SIZE,
            });
        }

        if !align.is_power_of_two() {
            return Err(EmbeddedError::InvalidAlignment {
                required: align,
                actual: 0,
            });
        }

        self.try_lock()?;

        let block_idx = match self.find_free_block() {
            Some(idx) => idx,
            None => {
                self.unlock();
                return Err(EmbeddedError::PoolExhausted);
            }
        };

        self.free_count.fetch_sub(1, Ordering::Relaxed);

        // SAFETY: We hold the lock and block_idx is valid
        let blocks = unsafe { &mut *self.blocks.get() };
        let ptr = blocks[block_idx].as_mut_ptr();

        // Verify alignment
        let ptr_addr = ptr as usize;
        if ptr_addr % align != 0 {
            // SAFETY: We just allocated this block
            unsafe { self.free_block(block_idx) };
            self.free_count.fetch_add(1, Ordering::Relaxed);
            self.unlock();
            return Err(EmbeddedError::InvalidAlignment {
                required: align,
                actual: ptr_addr % align.max(1),
            });
        }

        self.unlock();

        // SAFETY: ptr is non-null and valid
        let nonnull = unsafe { NonNull::new_unchecked(ptr) };
        Ok(nonnull)
    }

    unsafe fn deallocate(&self, ptr: NonNull<u8>, _size: usize, _align: usize) -> Result<()> {
        self.try_lock()?;

        // Calculate block index from pointer
        // SAFETY: We hold the lock and the blocks array is valid
        let blocks = unsafe { &*self.blocks.get() };
        let base_addr = blocks.as_ptr() as usize;
        let ptr_addr = ptr.as_ptr() as usize;

        if ptr_addr < base_addr {
            self.unlock();
            return Err(EmbeddedError::InvalidParameter);
        }

        let offset = ptr_addr.wrapping_sub(base_addr);
        let block_idx = offset / BLOCK_SIZE;

        if block_idx >= NUM_BLOCKS {
            self.unlock();
            return Err(EmbeddedError::InvalidParameter);
        }

        // SAFETY: We've verified block_idx is valid and ptr was allocated from this pool
        unsafe { self.free_block(block_idx) };
        self.free_count.fetch_add(1, Ordering::Relaxed);

        self.unlock();
        Ok(())
    }

    fn capacity(&self) -> usize {
        BLOCK_SIZE * NUM_BLOCKS
    }

    fn used(&self) -> usize {
        let free = self.free_count.load(Ordering::Relaxed);
        (NUM_BLOCKS.saturating_sub(free)) * BLOCK_SIZE
    }

    unsafe fn reset(&self) -> Result<()> {
        self.try_lock()?;

        // SAFETY: We hold the lock and the bitmap is valid
        let bitmap = unsafe { &mut *self.free_bitmap.get() };
        bitmap.fill(0xFF);

        self.free_count.store(NUM_BLOCKS, Ordering::Release);
        self.unlock();
        Ok(())
    }
}

// SAFETY: BlockPool uses atomic operations and locking for thread-safe access
unsafe impl<const BLOCK_SIZE: usize, const NUM_BLOCKS: usize, const BITMAP_SIZE: usize> Sync
    for BlockPool<BLOCK_SIZE, NUM_BLOCKS, BITMAP_SIZE>
{
}
unsafe impl<const BLOCK_SIZE: usize, const NUM_BLOCKS: usize, const BITMAP_SIZE: usize> Send
    for BlockPool<BLOCK_SIZE, NUM_BLOCKS, BITMAP_SIZE>
{
}

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

    #[test]
    fn test_static_pool_allocation() {
        let pool = StaticPool::<1024>::new();

        let ptr1 = pool.allocate(64, 8).expect("allocation failed");
        assert_eq!(pool.used(), 64);

        let ptr2 = pool.allocate(128, 16).expect("allocation failed");
        assert!(pool.used() >= 64 + 128);

        assert_ne!(ptr1, ptr2);
    }

    #[test]
    fn test_static_pool_exhaustion() {
        let pool = StaticPool::<128>::new();

        let _ptr1 = pool.allocate(64, 8).expect("allocation failed");
        let _ptr2 = pool.allocate(64, 8).expect("allocation failed");

        // Pool should be exhausted now
        let result = pool.allocate(64, 8);
        assert!(matches!(result, Err(EmbeddedError::PoolExhausted)));
    }

    #[test]
    fn test_static_pool_reset() {
        let pool = StaticPool::<1024>::new();

        let _ptr = pool.allocate(512, 8).expect("allocation failed");
        assert!(pool.used() > 0);

        // SAFETY: We won't use the pointer after reset
        unsafe { pool.reset().expect("reset failed") };
        assert_eq!(pool.used(), 0);
    }

    #[test]
    fn test_block_pool_allocation() {
        // BITMAP_SIZE = (NUM_BLOCKS + 7) / 8 = (16 + 7) / 8 = 2
        let pool = BlockPool::<64, 16, 2>::new();

        let ptr1 = pool.allocate(32, 4).expect("allocation failed");
        let ptr2 = pool.allocate(32, 4).expect("allocation failed");

        assert_ne!(ptr1, ptr2);
        assert_eq!(pool.used(), 128); // 2 blocks * 64 bytes
    }

    #[test]
    fn test_block_pool_deallocation() {
        // BITMAP_SIZE = (NUM_BLOCKS + 7) / 8 = (16 + 7) / 8 = 2
        let pool = BlockPool::<64, 16, 2>::new();

        let ptr = pool.allocate(32, 4).expect("allocation failed");
        assert_eq!(pool.used(), 64);

        // SAFETY: ptr was allocated from this pool
        unsafe { pool.deallocate(ptr, 32, 4).expect("deallocation failed") };
        assert_eq!(pool.used(), 0);
    }

    #[test]
    fn test_block_pool_exhaustion() {
        // BITMAP_SIZE = (NUM_BLOCKS + 7) / 8 = (4 + 7) / 8 = 1
        let pool = BlockPool::<64, 4, 1>::new();

        for _ in 0..4 {
            pool.allocate(32, 4).expect("allocation failed");
        }

        let result = pool.allocate(32, 4);
        assert!(matches!(result, Err(EmbeddedError::PoolExhausted)));
    }
}