| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -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 | #include "egl_cache.h" | 
|  | 18 | #include "egl_display.h" | 
|  | 19 | #include "egl_impl.h" | 
|  | 20 | #include "egldefs.h" | 
|  | 21 |  | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 22 | #include <fcntl.h> | 
|  | 23 | #include <sys/mman.h> | 
|  | 24 | #include <sys/stat.h> | 
|  | 25 | #include <sys/types.h> | 
|  | 26 | #include <unistd.h> | 
|  | 27 |  | 
| Jamie Gennis | 89c1d61 | 2011-11-19 16:25:24 -0800 | [diff] [blame] | 28 | #ifndef MAX_EGL_CACHE_ENTRY_SIZE | 
|  | 29 | #define MAX_EGL_CACHE_ENTRY_SIZE (16 * 1024); | 
|  | 30 | #endif | 
|  | 31 |  | 
|  | 32 | #ifndef MAX_EGL_CACHE_SIZE | 
|  | 33 | #define MAX_EGL_CACHE_SIZE (64 * 1024); | 
|  | 34 | #endif | 
|  | 35 |  | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 36 | // Cache size limits. | 
|  | 37 | static const size_t maxKeySize = 1024; | 
| Jamie Gennis | 89c1d61 | 2011-11-19 16:25:24 -0800 | [diff] [blame] | 38 | static const size_t maxValueSize = MAX_EGL_CACHE_ENTRY_SIZE; | 
|  | 39 | static const size_t maxTotalSize = MAX_EGL_CACHE_SIZE; | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 40 |  | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 41 | // Cache file header | 
|  | 42 | static const char* cacheFileMagic = "EGL$"; | 
|  | 43 | static const size_t cacheFileHeaderSize = 8; | 
|  | 44 |  | 
| Jamie Gennis | 99c3d70 | 2011-11-08 17:59:36 -0800 | [diff] [blame] | 45 | // The time in seconds to wait before saving newly inserted cache entries. | 
|  | 46 | static const unsigned int deferredSaveDelay = 4; | 
|  | 47 |  | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 48 | // ---------------------------------------------------------------------------- | 
|  | 49 | namespace android { | 
|  | 50 | // ---------------------------------------------------------------------------- | 
|  | 51 |  | 
|  | 52 | #define BC_EXT_STR "EGL_ANDROID_blob_cache" | 
|  | 53 |  | 
|  | 54 | // | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 55 | // Callback functions passed to EGL. | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 56 | // | 
| Jamie Gennis | c42fcf0 | 2011-11-09 15:35:34 -0800 | [diff] [blame] | 57 | static void setBlob(const void* key, EGLsizeiANDROID keySize, | 
|  | 58 | const void* value, EGLsizeiANDROID valueSize) { | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 59 | egl_cache_t::get()->setBlob(key, keySize, value, valueSize); | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 60 | } | 
|  | 61 |  | 
| Jamie Gennis | c42fcf0 | 2011-11-09 15:35:34 -0800 | [diff] [blame] | 62 | static EGLsizeiANDROID getBlob(const void* key, EGLsizeiANDROID keySize, | 
|  | 63 | void* value, EGLsizeiANDROID valueSize) { | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 64 | return egl_cache_t::get()->getBlob(key, keySize, value, valueSize); | 
|  | 65 | } | 
|  | 66 |  | 
|  | 67 | // | 
|  | 68 | // egl_cache_t definition | 
|  | 69 | // | 
|  | 70 | egl_cache_t::egl_cache_t() : | 
|  | 71 | mInitialized(false), | 
|  | 72 | mBlobCache(NULL) { | 
|  | 73 | } | 
|  | 74 |  | 
|  | 75 | egl_cache_t::~egl_cache_t() { | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 76 | } | 
|  | 77 |  | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 78 | egl_cache_t egl_cache_t::sCache; | 
|  | 79 |  | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 80 | egl_cache_t* egl_cache_t::get() { | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 81 | return &sCache; | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 82 | } | 
|  | 83 |  | 
|  | 84 | void egl_cache_t::initialize(egl_display_t *display) { | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 85 | Mutex::Autolock lock(mMutex); | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 86 | for (int i = 0; i < IMPL_NUM_IMPLEMENTATIONS; i++) { | 
|  | 87 | egl_connection_t* const cnx = &gEGLImpl[i]; | 
|  | 88 | if (cnx->dso && cnx->major >= 0 && cnx->minor >= 0) { | 
|  | 89 | const char* exts = display->disp[i].queryString.extensions; | 
|  | 90 | size_t bcExtLen = strlen(BC_EXT_STR); | 
|  | 91 | size_t extsLen = strlen(exts); | 
|  | 92 | bool equal = !strcmp(BC_EXT_STR, exts); | 
|  | 93 | bool atStart = !strncmp(BC_EXT_STR " ", exts, bcExtLen+1); | 
|  | 94 | bool atEnd = (bcExtLen+1) < extsLen && | 
|  | 95 | !strcmp(" " BC_EXT_STR, exts + extsLen - (bcExtLen+1)); | 
| Jamie Gennis | 9f43009 | 2011-11-16 21:45:30 -0800 | [diff] [blame] | 96 | bool inMiddle = strstr(exts, " " BC_EXT_STR " "); | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 97 | if (equal || atStart || atEnd || inMiddle) { | 
| Jamie Gennis | c42fcf0 | 2011-11-09 15:35:34 -0800 | [diff] [blame] | 98 | PFNEGLSETBLOBCACHEFUNCSANDROIDPROC eglSetBlobCacheFuncsANDROID; | 
|  | 99 | eglSetBlobCacheFuncsANDROID = | 
|  | 100 | reinterpret_cast<PFNEGLSETBLOBCACHEFUNCSANDROIDPROC>( | 
|  | 101 | cnx->egl.eglGetProcAddress( | 
|  | 102 | "eglSetBlobCacheFuncsANDROID")); | 
|  | 103 | if (eglSetBlobCacheFuncsANDROID == NULL) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 104 | ALOGE("EGL_ANDROID_blob_cache advertised by display %d, " | 
| Jamie Gennis | c42fcf0 | 2011-11-09 15:35:34 -0800 | [diff] [blame] | 105 | "but unable to get eglSetBlobCacheFuncsANDROID", i); | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 106 | continue; | 
|  | 107 | } | 
|  | 108 |  | 
| Jamie Gennis | c42fcf0 | 2011-11-09 15:35:34 -0800 | [diff] [blame] | 109 | eglSetBlobCacheFuncsANDROID(display->disp[i].dpy, | 
|  | 110 | android::setBlob, android::getBlob); | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 111 | EGLint err = cnx->egl.eglGetError(); | 
|  | 112 | if (err != EGL_SUCCESS) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 113 | ALOGE("eglSetBlobCacheFuncsANDROID resulted in an error: " | 
| Jamie Gennis | c42fcf0 | 2011-11-09 15:35:34 -0800 | [diff] [blame] | 114 | "%#x", err); | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 115 | } | 
|  | 116 | } | 
|  | 117 | } | 
|  | 118 | } | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 119 | mInitialized = true; | 
|  | 120 | } | 
|  | 121 |  | 
|  | 122 | void egl_cache_t::terminate() { | 
|  | 123 | Mutex::Autolock lock(mMutex); | 
|  | 124 | if (mBlobCache != NULL) { | 
|  | 125 | saveBlobCacheLocked(); | 
|  | 126 | mBlobCache = NULL; | 
|  | 127 | } | 
|  | 128 | mInitialized = false; | 
|  | 129 | } | 
|  | 130 |  | 
| Jamie Gennis | c42fcf0 | 2011-11-09 15:35:34 -0800 | [diff] [blame] | 131 | void egl_cache_t::setBlob(const void* key, EGLsizeiANDROID keySize, | 
|  | 132 | const void* value, EGLsizeiANDROID valueSize) { | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 133 | Mutex::Autolock lock(mMutex); | 
|  | 134 |  | 
|  | 135 | if (keySize < 0 || valueSize < 0) { | 
| Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 136 | ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed"); | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 137 | return; | 
|  | 138 | } | 
|  | 139 |  | 
|  | 140 | if (mInitialized) { | 
|  | 141 | sp<BlobCache> bc = getBlobCacheLocked(); | 
|  | 142 | bc->set(key, keySize, value, valueSize); | 
| Jamie Gennis | 99c3d70 | 2011-11-08 17:59:36 -0800 | [diff] [blame] | 143 |  | 
|  | 144 | if (!mSavePending) { | 
|  | 145 | class DeferredSaveThread : public Thread { | 
|  | 146 | public: | 
|  | 147 | DeferredSaveThread() : Thread(false) {} | 
|  | 148 |  | 
|  | 149 | virtual bool threadLoop() { | 
|  | 150 | sleep(deferredSaveDelay); | 
|  | 151 | egl_cache_t* c = egl_cache_t::get(); | 
|  | 152 | Mutex::Autolock lock(c->mMutex); | 
|  | 153 | if (c->mInitialized) { | 
|  | 154 | c->saveBlobCacheLocked(); | 
|  | 155 | } | 
|  | 156 | c->mSavePending = false; | 
|  | 157 | return false; | 
|  | 158 | } | 
|  | 159 | }; | 
|  | 160 |  | 
|  | 161 | // The thread will hold a strong ref to itself until it has finished | 
|  | 162 | // running, so there's no need to keep a ref around. | 
|  | 163 | sp<Thread> deferredSaveThread(new DeferredSaveThread()); | 
|  | 164 | mSavePending = true; | 
|  | 165 | deferredSaveThread->run(); | 
|  | 166 | } | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 167 | } | 
|  | 168 | } | 
|  | 169 |  | 
| Jamie Gennis | c42fcf0 | 2011-11-09 15:35:34 -0800 | [diff] [blame] | 170 | EGLsizeiANDROID egl_cache_t::getBlob(const void* key, EGLsizeiANDROID keySize, | 
|  | 171 | void* value, EGLsizeiANDROID valueSize) { | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 172 | Mutex::Autolock lock(mMutex); | 
|  | 173 |  | 
|  | 174 | if (keySize < 0 || valueSize < 0) { | 
| Steve Block | 32397c1 | 2012-01-05 23:22:43 +0000 | [diff] [blame] | 175 | ALOGW("EGL_ANDROID_blob_cache set: negative sizes are not allowed"); | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 176 | return 0; | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | if (mInitialized) { | 
|  | 180 | sp<BlobCache> bc = getBlobCacheLocked(); | 
|  | 181 | return bc->get(key, keySize, value, valueSize); | 
|  | 182 | } | 
|  | 183 | return 0; | 
|  | 184 | } | 
|  | 185 |  | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 186 | void egl_cache_t::setCacheFilename(const char* filename) { | 
|  | 187 | Mutex::Autolock lock(mMutex); | 
|  | 188 | mFilename = filename; | 
|  | 189 | } | 
|  | 190 |  | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 191 | sp<BlobCache> egl_cache_t::getBlobCacheLocked() { | 
|  | 192 | if (mBlobCache == NULL) { | 
|  | 193 | mBlobCache = new BlobCache(maxKeySize, maxValueSize, maxTotalSize); | 
|  | 194 | loadBlobCacheLocked(); | 
|  | 195 | } | 
|  | 196 | return mBlobCache; | 
|  | 197 | } | 
|  | 198 |  | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 199 | static uint32_t crc32c(const uint8_t* buf, size_t len) { | 
|  | 200 | const uint32_t polyBits = 0x82F63B78; | 
|  | 201 | uint32_t r = 0; | 
|  | 202 | for (size_t i = 0; i < len; i++) { | 
|  | 203 | r ^= buf[i]; | 
|  | 204 | for (int j = 0; j < 8; j++) { | 
|  | 205 | if (r & 1) { | 
|  | 206 | r = (r >> 1) ^ polyBits; | 
|  | 207 | } else { | 
|  | 208 | r >>= 1; | 
|  | 209 | } | 
|  | 210 | } | 
|  | 211 | } | 
|  | 212 | return r; | 
|  | 213 | } | 
|  | 214 |  | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 215 | void egl_cache_t::saveBlobCacheLocked() { | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 216 | if (mFilename.length() > 0) { | 
|  | 217 | size_t cacheSize = mBlobCache->getFlattenedSize(); | 
|  | 218 | size_t headerSize = cacheFileHeaderSize; | 
|  | 219 | const char* fname = mFilename.string(); | 
|  | 220 |  | 
|  | 221 | // Try to create the file with no permissions so we can write it | 
|  | 222 | // without anyone trying to read it. | 
|  | 223 | int fd = open(fname, O_CREAT | O_EXCL | O_RDWR, 0); | 
|  | 224 | if (fd == -1) { | 
|  | 225 | if (errno == EEXIST) { | 
|  | 226 | // The file exists, delete it and try again. | 
|  | 227 | if (unlink(fname) == -1) { | 
|  | 228 | // No point in retrying if the unlink failed. | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 229 | ALOGE("error unlinking cache file %s: %s (%d)", fname, | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 230 | strerror(errno), errno); | 
|  | 231 | return; | 
|  | 232 | } | 
|  | 233 | // Retry now that we've unlinked the file. | 
|  | 234 | fd = open(fname, O_CREAT | O_EXCL | O_RDWR, 0); | 
|  | 235 | } | 
|  | 236 | if (fd == -1) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 237 | ALOGE("error creating cache file %s: %s (%d)", fname, | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 238 | strerror(errno), errno); | 
|  | 239 | return; | 
|  | 240 | } | 
|  | 241 | } | 
|  | 242 |  | 
|  | 243 | size_t fileSize = headerSize + cacheSize; | 
|  | 244 | if (ftruncate(fd, fileSize) == -1) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 245 | ALOGE("error setting cache file size: %s (%d)", strerror(errno), | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 246 | errno); | 
|  | 247 | close(fd); | 
|  | 248 | unlink(fname); | 
|  | 249 | return; | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | uint8_t* buf = reinterpret_cast<uint8_t*>(mmap(NULL, fileSize, | 
|  | 253 | PROT_WRITE, MAP_SHARED, fd, 0)); | 
|  | 254 | if (buf == MAP_FAILED) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 255 | ALOGE("error mmaping cache file: %s (%d)", strerror(errno), | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 256 | errno); | 
|  | 257 | close(fd); | 
|  | 258 | unlink(fname); | 
|  | 259 | return; | 
|  | 260 | } | 
|  | 261 |  | 
|  | 262 | status_t err = mBlobCache->flatten(buf + headerSize, cacheSize, NULL, | 
|  | 263 | 0); | 
|  | 264 | if (err != OK) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 265 | ALOGE("error writing cache contents: %s (%d)", strerror(-err), | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 266 | -err); | 
|  | 267 | munmap(buf, fileSize); | 
|  | 268 | close(fd); | 
|  | 269 | unlink(fname); | 
|  | 270 | return; | 
|  | 271 | } | 
|  | 272 |  | 
|  | 273 | // Write the file magic and CRC | 
|  | 274 | memcpy(buf, cacheFileMagic, 4); | 
|  | 275 | uint32_t* crc = reinterpret_cast<uint32_t*>(buf + 4); | 
|  | 276 | *crc = crc32c(buf + headerSize, cacheSize); | 
|  | 277 |  | 
|  | 278 | munmap(buf, fileSize); | 
|  | 279 | fchmod(fd, S_IRUSR); | 
|  | 280 | close(fd); | 
|  | 281 | } | 
| Jamie Gennis | 7660108 | 2011-11-06 14:14:33 -0800 | [diff] [blame] | 282 | } | 
|  | 283 |  | 
|  | 284 | void egl_cache_t::loadBlobCacheLocked() { | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 285 | if (mFilename.length() > 0) { | 
|  | 286 | size_t headerSize = cacheFileHeaderSize; | 
|  | 287 |  | 
|  | 288 | int fd = open(mFilename.string(), O_RDONLY, 0); | 
|  | 289 | if (fd == -1) { | 
|  | 290 | if (errno != ENOENT) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 291 | ALOGE("error opening cache file %s: %s (%d)", mFilename.string(), | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 292 | strerror(errno), errno); | 
|  | 293 | } | 
|  | 294 | return; | 
|  | 295 | } | 
|  | 296 |  | 
|  | 297 | struct stat statBuf; | 
|  | 298 | if (fstat(fd, &statBuf) == -1) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 299 | ALOGE("error stat'ing cache file: %s (%d)", strerror(errno), errno); | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 300 | close(fd); | 
|  | 301 | return; | 
|  | 302 | } | 
|  | 303 |  | 
|  | 304 | // Sanity check the size before trying to mmap it. | 
|  | 305 | size_t fileSize = statBuf.st_size; | 
|  | 306 | if (fileSize > maxTotalSize * 2) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 307 | ALOGE("cache file is too large: %#llx", statBuf.st_size); | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 308 | close(fd); | 
|  | 309 | return; | 
|  | 310 | } | 
|  | 311 |  | 
|  | 312 | uint8_t* buf = reinterpret_cast<uint8_t*>(mmap(NULL, fileSize, | 
|  | 313 | PROT_READ, MAP_PRIVATE, fd, 0)); | 
|  | 314 | if (buf == MAP_FAILED) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 315 | ALOGE("error mmaping cache file: %s (%d)", strerror(errno), | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 316 | errno); | 
|  | 317 | close(fd); | 
|  | 318 | return; | 
|  | 319 | } | 
|  | 320 |  | 
|  | 321 | // Check the file magic and CRC | 
|  | 322 | size_t cacheSize = fileSize - headerSize; | 
|  | 323 | if (memcmp(buf, cacheFileMagic, 4) != 0) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 324 | ALOGE("cache file has bad mojo"); | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 325 | close(fd); | 
|  | 326 | return; | 
|  | 327 | } | 
|  | 328 | uint32_t* crc = reinterpret_cast<uint32_t*>(buf + 4); | 
|  | 329 | if (crc32c(buf + headerSize, cacheSize) != *crc) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 330 | ALOGE("cache file failed CRC check"); | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 331 | close(fd); | 
|  | 332 | return; | 
|  | 333 | } | 
|  | 334 |  | 
| Jamie Gennis | c42fcf0 | 2011-11-09 15:35:34 -0800 | [diff] [blame] | 335 | status_t err = mBlobCache->unflatten(buf + headerSize, cacheSize, NULL, | 
|  | 336 | 0); | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 337 | if (err != OK) { | 
| Steve Block | e6f43dd | 2012-01-06 19:20:56 +0000 | [diff] [blame] | 338 | ALOGE("error reading cache contents: %s (%d)", strerror(-err), | 
| Jamie Gennis | 98c6383 | 2011-11-07 17:03:54 -0800 | [diff] [blame] | 339 | -err); | 
|  | 340 | munmap(buf, fileSize); | 
|  | 341 | close(fd); | 
|  | 342 | return; | 
|  | 343 | } | 
|  | 344 |  | 
|  | 345 | munmap(buf, fileSize); | 
|  | 346 | close(fd); | 
|  | 347 | } | 
| Jamie Gennis | aca51c0 | 2011-11-03 17:42:43 -0700 | [diff] [blame] | 348 | } | 
|  | 349 |  | 
|  | 350 | // ---------------------------------------------------------------------------- | 
|  | 351 | }; // namespace android | 
|  | 352 | // ---------------------------------------------------------------------------- |