Flex  0.17.9
allocators.h
Go to the documentation of this file.
1 
16 #ifndef GRAPHSCOPE_UTILS_ALLOCATORS_H_
17 #define GRAPHSCOPE_UTILS_ALLOCATORS_H_
18 
19 #include <stdlib.h>
20 
21 #include <functional>
22 #include <string>
23 #include <vector>
24 
25 #include "flex/utils/mmap_array.h"
26 
27 namespace gs {
28 
30  static constexpr size_t batch_size = 16 * 1024 * 1024;
31 
32  public:
33  ArenaAllocator(MemoryStrategy strategy, const std::string& prefix)
34  : strategy_(strategy),
35  prefix_(prefix),
36  cur_loc_(0),
37  cur_size_(0),
41  prefix_.clear();
42  }
43  }
45  for (auto ptr : mmap_buffers_) {
46  delete ptr;
47  }
48  }
49 
50  void reserve(size_t cap) {
51  if (cur_size_ - cur_loc_ >= cap) {
52  return;
53  }
54  cap = (cap + batch_size - 1) ^ (batch_size - 1);
56  cur_loc_ = 0;
57  cur_size_ = cap;
58  }
59 
60  void* allocate(size_t size) {
61  allocated_memory_ += size;
62  if (cur_size_ - cur_loc_ >= size) {
63  void* ret = (char*) cur_buffer_ + cur_loc_;
64  cur_loc_ += size;
65  return ret;
66  } else if (size >= batch_size / 2) {
67  return allocate_batch(size);
68  } else {
70  void* ret = cur_buffer_;
71  cur_loc_ = size;
73  return ret;
74  }
75  }
76 
77  size_t allocated_memory() const { return allocated_memory_; }
78 
79  private:
80  void* allocate_batch(size_t size) {
81  allocated_batches_ += size;
82  if (prefix_.empty()) {
85  buf->open_with_hugepages("", size);
86  } else {
87  buf->open("", false);
88  }
89  buf->resize(size);
90  mmap_buffers_.push_back(buf);
91  return static_cast<void*>(buf->data());
92  } else {
94  buf->open(prefix_ + std::to_string(mmap_buffers_.size()), true);
95  buf->resize(size);
96  mmap_buffers_.push_back(buf);
97  return static_cast<void*>(buf->data());
98  }
99  }
100 
102  std::string prefix_;
103  std::vector<mmap_array<char>*> mmap_buffers_;
104 
105  void* cur_buffer_;
106  size_t cur_loc_;
107  size_t cur_size_;
108 
111 };
112 
114 
115 } // namespace gs
116 
117 #endif // GRAPHSCOPE_UTILS_ALLOCATORS_H_
gs::MemoryStrategy
MemoryStrategy
Definition: mmap_array.h:58
gs::ArenaAllocator::strategy_
MemoryStrategy strategy_
Definition: allocators.h:101
gs::ArenaAllocator::allocate_batch
void * allocate_batch(size_t size)
Definition: allocators.h:80
gs::ArenaAllocator::~ArenaAllocator
~ArenaAllocator()
Definition: allocators.h:44
gs::ArenaAllocator::prefix_
std::string prefix_
Definition: allocators.h:102
std::to_string
std::string to_string(const gs::flex::interactive::Code &status)
Definition: result.h:166
gs::mmap_array::resize
void resize(size_t size)
Definition: mmap_array.h:319
gs::ArenaAllocator::allocated_memory_
size_t allocated_memory_
Definition: allocators.h:109
gs
Definition: adj_list.h:23
gs::ArenaAllocator::batch_size
static constexpr size_t batch_size
Definition: allocators.h:30
gs::ArenaAllocator::ArenaAllocator
ArenaAllocator(MemoryStrategy strategy, const std::string &prefix)
Definition: allocators.h:33
gs::ArenaAllocator::cur_buffer_
void * cur_buffer_
Definition: allocators.h:105
gs::mmap_array::open
void open(const std::string &filename, bool sync_to_file=false)
Definition: mmap_array.h:129
gs::ArenaAllocator
Definition: allocators.h:29
gs::mmap_array::data
T * data()
Definition: mmap_array.h:405
gs::MemoryStrategy::kHugepagePrefered
@ kHugepagePrefered
gs::MemoryStrategy::kSyncToFile
@ kSyncToFile
gs::ArenaAllocator::mmap_buffers_
std::vector< mmap_array< char > * > mmap_buffers_
Definition: allocators.h:103
gs::ArenaAllocator::allocated_memory
size_t allocated_memory() const
Definition: allocators.h:77
gs::ArenaAllocator::cur_loc_
size_t cur_loc_
Definition: allocators.h:106
mmap_array.h
gs::mmap_array< char >
gs::ArenaAllocator::allocate
void * allocate(size_t size)
Definition: allocators.h:60
gs::ArenaAllocator::allocated_batches_
size_t allocated_batches_
Definition: allocators.h:110
gs::mmap_array::open_with_hugepages
void open_with_hugepages(const std::string &filename, size_t capacity=0)
Definition: mmap_array.h:214
gs::ArenaAllocator::cur_size_
size_t cur_size_
Definition: allocators.h:107
gs::ArenaAllocator::reserve
void reserve(size_t cap)
Definition: allocators.h:50