OpenFPM_pdata  4.1.0
Project that contain the implementation of distributed structures
BHeapMemory.hpp
1 /*
2  * RHeapMempory.hpp
3  *
4  * Created on: Aug 17, 2014
5  * Author: Pietro Incardona
6  */
7 
8 
9 #ifndef BHEAP_MEMORY_HPP
10 #define BHEAP_MEMORY_HPP
11 
12 #include "config.h"
13 #include "memory.hpp"
14 #include <cstddef>
15 #include <cstdint>
16 #include <iostream>
17 
18 typedef unsigned char byte;
19 
20 #define MEM_ALIGNMENT 32
21 
45 template<typename Memory>
46 class BMemory : public Memory
47 {
49  size_t buf_sz;
50 
51 public:
52 
58  BMemory(const BMemory<Memory> & mem)
59  :Memory(mem),buf_sz(mem.size())
60  {
61  }
62 
68  BMemory(BMemory<Memory> && mem) noexcept
69  :Memory((Memory &&)mem),buf_sz(mem.size())
70  {
71  }
72 
75  :Memory(),buf_sz(0)
76  {};
77 
79  virtual ~BMemory() noexcept
80  {
81  };
82 
92  virtual bool allocate(size_t sz)
93  {
94  bool ret = Memory::allocate(sz);
95 
96  if (ret == true)
97  buf_sz = sz;
98 
99  return ret;
100  }
101 
111  virtual bool resize(size_t sz)
112  {
113  bool ret = Memory::resize(sz);
114 
115  // if the allocated memory is enough, do not resize
116  if (ret == true)
117  buf_sz = sz;
118 
119  return ret;
120  }
121 
127  virtual size_t size() const
128  {
129  return buf_sz;
130  }
131 
138  size_t msize()
139  {
140  return Memory::size();
141  }
142 
151  {
152  buf_sz = mem.buf_sz;
153  static_cast<Memory *>(this)->operator=(mem);
154 
155  return *this;
156  }
157 
166  {
167  buf_sz = mem.buf_sz;
168  static_cast<Memory *>(this)->operator=(mem);
169 
170  return *this;
171  }
172 
177  void destroy()
178  {
179  Memory::destroy();
180  buf_sz = 0;
181  }
182 
188  void swap(BMemory<Memory> & mem)
189  {
190  Memory::swap(mem);
191 
192  size_t buf_sz_t = mem.buf_sz;
193  mem.buf_sz = buf_sz;
194  buf_sz = buf_sz_t;
195  }
196 };
197 
198 
199 #endif
size_t msize()
Return the memory size.
It override the behavior if size()
Definition: BHeapMemory.hpp:46
BMemory(BMemory< Memory > &&mem) noexcept
Copy the Heap memory.
Definition: BHeapMemory.hpp:68
virtual ~BMemory() noexcept
Destructor.
Definition: BHeapMemory.hpp:79
void destroy()
Destroy the internal memory.
BMemory & operator=(BMemory< Memory > &&mem)
Copy the memory.
virtual size_t size() const
Resize the buffer size.
BMemory(const BMemory< Memory > &mem)
Copy the Heap memory.
Definition: BHeapMemory.hpp:58
void swap(BMemory< Memory > &mem)
swap the two memory object
size_t buf_sz
size of the memory
Definition: BHeapMemory.hpp:49
BMemory & operator=(const BMemory< Memory > &mem)
Copy the memory.
virtual bool allocate(size_t sz)
allocate the memory
Definition: BHeapMemory.hpp:92
virtual bool resize(size_t sz)
Resize the allocated memory.
BMemory()
Constructor, we choose a default alignment of 32 for avx.
Definition: BHeapMemory.hpp:74