blob: e5c5e5bc25371ce32a61488178c79150bfa81913 [file] [log] [blame]
Mathias Agopian5f549b22017-03-08 22:27:13 -08001/*
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#ifndef ANDROID_BLOB_CACHE_H
18#define ANDROID_BLOB_CACHE_H
19
20#include <stddef.h>
21
Mathias Agopianb7f9a242017-03-08 22:29:31 -080022#include <memory>
23#include <vector>
Mathias Agopian5f549b22017-03-08 22:27:13 -080024
25namespace android {
26
27// A BlobCache is an in-memory cache for binary key/value pairs. A BlobCache
28// does NOT provide any thread-safety guarantees.
29//
30// The cache contents can be serialized to an in-memory buffer or mmap'd file
31// and then reloaded in a subsequent execution of the program. This
32// serialization is non-portable and the data should only be used by the device
33// that generated it.
Mathias Agopianb7f9a242017-03-08 22:29:31 -080034class BlobCache {
Mathias Agopian5f549b22017-03-08 22:27:13 -080035public:
Mathias Agopian5f549b22017-03-08 22:27:13 -080036 // Create an empty blob cache. The blob cache will cache key/value pairs
37 // with key and value sizes less than or equal to maxKeySize and
38 // maxValueSize, respectively. The total combined size of ALL cache entries
39 // (key sizes plus value sizes) will not exceed maxTotalSize.
40 BlobCache(size_t maxKeySize, size_t maxValueSize, size_t maxTotalSize);
41
42 // set inserts a new binary value into the cache and associates it with the
43 // given binary key. If the key or value are too large for the cache then
44 // the cache remains unchanged. This includes the case where a different
45 // value was previously associated with the given key - the old value will
46 // remain in the cache. If the given key and value are small enough to be
47 // put in the cache (based on the maxKeySize, maxValueSize, and maxTotalSize
48 // values specified to the BlobCache constructor), then the key/value pair
49 // will be in the cache after set returns. Note, however, that a subsequent
50 // call to set may evict old key/value pairs from the cache.
51 //
52 // Preconditions:
53 // key != NULL
54 // 0 < keySize
55 // value != NULL
56 // 0 < valueSize
57 void set(const void* key, size_t keySize, const void* value,
58 size_t valueSize);
59
60 // get retrieves from the cache the binary value associated with a given
61 // binary key. If the key is present in the cache then the length of the
62 // binary value associated with that key is returned. If the value argument
63 // is non-NULL and the size of the cached value is less than valueSize bytes
64 // then the cached value is copied into the buffer pointed to by the value
65 // argument. If the key is not present in the cache then 0 is returned and
66 // the buffer pointed to by the value argument is not modified.
67 //
68 // Note that when calling get multiple times with the same key, the later
69 // calls may fail, returning 0, even if earlier calls succeeded. The return
70 // value must be checked for each call.
71 //
72 // Preconditions:
73 // key != NULL
74 // 0 < keySize
75 // 0 <= valueSize
76 size_t get(const void* key, size_t keySize, void* value, size_t valueSize);
77
78
79 // getFlattenedSize returns the number of bytes needed to store the entire
80 // serialized cache.
81 size_t getFlattenedSize() const;
82
83 // flatten serializes the current contents of the cache into the memory
84 // pointed to by 'buffer'. The serialized cache contents can later be
85 // loaded into a BlobCache object using the unflatten method. The contents
86 // of the BlobCache object will not be modified.
87 //
88 // Preconditions:
89 // size >= this.getFlattenedSize()
Mathias Agopianb7f9a242017-03-08 22:29:31 -080090 int flatten(void* buffer, size_t size) const;
Mathias Agopian5f549b22017-03-08 22:27:13 -080091
92 // unflatten replaces the contents of the cache with the serialized cache
93 // contents in the memory pointed to by 'buffer'. The previous contents of
94 // the BlobCache will be evicted from the cache. If an error occurs while
95 // unflattening the serialized cache contents then the BlobCache will be
96 // left in an empty state.
97 //
Mathias Agopianb7f9a242017-03-08 22:29:31 -080098 int unflatten(void const* buffer, size_t size);
Mathias Agopian5f549b22017-03-08 22:27:13 -080099
Yichi Chen42cf4942018-03-29 21:41:07 +0800100 // clear flushes out all contents of the cache then the BlobCache, leaving
101 // it in an empty state.
102 void clear() { mCacheEntries.clear(); }
103
Stan Iliev9e7cd072017-10-09 15:56:10 -0400104protected:
105 // mMaxTotalSize is the maximum size that all cache entries can occupy. This
106 // includes space for both keys and values. When a call to BlobCache::set
107 // would otherwise cause this limit to be exceeded, either the key/value
108 // pair passed to BlobCache::set will not be cached or other cache entries
109 // will be evicted from the cache to make room for the new entry.
110 const size_t mMaxTotalSize;
111
Mathias Agopian5f549b22017-03-08 22:27:13 -0800112private:
113 // Copying is disallowed.
114 BlobCache(const BlobCache&);
115 void operator=(const BlobCache&);
116
117 // A random function helper to get around MinGW not having nrand48()
118 long int blob_random();
119
120 // clean evicts a randomly chosen set of entries from the cache such that
121 // the total size of all remaining entries is less than mMaxTotalSize/2.
122 void clean();
123
124 // isCleanable returns true if the cache is full enough for the clean method
125 // to have some effect, and false otherwise.
126 bool isCleanable() const;
127
128 // A Blob is an immutable sized unstructured data blob.
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800129 class Blob {
Mathias Agopian5f549b22017-03-08 22:27:13 -0800130 public:
131 Blob(const void* data, size_t size, bool copyData);
132 ~Blob();
133
134 bool operator<(const Blob& rhs) const;
135
136 const void* getData() const;
137 size_t getSize() const;
138
139 private:
140 // Copying is not allowed.
141 Blob(const Blob&);
142 void operator=(const Blob&);
143
144 // mData points to the buffer containing the blob data.
145 const void* mData;
146
147 // mSize is the size of the blob data in bytes.
148 size_t mSize;
149
150 // mOwnsData indicates whether or not this Blob object should free the
151 // memory pointed to by mData when the Blob gets destructed.
152 bool mOwnsData;
153 };
154
155 // A CacheEntry is a single key/value pair in the cache.
156 class CacheEntry {
157 public:
158 CacheEntry();
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800159 CacheEntry(const std::shared_ptr<Blob>& key, const std::shared_ptr<Blob>& value);
Mathias Agopian5f549b22017-03-08 22:27:13 -0800160 CacheEntry(const CacheEntry& ce);
161
162 bool operator<(const CacheEntry& rhs) const;
163 const CacheEntry& operator=(const CacheEntry&);
164
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800165 std::shared_ptr<Blob> getKey() const;
166 std::shared_ptr<Blob> getValue() const;
Mathias Agopian5f549b22017-03-08 22:27:13 -0800167
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800168 void setValue(const std::shared_ptr<Blob>& value);
Mathias Agopian5f549b22017-03-08 22:27:13 -0800169
170 private:
171
172 // mKey is the key that identifies the cache entry.
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800173 std::shared_ptr<Blob> mKey;
Mathias Agopian5f549b22017-03-08 22:27:13 -0800174
175 // mValue is the cached data associated with the key.
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800176 std::shared_ptr<Blob> mValue;
Mathias Agopian5f549b22017-03-08 22:27:13 -0800177 };
178
179 // A Header is the header for the entire BlobCache serialization format. No
180 // need to make this portable, so we simply write the struct out.
181 struct Header {
182 // mMagicNumber is the magic number that identifies the data as
183 // serialized BlobCache contents. It must always contain 'Blb$'.
184 uint32_t mMagicNumber;
185
186 // mBlobCacheVersion is the serialization format version.
187 uint32_t mBlobCacheVersion;
188
189 // mDeviceVersion is the device-specific version of the cache. This can
190 // be used to invalidate the cache.
191 uint32_t mDeviceVersion;
192
193 // mNumEntries is number of cache entries following the header in the
194 // data.
195 size_t mNumEntries;
196
197 // mBuildId is the build id of the device when the cache was created.
198 // When an update to the build happens (via an OTA or other update) this
199 // is used to invalidate the cache.
200 int mBuildIdLength;
201 char mBuildId[];
202 };
203
204 // An EntryHeader is the header for a serialized cache entry. No need to
205 // make this portable, so we simply write the struct out. Each EntryHeader
206 // is followed imediately by the key data and then the value data.
207 //
208 // The beginning of each serialized EntryHeader is 4-byte aligned, so the
209 // number of bytes that a serialized cache entry will occupy is:
210 //
211 // ((sizeof(EntryHeader) + keySize + valueSize) + 3) & ~3
212 //
213 struct EntryHeader {
214 // mKeySize is the size of the entry key in bytes.
215 size_t mKeySize;
216
217 // mValueSize is the size of the entry value in bytes.
218 size_t mValueSize;
219
220 // mData contains both the key and value data for the cache entry. The
221 // key comes first followed immediately by the value.
222 uint8_t mData[];
223 };
224
225 // mMaxKeySize is the maximum key size that will be cached. Calls to
226 // BlobCache::set with a keySize parameter larger than mMaxKeySize will
227 // simply not add the key/value pair to the cache.
228 const size_t mMaxKeySize;
229
230 // mMaxValueSize is the maximum value size that will be cached. Calls to
231 // BlobCache::set with a valueSize parameter larger than mMaxValueSize will
232 // simply not add the key/value pair to the cache.
233 const size_t mMaxValueSize;
234
Mathias Agopian5f549b22017-03-08 22:27:13 -0800235 // mTotalSize is the total combined size of all keys and values currently in
236 // the cache.
237 size_t mTotalSize;
238
239 // mRandState is the pseudo-random number generator state. It is passed to
240 // nrand48 to generate random numbers when needed.
241 unsigned short mRandState[3];
242
243 // mCacheEntries stores all the cache entries that are resident in memory.
244 // Cache entries are added to it by the 'set' method.
Mathias Agopianb7f9a242017-03-08 22:29:31 -0800245 std::vector<CacheEntry> mCacheEntries;
Mathias Agopian5f549b22017-03-08 22:27:13 -0800246};
247
248}
249
250#endif // ANDROID_BLOB_CACHE_H