ort_openrouter_cli/common/
alloc.rs

1//! ort: Open Router CLI
2//! https://github.com/grahamking/ort
3//!
4//! MIT License
5//! Copyright (c) 2025 Graham King
6
7use crate::libc;
8use core::alloc::Layout;
9use core::ffi::c_void;
10
11#[cfg(feature = "print-allocations")]
12use crate::common::utils::to_ascii;
13
14pub struct LibcAlloc;
15
16// In case you were wondering, yes all three methods get used. Rust does
17// a bnuch of alloc_zeroed and realloc.
18//
19// Build with feature "print-allocations" to see memory being allocated:
20// `cd cli && cargo build --features="print-allocations"`
21// `./target/debug/ort list 2> allocs.txt`
22//
23// There's a Python script at the end of this file to summarize the output.
24//
25// Normal usage seems to peak under 64 Kib of active memory.
26// `ort list` is the exception, it has one large (~512 KiB) allocation which is a string
27// holding the full list output.
28unsafe impl core::alloc::GlobalAlloc for LibcAlloc {
29    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
30        #[cfg(feature = "print-allocations")]
31        {
32            let mut buf = [0u8; 16];
33            buf[0] = b'+';
34            let len = to_ascii(layout.size(), &mut buf[1..]);
35            unsafe { crate::libc::write(2, buf.as_ptr().cast(), len) };
36        }
37        unsafe { libc::malloc(layout.size().max(layout.align())) as *mut u8 }
38    }
39
40    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
41        #[cfg(feature = "print-allocations")]
42        {
43            let mut buf = [0u8; 16];
44            buf[0] = b'+';
45            let len = to_ascii(layout.size(), &mut buf[1..]);
46            unsafe { crate::libc::write(2, buf.as_ptr().cast(), len) };
47        }
48        unsafe { libc::calloc(1, layout.size().max(layout.align())) as *mut u8 }
49    }
50
51    #[allow(unused_variables)]
52    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
53        #[cfg(feature = "print-allocations")]
54        {
55            let mut buf = [0u8; 16];
56            buf[0] = b'-';
57            let len = to_ascii(layout.size(), &mut buf[1..]);
58            unsafe { crate::libc::write(2, buf.as_ptr().cast(), len) };
59        }
60        unsafe { libc::free(ptr as *mut c_void) }
61    }
62
63    #[allow(unused_variables)]
64    unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, new_size: usize) -> *mut u8 {
65        #[cfg(feature = "print-allocations")]
66        {
67            let mut buf = [0u8; 16];
68            buf[0] = b'-';
69            let len = to_ascii(layout.size(), &mut buf[1..]);
70            unsafe { crate::libc::write(2, buf.as_ptr().cast(), len) };
71
72            buf[0] = b'+';
73            let len = to_ascii(new_size, &mut buf[1..]);
74            unsafe { crate::libc::write(2, buf.as_ptr().cast(), len) };
75        }
76        unsafe { libc::realloc(ptr as *mut c_void, new_size) as *mut u8 }
77    }
78}
79
80/*
81"""Print running totals from allocs.txt and report the maximum cumulative value."""
82
83from pathlib import Path
84
85
86def main() -> None:
87    path = Path("allocs.txt")
88    if not path.is_file():
89        raise SystemExit("allocs.txt not found in the current directory")
90
91    total = 0
92    max_total = None  # Highest cumulative total
93    max_plus = None   # Largest individual + value
94    max_minus = None  # Largest (most negative) individual - value
95
96    with path.open() as fh:
97        for raw_line in fh:
98            line = raw_line.strip()
99            if not line:
100                continue  # Skip blank lines silently
101
102            try:
103                delta = int(line)
104            except ValueError as exc:
105                raise SystemExit(f"Invalid line in allocs.txt: {line!r}") from exc
106
107            if delta > 0:
108                max_plus = delta if max_plus is None else max(max_plus, delta)
109            elif delta < 0:
110                max_minus = delta if max_minus is None else min(max_minus, delta)
111
112            total += delta
113            max_total = total if max_total is None else max(max_total, total)
114            print(f"{line} {total}")
115
116    print()  # Blank line before the summary, matching the example
117    print(f"Max: {max_total if max_total is not None else 0}")
118    print(f"Largest +: {max_plus if max_plus is not None else 0}")
119    print(f"Largest -: {max_minus if max_minus is not None else 0}")
120
121
122if __name__ == "__main__":
123    main()
124*/