为的malloc()/无对齐限制() [英] Alignment restrictions for malloc()/free()

查看:273
本文介绍了为的malloc()/无对齐限制()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

旧K&安培; R(第二版),我已阅读其他C语言文本的讨论风格的动态内存分配的实施的malloc()免费()通常还别说,在传球,东西长约数据类型对齐限制。显然,某些计算机硬件体系结构(CPU,寄存器和存储器存取)限制你如何储存和处理某些值类型。例如,可能有一个要求,即一个4字节()整数必须存储在该是四的倍数的地址开始。

Older K&R (2nd ed.) and other C-language texts I have read that discuss the implementation of a dynamic memory allocator in the style of malloc() and free() usually also mention, in passing, something about data type alignment restrictions. Apparently certain computer hardware architectures (CPU, registers, and memory access) restrict how you can store and address certain value types. For example, there may be a requirement that a 4 byte (long) integer must be stored beginning at addresses that are multiples of four.

什么限制,如果有的话,做的主要平台(英特尔&安培; AMD,SPARC,阿尔法)?并处内存分配和内存访问,或者我可以放心地忽略特定的地址边界对齐内存分配

What restrictions, if any, do major platforms (Intel & AMD, SPARC, Alpha) impose for memory allocation and memory access, or can I safely ignore aligning memory allocations on specific address boundaries?

推荐答案

的Sparc,MIPS,Alpha和大多数其他古典RISC式架构只允许一致访问内存,即使在今天。一个未对齐存取会造成一个异常,但某些操作系统将通过使用更小的加载和存储在软件所需的地址复制处理异常。该应用程序code不会知道有问题,但性能将非常糟糕。

Sparc, MIPS, Alpha, and most other "classical RISC" architectures only allow aligned accesses to memory, even today. An unaligned access will cause an exception, but some operating systems will handle the exception by copying from the desired address in software using smaller loads and stores. The application code won't know there was a problem, except that the performance will be very bad.

MIPS具有可用于未对齐的地址访问32位数量特殊指令(LWL和LWR)。每当编译器可以告诉大家,地址很可能不对齐它会使用,而不是一个正常的LW指令这两个指令序列。

MIPS has special instructions (lwl and lwr) which can be used to access 32 bit quantities from unaligned addresses. Whenever the compiler can tell that the address is likely unaligned it will use this two instruction sequence instead of a normal lw instruction.

86可处理未对齐的内存硬件访问没有一个例外,但仍有高达3倍比较一致的访问对性能的影响。

x86 can handle unaligned memory accesses in hardware without an exception, but there is still a performance hit of up to 3X compared to aligned accesses.

乌尔里希Drepper写这和其他内存相关主题的COM prehensive纸,什么每个程序员都应该知道内存。这是一个很长的书面记录,但充满嚼劲的善良。

Ulrich Drepper wrote a comprehensive paper on this and other memory-related topics, What Every Programmer Should Know About Memory. It is a very long writeup, but filled with chewy goodness.

这篇关于为的malloc()/无对齐限制()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