commonlibsse_ng\re\h/
hkMemoryAllocator.rs

1use core::ffi::c_void;
2
3use crate::re::hkBaseTypes::hkResult_CEnum;
4use crate::re::offsets_rtti::RTTI_hkMemoryAllocator;
5use crate::re::offsets_vtable::VTABLE_hkMemoryAllocator;
6use crate::rel::id::VariantID;
7
8#[repr(C)]
9pub struct hkMemoryAllocator {
10    pub vtbl: *const hkMemoryAllocatorVtbl,
11}
12
13const _: () = assert!(core::mem::size_of::<hkMemoryAllocator>() == 0x8);
14
15impl hkMemoryAllocator {
16    /// Address & offset of the runtime type information (RTTI) identifier.
17    pub const RTTI: VariantID = RTTI_hkMemoryAllocator;
18    /// Address & offset of the virtual function table.
19    pub const VTABLE: [VariantID; 1] = VTABLE_hkMemoryAllocator;
20}
21
22/// NOTE: Unlike the C++ implementation, `*mut u8` instead of `*mut c_void` for allocated memory blocks
23#[repr(C)]
24pub struct hkMemoryAllocatorVtbl {
25    pub CxxDrop: unsafe extern "C" fn(this: *mut hkMemoryAllocator), // 0x00
26
27    pub BlockAlloc: extern "C" fn(this: *mut hkMemoryAllocator, num_bytes: i32) -> *mut u8, // 0x01
28    pub BlockFree: extern "C" fn(this: *mut hkMemoryAllocator, ptr: *mut u8, num_bytes: i32), // 0x02
29    pub BufAlloc:
30        extern "C" fn(this: *mut hkMemoryAllocator, req_num_bytes_in_out: &mut i32) -> *mut u8, // 0x03
31    pub BufFree: extern "C" fn(this: *mut hkMemoryAllocator, ptr: *mut u8, num_bytes: i32), // 0x04
32    pub BufRealloc: extern "C" fn(
33        this: *mut hkMemoryAllocator,
34        old_ptr: *mut u8,
35        old_num_bytes: i32,
36        req_num_bytes_in_out: &mut i32,
37    ) -> *mut u8, // 0x05
38    pub BlockAllocBatch: extern "C" fn(
39        this: *mut hkMemoryAllocator,
40        ptrs_out: *mut *mut u8,
41        num_ptrs: i32,
42        block_size: i32,
43    ), // 0x06
44    pub BlockFreeBatch: extern "C" fn(
45        this: *mut hkMemoryAllocator,
46        ptrs_in: *mut *mut u8,
47        num_ptrs: i32,
48        block_size: i32,
49    ), // 0x07
50    pub GetMemoryStatistics:
51        extern "C" fn(this: *mut hkMemoryAllocator, usage: &mut MemoryStatistics), // 0x08
52    pub GetAllocatedSize:
53        extern "C" fn(this: *mut hkMemoryAllocator, obj: *const c_void, num_bytes: i32) -> i32, // 0x09
54    pub ResetPeakMemoryStatistics: extern "C" fn(this: *mut hkMemoryAllocator), // 0x0A
55}
56
57#[commonlibsse_ng_derive_internal::ffi_enum]
58#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
59#[repr(C)]
60pub enum MemoryState {
61    Ok = 0,
62    OutOfMemory = 1,
63}
64
65#[repr(C)]
66#[derive(Debug, Default)]
67pub struct MemoryStatistics {
68    pub allocated: i64,      // 0x00
69    pub inUse: i64,          // 0x08
70    pub peakInUse: i64,      // 0x10
71    pub available: i64,      // 0x18
72    pub totalAvailable: i64, // 0x20
73    pub largestBlock: i64,   // 0x28
74}
75const _: () = assert!(core::mem::size_of::<MemoryStatistics>() == 0x30);
76
77impl MemoryStatistics {
78    pub const INFINITE_SIZE: i64 = -1;
79}
80
81#[repr(C)]
82pub struct ExtendedInterface {
83    pub vtbl: *const ExtendedInterfaceVtbl,
84}
85const _: () = assert!(core::mem::size_of::<ExtendedInterface>() == 0x8);
86
87pub type MemoryWalkCallback =
88    unsafe extern "C" fn(start: *mut (), size: usize, allocated: bool, pool: i32, param: *mut ());
89
90#[repr(C)]
91pub struct ExtendedInterfaceVtbl {
92    pub CxxDrop: unsafe extern "C" fn(this: *mut ExtendedInterface), // 00
93    pub GarbageCollect: extern "C" fn(this: *mut ExtendedInterface), // 01
94    pub IncrementalGarbageCollect: extern "C" fn(this: *mut ExtendedInterface, num_blocks: i32), // 02
95    pub SetMemorySoftLimit: extern "C" fn(this: *mut ExtendedInterface, max_memory: usize) -> i32, // 03
96    pub GetMemorySoftLimit: extern "C" fn(this: *const ExtendedInterface) -> usize, // 04
97    pub CanAllocTotal: extern "C" fn(this: *mut ExtendedInterface, num_bytes: i32) -> bool, // 05
98    pub WalkMemory: extern "C" fn(
99        this: *mut ExtendedInterface,
100        callback: MemoryWalkCallback,
101        param: *mut c_void,
102    ) -> hkResult_CEnum,
103}