最近看到ngx_align_ptr,说是对齐用的,贴上代码:
#define ngx_align(d, a) (((d) + (a - 1)) & ~(a - 1))
#define ngx_align_ptr(p, a) \
(u_char *) (((uintptr_t) (p) + ((uintptr_t) a - 1)) & ~((uintptr_t) a - 1))
以下是示例:
ngx_align_ptr(0x1201, 4)
((uintptr_t) (p) + ((uintptr_t) a - 1)) :0001 0010 0000 0100
~((uintptr_t) a - 1):1111 1111 1111 1100
0001 0010 0000 0100 & 1111 1111 1111 1000
0x1204
本文深入解析ngx_align_ptr宏在内存对齐中的应用,通过具体示例代码解释其工作原理,展示了如何使用该宏进行指针对齐。
421

被折叠的 条评论
为什么被折叠?



