Usenet comp.os.linux.alpha (1997-2013)
 help / color / mirror / Atom feed
* Dealing with unaligned accesses
@ 1998-12-03  5:00 Oscar Levi
  1998-12-09  5:00 ` David Hagood
  0 siblings, 1 reply; 2+ messages in thread
From: Oscar Levi @ 1998-12-03  5:00 UTC (permalink / raw)


I'm working on eliminating some unaligned accesses in smail.  It happens
to be the case that the misalignment is due to a pointer, stored in a
structure, that is not being accessed on a 64 bit boundary.  My question
is how to cope with this kind of problem.

The message from the kernel says that address 2017fbec is where the
access occurs.  The data item at that address is an eight byte pointer.
The first byte of the structure is at address 2017fbd4.  This means that
moving the pointer to the top of the structure won't help.  In fact, the
structure is an even number of 64 bits in length.

When I look at the code, I see that this block is allocated by malloc.
I would expect malloc to return 64 bit aligned memory.  Humph.

Does anyone know the correct solution to this kind of problem?





^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Dealing with unaligned accesses
  1998-12-03  5:00 Dealing with unaligned accesses Oscar Levi
@ 1998-12-09  5:00 ` David Hagood
  0 siblings, 0 replies; 2+ messages in thread
From: David Hagood @ 1998-12-09  5:00 UTC (permalink / raw)


> access occurs.  The data item at that address is an eight byte pointer.
> The first byte of the structure is at address 2017fbd4.  This means that
> moving the pointer to the top of the structure won't help.  In fact, the
> structure is an even number of 64 bits in length.

First, re-order the structure so that it allocates the biggest items first:
pointers and arrays, then shorts, then chars.

Second, modify the malloc call to look something like this:

foo *my_pointer;
void *save_ptr;
save_ptr = malloc(sizeof(foo)+8);
my_pointer = (foo *)(((unsigned long)save_ptr+7)&(~7));

This will force the actual pointer to be qword aligned no matter what
malloc returns. There is one gotcha: you cannot free my_pointer, you must
free save_ptr.

Malloc ought to return aligned memory; perhaps there's a bug in libc/glibc.






^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~1998-12-09  5:00 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
1998-12-03  5:00 Dealing with unaligned accesses Oscar Levi
1998-12-09  5:00 ` David Hagood

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox