Tue, 30 Apr 2013 11:56:52 -0700
8011661: Insufficient memory message says "malloc" when sometimes it should say "mmap"
Reviewed-by: coleenp, zgu, hseigel
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 */
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"
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 }
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 }
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);
51 return res;
52 }
54 void* Symbol::operator new(size_t sz, int len, ClassLoaderData* loader_data, TRAPS) {
55 address res;
56 int alloc_size = size(len)*HeapWordSize;
57 res = (address) Metaspace::allocate(loader_data, size(len), true,
58 Metaspace::NonClassType, CHECK_NULL);
59 return res;
60 }
62 void Symbol::operator delete(void *p) {
63 assert(((Symbol*)p)->refcount() == 0, "should not call this");
64 FreeHeap(p);
65 }
67 // ------------------------------------------------------------------
68 // Symbol::equals
69 //
70 // Compares the symbol with a string of the given length.
71 bool Symbol::equals(const char* str, int len) const {
72 int l = utf8_length();
73 if (l != len) return false;
74 while (l-- > 0) {
75 if (str[l] != (char) byte_at(l))
76 return false;
77 }
78 assert(l == -1, "we should be at the beginning");
79 return true;
80 }
83 // ------------------------------------------------------------------
84 // Symbol::starts_with
85 //
86 // Tests if the symbol starts with the specified prefix of the given
87 // length.
88 bool Symbol::starts_with(const char* prefix, int len) const {
89 if (len > utf8_length()) return false;
90 while (len-- > 0) {
91 if (prefix[len] != (char) byte_at(len))
92 return false;
93 }
94 assert(len == -1, "we should be at the beginning");
95 return true;
96 }
99 // ------------------------------------------------------------------
100 // Symbol::index_of
101 //
102 // Finds if the given string is a substring of this symbol's utf8 bytes.
103 // Return -1 on failure. Otherwise return the first index where str occurs.
104 int Symbol::index_of_at(int i, const char* str, int len) const {
105 assert(i >= 0 && i <= utf8_length(), "oob");
106 if (len <= 0) return 0;
107 char first_char = str[0];
108 address bytes = (address) ((Symbol*)this)->base();
109 address limit = bytes + utf8_length() - len; // inclusive limit
110 address scan = bytes + i;
111 if (scan > limit)
112 return -1;
113 for (; scan <= limit; scan++) {
114 scan = (address) memchr(scan, first_char, (limit + 1 - scan));
115 if (scan == NULL)
116 return -1; // not found
117 assert(scan >= bytes+i && scan <= limit, "scan oob");
118 if (memcmp(scan, str, len) == 0)
119 return (int)(scan - bytes);
120 }
121 return -1;
122 }
125 char* Symbol::as_C_string(char* buf, int size) const {
126 if (size > 0) {
127 int len = MIN2(size - 1, utf8_length());
128 for (int i = 0; i < len; i++) {
129 buf[i] = byte_at(i);
130 }
131 buf[len] = '\0';
132 }
133 return buf;
134 }
136 char* Symbol::as_C_string() const {
137 int len = utf8_length();
138 char* str = NEW_RESOURCE_ARRAY(char, len + 1);
139 return as_C_string(str, len + 1);
140 }
142 char* Symbol::as_C_string_flexible_buffer(Thread* t,
143 char* buf, int size) const {
144 char* str;
145 int len = utf8_length();
146 int buf_len = len + 1;
147 if (size < buf_len) {
148 str = NEW_RESOURCE_ARRAY(char, buf_len);
149 } else {
150 str = buf;
151 }
152 return as_C_string(str, buf_len);
153 }
155 void Symbol::print_symbol_on(outputStream* st) const {
156 ResourceMark rm;
157 st = st ? st : tty;
158 st->print("%s", as_quoted_ascii());
159 }
161 char* Symbol::as_quoted_ascii() const {
162 const char *ptr = (const char *)&_body[0];
163 int quoted_length = UTF8::quoted_ascii_length(ptr, utf8_length());
164 char* result = NEW_RESOURCE_ARRAY(char, quoted_length + 1);
165 UTF8::as_quoted_ascii(ptr, utf8_length(), result, quoted_length + 1);
166 return result;
167 }
169 jchar* Symbol::as_unicode(int& length) const {
170 Symbol* this_ptr = (Symbol*)this;
171 length = UTF8::unicode_length((char*)this_ptr->bytes(), utf8_length());
172 jchar* result = NEW_RESOURCE_ARRAY(jchar, length);
173 if (length > 0) {
174 UTF8::convert_to_unicode((char*)this_ptr->bytes(), result, length);
175 }
176 return result;
177 }
179 const char* Symbol::as_klass_external_name(char* buf, int size) const {
180 if (size > 0) {
181 char* str = as_C_string(buf, size);
182 int length = (int)strlen(str);
183 // Turn all '/'s into '.'s (also for array klasses)
184 for (int index = 0; index < length; index++) {
185 if (str[index] == '/') {
186 str[index] = '.';
187 }
188 }
189 return str;
190 } else {
191 return buf;
192 }
193 }
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 }
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 }
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 }
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 }
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 }
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("'");
254 for (int i = 0; i < utf8_length(); i++) {
255 st->print("%c", byte_at(i));
256 }
257 st->print("'");
258 }
259 }
261 // SymbolTable prints this in its statistics
262 NOT_PRODUCT(int Symbol::_total_count = 0;)