Skip to content

Commit

Permalink
Fix AtomicBuffer constructors #28
Browse files Browse the repository at this point in the history
  • Loading branch information
XX committed Jan 21, 2025
1 parent 7a384d2 commit 80c7b2a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 29 deletions.
40 changes: 14 additions & 26 deletions src/concurrent/atomic_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,49 +82,37 @@ impl Write for AtomicBuffer {
}
}

// Where needed AtomicBuffer is accessed through mem fences or atomic operations.
// Seems its safe to share AtomicBuffer between threads.
unsafe impl Send for AtomicBuffer {}
unsafe impl Sync for AtomicBuffer {}

// todo: add bounds checks!!!
// todo: remove unsafe?
impl AtomicBuffer {
pub fn from_aligned(aligned: &AlignedBuffer) -> AtomicBuffer {
AtomicBuffer {
pub fn from_aligned(aligned: &AlignedBuffer) -> Self {
Self {
ptr: aligned.ptr,
len: aligned.len as Index,
}
}

pub fn wrap(buffer: AtomicBuffer) -> Self {
AtomicBuffer {
ptr: buffer.ptr,
len: buffer.len as Index,
}
}

pub fn wrap_slice(slice: &mut [u8]) -> Self {
AtomicBuffer {
Self {
ptr: slice.as_mut_ptr(),
len: slice.len() as Index,
}
}

pub fn wrap_raw_slice(slice: *mut [u8]) -> Self {
AtomicBuffer {
pub(crate) unsafe fn wrap_raw_slice(slice: *mut [u8]) -> Self {
Self {
ptr: slice as *mut _,
len: slice.len() as Index,
}
}

//TODO: check that len is ok and ptr is aligned
pub(crate) fn new(ptr: *mut u8, len: Index) -> AtomicBuffer {
AtomicBuffer { ptr, len }
// TODO: check that len is ok and ptr is aligned
pub(crate) fn new(ptr: *mut u8, len: Index) -> Self {
Self { ptr, len }
}

#[inline]
unsafe fn at(&self, offset: Index) -> *mut u8 {
const unsafe fn at(&self, offset: Index) -> *mut u8 {
self.ptr.offset(offset as isize)
}

Expand All @@ -134,7 +122,7 @@ impl AtomicBuffer {
pub fn view(&self, offset: Index, len: Index) -> Self {
self.bounds_check(offset, len);

AtomicBuffer {
Self {
ptr: unsafe { self.at(offset) },
len,
}
Expand Down Expand Up @@ -168,18 +156,18 @@ impl AtomicBuffer {
}

#[inline]
pub fn buffer(&self) -> *mut u8 {
pub const fn buffer(&self) -> *mut u8 {
self.ptr
}

#[inline]
pub fn set_memory(&self, position: Index, len: Index, value: u8) {
self.bounds_check(position, len);
let s = unsafe { slice::from_raw_parts_mut(self.ptr.offset(position as isize), len as usize) };
let slice = unsafe { slice::from_raw_parts_mut(self.ptr.offset(position as isize), len as usize) };

// poor man's memcp
for i in s {
*i = value
for byte in slice {
*byte = value
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/concurrent/status/status_indicator_reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn channel_status_to_str(status_id: i64) -> String {
}

fn static_buffer() -> AtomicBuffer {
let buffer = AtomicBuffer::wrap_raw_slice(&raw mut STATIC_BUFFER_SLICE);
let buffer = unsafe { AtomicBuffer::wrap_raw_slice(&raw mut STATIC_BUFFER_SLICE) };
buffer.put_ordered::<i64>(0, CHANNEL_ENDPOINT_ACTIVE);
buffer
}
Expand All @@ -54,14 +54,14 @@ impl StatusIndicatorReader {
pub fn new(input_buffer: AtomicBuffer, id: i32) -> Self {
if NO_ID_ALLOCATED == id {
Self {
buffer: AtomicBuffer::wrap(static_buffer()),
buffer: static_buffer(),
id,
offset: 0,
}
} else {
let offset = CountersReader::counter_offset(id);
Self {
buffer: AtomicBuffer::wrap(input_buffer),
buffer: input_buffer,
id,
offset,
}
Expand Down

0 comments on commit 80c7b2a

Please sign in to comment.