Skip to content

❓️ rename_allocation via interior mutability #241

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
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: 31 additions & 15 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,49 +12,65 @@ keywords = ["vulkan", "memory", "allocator"]
documentation = "https://docs.rs/gpu-allocator/"
rust-version = "1.69"

include = [
"/README.md",
"/LICENSE-*",
"/src",
"/examples",
]
include = ["/README.md", "/LICENSE-*", "/src", "/examples"]

[package.metadata.docs.rs]
all-features = true

[dependencies]
arc-swap = "1.7"
log = "0.4"
thiserror = "1.0"
presser = { version = "0.3" }
# Only needed for Vulkan. Disable all default features as good practice,
# such as the ability to link/load a Vulkan library.
ash = { version = "0.38", optional = true, default-features = false, features = ["debug"] }
ash = { version = "0.38", optional = true, default-features = false, features = [
"debug",
] }
# Only needed for visualizer.
egui = { version = ">=0.24, <=0.27", optional = true, default-features = false }
egui_extras = { version = ">=0.24, <=0.27", optional = true, default-features = false }

[target.'cfg(any(target_os = "macos", target_os = "ios"))'.dependencies]
metal = { version = "0.28.0", git = "https://github.com/gfx-rs/metal-rs", rev = "0d6214f", default-features = false, features = ["link", "dispatch"], optional = true }
metal = { version = "0.28.0", git = "https://github.com/gfx-rs/metal-rs", rev = "0d6214f", default-features = false, features = [
"link",
"dispatch",
], optional = true }

[target.'cfg(windows)'.dependencies]
# Only needed for public-winapi interop helpers
winapi = { version = "0.3.9", features = ["d3d12", "winerror", "impl-default", "impl-debug"], optional = true }
winapi = { version = "0.3.9", features = [
"d3d12",
"winerror",
"impl-default",
"impl-debug",
], optional = true }

[target.'cfg(windows)'.dependencies.windows]
version = ">=0.53,<=0.56"
features = [
"Win32_Graphics_Direct3D12",
"Win32_Graphics_Dxgi_Common",
]
features = ["Win32_Graphics_Direct3D12", "Win32_Graphics_Dxgi_Common"]
optional = true

[dev-dependencies]
# Enable the "loaded" feature to be able to access the Vulkan entrypoint.
ash = { version = "0.38", default-features = false, features = ["debug", "loaded"] }
ash = { version = "0.38", default-features = false, features = [
"debug",
"loaded",
] }
env_logger = "0.10"

[target.'cfg(windows)'.dev-dependencies]
winapi = { version = "0.3.9", features = ["d3d12", "d3d12sdklayers", "dxgi1_6", "winerror", "impl-default", "impl-debug", "winuser", "windowsx", "libloaderapi"] }
winapi = { version = "0.3.9", features = [
"d3d12",
"d3d12sdklayers",
"dxgi1_6",
"winerror",
"impl-default",
"impl-debug",
"winuser",
"windowsx",
"libloaderapi",
] }

[target.'cfg(windows)'.dev-dependencies.windows]
version = ">=0.53,<=0.56"
Expand Down
27 changes: 12 additions & 15 deletions src/allocator/dedicated_block_allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub(crate) mod visualizer;

use std::{backtrace::Backtrace, sync::Arc};

use arc_swap::ArcSwapOption;
use log::{log, Level};

use super::{AllocationReport, AllocationType, SubAllocator, SubAllocatorBase};
Expand All @@ -15,7 +16,7 @@ pub(crate) struct DedicatedBlockAllocator {
size: u64,
allocated: u64,
/// Only used if [`crate::AllocatorDebugSettings::store_stack_traces`] is [`true`]
name: Option<String>,
name: ArcSwapOption<String>,
backtrace: Arc<Backtrace>,
}

Expand All @@ -24,7 +25,7 @@ impl DedicatedBlockAllocator {
Self {
size,
allocated: 0,
name: None,
name: ArcSwapOption::empty(),
backtrace: Arc::new(Backtrace::disabled()),
}
}
Expand Down Expand Up @@ -52,7 +53,7 @@ impl SubAllocator for DedicatedBlockAllocator {
}

self.allocated = size;
self.name = Some(name.to_string());
self.name.swap(Some(Arc::new(name.to_string())));
self.backtrace = backtrace;

#[allow(clippy::unwrap_used)]
Expand All @@ -69,15 +70,11 @@ impl SubAllocator for DedicatedBlockAllocator {
}
}

