Q1ngbo commented on code in PR #3062:
URL: https://github.com/apache/brpc/pull/3062#discussion_r2278122506


##########
src/butil/single_iobuf.cpp:
##########
@@ -0,0 +1,279 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+// SingleIOBuf - A continuous zero-copied buffer
+
+#include "butil/logging.h"                  // CHECK, LOG
+#include "butil/iobuf.h"
+#include "butil/iobuf_inl.h"
+#include "butil/single_iobuf.h"
+
+namespace butil {
+
+SingleIOBuf::SingleIOBuf()
+    : _cur_block(NULL)
+    , _block_size(0) {
+    _cur_ref.offset = 0;
+    _cur_ref.length = 0;
+    _cur_ref.block = NULL;
+}
+
+SingleIOBuf::SingleIOBuf(const IOBuf::BlockRef& ref) {
+    _cur_block = NULL;
+    _block_size = 0;
+    if (ref.block) {
+        _cur_ref = ref;
+        _cur_ref.block->inc_ref();
+    }
+}
+
+SingleIOBuf::SingleIOBuf(const SingleIOBuf& other) {
+    _cur_block = NULL;
+    _block_size = 0;
+    if (other._cur_ref.block != NULL) {
+        _cur_ref = other._cur_ref;
+        _cur_ref.block->inc_ref();
+    }
+}
+
+SingleIOBuf::~SingleIOBuf() {
+    reset();
+}
+
+SingleIOBuf& SingleIOBuf::operator=(const SingleIOBuf& rhs) {
+    reset();
+    _block_size = 0;
+    if (rhs._cur_ref.block != NULL) {
+        _cur_ref = rhs._cur_ref;
+        _cur_ref.block->inc_ref();
+    }
+    return *this;
+}
+
+void SingleIOBuf::swap(SingleIOBuf& other) {
+    IOBuf::BlockRef tmp_ref = _cur_ref;
+    _cur_ref = other._cur_ref;
+    other._cur_ref = tmp_ref;
+    IOBuf::Block* tmp_ptr = other._cur_block;
+    other._cur_block = _cur_block;
+    _cur_block = tmp_ptr;
+    uint32_t tmp_size = other._block_size;
+    other._block_size = _block_size;
+    _block_size = tmp_size;
+}
+
+void* SingleIOBuf::allocate(uint32_t size) {
+    IOBuf::Block* b = alloc_block_by_size(size);
+    if (!b) {
+        return NULL;
+    }
+    _cur_ref.offset = b->size;
+    _cur_ref.length = size;
+    _cur_ref.block = b;
+    b->size += size;
+    b->inc_ref();
+    return b->data + _cur_ref.offset;
+}
+
+void SingleIOBuf::deallocate(void* p) {
+    if (_cur_ref.block) {
+        if (_cur_ref.block->data + _cur_ref.offset == (char*)p) { 
+            reset();
+        }
+    }
+}
+
+IOBuf::Block* SingleIOBuf::alloc_block_by_size(uint32_t data_size) {
+    if (_cur_block != NULL) {
+        if (_cur_block->left_space() >= data_size) {
+            return _cur_block;
+        } else {
+            _cur_block->dec_ref();
+            _cur_block = NULL;
+        }
+    }
+    uint32_t total_size = data_size + sizeof(IOBuf::Block);
+    if (total_size <= IOBuf::DEFAULT_BLOCK_SIZE) {
+        _cur_block = iobuf::acquire_tls_block();
+        if (_cur_block != NULL) {
+            if (_cur_block->left_space() >= data_size) {
+                return _cur_block;
+            } else {
+                _cur_block->dec_ref();
+                _cur_block = NULL;
+            }
+        }
+        _cur_block = iobuf::create_block();
+    } else {
+        _cur_block = iobuf::create_block(total_size);
+        _block_size = total_size;
+    }
+    if (BAIDU_UNLIKELY(!_cur_block)) {
+        errno = ENOMEM;
+        _block_size = 0;
+        return NULL;
+    }
+    return _cur_block;
+}
+
+void SingleIOBuf::memcpy_downward(void* old_p, uint32_t old_size,

Review Comment:
   
函数memcpy_downward被reallocate_downward调用,后者是对google/flatbuffers中ReallocateDownward的适配。ReallocateDownward作用是重分配用于序列化的缓冲区,它是一维结构。序列化所使用的flatbuffers
 
vtable元信息被存储在了缓冲区开头;写入数据时是从缓冲区末尾往前写的。因此in_use_back被赋值为了旧缓冲区中已写入数据总大小;in_use_front表示的是元信息占用的内存大小。重申请内存后需要分别拷贝这两部分。下图或许可以更直观地说明这两个变量的作用。
   



-- 
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: dev-unsubscr...@brpc.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@brpc.apache.org
For additional commands, e-mail: dev-h...@brpc.apache.org

Reply via email to