MemoryPool icon indicating copy to clipboard operation
MemoryPool copied to clipboard

关于MP_ALIGN_SIZE实现的改进

Open kakaxi3019 opened this issue 2 years ago • 1 comments

项目中对于字节对齐的实现如下: #define MP_ALIGN_SIZE(_n) (_n + sizeof(long) - ((sizeof(long) - 1) & (_n))) 看了下这个实现如果遇到_n已经是sizeof(long)的倍数了,则还会为其加上sizeof(long)字节,感觉这里是否可以稍作修改。 一个可选的方法如下:

#include <cstdint>

#define MP_ALIGN_SIZE(_n)       \
        (((uintptr_t) (_n) + ((sizeof(long)) - 1)) & ~((uintptr_t) ((sizeof(long)) - 1)))

kakaxi3019 avatar Oct 11 '24 08:10 kakaxi3019

thanks

userpro avatar Oct 12 '24 03:10 userpro