Memory alignment of arrays

后端 未结 3 1778
广开言路
广开言路 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:46

    The alignment attribute specifies the alignment of variables or structure fields, not single array elements. See Specifying Attributes of Variables for details.

    If you always want to align two integers together, you can define a structure

    struct dma_transfer {
        unsigned int e0 __attribute__ ((aligned (16)));
        unsigned int e1 __attribute__ ((aligned (16)));
    };
    

    This aligns the elements on 16 byte boundaries.

    int main(int argc, char **argv)
    {
        static struct dma_transfer a;
        static unsigned int b[2];
    
        printf("a.e0 = %p\n", &a.e0);
        printf("a.e1 = %p\n", &a.e1);
        printf("b[0] = %p\n", &b[0]);
        printf("b[1] = %p\n", &b[1]);
    
        return 0;
    }
    

    gives, e.g.

    a.e0 = 0x601060
    a.e1 = 0x601070
    b[0] = 0x601080
    b[1] = 0x601084
    

    But this means also, that you have holes between the two integer values. On a 32 bit system, you will have

    | int 4 bytes | hole 12 bytes |
    | int 4 bytes | hole 12 bytes |

提交回复
热议问题