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