storage.cc
Go to the documentation of this file.
1 
37 #include <algorithm>
38 
39 #include <utility/Exception.hh>
40 
42 
43 namespace multisense{
44 namespace legacy{
45 
46 namespace {
47  constexpr size_t NUM_ALLOCATION_RETRIES = 5;
48 }
49 
51  m_config(config)
52 {
53 
54  for (size_t i = 0 ; i < config.num_small_buffers ; ++i)
55  {
56  for (size_t t = 0; t < NUM_ALLOCATION_RETRIES ; ++t)
57  {
58  try
59  {
60  auto small_buffer = std::make_shared<std::vector<uint8_t>>();
61  small_buffer->reserve(config.small_buffer_size);
62  m_small_buffers.emplace_back(std::move(small_buffer));
63  break;
64  }
65  catch(const std::exception &e)
66  {
67  (void) e;
68  CRL_DEBUG("Failed to allocate small buffer. Retrying\n");
69  }
70  }
71  }
72 
73  for (size_t i = 0 ; i < config.num_large_buffers ; ++i)
74  {
75  for (size_t t = 0; t < NUM_ALLOCATION_RETRIES ; ++t)
76  {
77  try
78  {
79  auto large_buffer = std::make_shared<std::vector<uint8_t>>();
80  large_buffer->reserve(config.large_buffer_size);
81  m_large_buffers.emplace_back(std::move(large_buffer));
82  break;
83  }
84  catch(const std::exception &e)
85  {
86  (void) e;
87  CRL_DEBUG("Failed to allocate large buffer\n");
88  }
89  }
90  }
91 
92  if (m_small_buffers.size() != config.num_small_buffers || m_large_buffers.size() != config.num_large_buffers)
93  {
94  CRL_EXCEPTION("Failed to allocate buffers");
95  }
96 }
97 
98 std::shared_ptr<std::vector<uint8_t>> BufferPool::get_buffer(size_t target_size)
99 {
100  if (target_size <= m_config.small_buffer_size)
101  {
102  const auto &small_buffer = std::find_if(std::begin(m_small_buffers), std::end(m_small_buffers),
103  [](const auto &small_buffer)
104  {
105  return small_buffer.use_count() == 1;
106  });
107 
108  if (small_buffer != std::end(m_small_buffers))
109  {
110  (*small_buffer)->resize(target_size, 0);
111  return *small_buffer;
112  }
113  }
114  else if (target_size <= m_config.large_buffer_size)
115  {
116  const auto &large_buffer = std::find_if(std::begin(m_large_buffers), std::end(m_large_buffers),
117  [](const auto &large_buffer)
118  {
119  return large_buffer.use_count() == 1;
120  });
121 
122  if (large_buffer != std::end(m_large_buffers))
123  {
124  (*large_buffer)->resize(target_size, 0);
125  return *large_buffer;
126  }
127  }
128 
129  return nullptr;
130 }
131 
132 }
133 }
multisense::legacy::BufferPool::m_large_buffers
std::vector< std::shared_ptr< std::vector< uint8_t > > > m_large_buffers
The collection of large buffers.
Definition: LibMultiSense/include/details/legacy/storage.hh:106
multisense::legacy::BufferPoolConfig::small_buffer_size
size_t small_buffer_size
Definition: LibMultiSense/include/details/legacy/storage.hh:49
CRL_DEBUG
#define CRL_DEBUG(fmt,...)
Definition: Exception.hh:71
Exception.hh
multisense::legacy::BufferPool::get_buffer
std::shared_ptr< std::vector< uint8_t > > get_buffer(size_t target_size)
Get a buffer which will contain at least target_size bytes of storage.
Definition: storage.cc:98
multisense::legacy::BufferPool::m_small_buffers
std::vector< std::shared_ptr< std::vector< uint8_t > > > m_small_buffers
The collection of small buffers.
Definition: LibMultiSense/include/details/legacy/storage.hh:101
multisense::legacy::BufferPool::m_config
BufferPoolConfig m_config
The configured numbers and sizes of our internal buffers.
Definition: LibMultiSense/include/details/legacy/storage.hh:96
CRL_EXCEPTION
#define CRL_EXCEPTION(fmt,...)
Definition: Exception.hh:85
multisense::legacy::BufferPoolConfig::large_buffer_size
size_t large_buffer_size
Definition: LibMultiSense/include/details/legacy/storage.hh:51
multisense::legacy::BufferPool::BufferPool
BufferPool(const BufferPoolConfig &config)
Definition: storage.cc:50
multisense::legacy::BufferPoolConfig
Definition: LibMultiSense/include/details/legacy/storage.hh:46
storage.hh
multisense::legacy::BufferPoolConfig::num_large_buffers
size_t num_large_buffers
Definition: LibMultiSense/include/details/legacy/storage.hh:50
multisense
Definition: factory.cc:39
multisense::legacy::BufferPoolConfig::num_small_buffers
size_t num_small_buffers
Definition: LibMultiSense/include/details/legacy/storage.hh:48


multisense_lib
Author(s):
autogenerated on Thu Apr 17 2025 02:49:09