02.03.2014 Views

BSP Developer's Guide

BSP Developer's Guide

BSP Developer's Guide

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

8<br />

Writing Portable C Code<br />

8.1.3 Static Data Alignment<br />

Sometimes it is necessary to align a global static data element or structure on a<br />

specific boundary type. This typically happens with CPU-specific data structures<br />

that need cache boundary alignment.<br />

To handle this situation, another toolchain-specific macro has been introduced.<br />

The macro _WRS_DATA_ALIGN_BYTES(bytes) aligns the following data element<br />

with the byte alignment specified.<br />

For example:<br />

_WRS_DATA_ALIGN_BYTES(16) int myData = 0xFE00235F;<br />

This alignment macro should only be used with global data that has been<br />

initialized. Uninitialized data may not be placed in the data section, and the macro<br />

may not have the desired effect. Uninitialized data can be handled at runtime<br />

using memalign( ) or other suitable functions.<br />

8<br />

8.1.4 Runtime Alignment Checking<br />

Checking the Alignment of a Data Item<br />

You may need to know the alignment of a particular data item at runtime. Most<br />

compilers provide an extension for accessing this information, but there is no<br />

recognized standard for it. In the case of Wind River source code, the macro<br />

_WRS_ALIGNOF(x) is used to return the alignment of an item in byte units.<br />

if (WRS_ALIGNOF(itemA) < 4)<br />

{<br />

printf (“Error: itemA is not longword aligned”);<br />

}<br />

Verifying Pointer Alignment<br />

Pointers can be deliberately cast to be a pointer to a different type of object with<br />

different alignment requirements. Strict type checking at compile time is beneficial,<br />

but there are situations in which this checking must be performed at runtime. For<br />

this purpose, the macro _WRS_ALIGN_CHECK(ptr, type) is provided. This macro<br />

evaluates to either TRUE or FALSE. TRUE is returned if the pointer ptr is aligned<br />

163

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!