Skip to content

ByteBuffer custom allocator support#3526

Open
rnro wants to merge 1 commit intoapple:mainfrom
rnro:expose_bytebuffer_allocator
Open

ByteBuffer custom allocator support#3526
rnro wants to merge 1 commit intoapple:mainfrom
rnro:expose_bytebuffer_allocator

Conversation

@rnro
Copy link
Contributor

@rnro rnro commented Feb 27, 2026

Motivation

  • Other libraries often have byte buffer storage with similar concepts as NIO's ByteBuffer, linear storage with reader and writer indices - it would be nice to have efficient conversion to and from these objects.
  • Efficienct conversion would likely mean taking ownership of the byte storage allocated by the other object and exposing ByteBuffers internals so that they may be owned by another object, there is no API for this currently.

Modifications

  • Added new methods behind CustomByteBufferAllocator SPI.
  • Added public ByteBuffer initializer for adopting externally-allocated memory
  • Made ByteBufferAllocator initializer public with custom malloc/realloc/free/memcpy hooks
  • Updated realloc signature to pass old capacity alongside new capacity to allow implementations to copy over bytes
  • Added withVeryUnsafeMutableBytesWithStorageManagement for mutable storage access
  • Updated all existing tests using internal allocator API to use the new API
  • Added tests for custom allocator functionality

Result

  • Custom allocators can manage ByteBuffer memory with full size information
  • External systems can transfer memory ownership to ByteBuffer
  • System allocator maintains zero overhead due to inlining of the wrapper
  • API (SPI) is extensible for future allocation system integrations
@rnro rnro force-pushed the expose_bytebuffer_allocator branch from 93d5e32 to 661bfc5 Compare February 27, 2026 09:41
Motivation

* Other libraries often have byte buffer storage with similar concepts as
NIO's `ByteBuffer`, linear storage with reader and writer indices - it would
be nice to have efficient conversion to and from these objects.
* Efficienct conversion would likely mean taking ownership of the byte
storage allocated by the other object and exposing `ByteBuffer`s
internals so that they may be owned by another object, there is no API
for this currently.

Modifications

* Added new methods behind `CustomByteBufferAllocator` SPI.
* Added public `ByteBuffer` initializer for adopting externally-allocated memory
* Made `ByteBufferAllocator` initializer public with custom `malloc`/`realloc`/`free`/`memcpy` hooks
* Updated `realloc` signature to pass old capacity alongside new capacity to allow implementations to
copy over bytes
* Added `withVeryUnsafeMutableBytesWithStorageManagement` for mutable storage access
* Updated all existing tests using internal allocator API to use the new API
* Added tests for custom allocator functionality

Result

* Custom allocators can manage `ByteBuffer` memory with full size information
* External systems can transfer memory ownership to `ByteBuffer`
* System allocator maintains zero overhead due to inlining of the wrapper
* API (SPI) is extensible for future allocation system integrations
@rnro rnro force-pushed the expose_bytebuffer_allocator branch from 661bfc5 to 0e0f89e Compare February 27, 2026 09:44
@rnro rnro added the 🆕 semver/minor Adds new public API. label Feb 27, 2026
@rnro rnro marked this pull request as ready for review February 27, 2026 09:53
Copy link
Contributor

@Lukasa Lukasa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🆕 semver/minor Adds new public API.

2 participants