finefuture opened a new issue, #2805:
URL: https://github.com/apache/fory/issues/2805

   ### Feature Request
   
   Add a local MemoryAllocator to the MemoryBuffer.
   
   ### Is your feature request related to a problem? Please describe
   
   The current scenario is that I use ByteBuf. When the Buffer length is 
insufficient to expand, the grow method of MemoryAllocator will be used. If 
MemoryAllocator is used as a global variable, concurrency problems may occur.
   
   ### Describe the solution you'd like
   
   For example, In codec:
   ```
       @Override
       public void encode(ByteBuf buffer, Object data, Charset charset) throws 
EncodeException {
           int position = buffer.writerIndex();
           MemoryBuffer memoryBuffer = 
MemoryBuffer.fromByteBuffer(buffer.nioBuffer(position, buffer.writableBytes()));
           memoryBuffer.setAllocator(new ByteBufMemoryAllocator(buffer));
           fory.serialize(memoryBuffer, data);
           buffer.writerIndex(memoryBuffer.writerIndex() + position);
       }
   
       private static class ByteBufMemoryAllocator implements MemoryAllocator {
   
           private final ByteBuf targetBuf;
           private final int startIndex;
   
           public ByteBufMemoryAllocator(ByteBuf targetBuf) {
               this.targetBuf = targetBuf;
               this.startIndex = targetBuf.writerIndex();
           }
   
           @Override
           public MemoryBuffer allocate(int initialCapacity) {
               return MemoryBuffer.fromByteArray(new byte[initialCapacity]);
           }
   
           @Override
           public MemoryBuffer grow(MemoryBuffer buffer, int newCapacity) {
               targetBuf.ensureWritable(newCapacity);
               ByteBuffer expandedBB = targetBuf.nioBuffer(startIndex, 
newCapacity);
               buffer.initDirectBuffer(
                       ByteBufferUtil.getAddress(expandedBB) + 
expandedBB.position(), expandedBB.remaining(), expandedBB);
               return MemoryBuffer.fromByteBuffer(expandedBB);
           }
       }
   ```
   In MemoryBuffer:
   ```
        // add localAllocator
       private MemoryAllocator localAllocator;
   
       // add get and set method
       public void setAllocator(MemoryAllocator localAllocator) {
           this.localAllocator = localAllocator;
       }
   
        // localAllocator first
       public MemoryAllocator getAllocator() {
         return localAllocator != null ? localAllocator : globalAllocator;
        }
   
      // use getAllocator() to grow buffer
     public void grow(int neededSize) {
       int length = writerIndex + neededSize;
       if (length > size) {
         getAllocator().grow(this, length);
       }
     }
   
     /** For off-heap buffer, this will make a heap buffer internally. */
     public void ensure(int length) {
       if (length > size) {
         getAllocator().grow(this, length);
       }
     }
   ```
   
   ### Describe alternatives you've considered
   
   _No response_
   
   ### Additional context
   
   _No response_


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to