Need macro to align a memory pointer to match the platform's memory alignment policy

NEW
Assigned to

Status

NSPR
NSPR
15 years ago
7 years ago

People

(Reporter: Roland Mainz, Assigned: Wan-Teh Chang)

Tracking

Firefox Tracking Flags

(Not tracked)

Details

(Reporter)

Description

15 years ago
We need macro in NSPR to align a memory pointer to match the platform's memory
alignment policy.
The idea is that someone can alloc a larger chunk of memory and then use his/her
own way to assign this memory to structures instead of calling |malloc()| for
each structure, reducing dramatically the overhead caused my |malloc()| and
required error checking and cleanup.
The alignment macro is required for platforms which force the correct alignment
or send a SIGSEGV/SIGBUS to the application (SPARC is one of these platforms).

For example:
-- snip --
/* Alignment macros to force memory alignment required by most RISC platforms
 * MAX_ALIGN_BYTES = sizeof(pointer) incl. the doubleword alignment required
 * by some platforms, ALIGN_PTR() aligns the given pointer to match
 * MAX_ALIGN_BYTES alignment
 */
#define MAX_ALIGN_BYTES (sizeof(void *) * 2)
#define ALIGN_PTR(ptr) ((void *)(((PRUptrdiff)(ptr) & ~(MAX_ALIGN_BYTES-1L)) +
MAX_ALIGN_BYTES))
-- snip --
This macros allows that any "in" pointer is getting aligned to be a multiple of
|MAX_ALIGN_BYTES|.

Proposed macros:
1. "PR_MIN_ALIGN_BYTES" - a pointer on the matching platform must be a multiple
of |PR_MIN_ALIGN_BYTES| to be properly aligned ("1" for x86, "4" for SPARC
32bit, "8" for SPARC 64bit. etc.)
2. "PR_ALIGN_PTR" to align a pointer on a given number of bytes (this number
must be a result of 2^n)
#define PR_ALIGN_PTR(ptr, alignment) ((void *)(((PRUptrdiff)(ptr) &
~((alignment)-1L)) + (alignment)))
(Reporter)

Updated

15 years ago
Blocks: 192070
(Reporter)

Comment 1

15 years ago
wtc:
Any comments/suggestions/ideas ?
(Assignee)

Comment 2

15 years ago
I am not sure that this macro is generally useful
and I am not confident that I know how to test it.
(Reporter)

Comment 3

15 years ago
Wan-Teh Chang wrote:
> I am not sure that this macro is generally useful

AFAIK we have multiple places in Mozilla where people currently do their own
alignment stuff... my idea was to kill all the homegrown code (which is likely
to trigger portability issues sooner or later) and make a NSPR macro from it to
have a central place to do adjustments if neccesary.
QA Contact: wtchang → nspr

Comment 4

7 years ago
Yes, we definitely can use this change!  We're currently trying to install/use OpenAM with Apache 2.2.17 on a Solaris 10 Sparc (T1000).  The OpenAM agent crashes though every single time you try to use a secure socket with a SIGBUS error.  Just for the record, yes, I *did* ask them.  They pointed us here, since the problem is outside of their code.  I can provide a pstack or truss output here if that would be helpful.

Comment 5

7 years ago
Nick, this antique bug seems invalid to me. Do you think that [sizeof(Type)] is not a good alignment for Type?
(Assignee)

Comment 6

7 years ago
(In reply to comment #4)
Bernhard Thalmayr reported a crash that sounds like what you described on the
dev-tech-crypto@lists.mozilla.org mailing list.  He tracked it down to a bug in
the OpenAM agent code.  The variable in question is a local variable, allocated
on the stack by the compiler, so that crash is not related to this requested
macro.  (The OpenAM agent code declares a 'bool' local variable but casts its
address to int * .)
You need to log in before you can comment on or make changes to this bug.