Skip to content

Commit 972d798

Browse files
committed
Document Allocation
1 parent cb8fa33 commit 972d798

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

src/librustc/mir/interpret/allocation.rs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,8 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
170170

171171
/// Reading and writing
172172
impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
173+
/// Reads bytes until a `0` is encountered. Will error if the end of the allocation is reached
174+
/// before a `0` is found.
173175
pub fn read_c_str(
174176
&self,
175177
cx: &impl HasDataLayout,
@@ -188,14 +190,17 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
188190
}
189191
}
190192

193+
/// Validates that `ptr.offset` and `ptr.offset + size` do not point to the middle of a
194+
/// relocation. If `allow_ptr_and_undef` is `false`, also enforces that the memory in the
195+
/// given range contains neither relocations nor undef bytes.
191196
pub fn check_bytes(
192197
&self,
193198
cx: &impl HasDataLayout,
194199
ptr: Pointer<Tag>,
195200
size: Size,
196201
allow_ptr_and_undef: bool,
197202
) -> EvalResult<'tcx> {
198-
// Check bounds, align and relocations on the edges
203+
// Check bounds and relocations on the edges
199204
self.get_bytes_with_undef_and_ptr(cx, ptr, size)?;
200205
// Check undef and ptr
201206
if !allow_ptr_and_undef {
@@ -205,6 +210,9 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
205210
Ok(())
206211
}
207212

213+
/// Writes `src` to the memory starting at `ptr.offset`.
214+
///
215+
/// Will do bounds checks on the allocation.
208216
pub fn write_bytes(
209217
&mut self,
210218
cx: &impl HasDataLayout,
@@ -216,6 +224,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
216224
Ok(())
217225
}
218226

227+
/// Sets `count` bytes starting at `ptr.offset` with `val`. Basically `memset`.
219228
pub fn write_repeat(
220229
&mut self,
221230
cx: &impl HasDataLayout,
@@ -236,6 +245,8 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
236245
/// * byteorder cannot work with zero element buffers
237246
/// * in oder to obtain a `Pointer` we need to check for ZSTness anyway due to integer pointers
238247
/// being valid for ZSTs
248+
///
249+
/// Note: This function does not do *any* alignment checks, you need to do these before calling
239250
pub fn read_scalar(
240251
&self,
241252
cx: &impl HasDataLayout,
@@ -270,6 +281,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
270281
Ok(ScalarMaybeUndef::Scalar(Scalar::from_uint(bits, size)))
271282
}
272283

284+
/// Note: This function does not do *any* alignment checks, you need to do these before calling
273285
pub fn read_ptr_sized(
274286
&self,
275287
cx: &impl HasDataLayout,
@@ -284,6 +296,8 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
284296
/// * byteorder cannot work with zero element buffers
285297
/// * in oder to obtain a `Pointer` we need to check for ZSTness anyway due to integer pointers
286298
/// being valid for ZSTs
299+
///
300+
/// Note: This function does not do *any* alignment checks, you need to do these before calling
287301
pub fn write_scalar(
288302
&mut self,
289303
cx: &impl HasDataLayout,
@@ -330,6 +344,7 @@ impl<'tcx, Tag: Copy, Extra: AllocationExtra<Tag>> Allocation<Tag, Extra> {
330344
Ok(())
331345
}
332346

347+
/// Note: This function does not do *any* alignment checks, you need to do these before calling
333348
pub fn write_ptr_sized(
334349
&mut self,
335350
cx: &impl HasDataLayout,
@@ -357,7 +372,7 @@ impl<'tcx, Tag: Copy, Extra> Allocation<Tag, Extra> {
357372
self.relocations.range(Size::from_bytes(start)..end)
358373
}
359374

360-
/// Check that there ar eno relocations overlapping with the given range.
375+
/// Check that there are no relocations overlapping with the given range.
361376
#[inline(always)]
362377
fn check_relocations(
363378
&self,

0 commit comments

Comments
 (0)