yanglimingcn commented on code in PR #2942:
URL: https://github.com/apache/brpc/pull/2942#discussion_r2031323015


##########
src/butil/iobuf.cpp:
##########
@@ -1371,6 +1371,41 @@ IOBuf::Area IOBuf::reserve(size_t count) {
     return result;
 }
 
+IOBuf::Area IOBuf::reserve_aligned(size_t n, size_t alignment) {
+    size_t count = (n + alignment - 1) & ~(alignment - 1);
+    IOBuf::Area result = INVALID_AREA;
+    size_t total_nc = 0;
+    while (total_nc < count) {  // excluded count == 0
+        IOBuf::Block* b = iobuf::share_tls_block();
+        CHECK(alignment <= b->cap)
+            << "alignment must not greater than block capacity";
+        if (BAIDU_UNLIKELY(!b)) {
+            return INVALID_AREA;
+        }
+        const size_t remainder =
+            reinterpret_cast<uintptr_t>(b->data + b->size) % alignment;
+        const size_t align_offset =
+            (remainder != 0) ? alignment - remainder : 0;
+        if (b->left_space() - align_offset < alignment) {
+            b->size = b->cap;
+            continue;

Review Comment:
   这里要有个前提,要保证一个空block中一定包含一个alignment内存对齐的段。
   1,可以设置分配block的内存分配器为alignment对齐,内存大小为alignment的2倍(因为block header占用了一部分内存)。
   2,block的默认大小为alignment的3倍。
   还有别的建议吗?



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