blob: 31749fd7ea510d4d6fe5f4253a1d2d804c2ffff5 [file] [log] [blame]
Brian Carlstrom7e93b502011-08-04 14:16:22 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#include "intern_table.h"
4
5#include "scoped_ptr.h"
6
7namespace art {
8
9InternTable::InternTable() {
10 intern_table_lock_ = Mutex::Create("InternTable::Lock");
11}
12
13void InternTable::VisitRoots(Heap::RootVistor* root_visitor, void* arg) {
14 MutexLock mu(intern_table_lock_);
15 typedef Table::const_iterator It; // TODO: C++0x auto
16 for (It it = intern_table_.begin(), end = intern_table_.end(); it != end; ++it) {
17 root_visitor(it->second, arg);
18 }
19}
20
21String* InternTable::Intern(int32_t utf16_length, const char* utf8_data_in) {
22 scoped_ptr<uint16_t> utf16_data_out(new uint16_t[utf16_length]);
23 String::ConvertModifiedUtf8ToUtf16(utf16_data_out.get(), utf8_data_in);
24 int32_t hash_code = String::ComputeUtf16Hash(utf16_data_out.get(), utf16_length);
25 {
26 MutexLock mu(intern_table_lock_);
27 typedef Table::const_iterator It; // TODO: C++0x auto
28 for (It it = intern_table_.find(hash_code), end = intern_table_.end(); it != end; ++it) {
29 String* string = it->second;
30 if (string->Equals(utf16_data_out.get(), 0, utf16_length)) {
31 return string;
32 }
33 }
34 String* new_string = String::AllocFromUtf16(utf16_length, utf16_data_out.get(), hash_code);
35 intern_table_.insert(std::make_pair(hash_code, new_string));
36 return new_string;
37 }
38}
39
40} // namespace art