• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef ANDROID_BLOB_CACHE_H
18 #define ANDROID_BLOB_CACHE_H
19 
20 #include <stddef.h>
21 
22 #include <memory>
23 #include <vector>
24 
25 namespace 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.
34 class BlobCache {
35 public:
36     // 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     // Return value from set(), below.
43     enum class InsertResult {
44         // The key is larger than maxKeySize specified in the constructor.
45         kKeyTooBig,
46         // The value is larger than maxValueSize specified in the constructor.
47         kValueTooBig,
48         // The combined key + value is larger than maxTotalSize specified in the constructor.
49         kCombinedTooBig,
50         // keySize is 0
51         kInvalidKeySize,
52         // valueSize is 0
53         kInvalidValueSize,
54         // Unable to free enough space to fit the new entry.
55         kNotEnoughSpace,
56         // The new entry was inserted, but an old entry had to be evicted.
57         kDidClean,
58         // There was enough room in the cache and the new entry was inserted.
59         kInserted,
60 
61     };
62     // set inserts a new binary value into the cache and associates it with the
63     // given binary key.  If the key or value are too large for the cache then
64     // the cache remains unchanged.  This includes the case where a different
65     // value was previously associated with the given key - the old value will
66     // remain in the cache.  If the given key and value are small enough to be
67     // put in the cache (based on the maxKeySize, maxValueSize, and maxTotalSize
68     // values specified to the BlobCache constructor), then the key/value pair
69     // will be in the cache after set returns.  Note, however, that a subsequent
70     // call to set may evict old key/value pairs from the cache.
71     //
72     // Preconditions:
73     //   key != NULL
74     //   0 < keySize
75     //   value != NULL
76     //   0 < valueSize
77     InsertResult set(const void* key, size_t keySize, const void* value, size_t valueSize);
78 
79     // get retrieves from the cache the binary value associated with a given
80     // binary key.  If the key is present in the cache then the length of the
81     // binary value associated with that key is returned.  If the value argument
82     // is non-NULL and the size of the cached value is less than valueSize bytes
83     // then the cached value is copied into the buffer pointed to by the value
84     // argument.  If the key is not present in the cache then 0 is returned and
85     // the buffer pointed to by the value argument is not modified.
86     //
87     // Note that when calling get multiple times with the same key, the later
88     // calls may fail, returning 0, even if earlier calls succeeded.  The return
89     // value must be checked for each call.
90     //
91     // Preconditions:
92     //   key != NULL
93     //   0 < keySize
94     //   0 <= valueSize
95     size_t get(const void* key, size_t keySize, void* value, size_t valueSize);
96 
97     // getFlattenedSize returns the number of bytes needed to store the entire
98     // serialized cache.
99     size_t getFlattenedSize() const;
100 
101     // flatten serializes the current contents of the cache into the memory
102     // pointed to by 'buffer'.  The serialized cache contents can later be
103     // loaded into a BlobCache object using the unflatten method.  The contents
104     // of the BlobCache object will not be modified.
105     //
106     // Preconditions:
107     //   size >= this.getFlattenedSize()
108     int flatten(void* buffer, size_t size) const;
109 
110     // unflatten replaces the contents of the cache with the serialized cache
111     // contents in the memory pointed to by 'buffer'.  The previous contents of
112     // the BlobCache will be evicted from the cache.  If an error occurs while
113     // unflattening the serialized cache contents then the BlobCache will be
114     // left in an empty state.
115     //
116     int unflatten(void const* buffer, size_t size);
117 
118     // clear flushes out all contents of the cache then the BlobCache, leaving
119     // it in an empty state.
clear()120     void clear() {
121         mCacheEntries.clear();
122         mTotalSize = 0;
123     }
124 
125 protected:
126     // mMaxTotalSize is the maximum size that all cache entries can occupy. This
127     // includes space for both keys and values. When a call to BlobCache::set
128     // would otherwise cause this limit to be exceeded, either the key/value
129     // pair passed to BlobCache::set will not be cached or other cache entries
130     // will be evicted from the cache to make room for the new entry.
131     const size_t mMaxTotalSize;
132 
133 private:
134     // Copying is disallowed.
135     BlobCache(const BlobCache&);
136     void operator=(const BlobCache&);
137 
138     // A random function helper to get around MinGW not having nrand48()
139     long int blob_random();
140 
141     // clean evicts a randomly chosen set of entries from the cache such that
142     // the total size of all remaining entries is less than mMaxTotalSize/2.
143     void clean();
144 
145     // isCleanable returns true if the cache is full enough for the clean method
146     // to have some effect, and false otherwise.
147     bool isCleanable() const;
148 
149     // A Blob is an immutable sized unstructured data blob.
150     class Blob {
151     public:
152         Blob(const void* data, size_t size, bool copyData);
153         ~Blob();
154 
155         bool operator<(const Blob& rhs) const;
156 
157         const void* getData() const;
158         size_t getSize() const;
159 
160     private:
161         // Copying is not allowed.
162         Blob(const Blob&);
163         void operator=(const Blob&);
164 
165         // mData points to the buffer containing the blob data.
166         const void* mData;
167 
168         // mSize is the size of the blob data in bytes.
169         size_t mSize;
170 
171         // mOwnsData indicates whether or not this Blob object should free the
172         // memory pointed to by mData when the Blob gets destructed.
173         bool mOwnsData;
174     };
175 
176     // A CacheEntry is a single key/value pair in the cache.
177     class CacheEntry {
178     public:
179         CacheEntry();
180         CacheEntry(const std::shared_ptr<Blob>& key, const std::shared_ptr<Blob>& value);
181         CacheEntry(const CacheEntry& ce);
182 
183         bool operator<(const CacheEntry& rhs) const;
184         const CacheEntry& operator=(const CacheEntry&);
185 
186         std::shared_ptr<Blob> getKey() const;
187         std::shared_ptr<Blob> getValue() const;
188 
189         void setValue(const std::shared_ptr<Blob>& value);
190 
191     private:
192         // mKey is the key that identifies the cache entry.
193         std::shared_ptr<Blob> mKey;
194 
195         // mValue is the cached data associated with the key.
196         std::shared_ptr<Blob> mValue;
197     };
198 
199     // A Header is the header for the entire BlobCache serialization format. No
200     // need to make this portable, so we simply write the struct out.
201     struct Header {
202         // mMagicNumber is the magic number that identifies the data as
203         // serialized BlobCache contents.  It must always contain 'Blb$'.
204         uint32_t mMagicNumber;
205 
206         // mBlobCacheVersion is the serialization format version.
207         uint32_t mBlobCacheVersion;
208 
209         // mDeviceVersion is the device-specific version of the cache.  This can
210         // be used to invalidate the cache.
211         uint32_t mDeviceVersion;
212 
213         // mNumEntries is number of cache entries following the header in the
214         // data.
215         size_t mNumEntries;
216 
217         // mBuildId is the build id of the device when the cache was created.
218         // When an update to the build happens (via an OTA or other update) this
219         // is used to invalidate the cache.
220         int mBuildIdLength;
221         char mBuildId[];
222     };
223 
224     // An EntryHeader is the header for a serialized cache entry.  No need to
225     // make this portable, so we simply write the struct out.  Each EntryHeader
226     // is followed imediately by the key data and then the value data.
227     //
228     // The beginning of each serialized EntryHeader is 4-byte aligned, so the
229     // number of bytes that a serialized cache entry will occupy is:
230     //
231     //   ((sizeof(EntryHeader) + keySize + valueSize) + 3) & ~3
232     //
233     struct EntryHeader {
234         // mKeySize is the size of the entry key in bytes.
235         size_t mKeySize;
236 
237         // mValueSize is the size of the entry value in bytes.
238         size_t mValueSize;
239 
240         // mData contains both the key and value data for the cache entry.  The
241         // key comes first followed immediately by the value.
242         uint8_t mData[];
243     };
244 
245     // mMaxKeySize is the maximum key size that will be cached. Calls to
246     // BlobCache::set with a keySize parameter larger than mMaxKeySize will
247     // simply not add the key/value pair to the cache.
248     const size_t mMaxKeySize;
249 
250     // mMaxValueSize is the maximum value size that will be cached. Calls to
251     // BlobCache::set with a valueSize parameter larger than mMaxValueSize will
252     // simply not add the key/value pair to the cache.
253     const size_t mMaxValueSize;
254 
255     // mTotalSize is the total combined size of all keys and values currently in
256     // the cache.
257     size_t mTotalSize;
258 
259     // mRandState is the pseudo-random number generator state. It is passed to
260     // nrand48 to generate random numbers when needed.
261     unsigned short mRandState[3];
262 
263     // mCacheEntries stores all the cache entries that are resident in memory.
264     // Cache entries are added to it by the 'set' method.
265     std::vector<CacheEntry> mCacheEntries;
266 };
267 
268 } // namespace android
269 
270 #endif // ANDROID_BLOB_CACHE_H
271