mirror of
https://github.com/apple/swift-nio.git
synced 2026-05-20 20:30:36 +00:00
661bfc5aea
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