blob: 7fb40272d43422a500373077d32200289b25406b [file] [log] [blame]
Jesse Wilsonc4824e62011-11-01 14:39:04 -04001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16/*
17 * Common string pool for the profiler
18 */
19#include "hprof.h"
20#include "object.h"
21#include "unordered_set.h"
22#include "logging.h"
23
24namespace art {
25
26namespace hprof {
27
28typedef std::tr1::unordered_set<String*, StringHashCode> StringSet;
29typedef std::tr1::unordered_set<String*, StringHashCode>::iterator StringSetIterator; // TODO: equals by VALUE not REFERENCE
30static Mutex strings_lock_("hprof strings");
31static StringSet strings_;
32
33int hprofStartup_String() {
34 return 0;
35}
36
37int hprofShutdown_String() {
38 return 0;
39}
40
41hprof_string_id hprofLookupStringId(String* string) {
42 MutexLock mu(strings_lock_);
43 std::pair<StringSetIterator, bool> result = strings_.insert(string);
44 String* present = *result.first;
45 return (hprof_string_id) present;
46}
47
48hprof_string_id hprofLookupStringId(const char* string) {
49 return hprofLookupStringId(String::AllocFromModifiedUtf8(string)); // TODO: leaks? causes GC?
50}
51
52hprof_string_id hprofLookupStringId(std::string string) {
53 return hprofLookupStringId(string.c_str()); // TODO: leaks? causes GC?
54}
55
56int hprofDumpStrings(hprof_context_t *ctx) {
57 MutexLock mu(strings_lock_);
58
59 hprof_record_t *rec = &ctx->curRec;
60
61 for (StringSetIterator it = strings_.begin(); it != strings_.end(); ++it) {
62 String* string = *it;
63 CHECK(string != NULL);
64
65 int err = hprofStartNewRecord(ctx, HPROF_TAG_STRING, HPROF_TIME);
66 if (err != 0) {
67 return err;
68 }
69
70 /* STRING format:
71 *
72 * ID: ID for this string
73 * [uint8_t]*: UTF8 characters for string (NOT NULL terminated)
74 * (the record format encodes the length)
75 *
76 * We use the address of the string data as its ID.
77 */
78 err = hprofAddU4ToRecord(rec, (uint32_t) string);
79 if (err != 0) {
80 return err;
81 }
82 err = hprofAddUtf8StringToRecord(rec, string->ToModifiedUtf8().c_str()); // TODO: leak?
83 if (err != 0) {
84 return err;
85 }
86 }
87
88 return 0;
89}
90
91} // namespace hprof
92
93} // namespace art