Lines Matching refs:to
8 when it comes to memory access. This document presents some details about
9 unaligned accesses, why you need to write code that doesn't cause them,
10 and how to write such code!
16 Unaligned memory accesses occur when you try to read N bytes of data starting
24 or write a number of bytes to or from memory (e.g. movb, movw, movl in x86
25 assembly). As will become clear, it is relatively easy to spot C statements
26 which will compile to multiple-byte memory access instructions, namely when
33 The rule mentioned above forms what we refer to as natural alignment:
43 to achieve full portability.
50 to architecture. It would be easy to write a whole document on the differences
53 - Some architectures are able to perform unaligned memory accesses
56 happen. The exception handler is able to correct the unaligned access,
57 at significant cost to performance.
60 unaligned access to be corrected.
62 silently perform a different memory access to the one that was requested,
63 resulting in a subtle code bug that is hard to detect!
66 memory accesses to happen, your code will not work correctly on certain
73 At first, the concepts above may seem a little hard to relate to actual
89 not be unreasonable to expect that accessing field2 would cause an unaligned
90 access. You'd be expecting field2 to be located at offset 2 bytes into the
97 to pad structures so that accesses to fields are suitably aligned (assuming
98 you do not cast the field to a type of different length).
100 Similarly, you can also rely on the compiler to align variables and function
101 parameters to a naturally aligned scheme, based on the size of the type of
109 that you could reorder the fields in the structure in order to place fields
120 For a natural alignment scheme, the compiler would only have to add a single
122 to satisfy alignment constraints for arrays of these structures.
125 structure type. This GCC-specific attribute tells the compiler never to
126 insert any padding within structures, useful when you want to use a C struct
127 to represent some data that comes in a fixed arrangement 'off the wire'.
129 You might be inclined to believe that usage of this attribute can easily
130 lead to unaligned accesses when accessing fields that do not satisfy
132 of the alignment constraints and will generate extra instructions to perform
134 the extra instructions obviously cause a loss in performance compared to the
145 to compare two ethernet MAC addresses for equality.
163 able to access memory on arbitrary boundaries, the reference to a[0] causes
164 2 bytes (16 bits) to be read from memory starting at address addr1.
170 is included in the kernel anyway but is understood to only work normally on
171 16-bit-aligned addresses. It is up to the caller to ensure this alignment or
186 to an address that is not evenly divisible by 4.
190 1. Casting variables to types of different lengths
191 2. Pointer arithmetic followed by access to at least 2 bytes of data
197 The easiest way to avoid unaligned access is to use the get_unaligned() and
200 Going back to an earlier example of code that potentially causes unaligned
220 The get_unaligned() macro works similarly. Assuming 'data' is a pointer to
221 memory and you wish to avoid unaligned access, its usage is as follows:
226 in the examples above). Be aware that when compared to standard access of
227 aligned memory, using these macros to access unaligned memory can be costly in
230 If use of such macros is not convenient, another option is to use memcpy(),
232 Due to the byte-wise nature of this operation, unaligned accesses are avoided.