Coverage Report

Created: 2026-05-04 06:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/Fast-CDR/src/cpp/FastBuffer.cpp
Line
Count
Source
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
0
#define BUFFER_START_LENGTH 200
24
25
using namespace eprosima::fastcdr;
26
27
FastBuffer::FastBuffer(
28
        char* const buffer,
29
        const size_t bufferSize)
30
0
    : buffer_(buffer)
31
0
    , size_(bufferSize)
32
0
    , m_internalBuffer(false)
33
0
{
34
0
}
35
36
FastBuffer::~FastBuffer()
37
0
{
38
0
    if (m_internalBuffer && buffer_ != nullptr)
39
0
    {
40
0
        free(buffer_);
41
0
    }
42
0
}
43
44
bool FastBuffer::reserve(
45
        size_t size)
46
0
{
47
0
    if (m_internalBuffer && buffer_ == NULL)
48
0
    {
49
0
        buffer_ = reinterpret_cast<char*>(malloc(size));
50
0
        if (buffer_)
51
0
        {
52
0
            size_ = size;
53
0
            return true;
54
0
        }
55
0
    }
56
0
    return false;
57
0
}
58
59
bool FastBuffer::resize(
60
        size_t min_size_inc)
61
0
{
62
0
    size_t incBufferSize = BUFFER_START_LENGTH;
63
64
0
    if (m_internalBuffer)
65
0
    {
66
0
        if (min_size_inc > BUFFER_START_LENGTH)
67
0
        {
68
0
            incBufferSize = min_size_inc;
69
0
        }
70
71
0
        if (buffer_ == NULL)
72
0
        {
73
0
            size_ = incBufferSize;
74
75
0
            buffer_ = reinterpret_cast<char*>(malloc(size_));
76
77
0
            if (buffer_ != NULL)
78
0
            {
79
0
                return true;
80
0
            }
81
0
        }
82
0
        else
83
0
        {
84
0
            size_ += incBufferSize;
85
86
0
            buffer_ = reinterpret_cast<char*>(realloc(buffer_, size_));
87
88
0
            if (buffer_ != NULL)
89
0
            {
90
0
                return true;
91
0
            }
92
0
        }
93
0
    }
94
95
0
    return false;
96
0
}