feat(wallet_ffi): freeing of ffi programs

This commit is contained in:
Pravdyvy 2026-06-04 21:15:17 +03:00
parent 7ccd8e2435
commit 76696a5d08
2 changed files with 39 additions and 8 deletions

View File

@ -101,7 +101,7 @@ pub unsafe extern "C" fn wallet_ffi_program_deployment(
/// - Error code on other failures
///
/// # Memory
/// - freeing this data is a responsibility of a user
/// - `FfiProgram` can be freed with corresponding `wallet_ffi_free_ffi_program` function
///
/// # Safety
/// - `ffi_program` must be a non-null pointer
@ -136,7 +136,7 @@ pub unsafe extern "C" fn wallet_ffi_transfer_elf(ffi_program: *mut FfiProgram) -
/// - Error code on other failures
///
/// # Memory
/// - freeing this data is a responsibility of a user
/// - `FfiProgram` can be freed with corresponding `wallet_ffi_free_ffi_program` function
///
/// # Safety
/// - `ffi_program` must be a non-null pointer
@ -171,7 +171,7 @@ pub unsafe extern "C" fn wallet_ffi_token_elf(ffi_program: *mut FfiProgram) -> W
/// - Error code on other failures
///
/// # Memory
/// - freeing this data is a responsibility of a user
/// - `FfiProgram` can be freed with corresponding `wallet_ffi_free_ffi_program` function
///
/// # Safety
/// - `ffi_program` must be a non-null pointer
@ -206,7 +206,7 @@ pub unsafe extern "C" fn wallet_ffi_amm_elf(ffi_program: *mut FfiProgram) -> Wal
/// - Error code on other failures
///
/// # Memory
/// - freeing this data is a responsibility of a user
/// - `FfiProgram` can be freed with corresponding `wallet_ffi_free_ffi_program` function
///
/// # Safety
/// - `ffi_program` must be a non-null pointer
@ -228,3 +228,26 @@ pub unsafe extern "C" fn wallet_ffi_ata_elf(ffi_program: *mut FfiProgram) -> Wal
WalletFfiError::Success
}
/// Free a ffi program returned by functions `wallet_ffi_*_elf`.
///
/// # Safety
/// The result must be either null or a valid result from a elf getter function.
#[no_mangle]
pub unsafe extern "C" fn wallet_ffi_free_ffi_program(ffi_program: *mut FfiProgram) {
if ffi_program.is_null() {
return;
}
unsafe {
let ffi_program = &*ffi_program;
if !ffi_program.elf_data.is_null() {
let elf = std::slice::from_raw_parts_mut(
ffi_program.elf_data.cast_mut(),
ffi_program.elf_size,
);
drop(Box::from_raw(std::ptr::from_mut::<[u8]>(elf)));
}
}
}

View File

@ -909,7 +909,7 @@ enum WalletFfiError wallet_ffi_program_deployment(struct WalletHandle *handle,
* - Error code on other failures
*
* # Memory
* - freeing this data is a responsibility of a user
* - `FfiProgram` can be freed with corresponding `wallet_ffi_free_ffi_program` function
*
* # Safety
* - `ffi_program` must be a non-null pointer
@ -929,7 +929,7 @@ enum WalletFfiError wallet_ffi_transfer_elf(struct FfiProgram *ffi_program);
* - Error code on other failures
*
* # Memory
* - freeing this data is a responsibility of a user
* - `FfiProgram` can be freed with corresponding `wallet_ffi_free_ffi_program` function
*
* # Safety
* - `ffi_program` must be a non-null pointer
@ -949,7 +949,7 @@ enum WalletFfiError wallet_ffi_token_elf(struct FfiProgram *ffi_program);
* - Error code on other failures
*
* # Memory
* - freeing this data is a responsibility of a user
* - `FfiProgram` can be freed with corresponding `wallet_ffi_free_ffi_program` function
*
* # Safety
* - `ffi_program` must be a non-null pointer
@ -969,13 +969,21 @@ enum WalletFfiError wallet_ffi_amm_elf(struct FfiProgram *ffi_program);
* - Error code on other failures
*
* # Memory
* - freeing this data is a responsibility of a user
* - `FfiProgram` can be freed with corresponding `wallet_ffi_free_ffi_program` function
*
* # Safety
* - `ffi_program` must be a non-null pointer
*/
enum WalletFfiError wallet_ffi_ata_elf(struct FfiProgram *ffi_program);
/**
* Free a ffi program returned by functions `wallet_ffi_*_elf`.
*
* # Safety
* The result must be either null or a valid result from a elf getter function.
*/
void wallet_ffi_free_ffi_program(struct FfiProgram *ffi_program);
/**
* Synchronize private accounts to a specific block.
*