02.03.2014 Views

BSP Developer's Guide

BSP Developer's Guide

BSP Developer's Guide

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

VxWorks 5.5<br />

<strong>BSP</strong> Developer’s <strong>Guide</strong><br />

sufficiently for an item of type type. The test is normally done by examining<br />

low-order bits of the pointer’s value.<br />

void * pVoid;<br />

if (!WRS_ALIGN_CHECK(pVoid, long))<br />

{<br />

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

}<br />

Unaligned Accesses and Copying<br />

You may need to access data that may not be correctly aligned at runtime. It is<br />

recommended in this situation that you copy the data to a structure or other area<br />

that is properly aligned. After the data has been copied, it can be accessed without<br />

the possibility of causing unaligned access exceptions. The macro provided for this<br />

purpose is _WRS_UNALIGNED_COPY(pSrc, pDst, size). While the standard<br />

VxWorks bcopy( ) function could be used for this purpose, most compilers can do<br />

short copies more efficiently on their own. Using the macro is therefore desirable<br />

for performance reasons.<br />

The following example shows both _WRS_ALIGN_CHECK and<br />

_WRS_UNALIGNED_COPY used together to check an unknown pointer. If it is<br />

sufficiently aligned, the pointer can be cast to some other type and the item can be<br />

accessed directly. If the pointer is not aligned, the unaligned macro is used to copy<br />

the data element to a usable variable location.<br />

struct structA {<br />

long item1;<br />

long item2;<br />

char item3;<br />

} itemA;<br />

void * pVoid;<br />

long aLong;<br />

if (WRS_ALIGN_CHECK(pVoid, (struct structA)))<br />

{<br />

/* Alignment is okay, reference directly */<br />

aLongItem = ((struct structA *)pVoid)->item2;<br />

}<br />

else<br />

{<br />

/* alignment is not okay, use unaligned copy */<br />

_WRS_UNALIGNED_COPY(pVoid,&aLong,sizeof(aLong));<br />

}<br />

164

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

Saved successfully!

Ooh no, something went wrong!