| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  ** Copyright 2011, 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 | #define LOG_TAG "BlobCache" | 
 | 18 | //#define LOG_NDEBUG 0 | 
 | 19 |  | 
| Mark Salyzyn | 387c561 | 2014-04-17 08:32:11 -0700 | [diff] [blame] | 20 | #include <inttypes.h> | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 21 | #include <stdlib.h> | 
 | 22 | #include <string.h> | 
 | 23 |  | 
 | 24 | #include <utils/BlobCache.h> | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 25 | #include <utils/Errors.h> | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 26 | #include <utils/Log.h> | 
 | 27 |  | 
| Michael Lentine | 6078805 | 2015-05-18 13:04:01 -0700 | [diff] [blame] | 28 | #include <cutils/properties.h> | 
 | 29 |  | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 30 | namespace android { | 
 | 31 |  | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 32 | // BlobCache::Header::mMagicNumber value | 
| Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 33 | static const uint32_t blobCacheMagic = ('_' << 24) + ('B' << 16) + ('b' << 8) + '$'; | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 34 |  | 
 | 35 | // BlobCache::Header::mBlobCacheVersion value | 
| Michael Lentine | 6078805 | 2015-05-18 13:04:01 -0700 | [diff] [blame] | 36 | static const uint32_t blobCacheVersion = 3; | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 37 |  | 
 | 38 | // BlobCache::Header::mDeviceVersion value | 
 | 39 | static const uint32_t blobCacheDeviceVersion = 1; | 
 | 40 |  | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 41 | BlobCache::BlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize): | 
 | 42 |         mMaxKeySize(maxKeySize), | 
 | 43 |         mMaxValueSize(maxValueSize), | 
 | 44 |         mMaxTotalSize(maxTotalSize), | 
 | 45 |         mTotalSize(0) { | 
 | 46 |     nsecs_t now = systemTime(SYSTEM_TIME_MONOTONIC); | 
| Kenny Root | 967ad86 | 2011-06-15 20:41:15 -0700 | [diff] [blame] | 47 | #ifdef _WIN32 | 
 | 48 |     srand(now); | 
 | 49 | #else | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 50 |     mRandState[0] = (now >> 0) & 0xFFFF; | 
 | 51 |     mRandState[1] = (now >> 16) & 0xFFFF; | 
 | 52 |     mRandState[2] = (now >> 32) & 0xFFFF; | 
| Kenny Root | 967ad86 | 2011-06-15 20:41:15 -0700 | [diff] [blame] | 53 | #endif | 
| Mark Salyzyn | 5bed803 | 2014-04-30 11:10:46 -0700 | [diff] [blame] | 54 |     ALOGV("initializing random seed using %lld", (unsigned long long)now); | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 55 | } | 
 | 56 |  | 
 | 57 | void BlobCache::set(const void* key, size_t keySize, const void* value, | 
 | 58 |         size_t valueSize) { | 
 | 59 |     if (mMaxKeySize < keySize) { | 
| Mark Salyzyn | 387c561 | 2014-04-17 08:32:11 -0700 | [diff] [blame] | 60 |         ALOGV("set: not caching because the key is too large: %zu (limit: %zu)", | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 61 |                 keySize, mMaxKeySize); | 
 | 62 |         return; | 
 | 63 |     } | 
 | 64 |     if (mMaxValueSize < valueSize) { | 
| Mark Salyzyn | 387c561 | 2014-04-17 08:32:11 -0700 | [diff] [blame] | 65 |         ALOGV("set: not caching because the value is too large: %zu (limit: %zu)", | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 66 |                 valueSize, mMaxValueSize); | 
 | 67 |         return; | 
 | 68 |     } | 
 | 69 |     if (mMaxTotalSize < keySize + valueSize) { | 
| Steve Block | b37fbe9 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 70 |         ALOGV("set: not caching because the combined key/value size is too " | 
| Mark Salyzyn | 387c561 | 2014-04-17 08:32:11 -0700 | [diff] [blame] | 71 |                 "large: %zu (limit: %zu)", keySize + valueSize, mMaxTotalSize); | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 72 |         return; | 
 | 73 |     } | 
 | 74 |     if (keySize == 0) { | 
| Steve Block | 61d341b | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 75 |         ALOGW("set: not caching because keySize is 0"); | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 76 |         return; | 
 | 77 |     } | 
 | 78 |     if (valueSize <= 0) { | 
| Steve Block | 61d341b | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 79 |         ALOGW("set: not caching because valueSize is 0"); | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 80 |         return; | 
 | 81 |     } | 
 | 82 |  | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 83 |     sp<Blob> dummyKey(new Blob(key, keySize, false)); | 
 | 84 |     CacheEntry dummyEntry(dummyKey, NULL); | 
 | 85 |  | 
 | 86 |     while (true) { | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 87 |         ssize_t index = mCacheEntries.indexOf(dummyEntry); | 
 | 88 |         if (index < 0) { | 
 | 89 |             // Create a new cache entry. | 
 | 90 |             sp<Blob> keyBlob(new Blob(key, keySize, true)); | 
 | 91 |             sp<Blob> valueBlob(new Blob(value, valueSize, true)); | 
 | 92 |             size_t newTotalSize = mTotalSize + keySize + valueSize; | 
 | 93 |             if (mMaxTotalSize < newTotalSize) { | 
 | 94 |                 if (isCleanable()) { | 
 | 95 |                     // Clean the cache and try again. | 
 | 96 |                     clean(); | 
 | 97 |                     continue; | 
 | 98 |                 } else { | 
| Steve Block | b37fbe9 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 99 |                     ALOGV("set: not caching new key/value pair because the " | 
| Mark Salyzyn | 387c561 | 2014-04-17 08:32:11 -0700 | [diff] [blame] | 100 |                             "total cache size limit would be exceeded: %zu " | 
 | 101 |                             "(limit: %zu)", | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 102 |                             keySize + valueSize, mMaxTotalSize); | 
 | 103 |                     break; | 
 | 104 |                 } | 
 | 105 |             } | 
 | 106 |             mCacheEntries.add(CacheEntry(keyBlob, valueBlob)); | 
 | 107 |             mTotalSize = newTotalSize; | 
| Mark Salyzyn | 387c561 | 2014-04-17 08:32:11 -0700 | [diff] [blame] | 108 |             ALOGV("set: created new cache entry with %zu byte key and %zu byte value", | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 109 |                     keySize, valueSize); | 
 | 110 |         } else { | 
 | 111 |             // Update the existing cache entry. | 
 | 112 |             sp<Blob> valueBlob(new Blob(value, valueSize, true)); | 
 | 113 |             sp<Blob> oldValueBlob(mCacheEntries[index].getValue()); | 
 | 114 |             size_t newTotalSize = mTotalSize + valueSize - oldValueBlob->getSize(); | 
 | 115 |             if (mMaxTotalSize < newTotalSize) { | 
 | 116 |                 if (isCleanable()) { | 
 | 117 |                     // Clean the cache and try again. | 
 | 118 |                     clean(); | 
 | 119 |                     continue; | 
 | 120 |                 } else { | 
| Steve Block | b37fbe9 | 2011-10-20 11:56:00 +0100 | [diff] [blame] | 121 |                     ALOGV("set: not caching new value because the total cache " | 
| Mark Salyzyn | 387c561 | 2014-04-17 08:32:11 -0700 | [diff] [blame] | 122 |                             "size limit would be exceeded: %zu (limit: %zu)", | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 123 |                             keySize + valueSize, mMaxTotalSize); | 
 | 124 |                     break; | 
 | 125 |                 } | 
 | 126 |             } | 
 | 127 |             mCacheEntries.editItemAt(index).setValue(valueBlob); | 
 | 128 |             mTotalSize = newTotalSize; | 
| Mark Salyzyn | 387c561 | 2014-04-17 08:32:11 -0700 | [diff] [blame] | 129 |             ALOGV("set: updated existing cache entry with %zu byte key and %zu byte " | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 130 |                     "value", keySize, valueSize); | 
 | 131 |         } | 
 | 132 |         break; | 
 | 133 |     } | 
 | 134 | } | 
 | 135 |  | 
 | 136 | size_t BlobCache::get(const void* key, size_t keySize, void* value, | 
 | 137 |         size_t valueSize) { | 
 | 138 |     if (mMaxKeySize < keySize) { | 
| Mark Salyzyn | 387c561 | 2014-04-17 08:32:11 -0700 | [diff] [blame] | 139 |         ALOGV("get: not searching because the key is too large: %zu (limit %zu)", | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 140 |                 keySize, mMaxKeySize); | 
 | 141 |         return 0; | 
 | 142 |     } | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 143 |     sp<Blob> dummyKey(new Blob(key, keySize, false)); | 
 | 144 |     CacheEntry dummyEntry(dummyKey, NULL); | 
 | 145 |     ssize_t index = mCacheEntries.indexOf(dummyEntry); | 
 | 146 |     if (index < 0) { | 
| Mark Salyzyn | 387c561 | 2014-04-17 08:32:11 -0700 | [diff] [blame] | 147 |         ALOGV("get: no cache entry found for key of size %zu", keySize); | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 148 |         return 0; | 
 | 149 |     } | 
 | 150 |  | 
 | 151 |     // The key was found. Return the value if the caller's buffer is large | 
 | 152 |     // enough. | 
 | 153 |     sp<Blob> valueBlob(mCacheEntries[index].getValue()); | 
 | 154 |     size_t valueBlobSize = valueBlob->getSize(); | 
 | 155 |     if (valueBlobSize <= valueSize) { | 
| Mark Salyzyn | 387c561 | 2014-04-17 08:32:11 -0700 | [diff] [blame] | 156 |         ALOGV("get: copying %zu bytes to caller's buffer", valueBlobSize); | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 157 |         memcpy(value, valueBlob->getData(), valueBlobSize); | 
 | 158 |     } else { | 
| Mark Salyzyn | 387c561 | 2014-04-17 08:32:11 -0700 | [diff] [blame] | 159 |         ALOGV("get: caller's buffer is too small for value: %zu (needs %zu)", | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 160 |                 valueSize, valueBlobSize); | 
 | 161 |     } | 
 | 162 |     return valueBlobSize; | 
 | 163 | } | 
 | 164 |  | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 165 | static inline size_t align4(size_t size) { | 
 | 166 |     return (size + 3) & ~3; | 
 | 167 | } | 
 | 168 |  | 
 | 169 | size_t BlobCache::getFlattenedSize() const { | 
| Michael Lentine | 6078805 | 2015-05-18 13:04:01 -0700 | [diff] [blame] | 170 |     size_t size = align4(sizeof(Header) + PROPERTY_VALUE_MAX); | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 171 |     for (size_t i = 0; i < mCacheEntries.size(); i++) { | 
 | 172 |         const CacheEntry& e(mCacheEntries[i]); | 
 | 173 |         sp<Blob> keyBlob = e.getKey(); | 
 | 174 |         sp<Blob> valueBlob = e.getValue(); | 
| Christopher Ferris | de722e5 | 2014-09-24 13:06:35 -0700 | [diff] [blame] | 175 |         size += align4(sizeof(EntryHeader) + keyBlob->getSize() + | 
 | 176 |                        valueBlob->getSize()); | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 177 |     } | 
 | 178 |     return size; | 
 | 179 | } | 
 | 180 |  | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 181 | status_t BlobCache::flatten(void* buffer, size_t size) const { | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 182 |     // Write the cache header | 
 | 183 |     if (size < sizeof(Header)) { | 
| Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 184 |         ALOGE("flatten: not enough room for cache header"); | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 185 |         return BAD_VALUE; | 
 | 186 |     } | 
 | 187 |     Header* header = reinterpret_cast<Header*>(buffer); | 
 | 188 |     header->mMagicNumber = blobCacheMagic; | 
 | 189 |     header->mBlobCacheVersion = blobCacheVersion; | 
 | 190 |     header->mDeviceVersion = blobCacheDeviceVersion; | 
 | 191 |     header->mNumEntries = mCacheEntries.size(); | 
| Michael Lentine | 6078805 | 2015-05-18 13:04:01 -0700 | [diff] [blame] | 192 |     char buildId[PROPERTY_VALUE_MAX]; | 
 | 193 |     header->mBuildIdLength = property_get("ro.build.id", buildId, ""); | 
 | 194 |     memcpy(header->mBuildId, buildId, header->mBuildIdLength); | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 195 |  | 
 | 196 |     // Write cache entries | 
 | 197 |     uint8_t* byteBuffer = reinterpret_cast<uint8_t*>(buffer); | 
| Michael Lentine | 6078805 | 2015-05-18 13:04:01 -0700 | [diff] [blame] | 198 |     off_t byteOffset = align4(sizeof(Header) + header->mBuildIdLength); | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 199 |     for (size_t i = 0; i < mCacheEntries.size(); i++) { | 
 | 200 |         const CacheEntry& e(mCacheEntries[i]); | 
 | 201 |         sp<Blob> keyBlob = e.getKey(); | 
 | 202 |         sp<Blob> valueBlob = e.getValue(); | 
 | 203 |         size_t keySize = keyBlob->getSize(); | 
 | 204 |         size_t valueSize = valueBlob->getSize(); | 
 | 205 |  | 
 | 206 |         size_t entrySize = sizeof(EntryHeader) + keySize + valueSize; | 
| Christopher Ferris | de722e5 | 2014-09-24 13:06:35 -0700 | [diff] [blame] | 207 |         size_t totalSize = align4(entrySize); | 
 | 208 |         if (byteOffset + totalSize > size) { | 
| Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 209 |             ALOGE("flatten: not enough room for cache entries"); | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 210 |             return BAD_VALUE; | 
 | 211 |         } | 
 | 212 |  | 
 | 213 |         EntryHeader* eheader = reinterpret_cast<EntryHeader*>( | 
 | 214 |             &byteBuffer[byteOffset]); | 
 | 215 |         eheader->mKeySize = keySize; | 
 | 216 |         eheader->mValueSize = valueSize; | 
 | 217 |  | 
 | 218 |         memcpy(eheader->mData, keyBlob->getData(), keySize); | 
 | 219 |         memcpy(eheader->mData + keySize, valueBlob->getData(), valueSize); | 
 | 220 |  | 
| Antoine Labour | 5f6ebc2 | 2014-07-28 15:35:15 -0700 | [diff] [blame] | 221 |         if (totalSize > entrySize) { | 
 | 222 |             // We have padding bytes. Those will get written to storage, and contribute to the CRC, | 
 | 223 |             // so make sure we zero-them to have reproducible results. | 
 | 224 |             memset(eheader->mData + keySize + valueSize, 0, totalSize - entrySize); | 
 | 225 |         } | 
 | 226 |  | 
 | 227 |         byteOffset += totalSize; | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 228 |     } | 
 | 229 |  | 
 | 230 |     return OK; | 
 | 231 | } | 
 | 232 |  | 
