Skip to content
Merged
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
46 changes: 46 additions & 0 deletions ext/node/ops/zlib/alloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,49 @@ pub extern "C" fn zfree(_ptr: *mut c_void, address: *mut c_void) {
alloc::dealloc(ptr as *mut u8, layout)
}
}

pub extern "C" fn brotli_alloc(
_opaque: *mut brotli::ffi::broccoli::c_void,
size: usize,
) -> *mut brotli::ffi::broccoli::c_void {
// Allocate space for a `usize` header to store the allocation size
let total_size = match size
.checked_add(std::mem::size_of::<usize>())
.map(|size| align_up(size, ALIGN))
{
Some(i) => i,
None => return ptr::null_mut(),
};

let layout = match Layout::from_size_align(total_size, ALIGN) {
Ok(layout) => layout,
Err(_) => return ptr::null_mut(),
};

// SAFETY: `layout` has non-zero size
unsafe {
let ptr = alloc::alloc(layout) as *mut usize;
if ptr.is_null() {
return ptr as *mut brotli::ffi::broccoli::c_void;
}
*ptr = total_size;
ptr.add(1) as *mut brotli::ffi::broccoli::c_void
}
}

pub extern "C" fn brotli_free(
_opaque: *mut brotli::ffi::broccoli::c_void,
address: *mut brotli::ffi::broccoli::c_void,
) {
if address.is_null() {
return;
}

// SAFETY: Move back one pointer to read the size we stored in `brotli_alloc`
unsafe {
let ptr = (address as *mut usize).offset(-1);
let size = *ptr;
let layout = Layout::from_size_align_unchecked(size, ALIGN);
alloc::dealloc(ptr as *mut u8, layout)
}
}
10 changes: 6 additions & 4 deletions ext/node/ops/zlib/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ mod stream;
use mode::Flush;
use mode::Mode;

use self::alloc::brotli_alloc;
use self::alloc::brotli_free;
use self::stream::StreamWrapper;

#[inline]
Expand Down Expand Up @@ -593,8 +595,8 @@ impl BrotliEncoder {
// SAFETY: creates new brotli encoder instance. `params` is a valid slice of u32 values.
let inst = unsafe {
let state = ffi::compressor::BrotliEncoderCreateInstance(
None,
None,
Some(brotli_alloc),
Some(brotli_free),
std::ptr::null_mut(),
);
for (i, &value) in params.iter().enumerate() {
Expand Down Expand Up @@ -791,8 +793,8 @@ impl BrotliDecoder {
// SAFETY: creates new brotli decoder instance. `params` is a valid slice of u32 values.
let inst = unsafe {
let state = ffi::decompressor::ffi::BrotliDecoderCreateInstance(
None,
None,
Some(brotli_alloc),
Some(brotli_free),
std::ptr::null_mut(),
);
for (i, &value) in params.iter().enumerate() {
Expand Down
Loading