Skip to content

Commit 8ec47f1

Browse files
committed
phase 1
1 parent 518bf42 commit 8ec47f1

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

rust/flatbuffers/src/builder.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use core::fmt::{Debug, Display};
2222
use core::iter::{DoubleEndedIterator, ExactSizeIterator};
2323
use core::marker::PhantomData;
2424
use core::ops::{Add, AddAssign, Deref, DerefMut, Index, IndexMut, Sub, SubAssign};
25-
use core::ptr::write_bytes;
25+
use core::ptr::{write_bytes, NonNull};
2626

2727
use crate::endian_scalar::emplace_scalar;
2828
use crate::primitives::*;
@@ -55,6 +55,29 @@ pub unsafe trait Allocator: DerefMut<Target = [u8]> {
5555
fn len(&self) -> usize;
5656
}
5757

58+
/// A trait for supplying memory to a FlatBufferBuilder from an external source.
59+
///
60+
/// This trait represents a more traditional allocator model, separating the
61+
/// concerns of memory allocation from buffer management. It is designed to be
62+
/// easily implemented by modern, high-performance arena allocators like `bumpalo`.
63+
pub unsafe trait ExternalAllocator {
64+
/// Reallocates a buffer to a new size.
65+
///
66+
/// The implementer is responsible for allocating a new block of memory
67+
/// of at least `new_size` bytes, copying the contents of the `old_buf`,
68+
/// and deallocating the old buffer.
69+
///
70+
/// The returned pointer must point to the new block of memory.
71+
fn reallocate(&mut self, old_buf: NonNull<u8>, old_size: usize, new_size: usize) -> NonNull<u8>;
72+
73+
/// Deallocates a buffer.
74+
///
75+
/// This method is called when the `FlatBufferBuilder` is dropped, giving the
76+
/// allocator a chance to free the memory. For arena allocators, this is
77+
/// typically a no-op.
78+
fn deallocate(&mut self, buf: NonNull<u8>, size: usize);
79+
}
80+
5881
/// Default [`FlatBufferBuilder`] allocator backed by a [`Vec<u8>`].
5982
#[derive(Default)]
6083
pub struct DefaultAllocator(Vec<u8>);

0 commit comments

Comments
 (0)