Operating System - Linux
1827295 Members
4003 Online
109717 Solutions
New Discussion

Compiler Issue on PA-RISC when Accessing Unaligned Packed Structures

 
SOLVED
Go to solution
Dekun Cao
New Member

Compiler Issue on PA-RISC when Accessing Unaligned Packed Structures

Compiler Issue on PA-RISC when Accessing Unaligned Packed Structures

Background: we are attempting to minimize memory usage in areas where 10s of millions of items may exist. Using #pragma pack(1) to pack structures as much as possible and then these structures are being packed (along with other data) into large blocks of malloc'ed memory. Due to the variable nature of the other data, the structures may be aligned on any byte boundary, which should be ok as the pragma implies alignment of 1 as well.

On PA-RISC machines, we have been seeing "Bus error (core dumped)" messages and have narrowed it down to alignment when doubly de-referencing pointers to that memory. i.e. if we align the structures it works, or if we copy the pointer and then de-reference it works too!

Output of "uname -a" is: HP-UX dh-holly B.11.11 U 9000/800 3314646695 unlimited-user license
Compiler version is: HP ANSI C++ B3910B A.03.65

The same source code works OK on HP Itanium machine with
Output of "uname -a" is: HP-UX dh-ida B.11.23 U ia64 2988385317 unlimited-user license
Compiler version is: HP aC++/ANSI C B3910B A.06.05 [Jul 25 2005]

Build and Run Instruction: Build it with aCC and run it.

/********** Source code *************/
#include
#include

typedef struct
{
int i;
} REF_LEVEL_T;

#pragma pack 1
typedef struct
{
REF_LEVEL_T *pRefLevel;
char c; // doesn't core if this is moved above pRefLevel
} REF_MEMBER_T;
#pragma pack 0

typedef struct
{
REF_MEMBER_T *pRefMember;
} MEMBER_T;

int main()
{
REF_LEVEL_T RefLevel;
MEMBER_T Member;

RefLevel.i = 0;

char *p = (char *)malloc( 1000 );

for (int unalignBytes = 0; unalignBytes < 4; unalignBytes++)
{
fprintf( stderr, "trying unalignBytes %d\n", unalignBytes );

REF_MEMBER_T *pRefMember = (REF_MEMBER_T *)(p+unalignBytes);

pRefMember->pRefLevel = &RefLevel;

Member.pRefMember = pRefMember;

REF_MEMBER_T *pTestRefMember = Member.pRefMember;

int x = pTestRefMember->pRefLevel->i;

fprintf( stderr, "passed first test\n" );

int y = (Member.pRefMember)->pRefLevel->i;

fprintf( stderr, "passed second test\n" );
}
}


8 REPLIES 8
Steven E. Protter
Exalted Contributor

Re: Compiler Issue on PA-RISC when Accessing Unaligned Packed Structures

Shalom,

You've followed the guidelines for making code work on Itanium and PA-RISC.

Oracle couldn't do it, they maintain different code bases for Itanium versus PA-RISC

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Sandman!
Honored Contributor

Re: Compiler Issue on PA-RISC when Accessing Unaligned Packed Structures

Certainly looks more like a compiler issue. The code compiles fine with the ANSI C compiler but core dumps using the aC++ compiler.
Sandman!
Honored Contributor

Re: Compiler Issue on PA-RISC when Accessing Unaligned Packed Structures

Correction to my earlier post. When using aC++ supply the "+u1" switch on the command line for pointers to access non-natively aligned data.

# aCC +u1 your_prog.c

# ./a.out
trying unalignBytes 0
passed first test
passed second test
trying unalignBytes 1
passed first test
passed second test
trying unalignBytes 2
passed first test
passed second test
trying unalignBytes 3
passed first test
passed second test

~cheers
rick jones
Honored Contributor

Re: Compiler Issue on PA-RISC when Accessing Unaligned Packed Structures

Is it a safe assumption that the performance of this application isn't terribly important?
there is no rest for the wicked yet the virtuous have no pillows
Dekun Cao
New Member

Re: Compiler Issue on PA-RISC when Accessing Unaligned Packed Structures

Performance of this application is very important. I know the solution I got will have performance penalty. Any other suggestions?

Thanks.
Sandman!
Honored Contributor
Solution

Re: Compiler Issue on PA-RISC when Accessing Unaligned Packed Structures

If the performance of this application is so very important then it makes no sense to go against the grain as the machine architecture is optimized for doing loads and stores on a word boundary. Better to waste memory than consume CPU cycles.
Dekun Cao
New Member

Re: Compiler Issue on PA-RISC when Accessing Unaligned Packed Structures

I think I got all the information I need.

Thank you all very much for the help.

Dekun
Dekun Cao
New Member

Re: Compiler Issue on PA-RISC when Accessing Unaligned Packed Structures

I have got the solution.

Thanks for the help