Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
BEGIN LICENSE BLOCK ***** Version: EPL 1.0/GPL 2.0/LGPL 2.1 The contents of this file are subject to the Eclipse Public License Version 1.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.eclipse.org/legal/epl-v10.html Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for the specific language governing rights and limitations under the License. Copyright (C) 2002-2004 Jan Arne Petersen <jpetersen@uni-bonn.de> Copyright (C) 2002-2004 Anders Bengtsson <ndrsbngtssn@yahoo.se> Copyright (C) 2004-2006 Charles O Nutter <headius@headius.com> Copyright (C) 2004 Stefan Matthias Aust <sma@3plus4.de> Alternatively, the contents of this file may be used under the terms of either of the GNU General Public License Version 2 or later (the "GPL"), or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), in which case the provisions of the GPL or the LGPL are applicable instead of those above. If you wish to allow use of your version of this file only under the terms of either the GPL or the LGPL, and not to allow others to use your version of this file under the terms of the EPL, indicate your decision by deleting the provisions above and replace them with the notice and other provisions required by the GPL or the LGPL. If you do not delete the provisions above, a recipient may use your version of this file under the terms of any one of the EPL, the GPL or the LGPL. END LICENSE BLOCK ***
 
 package org.jruby.runtime;
 
 import java.util.*;
 
