Path: utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!yale!think!zaphod.mps.ohio-state.edu!usc!apple!apple.com!casseres From: casseres@apple.com (David Casseres) Newsgroups: comp.sys.mac.programmer Subject: An Obscure Lightspeed C/MPW C Incompatibility Message-ID: <6752@internal.Apple.COM> Date: 16 Feb 90 18:00:29 GMT References: <1256@kl-cs.UUCP> Sender: usenet@Apple.COM Organization: Apple Computer, Inc. Lines: 37 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!