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


##########
src/butil/iobuf.h:
##########
@@ -545,6 +547,47 @@ class IOBufCutter {
     IOBuf* _buf;
 };
 
+// SingleIOBuf is a lightweight buffer that manages a single IOBuf::Block.
+// It always ensures that the underlying memory is contiguous and 
+// avoids unnecessary memory copies through.

Review Comment:
   The comment has an incomplete sentence ending with 'through.' - it should 
either complete the thought or remove the trailing word.
   ```suggestion
   // avoids unnecessary memory copies through contiguous memory management.
   ```



##########
src/butil/iobuf.cpp:
##########
@@ -1976,6 +1976,259 @@ size_t IOBufCutter::cutn(void* out, size_t n) {
     }
 }
 
+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::alloc(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,
+                    void* new_p, uint32_t new_size,
+                    uint32_t in_use_back, uint32_t in_use_front) {
+    memcpy((u_char*)new_p + new_size - in_use_back, (u_char*)old_p + old_size 
- in_use_back,
+            in_use_back);
+    memcpy(new_p, old_p, in_use_front);
+}
+
+void* SingleIOBuf::reallocate_downward(uint32_t new_size, uint32_t in_use_back,
+                                        uint32_t in_use_front) {
+    IOBuf::BlockRef& ref = _cur_ref;
+    if (BAIDU_UNLIKELY(new_size <= ref.length)) {
+        LOG(ERROR) << "invaild new size:" << new_size;

Review Comment:
   Typo in error message: 'invaild' should be 'invalid'.
   ```suggestion
           LOG(ERROR) << "invalid new size:" << new_size;
   ```



##########
src/butil/iobuf.cpp:
##########
@@ -1976,6 +1976,259 @@ size_t IOBufCutter::cutn(void* out, size_t n) {
     }
 }
 
+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::alloc(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,
+                    void* new_p, uint32_t new_size,
+                    uint32_t in_use_back, uint32_t in_use_front) {
+    memcpy((u_char*)new_p + new_size - in_use_back, (u_char*)old_p + old_size 
- in_use_back,
+            in_use_back);
+    memcpy(new_p, old_p, in_use_front);
+}
+
+void* SingleIOBuf::reallocate_downward(uint32_t new_size, uint32_t in_use_back,
+                                        uint32_t in_use_front) {
+    IOBuf::BlockRef& ref = _cur_ref;
+    if (BAIDU_UNLIKELY(new_size <= ref.length)) {
+        LOG(ERROR) << "invaild new size:" << new_size;
+        errno = EINVAL;
+        return NULL;
+    }
+    if (BAIDU_UNLIKELY(ref.block == NULL)) {
+        LOG(ERROR) << "SingleIOBuf reallocate_downward failed. Block cannot be 
null!";
+        errno = EINVAL;
+        return NULL;
+    }
+    char* old_p = ref.block->data + ref.offset;
+    uint32_t old_size = ref.length;
+    IOBuf::Block* b = alloc_block_by_size(new_size);
+    if (!b) {
+        return NULL;
+    }
+    char* new_p = b->data + b->size;
+    memcpy_downward(old_p, old_size,
+                    new_p, new_size,
+                    in_use_back, in_use_front);
+    ref.block->dec_ref();
+    _cur_ref.offset = b->size;
+    _cur_ref.length = new_size;
+    _cur_ref.block = b;
+    b->size += new_size;
+    b->inc_ref();
+    return new_p;
+}
+
+const void* SingleIOBuf::get_begin() const {
+    if (_cur_ref.block) {
+        return _cur_ref.block->data + _cur_ref.offset;
+    }
+    return NULL;
+}
+
+uint32_t SingleIOBuf::get_length() const {
+    return _cur_ref.length;
+}
+
+void SingleIOBuf::reset() {
+    if (_cur_block) {
+        if (_block_size == 0) {
+            iobuf::release_tls_block(_cur_block);
+        } else {
+            _cur_block->dec_ref();
+            _block_size = 0;
+        }
+        _cur_block = NULL;
+    }
+    if (_cur_ref.block != NULL) {
+        _cur_ref.block->dec_ref();
+    }
+    _cur_ref.offset = 0;
+    _cur_ref.length = 0;
+    _cur_ref.block = NULL;
+}
+
+void SingleIOBuf::target_block_inc_ref(void* b) {
+    IOBuf::Block* block = (IOBuf::Block*)b;
+    block->inc_ref();
+}
+
+void SingleIOBuf::target_block_dec_ref(void* b) {
+    IOBuf::Block* block = (IOBuf::Block*)b;
+    block->dec_ref();
+}
+
+bool SingleIOBuf::assign(const IOBuf& buf, uint32_t msg_size) {
+    if (BAIDU_UNLIKELY(buf.length() < msg_size)) {
+        LOG(ERROR) << "Fail to dump_mult_iobuf msg_size:" << msg_size
+            << " from source iobuf of size:" << buf.length();
+        return false;
+    }
+    reset();
+    if (BAIDU_UNLIKELY(msg_size == 0)) {
+        return true;
+    }
+    const IOBuf::BlockRef& ref = buf._front_ref();
+    if (ref.length >= msg_size) {
+        _cur_ref.offset = ref.offset;
+        _cur_ref.length = msg_size;
+        _cur_ref.block = ref.block;
+        _cur_ref.block->inc_ref();
+        return true;
+    } else {
+        IOBuf::Block* b = alloc_block_by_size(msg_size);
+        if (!b) {
+            return false;
+        }
+        char* out = b->data + b->size;
+        const size_t nref = buf.backing_block_num();
+        uint32_t last_len = msg_size;
+        for (size_t i = 0; i < nref && last_len > 0; ++i) {
+            const IOBuf::BlockRef& r = buf._ref_at(i);
+            uint32_t n = std::min(r.length, last_len);
+            iobuf::cp(out, r.block->data + r.offset, n);
+            last_len -= n;
+            out += n;
+        }
+        _cur_ref.offset = b->size;
+        _cur_ref.length = msg_size;
+        _cur_ref.block = b;
+        b->size += msg_size;
+        _cur_ref.block->inc_ref();
+        return true;
+    }
+    return false;

Review Comment:
   This return statement is unreachable code. The function will always return 
from either the true or false branch of the preceding if-else statement.
   ```suggestion
   
   ```



##########
test/iobuf_unittest.cpp:
##########
@@ -1890,4 +1890,45 @@ TEST_F(IOBufTest, reserve_aligned) {
     }
 }
 
+TEST_F(IOBufTest, single_iobuf) {
+    butil::IOBuf buf1;
+    char *usr_str = (char *)malloc(16);

Review Comment:
   Memory allocated with malloc() is not freed in this test, causing a memory 
leak. Consider using RAII or explicitly calling free().



##########
src/butil/iobuf.cpp:
##########
@@ -1976,6 +1976,259 @@ size_t IOBufCutter::cutn(void* out, size_t n) {
     }
 }
 
+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::alloc(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) { 

Review Comment:
   [nitpick] The deallocate method only resets if the pointer matches exactly 
the beginning of the allocated region. This seems overly restrictive and may 
not handle all valid deallocation scenarios correctly.
   ```suggestion
           char* start = _cur_ref.block->data + _cur_ref.offset;
           char* end = start + _cur_ref.length;
           char* ptr = static_cast<char*>(p);
           if (ptr >= start && ptr < end) {
   ```



-- 
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