001    /*
002     *  This file is part of the Jikes RVM project (http://jikesrvm.org).
003     *
004     *  This file is licensed to You under the Eclipse Public License (EPL);
005     *  You may not use this file except in compliance with the License. You
006     *  may obtain a copy of the License at
007     *
008     *      http://www.opensource.org/licenses/eclipse-1.0.php
009     *
010     *  See the COPYRIGHT.txt file distributed with this work for information
011     *  regarding copyright ownership.
012     */
013    package org.mmtk.plan.immix;
014    
015    import org.mmtk.plan.*;
016    import org.mmtk.policy.Space;
017    import org.mmtk.policy.immix.MutatorLocal;
018    
019    import org.mmtk.utility.alloc.Allocator;
020    
021    import org.vmmagic.pragma.*;
022    import org.vmmagic.unboxed.*;
023    
024    /**
025     * This class implements <i>per-mutator thread</i> behavior
026     * and state for the <i>Immix</i> plan, which implements a full-heap
027     * immix collector.<p>
028     *
029     * Specifically, this class defines <i>Immix</i> mutator-time allocation
030     * and per-mutator thread collection semantics (flushing and restoring
031     * per-mutator allocator state).<p>
032     *
033     * @see Immix
034     * @see org.mmtk.policy.immix.CollectorLocal
035     * @see StopTheWorldMutator
036     * @see MutatorContext
037     */
038    @Uninterruptible
039    public class ImmixMutator extends StopTheWorldMutator {
040    
041      /****************************************************************************
042       * Instance fields
043       */
044      protected final MutatorLocal immix;
045    
046      /****************************************************************************
047       *
048       * Initialization
049       */
050    
051      /**
052       * Constructor
053       */
054      public ImmixMutator() {
055        immix = new org.mmtk.policy.immix.MutatorLocal(Immix.immixSpace, false);
056      }
057    
058      /****************************************************************************
059       *
060       * MutatorLocal-time allocation
061       */
062    
063      /**
064       * Allocate memory for an object. This class handles the default allocator
065       * from the mark sweep space, and delegates everything else to the
066       * superclass.
067       *
068       * @param bytes The number of bytes required for the object.
069       * @param align Required alignment for the object.
070       * @param offset Offset associated with the alignment.
071       * @param allocator The allocator associated with this request.
072       * @return The low address of the allocated memory.
073       */
074      @Inline
075      public Address alloc(int bytes, int align, int offset, int allocator, int site) {
076        if (allocator == Immix.ALLOC_DEFAULT)
077          return immix.alloc(bytes, align, offset);
078        return super.alloc(bytes, align, offset, allocator, site);
079      }
080    
081      /**
082       * Perform post-allocation actions.  Initialize the object header for
083       * objects in the mark-sweep space, and delegate to the superclass for
084       * other objects.
085       *
086       * @param ref The newly allocated object
087       * @param typeRef the type reference for the instance being created
088       * @param bytes The size of the space to be allocated (in bytes)
089       * @param allocator The allocator number to be used for this allocation
090       */
091      @Inline
092      public void postAlloc(ObjectReference ref, ObjectReference typeRef,
093          int bytes, int allocator) {
094        if (allocator == Immix.ALLOC_DEFAULT)
095          Immix.immixSpace.postAlloc(ref, bytes);
096        else
097          super.postAlloc(ref, typeRef, bytes, allocator);
098      }
099    
100      /**
101       * Return the allocator instance associated with a space
102       * <code>space</code>, for this plan instance.
103       *
104       * @param space The space for which the allocator instance is desired.
105       * @return The allocator instance associated with this plan instance
106       * which is allocating into <code>space</code>, or <code>null</code>
107       * if no appropriate allocator can be established.
108       */
109      public Allocator getAllocatorFromSpace(Space space) {
110        if (space == Immix.immixSpace) return immix;  // FIXME is it not a problem that we have a 2:1 mapping?
111        return super.getAllocatorFromSpace(space);
112      }
113    
114      /****************************************************************************
115       *
116       * Collection
117       */
118    
119      /**
120       * Perform a per-mutator collection phase.
121       *
122       * @param phaseId The collection phase to perform
123       * @param primary Perform any single-threaded activities using this thread.
124       */
125      @Inline
126      public void collectionPhase(short phaseId, boolean primary) {
127    
128        if (phaseId == Immix.PREPARE) {
129          super.collectionPhase(phaseId, primary);
130          immix.prepare();
131          return;
132        }
133    
134        if (phaseId == Immix.RELEASE) {
135          immix.release();
136          super.collectionPhase(phaseId, primary);
137          return;
138        }
139    
140        super.collectionPhase(phaseId, primary);
141      }
142    }