【发布时间】:2015-07-01 13:29:35
【问题描述】:
我得到了以下用于对齐的 c 代码
struct s *p, *new_p
p = (struct s*) malloc(sizeof(struct s) + BOUND -1);
new_p = (struct s*) (((int) p+BOUND-1) & ~(BOUND -1);
其中 BOUND 表示 32 个字节。一行高速缓存像 Pentium II 和 III 一样是 32 字节,但我无法弄清楚 p 和 new_p 对齐的方式。都对齐还是只有 new_p?
另外,我有一个 64 B 的缓存行代码,用于一组关联缓存,每组有 8 个块,大小为 32 Kb:
int *tempA, *tempB;
...
pA= (int *) malloc (sizeof(int)*N + 63);
tempA = (int *)(((int)pA+63)&~(63));
tempB = (int *)((((int)pA+63)&~(63))+4096+64)
附上这句话:如果你访问超过 8 个地址,间隔为 4 Kb,将会受到惩罚。
整个对我来说没有多大意义。有什么想法吗?
【问题讨论】:
标签: c caching memory-alignment