memory-alignment

SSE: unaligned load and store that crosses page boundary

China☆狼群 提交于 2019-12-03 03:13:52
I read somewhere that before performing unaligned load or store next to page boundary (e.g. using _mm_loadu_si128 / _mm_storeu_si128 intrinsics), code should first check if whole vector (in this case 16 bytes) belongs to the same page, and switch to non-vector instructions if not. I understand that this is needed to prevent coredump if next page does not belong to process. But what if both pages belongs to process (e.g. they are part of one buffer, and I know size of that buffer)? I wrote small test program which performed unaligned load and store that crossed page boundary, and it did not

Why is memory alignment required? [duplicate]

一个人想着一个人 提交于 2019-12-03 01:32:31
This question already has answers here : Purpose of memory alignment (8 answers) Possible Duplicate: Purpose of memory alignment I read some articles on net about memory alignment and could understand that from properly aligned memory (take 2-byte alignment) we can fetch data fastly in one go. But if we have memory like a single hardware piece, then given an address, why cannot we read 2-byte directly from that position. like: I thought over it. I think that if the memory is in odd-even banks kind of then the theory would apply. What am i missing ? Your pictures describe how we (humans)

Does a phantom type have the same alignment as the original one?

和自甴很熟 提交于 2019-12-03 01:14:06
Consider the following struct that contains some environment values: struct environment_values { uint16_t humidity; uint16_t temperature; uint16_t charging; }; I would like to add some additional information to those values with a phantom type* and make their types distinct at the same time: template <typename T, typename P> struct Tagged { T value; }; // Actual implementation will contain some more features struct Celsius{}; struct Power{}; struct Percent{}; struct Environment { Tagged<uint16_t,Percent> humidity; Tagged<uint16_t,Celsius> temperature; Tagged<uint16_t,Power> charging; }; Is the

Structure alignment padding, largest size of padding, and order of struct members

对着背影说爱祢 提交于 2019-12-02 22:13:50
问题 I've been learning about structure data padding since I found out my sizeof() operator wasn't returning what I expected. According to the pattern that I've observed, it aligns structure members with the largest data type. So for example... struct MyStruct1 { char a; // 1 byte char b; // 1 byte char c; // 1 byte char d; // 1 byte char e; // 1 byte // Total 5 Bytes //Total size of struct = 5 (no padding) }; struct MyStruct2 { char a; // 1 byte char b; // 1 byte char c; // 1 byte char d; // 1

Memory efficient AI objects for physics game

不羁的心 提交于 2019-12-02 20:01:16
问题 I am creating a physics game in java using box2d. I am writing an AI class and want to make sure my data is being stored as efficiently as possible, taking into consideration memory alignment. The tiniest increase will likely make a huge difference because I am literally running 'as many AI objects as I can' until the system slows down.The program is already using a lot of memory on the collision detection, because once again, I want to be able to support as many agents as possible. What I

Why do some types (e.g. Float80) have a memory alignment bigger than word size?

孤街浪徒 提交于 2019-12-02 19:47:28
To make it specific, I only want to know why on my 64 bit mac, the Swift compiler says the alignment of some types like Float80 is 16. To check the memory alignment requirement of a type, I use the alignof function. sizeof(Float80) // ~> 16 bytes, it only needs 10 bytes, but because of hardware design decisions it has to be a power of 2 strideof(Float80) // ~> 16 bytes, clear because it is exact on a power of 2, struct types with Float80 in it, can be bigger alignof(Float80) // ~> 16 bytes, why not 8 bytes, like String ? I understand the memory alignment of types less or equal the size of the

Cache Line Alignment (Need clarification on article)

橙三吉。 提交于 2019-12-02 19:42:01
I've recently encountered what I think is a false-sharing problem in my application, and I've looked up Sutter's article on how to align my data to cache lines. He suggests the following C++ code: // C++ (using C++0x alignment syntax) template<typename T> struct cache_line_storage { [[ align(CACHE_LINE_SIZE) ]] T data; char pad[ CACHE_LINE_SIZE > sizeof(T) ? CACHE_LINE_SIZE - sizeof(T) : 1 ]; }; I can see how this would work when CACHE_LINE_SIZE > sizeof(T) is true -- the struct cache_line_storage just ends up taking up one full cache line of memory. However, when the sizeof(T) is larger than

Size of class with virtual function

拟墨画扇 提交于 2019-12-02 18:19:11
问题 I was revising the C++ concepts, but I am stuck with a very simple code #include <iostream> using namespace std; class foo { public: //int i; void virtual foobar() { cout << "foobar\n"; } }; int main() { foo f; cout << sizeof(f) << endl; //cout << sizeof(f.i) << endl; return 1; } The output of the above code is 8 But when I removed comments from the code Output is 16 and 4 I did not understand when the class have no member variable present then VPTR size is 8 but after adding a variable size

How to allocate and free aligned memory in C

爷,独闯天下 提交于 2019-12-02 15:37:54
How do you allocate memory that's aligned to a specific boundary in C (e.g., cache line boundary)? I'm looking for malloc/free like implementation that ideally would be as portable as possible --- at least between 32 and 64 bit architectures. Edit to add: In other words, I'm looking for something that would behave like (the now obsolete?) memalign function, which can be freed using free. Jerome Here is a solution, which encapsulates the call to malloc, allocates a bigger buffer for alignment purpose, and stores the original allocated address just before the aligned buffer for a later call to

Byte Alignment for integer (or other) types in a uint8_t array

霸气de小男生 提交于 2019-12-02 10:06:31
问题 I am writing a memory manager for microcontrollers that uses a uint8_t array for the pool. From this pool, it allocates memory of the requested size to the user. I am looking at other memory implementations. Contiki has one named mmem . In their documentation they state: It must be noted that the memory allocated with mmem_alloc() is 1-byte aligned. This is different from what malloc() does. The memory allocated with malloc() is suitably aligned for every data type and the returned void