1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
use crate::{
    ser::{ScratchSpace, Serializer},
    Fallible,
};
use core::{
    alloc::Layout,
    fmt,
    ops::DerefMut,
    ptr::{copy_nonoverlapping, NonNull},
};

/// The error type returned by an [`BufferSerializer`].
#[derive(Debug)]
pub enum BufferSerializerError {
    /// Writing has overflowed the internal buffer.
    Overflow {
        /// The position of the serializer
        pos: usize,
        /// The number of bytes needed
        bytes_needed: usize,
        /// The total length of the archive
        archive_len: usize,
    },
}

impl fmt::Display for BufferSerializerError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Overflow {
                pos,
                bytes_needed,
                archive_len,
            } => write!(
                f,
                "writing has overflowed the serializer buffer: pos {}, needed {}, total length {}",
                pos, bytes_needed, archive_len
            ),
        }
    }
}

#[cfg(feature = "std")]
const _: () = {
    use std::error::Error;

    impl Error for BufferSerializerError {}
};

/// Wraps a byte buffer and equips it with [`Serializer`].
///
/// Common uses include archiving in `#![no_std]` environments and archiving small objects without
/// allocating.
///
/// # Examples
/// ```
/// use rkyv::{
///     archived_value,
///     ser::{Serializer, serializers::BufferSerializer},
///     AlignedBytes,
///     AlignedVec,
///     Archive,
///     Archived,
///     Serialize,
/// };
///
/// #[derive(Archive, Serialize)]
/// enum Event {
///     Spawn,
///     Speak(String),
///     Die,
/// }
///
/// let mut serializer = BufferSerializer::new(AlignedBytes([0u8; 256]));
/// let pos = serializer.serialize_value(&Event::Speak("Help me!".to_string()))
///     .expect("failed to archive event");
/// let buf = serializer.into_inner();
/// let archived = unsafe { archived_value::<Event>(buf.as_ref(), pos) };
/// if let Archived::<Event>::Speak(message) = archived {
///     assert_eq!(message.as_str(), "Help me!");
/// } else {
///     panic!("archived event was of the wrong type");
/// }
/// ```
pub struct BufferSerializer<T> {
    inner: T,
    pos: usize,
}

impl<T> BufferSerializer<T> {
    /// Creates a new archive buffer from a byte buffer.
    #[inline]
    pub fn new(inner: T) -> Self {
        Self::with_pos(inner, 0)
    }

    /// Creates a new archive buffer from a byte buffer. The buffer will start writing at the given
    /// position, but the buffer must contain all bytes (otherwise the alignments of types may not
    /// be correct).
    #[inline]
    pub fn with_pos(inner: T, pos: usize) -> Self {
        Self { inner, pos }
    }

    /// Consumes the serializer and returns the underlying type.
    #[inline]
    pub fn into_inner(self) -> T {
        self.inner
    }
}

impl<T: Default> Default for BufferSerializer<T> {
    #[inline]
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl<T> Fallible for BufferSerializer<T> {
    type Error = BufferSerializerError;
}

impl<T: AsMut<[u8]>> Serializer for BufferSerializer<T> {
    #[inline]
    fn pos(&self) -> usize {
        self.pos
    }

    fn write(&mut self, bytes: &[u8]) -> Result<(), Self::Error> {
        let end_pos = self.pos + bytes.len();
        let archive_len = self.inner.as_mut().len();
        if end_pos > archive_len {
            Err(BufferSerializerError::Overflow {
                pos: self.pos,
                bytes_needed: bytes.len(),
                archive_len,
            })
        } else {
            unsafe {
                copy_nonoverlapping(
                    bytes.as_ptr(),
                    self.inner.as_mut().as_mut_ptr().add(self.pos),
                    bytes.len(),
                );
            }
            self.pos = end_pos;
            Ok(())
        }
    }

