FastBuffer.cpp
Go to the documentation of this file.
1 // Copyright 2016 Proyectos y Sistemas de Mantenimiento SL (eProsima).
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <fastcdr/FastBuffer.h>
16 
17 #if !__APPLE__ && !__FreeBSD__ && !__VXWORKS__
18 #include <malloc.h>
19 #else
20 #include <stdlib.h>
21 #endif // if !__APPLE__ && !__FreeBSD__ && !__VXWORKS__
22 
23 #define BUFFER_START_LENGTH 200
24 
25 using namespace eprosima::fastcdr;
26 
28  : m_buffer(nullptr)
29  , m_bufferSize(0)
30  , m_internalBuffer(true)
31 {
32 }
33 
35  char* const buffer,
36  const size_t bufferSize)
37  : m_buffer(buffer)
38  , m_bufferSize(bufferSize)
39  , m_internalBuffer(false)
40 {
41 }
42 
44 {
45  if (m_internalBuffer && m_buffer != nullptr)
46  {
47  free(m_buffer);
48  }
49 }
50 
52  size_t size)
53 {
54  if (m_internalBuffer && m_buffer == NULL)
55  {
56  m_buffer = reinterpret_cast<char*>(malloc(size));
57  if (m_buffer)
58  {
60  return true;
61  }
62  }
63  return false;
64 }
65 
67  size_t minSizeInc)
68 {
69  size_t incBufferSize = BUFFER_START_LENGTH;
70 
71  if (m_internalBuffer)
72  {
73  if (minSizeInc > BUFFER_START_LENGTH)
74  {
75  incBufferSize = minSizeInc;
76  }
77 
78  if (m_buffer == NULL)
79  {
80  m_bufferSize = incBufferSize;
81 
82  m_buffer = reinterpret_cast<char*>(malloc(m_bufferSize));
83 
84  if (m_buffer != NULL)
85  {
86  return true;
87  }
88  }
89  else
90  {
91  m_bufferSize += incBufferSize;
92 
93  m_buffer = reinterpret_cast<char*>(realloc(m_buffer, m_bufferSize));
94 
95  if (m_buffer != NULL)
96  {
97  return true;
98  }
99  }
100  }
101 
102  return false;
103 }
#define nullptr
Definition: backward.hpp:386
bool m_internalBuffer
This variable indicates if the managed buffer is internal or is from the user.
Definition: FastBuffer.h:348
virtual ~FastBuffer()
Default destructor.
Definition: FastBuffer.cpp:43
FastBuffer()
This constructor creates an internal stream and assigns it to the eprosima::fastcdr::FastBuffers obje...
Definition: FastBuffer.cpp:27
size_t m_bufferSize
The total size of the user&#39;s buffer.
Definition: FastBuffer.h:345
#define BUFFER_START_LENGTH
Definition: FastBuffer.cpp:23
char * m_buffer
Pointer to the stream of bytes that contains the serialized data.
Definition: FastBuffer.h:342
bool resize(size_t minSizeInc)
This function resizes the raw buffer. It will call the user&#39;s defined function for this purpose...
Definition: FastBuffer.cpp:66
bool reserve(size_t size)
This function reserves memory for the internal raw buffer. It will only do so if the buffer is not ye...
Definition: FastBuffer.cpp:51
span_constexpr std::size_t size(span< T, Extent > const &spn)
Definition: span.hpp:1485


plotjuggler
Author(s): Davide Faconti
autogenerated on Mon Jun 19 2023 03:01:02