foobar2000 SDK  2015-01-14
memalign.h
Go to the documentation of this file.
1 #ifndef _MSC_VER
2 #include <stdlib.h>
3 #endif
4 
5 namespace pfc {
6  template<unsigned alignBytes = 16>
8  public:
11 
12  void * ptr() {return m_ptr;}
13  const void * ptr() const {return m_ptr;}
14  void * get_ptr() {return m_ptr;}
15  const void * get_ptr() const {return m_ptr;}
16  size_t size() const {return m_size;}
17  size_t get_size() const {return m_size;}
18 
19  void resize(size_t s) {
20  if (s == m_size) {
21  // nothing to do
22  } else if (s == 0) {
23  _free(m_ptr);
24  m_ptr = NULL;
25  } else {
26  void * ptr;
27 #ifdef _MSC_VER
28  if (m_ptr == NULL) ptr = _aligned_malloc(s, alignBytes);
29  else ptr = _aligned_realloc(m_ptr, s, alignBytes);
30  if ( ptr == NULL ) throw std::bad_alloc();
31 #else
32 #ifdef __ANDROID__
33  if ((ptr = memalign( s, alignBytes )) == NULL) throw std::bad_alloc();
34 #else
35  if (posix_memalign( &ptr, alignBytes, s ) < 0) throw std::bad_alloc();
36 #endif
37  if (m_ptr != NULL) {
38  memcpy( ptr, m_ptr, min_t<size_t>( m_size, s ) );
39  _free( m_ptr );
40  }
41 #endif
42  m_ptr = ptr;
43  }
44  m_size = s;
45  }
46  void set_size(size_t s) {resize(s);}
47 
49  _free(m_ptr);
50  }
51 
52  self_t const & operator=(self_t const & other) {
53  assign(other);
54  return *this;
55  }
56  mem_block_aligned(self_t const & other) : m_ptr(), m_size() {
57  assign(other);
58  }
59  void assign(self_t const & other) {
60  resize(other.size());
61  memcpy(ptr(), other.ptr(), size());
62  }
63  mem_block_aligned(self_t && other) {
64  m_ptr = other.m_ptr;
65  m_size = other.m_size;
66  other.m_ptr = NULL; other.m_size = 0;
67  }
68  self_t const & operator=(self_t && other) {
69  _free(m_ptr);
70  m_ptr = other.m_ptr;
71  m_size = other.m_size;
72  other.m_ptr = NULL; other.m_size = 0;
73  return *this;
74  }
75 
76  private:
77  static void _free(void * ptr) {
78 #ifdef _MSC_VER
79  _aligned_free(ptr);
80 #else
81  free(ptr);
82 #endif
83  }
84 
85  void * m_ptr;
86  size_t m_size;
87  };
88 
89  template<typename obj_t, unsigned alignBytes = 16>
91  public:
93  void resize(size_t s) { m.resize( multiply_guarded(s, sizeof(obj_t) ) ); }
94  void set_size(size_t s) {resize(s);}
95  size_t size() const { return m.size() / sizeof(obj_t); }
96  size_t get_size() const {return size();}
97  obj_t * ptr() { return reinterpret_cast<obj_t*>(m.ptr()); }
98  const obj_t * ptr() const { return reinterpret_cast<const obj_t*>(m.ptr()); }
99  obj_t * get_ptr() { return reinterpret_cast<obj_t*>(m.ptr()); }
100  const obj_t * get_ptr() const { return reinterpret_cast<const obj_t*>(m.ptr()); }
102  mem_block_aligned_t(self_t const & other) : m(other.m) {}
103  mem_block_aligned_t(self_t && other) : m(std::move(other.m)) {}
104  self_t const & operator=(self_t const & other) {m = other.m; return *this;}
105  self_t const & operator=(self_t && other) {m = std::move(other.m); return *this;}
106  private:
108  };
109 
110  template<typename obj_t, unsigned alignBytes = 16>
112  public:
114 
115  void resize(size_t s) {
116  if (s > m.size()) {
117  m.resize( multiply_guarded<size_t>(s, 3) / 2 );
118  }
119  m_size = s;
120  }
121  void set_size(size_t s) {resize(s);}
122 
123  size_t size() const { return m_size; }
124  size_t get_size() const {return m_size; }
125 
126  obj_t * ptr() { return m.ptr(); }
127  const obj_t * ptr() const { return m.ptr(); }
128  obj_t * get_ptr() { return m.ptr(); }
129  const obj_t * get_ptr() const { return m.ptr(); }
131  mem_block_aligned_incremental_t(self_t const & other) : m(other.m), m_size(other.m_size) {}
132  mem_block_aligned_incremental_t(self_t && other) : m(std::move(other.m)), m_size(other.m_size) { other.m_size = 0; }
133  self_t const & operator=(self_t const & other) {m = other.m; m_size = other.m_size; return *this;}
134  self_t const & operator=(self_t && other) {m = std::move(other.m); m_size = other.m_size; other.m_size = 0; return *this;}
135  private:
137  size_t m_size;
138  };
139 }
const obj_t * ptr() const
Definition: memalign.h:127
mem_block_aligned(self_t &&other)
Definition: memalign.h:63
mem_block_aligned_t(self_t &&other)
Definition: memalign.h:103
size_t get_size() const
Definition: memalign.h:96
mem_block_aligned< alignBytes > self_t
Definition: memalign.h:9
const void * get_ptr() const
Definition: memalign.h:15
mem_block_aligned(self_t const &other)
Definition: memalign.h:56
t_int multiply_guarded(t_int v1, t_int v2)
Definition: primitives.h:593
self_t const & operator=(self_t const &other)
Definition: memalign.h:104
STL namespace.
size_t size() const
Definition: memalign.h:16
mem_block_aligned_t< obj_t, alignBytes > m
Definition: memalign.h:136
self_t const & operator=(self_t &&other)
Definition: memalign.h:105
self_t const & operator=(self_t const &other)
Definition: memalign.h:133
void resize(size_t s)
Definition: memalign.h:19
size_t get_size() const
Definition: memalign.h:17
self_t const & operator=(self_t const &other)
Definition: memalign.h:52
const void * ptr() const
Definition: memalign.h:13
const obj_t * get_ptr() const
Definition: memalign.h:129
size_t size() const
Definition: memalign.h:95
mem_block_aligned< alignBytes > m
Definition: memalign.h:107
mem_block_aligned_incremental_t(self_t const &other)
Definition: memalign.h:131
mem_block_aligned_t< obj_t, alignBytes > self_t
Definition: memalign.h:113
mem_block_aligned_incremental_t(self_t &&other)
Definition: memalign.h:132
void assign(self_t const &other)
Definition: memalign.h:59
void resize(size_t s)
Definition: memalign.h:93
mem_block_aligned_t(self_t const &other)
Definition: memalign.h:102
self_t const & operator=(self_t &&other)
Definition: memalign.h:134
static void _free(void *ptr)
Definition: memalign.h:77
self_t const & operator=(self_t &&other)
Definition: memalign.h:68
const obj_t * ptr() const
Definition: memalign.h:98
const obj_t * get_ptr() const
Definition: memalign.h:100
void set_size(size_t s)
Definition: memalign.h:94
mem_block_aligned_t< obj_t, alignBytes > self_t
Definition: memalign.h:92
void set_size(size_t s)
Definition: memalign.h:46