Skip to content

Commit e58601a

Browse files
committed
Auto merge of #26955 - Gankro:raw-vec, r=bluss,alexcrichton
Per the top level comment: A low-level utility for more ergonomically allocating, reallocating, and deallocating a a buffer of memory on the heap without having to worry about all the corner cases involved. This type is excellent for building your own data structures like Vec and VecDeque. In particular: * Produces heap::EMPTY on zero-sized types * Produces heap::EMPTY on zero-length allocations * Catches all overflows in capacity computations (promotes them to "capacity overflow" panics) * Guards against 32-bit systems allocating more than isize::MAX bytes * Guards against overflowing your length * Aborts on OOM * Avoids freeing heap::EMPTY * Contains a ptr::Unique and thus endows the user with all related benefits This type does not in anyway inspect the memory that it manages. When dropped it *will* free its memory, but it *won't* try to Drop its contents. It is up to the user of RawVec to handle the actual things *stored* inside of a RawVec. Note that a RawVec always forces its capacity to be usize::MAX for zero-sized types. This enables you to use capacity growing logic catch the overflows in your length that might occur with zero-sized types. However this means that you need to be careful when roundtripping this type with a `Box<[T]>`: `cap()` won't yield the len. However `with_capacity`, `shrink_to_fit`, and `from_box` will actually set RawVec's private capacity field. This allows zero-sized types to not be special-cased by consumers of this type. Edit: fixes #18726 and fixes #23842
2 parents 5df259b + b0ee1eb commit e58601a

File tree

10 files changed

+638
-484
lines changed

10 files changed

+638
-484
lines changed

src/etc/debugger_pretty_printers_common.py

+7-9
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,10 @@
5555
SLICE_FIELD_NAMES = [SLICE_FIELD_NAME_DATA_PTR, SLICE_FIELD_NAME_LENGTH]
5656

5757
# std::Vec<> related constants
58-
STD_VEC_FIELD_NAME_DATA_PTR = "ptr"
5958
STD_VEC_FIELD_NAME_LENGTH = "len"
60-
STD_VEC_FIELD_NAME_CAPACITY = "cap"
61-
STD_VEC_FIELD_NAMES = [STD_VEC_FIELD_NAME_DATA_PTR,
62-
STD_VEC_FIELD_NAME_LENGTH,
63-
STD_VEC_FIELD_NAME_CAPACITY]
59+
STD_VEC_FIELD_NAME_BUF = "buf"
60+
STD_VEC_FIELD_NAMES = [STD_VEC_FIELD_NAME_BUF,
61+
STD_VEC_FIELD_NAME_LENGTH]
6462

6563
# std::String related constants
6664
STD_STRING_FIELD_NAMES = ["vec"]
@@ -302,13 +300,13 @@ def get_discriminant_value_as_integer(enum_val):
302300
def extract_length_ptr_and_cap_from_std_vec(vec_val):
303301
assert vec_val.type.get_type_kind() == TYPE_KIND_STD_VEC
304302
length_field_index = STD_VEC_FIELD_NAMES.index(STD_VEC_FIELD_NAME_LENGTH)
305-
ptr_field_index = STD_VEC_FIELD_NAMES.index(STD_VEC_FIELD_NAME_DATA_PTR)
306-
cap_field_index = STD_VEC_FIELD_NAMES.index(STD_VEC_FIELD_NAME_CAPACITY)
303+
buf_field_index = STD_VEC_FIELD_NAMES.index(STD_VEC_FIELD_NAME_BUF)
307304

308305
length = vec_val.get_child_at_index(length_field_index).as_integer()
309-
vec_ptr_val = vec_val.get_child_at_index(ptr_field_index)
310-
capacity = vec_val.get_child_at_index(cap_field_index).as_integer()
306+
buf = vec_val.get_child_at_index(buf_field_index)
311307

308+
vec_ptr_val = buf.get_child_at_index(0)
309+
capacity = buf.get_child_at_index(1).as_integer()
312310
unique_ptr_val = vec_ptr_val.get_child_at_index(0)
313311
data_ptr = unique_ptr_val.get_child_at_index(0)
314312
assert data_ptr.type.get_dwarf_type_kind() == DWARF_TYPE_CODE_PTR

src/liballoc/boxed.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use core::hash::{self, Hash};
6262
use core::marker::Unsize;
6363
use core::mem;
6464
use core::ops::{CoerceUnsized, Deref, DerefMut};
65-
use core::ptr::{Unique};
65+
use core::ptr::Unique;
6666
use core::raw::{TraitObject};
6767

6868
/// A value that represents the heap. This is the default place that the `box`

src/liballoc/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@
8888
#![feature(unique)]
8989
#![feature(unsafe_no_drop_flag, filling_drop)]
9090
#![feature(unsize)]
91+
#![feature(core_slice_ext)]
9192

9293
#![cfg_attr(test, feature(test, alloc, rustc_private, box_raw))]
9394
#![cfg_attr(all(not(feature = "external_funcs"), not(feature = "external_crate")),
@@ -122,6 +123,7 @@ mod boxed { pub use std::boxed::{Box, HEAP}; }
122123
mod boxed_test;
123124
pub mod arc;
124125
pub mod rc;
126+
pub mod raw_vec;
125127

126128
/// Common out-of-memory routine
127129
#[cold]

0 commit comments

Comments
 (0)