Skip to content

Add optimized grow()/shrink() functions to Allocator implementation for pool #191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
84 changes: 84 additions & 0 deletions src/core/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,54 @@ unsafe impl Allocator for Pool {
ngx_pfree(self.0.as_ptr(), ptr.as_ptr().cast());
}
}

unsafe fn grow(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
debug_assert!(
new_layout.size() >= old_layout.size(),
"`new_layout.size()` must be greater than or equal to `old_layout.size()`"
);
self.resize_in_place(ptr, old_layout, new_layout)
}

unsafe fn grow_zeroed(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
debug_assert!(
new_layout.size() >= old_layout.size(),
"`new_layout.size()` must be greater than or equal to `old_layout.size()`"
);
self.resize_in_place(ptr, old_layout, new_layout)
.inspect(|new_ptr| {
unsafe {
ptr::write_bytes(
new_ptr.as_ptr().cast::<u8>().byte_add(new_layout.size()),
0,
old_layout.size() - new_layout.size(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if new_layout.size() >= old_layout.size()

old_layout.size() - new_layout.size() will be negative?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ptr::write_bytes(
    new_ptr.as_ptr().cast::<u8>().add(old_layout.size()),  // Start at end of old data
    0,
    new_layout.size() - old_layout.size(),  // Zero the NEW bytes
)

)
};
})
}

unsafe fn shrink(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
debug_assert!(
new_layout.size() <= old_layout.size(),
"`new_layout.size()` must be smaller than or equal to `old_layout.size()`"
);
self.resize_in_place(ptr, old_layout, new_layout)
}
}

impl AsRef<ngx_pool_t> for Pool {
Expand Down Expand Up @@ -229,6 +277,42 @@ impl Pool {
p
}
}

/// Resizes a memory allocation in place if possible.
///
/// If resizing is requested for the last allocation in the pool, it may be
/// possible to adjust pool data and avoid any real allocations.
///
/// # Safety
/// This function is marked as unsafe because it involves raw pointer manipulation.
unsafe fn resize_in_place(
&self,
ptr: NonNull<u8>,
old_layout: Layout,
new_layout: Layout,
) -> Result<NonNull<[u8]>, AllocError> {
if ptr.byte_add(old_layout.size()).as_ptr() == self.as_ref().d.last
&& ptr.byte_add(new_layout.size()).as_ptr() <= self.as_ref().d.end
&& ptr.align_offset(new_layout.align()) == 0
{
let pool = self.0.as_ptr();
unsafe {
(*pool).d.last = (*pool)
.d
.last
.byte_offset(new_layout.size() as isize - old_layout.size() as isize)
};
Ok(NonNull::slice_from_raw_parts(ptr, new_layout.size()))
} else {
let size = core::cmp::min(old_layout.size(), new_layout.size());
let new_ptr = self.allocate(new_layout)?;
unsafe {
ptr::copy_nonoverlapping(ptr.as_ptr(), new_ptr.as_ptr().cast(), size);
self.deallocate(ptr, old_layout);
}
Ok(new_ptr)
}
}
}

/// Cleanup handler for a specific type `T`.
Expand Down
Loading