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
18typedef unsigned char byte;
19
20#define MEM_ALIGNMENT 32
21
22/**
23 * \brief It override the behavior if size()
24 *
25 * On normal memory like HeapMemory if you try to use resize to shrink the memory, nothing happen and size() return the old size.
26 * In case of BMemory<HeapMemory> if you try to shrink still the memory is not shrinked, but size() return the shrinked size.
27 * This gives a "feeling" of shrinkage. The real internal size can be retrieved with msize(). When we use resize to increase
28 * the memory size the behaviour remain the same as normal HeapMemory.
29 *
30 * \note this wrapper can be used in combination also with CudaMemory
31 *
32 * ### Allocate memory
33 *
34 * \snippet HeapMemory_unit_tests.hpp BAllocate some memory and fill with data
35 *
36 * ### Resize memory
37 *
38 * \snippet HeapMemory_unit_tests.hpp BResize the memory
39 *
40 * ### Shrink memory
41 *
42 * \snippet HeapMemory_unit_tests.hpp BShrink memory
43 *
44 */
45template<typename Memory>
46class BMemory : public Memory
47{
48 //! size of the memory
49 size_t buf_sz;
50
51public:
52
53 /*! \brief Copy the Heap memory
54 *
55 * \param mem memory to copy
56 *
57 */
58 BMemory(const BMemory<Memory> & mem)
59 :Memory(mem),buf_sz(mem.size())
60 {
61 }
62
63 /*! \brief Copy the Heap memory
64 *
65 * \param mem memory to copy
66 *
67 */
68 BMemory(BMemory<Memory> && mem) noexcept
69 :Memory((Memory &&)mem),buf_sz(mem.size())
70 {
71 }
72
73 //! Constructor, we choose a default alignment of 32 for avx
74 BMemory()
75 :Memory(),buf_sz(0)
76 {};
77
78 //! Destructor
79 virtual ~BMemory() noexcept
80 {
81 };
82
83 /*! \brief allocate the memory
84 *
85 * Resize the allocated memory, if request is smaller than the allocated memory
86 * is not resized
87 *
88 * \param sz size
89 * \return true if the resize operation complete correctly
90 *
91 */
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
102 /*! \brief Resize the allocated memory
103 *
104 * Resize the allocated memory, if request is smaller than the allocated memory
105 * is not resized
106 *
107 * \param sz size
108 * \return true if the resize operation complete correctly
109 *
110 */
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
122 /*! \brief Resize the buffer size
123 *
124 * \return the buffer size
125 *
126 */
127 virtual size_t size() const
128 {
129 return buf_sz;
130 }
131
132 /*! \brief Return the memory size
133 *
134 *
135 * \return The allocated memory size
136 *
137 */
138 size_t msize()
139 {
140 return Memory::size();
141 }
142
143 /*! \brief Copy the memory
144 *
145 * \param mem memory to copy
146 *
147 * \return itself
148 *
149 */
150 BMemory & operator=(const BMemory<Memory> & mem)
151 {
152 buf_sz = mem.buf_sz;
153 static_cast<Memory *>(this)->operator=(mem);
154
155 return *this;
156 }
157
158 /*! \brief Copy the memory
159 *
160 * \param mem memory to copy
161 *
162 * \return itself
163 *
164 */
165 BMemory & operator=(BMemory<Memory> && mem)
166 {
167 buf_sz = mem.buf_sz;
168 static_cast<Memory *>(this)->operator=(mem);
169
170 return *this;
171 }
172
173 /*! \brief Destroy the internal memory
174 *
175 *
176 */
177 void destroy()
178 {
179 Memory::destroy();
180 buf_sz = 0;
181 }
182
183 /*! \brief swap the two memory object
184 *
185 * \param mem Memory to swap with
186 *
187 */
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
200