mod support; use std::alloc::{GlobalAlloc, Layout, System}; use std::cell::Cell; use support::harness::Harness; thread_local! { static ALLOCATED: Cell> = const { Cell::new(None) }; } struct ObservedSystem; fn counted(size: usize) { let _ = ALLOCATED.try_with(|total| { if let Some(before) = total.get() { total.set(Some(before.saturating_add(size))); } }); } // SAFETY: every allocation and deallocation delegates to System with the // caller's unchanged pointer and layout; observation only updates a TLS Cell. unsafe impl GlobalAlloc for ObservedSystem { unsafe fn alloc(&self, layout: Layout) -> *mut u8 { counted(layout.size()); // SAFETY: the GlobalAlloc caller supplies a valid allocation layout. unsafe { System.alloc(layout) } } unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { counted(layout.size()); // SAFETY: the GlobalAlloc caller supplies a valid allocation layout. unsafe { System.alloc_zeroed(layout) } } unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, size: usize) -> *mut u8 { counted(size); // SAFETY: System receives the original allocation and valid new size. unsafe { System.realloc(ptr, layout, size) } } unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { // SAFETY: System receives the pointer and layout provided by its caller. unsafe { System.dealloc(ptr, layout) } } } #[global_allocator] static ALLOCATOR: ObservedSystem = ObservedSystem; fn allocated(work: impl FnOnce() -> T) -> (T, usize) { ALLOCATED.with(|total| assert!(total.replace(Some(0)).is_none())); let answer = work(); let bytes = ALLOCATED.with(|total| total.replace(None).unwrap()); (answer, bytes) } /// RULE: a read-only Home borrows the resolved catalogue; inspecting names and views must not deep-copy the definition tree on every glance. #[test] fn home_borrows_the_resolved_definition_tree() { let seed = Harness::new(); let directory = tempfile::tempdir().unwrap(); let path = directory.path().join("catalogue.cotton"); seed.run("VACUUM INTO ?", &[rusqlite::types::Value::Text(path.to_string_lossy().into_owned())]); // The production cache belongs to a file; in-memory test databases deliberately // resolve afresh and cannot demonstrate borrowing an existing catalogue. let h = Harness::open_copy(&path.to_string_lossy()); cotton_tools::home_arrangement::home(&h.db, &h.workspace_id, None, true).unwrap(); let shared = cotton_tools::definitions::shared_table_definitions_from_driver(&h.db, &h.workspace_id).unwrap(); let (copied, copy_bytes) = allocated(|| shared.as_ref().clone()); drop(copied); // Tools are records: Home reads this database's tool rows, which is its // own work, not a copy of the table catalogue. let (tools, tool_bytes) = allocated(|| cotton_tools::definitions::tool_definitions_from_driver(&h.db, &h.workspace_id).unwrap()); drop(tools); let (home, read_bytes) = allocated(|| { cotton_tools::home_arrangement::home(&h.db, &h.workspace_id, None, true).unwrap() }); assert!(!home.views.is_empty(), "the read still assembles Home"); println!("Home allocated {read_bytes} bytes; one catalogue copy allocates {copy_bytes}; its tool rows {tool_bytes}"); assert!(read_bytes < copy_bytes + tool_bytes, "read-only Home copied its catalogue: {read_bytes} bytes versus {copy_bytes} for a full copy and {tool_bytes} for its tool rows"); }