    fn pad(&mut self, padding: usize) -> Result<(), Self::Error> {
        let end_pos = self.pos + padding;
        let archive_len = self.inner.as_mut().len();
        if end_pos > archive_len {
            Err(BufferSerializerError::Overflow {
                pos: self.pos,
                bytes_needed: padding,
                archive_len,
            })
        } else {
            self.pos = end_pos;
            Ok(())
        }
    }
}

/// Errors that can occur when using a fixed-size allocator.
///
/// Pairing a fixed-size allocator with a fallback allocator can help prevent running out of scratch
/// space unexpectedly.
#[derive(Debug)]
pub enum FixedSizeScratchError {
    /// The allocator ran out of scratch space.
    OutOfScratch(Layout),
    /// Scratch space was not popped in reverse order.
    NotPoppedInReverseOrder {
        /// The current position of the start of free memory
        pos: usize,
        /// The next position according to the erroneous pop
        next_pos: usize,
        /// The size of the memory according to the erroneous pop
        next_size: usize,
    },
    /// The given allocation did not belong to the scratch allocator.
    UnownedAllocation,
}

impl fmt::Display for FixedSizeScratchError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::OutOfScratch(layout) => write!(
                f,
                "out of scratch: requested scratch space with size {} and align {}",
                layout.size(),
                layout.align()
            ),
            Self::NotPoppedInReverseOrder {
                pos,
                next_pos,
                next_size,
            } => write!(
                f,
                "scratch space was not popped in reverse order: pos {}, next pos {}, next size {}",
                pos, next_pos, next_size
            ),
            Self::UnownedAllocation => write!(f, "unowned allocation"),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for FixedSizeScratchError {}

/// Scratch space that allocates within a buffer.
pub struct BufferScratch<T> {
    buffer: T,
    pos: usize,
}

impl<T> BufferScratch<T> {
    /// Creates a new buffer scratch allocator.
    pub fn new(buffer: T) -> Self {
        Self { buffer, pos: 0 }
    }

    /// Resets the scratch space to its initial state.
    pub fn clear(&mut self) {
        self.pos = 0;
    }

    /// Consumes the buffer scratch allocator, returning the underlying buffer.
    pub fn into_inner(self) -> T {
        self.buffer
    }
}

impl<T: Default> Default for BufferScratch<T> {
    fn default() -> Self {
        Self::new(T::default())
    }
}

impl<T> Fallible for BufferScratch<T> {
    type Error = FixedSizeScratchError;
}

impl<T: DerefMut<Target = U>, U: AsMut<[u8]>> ScratchSpace for BufferScratch<T> {
    #[inline]
    unsafe fn push_scratch(&mut self, layout: Layout) -> Result<NonNull<[u8]>, Self::Error> {
        let bytes = self.buffer.as_mut();

        let start = bytes.as_ptr().add(self.pos);
        let pad = match (start as usize) & (layout.align() - 1) {
            0 => 0,
            x => layout.align() - x,
        };
        if pad + layout.size() <= bytes.len() - self.pos {
            self.pos += pad;
            let result_slice = ptr_meta::from_raw_parts_mut(
                bytes.as_mut_ptr().add(self.pos).cast(),
                layout.size(),
            );
            let result = NonNull::new_unchecked(result_slice);
            self.pos += layout.size();
            Ok(result)
        } else {
            Err(FixedSizeScratchError::OutOfScratch(layout))
        }
    }

    #[inline]
    unsafe fn pop_scratch(&mut self, ptr: NonNull<u8>, layout: Layout) -> Result<(), Self::Error> {
        let bytes = self.buffer.as_mut();

        let ptr = ptr.as_ptr();
        if ptr >= bytes.as_mut_ptr() && ptr < bytes.as_mut_ptr().add(bytes.len()) {
            let next_pos = ptr.offset_from(bytes.as_ptr()) as usize;
            if next_pos + layout.size() <= self.pos {
                self.pos = next_pos;
                Ok(())
            } else {
                Err(FixedSizeScratchError::NotPoppedInReverseOrder {
                    pos: self.pos,
                    next_pos,
                    next_size: layout.size(),
                })
            }
        } else {
            Err(FixedSizeScratchError::UnownedAllocation)
        }
    }
}

/// Allocates scratch space with a main and backup scratch.
pub struct FallbackScratch<M, F> {
    main: M,
    fallback: F,
}

impl<M, F> FallbackScratch<M, F> {
    /// Creates fallback scratch from a main and backup scratch.
    pub fn new(main: M, fallback: F) -> Self {
        Self { main, fallback }
    }
}

impl<M: Default, F: Default> Default for FallbackScratch<M, F> {
    fn default() -> Self {
        Self {
            main: M::default(),
            fallback: F::default(),
        }
    }
}

impl<M, F: Fallible> Fallible for FallbackScratch<M, F> {
    type Error = F::Error;
}

impl<M: ScratchSpace, F: ScratchSpace> ScratchSpace for FallbackScratch<M, F> {
    #[inline]
    unsafe fn push_scratch(&mut self, layout: Layout) -> Result<NonNull<[u8]>, Self::Error> {
        self.main
            .push_scratch(layout)
            .or_else(|_| self.fallback.push_scratch(layout))
    }

    #[inline]
    unsafe fn pop_scratch(&mut self, ptr: NonNull<u8>, layout: Layout) -> Result<(), Self::Error> {
        self.main
            .pop_scratch(ptr, layout)
            .or_else(|_| self.fallback.pop_scratch(ptr, layout))
    }
}