This commit is contained in:
nora 2021-12-20 21:54:10 +01:00
parent 32c3eef960
commit ef6c357b27
3 changed files with 163 additions and 40 deletions

View file

@ -1,28 +1,30 @@
A `Vec<T: ?Sized>`
# A `Vec<T: ?Sized>`
It's implemented by laying out the elements in memory contiguously like `alloc::vec::Vec`
It's implemented by laying out the elements in memory contiguously like [`alloc::vec::Vec`]
# Layout
A `Vechonk` is 3 `usize` long. It owns a single allocation, containing the elements and the metadata.
A [`Vechonk`] is 3 `usize` long. It owns a single allocation, containing the elements and the metadata.
The elements are laid out contiguously from the front, while the metadata is laid out contiguously from the back.
Both grow towards the center until they meet and get realloced to separate them again.
```txt
Vechonk<str>
------------------------
| ptr | len | cap |
---|---------------------
|
|___
|
Heap v
------------------------------------------------------------------------
| "hello" | "uwu" | <uninit> | 0 - 5 | 5 - 3 |
|-----------|----------|-----------------|--------------|--------------|
| dynamic | dynamic | rest of alloc | usize + meta | usize + meta |
--------------------------------------------|--------------|------------
^ ^ | |
|___________ | _________________________| |
|_________________________________________|
Vechonk<str>
------------------------------------
| ptr | len | cap | elem_size |
---|---------------|--------|-------
| | |
| |_______ | ______________________________________
| | |
| _________| |
Heap v v v
-----------------------|-----------------------------------------------
value | "hello" | "uwu" | <uninit> | 0 - 5 | 5 - 3 |
|------------|---------|-----------------|--------------|--------------|
size | dynamic | dynamic | rest of alloc | usize + meta | usize + meta |
--------------------------------------------|--------------|------------
^ ^ | |
|___________ | _________________________| |
|_________________________________________|
```