/*
* =====================================================================================
*
* Filename: alignment_trap_demo.c
*
* Description: alignment trap demo
* test on
* Processor: ARM926EJ-S
* HardWare: DaVinci DM6467 EVM
* compile with:
* arm_v5t_le-gcc -Wall alignment_trap_demo.c -o alignment_trap_demo
* reference:
* http://e2e.ti.com/support/embedded/linux/f/354/t/41140.aspx
* http://www.rt-embedded.com/blog/archives/resolving-alignment-traps/
* summary:
* 1. Try to use 32-bit variables.
* Short and Char variables do not save memory or perform faster anyway
* because the CPU loads them into 32-bit registers in any case,
* and has even more work to deal with them correctly.
* 2. Use casting with caution. Beware of arrays of bytes;
* don’t try to access this array with a 32-bit pointer.
* 3. Clean the warnings. Don’t leave warnings in your code.
* 4. Don’t fix warnings that you are not sure how to fix. Consult with others.
*
* Version: 1.0
* Created: 07/06/2012 12:41:38 PM
* Revision: none
* Compiler: gcc
*
* Author: TED (gk), guolb57@163.com
* =====================================================================================
*/
#include <stdio.h>
#define BEGIN_CASE {
#define END_CASE }
struct rte_unpacked_struct_t
{
char c1;
int i;
char c2;
short s1;
char c3;
};
struct rte_packed_struct_t
{
char c1;
int i;
char c2;
short s1;
char c3;
}__attribute__((__packed__));
struct rte_unpacked_struct_t unpacked;
struct rte_packed_struct_t packed;
short *rte_get_s1(void)
{
return &packed.s1;
}
short *rte_get_s2(void)
{
return &unpacked.s1;
}
int main(int argc, const char *argv[])
{
#if 0
/*case 1 begin *********************************************** */
BEGIN_CASE
/* passed case */
short *val;
val = rte_get_s1();
printf("val = %d\n", *val);
END_CASE
BEGIN_CASE
int *val;
printf("%d %d\n", sizeof(unpacked), sizeof(packed));
/* bus error here */
val = rte_get_s2();
printf("val = %d\n", *val);
END_CASE
/*case 1 end *********************************************** */
#endif
/*case 2 begin *********************************************** */
BEGIN_CASE
/* passed case */
char buf[20] = {0};
char *p = buf;
struct rte_unpacked_struct_t* p_unpack;
p_unpack = (struct rte_unpacked_struct_t*)p;
printf("%d\n", p_unpack->s1);
END_CASE
#if 0
/*@todo open this case, the next case will pass, why? */
BEGIN_CASE
/* passed case */
char buf[19] = {0};
char *p = buf;
struct rte_packed_struct_t* p_unpack;
printf("%d %d\n", sizeof(unpacked), sizeof(packed));
p_unpack = (struct rte_packed_struct_t*)p;
printf("%p %p\n", p, p_unpack);
printf("%d\n", p_unpack->s1);
END_CASE
#endif
BEGIN_CASE
char buf[19] = {0};
char *p = buf;
struct rte_unpacked_struct_t* p_unpack;
printf("%d %d\n", sizeof(unpacked), sizeof(packed));
/* crash here [bus error]
* use dmesg: see "alignment trap blabla...."*/
p_unpack = (struct rte_unpacked_struct_t*)p;
printf("%p %p\n", p, p_unpack);
printf("%d\n", p_unpack->s1);
END_CASE
/*case 2 end *********************************************** */
return 0;
}
字节对齐陷阱(Alignment trap)-demo for ARM9
最新推荐文章于 2025-04-03 14:35:53 发布