autocommit 2022-01-03 18:56:52 CET
This commit is contained in:
parent
fc887b339e
commit
30376c595c
39 changed files with 3157 additions and 309 deletions
|
|
@ -1,21 +1,21 @@
|
|||
use crate::object::ObjectId;
|
||||
use crate::utils::buffd::buf_out::{BufFdOut, MsgFds};
|
||||
use std::mem;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::mem::{MaybeUninit};
|
||||
use uapi::OwnedFd;
|
||||
|
||||
pub struct MsgFormatter<'a> {
|
||||
buf: &'a mut BufFdOut,
|
||||
pos: usize,
|
||||
fds: Vec<OwnedFd>,
|
||||
fds: &'a mut Vec<OwnedFd>,
|
||||
}
|
||||
|
||||
impl<'a> MsgFormatter<'a> {
|
||||
pub fn new(buf: &'a mut BufFdOut) -> Self {
|
||||
pub fn new(buf: &'a mut BufFdOut, fds: &'a mut Vec<OwnedFd>) -> Self {
|
||||
Self {
|
||||
pos: buf.out_pos,
|
||||
buf,
|
||||
fds: vec![],
|
||||
fds,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -57,10 +57,29 @@ impl<'a> MsgFormatter<'a> {
|
|||
pub fn header(&mut self, obj: ObjectId, event: u32) -> &mut Self {
|
||||
self.object(obj).uint(event)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Drop for MsgFormatter<'a> {
|
||||
fn drop(&mut self) {
|
||||
pub fn array<F: FnOnce(&mut MsgFormatter<'_>)>(&mut self, f: F) -> &mut Self {
|
||||
let pos = self.buf.out_pos;
|
||||
self.uint(0);
|
||||
let len = {
|
||||
let mut fmt = MsgFormatter {
|
||||
buf: self.buf,
|
||||
pos,
|
||||
fds: self.fds,
|
||||
};
|
||||
f(&mut fmt);
|
||||
let len = self.buf.out_pos - pos + 4;
|
||||
let none = [MaybeUninit::new(0); 4];
|
||||
self.buf.write(&none[..self.buf.out_pos.wrapping_neg() & 3]);
|
||||
len as u32
|
||||
};
|
||||
unsafe {
|
||||
(*self.buf.out_buf)[pos..pos + 4].copy_from_slice(uapi::as_maybe_uninit_bytes(&len));
|
||||
}
|
||||
self
|
||||
}
|
||||
|
||||
pub fn write_len(self) {
|
||||
assert!(self.buf.out_pos - self.pos >= 8);
|
||||
assert_eq!(self.pos % 4, 0);
|
||||
unsafe {
|
||||
|
|
@ -71,7 +90,7 @@ impl<'a> Drop for MsgFormatter<'a> {
|
|||
if self.fds.len() > 0 {
|
||||
self.buf.fds.push_back(MsgFds {
|
||||
pos: self.pos,
|
||||
fds: mem::take(&mut self.fds),
|
||||
fds: mem::take(self.fds),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,7 @@
|
|||
use ahash::AHashMap;
|
||||
use std::cell::{RefCell, RefMut};
|
||||
use std::hash::Hash;
|
||||
use std::mem;
|
||||
|
||||
pub struct CopyHashMap<K, V> {
|
||||
map: RefCell<AHashMap<K, V>>,
|
||||
|
|
@ -40,6 +41,10 @@ impl<K: Eq + Hash, V: Clone> CopyHashMap<K, V> {
|
|||
}
|
||||
|
||||
pub fn clear(&self) {
|
||||
self.map.borrow_mut().clear();
|
||||
mem::take(&mut *self.map.borrow_mut());
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.map.borrow().is_empty()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
176
src/utils/linkedlist.rs
Normal file
176
src/utils/linkedlist.rs
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
use crate::utils::ptr_ext::PtrExt;
|
||||
use crate::NumCell;
|
||||
use std::cell::Cell;
|
||||
use std::mem::MaybeUninit;
|
||||
use std::ops::Deref;
|
||||
use std::ptr::NonNull;
|
||||
|
||||
pub struct LinkedList<T> {
|
||||
root: Node<T>,
|
||||
}
|
||||
|
||||
impl<T> Default for LinkedList<T> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> LinkedList<T> {
|
||||
pub fn new() -> Self {
|
||||
let node = Box::into_raw(Box::new(NodeData {
|
||||
rc: NumCell::new(3),
|
||||
prev: Cell::new(NonNull::dangling()),
|
||||
next: Cell::new(NonNull::dangling()),
|
||||
data: MaybeUninit::uninit(),
|
||||
}));
|
||||
unsafe {
|
||||
node.deref().prev.set(NonNull::new_unchecked(node));
|
||||
node.deref().next.set(NonNull::new_unchecked(node));
|
||||
Self {
|
||||
root: Node {
|
||||
data: NonNull::new_unchecked(node),
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prepend(&self, t: T) -> Node<T> {
|
||||
self.root.prepend(t)
|
||||
}
|
||||
|
||||
pub fn append(&self, t: T) -> Node<T> {
|
||||
self.root.append(t)
|
||||
}
|
||||
|
||||
pub fn iter(&self) -> LinkedListIter<T> {
|
||||
unsafe {
|
||||
let root = self.root.data.as_ref();
|
||||
root.rc.fetch_add(1);
|
||||
root.next.get().as_ref().rc.fetch_add(1);
|
||||
LinkedListIter {
|
||||
root: self.root.data,
|
||||
next: root.next.get(),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct LinkedListIter<T> {
|
||||
root: NonNull<NodeData<T>>,
|
||||
next: NonNull<NodeData<T>>,
|
||||
}
|
||||
|
||||
impl<T> Iterator for LinkedListIter<T> {
|
||||
type Item = NodeRef<T>;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.root == self.next {
|
||||
return None;
|
||||
}
|
||||
unsafe {
|
||||
let old_next = self.next;
|
||||
self.next = old_next.as_ref().next.get();
|
||||
self.next.as_ref().rc.fetch_add(1);
|
||||
Some(NodeRef { data: old_next })
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drop for LinkedListIter<T> {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
dec_ref_count(self.root, 1);
|
||||
dec_ref_count(self.next, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Node<T> {
|
||||
data: NonNull<NodeData<T>>,
|
||||
}
|
||||
|
||||
impl<T> Deref for Node<T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
unsafe { self.data.as_ref().data.assume_init_ref() }
|
||||
}
|
||||
}
|
||||
|
||||
pub struct NodeRef<T> {
|
||||
data: NonNull<NodeData<T>>,
|
||||
}
|
||||
|
||||
impl<T> Deref for NodeRef<T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
unsafe { self.data.as_ref().data.assume_init_ref() }
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drop for NodeRef<T> {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
dec_ref_count(self.data, 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct NodeData<T> {
|
||||
rc: NumCell<usize>,
|
||||
prev: Cell<NonNull<NodeData<T>>>,
|
||||
next: Cell<NonNull<NodeData<T>>>,
|
||||
data: MaybeUninit<T>,
|
||||
}
|
||||
|
||||
unsafe fn dec_ref_count<T>(slf: NonNull<NodeData<T>>, n: usize) {
|
||||
if slf.as_ref().rc.fetch_sub(n) == n {
|
||||
drop(Box::from_raw(slf.as_ptr()));
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Drop for Node<T> {
|
||||
fn drop(&mut self) {
|
||||
unsafe {
|
||||
{
|
||||
let data = self.data.as_ref();
|
||||
data.prev.get().as_ref().next.set(data.next.get());
|
||||
data.next.get().as_ref().prev.set(data.prev.get());
|
||||
}
|
||||
dec_ref_count(self.data, 3);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Node<T> {
|
||||
pub fn prepend(&self, t: T) -> Node<T> {
|
||||
unsafe {
|
||||
let data = self.data.as_ref();
|
||||
let node = NonNull::new_unchecked(Box::into_raw(Box::new(NodeData {
|
||||
rc: NumCell::new(3),
|
||||
prev: Cell::new(data.prev.get()),
|
||||
next: Cell::new(self.data),
|
||||
data: MaybeUninit::new(t),
|
||||
})));
|
||||
data.prev.get().as_ref().next.set(node);
|
||||
data.prev.set(node);
|
||||
Node { data: node }
|
||||
}
|
||||
}
|
||||
|
||||
pub fn append(&self, t: T) -> Node<T> {
|
||||
unsafe {
|
||||
let data = self.data.as_ref();
|
||||
let node = NonNull::new_unchecked(Box::into_raw(Box::new(NodeData {
|
||||
rc: NumCell::new(3),
|
||||
prev: Cell::new(self.data),
|
||||
next: Cell::new(data.next.get()),
|
||||
data: MaybeUninit::new(t),
|
||||
})));
|
||||
data.next.get().as_ref().prev.set(node);
|
||||
data.next.set(node);
|
||||
Node { data: node }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,9 @@
|
|||
pub mod buffd;
|
||||
pub mod copyhashmap;
|
||||
pub mod linkedlist;
|
||||
pub mod lock;
|
||||
pub mod numcell;
|
||||
pub mod oneshot;
|
||||
pub mod ptr_ext;
|
||||
pub mod queue;
|
||||
pub mod vec_ext;
|
||||
|
|
|
|||
25
src/utils/ptr_ext.rs
Normal file
25
src/utils/ptr_ext.rs
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
pub trait PtrExt<T> {
|
||||
unsafe fn deref<'a>(self) -> &'a T;
|
||||
}
|
||||
|
||||
pub trait MutPtrExt<T> {
|
||||
unsafe fn deref_mut<'a>(self) -> &'a mut T;
|
||||
}
|
||||
|
||||
impl<T> PtrExt<T> for *const T {
|
||||
unsafe fn deref<'a>(self) -> &'a T {
|
||||
&*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> PtrExt<T> for *mut T {
|
||||
unsafe fn deref<'a>(self) -> &'a T {
|
||||
&*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> MutPtrExt<T> for *mut T {
|
||||
unsafe fn deref_mut<'a>(self) -> &'a mut T {
|
||||
&mut *self
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue