dep-protobuf/rust/cpp_kernel/rust_alloc_for_cpp_api.rs
Protobuf Team Bot a9bc366522 Stop using double underscores for our C function names and standardize on the 'rust_proto_' prefix.
Besides unnecessary inconsistency on our C symbols, double underscores anywhere in the name are reserved for stdlib use.  In practice its unlikely these symbols would ever hit a collision problem (maybe the prior name 'utf8_debug_string' with no prefix as having some risk), but safer to just standardize on this and have no concerns going forward.

PiperOrigin-RevId: 648709299
2024-07-02 07:19:11 -07:00

20 lines
732 B
Rust

// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
use std::alloc::{alloc, Layout};
#[no_mangle]
extern "C" fn rust_proto_alloc(size: usize, align: usize) -> *mut u8 {
if size == 0 {
// A 0-sized layout is legal but the global allocator isn't required to support
// it so return a dangling pointer instead.
std::ptr::null_mut::<u8>().wrapping_add(align)
} else {
let layout = Layout::from_size_align(size, align).unwrap();
unsafe { alloc(layout) }
}
}