fn rename_allocation(
&mut self,
chunk_id: Option<std::num::NonZeroU64>,
name: &str,
) -> Result<()> {
fn rename_allocation(&self, chunk_id: Option<std::num::NonZeroU64>, name: &str) -> Result<()> {
if chunk_id != std::num::NonZeroU64::new(1) {
Err(AllocationError::Internal("Chunk ID must be 1.".into()))
} else {
self.name = Some(name.into());
self.name.swap(Some(Arc::new(name.into())));
Ok(())
}
}
Expand All @@ -88,8 +85,8 @@ impl SubAllocator for DedicatedBlockAllocator {
memory_type_index: usize,
memory_block_index: usize,
) {
let empty = "".to_string();
let name = self.name.as_ref().unwrap_or(&empty);
let name = self.name.load();
let name = (*name).as_ref().map_or("", |name| name);

log!(
log_level,
Expand All @@ -112,10 +109,10 @@ impl SubAllocator for DedicatedBlockAllocator {

fn report_allocations(&self) -> Vec<AllocationReport> {
vec![AllocationReport {
name: self
.name
.clone()
.unwrap_or_else(|| "<Unnamed Dedicated allocation>".to_owned()),
name: self.name.load().as_ref().map_or_else(
|| "<Unnamed Dedicated allocation>".to_owned(),
|s: &Arc<String>| (**s).clone(),
),
offset: 0,
size: self.size,
#[cfg(feature = "visualizer")]
Expand Down
34 changes: 16 additions & 18 deletions src/allocator/free_list_allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{
sync::Arc,
};

use arc_swap::ArcSwapOption;
use log::{log, Level};

use super::{AllocationReport, AllocationType, SubAllocator, SubAllocatorBase};
Expand All @@ -30,7 +31,7 @@ pub(crate) struct MemoryChunk {
pub(crate) size: u64,
pub(crate) offset: u64,
pub(crate) allocation_type: AllocationType,
pub(crate) name: Option<String>,
pub(crate) name: ArcSwapOption<String>,
/// Only used if [`crate::AllocatorDebugSettings::store_stack_traces`] is [`true`]
pub(crate) backtrace: Arc<Backtrace>,
next: Option<std::num::NonZeroU64>,
Expand Down Expand Up @@ -78,7 +79,7 @@ impl FreeListAllocator {
size,
offset: 0,
allocation_type: AllocationType::Free,
name: None,
name: ArcSwapOption::empty(),
backtrace: Arc::new(Backtrace::disabled()),
prev: None,
next: None,
Expand Down Expand Up @@ -248,7 +249,7 @@ impl SubAllocator for FreeListAllocator {
size: best_aligned_size,
offset: free_chunk.offset,
allocation_type,
name: Some(name.to_string()),
name: ArcSwapOption::from_pointee(name.to_string()),
backtrace,
prev: free_chunk.prev,
next: Some(first_fit_id),
Expand Down Expand Up @@ -277,7 +278,7 @@ impl SubAllocator for FreeListAllocator {
.ok_or_else(|| AllocationError::Internal("Invalid chunk reference.".into()))?;

chunk.allocation_type = allocation_type;
chunk.name = Some(name.to_string());
chunk.name.swap(Some(Arc::new(name.to_string())));
chunk.backtrace = backtrace;

self.remove_id_from_free_list(first_fit_id);
Expand All @@ -301,7 +302,7 @@ impl SubAllocator for FreeListAllocator {
)
})?;
chunk.allocation_type = AllocationType::Free;
chunk.name = None;
chunk.name.swap(None);
chunk.backtrace = Arc::new(Backtrace::disabled());

self.allocated -= chunk.size;
Expand All @@ -325,15 +326,11 @@ impl SubAllocator for FreeListAllocator {
Ok(())
}

fn rename_allocation(
&mut self,
chunk_id: Option<std::num::NonZeroU64>,
name: &str,
) -> Result<()> {
fn rename_allocation(&self, chunk_id: Option<std::num::NonZeroU64>, name: &str) -> Result<()> {
let chunk_id = chunk_id
.ok_or_else(|| AllocationError::Internal("Chunk ID must be a valid value.".into()))?;

let chunk = self.chunks.get_mut(&chunk_id).ok_or_else(|| {
let chunk = self.chunks.get(&chunk_id).ok_or_else(|| {
AllocationError::Internal(
"Attempting to rename chunk that is not in chunk list.".into(),
)
Expand All @@ -345,7 +342,7 @@ impl SubAllocator for FreeListAllocator {
));
}

chunk.name = Some(name.into());
chunk.name.swap(Some(Arc::new(name.into())));

Ok(())
}
Expand All @@ -360,8 +357,9 @@ impl SubAllocator for FreeListAllocator {
if chunk.allocation_type == AllocationType::Free {
continue;
}
let empty = "".to_string();
let name = chunk.name.as_ref().unwrap_or(&empty);

let name = chunk.name.load();
let name = (*name).as_ref().map_or("", |name| name);

log!(
log_level,
Expand Down Expand Up @@ -394,10 +392,10 @@ impl SubAllocator for FreeListAllocator {
.iter()
.filter(|(_key, chunk)| chunk.allocation_type != AllocationType::Free)
.map(|(_key, chunk)| AllocationReport {
name: chunk
.name
.clone()
.unwrap_or_else(|| "<Unnamed FreeList allocation>".to_owned()),
name: chunk.name.load().as_ref().map_or_else(
|| "<Unnamed FreeList allocation>".to_owned(),
|s: &Arc<String>| (**s).clone(),
),
offset: chunk.offset,
size: chunk.size,
#[cfg(feature = "visualizer")]
Expand Down
6 changes: 1 addition & 5 deletions src/allocator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,7 @@ pub(crate) trait SubAllocator: SubAllocatorBase + fmt::Debug + Sync + Send {

fn free(&mut self, chunk_id: Option<std::num::NonZeroU64>) -> Result<()>;

fn rename_allocation(
&mut self,
chunk_id: Option<std::num::NonZeroU64>,
name: &str,
) -> Result<()>;
fn rename_allocation(&self, chunk_id: Option<std::num::NonZeroU64>, name: &str) -> Result<()>;

fn report_memory_leaks(
&self,
Expand Down
20 changes: 11 additions & 9 deletions src/d3d12/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{backtrace::Backtrace, fmt, sync::Arc};

use arc_swap::ArcSwapOption;
use log::{debug, warn, Level};
use windows::Win32::{
Foundation::E_OUTOFMEMORY,
Expand Down Expand Up @@ -320,7 +321,7 @@ pub struct Allocation {
memory_type_index: usize,
heap: ID3D12Heap,

name: Option<Box<str>>,
name: ArcSwapOption<String>,
}

impl Allocation {
Expand Down Expand Up @@ -485,7 +486,7 @@ impl MemoryType {
memory_block_index: block_index,
memory_type_index: self.memory_type_index,
heap: mem_block.heap.clone(),
name: Some(desc.name.into()),
name: ArcSwapOption::from_pointee(desc.name.to_owned()),
});
}

Expand All @@ -510,7 +511,7 @@ impl MemoryType {
memory_block_index: mem_block_i,
memory_type_index: self.memory_type_index,
heap: mem_block.heap.clone(),
name: Some(desc.name.into()),
name: ArcSwapOption::from_pointee(desc.name.to_owned()),
});
}
Err(AllocationError::OutOfMemory) => {} // Block is full, continue search.
Expand Down Expand Up @@ -564,7 +565,7 @@ impl MemoryType {
memory_block_index: new_block_index,
memory_type_index: self.memory_type_index,
heap: mem_block.heap.clone(),
name: Some(desc.name.into()),
name: ArcSwapOption::from_pointee(desc.name.to_owned()),
})
}

Expand Down Expand Up @@ -766,7 +767,8 @@ impl Allocator {

pub fn free(&mut self, allocation: Allocation) -> Result<()> {
if self.debug_settings.log_frees {
let name = allocation.name.as_deref().unwrap_or("<null>");
let name = allocation.name.load();
let name = name.as_deref().map_or("<null>", |name| name);
debug!("Freeing `{}`.", name);
if self.debug_settings.log_stack_traces {
let backtrace = Backtrace::force_capture();
Expand All @@ -783,16 +785,16 @@ impl Allocator {
Ok(())
}

pub fn rename_allocation(&mut self, allocation: &mut Allocation, name: &str) -> Result<()> {
allocation.name = Some(name.into());
pub fn rename_allocation(&self, allocation: &Allocation, name: &str) -> Result<()> {
allocation.name.swap(Some(Arc::new(name.into())));

if allocation.is_null() {
return Ok(());
}

let mem_type = &mut self.memory_types[allocation.memory_type_index];
let mem_type = &self.memory_types[allocation.memory_type_index];
let mem_block = mem_type.memory_blocks[allocation.memory_block_index]
.as_mut()
.as_ref()
.ok_or_else(|| AllocationError::Internal("Memory block must be Some.".into()))?;

mem_block
Expand Down
2 changes: 1 addition & 1 deletion src/visualizer/memory_chunks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ pub(crate) fn render_memory_chunks_ui<'a>(
"allocation_type: {}",
chunk.allocation_type.as_str()
));
if let Some(name) = &chunk.name {
if let Some(name) = &*chunk.name.load() {
ui.label(format!("name: {}", name));
}
if settings.show_backtraces
Expand Down
Loading