36 lock_guard<mutex> lock(m_mutex);
37 for (BlockCache::const_iterator iter = m_cache.begin();
38 iter != m_cache.end(); ++iter)
40 delete [] (*iter).block;
47 lock_guard<mutex> lock(m_mutex);
49 HashIndex::iterator iter;
53 if ((iter = hash_index.find(make_key(file_id, file_offset))) == hash_index.end())
59 hash_index.erase(iter);
61 pair<Sequence::iterator, bool> insert_result = m_cache.push_back(entry);
62 assert(insert_result.second);
64 *blockp = (*insert_result.first).block;
65 *lengthp = (*insert_result.first).length;
73 lock_guard<mutex> lock(m_mutex);
75 HashIndex::iterator iter;
77 iter = hash_index.find(make_key(file_id, file_offset));
79 assert(iter != hash_index.end() && (*iter).ref_count > 0);
87 uint8_t *block, uint32_t length,
88 const EventPtr &event,
bool checkout) {
89 lock_guard<mutex> lock(m_mutex);
92 if (hash_index.find(make_key(file_id, file_offset)) != hash_index.end())
95 if (m_available < length)
98 if (m_available < length) {
99 if ((length-m_available) <= (m_max_memory-m_limit)) {
100 m_limit += (length-m_available);
101 m_available += (length-m_available);
112 pair<Sequence::iterator, bool> insert_result = m_cache.push_back(entry);
113 assert(insert_result.second);
116 m_available -= length;
123 lock_guard<mutex> lock(m_mutex);
124 HashIndex &hash_index = m_cache.get<1>();
127 if (hash_index.find(make_key(file_id, file_offset)) != hash_index.end()) {
137 lock_guard<mutex> lock(m_mutex);
138 int64_t adjusted_amount = amount;
139 if ((m_max_memory-m_limit) < amount)
140 adjusted_amount = m_max_memory - m_limit;
141 m_limit += adjusted_amount;
142 m_available += adjusted_amount;
147 lock_guard<mutex> lock(m_mutex);
148 int64_t memory_freed = 0;
149 if (m_available < amount) {
150 if (amount > (m_limit - m_min_memory))
151 amount = m_limit - m_min_memory;
152 memory_freed = make_room(amount);
153 if (m_available < amount)
154 amount = m_available;
156 m_available -= amount;
163 BlockCache::iterator iter = m_cache.begin();
164 int64_t amount_freed = 0;
165 while (iter != m_cache.end()) {
166 if ((*iter).ref_count == 0) {
167 m_available += (*iter).length;
168 amount_freed += (*iter).length;
170 delete [] iter->block;
171 iter = m_cache.erase(iter);
172 if (m_available >= amount)
182 uint64_t *accessesp, uint64_t *hitsp) {
183 lock_guard<mutex> lock(m_mutex);
184 *max_memoryp = m_limit;
185 *available_memoryp = m_available;
186 *accessesp = m_accesses;
int64_t make_room(int64_t amount)
static std::atomic< int > ms_next_file_id
std::shared_ptr< Event > EventPtr
Smart pointer to Event.
bool contains(int file_id, uint64_t file_offset)
void checkin(int file_id, uint64_t file_offset)
Compatibility Macros for C/C++.
void increase_limit(int64_t amount)
bool insert(int file_id, uint64_t file_offset, uint8_t *block, uint32_t length, const EventPtr &event, bool checkout)
bool checkout(int file_id, uint64_t file_offset, uint8_t **blockp, uint32_t *lengthp)
void get_stats(uint64_t *max_memoryp, uint64_t *available_memoryp, uint64_t *accessesp, uint64_t *hitsp)
int64_t decrease_limit(int64_t amount)
Lowers the memory limit.
BlockCache::nth_index< 1 >::type HashIndex