2 * Copyright (C) 2008 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * Common string pool for the profiler
21 static HashTable *gStringHashTable;
23 int hprofStartup_String()
25 gStringHashTable = dvmHashTableCreate(512, free);
26 if (gStringHashTable == NULL) {
27 return UNIQUE_ERROR();
32 int hprofShutdown_String()
34 dvmHashTableFree(gStringHashTable);
38 static u4 computeUtf8Hash(const char *str)
45 while ((c = *cp++) != '\0') {
52 hprof_string_id hprofLookupStringId(const char *str)
57 dvmHashTableLock(gStringHashTable);
59 hashValue = computeUtf8Hash(str);
60 val = dvmHashTableLookup(gStringHashTable, hashValue, (void *)str,
61 (HashCompareFunc)strcmp, false);
66 val = dvmHashTableLookup(gStringHashTable, hashValue, (void *)newStr,
67 (HashCompareFunc)strcmp, true);
71 dvmHashTableUnlock(gStringHashTable);
73 return (hprof_string_id)val;
76 int hprofDumpStrings(hprof_context_t *ctx)
79 hprof_record_t *rec = &ctx->curRec;
82 dvmHashTableLock(gStringHashTable);
84 for (err = 0, dvmHashIterBegin(gStringHashTable, &iter);
85 err == 0 && !dvmHashIterDone(&iter);
86 dvmHashIterNext(&iter))
88 err = hprofStartNewRecord(ctx, HPROF_TAG_STRING, HPROF_TIME);
92 str = (const char *)dvmHashIterData(&iter);
97 * ID: ID for this string
98 * [u1]*: UTF8 characters for string (NOT NULL terminated)
99 * (the record format encodes the length)
101 * We use the address of the string data as its ID.
103 err = hprofAddU4ToRecord(rec, (u4)str);
105 err = hprofAddUtf8StringToRecord(rec, str);
110 dvmHashTableUnlock(gStringHashTable);