]>
git.saurik.com Git - android/aapt.git/blob - ResourceIdCache.cpp
e03f4f668a1e7ed2b9ead235e1774f0e990e33f3
2 // Copyright 2012 The Android Open Source Project
4 // Manage a resource ID cache.
6 #define LOG_TAG "ResourceIdCache"
8 #include <utils/String16.h>
10 #include "ResourceIdCache.h"
15 static size_t mHits
= 0;
16 static size_t mMisses
= 0;
17 static size_t mCollisions
= 0;
19 static const size_t MAX_CACHE_ENTRIES
= 2048;
20 static const android::String16
TRUE16("1");
21 static const android::String16
FALSE16("0");
24 // concatenation of the relevant strings into a single instance
25 android::String16 hashedName
;
29 CacheEntry(const android::String16
& name
, uint32_t resId
) : hashedName(name
), id(resId
) { }
32 static map
< uint32_t, CacheEntry
> mIdMap
;
35 // djb2; reasonable choice for strings when collisions aren't particularly important
36 static inline uint32_t hashround(uint32_t hash
, int c
) {
37 return ((hash
<< 5) + hash
) + c
; /* hash * 33 + c */
40 static uint32_t hash(const android::String16
& hashableString
) {
42 const char16_t* str
= hashableString
.string();
43 while (int c
= *str
++) hash
= hashround(hash
, c
);
49 static inline String16
makeHashableName(const android::String16
& package
,
50 const android::String16
& type
,
51 const android::String16
& name
,
53 String16 hashable
= String16(name
);
56 hashable
+= (onlyPublic
? TRUE16
: FALSE16
);
60 uint32_t ResourceIdCache::lookup(const android::String16
& package
,
61 const android::String16
& type
,
62 const android::String16
& name
,
64 const String16 hashedName
= makeHashableName(package
, type
, name
, onlyPublic
);
65 const uint32_t hashcode
= hash(hashedName
);
66 map
<uint32_t, CacheEntry
>::iterator item
= mIdMap
.find(hashcode
);
67 if (item
== mIdMap
.end()) {
74 if (hashedName
== (*item
).second
.hashedName
) {
76 return (*item
).second
.id
;
81 mIdMap
.erase(hashcode
);
85 // returns the resource ID being stored, for callsite convenience
86 uint32_t ResourceIdCache::store(const android::String16
& package
,
87 const android::String16
& type
,
88 const android::String16
& name
,
91 if (mIdMap
.size() < MAX_CACHE_ENTRIES
) {
92 const String16 hashedName
= makeHashableName(package
, type
, name
, onlyPublic
);
93 const uint32_t hashcode
= hash(hashedName
);
94 mIdMap
[hashcode
] = CacheEntry(hashedName
, resId
);
99 void ResourceIdCache::dump() {
100 printf("ResourceIdCache dump:\n");
101 printf("Size: %ld\n", mIdMap
.size());
102 printf("Hits: %ld\n", mHits
);
103 printf("Misses: %ld\n", mMisses
);
104 printf("(Collisions: %ld)\n", mCollisions
);