Skip to content

Commit

Permalink
Improved: Align buffers on position rather than base pointer (resolvi…
Browse files Browse the repository at this point in the history
…ng additional edge cases)
  • Loading branch information
Sewer56 committed Dec 26, 2023
1 parent a5774d9 commit 132aa41
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src-rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src-rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "reloaded-memory-buffers"
version = "4.0.2"
version = "4.0.3"
edition = "2021"
authors = [ "sewer56" ]
description = "Shared, Concurrent, Permanent Memory Allocator tied to Process Lifetime"
Expand Down
17 changes: 12 additions & 5 deletions src-rust/src/buffers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,23 @@ impl Buffers {
settings: &BufferSearchSettings,
alignment: u32,
) -> Result<SafeLocatorItem, BufferSearchError> {
// Add expected size.
let max_misalignment = alignment.saturating_sub(1);

// Adjust the size to include potential extra space for alignment.
let mut new_settings = *settings;
new_settings.size += alignment.saturating_sub(1);
new_settings.size += max_misalignment;

let result = Self::get_buffer(&new_settings)?;
unsafe {
let locator_item = result.item.get();
let base_address = (*locator_item).base_address.value;
let aligned_address = round_up(base_address, alignment as usize);
(*locator_item).base_address.value = aligned_address;
let address = (*locator_item).base_address.value + (*locator_item).position as usize;

// Use the round_up function to efficiently align the address.
let aligned_address = round_up(address, alignment as usize);
let delta = aligned_address - address;

// Adjust the position in the buffer accordingly.
(*locator_item).position += delta as u32;
Ok(result)
}
}
Expand Down

0 comments on commit 132aa41

Please sign in to comment.