Comments (2)
Note the following code:
/// Gives the offset in units of T (as if the pointer started at an array of T) that the slice actually starts at.
#[inline(always)]
fn offset() -> usize {
// We need to first compute the first location we can start in align units.
// Then we go from align units to offset units using mem::align_of::<T>() / mem::size_of::<T>().
(mem::size_of::<HeaderVecHeader<H>>() + mem::size_of::<T>() - 1) / mem::size_of::<T>()
}
/// Compute the number of elements (in units of T) to allocate for a given capacity.
#[inline(always)]
fn elems_to_mem_elems(capacity: usize) -> usize {
Self::offset() + capacity
}
/// Compute the number of elements (in units of T) to allocate for a given capacity.
#[inline(always)]
fn elems_to_mem_bytes(capacity: usize) -> usize {
Self::elems_to_mem_elems(capacity) * mem::size_of::<T>()
}
/// Compute the number of elements (in units of T) to allocate for a given capacity.
#[inline(always)]
fn layout(capacity: usize) -> alloc::alloc::Layout {
alloc::alloc::Layout::from_size_align(
Self::elems_to_mem_bytes(capacity),
cmp::max(mem::align_of::<H>(), mem::align_of::<T>()),
)
.expect("unable to produce memory layout with Hrc key type (is it a zero sized type? they are not permitted)")
}
This is where from_size_align
is used. It is passed Self::elems_to_mem_bytes(capacity)
.
Self::elems_to_mem_bytes(capacity)
returns Self::elems_to_mem_elems(capacity) * mem::size_of::<T>()
.
Self::elems_to_mem_elems(capacity)
returns Self::offset() + capacity
Self::offset()
returns (mem::size_of::<HeaderVecHeader<H>>() + mem::size_of::<T>() - 1) / mem::size_of::<T>()
Self::offset()
gets the first offset in size units of T
. Basically, it reserves space for the header by reserving several T
-sized units at the beginning.
Self::elems_to_mem_elems(capacity)
then returns the offset (in your example 4
) plus the capacity 1
, so it returns 5
.
Self::elems_to_mem_bytes(capacity)
then multiplies 5
by 1
(the number of bytes per T
)
So ultimately, you will get:
alloc::alloc::Layout::from_size_align(
5,
4,
)
Let me know if this is incorrect though, but I believe this should be fine.
from header-vec.
Ah, sorry about that. I will try to read more thoroughly in future. (I think I sort of assumed some vague combination of some vague readings that either elems_to_mem_elems wasn't internal or just did what I guessed it would do)
from header-vec.
Related Issues (6)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from header-vec.