| Mathias Agopian | 6d611a8 | 2013-07-29 21:24:40 -0700 | [diff] [blame] | 233 | status_t BlobCache::unflatten(void const* buffer, size_t size) { | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 234 |     // All errors should result in the BlobCache being in an empty state. | 
 | 235 |     mCacheEntries.clear(); | 
 | 236 |  | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 237 |     // Read the cache header | 
 | 238 |     if (size < sizeof(Header)) { | 
| Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 239 |         ALOGE("unflatten: not enough room for cache header"); | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 240 |         return BAD_VALUE; | 
 | 241 |     } | 
 | 242 |     const Header* header = reinterpret_cast<const Header*>(buffer); | 
 | 243 |     if (header->mMagicNumber != blobCacheMagic) { | 
| Mark Salyzyn | 387c561 | 2014-04-17 08:32:11 -0700 | [diff] [blame] | 244 |         ALOGE("unflatten: bad magic number: %" PRIu32, header->mMagicNumber); | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 245 |         return BAD_VALUE; | 
 | 246 |     } | 
| Michael Lentine | 6078805 | 2015-05-18 13:04:01 -0700 | [diff] [blame] | 247 |     char buildId[PROPERTY_VALUE_MAX]; | 
 | 248 |     int len = property_get("ro.build.id", buildId, ""); | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 249 |     if (header->mBlobCacheVersion != blobCacheVersion || | 
| Michael Lentine | 6078805 | 2015-05-18 13:04:01 -0700 | [diff] [blame] | 250 |             header->mDeviceVersion != blobCacheDeviceVersion || | 
 | 251 |             len != header->mBuildIdLength || | 
 | 252 |             strncmp(buildId, header->mBuildId, len)) { | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 253 |         // We treat version mismatches as an empty cache. | 
 | 254 |         return OK; | 
 | 255 |     } | 
 | 256 |  | 
 | 257 |     // Read cache entries | 
 | 258 |     const uint8_t* byteBuffer = reinterpret_cast<const uint8_t*>(buffer); | 
| Michael Lentine | 6078805 | 2015-05-18 13:04:01 -0700 | [diff] [blame] | 259 |     off_t byteOffset = align4(sizeof(Header) + header->mBuildIdLength); | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 260 |     size_t numEntries = header->mNumEntries; | 
 | 261 |     for (size_t i = 0; i < numEntries; i++) { | 
 | 262 |         if (byteOffset + sizeof(EntryHeader) > size) { | 
 | 263 |             mCacheEntries.clear(); | 
| Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 264 |             ALOGE("unflatten: not enough room for cache entry headers"); | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 265 |             return BAD_VALUE; | 
 | 266 |         } | 
 | 267 |  | 
 | 268 |         const EntryHeader* eheader = reinterpret_cast<const EntryHeader*>( | 
 | 269 |                 &byteBuffer[byteOffset]); | 
 | 270 |         size_t keySize = eheader->mKeySize; | 
 | 271 |         size_t valueSize = eheader->mValueSize; | 
 | 272 |         size_t entrySize = sizeof(EntryHeader) + keySize + valueSize; | 
 | 273 |  | 
| Christopher Ferris | de722e5 | 2014-09-24 13:06:35 -0700 | [diff] [blame] | 274 |         size_t totalSize = align4(entrySize); | 
 | 275 |         if (byteOffset + totalSize > size) { | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 276 |             mCacheEntries.clear(); | 
| Steve Block | 1b781ab | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 277 |             ALOGE("unflatten: not enough room for cache entry headers"); | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 278 |             return BAD_VALUE; | 
 | 279 |         } | 
 | 280 |  | 
 | 281 |         const uint8_t* data = eheader->mData; | 
 | 282 |         set(data, keySize, data + keySize, valueSize); | 
 | 283 |  | 
| Christopher Ferris | de722e5 | 2014-09-24 13:06:35 -0700 | [diff] [blame] | 284 |         byteOffset += totalSize; | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 285 |     } | 
 | 286 |  | 
 | 287 |     return OK; | 
 | 288 | } | 
 | 289 |  | 
