Skip to content
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

Add precise registry types and allocation-free queries and updates #3184

Merged
merged 4 commits into from
Aug 1, 2024
Merged
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
129 changes: 129 additions & 0 deletions crates/libs/registry/src/data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
use super::*;

// Minimal `Vec<u8>` replacement providing at least `u16` alignment so that it can be used for wide strings.
pub struct Data {
ptr: *mut u8,
len: usize,
}

impl Data {
// Creates a buffer with the specified length of zero bytes.
pub fn new(len: usize) -> Result<Self> {
unsafe {
let bytes = Self::alloc(len)?;

if len > 0 {
core::ptr::write_bytes(bytes.ptr, 0, len);
}

Ok(bytes)
}
}

// Returns the buffer as a slice of u16 for reading wide characters. The slice trims off any trailing zero bytes.
pub fn as_wide(&self) -> &[u16] {
if self.ptr.is_null() {
&[]
} else {
let mut wide =
unsafe { core::slice::from_raw_parts(self.ptr as *const u16, self.len / 2) };

while wide.last() == Some(&0) {
wide = &wide[..wide.len() - 1];
}

wide
}
}

// Creates a buffer by copying the bytes from the slice.
pub fn from_slice(slice: &[u8]) -> Result<Self> {
unsafe {
let bytes = Self::alloc(slice.len())?;

if !slice.is_empty() {
core::ptr::copy_nonoverlapping(slice.as_ptr(), bytes.ptr, slice.len());
}

Ok(bytes)
}
}

// Allocates an uninitialized buffer.
unsafe fn alloc(len: usize) -> Result<Self> {
if len == 0 {
Ok(Self {
ptr: null_mut(),
len: 0,
})
} else {
// This pointer will have at least 8 byte alignment.
let ptr = HeapAlloc(GetProcessHeap(), 0, len) as *mut u8;

if ptr.is_null() {
Err(Error::from_hresult(HRESULT::from_win32(ERROR_OUTOFMEMORY)))
} else {
Ok(Self { ptr, len })
}
}
}
}

impl Drop for Data {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe {
HeapFree(GetProcessHeap(), 0, self.ptr as *mut _);
}
}
}
}

impl Deref for Data {
type Target = [u8];

fn deref(&self) -> &[u8] {
if self.ptr.is_null() {
&[]
} else {
unsafe { core::slice::from_raw_parts(self.ptr, self.len) }
}
}
}

impl core::ops::DerefMut for Data {
fn deref_mut(&mut self) -> &mut [u8] {
if self.ptr.is_null() {
&mut []
} else {
unsafe { core::slice::from_raw_parts_mut(self.ptr, self.len) }
}
}
}

impl Clone for Data {
fn clone(&self) -> Self {
Self::from_slice(self).unwrap()
}
}

impl PartialEq for Data {
fn eq(&self, other: &Self) -> bool {
self.deref() == other.deref()
}
}

impl Eq for Data {}

impl core::fmt::Debug for Data {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.deref().fmt(f)
}
}

impl<const N: usize> TryFrom<[u8; N]> for Data {
type Error = Error;
fn try_from(from: [u8; N]) -> Result<Self> {
Self::from_slice(&from)
}
}
Loading