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


##########
src/butil/iobuf.cpp:
##########
@@ -1371,6 +1371,50 @@ IOBuf::Area IOBuf::reserve(size_t count) {
     return result;
 }
 
+IOBuf::Area IOBuf::reserve_aligned(size_t n, size_t alignment) {
+    bool is_power_two = alignment > 0 && (alignment & (alignment - 1));
+    if (is_power_two != 0) {
+        LOG(ERROR) << "Invalid alignment, must power of two";
+        return INVALID_AREA;
+    }
+    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();
+        if (BAIDU_UNLIKELY(!b)) {
+            return INVALID_AREA;
+        }
+        const size_t remainder =

Review Comment:
   是不是可以这样:
   1. 在TLS上找到满足对齐和空间的block,则使用;
   2. 否则,自己创建一个。
   
   这样实现会简单一些,连续内存的cache局部性也更好。



##########
src/butil/iobuf.cpp:
##########
@@ -1371,6 +1371,50 @@ IOBuf::Area IOBuf::reserve(size_t count) {
     return result;
 }
 
+IOBuf::Area IOBuf::reserve_aligned(size_t n, size_t alignment) {
+    bool is_power_two = alignment > 0 && (alignment & (alignment - 1));
+    if (is_power_two != 0) {
+        LOG(ERROR) << "Invalid alignment, must power of two";
+        return INVALID_AREA;
+    }
+    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();
+        if (BAIDU_UNLIKELY(!b)) {
+            return INVALID_AREA;
+        }
+        const size_t remainder =

Review Comment:
   一般只是需要长度为n的连续内存块起始地址对齐,需要每个block都是对齐的吗?



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