hotspot/src/share/vm/oops/symbolOop.hpp
author kvn
Wed, 21 May 2008 16:31:35 -0700
changeset 591 04d2e26e6d69
parent 1 489c9b5090e2
child 4567 7fc02fbe5c7a
permissions -rw-r--r--
6703888: Compressed Oops: use the 32-bits gap after klass in a object Summary: Use the gap also for a narrow oop field and a boxing object value. Reviewed-by: coleenp, never
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
489c9b5090e2 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2005 Sun Microsystems, Inc.  All Rights Reserved.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    19
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    20
 * CA 95054 USA or visit www.sun.com if you need additional information or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    21
 * have any questions.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
// A symbolOop is a canonicalized string.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
// All symbolOops reside in global symbolTable.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
// See oopFactory::new_symbol for how to allocate a symbolOop
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
class symbolOopDesc : public oopDesc {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
  friend class VMStructs;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
  unsigned short _length; // number of UTF8 characters in the symbol
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  jbyte _body[1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
  enum {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
    // max_symbol_length is constrained by type of _length
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
    max_symbol_length = (1 << 16) -1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
  };
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  // Low-level access (used with care, since not GC-safe)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  jbyte* base() { return &_body[0]; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  // Returns the largest size symbol we can safely hold.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  static int max_length() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
    return max_symbol_length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  static int object_size(int length) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
    int size = header_size() + (sizeof(unsigned short) + length + HeapWordSize - 1) / HeapWordSize;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
    return align_object_size(size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  int object_size() { return object_size(utf8_length()); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  int byte_at(int index) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
    assert(index >=0 && index < _length, "symbol index overflow");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
    return ((symbolOopDesc*)this)->base()[index];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  void byte_at_put(int index, int value) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
    assert(index >=0 && index < _length, "symbol index overflow");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
    ((symbolOopDesc*)this)->base()[index] = value;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  jbyte* bytes() { return base(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  int utf8_length() const { return _length; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  void set_utf8_length(int len) { _length = len; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  // Compares the symbol with a string
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  bool equals(const char* str, int len) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  // Three-way compare for sorting; returns -1/0/1 if receiver is </==/> than arg
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  // note that the ordering is not alfabetical
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  inline int fast_compare(symbolOop other) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  // Returns receiver converted to null-terminated UTF-8 string; string is
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  // allocated in resource area, or in the char buffer provided by caller.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  char* as_C_string() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  char* as_C_string(char* buf, int size) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  // Use buf if needed buffer length is <= size.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  char* as_C_string_flexible_buffer(Thread* t, char* buf, int size) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  // Returns a null terminated utf8 string in a resource array
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  char* as_utf8() const { return as_C_string(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  char* as_utf8_flexible_buffer(Thread* t, char* buf, int size) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
    return as_C_string_flexible_buffer(t, buf, size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  jchar* as_unicode(int& length) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  // Treating this symbol as a class name, returns the Java name for the class.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  // String is allocated in resource area if buffer is not provided.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  // See Klass::external_name()
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  const char* as_klass_external_name() const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  const char* as_klass_external_name(char* buf, int size) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  bool object_is_parsable() const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
    return (utf8_length() > 0 || (oop)this == Universe::emptySymbol());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  // Printing
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  void print_symbol_on(outputStream* st = NULL);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
// Note: this comparison is used for vtable sorting only; it doesn't matter
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
// what order it defines, as long as it is a total, time-invariant order
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
// Since symbolOops are in permSpace, their relative order in memory never changes,
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
// so use address comparison for speed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
int symbolOopDesc::fast_compare(symbolOop other) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
 return (((uintptr_t)this < (uintptr_t)other) ? -1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
   : ((uintptr_t)this == (uintptr_t) other) ? 0 : 1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
}