home *** CD-ROM | disk | FTP | other *** search
- /* SPIM S20 MIPS simulator.
- Copyright (C) 1990 by James Larus (larus@cs.wisc.edu).
-
- Macintosh Version by Philip Delaquess (delaques@gcg.com)
- Copyright (C) 1993 by Saunders College Publishing and Morgan Kaufman Publishers.
-
- SPIM is free software; you can redistribute it and/or modify it
- under the terms of the GNU General Public License as published by the
- Free Software Foundation; either version 1, or (at your option) any
- later version.
-
- SPIM is distributed in the hope that it will be useful, but WITHOUT
- ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
- FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
- for more details.
-
- You should have received a copy of the GNU General Public License
- along with GNU CC; see the file COPYING. If not, write to James R.
- Larus, Computer Sciences Department, University of Wisconsin--Madison,
- 1210 West Dayton Street, Madison, WI 53706, USA or to the Free
- Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
-
- Problems: 1) SPIM was designed for a huge virtual memory machine, so it mallocs
- ad hoc and doesn't have to free rigourously. 2) The malloc and free routines with
- THINK C are sorta flaky anyway. Loading a huge program, clearing everything, then
- loading the same program again runs out of memory and crashes nastily. Not all
- the mallocs are verified.
-
- Solution: Ask the Mac OS how much memory there is. Allocate a relocatable block of
- about 90% of that (after building most of the interface), move it high, and lock
- it. Malloc requests are satisfied from this giant block, with no housekeeping.
- Free requests are simply ignored. When the user wants to Clear All, we just reset
- the pointer to the beginning of our huge block and start again.
-
- */
-
- #define NUM 8
- #define DEN 10 /* we grab NUM/DEN of the total available memory */
-
- static Handle MacMemory;
- static Ptr NextAvailable;
- static Size MemorySize, AmountRemaining;
-
- /* CreateMacMemory
- *
- * called once at initialization, it allocates a huge relocatable block and locks it.
- */
-
- void CreateMacMemory()
- {
- Size grow, maximum;
-
- maximum = MaxMem(&grow);
- AmountRemaining = MemorySize = (maximum * NUM) / DEN;
- MacMemory = NewHandle(MemorySize);
- HLock(MacMemory);
- NextAvailable = *MacMemory;
- } /* CreateMacMemory */
-
- /* ResetMacMemory
- *
- * resets the memory pointer to the beginning of the block.
- */
-
- void ResetMacMemory()
- {
- AmountRemaining = MemorySize;
- NextAvailable = *MacMemory;
- } /* ResetMacMemory */
-
- /* FreeMacMemory
- *
- * gives the big block back to the OS.
- */
-
- void FreeMacMemory()
- {
- HUnlock(MacMemory);
- DisposHandle(MacMemory);
- } /* FreeMacMemory */
-
- /* malloc
- *
- * is our treacherous friend from the standard library. This version just grabs the
- * next several bytes from the big block.
- */
-
- void *malloc(Size size)
- {
- void *temp;
-
- if ( size > AmountRemaining )
- return (void *) 0;
- temp = NextAvailable;
- NextAvailable += size;
- AmountRemaining -= size;
-
- /* force even */
- if ( (long) NextAvailable & 0x01 ) {
- ++NextAvailable;
- --AmountRemaining;
- }
- return temp;
- } /* malloc */
-
- /* realloc and free
- *
- * are do-nothing stubs.
- */
-
- void *realloc(void *ptr, Size size)
- {
- } /* realloc */
-
- void free(void *ptr)
- {
- } /* free */
-