| Kenny Root | 967ad86 | 2011-06-15 20:41:15 -0700 | [diff] [blame] | 290 | long int BlobCache::blob_random() { | 
 | 291 | #ifdef _WIN32 | 
 | 292 |     return rand(); | 
 | 293 | #else | 
 | 294 |     return nrand48(mRandState); | 
 | 295 | #endif | 
 | 296 | } | 
 | 297 |  | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 298 | void BlobCache::clean() { | 
 | 299 |     // Remove a random cache entry until the total cache size gets below half | 
 | 300 |     // the maximum total cache size. | 
 | 301 |     while (mTotalSize > mMaxTotalSize / 2) { | 
| Kenny Root | 967ad86 | 2011-06-15 20:41:15 -0700 | [diff] [blame] | 302 |         size_t i = size_t(blob_random() % (mCacheEntries.size())); | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 303 |         const CacheEntry& entry(mCacheEntries[i]); | 
 | 304 |         mTotalSize -= entry.getKey()->getSize() + entry.getValue()->getSize(); | 
 | 305 |         mCacheEntries.removeAt(i); | 
 | 306 |     } | 
 | 307 | } | 
 | 308 |  | 
 | 309 | bool BlobCache::isCleanable() const { | 
 | 310 |     return mTotalSize > mMaxTotalSize / 2; | 
 | 311 | } | 
 | 312 |  | 
 | 313 | BlobCache::Blob::Blob(const void* data, size_t size, bool copyData): | 
 | 314 |         mData(copyData ? malloc(size) : data), | 
 | 315 |         mSize(size), | 
 | 316 |         mOwnsData(copyData) { | 
| Jamie Gennis | 7451bb4 | 2011-05-12 17:39:03 -0700 | [diff] [blame] | 317 |     if (data != NULL && copyData) { | 
| Jamie Gennis | 1a20993 | 2011-04-28 16:19:45 -0700 | [diff] [blame] | 318 |         memcpy(const_cast<void*>(mData), data, size); | 
 | 319 |     } | 
 | 320 | } | 
 | 321 |  | 
 | 322 | BlobCache::Blob::~Blob() { | 
 | 323 |     if (mOwnsData) { | 
 | 324 |         free(const_cast<void*>(mData)); | 
 | 325 |     } | 
 | 326 | } | 
 | 327 |  | 
 | 328 | bool BlobCache::Blob::operator<(const Blob& rhs) const { | 
 | 329 |     if (mSize == rhs.mSize) { | 
 | 330 |         return memcmp(mData, rhs.mData, mSize) < 0; | 
 | 331 |     } else { | 
 | 332 |         return mSize < rhs.mSize; | 
 | 333 |     } | 
 | 334 | } | 
 | 335 |  | 
 | 336 | const void* BlobCache::Blob::getData() const { | 
 | 337 |     return mData; | 
 | 338 | } | 
 | 339 |  | 
 | 340 | size_t BlobCache::Blob::getSize() const { | 
 | 341 |     return mSize; | 
 | 342 | } | 
 | 343 |  | 
 | 344 | BlobCache::CacheEntry::CacheEntry() { | 
 | 345 | } | 
 | 346 |  | 
 | 347 | BlobCache::CacheEntry::CacheEntry(const sp<Blob>& key, const sp<Blob>& value): | 
 | 348 |         mKey(key), | 
 | 349 |         mValue(value) { | 
 | 350 | } | 
 | 351 |  | 
 | 352 | BlobCache::CacheEntry::CacheEntry(const CacheEntry& ce): | 
 | 353 |         mKey(ce.mKey), | 
 | 354 |         mValue(ce.mValue) { | 
 | 355 | } | 
 | 356 |  | 
 | 357 | bool BlobCache::CacheEntry::operator<(const CacheEntry& rhs) const { | 
 | 358 |     return *mKey < *rhs.mKey; | 
 | 359 | } | 
 | 360 |  | 
 | 361 | const BlobCache::CacheEntry& BlobCache::CacheEntry::operator=(const CacheEntry& rhs) { | 
 | 362 |     mKey = rhs.mKey; | 
 | 363 |     mValue = rhs.mValue; | 
 | 364 |     return *this; | 
 | 365 | } | 
 | 366 |  | 
 | 367 | sp<BlobCache::Blob> BlobCache::CacheEntry::getKey() const { | 
 | 368 |     return mKey; | 
 | 369 | } | 
 | 370 |  | 
 | 371 | sp<BlobCache::Blob> BlobCache::CacheEntry::getValue() const { | 
 | 372 |     return mValue; | 
 | 373 | } | 
 | 374 |  | 
 | 375 | void BlobCache::CacheEntry::setValue(const sp<Blob>& value) { | 
 | 376 |     mValue = value; | 
 | 377 | } | 
 | 378 |  | 
 | 379 | } // namespace android |