CernVM-FS  2.10.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bigvector.h
Go to the documentation of this file.
1 
7 #ifndef CVMFS_BIGVECTOR_H_
8 #define CVMFS_BIGVECTOR_H_
9 
10 #include <cassert>
11 #include <cstdlib>
12 
13 #include "smalloc.h"
14 
15 template<class Item>
16 class BigVector {
17  public:
19  Alloc(kNumInit);
20  size_ = 0;
21  shared_buffer_ = false;
22  }
23 
24  explicit BigVector(const size_t num_items) {
25  assert(num_items > 0);
26  Alloc(num_items);
27  size_ = 0;
28  shared_buffer_ = false;
29  }
30 
31  BigVector(const BigVector<Item> &other) {
32  CopyFrom(other);
33  }
34 
36  if (&other == this)
37  return *this;
38 
39  if (!shared_buffer_)
40  Dealloc();
41  CopyFrom(other);
42  return *this;
43  }
44 
46  if (!shared_buffer_)
47  Dealloc();
48  }
49 
50  Item At(const size_t index) const {
51  assert(index < size_);
52  return buffer_[index];
53  }
54 
55  const Item *AtPtr(const size_t index) const {
56  assert(index < size_);
57  return &buffer_[index];
58  }
59 
60  void PushBack(const Item &item) {
61  if (size_ == capacity_)
63  new (buffer_ + size_) Item(item);
64  size_++;
65  }
66 
67  bool IsEmpty() const {
68  return size_ == 0;
69  }
70 
71  void Clear() {
72  Dealloc();
73  Alloc(kNumInit);
74  }
75 
76  void ShareBuffer(Item **duplicate, bool *large_alloc) {
77  *duplicate = buffer_;
78  *large_alloc = large_alloc_;
79  shared_buffer_ = true;
80  }
81 
82  void DoubleCapacity() {
83  Item *old_buffer = buffer_;
84  bool old_large_alloc = large_alloc_;
85 
86  assert(capacity_ > 0);
87  Alloc(capacity_ * 2);
88  for (size_t i = 0; i < size_; ++i)
89  new (buffer_ + i) Item(old_buffer[i]);
90 
91  FreeBuffer(old_buffer, size_, old_large_alloc);
92  }
93 
94  // Careful! Only for externally modified buffer.
95  void SetSize(const size_t new_size) {
96  assert(new_size <= capacity_);
97  size_ = new_size;
98  }
99 
100  size_t size() const { return size_; }
101  size_t capacity() const { return capacity_; }
102 
103  private:
104  static const size_t kNumInit = 16;
105  static const size_t kMmapThreshold = 128*1024;
106 
107  void Alloc(const size_t num_elements) {
108  size_t num_bytes = sizeof(Item) * num_elements;
109  if (num_bytes >= kMmapThreshold) {
110  buffer_ = static_cast<Item *>(smmap(num_bytes));
111  large_alloc_ = true;
112  } else {
113  buffer_ = static_cast<Item *>(smalloc(num_bytes));
114  large_alloc_ = false;
115  }
116  capacity_ = num_elements;
117  }
118 
119  void Dealloc() {
121  buffer_ = NULL;
122  capacity_ = 0;
123  size_ = 0;
124  }
125 
126  void FreeBuffer(Item *buf, const size_t size, const bool large) {
127  for (size_t i = 0; i < size; ++i)
128  buf[i].~Item();
129 
130  if (buf) {
131  if (large) {
132  smunmap(buf);
133  } else {
134  free(buf);
135  }
136  }
137  }
138 
139  void CopyFrom(const BigVector<Item> &other) {
140  Alloc(other.capacity_);
141  for (size_t i = 0; i < other.size_; ++i) {
142  new (buffer_ + i) Item(*other.AtPtr(i));
143  }
144  size_ = other.size_;
145  shared_buffer_ = false;
146  }
147 
148  Item *buffer_;
149  size_t size_;
150  size_t capacity_;
153 };
154 
155 #endif // CVMFS_BIGVECTOR_H_
Item At(const size_t index) const
Definition: bigvector.h:50
void ShareBuffer(Item **duplicate, bool *large_alloc)
Definition: bigvector.h:76
size_t size_
Definition: bigvector.h:149
bool shared_buffer_
Definition: bigvector.h:152
assert((mem||(size==0))&&"Out Of Memory")
~BigVector()
Definition: bigvector.h:45
Item * buffer_
Definition: bigvector.h:148
void Dealloc()
Definition: bigvector.h:119
void FreeBuffer(Item *buf, const size_t size, const bool large)
Definition: bigvector.h:126
void DoubleCapacity()
Definition: bigvector.h:82
BigVector(const BigVector< Item > &other)
Definition: bigvector.h:31
void CopyFrom(const BigVector< Item > &other)
Definition: bigvector.h:139
BigVector< Item > & operator=(const BigVector< Item > &other)
Definition: bigvector.h:35
void SetSize(const size_t new_size)
Definition: bigvector.h:95
void Clear()
Definition: bigvector.h:71
void Alloc(const size_t num_elements)
Definition: bigvector.h:107
size_t capacity() const
Definition: bigvector.h:101
BigVector(const size_t num_items)
Definition: bigvector.h:24
bool IsEmpty() const
Definition: bigvector.h:67
static const size_t kNumInit
Definition: bigvector.h:104
void PushBack(const Item &item)
Definition: bigvector.h:60
BigVector()
Definition: bigvector.h:18
static const size_t kMmapThreshold
Definition: bigvector.h:105
bool large_alloc_
Definition: bigvector.h:151
size_t capacity_
Definition: bigvector.h:150
const Item * AtPtr(const size_t index) const
Definition: bigvector.h:55
size_t size() const
Definition: bigvector.h:100