Zero  0.1.0
backup_alloc_cache.h
Go to the documentation of this file.
1 #ifndef BACKUP_ALLOC_CACHE_H
2 #define BACKUP_ALLOC_CACHE_H
3 
4 #include "w_defines.h"
5 #include "alloc_cache.h"
6 #include <vector>
7 #include <unordered_set>
8 
10 public:
11  static constexpr size_t extent_size = alloc_cache_t::extent_size;
12 
14  : end_pid(end_pid) {
15  auto extents = end_pid / extent_size;
16  if (end_pid % extent_size != 0) {
17  extents++;
18  }
19 
20  alloc_pages.resize(extents);
21  loaded_ext.resize(extents, false);
22  }
23 
24  bool is_allocated(PageID pid) {
25  if (pid >= end_pid) {
26  return false;
27  }
28  auto ext = pid / extent_size;
29  PageID alloc_pid = ext * extent_size;
30  if (pid == alloc_pid) {
31  return true;
32  }
33 
34  ensure_loaded(ext);
35 
36  auto alloc_p = reinterpret_cast<alloc_page*>(&alloc_pages[ext]);
37  w_assert0(alloc_pid == alloc_p->pid);
38  return alloc_p->get_bit(pid - alloc_pid);
39  }
40 
42  return end_pid;
43  }
44 
45 private:
46  // Required if vol_t uses O_DIRECT (true by default)
48 
49  const PageID end_pid;
50 
51  std::vector<generic_page, Alloc> alloc_pages;
52 
53  std::vector<bool> loaded_ext;
54 
55  void ensure_loaded(size_t ext) {
56  w_assert0(ext < alloc_pages.size());
57  if (!loaded_ext[ext]) {
58  smlevel_0::vol->read_backup(ext * extent_size, 1, &alloc_pages[ext]);
59  loaded_ext[ext] = true;
60  }
61  }
62 };
63 
64 #endif
static constexpr size_t extent_size
Definition: backup_alloc_cache.h:11
Definition: backup_alloc_cache.h:9
std::vector< bool > loaded_ext
Definition: backup_alloc_cache.h:53
backup_alloc_cache_t(PageID end_pid)
Definition: backup_alloc_cache.h:13
static constexpr size_t extent_size
Definition: alloc_cache.h:63
Definition: allocator.h:59
void ensure_loaded(size_t ext)
Definition: backup_alloc_cache.h:55
uint32_t PageID
Definition: basics.h:45
#define w_assert0(x)
Default assert/debug level is 0.
Definition: w_base.h:175
Free-page allocation/deallocation page.
Definition: alloc_page.h:28
PageID get_end_pid()
Definition: backup_alloc_cache.h:41
bool is_allocated(PageID pid)
Definition: backup_alloc_cache.h:24
std::vector< generic_page, Alloc > alloc_pages
Definition: backup_alloc_cache.h:51
const PageID end_pid
Definition: backup_alloc_cache.h:49