Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
189 lines
5.7 KiB
189 lines
5.7 KiB
2 years ago
|
// Protocol Buffers - Google's data interchange format
|
||
2 years ago
|
// Copyright 2023 Google Inc. All rights reserved.
|
||
2 years ago
|
// https://developers.google.com/protocol-buffers/
|
||
|
//
|
||
|
// Redistribution and use in source and binary forms, with or without
|
||
|
// modification, are permitted provided that the following conditions are
|
||
|
// met:
|
||
|
//
|
||
|
// * Redistributions of source code must retain the above copyright
|
||
|
// notice, this list of conditions and the following disclaimer.
|
||
|
// * Redistributions in binary form must reproduce the above
|
||
|
// copyright notice, this list of conditions and the following disclaimer
|
||
|
// in the documentation and/or other materials provided with the
|
||
|
// distribution.
|
||
|
// * Neither the name of Google Inc. nor the names of its
|
||
|
// contributors may be used to endorse or promote products derived from
|
||
|
// this software without specific prior written permission.
|
||
|
//
|
||
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
|
|
||
2 years ago
|
//! UPB FFI wrapper code for use by Rust Protobuf.
|
||
2 years ago
|
|
||
2 years ago
|
use std::alloc;
|
||
|
use std::alloc::Layout;
|
||
|
use std::cell::UnsafeCell;
|
||
1 year ago
|
use std::fmt;
|
||
2 years ago
|
use std::marker::PhantomData;
|
||
|
use std::mem::MaybeUninit;
|
||
1 year ago
|
use std::ops::Deref;
|
||
|
use std::ptr::NonNull;
|
||
|
use std::slice;
|
||
|
|
||
|
/// See `upb/port/def.inc`.
|
||
|
const UPB_MALLOC_ALIGN: usize = 8;
|
||
|
|
||
|
/// A UPB-managed pointer to a raw arena.
|
||
|
pub type RawArena = NonNull<RawArenaData>;
|
||
|
|
||
|
/// The data behind a [`RawArena`]. Do not use this type.
|
||
|
#[repr(C)]
|
||
|
pub struct RawArenaData {
|
||
|
_data: [u8; 0],
|
||
|
}
|
||
|
|
||
|
/// A wrapper over a `upb_Arena`.
|
||
|
///
|
||
|
/// This is not a safe wrapper per se, because the allocation functions still
|
||
|
/// have sharp edges (see their safety docs for more info).
|
||
|
///
|
||
|
/// This is an owning type and will automatically free the arena when
|
||
|
/// dropped.
|
||
|
///
|
||
|
/// Note that this type is neither `Sync` nor `Send`.
|
||
|
pub struct Arena {
|
||
|
raw: RawArena,
|
||
|
_not_sync: PhantomData<UnsafeCell<()>>,
|
||
|
}
|
||
|
|
||
|
extern "C" {
|
||
|
fn upb_Arena_New() -> RawArena;
|
||
|
fn upb_Arena_Free(arena: RawArena);
|
||
|
fn upb_Arena_Malloc(arena: RawArena, size: usize) -> *mut u8;
|
||
|
fn upb_Arena_Realloc(arena: RawArena, ptr: *mut u8, old: usize, new: usize) -> *mut u8;
|
||
|
}
|
||
|
|
||
|
impl Arena {
|
||
|
/// Allocates a fresh arena.
|
||
|
#[inline]
|
||
|
pub fn new() -> Self {
|
||
|
Self { raw: unsafe { upb_Arena_New() }, _not_sync: PhantomData }
|
||
|
}
|
||
|
|
||
|
/// Returns the raw, UPB-managed pointer to the arena.
|
||
|
#[inline]
|
||
|
pub fn raw(&self) -> RawArena {
|
||
|
self.raw
|
||
|
}
|
||
|
|
||
|
/// Allocates some memory on the arena.
|
||
|
///
|
||
|
/// # Safety
|
||
|
///
|
||
|
/// `layout`'s alignment must be less than `UPB_MALLOC_ALIGN`.
|
||
|
#[inline]
|
||
|
pub unsafe fn alloc(&self, layout: Layout) -> &mut [MaybeUninit<u8>] {
|
||
|
debug_assert!(layout.align() <= UPB_MALLOC_ALIGN);
|
||
|
let ptr = upb_Arena_Malloc(self.raw, layout.size());
|
||
|
if ptr.is_null() {
|
||
|
alloc::handle_alloc_error(layout);
|
||
|
}
|
||
|
|
||
|
slice::from_raw_parts_mut(ptr.cast(), layout.size())
|
||
|
}
|
||
|
|
||
|
/// Resizes some memory on the arena.
|
||
|
///
|
||
|
/// # Safety
|
||
|
///
|
||
|
/// After calling this function, `ptr` is essentially zapped. `old` must
|
||
|
/// be the layout `ptr` was allocated with via [`Arena::alloc()`]. `new`'s
|
||
|
/// alignment must be less than `UPB_MALLOC_ALIGN`.
|
||
|
#[inline]
|
||
|
pub unsafe fn resize(&self, ptr: *mut u8, old: Layout, new: Layout) -> &[MaybeUninit<u8>] {
|
||
|
debug_assert!(new.align() <= UPB_MALLOC_ALIGN);
|
||
|
let ptr = upb_Arena_Realloc(self.raw, ptr, old.size(), new.size());
|
||
|
if ptr.is_null() {
|
||
|
alloc::handle_alloc_error(new);
|
||
|
}
|
||
|
|
||
|
slice::from_raw_parts_mut(ptr.cast(), new.size())
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl Drop for Arena {
|
||
|
#[inline]
|
||
|
fn drop(&mut self) {
|
||
|
unsafe {
|
||
|
upb_Arena_Free(self.raw);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
/// Represents serialized Protobuf wire format data.
|
||
|
///
|
||
|
/// It's typically produced by `<Message>::serialize()`.
|
||
2 years ago
|
pub struct SerializedData {
|
||
|
data: NonNull<u8>,
|
||
|
len: usize,
|
||
2 years ago
|
|
||
|
// The arena that owns `data`.
|
||
1 year ago
|
_arena: Arena,
|
||
2 years ago
|
}
|
||
|
|
||
|
impl SerializedData {
|
||
1 year ago
|
pub unsafe fn from_raw_parts(arena: Arena, data: NonNull<u8>, len: usize) -> Self {
|
||
2 years ago
|
SerializedData { _arena: arena, data, len }
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
|
impl Deref for SerializedData {
|
||
|
type Target = [u8];
|
||
|
fn deref(&self) -> &Self::Target {
|
||
|
unsafe { slice::from_raw_parts(self.data.as_ptr() as *const _, self.len) }
|
||
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
impl fmt::Debug for SerializedData {
|
||
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||
|
fmt::Debug::fmt(self.deref(), f)
|
||
2 years ago
|
}
|
||
|
}
|
||
|
|
||
2 years ago
|
#[cfg(test)]
|
||
|
mod tests {
|
||
|
use super::*;
|
||
|
|
||
|
#[test]
|
||
|
fn test_arena_new_and_free() {
|
||
2 years ago
|
let arena = Arena::new();
|
||
|
drop(arena);
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
#[test]
|
||
|
fn test_serialized_data_roundtrip() {
|
||
2 years ago
|
let arena = Arena::new();
|
||
2 years ago
|
let original_data = b"Hello world";
|
||
|
let len = original_data.len();
|
||
|
|
||
|
let serialized_data = unsafe {
|
||
|
SerializedData::from_raw_parts(
|
||
|
arena,
|
||
|
NonNull::new(original_data as *const _ as *mut _).unwrap(),
|
||
|
len,
|
||
|
)
|
||
|
};
|
||
|
assert_eq!(&*serialized_data, b"Hello world");
|
||
|
}
|
||
2 years ago
|
}
|