Skip to content

Conversation

dallasmarlow
Copy link

@dallasmarlow dallasmarlow commented Jul 30, 2025

feat: Add support for custom external allocators

This pull request adds support for third-party allocators by introducing a new ExternalAllocator trait.

Motivation

The existing Allocator trait requires DerefMut<Target=[u8]>, which is incompatible with certain allocator designs, such as arena allocators that do not expose their memory as a single slice. This change introduces a reallocate/deallocate model to support these use cases.

Implementation

This feature is implemented as a non-breaking addition to the existing allocation system.

  • ExternalAllocator Trait: A new pub unsafe trait ExternalAllocator is defined, which requires reallocate and deallocate methods.
  • AllocatorAdapter Struct: A generic struct that implements the Allocator trait by wrapping a type E that implements ExternalAllocator.
  • It uses static dispatch by being generic over the allocator type, which avoids runtime vtable lookups.
  • A Drop implementation on the adapter calls the deallocate method to ensure managed resources are correctly released.
  • Constructor: The AllocatorAdapter::new_builder function is added to construct a FlatBufferBuilder with a given custom allocator.

Validation

This change is backward-compatible. All existing tests pass, and a new integration test using bumpalo as a dev-dependency has been added to verify the functionality of the ExternalAllocator implementation.

Copy link

google-cla bot commented Jul 30, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

phase 2

phase 3

Update

Update
@dallasmarlow dallasmarlow force-pushed the external-allocators branch from a7b3582 to 6045ee0 Compare July 30, 2025 04:39
@dallasmarlow dallasmarlow marked this pull request as draft July 30, 2025 23:05
@dallasmarlow
Copy link
Author

After a closer look, this PR is not ready for review yet and needs further work. Moved to a draft for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant