2011-01-27 16:11:27 -08:00
|
|
|
/*
|
2019-01-07 10:21:43 +01:00
|
|
|
* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.
|
2011-01-27 16:11:27 -08:00
|
|
|
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License version 2 only, as
|
|
|
|
* published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* version 2 for more details (a copy is included in the LICENSE file that
|
|
|
|
* accompanied this code).
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License version
|
|
|
|
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*
|
|
|
|
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
|
|
* or visit www.oracle.com if you need additional information or have any
|
|
|
|
* questions.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2019-01-10 15:13:51 -05:00
|
|
|
#ifndef SHARE_OOPS_SYMBOL_HPP
|
|
|
|
#define SHARE_OOPS_SYMBOL_HPP
|
2011-01-27 16:11:27 -08:00
|
|
|
|
|
|
|
#include "memory/allocation.hpp"
|
2016-04-12 09:53:43 +02:00
|
|
|
#include "utilities/exceptions.hpp"
|
2016-08-21 20:56:37 -04:00
|
|
|
#include "utilities/macros.hpp"
|
2011-01-27 16:11:27 -08:00
|
|
|
|
|
|
|
// A Symbol is a canonicalized string.
|
|
|
|
// All Symbols reside in global SymbolTable and are reference counted.
|
|
|
|
|
|
|
|
// Reference counting
|
|
|
|
//
|
|
|
|
// All Symbols are allocated and added to the SymbolTable.
|
|
|
|
// When a class is unloaded, the reference counts of the Symbol pointers in
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
// the ConstantPool and in InstanceKlass (see release_C_heap_structures) are
|
2011-01-27 16:11:27 -08:00
|
|
|
// decremented. When the reference count for a Symbol goes to 0, the garbage
|
|
|
|
// collector can free the Symbol and remove it from the SymbolTable.
|
|
|
|
//
|
|
|
|
// 0) Symbols need to be reference counted when a pointer to the Symbol is
|
|
|
|
// saved in persistent storage. This does not include the pointer
|
|
|
|
// in the SymbolTable bucket (the _literal field in HashtableEntry)
|
|
|
|
// that points to the Symbol. All other stores of a Symbol*
|
|
|
|
// to a field of a persistent variable (e.g., the _name filed in
|
2013-09-13 22:38:02 -04:00
|
|
|
// fieldDescriptor or _ptr in a CPSlot) is reference counted.
|
2011-01-27 16:11:27 -08:00
|
|
|
//
|
|
|
|
// 1) The lookup of a "name" in the SymbolTable either creates a Symbol F for
|
|
|
|
// "name" and returns a pointer to F or finds a pre-existing Symbol F for
|
|
|
|
// "name" and returns a pointer to it. In both cases the reference count for F
|
|
|
|
// is incremented under the assumption that a pointer to F will be created from
|
|
|
|
// the return value. Thus the increment of the reference count is on the lookup
|
|
|
|
// and not on the assignment to the new Symbol*. That is
|
|
|
|
// Symbol* G = lookup()
|
|
|
|
// ^ increment on lookup()
|
|
|
|
// and not
|
|
|
|
// Symbol* G = lookup()
|
|
|
|
// ^ increment on assignmnet
|
|
|
|
// The reference count must be decremented manually when the copy of the
|
|
|
|
// pointer G is destroyed.
|
|
|
|
//
|
|
|
|
// 2) For a local Symbol* A that is a copy of an existing Symbol* B, the
|
|
|
|
// reference counting is elided when the scope of B is greater than the scope
|
|
|
|
// of A. For example, in the code fragment
|
|
|
|
// below "klass" is passed as a parameter to the method. Symbol* "kn"
|
|
|
|
// is a copy of the name in "klass".
|
|
|
|
//
|
|
|
|
// Symbol* kn = klass->name();
|
|
|
|
// unsigned int d_hash = dictionary()->compute_hash(kn, class_loader);
|
|
|
|
//
|
|
|
|
// The scope of "klass" is greater than the scope of "kn" so the reference
|
|
|
|
// counting for "kn" is elided.
|
|
|
|
//
|
|
|
|
// Symbol* copied from ConstantPool entries are good candidates for reference
|
|
|
|
// counting elision. The ConstantPool entries for a class C exist until C is
|
|
|
|
// unloaded. If a Symbol* is copied out of the ConstantPool into Symbol* X,
|
|
|
|
// the Symbol* in the ConstantPool will in general out live X so the reference
|
|
|
|
// counting on X can be elided.
|
|
|
|
//
|
|
|
|
// For cases where the scope of A is not greater than the scope of B,
|
|
|
|
// the reference counting is explicitly done. See ciSymbol,
|
|
|
|
// ResolutionErrorEntry and ClassVerifier for examples.
|
|
|
|
//
|
|
|
|
// 3) When a Symbol K is created for temporary use, generally for substrings of
|
|
|
|
// an existing symbol or to create a new symbol, assign it to a
|
|
|
|
// TempNewSymbol. The SymbolTable methods new_symbol(), lookup()
|
|
|
|
// and probe() all potentially return a pointer to a new Symbol.
|
|
|
|
// The allocation (or lookup) of K increments the reference count for K
|
|
|
|
// and the destructor decrements the reference count.
|
|
|
|
//
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
// This cannot be inherited from ResourceObj because it cannot have a vtable.
|
|
|
|
// Since sometimes this is allocated from Metadata, pick a base allocation
|
|
|
|
// type without virtual functions.
|
|
|
|
class ClassLoaderData;
|
|
|
|
|
2018-07-20 14:52:11 -04:00
|
|
|
// Set _refcount to PERM_REFCOUNT to prevent the Symbol from being freed.
|
2015-07-15 12:24:41 -07:00
|
|
|
#ifndef PERM_REFCOUNT
|
2018-07-20 14:52:11 -04:00
|
|
|
#define PERM_REFCOUNT ((1 << 16) - 1)
|
2015-07-15 12:24:41 -07:00
|
|
|
#endif
|
|
|
|
|
2015-08-14 10:10:35 -07:00
|
|
|
class Symbol : public MetaspaceObj {
|
2011-01-27 16:11:27 -08:00
|
|
|
friend class VMStructs;
|
|
|
|
friend class SymbolTable;
|
2015-08-14 10:10:35 -07:00
|
|
|
|
2011-01-27 16:11:27 -08:00
|
|
|
private:
|
2018-07-20 14:52:11 -04:00
|
|
|
|
|
|
|
// This is an int because it needs atomic operation on the refcount. Mask length
|
|
|
|
// in high half word. length is the number of UTF8 characters in the symbol
|
|
|
|
volatile uint32_t _length_and_refcount;
|
2015-08-14 10:10:35 -07:00
|
|
|
short _identity_hash;
|
2018-10-03 09:46:46 -04:00
|
|
|
u1 _body[2];
|
2011-01-27 16:11:27 -08:00
|
|
|
|
|
|
|
enum {
|
2018-07-20 14:52:11 -04:00
|
|
|
// max_symbol_length must fit into the top 16 bits of _length_and_refcount
|
2011-01-27 16:11:27 -08:00
|
|
|
max_symbol_length = (1 << 16) -1
|
|
|
|
};
|
|
|
|
|
2017-08-02 18:06:38 -07:00
|
|
|
static int byte_size(int length) {
|
|
|
|
// minimum number of natural words needed to hold these bits (no non-heap version)
|
|
|
|
return (int)(sizeof(Symbol) + (length > 2 ? length - 2 : 0));
|
|
|
|
}
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
static int size(int length) {
|
2016-01-30 11:02:29 -05:00
|
|
|
// minimum number of natural words needed to hold these bits (no non-heap version)
|
2017-08-02 18:06:38 -07:00
|
|
|
return (int)heap_word_size(byte_size(length));
|
2011-01-27 16:11:27 -08:00
|
|
|
}
|
|
|
|
|
2018-10-03 09:46:46 -04:00
|
|
|
void byte_at_put(int index, u1 value) {
|
2018-07-20 14:52:11 -04:00
|
|
|
assert(index >=0 && index < length(), "symbol index overflow");
|
2011-01-27 16:11:27 -08:00
|
|
|
_body[index] = value;
|
|
|
|
}
|
|
|
|
|
2012-03-23 11:16:05 -04:00
|
|
|
Symbol(const u1* name, int length, int refcount);
|
2013-08-29 18:56:29 -04:00
|
|
|
void* operator new(size_t size, int len, TRAPS) throw();
|
|
|
|
void* operator new(size_t size, int len, Arena* arena, TRAPS) throw();
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
|
|
|
|
void operator delete(void* p);
|
2011-01-27 16:11:27 -08:00
|
|
|
|
2018-07-20 14:52:11 -04:00
|
|
|
static int extract_length(uint32_t value) { return value >> 16; }
|
|
|
|
static int extract_refcount(uint32_t value) { return value & 0xffff; }
|
|
|
|
static uint32_t pack_length_and_refcount(int length, int refcount);
|
|
|
|
|
|
|
|
int length() const { return extract_length(_length_and_refcount); }
|
|
|
|
|
2011-01-27 16:11:27 -08:00
|
|
|
public:
|
|
|
|
// Low-level access (used with care, since not GC-safe)
|
2018-10-03 09:46:46 -04:00
|
|
|
const u1* base() const { return &_body[0]; }
|
2011-01-27 16:11:27 -08:00
|
|
|
|
6964458: Reimplement class meta-data storage to use native memory
Remove PermGen, allocate meta-data in metaspace linked to class loaders, rewrite GC walking, rewrite and rename metadata to be C++ classes
Co-authored-by: Stefan Karlsson <stefan.karlsson@oracle.com>
Co-authored-by: Mikael Gerdin <mikael.gerdin@oracle.com>
Co-authored-by: Tom Rodriguez <tom.rodriguez@oracle.com>
Reviewed-by: jmasa, stefank, never, coleenp, kvn, brutisso, mgerdin, dholmes, jrose, twisti, roland
2012-09-01 13:25:18 -04:00
|
|
|
int size() { return size(utf8_length()); }
|
2017-08-02 18:06:38 -07:00
|
|
|
int byte_size() { return byte_size(utf8_length()); }
|
|
|
|
|
|
|
|
// Symbols should be stored in the read-only region of CDS archive.
|
|
|
|
static bool is_read_only_by_default() { return true; }
|
2011-01-27 16:11:27 -08:00
|
|
|
|
|
|
|
// Returns the largest size symbol we can safely hold.
|
2015-12-08 20:04:03 +01:00
|
|
|
static int max_length() { return max_symbol_length; }
|
|
|
|
unsigned identity_hash() const {
|
2015-08-14 10:10:35 -07:00
|
|
|
unsigned addr_bits = (unsigned)((uintptr_t)this >> (LogMinObjAlignmentInBytes + 3));
|
|
|
|
return ((unsigned)_identity_hash & 0xffff) |
|
2018-07-20 14:52:11 -04:00
|
|
|
((addr_bits ^ (length() << 8) ^ (( _body[0] << 8) | _body[1])) << 16);
|
2015-08-14 10:10:35 -07:00
|
|
|
}
|
2011-01-27 16:11:27 -08:00
|
|
|
|
|
|
|
// Reference counting. See comments above this class for when to use.
|
2018-07-20 14:52:11 -04:00
|
|
|
int refcount() const { return extract_refcount(_length_and_refcount); }
|
|
|
|
bool try_increment_refcount();
|
2013-02-27 09:40:30 +01:00
|
|
|
void increment_refcount();
|
|
|
|
void decrement_refcount();
|
2017-08-02 18:06:38 -07:00
|
|
|
bool is_permanent() {
|
2018-07-20 14:52:11 -04:00
|
|
|
return (refcount() == PERM_REFCOUNT);
|
2017-08-02 18:06:38 -07:00
|
|
|
}
|
2019-03-14 18:56:25 +01:00
|
|
|
void make_permanent();
|
2011-01-27 16:11:27 -08:00
|
|
|
|
2018-10-03 09:46:46 -04:00
|
|
|
// Function char_at() returns the Symbol's selected u1 byte as a char type.
|
|
|
|
//
|
|
|
|
// Note that all multi-byte chars have the sign bit set on all their bytes.
|
|
|
|
// No single byte chars have their sign bit set.
|
|
|
|
char char_at(int index) const {
|
2018-07-20 14:52:11 -04:00
|
|
|
assert(index >=0 && index < length(), "symbol index overflow");
|
2018-10-03 09:46:46 -04:00
|
|
|
return (char)base()[index];
|
2011-01-27 16:11:27 -08:00
|
|
|
}
|
|
|
|
|
2018-10-03 09:46:46 -04:00
|
|
|
const u1* bytes() const { return base(); }
|
2011-01-27 16:11:27 -08:00
|
|
|
|
2018-07-20 14:52:11 -04:00
|
|
|
int utf8_length() const { return length(); }
|
2011-01-27 16:11:27 -08:00
|
|
|
|
|
|
|
// Compares the symbol with a string.
|
2017-05-24 12:42:44 +02:00
|
|
|
bool equals(const char* str, int len) const {
|
|
|
|
int l = utf8_length();
|
|
|
|
if (l != len) return false;
|
|
|
|
while (l-- > 0) {
|
2018-10-03 09:46:46 -04:00
|
|
|
if (str[l] != char_at(l))
|
2017-05-24 12:42:44 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
assert(l == -1, "we should be at the beginning");
|
|
|
|
return true;
|
|
|
|
}
|
2011-01-27 16:11:27 -08:00
|
|
|
bool equals(const char* str) const { return equals(str, (int) strlen(str)); }
|
|
|
|
|
|
|
|
// Tests if the symbol starts with the given prefix.
|
|
|
|
bool starts_with(const char* prefix, int len) const;
|
|
|
|
bool starts_with(const char* prefix) const {
|
|
|
|
return starts_with(prefix, (int) strlen(prefix));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Tests if the symbol starts with the given prefix.
|
|
|
|
int index_of_at(int i, const char* str, int len) const;
|
|
|
|
|
|
|
|
// Three-way compare for sorting; returns -1/0/1 if receiver is </==/> than arg
|
|
|
|
// note that the ordering is not alfabetical
|
2015-12-08 20:04:03 +01:00
|
|
|
inline int fast_compare(const Symbol* other) const;
|
2011-01-27 16:11:27 -08:00
|
|
|
|
|
|
|
// Returns receiver converted to null-terminated UTF-8 string; string is
|
|
|
|
// allocated in resource area, or in the char buffer provided by caller.
|
|
|
|
char* as_C_string() const;
|
|
|
|
char* as_C_string(char* buf, int size) const;
|
|
|
|
|
2012-11-12 14:03:53 -08:00
|
|
|
// Returns an escaped form of a Java string.
|
|
|
|
char* as_quoted_ascii() const;
|
2011-01-27 16:11:27 -08:00
|
|
|
|
|
|
|
// Returns a null terminated utf8 string in a resource array
|
|
|
|
char* as_utf8() const { return as_C_string(); }
|
|
|
|
|
|
|
|
jchar* as_unicode(int& length) const;
|
|
|
|
|
|
|
|
// Treating this symbol as a class name, returns the Java name for the class.
|
|
|
|
// String is allocated in resource area if buffer is not provided.
|
|
|
|
// See Klass::external_name()
|
|
|
|
const char* as_klass_external_name() const;
|
|
|
|
const char* as_klass_external_name(char* buf, int size) const;
|
|
|
|
|
2019-04-04 09:39:44 +02:00
|
|
|
// Treating the symbol as a signature, print the return
|
|
|
|
// type to the outputStream. Prints external names as 'double' or
|
|
|
|
// 'java.lang.Object[][]'.
|
|
|
|
void print_as_signature_external_return_type(outputStream *os);
|
|
|
|
// Treating the symbol as a signature, print the parameter types
|
|
|
|
// seperated by ', ' to the outputStream. Prints external names as
|
|
|
|
// 'double' or 'java.lang.Object[][]'.
|
|
|
|
void print_as_signature_external_parameters(outputStream *os);
|
|
|
|
|
2017-08-02 18:06:38 -07:00
|
|
|
void metaspace_pointers_do(MetaspaceClosure* it);
|
|
|
|
MetaspaceObj::Type type() const { return SymbolType; }
|
|
|
|
|
2011-01-27 16:11:27 -08:00
|
|
|
// Printing
|
|
|
|
void print_symbol_on(outputStream* st = NULL) const;
|
2015-10-26 10:36:54 +01:00
|
|
|
void print_utf8_on(outputStream* st) const;
|
2011-01-27 16:11:27 -08:00
|
|
|
void print_on(outputStream* st) const; // First level print
|
|
|
|
void print_value_on(outputStream* st) const; // Second level print.
|
|
|
|
|
|
|
|
// printing on default output stream
|
|
|
|
void print() { print_on(tty); }
|
|
|
|
void print_value() { print_value_on(tty); }
|
|
|
|
|
2018-10-04 16:39:07 +02:00
|
|
|
static bool is_valid(Symbol* s);
|
|
|
|
|
2011-01-27 16:11:27 -08:00
|
|
|
#ifndef PRODUCT
|
|
|
|
// Empty constructor to create a dummy symbol object on stack
|
|
|
|
// only for getting its vtable pointer.
|
|
|
|
Symbol() { }
|
|
|
|
|
2018-08-14 18:42:14 -05:00
|
|
|
static size_t _total_count;
|
2011-01-27 16:11:27 -08:00
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
|
|
|
// Note: this comparison is used for vtable sorting only; it doesn't matter
|
|
|
|
// what order it defines, as long as it is a total, time-invariant order
|
|
|
|
// Since Symbol*s are in C_HEAP, their relative order in memory never changes,
|
|
|
|
// so use address comparison for speed
|
2015-12-08 20:04:03 +01:00
|
|
|
int Symbol::fast_compare(const Symbol* other) const {
|
2011-01-27 16:11:27 -08:00
|
|
|
return (((uintptr_t)this < (uintptr_t)other) ? -1
|
|
|
|
: ((uintptr_t)this == (uintptr_t) other) ? 0 : 1);
|
|
|
|
}
|
2019-01-10 15:13:51 -05:00
|
|
|
#endif // SHARE_OOPS_SYMBOL_HPP
|