memory-alignment

creating a C function with a given size in the text segment

大城市里の小女人 提交于 2019-12-05 13:28:45
问题 I'm programming an embedded powerpc 32 system with a 32 kbyte 8-way set associative L2 instruction cache. To avoid cache thrashing we align functions in a way such that the text of a set of functions called at a high frequency (think interrupt code) ends up in separate cache sets. We do this by inserting dummy functions as needed, e.g. void high_freq1(void) { ... } void dummy(void) { __asm__(/* Silly opcodes to fill ~100 to ~1000 bytes of text segment */); } void high_freq2(void) { ... } This

Does the order of instance variable declaration matter in Objective-C?

拈花ヽ惹草 提交于 2019-12-05 12:38:01
I was searching the internet for tips to optimizing Objective-C code and came across this link. In the article I saw the note below, which I am not able to understand. This article is out of date. It was at one time true that ivars were stored in an Objective-C instance just as the members of a struct are stored, and thus that memory alignment could (marginally) affect access time. Now, however, ivars are indirectly accessed (at least in Apple's runtime) ; the instance now holds the offset of the ivar, and uses that to access the variable. Since all those offsets are of the same type, and you

__memcpy_sse2_unaligned - what does this mean in detail?

烂漫一生 提交于 2019-12-05 11:13:21
问题 While working on my compiler I got this error: Program received signal SIGSEGV, Segmentation fault. __memcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/memcpy-sse2-unaligned.S:33 How do I get details of what went wrong here? I know from the backtrace it's a memcpy line that causes it, but how do I see how the memory is aligned? And how do I know how it should be aligned? The project is a compiler with an LLVM back-end using the Zend/PHP runtime with the OCaml garbage collector, so there

Is there cases where a 32-bit variable could not been properly-aligned

二次信任 提交于 2019-12-05 09:48:53
In the following link: http://msdn.microsoft.com/en-us/library/ms684122(VS.85).aspx , it is said that "Simple reads and writes to properly-aligned 32-bit variables are atomic operations". I'm wondering if in a c++ program all 32-bit variables are by default properly-aligned. Saying differently is there any case where a 32-bit variable could not been properly-aligned. If you don't tell the compiler to do otherwise, then it will align 32-bit variables correctly. You can write code which places 32-bit variables at non-aligned addresses (for example by creating an array of char , and writing your

What alignment issues limit the use of a block of memory created by malloc?

不羁的心 提交于 2019-12-05 06:09:31
I am writing a library for various mathematical computations in C. Several of these need some "scratch" space -- memory that is used for intermediate calculations. The space required depends on the size of the inputs, so it cannot be statically allocated. The library will typically be used to perform many iterations of the same type of calculation with the same size inputs, so I'd prefer not to malloc and free inside the library for each call; it would be much more efficient to allocate a large enough block once, re-use it for all the calculations, then free it. My intended strategy is to

What are the alignment limitations of the standard global default operator new?

こ雲淡風輕ζ 提交于 2019-12-05 05:54:07
I'm working on some older code that uses ATL's CComBSTR type. I'm changing it so that it will compile using Visual C++ Express Edition, which does not come with ATL. I used only a very small subset of CComBSTR , so doing this is fairly simple. However, when allocating the BSTR memory block, I need to fill the first four bytes with a 4 byte length prefix. I'm concerned that if I use a new char[size] expression to allocate the memory for the string, that I will cause alignment faults due to the allocated char array not having the correct alignment for the four byte prefix. Is there anything in

Common initial sequence and alignment

懵懂的女人 提交于 2019-12-05 00:03:22
While thinking of a counter-example for this question , I came up with: struct A { alignas(2) char byte; }; But if that's legal and standard-layout, is it layout-compatible to this struct B ? struct B { char byte; }; Furthermore, if we have struct A { alignas(2) char x; alignas(4) char y; }; // possible alignment, - is padding // 00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 // x - - - y - - - x - - - y - - - struct B { char x; char y; }; // no padding required union U { A a; B b; } u; Is there a common initial sequence for A and B ? If so, does it include A::y & B::y ? I.e., may we write

Struct Reordering by compiler [duplicate]

本秂侑毒 提交于 2019-12-04 22:14:22
This question already has an answer here: Why can't C compilers rearrange struct members to eliminate alignment padding? [duplicate] 11 answers Suppose I have a struct like this: struct MyStruct { uint8_t var0; uint32_t var1; uint8_t var2; uint8_t var3; uint8_t var4; }; This is possibly going to waste a bunch (well not a ton) of space. This is because of necessary alignment of the uint32_t variable. In actuality (after aligning the structure so that it can actually use the uint32_t variable) it might look something like this: struct MyStruct { uint8_t var0; uint8_t unused[3]; //3 bytes of

Is it guaranteed that the padding bits of “zeroed” structure will be zeroed in C?

独自空忆成欢 提交于 2019-12-04 17:33:31
This statement in the article made me embarrassed: C permits an implementation to insert padding into structures (but not into arrays) to ensure that all fields have a useful alignment for the target. If you zero a structure and then set some of the fields, will the padding bits all be zero? According to the results of the survey, 36 percent were sure that they would be, and 29 percent didn't know. Depending on the compiler (and optimization level), it may or may not be . It was not completely clear, so I turned to the standard. The ISO/IEC 9899 in §6.2.6.1 states: When a value is stored in an

Memory Allocation in Linux Kernel

旧巷老猫 提交于 2019-12-04 14:29:52
问题 I had an interview today and was asked this question. What Kernel Memory allocation strategy would you use, if you were asked to allocate memory of size 2KB and that allocated memory should be page aligned. KMALLOC handles smaller memory allocation strategies but the lowest unit that it supports is 4KB, which is the size of the physical page. I asked him, if he was expecting slab allocators? He didn't reply positively. 回答1: I read in: http://www.makelinux.net/books/lkd2/ch11lev1sec4 The