Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!usc!apple!keith From: keith@Apple.COM (Keith Rollin) Newsgroups: comp.sys.mac.programmer Subject: Re: Speaking of Munger... Message-ID: <42917@apple.Apple.COM> Date: 12 Jul 90 20:17:10 GMT References: <233@sun13.scri.fsu.edu> Organization: Apple Computer Inc, Cupertino, CA Lines: 84 In article <233@sun13.scri.fsu.edu> pepke@gw.scri.fsu.edu (Eric Pepke) writes: >Speaking of Munger, it's a cute little routine, but what happens when it >fails due to insufficient memory? > >I run this little test program: > >#include >#include >#include > >main() >{ > unsigned char buffer[1000]; > Handle block; > long beforeLength, afterLength; > > printf("Yahoo!\n"); > > block = NewHandle(0L); > > for (;;) > { > beforeLength = GetHandleSize(block); > Munger(block, GetHandleSize(block), (unsigned char *) 0, > 0L, buffer, (long) sizeof(buffer)); > afterLength = GetHandleSize(block); > > if (beforeLength + sizeof(buffer) > afterLength || MemError()) > { > printf("\nSize increased from %ld to %ld\n", > beforeLength, afterLength); > printf("MemError() == %d\n", MemError()); > exit(-1); > } > else > { > printf("."); > } > } >} > >and I get > >Yahoo! >........................................................................... >..... >........................................................................... >..... >........................................................................... >..... >....................................................................... >Size increased from 311000 to 311000 >MemError() == 0 > >So, when Munger fails due to lack of memory, you cannot tell from looking >at MemError. In this case, at least, it left the handle unchanged. Can >somebody who knows about the internals of Munger tell me if that is >guaranteed to be the case, or is there a possibility that the handle will >be corrupted in any way, such as being left larger than it was but not as >large as is needed? When Munger needs some more memory, it calls SetHandleSize. If SetHandleSize returns an error (returned in register D0), Munger exits immediately. This means that the error result code is still in D0, and you can retrieve it with a little bit of glue code or inline assembly. The reason why MemError isn't set is because the Memory Manager doesn't set it; that's the function of the glue code provided for those calls by your development system. Your call to something like SetHandleSize is really a call to subroutine that takes your parameters off the stack, puts them in registers, calls the function, and puts the result code in MemError. Since Munger is written in assembly, it doesn't use this little subroutine. Hence, MemError never gets set with the result code. I've heard that there will be an article on this in an upcoming issue of d e v e l o p. -- ------------------------------------------------------------------------------ Keith Rollin --- Apple Computer, Inc. --- Developer Technical Support INTERNET: keith@apple.com UUCP: {decwrl, hoptoad, nsc, sun, amdahl}!apple!keith "Argue for your Apple, and sure enough, it's yours" - Keith Rollin, Contusions