[comp.sys.mac.programmer] Can I count on any alignment from NewHandle or NewPtr?

dhoyt@vx.acs.umn.edu (DAVID HOYT) (03/11/91)

'''
  Can I count on any sort of alignment from the mac memory manager?  Or do I
have to roll my own by allocating n-1 more bytes than I really need?
  Also, is there any guarentee on stack alignment?  I'm trying to build a
garbage collector for MacApp/C++ and it would be faster if I could rely on
alignment.

  david | dhoyt@vx.acs.umn.edu | dhoyt@umnacvx.bitnet

mneerach@iiic.ethz.ch (Matthias Ulrich Neeracher) (03/11/91)

In article <3588@ux.acs.umn.edu>, dhoyt@vx.acs.umn.edu (DAVID HOYT) writes:
>'''
>  Can I count on any sort of alignment from the mac memory manager?  Or do I
>have to roll my own by allocating n-1 more bytes than I really need?
>  Also, is there any guarentee on stack alignment?  I'm trying to build a
>garbage collector for MacApp/C++ and it would be faster if I could rely on
>alignment.

You can count on word alignment, but not longword alignment both for heap
blocks and stack values. There are certain compilers which try to keep
the stack always longword aligned - one of them is described in a paper by
Michael Franz in a recent issue of SIGPLAN Notices. It *might be* that on
newer machines, heap blocks are longword aligned, but I know from painful
experience that at least on a Plus, this is not always true.

Matthias

-- 
Matthias Neeracher                                   mneerach@iiic.ethz.ch
   "These days, though, you have to be pretty technical before you can 
    even aspire to crudeness." -- William Gibson, _Johnny Mnemonic_

stevec@Apple.COM (Steve Christensen) (03/14/91)

In article <3588@ux.acs.umn.edu> dhoyt@vx.acs.umn.edu writes:
>  Can I count on any sort of alignment from the mac memory manager?  Or do I
>have to roll my own by allocating n-1 more bytes than I really need?
>  Also, is there any guarentee on stack alignment?  I'm trying to build a
>garbage collector for MacApp/C++ and it would be faster if I could rely on
>alignment.

As I recall, heap blocks are word aligned on 68000 Macs and are long word
aligned on 68020 and 68030 Macs.  The best you can hope for is word alignment
on the stack since both word- and long word-sized values are stored on the
stack...

steve

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Steve Christensen			Never hit a man with glasses.
  stevec@apple.com			Hit him with a baseball bat.

MacUserLabs@cup.portal.com (Stephan - Somogyi) (03/16/91)

stevec@Apple.COM (Steve Christensen) writes:
 
>As I recall, heap blocks are word aligned on 68000 Macs and are long
>word aligned on 68020 and 68030 Macs.
 
If true, this is too cool for words. Is there any way of finding out
for sure?
 
______________________________________________________________________
Stephan Somogyi      1/8 of the world's population, 2/3 of the lawyers
MacUser

Greg@AppleLink.Apple.Com (Greg Marriott) (03/17/91)

In article <40191@cup.portal.com>, MacUserLabs@cup.portal.com (Stephan - Somogyi) writes:
> 
> stevec@Apple.COM (Steve Christensen) writes:
>  
> >As I recall, heap blocks are word aligned on 68000 Macs and are long
> >word aligned on 68020 and 68030 Macs.
>  
> If true, this is too cool for words. Is there any way of finding out
> for sure?

Well, hop into Macsbug and do a heap dump (hd).  If every single block is
long aligned, then I'd say the odds are really good that you've found out
for sure.  Either that, or you should call Ripley's because the odds against
all allocations "accidentally" being long aligned are about a grillion to one.

Greg Marriott
Just Some Guy
Apple Computer, Inc.
adie ?
Qui est le plus coherent des deux pour la societe ?


-- 
serge@imag.fr

Lewis_P@cc.curtin.edu.au (Peter N Lewis) (03/18/91)

In article <12563@goofy.Apple.COM>, 
           Greg@AppleLink.Apple.Com (Greg Marriott) writes:
> Well, hop into Macsbug and do a heap dump (hd).  If every single block is
> long aligned, then I'd say the odds are really good that you've found out
> for sure.  Either that, or you should call Ripley's because the odds against
> all allocations "accidentally" being long aligned are about a grillion to one.

