[comp.sys.mac.programmer] An Obscure Lightspeed C/MPW C Incompatibility

casseres@apple.com (David Casseres) (02/17/90)

My apologies if this has been covered recently, I think it's worth telling 
about.  Recently, for reasons too irritating to explain, I had to port 
some C library code from MPW to Lightspeed 3.0.  This library code has to 
be compiled into an application under Lightspeed 3.0, but then it must 
interact with other code (a Mac driver) compiled under MPW.

Well, I dealt with all the obvious stuff like "int" being 4 bytes in MPW 
and 2 in Lightspeed, (and if you have to do this, remember that "unsigned" 
means "unsigned int" and functions without specified result types 
implicitly return "int".  Wow, wotta language!!!  It's so *portable*!!!).  
But the code didn't work; it crashed in various puzzling ways.

I spent way too much time finding out what the problem was:  MPW and 
Lightspeed C have different implementations of setjmp and longjmp!  
Specifically, MPW C saves the D2 register and and Lightspeed does not.  
Because of this, the jump buffer that is set up by setjmp and used by 
longjmp (type jmp_buf) is an array of 12 longs in MPW and 11 longs in 
Lightspeed.

The library code in question was being pretty intimate with the jump 
buffer; it assumed that it knew the right index for picking the stack 
pointer out of the jump buffer, for instance.  Of course it had the wrong 
index.  That was easy to fix, but things still didn't work.  The library 
code was passing jump buffers to the driver!  Aaaarrrghhh!  For a while I 
thought I would have to write my own implementation of setjmp and longjmp, 
compatible with MPW C, but fortunately it turned out the driver doesn't 
look inside the jump buffer; it just needs the buffer to be the right size 
(i.e. 12 instead of 11 longs) because it's a field of a data structure and 
the other fields have to be at the right offsets.  That was fixed by 
changing the declaration of jmp_buf in the setjmp.h file.

Whew.



David Casseres
     Exclaimer:  Hey!

tarr-michael@CS.YALE.EDU (michael tarr) (02/17/90)

I am looking for anyone willing to share LSC 4.0 code fragments that do
the following:
                                                                               
1. Create 1 or more offscreen pixmaps that I can draw or read a PICT2
file into.
     
2. Use copybits to move the offscreen maps to the visible window as fast
as possible.
   
                                                                        
I need to support 8 bit color and full screen images. One possibility is
buying a graphics library sold commercially. Does anyone know if such a
package exists -- there are lots for the IBMPC.
                                                                      
I am also looking for code fragments to force the menu bar to disappear
so that the visible window covers the entire screen.
                                                                            
The reason for all this is that I am an experimental psychologist that
wants to show images on the mac for experiments. The basic idea is to
throw up pictures that have been drawn beforehand as quickly is
possible. After the software is written I plan to distribute it **free**
to any psychologists who are interested in using it. I am rather annoyed
at academics who have developed packages and now sell it without sources
-- their stuff won't do what I want and I can't make use of techniques
they have discovered in my own programs since they won't share. Doesn't
seem very neighborly or in the spirit of the larger intellectual
community.

Last question: does anyone know if the EventRecord.when field returned
with a keyDown from WaitNextEvent is always guaranteed to be correct, no
matter what the system was doing at the time the key was pressed?

Thanks,

Mike Tarr
Yale University
tarr@yale.cs.edu or mtarr@yalevm.bitnet

ccc_ldo@waikato.ac.nz (02/19/90)

Which version of MPW C were you referring to? Remember, 2.0.2 and 3.0
are completely different compilers. At a guess, I'd say you were talking
about 2.0.2, but it would be nice to be sure.

casseres@apple.com (David Casseres) (02/20/90)

In article <160.25dfdf34@waikato.ac.nz> ccc_ldo@waikato.ac.nz writes:
> Which version of MPW C were you referring to? Remember, 2.0.2 and 3.0
> are completely different compilers. At a guess, I'd say you were talking
> about 2.0.2, but it would be nice to be sure.

Actually I was using 3.1.

David Casseres
     Exclaimer:  Hey!