-
Notifications
You must be signed in to change notification settings - Fork 37
Add SimpleVramAllocator::free_all and attach a lifetime to VRAM chunks
#119
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,9 @@ | ||
| use crate::sys::TexturePixelFormat; | ||
| use crate::sys::{sceGeEdramGetAddr, sceGeEdramGetSize}; | ||
| use core::marker::PhantomData; | ||
| use core::mem::size_of; | ||
| use core::ptr::null_mut; | ||
| use core::sync::atomic::{AtomicU32, Ordering}; | ||
|
|
||
| type VramAllocator = SimpleVramAllocator; | ||
|
|
||
|
|
@@ -27,14 +29,20 @@ impl VramAllocatorSingleton { | |
| } | ||
| } | ||
|
|
||
| pub struct VramMemChunk { | ||
| pub struct VramMemChunk<'a> { | ||
| start: u32, | ||
| len: u32, | ||
| // Needed since VramMemChunk has a lifetime, but doesn't contain references | ||
| vram: PhantomData<&'a mut ()>, | ||
| } | ||
|
|
||
| impl VramMemChunk { | ||
| impl VramMemChunk<'_> { | ||
| fn new(start: u32, len: u32) -> Self { | ||
| Self { start, len } | ||
| Self { | ||
| start, | ||
| len, | ||
| vram: PhantomData, | ||
| } | ||
| } | ||
|
|
||
| pub fn as_mut_ptr_from_zero(&self) -> *mut u8 { | ||
|
|
@@ -54,38 +62,55 @@ impl VramMemChunk { | |
| // TODO: pin? | ||
| #[derive(Debug)] | ||
| pub struct SimpleVramAllocator { | ||
| offset: u32, | ||
| offset: AtomicU32, | ||
| } | ||
|
|
||
| impl SimpleVramAllocator { | ||
| const fn new() -> Self { | ||
| Self { offset: 0 } | ||
| Self { | ||
| offset: AtomicU32::new(0), | ||
| } | ||
| } | ||
|
|
||
| // TODO: return a Result instead of panicking | ||
| pub fn alloc(&mut self, size: u32) -> VramMemChunk { | ||
| let old_offset = self.offset; | ||
| self.offset += size; | ||
| /// Frees all previously allocated VRAM chunks. | ||
| /// | ||
| /// This resets the allocator's counter, but does not change the contents of | ||
| /// VRAM. Since this method requires `&mut Self`, it cannot overlap with any | ||
| /// previously allocated `VramMemChunk`s since they have the lifetime of the | ||
| /// `&Self` that allocated them. | ||
| pub fn free_all(&mut self) { | ||
| self.offset.store(0, Ordering::Relaxed); | ||
| } | ||
|
|
||
| if self.offset > self.total_mem() { | ||
| // TODO: return a Result instead of panicking | ||
| /// Allocates `size` bytes of VRAM | ||
| /// | ||
| /// The returned VRAM chunk has the same lifetime as the | ||
| /// `SimpleVramAllocator` borrow (i.e. `&self`) that allocated it. | ||
| pub fn alloc<'a>(&'a self, size: u32) -> VramMemChunk<'a> { | ||
| let old_offset = self.offset.load(Ordering::Relaxed); | ||
| let new_offset = old_offset + size; | ||
| self.offset.store(new_offset, Ordering::Relaxed); | ||
|
Comment on lines
+91
to
+93
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see a good old race condition right there.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this intentional btw? Perhaps this was written as a singlethreaded code?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. But the problem is
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In bumpalo thread support is provided by a "herd" which is roughly a
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
No, i think have figured atomics out. It's just nvidia-driver-390 killed my os, |
||
|
|
||
| if new_offset > self.total_mem() { | ||
| panic!("Total VRAM size exceeded!"); | ||
| } | ||
|
|
||
| VramMemChunk::new(old_offset, size) | ||
| } | ||
|
|
||
| // TODO: ensure 16-bit alignment? | ||
| pub fn alloc_sized<T: Sized>(&mut self, count: u32) -> VramMemChunk { | ||
| pub fn alloc_sized<'a, T: Sized>(&'a self, count: u32) -> VramMemChunk<'a> { | ||
| let size = size_of::<T>() as u32; | ||
| self.alloc(count * size) | ||
| } | ||
|
|
||
| pub fn alloc_texture_pixels( | ||
| &mut self, | ||
| pub fn alloc_texture_pixels<'a>( | ||
| &'a self, | ||
| width: u32, | ||
| height: u32, | ||
| psm: TexturePixelFormat, | ||
| ) -> VramMemChunk { | ||
| ) -> VramMemChunk<'a> { | ||
ayrtonm marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| let size = get_memory_size(width, height, psm); | ||
| self.alloc(size) | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.