1
\$\begingroup\$

What is the purpose of a pragma Data_Align in TI emb. compiler.

The datasheet explanation:

http://www.ti.com/lit/ug/spnu151j/spnu151j.pdf

"The DATA_ALIGN pragma aligns the symbol in C, or the next symbol declared in C++, to an alignment boundary. The alignment boundary is the maximum of the symbol's default alignment value or the value of the constant in bytes. The constant must be a power of 2. The maximum alignment is 32768. The DATA_ALIGN pragma cannot be used to reduce an object's natural alignment."

Can someone re-explain it in a different method? I really don't understand the purpose of this pre-compile directive.

\$\endgroup\$
4
  • \$\begingroup\$ Do you know what alignment is and what is "natural" alignment? \$\endgroup\$
    – Eugene Sh.
    Jun 30, 2015 at 15:56
  • \$\begingroup\$ No, can you explain it. My knowledge with compiler directive is very limited. \$\endgroup\$
    – MathieuL
    Jun 30, 2015 at 16:03
  • 1
    \$\begingroup\$ It's not related to compiler directives. It is related to a memory architecture. Look up "memory alignment" on google. \$\endgroup\$
    – Eugene Sh.
    Jun 30, 2015 at 16:04
  • \$\begingroup\$ So if I got it correct, that directive just make sure that the Variable will follow the memory Alignement? So if I declare a 1024 byte variable with that directive everytime, I write into this variable, it will automatically manage the bit shift of n bytes? \$\endgroup\$
    – MathieuL
    Jun 30, 2015 at 18:19

1 Answer 1

1
\$\begingroup\$

DATA_ALIGN will cause the symbol to be located at an address that satisfies the specified alignment requirement. For example:

The following code will locate mybyte at an even address.

#pragma DATA_ALIGN(mybyte, 2)
char mybyte;

The following code will locate mybuffer at an address that is evenly divisible by 1024.

#pragma DATA_ALIGN(mybuffer, 1024)
char mybuffer[256];

Most symbols don't require any special alignment beyond the data type's default alignment so you won't need to use DATA_ALIGN often. But occasionally you'll want a symbol to be located on a special address boundary and that's when DATA_ALIGN is useful. For example, sometimes buffers used with a DMA controller should be aligned on a special boundary.

\$\endgroup\$
3
  • \$\begingroup\$ Yeah, it is for a DMA controller application. So if you allocate 1026 byte with DATA_ALIGN, the precompile directive make sure that the 1024 bytes adress are between n and n + 1024, so you can more easily navigate throught the buffer? \$\endgroup\$
    – MathieuL
    Jun 30, 2015 at 21:00
  • 1
    \$\begingroup\$ DATA_ALIGN does NOT allocate any memory and doesn't do anything to specify the size of the buffer. DATA_ALIGN ensures the buffer starts at an address that is evenly divisible by 1024. In the example in the answer, mybuffer is only 256 bytes long but it starts at an address that is evenly divisible by 1024. \$\endgroup\$
    – kkrambo
    Jun 30, 2015 at 21:20
  • \$\begingroup\$ "Evenly divisible by 1024" means that there is no remainder if you were to divide the address by 1024. Examples of addresses that are evenly divisible by 1024 include 0x00000400, 0x01234800, and 0x80000C00. Said differently, it's any address whose least significant 10 bits are zero (1024 = 2 ^ 10). Thinking of it this way helps explain why the constant in the DATA_ALIGN statement must be a power of 2. \$\endgroup\$
    – kkrambo
    Jun 30, 2015 at 21:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.