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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Per Lindgren
poolman
Commits
4d3716d3
Commit
4d3716d3
authored
4 years ago
by
Per Lindgren
Browse files
Options
Downloads
Patches
Plain Diff
first commit
parents
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
.gitignore
+1
-0
1 addition, 0 deletions
.gitignore
Cargo.toml
+9
-0
9 additions, 0 deletions
Cargo.toml
src/main.rs
+91
-0
91 additions, 0 deletions
src/main.rs
with
101 additions
and
0 deletions
.gitignore
0 → 100644
+
1
−
0
View file @
4d3716d3
/target
This diff is collapsed.
Click to expand it.
Cargo.toml
0 → 100644
+
9
−
0
View file @
4d3716d3
[package]
name
=
"poolman"
version
=
"0.1.0"
authors
=
[
"Per Lindgren <per.lindgren@ltu.se>"
]
edition
=
"2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
This diff is collapsed.
Click to expand it.
src/main.rs
0 → 100644
+
91
−
0
View file @
4d3716d3
use
core
::
mem
::{
self
,
MaybeUninit
};
use
core
::
ops
::{
Deref
,
DerefMut
,
Drop
};
use
core
::
cell
::
UnsafeCell
;
/// Internal replacement for `static mut T`
// TODO: Decide name and location.
pub
struct
RacyCell
<
T
>
(
UnsafeCell
<
T
>
);
impl
<
T
>
RacyCell
<
T
>
{
/// Create a RacyCell
pub
const
fn
new
(
value
:
T
)
->
Self
{
RacyCell
(
UnsafeCell
::
new
(
value
))
}
/// Get &mut T
pub
unsafe
fn
get_mut_unchecked
(
&
self
)
->
&
mut
T
{
&
mut
*
self
.0
.get
()
}
}
// The type wrapped need to be Sync for RacyCell<T> to be Sync
//unsafe impl<T> Sync for RacyCell<T> where T: Sync {}
unsafe
impl
<
T
>
Sync
for
RacyCell
<
T
>
{}
struct
Box
<
T
:
'static
+
Sized
,
const
S
:
usize
>
{
// data: &'static mut T,
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
{
mem
::
transmute
(
self
.allocator.data
[
self
.index
]
.get_mut_unchecked
()
.as_ptr
())
}
}
}
impl
<
T
:
'static
,
const
S
:
usize
>
DerefMut
for
Box
<
T
,
S
>
{
// type Target = T;
fn
deref_mut
(
&
mut
self
)
->
&
mut
T
{
unsafe
{
mem
::
transmute
(
self
.allocator.data
[
self
.index
]
.get_mut_unchecked
()
.as_mut_ptr
(),
)
}
}
}
struct
PoolMan
<
T
:
'static
,
const
S
:
usize
>
{
data
:
[
RacyCell
<
MaybeUninit
<
T
>>
;
S
],
}
impl
<
T
,
const
S
:
usize
>
PoolMan
<
T
,
S
>
where
T
:
Sized
,
{
const
Init
:
RacyCell
<
MaybeUninit
<
T
>>
=
RacyCell
::
new
(
MaybeUninit
::
uninit
());
const
fn
new
()
->
Self
{
Self
{
data
:
[
Self
::
Init
;
S
],
}
}
fn
dealloc
(
&
self
,
index
:
usize
)
{
println!
(
"dealloc {}"
,
index
)
}
fn
alloc
(
&
'static
self
)
->
Box
<
T
,
S
>
{
Box
{
index
:
0
,
allocator
:
self
,
}
}
}
static
P
:
PoolMan
<
u32
,
5
>
=
PoolMan
::
new
();
fn
main
()
{
let
mut
e
=
P
.alloc
();
*
e
=
5
;
println!
(
"e {}"
,
*
e
);
}
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