CernVM-FS  2.12.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
buffer.h
Go to the documentation of this file.
1 
5 #ifndef CVMFS_UTIL_BUFFER_H_
6 #define CVMFS_UTIL_BUFFER_H_
7 
8 #include <cassert>
9 #include <cstddef>
10 #include <memory>
11 
12 
13 #ifdef CVMFS_NAMESPACE_GUARD
14 namespace CVMFS_NAMESPACE_GUARD {
15 #endif
16 
17 
18 template<typename T, class A = std::allocator<T> >
19 class Buffer {
20  public:
21  typedef typename A::pointer pointer_t;
22 
23  Buffer() : used_(0), size_(0), buffer_(NULL), initialized_(false) {}
24 
25  explicit Buffer(const size_t size)
26  : used_(0)
27  , size_(0)
28  , buffer_(NULL)
29  , initialized_(false)
30  {
31  Allocate(size);
32  }
33 
34  virtual ~Buffer() {
35  Deallocate();
36  }
37 
38  void Allocate(const size_t size) {
39  assert(!IsInitialized());
40  size_ = size;
41  buffer_ = allocator_.allocate(size_bytes());
42  initialized_ = true;
43  }
44 
45  bool IsInitialized() const { return initialized_; }
46 
47  typename A::pointer ptr() {
48  assert(IsInitialized());
49  return buffer_;
50  }
51  const typename A::pointer ptr() const {
52  assert(IsInitialized());
53  return buffer_;
54  }
55 
56  typename A::pointer free_space_ptr() {
57  assert(IsInitialized());
58  return buffer_ + used();
59  }
60 
61  const typename A::pointer free_space_ptr() const {
62  assert(IsInitialized());
63  return buffer_ + used();
64  }
65 
66  void SetUsed(const size_t items) {
67  assert(items <= size());
68  used_ = items;
69  }
70 
71  void SetUsedBytes(const size_t bytes) {
72  assert(bytes <= size_bytes());
73  assert(bytes % sizeof(T) == 0);
74  used_ = bytes / sizeof(T);
75  }
76 
77  size_t size() const { return size_; }
78  size_t size_bytes() const { return size_ * sizeof(T); }
79  size_t used() const { return used_; }
80  size_t used_bytes() const { return used_ * sizeof(T); }
81  size_t free() const { return size_ - used_; }
82  size_t free_bytes() const { return free() * sizeof(T); }
83 
84  private:
85  Buffer(const Buffer &other) { assert(false); } // no copy!
86  Buffer& operator=(const Buffer& other) { assert (false); }
87 
88  void Deallocate() {
89  if (size_ == 0) {
90  return;
91  }
92  allocator_.deallocate(buffer_, size_bytes());
93  buffer_ = NULL;
94  size_ = 0;
95  used_ = 0;
96  initialized_ = false;
97  }
98 
99  private:
101  size_t used_;
102  size_t size_;
103  typename A::pointer buffer_;
105 };
106 
107 #ifdef CVMFS_NAMESPACE_GUARD
108 } // namespace CVMFS_NAMESPACE_GUARD
109 #endif
110 
111 #endif // CVMFS_UTIL_BUFFER_H_
Buffer(const size_t size)
Definition: buffer.h:25
bool IsInitialized() const
Definition: buffer.h:45
size_t used_
Definition: buffer.h:101
virtual ~Buffer()
Definition: buffer.h:34
void Deallocate()
Definition: buffer.h:88
assert((mem||(size==0))&&"Out Of Memory")
A allocator_
Definition: buffer.h:100
size_t used_bytes() const
Definition: buffer.h:80
size_t used() const
Definition: buffer.h:79
A::pointer free_space_ptr()
Definition: buffer.h:56
A::pointer pointer_t
Definition: buffer.h:21
size_t free_bytes() const
Definition: buffer.h:82
size_t size() const
Definition: buffer.h:77
const A::pointer free_space_ptr() const
Definition: buffer.h:61
A::pointer ptr()
Definition: buffer.h:47
size_t free() const
Definition: buffer.h:81
void SetUsed(const size_t items)
Definition: buffer.h:66
A::pointer buffer_
Definition: buffer.h:103
void Allocate(const size_t size)
Definition: buffer.h:38
Buffer(const Buffer &other)
Definition: buffer.h:85
const A::pointer ptr() const
Definition: buffer.h:51
size_t size_
Definition: buffer.h:102
Buffer()
Definition: buffer.h:23
bool initialized_
Definition: buffer.h:104
Buffer & operator=(const Buffer &other)
Definition: buffer.h:86
Definition: buffer.h:19
void SetUsedBytes(const size_t bytes)
Definition: buffer.h:71
static void size_t size
Definition: smalloc.h:54
size_t size_bytes() const
Definition: buffer.h:78