libvisual  0.5.0
lv_buffer.cpp
1 /* Libvisual - The audio visualisation framework.
2  *
3  * Copyright (C) 2012 Libvisual team
4  * 2004-2006 Dennis Smit
5  *
6  * Authors: Chong Kai Xiong <kaixiong@codeleft.sg>
7  * Dennis Smit <ds@nerds-incorporated.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU Lesser General Public License as
11  * published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  */
23 
24 #include "config.h"
25 #include "lv_buffer.h"
26 #include "lv_common.h"
27 
28 namespace LV {
29 
30  class Buffer::Impl
31  {
32  public:
33 
34  void* data;
35  std::size_t size;
36  bool is_owner;
37 
38  Impl ()
39  : data (0)
40  , size (0)
41  , is_owner (false)
42  {}
43 
44  ~Impl ()
45  {
46  free ();
47  }
48 
49  void wrap (void* data_, std::size_t size_, bool own)
50  {
51  if (is_owner) {
52  visual_mem_free (data);
53  }
54 
55  data = data_;
56  size = size_;
57  is_owner = own;
58  }
59 
60  void allocate (std::size_t size_)
61  {
62  if (is_owner) {
63  visual_mem_free (data);
64  }
65 
66  data = visual_mem_malloc0 (size_);
67  size = size_;
68  is_owner = true;
69  }
70 
71  void free ()
72  {
73  if (is_owner) {
74  visual_mem_free (data);
75  }
76 
77  data = 0;
78  size = 0;
79  is_owner = false;
80  }
81  };
82 
83  Buffer::Buffer ()
84  : m_impl (new Impl)
85  , m_ref_count (1)
86  {
87  }
88 
90  {
91  return BufferPtr (new Buffer, false);
92  }
93 
94  BufferPtr Buffer::wrap (void* data, std::size_t size, bool own)
95  {
96  BufferPtr self (new Buffer, false);
97 
98  self->m_impl->wrap (data, size, own);
99 
100  return self;
101  }
102 
103  BufferPtr Buffer::create (std::size_t size)
104  {
105  BufferPtr self (new Buffer, false);
106 
107  self->m_impl->allocate (size);
108 
109  return self;
110  }
111 
112  Buffer::~Buffer ()
113  {
114  // empty
115  }
116 
118  {
119  m_impl->free ();
120  }
121 
122  void Buffer::set (void* data, std::size_t size)
123  {
124  m_impl->wrap (data, size, false);
125  }
126 
127  void Buffer::set_size (std::size_t size)
128  {
129  m_impl->size = size;
130  }
131 
132  void Buffer::set_data (void *data)
133  {
134  m_impl->wrap (data, m_impl->size, false);
135  }
136 
137  void Buffer::allocate (std::size_t size)
138  {
139  m_impl->allocate (size);
140  }
141 
142  void* Buffer::get_data () const
143  {
144  return m_impl->data;
145  }
146 
147  void* Buffer::get_data (std::size_t offset) const
148  {
149  return static_cast<uint8_t*> (m_impl->data) + offset;
150  }
151 
152  std::size_t Buffer::get_size () const
153  {
154  return m_impl->size;
155  }
156 
157  bool Buffer::is_allocated () const
158  {
159  return m_impl->is_owner;
160  }
161 
162  void Buffer::copy (BufferConstPtr const& src)
163  {
164  m_impl->allocate (src->m_impl->size);
165  visual_mem_copy (m_impl->data, src->m_impl->data, src->m_impl->size);
166  }
167 
168  void Buffer::copy_to (void* dest, std::size_t size)
169  {
170  visual_return_if_fail (dest != nullptr);
171 
172  visual_mem_copy (dest, m_impl->data, std::min (size, m_impl->size));
173  }
174 
175  void Buffer::copy_to (BufferPtr const& dest)
176  {
177  copy_to (dest->get_data (), dest->get_size ());
178  }
179 
180  void Buffer::put (BufferConstPtr const& src, std::size_t offset)
181  {
182  put (src->m_impl->data, src->m_impl->size, offset);
183  }
184 
185  void Buffer::put (void const* data, std::size_t size, std::size_t offset)
186  {
187  visual_return_if_fail (data != nullptr);
188  visual_return_if_fail (offset < m_impl->size);
189 
190  size = std::min (m_impl->size - offset, size);
191 
192  visual_mem_copy (static_cast<uint8_t*> (m_impl->data) + offset, data, size);
193  }
194 
195  void Buffer::fill (uint8_t value)
196  {
197  visual_mem_set (m_impl->data, value, m_impl->size);
198  }
199 
200  void Buffer::fill_with_pattern (void const* data, std::size_t size)
201  {
202  visual_return_if_fail (data != nullptr);
203 
204  for (std::size_t offset = 0; offset < m_impl->size; offset += size)
205  put (data, size, offset);
206  }
207 
208 } // LV namespace