Well, according to this fine probabalistic proof,  the Classic definitely
word alligns the heap (would be bad if it didn't :-).  And by counter
example taken from the head it definitely does NOT long word allign the
heap.  I dont have a '30 machine so someone else will have to try that out.
(unless someone would like to give me an fx? :-)
   Peter.

Disclaimer:Curtin & I have an agreement:Neither of us listen to either of us.
*-------+---------+---------+---------+---------+---------+---------+-------*
Internet: Lewis_P@cc.curtin.edu.au              I Peter Lewis
Bitnet: Lewis_P%cc.curtin.edu.au@cunyvm.bitnet  I NCRPDA, Curtin University
UUCP: uunet!munnari.oz!cc.curtin.edu.au!Lewis_P I GPO Box U1987
PSImail: psi%050529452300070::Lewis_P           I Perth, WA, 6001, AUSTRALIA
Has anyone ever found someone who used a Mac and then Changed To a PC?

gurgle@well.sf.ca.us (Pete Gontier) (03/20/91)

In article <7496.27e4a472@cc.curtin.edu.au> Lewis_P@cc.curtin.edu.au (Peter N Lewis) writes:
>In article <12563@goofy.Apple.COM>, Greg@AppleLink.Apple.Com (Greg Marriott) writes:
>> Well, hop into Macsbug and do a heap dump (hd).  If every single block is
>> long aligned, then I'd say the odds are really good that you've found out
>> for sure.
>Well, according to this fine probabalistic proof,  the Classic definitely
>word alligns the heap (would be bad if it didn't :-).  And by counter
>example taken from the head it definitely does NOT long word allign the
>heap.

According to another probabalistic proof I have just derived, '030 machines,
or at least the IIci, do long word alignment. Now we need to hear from an
'020 user.
-- 
 Pete Gontier, gurgle@well.sf.ca.us
 Software Imagineer, Kiwi Software, Inc.

russotto@eng.umd.edu (Matthew T. Russotto) (03/21/91)

In article <23706@well.sf.ca.us> gurgle@well.sf.ca.us (Pete Gontier) writes:
>In article <7496.27e4a472@cc.curtin.edu.au> Lewis_P@cc.curtin.edu.au (Peter N Lewis) writes:
>>In article <12563@goofy.Apple.COM>, Greg@AppleLink.Apple.Com (Greg Marriott) writes:
>>> Well, hop into Macsbug and do a heap dump (hd).  If every single block is
>>> long aligned, then I'd say the odds are really good that you've found out
>>> for sure.
>>Well, according to this fine probabalistic proof,  the Classic definitely
>>word alligns the heap (would be bad if it didn't :-).  And by counter
>>example taken from the head it definitely does NOT long word allign the
>>heap.
>
>According to another probabalistic proof I have just derived, '030 machines,
>or at least the IIci, do long word alignment. Now we need to hear from an
>'020 user.

According to this same proof, the Mac II does long word alignment.
--
Matthew T. Russotto	russotto@eng.umd.edu	russotto@wam.umd.edu
     .sig under construction, like the rest of this campus.

stevec@Apple.COM (Steve Christensen) (03/21/91)

In reply to the general discussion on heap block and stack alignment:

Since we know that both heap blocks and elements on the stack are at least
word-aligned on every Macintosh, then perhaps it's best to just assume that
alignment for all Macs (whether 68000 or '020,'030) so that one's software
is compatible with all Macs.  Unless one knows one is runing on an '020 or
'030.

Of course, one shouldn't assume too much about low-level aspects of the Mac
since they have a notorious habit of changing...

steve

-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  Steve Christensen			Never hit a man with glasses.
  stevec@apple.com			Hit him with a baseball bat.

kaufman@neon.Stanford.EDU (Marc T. Kaufman) (03/21/91)

In article <50569@apple.Apple.COM> stevec@Apple.COM (Steve Christensen) writes:
>In reply to the general discussion on heap block and stack alignment:

>Since we know that both heap blocks and elements on the stack are at least
>word-aligned on every Macintosh, then perhaps it's best to just assume that
>alignment for all Macs (whether 68000 or '020,'030) so that one's software
>is compatible with all Macs.  Unless one knows one is runing on an '020 or
>'030.

I presume the original question was to determine if it was legal to use
longword moves, beginning at the origin of the block.  From the responses,
the answer is YES, in all cases.  The 68000 can do a longword move from any
word (16-bit) aligned address. [so can the 68020, but it takes two cycles,
so that is not very efficient].

Marc Kaufman (kaufman@Neon.stanford.edu)

dhoyt@ux.acs.umn.edu (David Hoyt) (03/21/91)

...
  Yes, I have checked the heap for alignment on my '20.  For the most
part things are quad (8 byte) aligned, but there are a few long aligned
blocks too.  That's what made me a bit nervious in the first place.  There
might be a word aligned object somewhere that I'm just missing.

  The responses that I have gotten seem to indicate that I can count on word
alignment (for now), but to count on long alignment would be dangerous
and in any case apple is bound to change things just to be ornery.  I'm
not sure I believe the last one; apple would have to be pretty stupid to
cause nonaligned accesses as it would slow everything down.  But it is possible
to write buggy memory managers.

david | dhoyt@ux.acs.umn.edu

MacUserLabs@cup.portal.com (Stephan - Somogyi) (03/22/91)

>I presume the original question was to determine if it was legal to
>use longword moves, beginning at the origin of the block.
 
The issue for me was whether my data structures would be longword
aligned or not, since longword-aligned data is sucked in faster on
020s and 030s.
 
The answer ended up being "heap blocks are longword aligned on CPUs
where it makes a difference." My thanks to the people who are
responsible for the Memory Manager behaving this way.
 
______________________________________________________________________
Stephan Somogyi      1/8 of the world's population, 2/3 of the lawyers
MacUser