LCOV - code coverage report
Current view: top level - src/zerocopy - proxy_data.cc (source / functions) Hit Total Coverage
Test: vlink Lines: 266 281 94.7 %
Date: 2026-07-26 14:05:51 Functions: 34 34 100.0 %
Branches: 157 222 70.7 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Copyright (C) 2026 by Thun Lu. All rights reserved.
       3                 :            :  * Author: Thun Lu <thun.lu@zohomail.cn>
       4                 :            :  * Repo:   https://github.com/thun-res/vlink
       5                 :            :  *  _    __   __      _           __
       6                 :            :  * | |  / /  / /     (_) ____    / /__
       7                 :            :  * | | / /  / /     / / / __ \  / //_/
       8                 :            :  * | |/ /  / /___  / / / / / / / ,<
       9                 :            :  * |___/  /_____/ /_/ /_/ /_/ /_/|_|
      10                 :            :  *
      11                 :            :  * Licensed under the Apache License, Version 2.0 (the "License");
      12                 :            :  * you may not use this file except in compliance with the License.
      13                 :            :  * You may obtain a copy of the License at
      14                 :            :  *
      15                 :            :  *     http://www.apache.org/licenses/LICENSE-2.0
      16                 :            :  *
      17                 :            :  * Unless required by applicable law or agreed to in writing, software
      18                 :            :  * distributed under the License is distributed on an "AS IS" BASIS,
      19                 :            :  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
      20                 :            :  * See the License for the specific language governing permissions and
      21                 :            :  * limitations under the License.
      22                 :            :  */
      23                 :            : 
      24                 :            : #include "./zerocopy/proxy_data.h"
      25                 :            : 
      26                 :            : #include <cstdint>
      27                 :            : #include <limits>
      28                 :            : #include <string_view>
      29                 :            : 
      30                 :            : #include "./zerocopy/header.h"
      31                 :            : 
      32                 :            : namespace vlink {
      33                 :            : 
      34                 :            : namespace zerocopy {
      35                 :            : 
      36                 :            : // ProxyData
      37                 :         73 : ProxyData::ProxyData() noexcept {
      38                 :            : #if defined(__arm__) || defined(__x86__) || defined(__i386__)
      39                 :            : #ifndef __ANDROID__
      40                 :            : #warning "[ProxyData] No support for 32-bit architecture."
      41                 :            : #endif
      42                 :            : #else
      43                 :            :   static_assert(sizeof(ProxyData) == 80, "Sizeof must be 80 bytes.");
      44                 :            : #endif
      45                 :         73 : }
      46                 :            : 
      47                 :         76 : ProxyData::~ProxyData() noexcept {
      48   [ +  +  +  -  :         76 :   if (is_owner_ && data_ && size_ != 0) {
                   +  - ]
      49                 :         45 :     Bytes::bytes_free(data_, size_);
      50                 :            :   }
      51                 :         76 : }
      52                 :            : 
      53                 :          2 : ProxyData::ProxyData(const ProxyData& target) noexcept { deep_copy(target); }
      54                 :            : 
      55                 :          1 : ProxyData::ProxyData(ProxyData&& target) noexcept { move_copy(target); }
      56                 :            : 
      57                 :          2 : ProxyData& ProxyData::operator=(const ProxyData& target) noexcept {
      58         [ +  + ]:          2 :   if VUNLIKELY (this == &target) {
      59                 :          1 :     return *this;
      60                 :            :   }
      61                 :            : 
      62                 :          1 :   deep_copy(target);
      63                 :            : 
      64                 :          1 :   return *this;
      65                 :            : }
      66                 :            : 
      67                 :          2 : ProxyData& ProxyData::operator=(ProxyData&& target) noexcept {
      68         [ +  + ]:          2 :   if VUNLIKELY (this == &target) {
      69                 :          1 :     return *this;
      70                 :            :   }
      71                 :            : 
      72                 :          1 :   move_copy(target);
      73                 :            : 
      74                 :          1 :   return *this;
      75                 :            : }
      76                 :            : 
      77                 :         18 : bool ProxyData::operator<<(const Bytes& bytes) noexcept {
      78                 :            :   static constexpr size_t kMagicNumberBeginSize = sizeof(kMagicNumberBegin);
      79                 :            :   static constexpr size_t kVersionSize = sizeof(kWireVersion);
      80                 :            :   // static constexpr size_t kMagicNumberEndSize = sizeof(kMagicNumberEnd);
      81                 :            : 
      82         [ +  + ]:         18 :   if VUNLIKELY (bytes.empty()) {
      83                 :          1 :     return false;
      84                 :            :   }
      85                 :            : 
      86         [ +  + ]:         17 :   if VUNLIKELY (!check_valid(bytes)) {
      87                 :          1 :     return false;
      88                 :            :   }
      89                 :            : 
      90                 :         16 :   uint32_t wire_version = 0;
      91                 :         16 :   std::memcpy(&wire_version, bytes.data() + kMagicNumberBeginSize, kVersionSize);
      92                 :            : 
      93         [ -  + ]:         16 :   if VUNLIKELY (version_major(wire_version) != version_major(kWireVersion)) {
      94                 :          0 :     return false;
      95                 :            :   }
      96                 :            : 
      97   [ +  +  +  -  :         16 :   if (is_owner_ && data_ && size_ != 0) {
                   +  - ]
      98                 :          1 :     Bytes::bytes_free(data_, size_);
      99                 :            :   }
     100                 :            : 
     101                 :            : #if defined(__GNUC__) && !defined(__clang__)
     102                 :            : #pragma GCC diagnostic push
     103                 :            : #pragma GCC diagnostic ignored "-Wclass-memaccess"
     104                 :            : #if __GNUC__ >= 11
     105                 :            : #pragma GCC diagnostic ignored "-Wstringop-overread"
     106                 :            : #endif
     107                 :            : #endif
     108                 :            : 
     109                 :         16 :   auto* target_ptr = reinterpret_cast<uint8_t*>(this);
     110                 :            : 
     111                 :         16 :   std::memcpy(target_ptr, bytes.data() + kMagicNumberBeginSize + kVersionSize, sizeof(ProxyData));
     112                 :            : 
     113                 :            : #if defined(__GNUC__) && !defined(__clang__)
     114                 :            : #pragma GCC diagnostic pop
     115                 :            : #endif
     116                 :            : 
     117                 :         16 :   data_ = const_cast<uint8_t*>(bytes.data() + kMagicNumberBeginSize + kVersionSize + sizeof(ProxyData));
     118                 :         16 :   is_owner_ = false;
     119                 :            : 
     120         [ +  + ]:         16 :   if VUNLIKELY (bytes.size() != get_serialized_size()) {
     121                 :          1 :     clear();
     122                 :          1 :     return false;
     123                 :            :   }
     124                 :            : 
     125         [ +  + ]:         15 :   if VUNLIKELY (static_cast<uint64_t>(data_pos_) + data_size_ != url_pos_) {
     126                 :          1 :     clear();
     127                 :          1 :     return false;
     128                 :            :   }
     129                 :            : 
     130         [ +  + ]:         14 :   if VUNLIKELY (static_cast<uint64_t>(url_pos_) + url_size_ != ser_pos_) {
     131                 :          1 :     clear();
     132                 :          1 :     return false;
     133                 :            :   }
     134                 :            : 
     135         [ +  + ]:         13 :   if VUNLIKELY (static_cast<uint64_t>(ser_pos_) + ser_size_ != hostname_pos_) {
     136                 :          1 :     clear();
     137                 :          1 :     return false;
     138                 :            :   }
     139                 :            : 
     140         [ +  + ]:         12 :   if VUNLIKELY (static_cast<uint64_t>(hostname_pos_) + hostname_size_ != size_) {
     141                 :          1 :     clear();
     142                 :          1 :     return false;
     143                 :            :   }
     144                 :            : 
     145                 :         11 :   return true;
     146                 :            : }
     147                 :            : 
     148                 :         21 : bool ProxyData::operator>>(Bytes& bytes) const noexcept {
     149                 :            :   static constexpr size_t kMagicNumberBeginSize = sizeof(kMagicNumberBegin);
     150                 :            :   static constexpr size_t kVersionSize = sizeof(kWireVersion);
     151                 :            :   static constexpr size_t kMagicNumberEndSize = sizeof(kMagicNumberEnd);
     152                 :            : 
     153   [ +  +  +  +  :         21 :   if (bytes.empty() || bytes.size() != get_serialized_size()) {
                   +  + ]
     154                 :         20 :     bytes = Bytes::create(get_serialized_size());
     155                 :            : 
     156         [ -  + ]:         20 :     if VUNLIKELY (bytes.empty()) {
     157                 :          0 :       return false;
     158                 :            :     }
     159                 :            :   }
     160                 :            : 
     161                 :         21 :   std::memcpy(bytes.data(), &kMagicNumberBegin, kMagicNumberBeginSize);
     162                 :            : 
     163                 :         21 :   std::memcpy(bytes.data() + kMagicNumberBeginSize, &kWireVersion, kVersionSize);
     164                 :            : 
     165                 :            :   // NOLINTNEXTLINE(bugprone-undefined-memory-manipulation)
     166                 :         21 :   std::memcpy(bytes.data() + kMagicNumberBeginSize + kVersionSize, this, sizeof(ProxyData));
     167                 :            : 
     168                 :         21 :   const auto data_offset = reinterpret_cast<const uint8_t*>(&data_) - reinterpret_cast<const uint8_t*>(this);
     169                 :         21 :   const size_t data_pointer_size = sizeof(data_);
     170                 :         21 :   std::memset(bytes.data() + kMagicNumberBeginSize + kVersionSize + data_offset, 0, data_pointer_size);
     171                 :            : 
     172   [ +  -  +  -  :         21 :   if VLIKELY (data_ != nullptr && size_ != 0) {
                   +  - ]
     173                 :         21 :     std::memcpy(bytes.data() + kMagicNumberBeginSize + kVersionSize + sizeof(ProxyData), data_, size_);
     174                 :            :   }
     175                 :            : 
     176                 :         21 :   std::memcpy(bytes.data() + kMagicNumberBeginSize + kVersionSize + sizeof(ProxyData) + size_, &kMagicNumberEnd,
     177                 :            :               kMagicNumberEndSize);
     178                 :            : 
     179                 :         21 :   return true;
     180                 :            : }
     181                 :            : 
     182                 :          7 : uint32_t ProxyData::control_id() const noexcept { return control_id_; }
     183                 :            : 
     184                 :          5 : uint32_t ProxyData::mode() const noexcept { return mode_; }
     185                 :            : 
     186                 :          5 : int64_t ProxyData::timestamp() const noexcept { return timestamp_; }
     187                 :            : 
     188                 :          5 : int64_t ProxyData::seq() const noexcept { return seq_; }
     189                 :            : 
     190                 :         20 : uint32_t ProxyData::schema() const noexcept { return schema_; }
     191                 :            : 
     192                 :         10 : Bytes ProxyData::raw() const noexcept {
     193   [ +  +  -  +  :         10 :   if VUNLIKELY (!data_ || size_ == 0 || data_size_ == 0) {
          +  +  -  +  +  
                      + ]
     194                 :          1 :     return Bytes();
     195                 :            :   }
     196                 :            : 
     197                 :          9 :   return Bytes::shallow_copy(data_ + data_pos_, data_size_);
     198                 :            : }
     199                 :            : 
     200                 :         26 : std::string_view ProxyData::url() const noexcept {
     201   [ +  +  -  +  :         26 :   if VUNLIKELY (!data_ || size_ == 0 || url_size_ == 0) {
          +  +  -  +  +  
                      + ]
     202                 :          2 :     return std::string_view();
     203                 :            :   }
     204                 :            : 
     205                 :         24 :   return std::string_view(reinterpret_cast<char*>(data_) + url_pos_, url_size_);
     206                 :            : }
     207                 :            : 
     208                 :         12 : std::string_view ProxyData::ser() const noexcept {
     209   [ +  +  -  +  :         12 :   if VUNLIKELY (!data_ || size_ == 0 || ser_size_ == 0) {
          +  +  -  +  +  
                      + ]
     210                 :          2 :     return std::string_view();
     211                 :            :   }
     212                 :            : 
     213                 :         10 :   return std::string_view(reinterpret_cast<char*>(data_) + ser_pos_, ser_size_);
     214                 :            : }
     215                 :            : 
     216                 :         18 : std::string_view ProxyData::hostname() const noexcept {
     217   [ +  +  -  +  :         18 :   if VUNLIKELY (!data_ || size_ == 0 || hostname_size_ == 0) {
          +  +  +  +  +  
                      + ]
     218                 :          3 :     return std::string_view();
     219                 :            :   }
     220                 :            : 
     221                 :         15 :   return std::string_view(reinterpret_cast<char*>(data_) + hostname_pos_, hostname_size_);
     222                 :            : }
     223                 :            : 
     224                 :          5 : void ProxyData::set_control_id(uint32_t control_id) noexcept { control_id_ = control_id; }
     225                 :            : 
     226                 :          2 : void ProxyData::set_mode(uint32_t mode) noexcept { mode_ = mode; }
     227                 :            : 
     228                 :          3 : void ProxyData::set_timestamp(int64_t timestamp) noexcept { timestamp_ = timestamp; }
     229                 :            : 
     230                 :          2 : void ProxyData::set_seq(int64_t seq) noexcept { seq_ = seq; }
     231                 :            : 
     232                 :          1 : void ProxyData::set_schema(uint32_t schema) noexcept { schema_ = schema; }
     233                 :            : 
     234                 :         24 : bool ProxyData::check_valid(const Bytes& bytes) noexcept {
     235                 :            :   static constexpr size_t kMagicNumberBeginSize = sizeof(kMagicNumberBegin);
     236                 :            :   static constexpr size_t kVersionSize = sizeof(kWireVersion);
     237                 :            :   static constexpr size_t kMagicNumberEndSize = sizeof(kMagicNumberEnd);
     238                 :            : 
     239         [ +  + ]:         24 :   if VUNLIKELY (bytes.size() < kMagicNumberBeginSize + kVersionSize + sizeof(ProxyData) + kMagicNumberEndSize) {
     240                 :          1 :     return false;
     241                 :            :   }
     242                 :            : 
     243                 :         23 :   uint32_t check_magic = 0;
     244                 :            : 
     245                 :         23 :   std::memcpy(&check_magic, bytes.begin(), kMagicNumberBeginSize);
     246                 :            : 
     247         [ +  + ]:         23 :   if VUNLIKELY (check_magic != kMagicNumberBegin) {
     248                 :          1 :     return false;
     249                 :            :   }
     250                 :            : 
     251                 :         22 :   uint32_t wire_version = 0;
     252                 :         22 :   std::memcpy(&wire_version, bytes.data() + kMagicNumberBeginSize, kVersionSize);
     253                 :            : 
     254         [ +  + ]:         22 :   if VUNLIKELY (version_major(wire_version) != version_major(kWireVersion)) {
     255                 :          2 :     return false;
     256                 :            :   }
     257                 :            : 
     258                 :         20 :   std::memcpy(&check_magic, bytes.end() - kMagicNumberEndSize, kMagicNumberEndSize);
     259                 :            : 
     260         [ +  + ]:         20 :   if VUNLIKELY (check_magic != kMagicNumberEnd) {
     261                 :          1 :     return false;
     262                 :            :   }
     263                 :            : 
     264                 :         19 :   return true;
     265                 :            : }
     266                 :            : 
     267                 :         42 : size_t ProxyData::get_serialized_size() const noexcept {
     268                 :            :   static constexpr size_t kMagicNumberBeginSize = sizeof(kMagicNumberBegin);
     269                 :            :   static constexpr size_t kVersionSize = sizeof(kWireVersion);
     270                 :            :   static constexpr size_t kMagicNumberEndSize = sizeof(kMagicNumberEnd);
     271                 :            : 
     272                 :         42 :   return kMagicNumberBeginSize + kVersionSize + sizeof(ProxyData) + size_ + kMagicNumberEndSize;
     273                 :            : }
     274                 :            : 
     275                 :         26 : bool ProxyData::is_valid() const noexcept {
     276         [ -  + ]:         26 :   if VUNLIKELY (static_cast<uint64_t>(data_pos_) + data_size_ != url_pos_) {
     277                 :          0 :     return false;
     278                 :            :   }
     279                 :            : 
     280         [ -  + ]:         26 :   if VUNLIKELY (static_cast<uint64_t>(url_pos_) + url_size_ != ser_pos_) {
     281                 :          0 :     return false;
     282                 :            :   }
     283                 :            : 
     284         [ -  + ]:         26 :   if VUNLIKELY (static_cast<uint64_t>(ser_pos_) + ser_size_ != hostname_pos_) {
     285                 :          0 :     return false;
     286                 :            :   }
     287                 :            : 
     288         [ -  + ]:         26 :   if VUNLIKELY (static_cast<uint64_t>(hostname_pos_) + hostname_size_ != size_) {
     289                 :          0 :     return false;
     290                 :            :   }
     291                 :            : 
     292   [ +  +  +  - ]:         26 :   return data_ != nullptr && size_ != 0;
     293                 :            : }
     294                 :            : 
     295                 :         15 : bool ProxyData::shallow_copy(const ProxyData& target) noexcept {
     296         [ +  + ]:         15 :   if VUNLIKELY (this == &target) {
     297                 :          2 :     return false;
     298                 :            :   }
     299                 :            : 
     300   [ +  +  +  -  :         13 :   if (is_owner_ && data_ && size_ != 0) {
                   +  - ]
     301                 :          3 :     const auto current = reinterpret_cast<uintptr_t>(data_);
     302                 :          3 :     const auto source = reinterpret_cast<uintptr_t>(target.data_);
     303                 :            : 
     304   [ +  +  +  -  :          3 :     if VUNLIKELY (source >= current && source - current < size_) {
                   +  + ]
     305                 :          1 :       return false;
     306                 :            :     }
     307                 :            : 
     308                 :          2 :     Bytes::bytes_free(data_, size_);
     309                 :            :   }
     310                 :            : 
     311                 :         12 :   control_id_ = target.control_id_;
     312                 :         12 :   mode_ = target.mode_;
     313                 :         12 :   timestamp_ = target.timestamp_;
     314                 :         12 :   seq_ = target.seq_;
     315                 :         12 :   schema_ = target.schema_;
     316                 :            : 
     317                 :         12 :   data_pos_ = target.data_pos_;
     318                 :         12 :   data_size_ = target.data_size_;
     319                 :            : 
     320                 :         12 :   url_pos_ = target.url_pos_;
     321                 :         12 :   url_size_ = target.url_size_;
     322                 :            : 
     323                 :         12 :   ser_pos_ = target.ser_pos_;
     324                 :         12 :   ser_size_ = target.ser_size_;
     325                 :            : 
     326                 :         12 :   hostname_pos_ = target.hostname_pos_;
     327                 :         12 :   hostname_size_ = target.hostname_size_;
     328                 :            : 
     329                 :         12 :   reserved_buf_ = target.reserved_buf_;
     330                 :         12 :   reserved_buf2_ = target.reserved_buf2_;
     331                 :            : 
     332                 :         12 :   size_ = target.size_;
     333                 :         12 :   data_ = target.data_;
     334                 :            : 
     335                 :         12 :   is_owner_ = false;
     336                 :            : 
     337                 :         12 :   return true;
     338                 :            : }
     339                 :            : 
     340                 :          8 : bool ProxyData::deep_copy(const ProxyData& target) noexcept {
     341   [ +  +  +  -  :          8 :   if VLIKELY (data_ && is_owner_ && target.data_ && size_ != 0 && size_ == target.size_) {
          +  +  +  -  +  
          +  +  -  +  +  
             +  -  +  + ]
     342                 :          3 :     const auto current = reinterpret_cast<uintptr_t>(data_);
     343                 :          3 :     const auto source = reinterpret_cast<uintptr_t>(target.data_);
     344                 :            : 
     345   [ +  +  +  -  :          3 :     if VUNLIKELY (source >= current && source - current < size_) {
                   +  + ]
     346                 :          2 :       return false;
     347                 :            :     }
     348                 :            : 
     349                 :          1 :     control_id_ = target.control_id_;
     350                 :          1 :     mode_ = target.mode_;
     351                 :          1 :     timestamp_ = target.timestamp_;
     352                 :          1 :     seq_ = target.seq_;
     353                 :          1 :     schema_ = target.schema_;
     354                 :            : 
     355                 :          1 :     data_pos_ = target.data_pos_;
     356                 :          1 :     data_size_ = target.data_size_;
     357                 :            : 
     358                 :          1 :     url_pos_ = target.url_pos_;
     359                 :          1 :     url_size_ = target.url_size_;
     360                 :            : 
     361                 :          1 :     ser_pos_ = target.ser_pos_;
     362                 :          1 :     ser_size_ = target.ser_size_;
     363                 :            : 
     364                 :          1 :     hostname_pos_ = target.hostname_pos_;
     365                 :          1 :     hostname_size_ = target.hostname_size_;
     366                 :            : 
     367                 :          1 :     reserved_buf_ = target.reserved_buf_;
     368                 :          1 :     reserved_buf2_ = target.reserved_buf2_;
     369                 :            : 
     370                 :          1 :     std::memcpy(data_, target.data_, size_);
     371                 :            : 
     372                 :          1 :     return true;
     373                 :            :   }
     374                 :            : 
     375         [ -  + ]:          5 :   if VUNLIKELY (!shallow_copy(target)) {
     376                 :          0 :     return false;
     377                 :            :   }
     378                 :            : 
     379   [ +  -  +  - ]:          5 :   if (data_ && size_ != 0) {
     380                 :          5 :     auto* target_data = data_;
     381                 :          5 :     data_ = Bytes::bytes_malloc(size_);
     382                 :            : 
     383         [ -  + ]:          5 :     if VUNLIKELY (!data_) {
     384                 :          0 :       clear();
     385                 :          0 :       return false;
     386                 :            :     }
     387                 :            : 
     388                 :          5 :     std::memcpy(data_, target_data, size_);
     389                 :          5 :     is_owner_ = true;
     390                 :            :   }
     391                 :            : 
     392                 :          5 :   return true;
     393                 :            : }
     394                 :            : 
     395                 :          5 : bool ProxyData::move_copy(ProxyData& target) noexcept {
     396         [ +  + ]:          5 :   if VUNLIKELY (!shallow_copy(target)) {
     397                 :          1 :     return false;
     398                 :            :   }
     399                 :            : 
     400                 :          4 :   is_owner_ = target.is_owner_;
     401                 :            : 
     402                 :          4 :   target.control_id_ = 0;
     403                 :          4 :   target.mode_ = 0;
     404                 :          4 :   target.timestamp_ = 0;
     405                 :          4 :   target.seq_ = 0;
     406                 :          4 :   target.schema_ = 0;
     407                 :            : 
     408                 :          4 :   target.data_pos_ = 0;
     409                 :          4 :   target.data_size_ = 0;
     410                 :            : 
     411                 :          4 :   target.url_pos_ = 0;
     412                 :          4 :   target.url_size_ = 0;
     413                 :            : 
     414                 :          4 :   target.ser_pos_ = 0;
     415                 :          4 :   target.ser_size_ = 0;
     416                 :            : 
     417                 :          4 :   target.hostname_pos_ = 0;
     418                 :          4 :   target.hostname_size_ = 0;
     419                 :            : 
     420                 :          4 :   target.reserved_buf_ = 0;
     421                 :          4 :   target.reserved_buf2_ = 0;
     422                 :            : 
     423                 :          4 :   target.size_ = 0;
     424                 :          4 :   target.data_ = nullptr;
     425                 :          4 :   target.is_owner_ = false;
     426                 :            : 
     427                 :          4 :   return true;
     428                 :            : }
     429                 :            : 
     430                 :         49 : void ProxyData::create(const Bytes& raw, std::string_view url, std::string_view ser, uint32_t schema,
     431                 :            :                        std::string_view hostname) noexcept {
     432                 :            :   static constexpr uint64_t kMax = std::numeric_limits<uint32_t>::max();
     433                 :            : 
     434   [ +  +  -  +  :         49 :   if VUNLIKELY (raw.size() > kMax || url.size() > kMax || ser.size() > kMax || hostname.size() > kMax) {
          +  +  -  +  +  
             +  -  +  +  
                      + ]
     435                 :            :     clear();  // LCOV_EXCL_LINE GCOVR_EXCL_LINE
     436                 :            :     return;   // LCOV_EXCL_LINE GCOVR_EXCL_LINE
     437                 :            :   }
     438                 :            : 
     439                 :         48 :   const uint64_t total = static_cast<uint64_t>(raw.size()) + url.size() + ser.size() + hostname.size();
     440                 :            : 
     441         [ -  + ]:         48 :   if VUNLIKELY (total > kMax) {
     442                 :            :     clear();  // LCOV_EXCL_LINE GCOVR_EXCL_LINE
     443                 :            :     return;   // LCOV_EXCL_LINE GCOVR_EXCL_LINE
     444                 :            :   }
     445                 :            : 
     446                 :         48 :   bool aliases_owned = false;
     447                 :            : 
     448   [ +  +  +  -  :         48 :   if (is_owner_ && data_ && size_ != 0) {
                   +  - ]
     449                 :          1 :     const auto current = reinterpret_cast<uintptr_t>(data_);
     450                 :          1 :     const auto raw_source = reinterpret_cast<uintptr_t>(raw.data());
     451                 :          1 :     const auto url_source = reinterpret_cast<uintptr_t>(url.data());
     452                 :          1 :     const auto ser_source = reinterpret_cast<uintptr_t>(ser.data());
     453                 :          1 :     const auto hostname_source = reinterpret_cast<uintptr_t>(hostname.data());
     454                 :            : 
     455   [ +  -  -  + ]:          2 :     aliases_owned = (!raw.empty() && raw_source >= current && raw_source - current < size_) ||
     456   [ #  #  #  #  :          0 :                     (!url.empty() && url_source >= current && url_source - current < size_) ||
                   #  # ]
     457   [ +  -  -  -  :          2 :                     (!ser.empty() && ser_source >= current && ser_source - current < size_) ||
             -  -  -  - ]
     458   [ #  #  #  #  :          0 :                     (!hostname.empty() && hostname_source >= current && hostname_source - current < size_);
                   #  # ]
     459                 :            : 
     460         [ -  + ]:          1 :     if VLIKELY (!aliases_owned) {
     461                 :          0 :       Bytes::bytes_free(data_, size_);
     462                 :          0 :       data_ = nullptr;
     463                 :          0 :       size_ = 0;
     464                 :          0 :       is_owner_ = false;
     465                 :            :     }
     466                 :            :   }
     467                 :            : 
     468                 :         48 :   const auto data_size = static_cast<uint32_t>(raw.size());
     469                 :         48 :   const uint32_t url_pos = data_size;
     470                 :         48 :   const auto url_size = static_cast<uint32_t>(url.size());
     471                 :         48 :   const uint32_t ser_pos = url_pos + url_size;
     472                 :         48 :   const auto ser_size = static_cast<uint32_t>(ser.size());
     473                 :         48 :   const uint32_t hostname_pos = ser_pos + ser_size;
     474                 :         48 :   const auto hostname_size = static_cast<uint32_t>(hostname.size());
     475                 :            : 
     476                 :         48 :   uint8_t* new_data = nullptr;
     477         [ +  - ]:         48 :   if VLIKELY (total != 0) {
     478                 :         48 :     new_data = Bytes::bytes_malloc(total);
     479                 :            : 
     480         [ -  + ]:         48 :     if VUNLIKELY (!new_data) {
     481                 :            :       clear();  // LCOV_EXCL_LINE GCOVR_EXCL_LINE
     482                 :            :       return;   // LCOV_EXCL_LINE GCOVR_EXCL_LINE
     483                 :            :     }
     484                 :            : 
     485         [ +  + ]:         48 :     if VLIKELY (data_size != 0) {
     486                 :         47 :       std::memcpy(new_data, raw.data(), data_size);
     487                 :            :     }
     488                 :            : 
     489         [ +  + ]:         48 :     if (url_size != 0) {
     490                 :         47 :       std::memcpy(new_data + url_pos, url.data(), url_size);
     491                 :            :     }
     492                 :            : 
     493         [ +  + ]:         48 :     if (ser_size != 0) {
     494                 :         47 :       std::memcpy(new_data + ser_pos, ser.data(), ser_size);
     495                 :            :     }
     496                 :            : 
     497         [ +  + ]:         48 :     if (hostname_size != 0) {
     498                 :         21 :       std::memcpy(new_data + hostname_pos, hostname.data(), hostname_size);
     499                 :            :     }
     500                 :            :   }
     501                 :            : 
     502         [ +  + ]:         48 :   if VUNLIKELY (aliases_owned) {
     503                 :          1 :     Bytes::bytes_free(data_, size_);
     504                 :            :   }
     505                 :            : 
     506                 :         48 :   schema_ = schema;
     507                 :            : 
     508                 :         48 :   data_pos_ = 0;
     509                 :         48 :   data_size_ = data_size;
     510                 :            : 
     511                 :         48 :   url_pos_ = url_pos;
     512                 :         48 :   url_size_ = url_size;
     513                 :            : 
     514                 :         48 :   ser_pos_ = ser_pos;
     515                 :         48 :   ser_size_ = ser_size;
     516                 :            : 
     517                 :         48 :   hostname_pos_ = hostname_pos;
     518                 :         48 :   hostname_size_ = hostname_size;
     519                 :            : 
     520                 :         48 :   size_ = total;
     521                 :         48 :   data_ = new_data;
     522                 :         48 :   is_owner_ = new_data != nullptr;
     523                 :            : }
     524                 :            : 
     525                 :          9 : void ProxyData::clear() noexcept {
     526   [ +  +  +  -  :          9 :   if (is_owner_ && data_ && size_ != 0) {
                   +  - ]
     527                 :          4 :     Bytes::bytes_free(data_, size_);
     528                 :            :   }
     529                 :            : 
     530                 :          9 :   control_id_ = 0;
     531                 :          9 :   mode_ = 0;
     532                 :          9 :   timestamp_ = 0;
     533                 :          9 :   seq_ = 0;
     534                 :          9 :   schema_ = 0;
     535                 :            : 
     536                 :          9 :   data_pos_ = 0;
     537                 :          9 :   data_size_ = 0;
     538                 :            : 
     539                 :          9 :   url_pos_ = 0;
     540                 :          9 :   url_size_ = 0;
     541                 :            : 
     542                 :          9 :   ser_pos_ = 0;
     543                 :          9 :   ser_size_ = 0;
     544                 :            : 
     545                 :          9 :   hostname_pos_ = 0;
     546                 :          9 :   hostname_size_ = 0;
     547                 :            : 
     548                 :          9 :   reserved_buf_ = 0;
     549                 :          9 :   reserved_buf2_ = 0;
     550                 :            : 
     551                 :          9 :   size_ = 0;
     552                 :          9 :   data_ = nullptr;
     553                 :          9 :   is_owner_ = false;
     554                 :          9 : }
     555                 :            : 
     556                 :          7 : size_t ProxyData::size() const noexcept { return size_; }
     557                 :            : 
     558                 :         24 : bool ProxyData::is_owner() const noexcept { return is_owner_; }
     559                 :            : 
     560                 :          7 : uint8_t& ProxyData::get_reserved() noexcept { return reserved_buf_; }
     561                 :            : 
     562                 :          7 : uint16_t& ProxyData::get_reserved2() noexcept { return reserved_buf2_; }
     563                 :            : 
     564                 :            : }  // namespace zerocopy
     565                 :            : 
     566                 :            : }  // namespace vlink

Generated by: LCOV version 1.14