Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
poolman
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Per Lindgren
poolman
Commits
68d9e9e2
Commit
68d9e9e2
authored
4 years ago
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
pool trait
parent
83868d29
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
examples/usage.rs
+5
-4
5 additions, 4 deletions
examples/usage.rs
src/lib.rs
+117
-36
117 additions, 36 deletions
src/lib.rs
with
122 additions
and
40 deletions
examples/usage.rs
+
5
−
4
View file @
68d9e9e2
...
...
@@ -3,6 +3,7 @@ use poolman::PoolMan;
#[derive(Debug)]
struct
IDrop
(
u32
);
unsafe
impl
Sync
for
IDrop
(
u32
)
{}
impl
core
::
ops
::
Drop
for
IDrop
{
fn
drop
(
&
mut
self
)
{
...
...
@@ -15,17 +16,17 @@ static P: PoolMan<IDrop, 2> = PoolMan::new([IDrop(10), IDrop(11)]);
fn
main
()
{
P
.init
();
{
let
e
=
P
.alloc
()
.unwrap
();
let
e
=
P
.alloc
b
()
.unwrap
();
println!
(
"e {:?}"
,
*
e
);
mem
::
drop
(
e
);
let
mut
e1
=
P
.alloc
()
.unwrap
();
let
mut
e1
=
P
.alloc
b
()
.unwrap
();
println!
(
"e1 {:?}"
,
*
e1
);
*
e1
=
IDrop
(
2
);
println!
(
"e1 {:?}"
,
*
e1
);
let
e2
=
P
.alloc
()
.unwrap
();
let
e2
=
P
.alloc
b
()
.unwrap
();
println!
(
"e2 {:?}"
,
*
e2
);
}
let
e
=
P
.alloc
()
.unwrap
();
let
e
=
P
.alloc
b
()
.unwrap
();
println!
(
"e {:?}"
,
*
e
);
}
This diff is collapsed.
Click to expand it.
src/lib.rs
+
117
−
36
View file @
68d9e9e2
#![no_std]
use
core
::
cell
::
UnsafeCell
;
// use core::marker::PhantomData;
use
core
::
ops
::{
Deref
,
DerefMut
,
Drop
};
use
cortex_m
::
interrupt
;
...
...
@@ -42,40 +43,73 @@ pub enum Error {
AlreadyInitialized
,
}
pub
struct
Box
<
T
:
'static
+
Sized
,
const
S
:
usize
>
{
trait
Pool
<
T
>
where
T
:
Send
+
Sync
,
{
fn
dealloc
(
&
'static
self
,
index
:
usize
);
fn
index_deref
(
&
'static
self
,
index
:
usize
)
->
&
T
;
fn
index_deref_mut
(
&
'static
self
,
index
:
usize
)
->
&
mut
T
;
}
pub
struct
B
<
T
:
'static
+
Sized
+
Send
+
Sync
>
{
index
:
usize
,
allocator
:
&
'static
PoolMan
<
T
,
S
>
,
allocator
:
&
'static
dyn
Pool
<
T
>
,
}
impl
<
T
:
'static
,
const
S
:
usize
>
Drop
for
B
ox
<
T
,
S
>
{
impl
<
T
:
'static
+
Sized
+
Send
+
Sync
>
Drop
for
B
<
T
>
{
fn
drop
(
&
mut
self
)
{
self
.allocator
.dealloc
(
self
.index
)
}
}
impl
<
T
:
'static
,
const
S
:
usize
>
Deref
for
B
ox
<
T
,
S
>
{
impl
<
T
:
'static
+
Sized
+
Send
+
Sync
>
Deref
for
B
<
T
>
{
type
Target
=
T
;
fn
deref
(
&
self
)
->
&
T
{
unsafe
{
self
.allocator
.data
/* RacyCell */
.get_mut_unchecked
()
/* Array */
.get_unchecked
(
self
.index
)
}
self
.allocator
.index_deref
(
self
.index
)
}
}
impl
<
T
:
'static
,
const
S
:
usize
>
DerefMut
for
B
ox
<
T
,
S
>
{
impl
<
T
:
'static
+
Sized
+
Send
+
Sync
>
DerefMut
for
B
<
T
>
{
fn
deref_mut
(
&
mut
self
)
->
&
mut
T
{
unsafe
{
self
.allocator
.data
/* RacyCell */
.get_mut_unchecked
()
/* Array */
.get_unchecked_mut
(
self
.index
)
}
self
.allocator
.index_deref_mut
(
self
.index
)
}
}
// pub struct Box<T: 'static + Sized, const S: usize> {
// index: usize,
// allocator: &'static PoolMan<T, S>,
// }
// impl<T: 'static, const S: usize> Drop for Box<T, S> {
// fn drop(&mut self) {
// self.allocator.dealloc(self.index)
// }
// }
// impl<T: 'static, const S: usize> Deref for Box<T, S> {
// type Target = T;
// fn deref(&self) -> &T {
// unsafe {
// self.allocator
// .data /* RacyCell */
// .get_mut_unchecked() /* Array */
// .get_unchecked(self.index)
// }
// }
// }
// impl<T: 'static, const S: usize> DerefMut for Box<T, S> {
// fn deref_mut(&mut self) -> &mut T {
// unsafe {
// self.allocator
// .data /* RacyCell */
// .get_mut_unchecked() /* Array */
// .get_unchecked_mut(self.index)
// }
// }
// }
pub
struct
PoolMan
<
T
:
'static
,
const
S
:
usize
>
{
data
:
RacyCell
<
[
T
;
S
]
>
,
free
:
RacyCell
<
[
usize
;
S
]
>
,
...
...
@@ -83,13 +117,15 @@ pub struct PoolMan<T: 'static, const S: usize> {
init
:
RacyCell
<
bool
>
,
}
impl
<
T
,
const
S
:
usize
>
PoolMan
<
T
,
S
>
where
T
:
Sized
,
T
:
Sized
+
Send
+
Sync
,
{
pub
const
fn
new
(
v
:
[
T
;
S
])
->
Self
where
T
:
Sized
,
T
:
Sized
+
Send
+
Sync
,
{
Self
{
data
:
RacyCell
::
new
(
v
),
...
...
@@ -115,23 +151,45 @@ where
})
}
// Safety:
// dealloc can only be called by dropping Box
// which can happen only if already initialized
// thus it is safe to assume init == true
//
// not acc
i
ssible from user code
#[inline]
fn
dealloc
(
&
self
,
index
:
usize
)
{
interrupt_free
(||
unsafe
{
*
self
.free
.get_mut_unchecked
()
.get_unchecked_mut
(
index
)
=
*
self
.head
.get_mut_unchecked
();
*
self
.head
.get_mut_unchecked
()
=
index
;
});
}
//
//
Safety:
//
//
dealloc can only be called by dropping Box
//
//
which can happen only if already initialized
//
//
thus it is safe to assume init == true
//
//
//
//
not acc
e
ssible from user code
//
#[inline]
//
fn dealloc(&self, index: usize) {
//
interrupt_free(|| unsafe {
//
*self.free.get_mut_unchecked().get_unchecked_mut(index) =
//
*self.head.get_mut_unchecked();
//
*self.head.get_mut_unchecked() = index;
//
});
//
}
#[inline]
pub
fn
alloc
(
&
'static
self
)
->
Result
<
Box
<
T
,
S
>
,
Error
>
{
// #[inline]
// pub fn alloc(&'static self) -> Result<Box<T, S>, Error> {
// // this check does not need to be inside of critical section
// // as initialization is monotonic, cannot be undone
// if !unsafe { *self.init.get_mut_unchecked() } {
// Err(Error::Uninitialized)
// } else {
// interrupt_free(|| unsafe {
// let index = *self.head.get_mut_unchecked();
// *self.head.get_mut_unchecked() =
// *self.free.get_mut_unchecked().get_unchecked(index);
// if *self.head.get_mut_unchecked() > S {
// Err(Error::OutOfMemory)
// } else {
// Ok(Box {
// index,
// allocator: self,
// })
// }
// })
// }
// }
pub
fn
allocb
(
&
'static
self
)
->
Result
<
B
<
T
>
,
Error
>
{
// this check does not need to be inside of critical section
// as initialization is monotonic, cannot be undone
if
!
unsafe
{
*
self
.init
.get_mut_unchecked
()
}
{
...
...
@@ -144,7 +202,7 @@ where
if
*
self
.head
.get_mut_unchecked
()
>
S
{
Err
(
Error
::
OutOfMemory
)
}
else
{
Ok
(
B
ox
{
Ok
(
B
{
index
,
allocator
:
self
,
})
...
...
@@ -153,3 +211,26 @@ where
}
}
}
// sealed, should only be externally accessible
impl
<
T
,
const
S
:
usize
>
Pool
<
T
>
for
PoolMan
<
T
,
S
>
where
T
:
Sized
+
Send
+
Sync
,
{
#[inline]
fn
dealloc
(
&
'static
self
,
index
:
usize
)
{
interrupt_free
(||
unsafe
{
*
self
.free
.get_mut_unchecked
()
.get_unchecked_mut
(
index
)
=
*
self
.head
.get_mut_unchecked
();
*
self
.head
.get_mut_unchecked
()
=
index
;
});
}
fn
index_deref
(
&
'static
self
,
index
:
usize
)
->
&
'static
T
{
unsafe
{
self
.data
.get_mut_unchecked
()
.get_unchecked
(
index
)
}
}
fn
index_deref_mut
(
&
'static
self
,
index
:
usize
)
->
&
'static
mut
T
{
unsafe
{
self
.data
.get_mut_unchecked
()
.get_unchecked_mut
(
index
)
}
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment