1 /*
2 * Copyright (c) 1997, 2012, 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
25
26 #include "precompiled.hpp"
27 #include "classfile/altHashing.hpp"
28 #include "classfile/classLoaderData.hpp"
29 #include "oops/symbol.hpp"
30 #include "runtime/os.hpp"
31 #include "memory/allocation.inline.hpp"
32 #include "memory/resourceArea.hpp"
33
34 Symbol::Symbol(const u1* name, int length, int refcount) : _refcount(refcount), _length(length) {
35 _identity_hash = os::random();
36 for (int i = 0; i < _length; i++) {
37 byte_at_put(i, name[i]);
38 }
39 }
40
41 void* Symbol::operator new(size_t sz, int len, TRAPS) {
42 int alloc_size = size(len)*HeapWordSize;
43 address res = (address) AllocateHeap(alloc_size, mtSymbol);
44 return res;
45 }
46
47 void* Symbol::operator new(size_t sz, int len, Arena* arena, TRAPS) {
48 int alloc_size = size(len)*HeapWordSize;
49 address res = (address)arena->Amalloc(alloc_size);
191 }
192 }
193
194 const char* Symbol::as_klass_external_name() const {
195 char* str = as_C_string();
196 int length = (int)strlen(str);
197 // Turn all '/'s into '.'s (also for array klasses)
198 for (int index = 0; index < length; index++) {
199 if (str[index] == '/') {
200 str[index] = '.';
201 }
202 }
203 return str;
204 }
205
206 // Alternate hashing for unbalanced symbol tables.
207 unsigned int Symbol::new_hash(jint seed) {
208 ResourceMark rm;
209 // Use alternate hashing algorithm on this symbol.
210 return AltHashing::murmur3_32(seed, (const jbyte*)as_C_string(), utf8_length());
211 }
212
213 void Symbol::print_on(outputStream* st) const {
214 if (this == NULL) {
215 st->print_cr("NULL");
216 } else {
217 st->print("Symbol: '");
218 print_symbol_on(st);
219 st->print("'");
220 st->print(" count %d", refcount());
221 }
222 }
223
224 // The print_value functions are present in all builds, to support the
225 // disassembler and error reporting.
226 void Symbol::print_value_on(outputStream* st) const {
227 if (this == NULL) {
228 st->print("NULL");
229 } else {
230 st->print("'");
|
1 /*
2 * Copyright (c) 1997, 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
25
26 #include "precompiled.hpp"
27 #include "classfile/altHashing.hpp"
28 #include "classfile/classLoaderData.hpp"
29 #include "oops/symbol.hpp"
30 #include "runtime/atomic.inline.hpp"
31 #include "runtime/os.hpp"
32 #include "memory/allocation.inline.hpp"
33 #include "memory/resourceArea.hpp"
34
35 Symbol::Symbol(const u1* name, int length, int refcount) : _refcount(refcount), _length(length) {
36 _identity_hash = os::random();
37 for (int i = 0; i < _length; i++) {
38 byte_at_put(i, name[i]);
39 }
40 }
41
42 void* Symbol::operator new(size_t sz, int len, TRAPS) {
43 int alloc_size = size(len)*HeapWordSize;
44 address res = (address) AllocateHeap(alloc_size, mtSymbol);
45 return res;
46 }
47
48 void* Symbol::operator new(size_t sz, int len, Arena* arena, TRAPS) {
49 int alloc_size = size(len)*HeapWordSize;
50 address res = (address)arena->Amalloc(alloc_size);
192 }
193 }
194
195 const char* Symbol::as_klass_external_name() const {
196 char* str = as_C_string();
197 int length = (int)strlen(str);
198 // Turn all '/'s into '.'s (also for array klasses)
199 for (int index = 0; index < length; index++) {
200 if (str[index] == '/') {
201 str[index] = '.';
202 }
203 }
204 return str;
205 }
206
207 // Alternate hashing for unbalanced symbol tables.
208 unsigned int Symbol::new_hash(jint seed) {
209 ResourceMark rm;
210 // Use alternate hashing algorithm on this symbol.
211 return AltHashing::murmur3_32(seed, (const jbyte*)as_C_string(), utf8_length());
212 }
213
214 void Symbol::increment_refcount() {
215 // Only increment the refcount if positive. If negative either
216 // overflow has occurred or it is a permanent symbol in a read only
217 // shared archive.
218 if (_refcount >= 0) {
219 Atomic::inc(&_refcount);
220 NOT_PRODUCT(Atomic::inc(&_total_count);)
221 }
222 }
223
224 void Symbol::decrement_refcount() {
225 if (_refcount >= 0) {
226 Atomic::dec(&_refcount);
227 #ifdef ASSERT
228 if (_refcount < 0) {
229 print();
230 assert(false, "reference count underflow for symbol");
231 }
232 #endif
233 }
234 }
235
236 void Symbol::print_on(outputStream* st) const {
237 if (this == NULL) {
238 st->print_cr("NULL");
239 } else {
240 st->print("Symbol: '");
241 print_symbol_on(st);
242 st->print("'");
243 st->print(" count %d", refcount());
244 }
245 }
246
247 // The print_value functions are present in all builds, to support the
248 // disassembler and error reporting.
249 void Symbol::print_value_on(outputStream* st) const {
250 if (this == NULL) {
251 st->print("NULL");
252 } else {
253 st->print("'");
|