Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
buffer_factory.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Roc Streaming authors
3  *
4  * This Source Code Form is subject to the terms of the Mozilla Public
5  * License, v. 2.0. If a copy of the MPL was not distributed with this
6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
7  */
8 
9 //! @file roc_core/buffer_factory.h
10 //! @brief Buffer factory.
11 
12 #ifndef ROC_CORE_BUFFER_FACTORY_H_
13 #define ROC_CORE_BUFFER_FACTORY_H_
14 
16 #include "roc_core/buffer.h"
17 #include "roc_core/noncopyable.h"
18 #include "roc_core/shared_ptr.h"
19 #include "roc_core/slab_pool.h"
20 
21 namespace roc {
22 namespace core {
23 
24 //! Buffer factory.
25 //! Allows to instantiate fixed-size buffers.
26 //! @tparam T define buffer element type.
27 template <class T> class BufferFactory : public core::NonCopyable<> {
28 public:
29  //! Initialization.
30  //! @p buffer_size defines number of elements in buffer.
32  : buffer_pool_("buffer_pool", arena, sizeof(Buffer<T>) + sizeof(T) * buffer_size)
33  , buffer_size_(buffer_size) {
34  }
35 
36  //! Get number of elements in buffer.
37  size_t buffer_size() const {
38  return buffer_size_;
39  }
40 
41  //! Allocate new buffer.
43  return new (buffer_pool_) Buffer<T>(buffer_pool_, buffer_size_);
44  }
45 
46 private:
47  SlabPool<Buffer<T> > buffer_pool_;
48  const size_t buffer_size_;
49 };
50 
51 } // namespace core
52 } // namespace roc
53 
54 #endif // ROC_CORE_BUFFER_FACTORY_H_
Allocation policies.
Buffer.
Buffer factory. Allows to instantiate fixed-size buffers.
size_t buffer_size() const
Get number of elements in buffer.
BufferFactory(IArena &arena, size_t buffer_size)
Initialization. buffer_size defines number of elements in buffer.
SharedPtr< Buffer< T > > new_buffer()
Allocate new buffer.
Fixed-size dynamically-allocated buffer.
Definition: buffer.h:35
Memory arena interface.
Definition: iarena.h:23
Base class for non-copyable objects.
Definition: noncopyable.h:23
Shared ownership intrusive pointer.
Definition: shared_ptr.h:32
Memory pool.
Definition: slab_pool.h:65
Root namespace.
Non-copyable object.
Shared ownership intrusive pointer.
Memory pool.