FIXME: This version is faster than the previous, but both suffer from a crucial flaw: It is impossible to create an ObjectSpace with an iterator that doesn't either: a. hold on to objects that might otherwise be collected or b. have no way to guarantee that a call to hasNext() will be correct or that a subsequent call to next() will produce an object. For our purposes, for now, this may be acceptable.
 
 public class ObjectSpace {
     private final ReferenceQueue<ObjectdeadReferences = new ReferenceQueue<Object>();
     private WeakReferenceListNode top;
 
     private final Map identities = new HashMap();
     private final Map identitiesByObject = new WeakIdentityHashMap();
     private static final AtomicLong maxId = new AtomicLong(1000);
 
     public void registerObjectId(long idIRubyObject object) {
         synchronized () {
             cleanIdentities();
             .put(idnew IdReference(objectid));
             .put(objectid);
         }
     }
 
     public static long calculateObjectId(Object object) {
         // Fixnums get all the odd IDs, so we use identityHashCode * 2
         return .getAndIncrement() * 2;
     }
     
     public long createAndRegisterObjectId(IRubyObject rubyObject) {
         synchronized () {
             Long longId = (Long.get(rubyObject);
             if (longId == null) {
                 longId = createId(rubyObject);
             }
             return longId.longValue();
         }
     }
 
     private long createId(IRubyObject object) {
         long id = calculateObjectId(object);
         registerObjectId(idobject);
         return id;
     }
 
     public IRubyObject id2ref(long id) {
         synchronized () {
             cleanIdentities();
             IdReference reference = (IdReference.get(Long.valueOf(id));
             if (reference == null) {
                return null;
            }
            return (IRubyObjectreference.get();
        }
    }
    private void cleanIdentities() {
        IdReference ref;
        while ((ref = (IdReference.poll()) != null)
            .remove(Long.valueOf(ref.id()));
    }
    @Deprecated
    public long idOf(IRubyObject rubyObject) {
        return createAndRegisterObjectId(rubyObject);
    }
    
    public void addFinalizer(IRubyObject objectIRubyObject proc) {
        object.addFinalizer(proc);
    }
    
    public void removeFinalizers(long id) {
        IRubyObject object = id2ref(id);
        if (object != null) {
            object.removeFinalizers();
        }
    }
    
    public void add(IRubyObject object) {
        if (true && object.getMetaClass() != null && !(object instanceof JavaProxy)) {
            getObjectGroup().add(object);
        } else {
            addIndividualWeakReference(object);
        }
    }
    private synchronized void addIndividualWeakReference(IRubyObject object) {
        cleanup();
         = new WeakReferenceListNode<Object>(object);
    }
    private synchronized void registerGroupSweeper() {
        if ( == null || .get() == null) {
             = new WeakReference<GroupSweeper>(new GroupSweeper());
        }
    }
    private synchronized void splitObjectGroups() {
        // Split apart each group into individual weak references
        WeakReferenceListNode node = ;
        while (node != null) {
            Object obj = node.get();
            if (obj instanceof ObjectGroup) {
                ObjectGroup objectGroup = (ObjectGroupobj;
                for (int i = 0; i < objectGroup.size(); i++) {
                    IRubyObject rubyObject = objectGroup.set(inull);
                    if (rubyObject != null) {
                         = new WeakReferenceListNode<Object>(rubyObject);
                    }
                }
            }
            node = node.nextNode;
        }
    }
    public synchronized Iterator iterator(RubyModule rubyClass) {
        final List<Reference<Object>> objList = new ArrayList<Reference<Object>>();
        WeakReferenceListNode current = ;
        while (current != null) {
            Object obj = current.get();
            if (obj instanceof IRubyObject) {
                IRubyObject rubyObject = (IRubyObject)current.get();
                if (rubyObject != null && rubyClass.isInstance(rubyObject)) {
                    objList.add(current);
                }
            } else if (obj instanceof ObjectGroup) {
                for (IRubyObject rubyObject : (ObjectGroupobj) {
                    if (rubyObject != null && rubyClass.isInstance(rubyObject)) {
                        objList.add(new WeakReference<Object>(rubyObject));
                    }
                }
            }
            current = current.nextNode;
        }
        return new Iterator() {
            private Iterator<Reference<Object>> iter = objList.iterator();
            public boolean hasNext() {
                throw new UnsupportedOperationException();
            }
            public Object next() {
                Object obj = null;
                while (.hasNext()) {
                    obj = .next().get();
                    if (obj != nullbreak;
                }
                return obj;
            }
            public void remove() {
                throw new UnsupportedOperationException();
            }
        };
    }
    private void cleanup(ReferenceQueue<?> referenceQueue) {
        WeakReferenceListNode reference;
        while ((reference = (WeakReferenceListNode)referenceQueue.poll()) != null) {
            reference.remove();
        }
    }
    private class WeakReferenceListNode<T> extends WeakReference<T> {
        private WeakReferenceListNode prevNode;
        private WeakReferenceListNode nextNode;
        public WeakReferenceListNode(T referentReferenceQueue<T> queueWeakReferenceListNode<?> next) {
            super(referentqueue);
            this. = next;
            if (next != null) {
                next.prevNode = this;
            }
        }
        private void remove() {
            if ( != null) {
                . = ;
            } else {
                 = ;
            }
            if ( != null) {
                . = ;
            }
        }
    }
    private static class IdReference extends WeakReference {
        private final long id;
        public IdReference(IRubyObject objectlong idReferenceQueue queue) {
            super(objectqueue);
            this. = id;
        }
        public long id() {
            return ;
        }
    }
    private ObjectGroup getObjectGroup() {
        Reference<ObjectGroupref = .get();
        ObjectGroup objectGroup = ref != null ? ref.get() : null;
        return objectGroup != null && !objectGroup.isFull() ? objectGroup : addObjectGroup();
    }
    private synchronized ObjectGroup addObjectGroup() {
        ObjectGroup objectGroup;
        WeakReferenceListNode<ObjectGroupref = new WeakReferenceListNode<ObjectGroup>(objectGroup = new ObjectGroup(),
                );
        .set(ref);
         = ref;
        if ( == nullregisterGroupSweeper();
        return objectGroup;
    }
    private static final class ObjectGroup extends AbstractList<IRubyObject> {
        private static final int MAX_OBJECTS_PER_GROUP = 64;
        private int nextIndex = 0;
        public boolean add(IRubyObject obj) {
            obj.getMetaClass().getObjectGroupAccessorForWrite().set(objthis);
            .set(obj);
            ++;
            return true;
        }
        @Override
        public IRubyObject get(int index) {
            return .get(index);
        }
        @Override
        public IRubyObject set(int indexIRubyObject element) {
            return .getAndSet(indexelement);
        }
        private boolean isFull() {
            return  >= .length();
        }
        public int size() {
            return .length();
        }
    }
    // This class is used as a low-memory cleaner.  When it is finalized, it will cause all groups to be split into
    // individual weak refs, so each object can be collected.
    private final class GroupSweeper {
        @Override
        protected void finalize() throws Throwable {
            try {
                splitObjectGroups();
            } finally {
                registerGroupSweeper();
                super.finalize();
            }
        }
    }
New to GrepCode? Check out our FAQ X