|
1
|
1 #pragma once
|
|
|
2
|
|
|
3 namespace pfc {
|
|
|
4
|
|
|
5 void alignedAlloc( void* & ptr, size_t & ptrSize, size_t newSize, size_t alignBytes);
|
|
|
6 void * alignedAlloc( size_t size, size_t align );
|
|
|
7 void alignedFree( void * ptr );
|
|
|
8
|
|
|
9 template<unsigned alignBytes = 16>
|
|
|
10 class mem_block_aligned {
|
|
|
11 public:
|
|
|
12 typedef mem_block_aligned<alignBytes> self_t;
|
|
|
13 mem_block_aligned() : m_ptr(), m_size() {}
|
|
|
14
|
|
|
15 void * ptr() {return m_ptr;}
|
|
|
16 const void * ptr() const {return m_ptr;}
|
|
|
17 void * get_ptr() {return m_ptr;}
|
|
|
18 const void * get_ptr() const {return m_ptr;}
|
|
|
19 size_t size() const {return m_size;}
|
|
|
20 size_t get_size() const {return m_size;}
|
|
|
21
|
|
|
22 void resize(size_t s) {
|
|
|
23 alignedAlloc( m_ptr, m_size, s, alignBytes );
|
|
|
24 }
|
|
|
25 void set_size(size_t s) {resize(s);}
|
|
|
26
|
|
|
27 ~mem_block_aligned() {
|
|
|
28 alignedFree(m_ptr);
|
|
|
29 }
|
|
|
30
|
|
|
31 self_t const & operator=(self_t const & other) {
|
|
|
32 assign(other);
|
|
|
33 return *this;
|
|
|
34 }
|
|
|
35 mem_block_aligned(self_t const & other) : m_ptr(), m_size() {
|
|
|
36 assign(other);
|
|
|
37 }
|
|
|
38 void assign(self_t const & other) {
|
|
|
39 resize(other.size());
|
|
|
40 memcpy(ptr(), other.ptr(), size());
|
|
|
41 }
|
|
|
42 mem_block_aligned(self_t && other) {
|
|
|
43 m_ptr = other.m_ptr;
|
|
|
44 m_size = other.m_size;
|
|
|
45 other.m_ptr = NULL; other.m_size = 0;
|
|
|
46 }
|
|
|
47 self_t const & operator=(self_t && other) {
|
|
|
48 alignedFree(m_ptr);
|
|
|
49 m_ptr = other.m_ptr;
|
|
|
50 m_size = other.m_size;
|
|
|
51 other.m_ptr = NULL; other.m_size = 0;
|
|
|
52 return *this;
|
|
|
53 }
|
|
|
54
|
|
|
55 private:
|
|
|
56 void * m_ptr;
|
|
|
57 size_t m_size;
|
|
|
58 };
|
|
|
59
|
|
|
60 template<typename obj_t, unsigned alignBytes = 16>
|
|
|
61 class mem_block_aligned_t {
|
|
|
62 public:
|
|
|
63 typedef mem_block_aligned_t<obj_t, alignBytes> self_t;
|
|
|
64 void resize(size_t s) { m.resize( multiply_guarded(s, sizeof(obj_t) ) ); }
|
|
|
65 void set_size(size_t s) {resize(s);}
|
|
|
66 size_t size() const { return m.size() / sizeof(obj_t); }
|
|
|
67 size_t get_size() const {return size();}
|
|
|
68 obj_t * ptr() { return reinterpret_cast<obj_t*>(m.ptr()); }
|
|
|
69 const obj_t * ptr() const { return reinterpret_cast<const obj_t*>(m.ptr()); }
|
|
|
70 obj_t * get_ptr() { return reinterpret_cast<obj_t*>(m.ptr()); }
|
|
|
71 const obj_t * get_ptr() const { return reinterpret_cast<const obj_t*>(m.ptr()); }
|
|
|
72 mem_block_aligned_t() {}
|
|
|
73 private:
|
|
|
74 mem_block_aligned<alignBytes> m;
|
|
|
75 };
|
|
|
76
|
|
|
77 template<typename obj_t, unsigned alignBytes = 16>
|
|
|
78 class mem_block_aligned_incremental_t {
|
|
|
79 public:
|
|
|
80 typedef mem_block_aligned_t<obj_t, alignBytes> self_t;
|
|
|
81
|
|
|
82 void resize(size_t s) {
|
|
|
83 if (s > m.size()) {
|
|
|
84 m.resize( multiply_guarded<size_t>(s, 3) / 2 );
|
|
|
85 }
|
|
|
86 m_size = s;
|
|
|
87 }
|
|
|
88 void set_size(size_t s) {resize(s);}
|
|
|
89
|
|
|
90 size_t size() const { return m_size; }
|
|
|
91 size_t get_size() const {return m_size; }
|
|
|
92
|
|
|
93 obj_t * ptr() { return m.ptr(); }
|
|
|
94 const obj_t * ptr() const { return m.ptr(); }
|
|
|
95 obj_t * get_ptr() { return m.ptr(); }
|
|
|
96 const obj_t * get_ptr() const { return m.ptr(); }
|
|
|
97 mem_block_aligned_incremental_t() : m_size() {}
|
|
|
98 mem_block_aligned_incremental_t(self_t const & other) : m(other.m), m_size(other.m_size) {}
|
|
|
99 mem_block_aligned_incremental_t(self_t && other) : m(std::move(other.m)), m_size(other.m_size) { other.m_size = 0; }
|
|
|
100 self_t const & operator=(self_t const & other) {m = other.m; m_size = other.m_size; return *this;}
|
|
|
101 self_t const & operator=(self_t && other) {m = std::move(other.m); m_size = other.m_size; other.m_size = 0; return *this;}
|
|
|
102 private:
|
|
|
103 mem_block_aligned_t<obj_t, alignBytes> m;
|
|
|
104 size_t m_size;
|
|
|
105 };
|
|
|
106 }
|