Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
packet_factory.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2022 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_packet/packet_factory.h
10 //! @brief Packet factory.
11 
12 #ifndef ROC_PACKET_PACKET_FACTORY_H_
13 #define ROC_PACKET_PACKET_FACTORY_H_
14 
15 #include "roc_core/buffer.h"
16 #include "roc_core/iarena.h"
17 #include "roc_core/ipool.h"
18 #include "roc_core/noncopyable.h"
19 #include "roc_core/optional.h"
20 #include "roc_core/slab_pool.h"
21 #include "roc_packet/packet.h"
22 
23 namespace roc {
24 namespace packet {
25 
26 //! Packet factory.
27 //!
28 //! Allows allocating packets and packet buffers (byte buffers of appropriate size
29 //! that should be attached to packet to hold payload).
30 //!
31 //! Serves several purposes:
32 //! - implements convenient and type-safe wrapper on top of memory pools
33 //! - combines two related pools (packet pool and buffer pool) in one class
34 //! - detaches pipeline logic from memory management interface, so that it can
35 //! change independently without affecting every pipeline element
36 class PacketFactory : public core::NonCopyable<> {
37 public:
38  //! Initialize with default pools.
39  //! @p buffer_size defines number of bytes in packet buffer.
40  PacketFactory(core::IArena& arena, size_t buffer_size);
41 
42  //! Initialize with custom pools.
43  //! @p packet_pool is a pool of packet::Packet objects.
44  //! @p buffer_pool is a pool of core::Buffer objects.
45  PacketFactory(core::IPool& packet_pool, core::IPool& buffer_pool);
46 
47  //! Get packet buffer size in bytes.
48  size_t packet_buffer_size() const;
49 
50  //! Allocate packet buffer.
51  //! @remarks
52  //! Returned buffer may be attached to packet using Packet::set_buffer().
54 
55  //! Allocate packet.
57 
58 private:
59  // used if factory is created with default pools
60  core::Optional<core::SlabPool<Packet> > default_packet_pool_;
61  core::Optional<core::SlabPool<core::Buffer> > default_buffer_pool_;
62 
63  core::IPool* packet_pool_;
64  core::IPool* buffer_pool_;
65  size_t buffer_size_;
66 };
67 
68 } // namespace packet
69 } // namespace roc
70 
71 #endif // ROC_PACKET_PACKET_FACTORY_H_
Buffer.
Memory arena interface.
Definition: iarena.h:23
Memory pool interface.
Definition: ipool.h:23
Base class for non-copyable objects.
Definition: noncopyable.h:23
Optionally constructed object.
Definition: optional.h:25
PacketPtr new_packet()
Allocate packet.
PacketFactory(core::IArena &arena, size_t buffer_size)
Initialize with default pools. buffer_size defines number of bytes in packet buffer.
size_t packet_buffer_size() const
Get packet buffer size in bytes.
core::BufferPtr new_packet_buffer()
Allocate packet buffer.
PacketFactory(core::IPool &packet_pool, core::IPool &buffer_pool)
Initialize with custom pools. packet_pool is a pool of packet::Packet objects. buffer_pool is a pool ...
Memory arena interface.
Memory pool interface.
Root namespace.
Non-copyable object.
Optionally constructed object.
Packet.
Memory pool.