@@ -0,0 +1,94 @@ |
| 1 | +//! Memory management — allocate, deallocate, string operations. |
| 2 | +//! |
| 3 | +//! All heap allocation goes through these functions so we can |
| 4 | +//! track allocations, detect leaks, and implement Fortran's |
| 5 | +//! automatic deallocation semantics. |
| 6 | + |
| 7 | +use std::alloc::{self, Layout}; |
| 8 | +use std::ptr; |
| 9 | + |
| 10 | +/// Allocate `size` bytes on the heap. Returns a pointer. |
| 11 | +/// Aborts on allocation failure (Fortran ALLOCATE with no STAT=). |
| 12 | +#[no_mangle] |
| 13 | +pub extern "C" fn _afs_allocate(size: i64) -> *mut u8 { |
| 14 | + if size <= 0 { |
| 15 | + return ptr::null_mut(); |
| 16 | + } |
| 17 | + let layout = Layout::from_size_align(size as usize, 16) |
| 18 | + .expect("invalid allocation layout"); |
| 19 | + let ptr = unsafe { alloc::alloc(layout) }; |
| 20 | + if ptr.is_null() { |
| 21 | + eprintln!("ALLOCATE: out of memory ({} bytes)", size); |
| 22 | + std::process::exit(1); |
| 23 | + } |
| 24 | + ptr |
| 25 | +} |
| 26 | + |
| 27 | +/// Deallocate memory previously allocated by _afs_allocate. |
| 28 | +#[no_mangle] |
| 29 | +pub extern "C" fn _afs_deallocate(ptr: *mut u8) { |
| 30 | + if ptr.is_null() { |
| 31 | + return; |
| 32 | + } |
| 33 | + // We don't track the layout, so we use a minimum layout. |
| 34 | + // In a full implementation, the descriptor carries the size. |
| 35 | + // For now, this is a known simplification. |
| 36 | + let layout = Layout::from_size_align(1, 1).unwrap(); |
| 37 | + unsafe { alloc::dealloc(ptr, layout) }; |
| 38 | +} |
| 39 | + |
| 40 | +/// Concatenate two strings. Returns a newly allocated string. |
| 41 | +/// Caller is responsible for freeing the result. |
| 42 | +#[no_mangle] |
| 43 | +pub extern "C" fn _afs_string_concat( |
| 44 | + a: *const u8, alen: i64, |
| 45 | + b: *const u8, blen: i64, |
| 46 | +) -> *mut u8 { |
| 47 | + let total = (alen + blen) as usize; |
| 48 | + let result = _afs_allocate(total as i64); |
| 49 | + if !a.is_null() && alen > 0 { |
| 50 | + unsafe { ptr::copy_nonoverlapping(a, result, alen as usize) }; |
| 51 | + } |
| 52 | + if !b.is_null() && blen > 0 { |
| 53 | + unsafe { ptr::copy_nonoverlapping(b, result.add(alen as usize), blen as usize) }; |
| 54 | + } |
| 55 | + result |
| 56 | +} |
| 57 | + |
| 58 | +/// Copy a string into a fixed-length buffer, padding with spaces. |
| 59 | +/// Used for character assignment to fixed-length variables. |
| 60 | +#[no_mangle] |
| 61 | +pub extern "C" fn _afs_string_copy( |
| 62 | + dest: *mut u8, dest_len: i64, |
| 63 | + src: *const u8, src_len: i64, |
| 64 | +) { |
| 65 | + if dest.is_null() || dest_len <= 0 { |
| 66 | + return; |
| 67 | + } |
| 68 | + let copy_len = std::cmp::min(src_len, dest_len) as usize; |
| 69 | + if !src.is_null() && copy_len > 0 { |
| 70 | + unsafe { ptr::copy_nonoverlapping(src, dest, copy_len) }; |
| 71 | + } |
| 72 | + // Pad remainder with spaces (Fortran character assignment rule). |
| 73 | + if (copy_len as i64) < dest_len { |
| 74 | + unsafe { |
| 75 | + ptr::write_bytes(dest.add(copy_len), b' ', (dest_len as usize) - copy_len); |
| 76 | + } |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// Compare two strings lexicographically. |
| 81 | +/// Returns negative, zero, or positive (like strcmp but for counted strings). |
| 82 | +#[no_mangle] |
| 83 | +pub extern "C" fn _afs_string_compare( |
| 84 | + a: *const u8, alen: i64, |
| 85 | + b: *const u8, blen: i64, |
| 86 | +) -> i32 { |
| 87 | + let sa = if !a.is_null() && alen > 0 { |
| 88 | + unsafe { std::slice::from_raw_parts(a, alen as usize) } |
| 89 | + } else { &[] }; |
| 90 | + let sb = if !b.is_null() && blen > 0 { |
| 91 | + unsafe { std::slice::from_raw_parts(b, blen as usize) } |
| 92 | + } else { &[] }; |
| 93 | + sa.cmp(sb) as i32 |
| 94 | +} |