@@ -22,7 +22,7 @@ use core::fmt::{Debug, Display};
2222use core:: iter:: { DoubleEndedIterator , ExactSizeIterator } ;
2323use core:: marker:: PhantomData ;
2424use core:: ops:: { Add , AddAssign , Deref , DerefMut , Index , IndexMut , Sub , SubAssign } ;
25- use core:: ptr:: write_bytes;
25+ use core:: ptr:: { write_bytes, NonNull } ;
2626
2727use crate :: endian_scalar:: emplace_scalar;
2828use 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 ) ]
6083pub struct DefaultAllocator ( Vec < u8 > ) ;
0 commit comments