home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #3 / NN_1993_3.iso / spool / comp / sys / mac / programm / 21966 < prev    next >
Encoding:
Text File  |  1993-01-22  |  2.6 KB  |  65 lines

  1. Newsgroups: comp.sys.mac.programmer
  2. Path: sparky!uunet!mcsun!chsun!bernina!bernina!neeri
  3. From: neeri@iis.ethz.ch (Matthias Neeracher)
  4. Subject: Re: Verifying valid handles, how to?
  5. In-Reply-To: gwatts@fnalo.fnal.gov's message of Fri, 22 Jan 1993 07:55:20 GMT
  6. Message-ID: <NEERI.93Jan22190934@iis.ethz.ch>
  7. Sender: news@bernina.ethz.ch (USENET News System)
  8. Organization: Integrated Systems Laboratory, ETH, Zurich
  9. References: <1993Jan22.015520.1@fnalo.fnal.gov>
  10. Date: Fri, 22 Jan 1993 18:09:34 GMT
  11. Lines: 52
  12.  
  13. In article <1993Jan22.015520.1@fnalo.fnal.gov>, gwatts@fnalo.fnal.gov writes:
  14. >   At any rate, I was thinking.  I've got only indirect objects in my project.
  15. > This means every object is a handle, right?  Well, why not, in the debug
  16. > version of the message dispatcher (oopDebug library) put a little code that
  17. > will check the object is infact allocated as a handle?
  18. >   I checked out the routine in msg.c (in the oops Libraries folder), and
  19. > the handle is stored in register a1.  I don't know, however, how to check
  20. > if it is a valid handle without causing an error (bus or otherwise) of
  21. > somesort.  Especially if it is a random number!  Anyone know?  Is there
  22. > some memory manager routine, given a suspected handle, will tell me this?
  23.  
  24. Here you go. This code is not guaranteed to work 100% of the time, but I doubt
  25. you will get it to produce an address error for any normal memory setup (One
  26. exception I can think of are macs with a memory upgrade that makes the ROM
  27. appear in the middle of the application heap).
  28.  
  29. /* Heuristic to determine whether a given address is a Handle            */
  30. /* Based on the articles of Lloyd Lim and Matthew T Russotto in the UMPG */
  31. /* This code may be redistributed without any restrictions               */
  32.  
  33. Boolean RealHandle(void * addr)
  34. {
  35.    THz   sysZone;
  36.    THz   applZone;
  37.    THz   heapZone;
  38.    
  39.    addr  =  StripAddress(addr);
  40.    if (addr && !((long) addr & 1))  {
  41.       sysZone  =  SystemZone();
  42.       applZone =  ApplicZone();
  43.       if (addr >= (Ptr) &sysZone->heapData   && 
  44.           addr <  (Ptr) sysZone->bkLim          ||
  45.           addr >= (Ptr) &applZone->heapData  && 
  46.           addr <  (Ptr) applZone->bkLim
  47.          )
  48.          if (*(long *)addr && !(*(long *)addr & 1)) {
  49.             heapZone =  HandleZone(addr);
  50.             if (!MemError())
  51.                if (heapZone == sysZone || heapZone == applZone)
  52.                   return true;
  53.          }
  54.    }
  55.    
  56.    return false;
  57. }
  58.  
  59. Matthias
  60.  
  61. -----
  62. Matthias Neeracher                                      neeri@iis.ethz.ch
  63.  `We say "gestalt" when things combine to act in ways we can't explain'
  64.                              -- Marvin Minsky, _The Society Of Mind_
  65.