/src/Fast-DDS/src/cpp/rtps/common/SerializedPayload.cpp
Line | Count | Source |
1 | | // Copyright 2024 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 | | /*! |
16 | | * @file SerializedPayload.cpp |
17 | | */ |
18 | | |
19 | | #include <fastdds/rtps/common/SerializedPayload.hpp> |
20 | | |
21 | | namespace eprosima { |
22 | | namespace fastdds { |
23 | | namespace rtps { |
24 | | |
25 | | SerializedPayload_t& SerializedPayload_t::operator = ( |
26 | | SerializedPayload_t&& other) noexcept |
27 | 0 | { |
28 | 0 | if (this == &other) |
29 | 0 | { |
30 | 0 | return *this; |
31 | 0 | } |
32 | | |
33 | 0 | if (payload_owner != nullptr) |
34 | 0 | { |
35 | 0 | bool success = payload_owner->release_payload(*this); |
36 | 0 | static_cast<void>(success); |
37 | 0 | assert(success); |
38 | 0 | payload_owner = nullptr; |
39 | 0 | } |
40 | 0 | else if (data != nullptr) |
41 | 0 | { |
42 | 0 | free(data); |
43 | 0 | } |
44 | |
|
45 | 0 | encapsulation = other.encapsulation; |
46 | 0 | length = other.length; |
47 | 0 | data = other.data; |
48 | 0 | max_size = other.max_size; |
49 | 0 | pos = other.pos; |
50 | 0 | payload_owner = other.payload_owner; |
51 | 0 | is_serialized_key = other.is_serialized_key; |
52 | |
|
53 | 0 | other.encapsulation = CDR_BE; |
54 | 0 | other.length = 0; |
55 | 0 | other.data = nullptr; |
56 | 0 | other.max_size = 0; |
57 | 0 | other.pos = 0; |
58 | 0 | other.payload_owner = nullptr; |
59 | 0 | other.is_serialized_key = false; |
60 | |
|
61 | 0 | return *this; |
62 | 0 | } |
63 | | |
64 | | SerializedPayload_t::~SerializedPayload_t() |
65 | 185k | { |
66 | 185k | if (payload_owner != nullptr) |
67 | 0 | { |
68 | 0 | bool success = payload_owner->release_payload(*this); |
69 | 0 | static_cast<void>(success); |
70 | 0 | assert(success); |
71 | 0 | payload_owner = nullptr; |
72 | 0 | } |
73 | 185k | this->empty(); |
74 | 185k | } |
75 | | |
76 | | bool SerializedPayload_t::operator == ( |
77 | | const SerializedPayload_t& other) const |
78 | 0 | { |
79 | 0 | return ((encapsulation == other.encapsulation) && |
80 | 0 | (is_serialized_key == other.is_serialized_key) && |
81 | 0 | (length == other.length) && |
82 | 0 | (length == 0 || 0 == memcmp(data, other.data, length))); |
83 | 0 | } |
84 | | |
85 | | bool SerializedPayload_t::copy( |
86 | | const SerializedPayload_t* serData, |
87 | | bool with_limit) |
88 | 586 | { |
89 | 586 | length = serData->length; |
90 | | |
91 | 586 | if (serData->length > max_size) |
92 | 0 | { |
93 | 0 | if (with_limit) |
94 | 0 | { |
95 | 0 | return false; |
96 | 0 | } |
97 | 0 | else |
98 | 0 | { |
99 | 0 | this->reserve(serData->length); |
100 | 0 | } |
101 | 0 | } |
102 | 586 | encapsulation = serData->encapsulation; |
103 | 586 | is_serialized_key = serData->is_serialized_key; |
104 | 586 | if (length == 0) |
105 | 586 | { |
106 | 586 | return true; |
107 | 586 | } |
108 | 0 | memcpy(data, serData->data, length); |
109 | 0 | return true; |
110 | 586 | } |
111 | | |
112 | | bool SerializedPayload_t::reserve_fragmented( |
113 | | SerializedPayload_t* serData) |
114 | 0 | { |
115 | 0 | length = serData->length; |
116 | 0 | max_size = serData->length; |
117 | 0 | encapsulation = serData->encapsulation; |
118 | 0 | is_serialized_key = serData->is_serialized_key; |
119 | 0 | data = (octet*)calloc(length, sizeof(octet)); |
120 | 0 | return true; |
121 | 0 | } |
122 | | |
123 | | void SerializedPayload_t::empty() |
124 | 185k | { |
125 | 185k | assert(payload_owner == nullptr); |
126 | | |
127 | 185k | length = 0; |
128 | 185k | encapsulation = CDR_BE; |
129 | 185k | max_size = 0; |
130 | 185k | if (data != nullptr) |
131 | 23.1k | { |
132 | 23.1k | free(data); |
133 | 23.1k | } |
134 | 185k | data = nullptr; |
135 | 185k | is_serialized_key = false; |
136 | 185k | } |
137 | | |
138 | | void SerializedPayload_t::reserve( |
139 | | uint32_t new_size) |
140 | 42.3k | { |
141 | 42.3k | if (new_size <= this->max_size) |
142 | 16.8k | { |
143 | 16.8k | return; |
144 | 16.8k | } |
145 | 25.4k | if (data == nullptr) |
146 | 23.1k | { |
147 | 23.1k | data = (octet*)calloc(new_size, sizeof(octet)); |
148 | 23.1k | if (!data) |
149 | 0 | { |
150 | 0 | throw std::bad_alloc(); |
151 | 0 | } |
152 | 23.1k | } |
153 | 2.24k | else |
154 | 2.24k | { |
155 | 2.24k | void* old_data = data; |
156 | 2.24k | data = (octet*)realloc(data, new_size); |
157 | 2.24k | if (!data) |
158 | 0 | { |
159 | 0 | free(old_data); |
160 | 0 | throw std::bad_alloc(); |
161 | 0 | } |
162 | 2.24k | memset(data + max_size, 0, (new_size - max_size) * sizeof(octet)); |
163 | 2.24k | } |
164 | 25.4k | max_size = new_size; |
165 | 25.4k | } |
166 | | |
167 | | } /* namespace rtps */ |
168 | | } /* namespace fastdds */ |
169 | | } /* namespace eprosima */ |