1/*
2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
23 */
24
25package sun.jvm.hotspot.runtime;
26
27import java.io.*;
28import java.util.*;
29import sun.jvm.hotspot.debugger.*;
30import sun.jvm.hotspot.oops.*;
31import sun.jvm.hotspot.types.*;
32
33/** <P> ThreadLocalAllocBuffer: a descriptor for thread-local storage
34    used by the threads for allocation. </P>
35
36    <P> It is thread-private at any time, but maybe multiplexed over
37    time across multiple threads. </P> */
38
39public class ThreadLocalAllocBuffer extends VMObject {
40  private static AddressField  startField;
41  private static AddressField  topField;
42  private static AddressField  endField;
43  private static CIntegerField desired_sizeField;
44
45  static {
46    VM.registerVMInitializedObserver(new Observer() {
47        public void update(Observable o, Object data) {
48          initialize(VM.getVM().getTypeDataBase());
49        }
50      });
51  }
52
53  private static synchronized void initialize(TypeDataBase db) {
54    Type type = db.lookupType("ThreadLocalAllocBuffer");
55
56    startField         = type.getAddressField("_start");
57    topField           = type.getAddressField("_top");
58    endField           = type.getAddressField("_end");
59    desired_sizeField          = type.getCIntegerField("_desired_size");
60  }
61
62  public ThreadLocalAllocBuffer(Address addr) {
63    super(addr);
64  }
65
66  public Address start()    { return startField.getValue(addr); }
67  public Address end()      { return   endField.getValue(addr); }
68  public Address top()      { return   topField.getValue(addr); }
69  public Address hardEnd()  { return end().addOffsetTo(alignmentReserve()); }
70
71  private long alignmentReserve() {
72    return Oop.alignObjectSize(endReserve());
73  }
74
75  private long endReserve() {
76    long minFillerArraySize = Array.baseOffsetInBytes(BasicType.T_INT);
77    long reserveForAllocationPrefetch = VM.getVM().getReserveForAllocationPrefetch();
78    long heapWordSize = VM.getVM().getHeapWordSize();
79
80    return Math.max(minFillerArraySize, reserveForAllocationPrefetch * heapWordSize);
81  }
82
83  /** Support for iteration over heap -- not sure how this will
84      interact with GC in reflective system, but necessary for the
85      debugging mechanism */
86  public OopHandle startAsOopHandle() {
87    return startField.getOopHandle(addr);
88  }
89
90  /** Support for iteration over heap -- not sure how this will
91      interact with GC in reflective system, but necessary for the
92      debugging mechanism */
93  public OopHandle nextOopHandle(OopHandle handle, long size) {
94    return handle.addOffsetToAsOopHandle(size);
95  }
96
97  //  public int     size();           // FIXME: must adjust this to be in bytes
98  //  public int     availableSize();  // FIXME: must adjust this to be in bytes
99  public void print() {
100    printOn(System.out);
101  }
102
103  public boolean contains(Address p) {
104    if (top() == null) {
105      return false;
106    }
107    return (start().lessThanOrEqual(p) && top().greaterThan(p));
108  }
109
110  public void printOn(PrintStream tty) {
111    tty.println(" [" + start() + "," +
112                top() + "," + end() + ",{" + hardEnd() + "})");
113  }
114}
115