[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Alignment of Lisp_Subr

From: Miles Bader
Subject: Re: Alignment of Lisp_Subr
Date: Wed, 12 Nov 2003 18:30:13 -0500
User-agent: Mutt/1.3.28i

On Wed, Nov 12, 2003 at 11:22:33AM -0500, Stefan Monnier wrote:
> - Structures of type Lisp_Subr are statically allocated and are not
>   guaranteed to be aligned on a multiple of 8.  I currently solve this
>   by adding __attributes__ ((__aligned__ (8))), but this only works for GCC
>   AFAIK.  The only alternative I can think of is to add a padding field
>   to Lisp_Subr and when its address is not a multiple of 8, memmove it
>   by a few bytes (I think `defsubr' is the only place where we refer
>   to these statically allocated Sfoo variables).
>   Any comment on which approach is preferable ?

I think the compiler will force the structure to be aligned to the strictest
alignment of any component, so if you stick an field with 8-byte-alignment in
there _somewhere_, the compiler should do everything for you.  It should even
work to put the `forcing field' in as a member of a union with another field,
and simply never use the forcing field.  On many platforms, `double' could be
used as the type of the forcing-field, but I'm not sure how universal this

Given that subrs are not the most space critical object though, it seems
simplest to just use __attributes__ when compiling with gcc (#ifdef
__GNUC__), and add the padding field when compiling with another compiler.

> - malloc does not guarantee on all systems that the returned value
>   will be a multiple of 8 (or does it?).

It guarantees `most strict' alignment, which on many platforms is 8 bytes
(because of doubles), and I think many malloc implementations are
conservative and assume 8 bytes anyway.  However I don't think you can assume
this though; a quick google shows plenty of people complaining about exactly
this issue (they expected 8-byte alignment, and got less; common culprits
seem to be MS and SCO mallocs... :-)

I guess in the common case where emacs uses its builtin malloc, or glibc's,
you'll have #defines to tell you though...

In New York, most people don't have cars, so if you want to kill a person, you
have to take the subway to their house.  And sometimes on the way, the train
is delayed and you get impatient, so you have to kill someone on the subway.
  [George Carlin]

reply via email to

[Prev in Thread] Current Thread [Next in Thread]