Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 Google Inc. All rights reserved. |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 17 | use core::ptr::write_bytes; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 18 | |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 19 | use crate::endian_scalar::emplace_scalar; |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 20 | use crate::primitives::*; |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 21 | |
| 22 | /// VTableWriter compartmentalizes actions needed to create a vtable. |
| 23 | #[derive(Debug)] |
| 24 | pub struct VTableWriter<'a> { |
| 25 | buf: &'a mut [u8], |
| 26 | } |
| 27 | |
| 28 | impl<'a> VTableWriter<'a> { |
| 29 | #[inline(always)] |
| 30 | pub fn init(buf: &'a mut [u8]) -> Self { |
Austin Schuh | 272c613 | 2020-11-14 16:37:52 -0800 | [diff] [blame] | 31 | VTableWriter { buf } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /// Writes the vtable length (in bytes) into the vtable. |
| 35 | /// |
| 36 | /// Note that callers already need to have computed this to initialize |
| 37 | /// a VTableWriter. |
| 38 | /// |
| 39 | /// In debug mode, asserts that the length of the underlying data is equal |
| 40 | /// to the provided value. |
| 41 | #[inline(always)] |
| 42 | pub fn write_vtable_byte_length(&mut self, n: VOffsetT) { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 43 | unsafe { |
| 44 | emplace_scalar::<VOffsetT>(&mut self.buf[..SIZE_VOFFSET], n); |
| 45 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 46 | debug_assert_eq!(n as usize, self.buf.len()); |
| 47 | } |
| 48 | |
| 49 | /// Writes an object length (in bytes) into the vtable. |
| 50 | #[inline(always)] |
| 51 | pub fn write_object_inline_size(&mut self, n: VOffsetT) { |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 52 | unsafe { |
| 53 | emplace_scalar::<VOffsetT>(&mut self.buf[SIZE_VOFFSET..2 * SIZE_VOFFSET], n); |
| 54 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 55 | } |
| 56 | |
| 57 | /// Writes an object field offset into the vtable. |
| 58 | /// |
| 59 | /// Note that this expects field offsets (which are like pointers), not |
| 60 | /// field ids (which are like array indices). |
| 61 | #[inline(always)] |
| 62 | pub fn write_field_offset(&mut self, vtable_offset: VOffsetT, object_data_offset: VOffsetT) { |
| 63 | let idx = vtable_offset as usize; |
James Kuszmaul | 8e62b02 | 2022-03-22 09:33:25 -0700 | [diff] [blame^] | 64 | unsafe { |
| 65 | emplace_scalar::<VOffsetT>(&mut self.buf[idx..idx + SIZE_VOFFSET], object_data_offset); |
| 66 | } |
Austin Schuh | e89fa2d | 2019-08-14 20:24:23 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | /// Clears all data in this VTableWriter. Used to cleanly undo a |
| 70 | /// vtable write. |
| 71 | #[inline(always)] |
| 72 | pub fn clear(&mut self) { |
| 73 | // This is the closest thing to memset in Rust right now. |
| 74 | let len = self.buf.len(); |
| 75 | let p = self.buf.as_mut_ptr() as *mut u8; |
| 76 | unsafe { |
| 77 | write_bytes(p, 0, len); |
| 78 | } |
| 79 | } |
| 80 | } |