Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ edition = "2021" # Rust version
crate-type = ["rlib", "cdylib", "staticlib"]

[dependencies]
libc = { version = "0.2.152", optional = true }
libc = { version = "0.2.152" }

# The dependencies below are also workspace members in order to include their
# custom wrappers (lib.rs) in this project. However, even if their respective
Expand Down Expand Up @@ -74,7 +74,7 @@ cbindgen = "0.26.0"
default = ["dav1d", "libyuv", "png", "jpeg", "gif", "sharpyuv"]
cli = ["dep:clap", "dep:clap_derive"]
capi = []
dav1d = ["dep:libc", "dep:dav1d-sys"]
dav1d = ["dep:dav1d-sys"]
libgav1 = ["dep:libgav1-sys"]
libyuv = ["dep:libyuv-sys"]
android_mediacodec = ["dep:ndk-sys"]
Expand Down
2 changes: 1 addition & 1 deletion include/avif/avif.h
Original file line number Diff line number Diff line change
Expand Up @@ -1016,7 +1016,7 @@ void crabby_avifGetPixelFormatInfo(avifPixelFormat format, avifPixelFormatInfo *
void crabby_avifDiagnosticsClearError(avifDiagnostics *diag);

/// # Safety
/// C API function that does not perform any unsafe operation.
/// C API function.
void *crabby_avifAlloc(size_t size);

/// # Safety
Expand Down
17 changes: 5 additions & 12 deletions src/capi/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,18 +548,11 @@ pub const AVIF_COLOR_PRIMARIES_DCI_P3: u16 = 12;
pub const AVIF_TRANSFER_CHARACTERISTICS_SMPTE2084: u16 = 16;

/// # Safety
/// C API function that does not perform any unsafe operation.
/// C API function.
#[no_mangle]
pub unsafe extern "C" fn crabby_avifAlloc(size: usize) -> *mut c_void {
let mut data: Vec<u8> = Vec::new();
if data.try_reserve_exact(size).is_err() {
return std::ptr::null_mut();
}
data.resize(size, 0);
let mut boxed_slice = data.into_boxed_slice();
let ptr = boxed_slice.as_mut_ptr();
std::mem::forget(boxed_slice);
ptr as *mut c_void
// SAFETY: Calling a C function with valid parameters.
unsafe { libc::calloc(1, size) }
}

/// # Safety
Expand All @@ -568,8 +561,8 @@ pub unsafe extern "C" fn crabby_avifAlloc(size: usize) -> *mut c_void {
#[no_mangle]
pub unsafe extern "C" fn crabby_avifFree(p: *mut c_void) {
if !p.is_null() {
// SAFETY: Safe because of the pre-condition and the null check.
let _ = unsafe { Box::from_raw(p as *mut u8) };
// SAFETY: Calling a C function with valid parameters.
unsafe { libc::free(p) }
}
}

Expand Down
Loading