Memory alignment of arrays

后端 未结 3 1771
广开言路
广开言路 2020-12-19 03:08

I am having trouble aligning memory for DMA transfer on the Cell processor. I need the last 4 bits of an address to be 0.

I have 4 arrays of unsigned int

3条回答
  •  青春惊慌失措
    2020-12-19 03:50

    If arr is an array of 32-bit elements, and the address of arr[0] is 0xXXXXXXX0, then the address of arr[1] will necessarily be 0xXXXXXXX4.

    For your purpose, you need to use arrays of 16-byte elements:

    typedef struct
    {
        unsigned int x;
        unsigned char reserved[16-sizeof(unsigned int)];
    }
    element_t;
    
    static element_t a[2] __attribute__ ((aligned (16)));
    static element_t b[2] __attribute__ ((aligned (16)));
    static element_t c[2] __attribute__ ((aligned (16)));
    static element_t d[2] __attribute__ ((aligned (16)));
    

    Alternatively, you can simply refrain from using arrays altogether.

    Instead, use plain variables, and tell the compiler to align them to 16 bytes:

    static unsigned int a0 __attribute__ ((aligned (16)));
    static unsigned int a1 __attribute__ ((aligned (16)));
    static unsigned int b0 __attribute__ ((aligned (16)));
    static unsigned int b1 __attribute__ ((aligned (16)));
    static unsigned int c0 __attribute__ ((aligned (16)));
    static unsigned int c1 __attribute__ ((aligned (16)));
    static unsigned int d0 __attribute__ ((aligned (16)));
    static unsigned int d1 __attribute__ ((aligned (16)));
    

提交回复
热议问题