001    // HeapMemory.java, created by wbeebee
002    // Copyright (C) 2001 Wes Beebee <wbeebee@mit.edu>
003    // Licensed under the terms of the GNU GPL; see COPYING for details.
004    package javax.realtime;
005    
006    import java.lang.Runtime;
007    
008    /**
009     * @author Wes Beebee <<a href="mailto:wbeebee@mit.edu">wbeebee@mit.edu</a>>
010     */
011    
012    /** The <code>HeapMemory</code> class is a singleton object that allows logic
013     *  within other scoped memory to allocate objects in the Java heap.
014     */
015    public final class HeapMemory extends MemoryArea {
016        /** The one and only HeapMemory. */
017        private static HeapMemory theHeap;
018        
019        private HeapMemory() {  
020            super(1000000000); // Totally bogus
021            //      super(Runtime.getRuntime().totalMemory());  // still bogus
022            //      memoryConsumed = size - Runtime.getRuntime().freeMemory();
023    
024            heap = true;
025        }
026    
027        /** Return a pointer to the singleton instace of <code>HeapMemory</code>
028         *  representing the Java heap.
029         *
030         *  @return The singleton <code>HeapMemory</code> object.
031         */
032        public static HeapMemory instance() {
033            if (theHeap == null) { // Bypass static initializer problem.
034                theHeap = new HeapMemory();
035            }
036            return theHeap;
037        }
038        
039        /** Indicates the amount of memory currently allocated in the Java heap.
040         *
041         *  @return The amount of allocated memory in the Java heap in bytes.
042         */
043        public long memoryConsumed() {
044            return memoryConsumed;
045        }
046    
047        /** Indicates the free memory remaining in the Java heap.
048         *
049         *  @return The amount of free memory remaining in the Java heap in bytes.
050         */
051        public long memoryRemaining() {
052            return (size - memoryConsumed);
053        }
054    
055        /** Initialize the native component of the HeapMemory. */
056        protected native void initNative(long sizeInBytes);
057    
058        /** Print a helpful string describing this HeapMemory. */
059        public String toString() {
060            return "HeapMemory: " + super.toString();
061        }
062    
063        /** Create a new array, checking to make sure that we're not in a 
064         *  NoHeapRealtimeThread. 
065         */
066        public synchronized Object newArray(Class type,
067                                            int number) 
068            throws IllegalAccessException, InstantiationException, OutOfMemoryError
069        {
070            checkNoHeap();
071            return super.newArray(type, number);
072        }
073    
074        /** Create a new multi-dimensional array, checking to make sure that 
075         *  we're not in a NoHeapRealtimeThread. 
076         */
077    
078        public synchronized Object newArray(Class type,
079                                            int[] dimensions) 
080            throws IllegalAccessException, OutOfMemoryError
081        {
082            checkNoHeap();
083            return super.newArray(type, dimensions);
084        }
085    
086        /** Create a new object, checking to make sure that we're not in a 
087         *  NoHeapRealtimeThread.
088         */
089    
090        public synchronized Object newInstance(Class type, 
091                                               Class[] parameterTypes,
092                                               Object[] parameters) 
093            throws IllegalAccessException, InstantiationException,
094                   OutOfMemoryError {
095            checkNoHeap();
096            return super.newInstance(type, parameterTypes, parameters);
097        }
098    
099        /** Create a new object, checking to make sure that we're not in a 
100         *  NoHeapRealtimeThread.
101         */
102    
103        public synchronized Object newInstance(Class type) 
104            throws IllegalAccessException, InstantiationException,
105                   OutOfMemoryError {
106            checkNoHeap();
107            return super.newInstance(type);
108        }
109    
110        /** Check to make sure that we're not in a NoHeapRealtimeThread. */
111    
112        public void checkNoHeap() {
113            RealtimeThread realtimeThread = 
114                RealtimeThread.currentRealtimeThread();
115            if (realtimeThread.noHeap) {
116                throw new IllegalAssignmentError("Cannot assign to " + toString()
117                                                 + " from " + 
118                                                 realtimeThread.toString());
119            }
120        }
121    }