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.semispace;
014    
015    import org.mmtk.plan.*;
016    import org.mmtk.policy.CopyLocal;
017    import org.mmtk.policy.Space;
018    import org.mmtk.utility.alloc.Allocator;
019    
020    import org.vmmagic.unboxed.*;
021    import org.vmmagic.pragma.*;
022    
023    /**
024     * This class implements <i>per-mutator thread</i> behavior
025     * and state for the <i>SS</i> plan, which implements a full-heap
026     * semi-space collector.<p>
027     *
028     * Specifically, this class defines <i>SS</i> mutator-time allocation
029     * and per-mutator thread collection semantics (flushing and restoring
030     * per-mutator allocator state).<p>
031     *
032     * See {@link SS} for an overview of the semi-space algorithm.<p>
033     *
034     * @see SS
035     * @see SSCollector
036     * @see StopTheWorldMutator
037     * @see MutatorContext
038     */
039    @Uninterruptible
040    public class SSMutator extends StopTheWorldMutator {
041      /****************************************************************************
042       * Instance fields
043       */
044      protected final CopyLocal ss;
045    
046      /****************************************************************************
047       *
048       * Initialization
049       */
050    
051      /**
052       * Constructor
053       */
054      public SSMutator() {
055        ss = new CopyLocal();
056      }
057    
058      /**
059       * Called before the MutatorContext is used, but after the context has been
060       * fully registered and is visible to collection.
061       */
062      public void initMutator(int id) {
063        super.initMutator(id);
064        ss.rebind(SS.toSpace());
065      }
066    
067      /****************************************************************************
068       *
069       * Mutator-time allocation
070       */
071    
072      /**
073       * Allocate space (for an object)
074       *
075       * @param bytes The size of the space to be allocated (in bytes)
076       * @param align The requested alignment.
077       * @param offset The alignment offset.
078       * @param allocator The allocator number to be used for this allocation
079       * @param site Allocation site
080       * @return The address of the first byte of the allocated region
081       */
082      @Inline
083      public Address alloc(int bytes, int align, int offset, int allocator, int site) {
084        if (allocator == SS.ALLOC_SS)
085          return ss.alloc(bytes, align, offset);
086        else
087          return super.alloc(bytes, align, offset, allocator, site);
088      }
089    
090      /**
091       * Perform post-allocation actions.  For many allocators none are
092       * required.
093       *
094       * @param object The newly allocated object
095       * @param typeRef The type reference for the instance being created
096       * @param bytes The size of the space to be allocated (in bytes)
097       * @param allocator The allocator number to be used for this allocation
098       */
099      @Inline
100      public void postAlloc(ObjectReference object, ObjectReference typeRef,
101          int bytes, int allocator) {
102        if (allocator == SS.ALLOC_SS) return;
103        super.postAlloc(object, typeRef, bytes, allocator);
104      }
105    
106      /**
107       * Return the allocator instance associated with a space
108       * <code>space</code>, for this plan instance.
109       *
110       * @param space The space for which the allocator instance is desired.
111       * @return The allocator instance associated with this plan instance
112       * which is allocating into <code>space</code>, or <code>null</code>
113       * if no appropriate allocator can be established.
114       */
115      public Allocator getAllocatorFromSpace(Space space) {
116        if (space == SS.copySpace0 || space == SS.copySpace1) return ss;
117        return super.getAllocatorFromSpace(space);
118      }
119    
120      /****************************************************************************
121       *
122       * Collection
123       */
124    
125      /**
126       * Perform a per-mutator collection phase.
127       *
128       * @param phaseId The collection phase to perform
129       * @param primary Perform any single-threaded activities using this thread.
130       */
131      @Inline
132      public void collectionPhase(short phaseId, boolean primary) {
133        if (phaseId == SS.PREPARE) {
134          super.collectionPhase(phaseId, primary);
135          return;
136        }
137    
138        if (phaseId == SS.RELEASE) {
139          super.collectionPhase(phaseId, primary);
140          // rebind the allocation bump pointer to the appropriate semispace.
141          ss.rebind(SS.toSpace());
142          return;
143        }
144    
145        super.collectionPhase(phaseId, primary);
146      }
147    
148    
149      /****************************************************************************
150       *
151       * Miscellaneous
152       */
153    
154      /**
155       * Show the status of each of the allocators.
156       */
157      public final void show() {
158        ss.show();
159        los.show();
160        immortal.show();
161      }
162    
163    }