Forum Discussion
Altera_Forum
Honored Contributor
15 years agoYou aren't missing anything and it isn't a bug, it is just a side effect of how the cache is designed. You have to take it into account, and the best way to do it is to align all the memory areas that you intend to flush or invalidate on the cache line size.
For a dynamically allocated memory area, you can allocate more than needed and then perform some operation on the pointer to align it. I haven't tested it, but this should align your pointer correctly:
// to allocate a buffer of n bytes
my_buffer = malloc(n+NIOS2_DCACHE_LINE_SIZE-1);
my_pointer = (buffer + NIOS2_DCACHE_LINE_SIZE - 1) & (-NIOS2_DCACHE_LINE_SIZE);
This code will only work if NIOS2_DCACHE_LINE_SIZE is a power of 2 but I think it always is. It isn't a very elegant piece of code, but it may be rendered more readable by defining a 'mask" constant first, that is applied to the pointer. Remember to keep both pointers, as you will need the original my_buffer to free it. On a static variable, you can use gcc's align attribute: my_type my_variable __attribute__ ((aligned (NIOS2_DCACHE_LINE_SIZE)));