ehds opened a new issue, #2416:
URL: https://github.com/apache/brpc/issues/2416

   **Is your feature request related to a problem? (你需要的功能是否与某个问题有关?)**
   当前 brpc 代码默认使用的 C++ 标准为 11,不支持 align new (since c++17 
https://en.cppreference.com/w/cpp/memory/new/operator_new) .
   
   所以某个 class 指定了 alignment 要求 例如 使用宏 BAIDU_CACHELINE_ALIGNMENT 时,使用 new 
的方式来分配其对象时,对象的地址有可能并不是严格按照其对齐方式的。
   > 要让一个变量或结构体按cacheline对齐,可以include 
<butil/macros.h>后使用BAIDU_CACHELINE_ALIGNMENT宏,请自行grep brpc的代码了解用法。
   
   
https://github.com/apache/brpc/blob/master/docs/cn/atomic_instructions.md#cacheline
   
   例如以下代码,使用当前编译选项可能会出现错误。
   ```c++
   
   class BAIDU_CACHELINE_ALIGNMENT A {
       int i;
   };
   
   int main() {
       for(size_t i =0 ;i<100;i++) {
           A* a = new A();
           // maybe fail.
           assert((reinterpret_cast<uintptr_t>(a) & (63)) == 0);
        }
   ```
   **Describe the solution you'd like (描述你期望的解决方法)**
   
   使用 new 来分配指定对齐要求的类时,内存地址应满足对齐的要求。
   升级为 C++17 标准,或者开启 -faligned_new (gcc 7.4+,clang 7.1.0+ 都已经支持)  .
   
   目前来看是强行关闭了改 warning.
   
https://github.com/apache/brpc/blob/f3fe5fc4ff315aeed1f1e4b6c43c2ebf470d4381/CMakeLists.txt#L70-L72
   
   **Describe alternatives you've considered (描述你想到的折衷方案)**
   对于需要对齐的类,在使用 new 分配内存时使用 aligned_alloc/posix_memalign 等函数申请 alignment 内存,再使用 
Placement new 指定内存空间进行初始化.
   
   **Additional context/screenshots (更多上下文/截图)**
   
   


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