pjmsg_mcap_wrapper
Loading...
Searching...
No Matches
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
25using namespace eprosima::fastcdr;
26
28 char* const buffer,
29 const size_t bufferSize)
30 : buffer_(buffer)
31 , size_(bufferSize)
32 , m_internalBuffer(false)
33{
34}
35
37{
38 if (m_internalBuffer && buffer_ != nullptr)
39 {
40 free(buffer_);
41 }
42}
43
45 size_t size)
46{
47 if (m_internalBuffer && buffer_ == NULL)
48 {
49 buffer_ = reinterpret_cast<char*>(malloc(size));
50 if (buffer_)
51 {
52 size_ = size;
53 return true;
54 }
55 }
56 return false;
57}
58
60 size_t min_size_inc)
61{
62 size_t incBufferSize = BUFFER_START_LENGTH;
63
65 {
66 if (min_size_inc > BUFFER_START_LENGTH)
67 {
68 incBufferSize = min_size_inc;
69 }
70
71 if (buffer_ == NULL)
72 {
73 size_ = incBufferSize;
74
75 buffer_ = reinterpret_cast<char*>(malloc(size_));
76
77 if (buffer_ != NULL)
78 {
79 return true;
80 }
81 }
82 else
83 {
84 size_ += incBufferSize;
85
86 buffer_ = reinterpret_cast<char*>(realloc(buffer_, size_));
87
88 if (buffer_ != NULL)
89 {
90 return true;
91 }
92 }
93 }
94
95 return false;
96}
#define BUFFER_START_LENGTH
size_t size_
The total size of the user's buffer.
Definition FastBuffer.h:360
FastBuffer()=default
This constructor creates an internal stream and assigns it to the eprosima::fastcdr::FastBuffers obje...
bool resize(size_t min_size_inc)
This function resizes the raw buffer. It will call the user's defined function for this purpose.
bool m_internalBuffer
This variable indicates if the managed buffer is internal or is from the user.
Definition FastBuffer.h:363
char * buffer_
Pointer to the stream of bytes that contains the serialized data.
Definition FastBuffer.h:357
virtual ~FastBuffer()
Default destructor.
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...