Sender: Law (game * Life), the letter area: C Title: Re: Justice Club Send Station: Drinking Water Source (Fri May 24 15:59:56 2002), transferred data access on x86 It is an influentity speed, and it is a bus error on the SPARC or MIPS. The alignment principle is generally the length of the corresponding instruction operation data. For example, * (int *) p = 1; generally compiled into a SW (Store Word) instruction P to be aligned in sizeof (int)) on MIPS. * (char *) p = 1. No alignment is required to correspond to the SB command.
Sender: Jackzhang (Programming Raw), Letter Area: C Title: Summary and Questioning Station: Drinking Water Source (May 24, 2002 21:15:32 Friday), station letters assume all data types The length is 2 N times (Char [5] == 5 char) hypothesis the compiler adopts this alignment policy: "Previous" field (initial = 0) OFFSET "Current Field" Count "Current" field Type Type (Count) "Previous" field and "Current" Filler FillSize If satisfied (Offset Sizeof (Type (Count-1)) FillSize) mod size == 0 It is considered to be aligned. Of course, the entire STRUCT base address is "0" if it is not satisfied, then fill in the count and the (count 1) field, these are compiled when compiling. However, it is not guaranteed that each field is also aligned, the key is the actual base address of Struct, such as Struct A {Word A1; DWORD A2;}; if & a == 2, then A.a2 is not aligned, then The allocation strategy of the runtime, as long as the Struct's base address can be divided by the length of the maximum field in Struct, then every field is also aligned at runtime. Accurate statement is the length of all fields The smallest mide is divided, because it is assumed that all fields are 2 N times, but there is a problem here: struct itself can also be regarded as a single field, such as: struct foo {Word A1; Word A2; Word A3;}; structure Foo2 {foo f1; wdord d;}; can be seen as a field of 6 prove: use mathematical induction method, compile, alignment equality in alignment, replace it with Base Offset, and because Base can be maximum 2 n Multi-level, so the equation is still established. For Win32 memory allocation Virtualalloc, its base site is inevitable, which is theoretically used by the Virtualalloc allocation. Because you can think There is no more field type than 64K. For New (Malloc), it is equivalent to managing the memory of the VirtualAlloc assignment for internal redistribution, of course, the allocation algorithm is impossible to go to STUC. The maximum field length of T, for 32-bit computers, the Struct base address can be at least 4, guess: the internal data of the use of the flag stack is 32-bit, such as the use of a memory block. Description Struct {DWORD LEN; CHAR BUF [1];}; so it causes BUF (that is, the address returned by new) is on the 32-bit boundary. Of course, the management of the C / C HEAP is more complicated. In short, it is running It is not necessarily guaranteed, because on the one hand is not all the field lengths are 1, 2, 4, and there are other, such as 6, unless Base can be removed by the least common multiple of all fields. Why don't you be aligned on the way home? Because there is a growing field in my Struct, then a memory contains N struct, so "next" Struct may not align in this memory. Struct string {short len; char buf [1];}; solution The way can be used in readword () that Law, but the efficiency is low, I think I think it is struct string {short len; char * buf;}; then put the growing content into the "all" Struct back On the one hand, when the memory is generated, there is a little cumbersome (requires the last idle position pointer), and the more critical is that there is no solving the problem, in other words, if a structure is completely composed of Word and DWORD, it is not guaranteed "Next"
Struct is aligned, such as Struct A {Word A;}; struct b {dword b;}; if there is a sum of A and B in memory, the BB is not necessarily aligned. So my solution is to specify that Struct must be composed of Word (Of course, you can also be composed of DWORD) strunt string {short len; short xxx; // ... char buf [1];}; for the actual length of BUF must be 2 integer times Len = Strlen (STR) Strlen (STR) MOD 2; string * s = (string *) malloc (SIZEOF (STRING) LEN - 1); Memcpy (S-> BUF, STR, LEN);
Sender: LAW (game * Life), the letter area: C Title: Re: Summary and questions from the problem: Drinking water Source (May 24, 2002 23:36:12 Friday), station letter Glibc Malloc The strategy is roughly 2 * sizeof (size_t) alignment because its memory head is defined as big. When you need a bigger alignment by one, you can assign by Memalign, which assigns a piece of memory and takes the alignment section and reclaims previous zero. The general C library allocation is at least in line with SIZEOF (int). However, it will be wasted to be a waste of time because of the inner fragments. So I have designed more complex allocation strategies for specific applications. It is to organize a small memory head to be organized into a Hash table with small memory heads that do not need to align the operation, and placed in separate stacks, general operation or 2 * sizeof (size_t) alignment, greater alignment of Glibc strategy . This is actually a strategy for time-changing space. You can save memory when your application has a large amount of zero memory block. But because the memory head is relatively small, it is more slow to talk about some bit operations.
In fact, the compiler's alignment strategy is basically consistently found in the program that is not aligned in the program is not a hard thing because there is Core Dump.