From 8a365cf905f9f605c1dff93c8ae083caeb52d03d Mon Sep 17 00:00:00 2001 From: marc0246 <40955683+marc0246@users.noreply.github.com> Date: Tue, 22 Oct 2024 13:50:13 +0200 Subject: [PATCH] Add `Suballocation[Node]::as[_usize]_range` (#2586) --- .../src/memory/allocator/suballocator/mod.rs | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/vulkano/src/memory/allocator/suballocator/mod.rs b/vulkano/src/memory/allocator/suballocator/mod.rs index f4a8ca6b..727c9062 100644 --- a/vulkano/src/memory/allocator/suballocator/mod.rs +++ b/vulkano/src/memory/allocator/suballocator/mod.rs @@ -12,6 +12,7 @@ use crate::{image::ImageTiling, DeviceSize}; use std::{ error::Error, fmt::{self, Debug, Display}, + ops::Range, }; mod buddy; @@ -287,6 +288,24 @@ pub struct Suballocation { pub handle: AllocationHandle, } +impl Suballocation { + /// Returns the suballocation as a `DeviceSize` range. + /// + /// This is identical to `self.offset..self.offset + self.size`. + #[inline] + pub fn as_range(&self) -> Range { + self.offset..self.offset + self.size + } + + /// Returns the suballocation as a `usize` range. + /// + /// This is identical to `self.offset as usize..(self.offset + self.size) as usize`. + #[inline] + pub fn as_usize_range(&self) -> Range { + self.offset as usize..(self.offset + self.size) as usize + } +} + /// Error that can be returned when creating an [allocation] using a [suballocator]. /// /// [allocation]: Suballocation @@ -331,6 +350,24 @@ pub struct SuballocationNode { pub allocation_type: SuballocationType, } +impl SuballocationNode { + /// Returns the suballocation as a `DeviceSize` range. + /// + /// This is identical to `self.offset..self.offset + self.size`. + #[inline] + pub fn as_range(&self) -> Range { + self.offset..self.offset + self.size + } + + /// Returns the suballocation as a `usize` range. + /// + /// This is identical to `self.offset as usize..(self.offset + self.size) as usize`. + #[inline] + pub fn as_usize_range(&self) -> Range { + self.offset as usize..(self.offset + self.size) as usize + } +} + /// Tells us if an allocation within a [suballocator]'s list/tree of suballocations is free, and if /// not, what type of resources can be bound to it. The suballocator needs to keep track of this in /// order to be able to respect the buffer-image granularity.