LCOV - code coverage report
Current view: top level - src/zerocopy - object_array.cc (source / functions) Hit Total Coverage
Test: vlink Lines: 219 249 88.0 %
Date: 2026-07-26 14:05:51 Functions: 36 36 100.0 %
Branches: 150 208 72.1 %

           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/object_array.h"
      25                 :            : 
      26                 :            : #include <cstdint>
      27                 :            : #include <limits>
      28                 :            : 
      29                 :            : namespace vlink {
      30                 :            : 
      31                 :            : namespace zerocopy {
      32                 :            : 
      33                 :            : // Object
      34                 :         45 : ObjectArray::Object::Object() noexcept {
      35                 :            : #if defined(__arm__) || defined(__x86__) || defined(__i386__)
      36                 :            : #ifndef __ANDROID__
      37                 :            : #warning "[ObjectArray::Object] No support for 32-bit architecture."
      38                 :            : #endif
      39                 :            : #else
      40                 :            :   static_assert(sizeof(ObjectArray::Object) == 144, "Sizeof must be 144 bytes.");
      41                 :            : #endif
      42                 :         45 : }
      43                 :            : 
      44                 :            : // ObjectArray
      45                 :         61 : ObjectArray::ObjectArray() noexcept {
      46                 :            : #if defined(__arm__) || defined(__x86__) || defined(__i386__)
      47                 :            : #ifndef __ANDROID__
      48                 :            : #warning "[ObjectArray] No support for 32-bit architecture."
      49                 :            : #endif
      50                 :            : #else
      51                 :            :   static_assert(sizeof(ObjectArray) == 112, "Sizeof must be 112 bytes.");
      52                 :            : #endif
      53                 :         61 : }
      54                 :            : 
      55                 :         63 : ObjectArray::~ObjectArray() noexcept {
      56   [ +  +  +  -  :         63 :   if (is_owner_ && data_ && capacity_ != 0) {
                   +  - ]
      57                 :         38 :     Bytes::bytes_free(data_, capacity_);
      58                 :            :   }
      59                 :         63 : }
      60                 :            : 
      61                 :          1 : ObjectArray::ObjectArray(const ObjectArray& target) noexcept { deep_copy(target); }
      62                 :            : 
      63                 :          1 : ObjectArray::ObjectArray(ObjectArray&& target) noexcept { move_copy(target); }
      64                 :            : 
      65                 :          1 : ObjectArray& ObjectArray::operator=(const ObjectArray& target) noexcept {
      66         [ -  + ]:          1 :   if VUNLIKELY (this == &target) {
      67                 :            :     return *this;  // LCOV_EXCL_LINE GCOVR_EXCL_LINE
      68                 :            :   }
      69                 :            : 
      70                 :          1 :   deep_copy(target);
      71                 :            : 
      72                 :          1 :   return *this;
      73                 :            : }
      74                 :            : 
      75                 :          1 : ObjectArray& ObjectArray::operator=(ObjectArray&& target) noexcept {
      76         [ -  + ]:          1 :   if VUNLIKELY (this == &target) {
      77                 :            :     return *this;  // LCOV_EXCL_LINE GCOVR_EXCL_LINE
      78                 :            :   }
      79                 :            : 
      80                 :          1 :   move_copy(target);
      81                 :            : 
      82                 :          1 :   return *this;
      83                 :            : }
      84                 :            : 
      85                 :         10 : bool ObjectArray::operator<<(const Bytes& bytes) noexcept {
      86                 :            :   static constexpr size_t kMagicNumberBeginSize = sizeof(kMagicNumberBegin);
      87                 :            :   static constexpr size_t kVersionSize = sizeof(kWireVersion);
      88                 :            :   static constexpr size_t kMagicNumberEndSize = sizeof(kMagicNumberEnd);
      89                 :            :   static constexpr size_t kSerializedOverhead =
      90                 :            :       kMagicNumberBeginSize + kVersionSize + sizeof(ObjectArray) + kMagicNumberEndSize;
      91                 :            : 
      92         [ +  + ]:         10 :   if VUNLIKELY (bytes.empty()) {
      93                 :          1 :     return false;
      94                 :            :   }
      95                 :            : 
      96         [ +  + ]:          9 :   if VUNLIKELY (!check_valid(bytes)) {
      97                 :          2 :     return false;
      98                 :            :   }
      99                 :            : 
     100                 :          7 :   uint32_t wire_version = 0;
     101                 :          7 :   std::memcpy(&wire_version, bytes.data() + kMagicNumberBeginSize, kVersionSize);
     102                 :            : 
     103         [ -  + ]:          7 :   if VUNLIKELY (version_major(wire_version) != version_major(kWireVersion)) {
     104                 :            :     return false;  // LCOV_EXCL_LINE GCOVR_EXCL_LINE
     105                 :            :   }
     106                 :            : 
     107   [ +  +  +  -  :          7 :   if (is_owner_ && data_ && capacity_ != 0) {
                   +  - ]
     108                 :          2 :     Bytes::bytes_free(data_, capacity_);
     109                 :            :   }
     110                 :            : 
     111                 :            : #if defined(__GNUC__) && !defined(__clang__)
     112                 :            : #pragma GCC diagnostic push
     113                 :            : #pragma GCC diagnostic ignored "-Wclass-memaccess"
     114                 :            : #if __GNUC__ >= 11
     115                 :            : #pragma GCC diagnostic ignored "-Wstringop-overread"
     116                 :            : #endif
     117                 :            : #endif
     118                 :            : 
     119                 :          7 :   auto* target_ptr = reinterpret_cast<uint8_t*>(this);
     120                 :            : 
     121                 :          7 :   std::memcpy(target_ptr, bytes.data() + kMagicNumberBeginSize + kVersionSize, sizeof(ObjectArray));
     122                 :            : 
     123                 :            : #if defined(__GNUC__) && !defined(__clang__)
     124                 :            : #pragma GCC diagnostic pop
     125                 :            : #endif
     126                 :            : 
     127                 :          7 :   data_ = const_cast<uint8_t*>(bytes.data() + kMagicNumberBeginSize + kVersionSize + sizeof(ObjectArray));
     128                 :            : 
     129                 :          7 :   capacity_ = 0;
     130                 :            : 
     131                 :          7 :   is_owner_ = false;
     132                 :            : 
     133                 :          7 :   const auto payload_size = bytes.size() - kSerializedOverhead;
     134                 :            : 
     135   [ +  +  +  -  :          7 :   if VUNLIKELY (count_ != 0 &&
          +  +  +  +  +  
                      + ]
     136                 :            :                 (pack_size_ != sizeof(Object) || static_cast<size_t>(count_) > payload_size / sizeof(Object))) {
     137                 :          2 :     clear();
     138                 :          2 :     return false;
     139                 :            :   }
     140                 :            : 
     141         [ -  + ]:          5 :   if VUNLIKELY (bytes.size() != get_serialized_size()) {
     142                 :          0 :     clear();
     143                 :          0 :     return false;
     144                 :            :   }
     145                 :            : 
     146                 :          5 :   return true;
     147                 :            : }
     148                 :            : 
     149                 :         10 : bool ObjectArray::operator>>(Bytes& bytes) const noexcept {
     150                 :            :   static constexpr size_t kMagicNumberBeginSize = sizeof(kMagicNumberBegin);
     151                 :            :   static constexpr size_t kVersionSize = sizeof(kWireVersion);
     152                 :            :   static constexpr size_t kMagicNumberEndSize = sizeof(kMagicNumberEnd);
     153                 :            : 
     154   [ +  +  +  +  :         10 :   if (bytes.empty() || bytes.size() != get_serialized_size()) {
                   +  + ]
     155                 :          9 :     bytes = Bytes::create(get_serialized_size());
     156                 :            : 
     157         [ -  + ]:          9 :     if VUNLIKELY (bytes.empty()) {
     158                 :          0 :       return false;
     159                 :            :     }
     160                 :            :   }
     161                 :            : 
     162                 :         10 :   std::memcpy(bytes.data(), &kMagicNumberBegin, kMagicNumberBeginSize);
     163                 :            : 
     164                 :         10 :   std::memcpy(bytes.data() + kMagicNumberBeginSize, &kWireVersion, kVersionSize);
     165                 :            : 
     166                 :            :   // NOLINTNEXTLINE(bugprone-undefined-memory-manipulation)
     167                 :         10 :   std::memcpy(bytes.data() + kMagicNumberBeginSize + kVersionSize, this, sizeof(ObjectArray));
     168                 :            : 
     169                 :         10 :   const auto data_offset = reinterpret_cast<const uint8_t*>(&data_) - reinterpret_cast<const uint8_t*>(this);
     170                 :         10 :   const size_t data_pointer_size = sizeof(data_);
     171                 :         10 :   std::memset(bytes.data() + kMagicNumberBeginSize + kVersionSize + data_offset, 0, data_pointer_size);
     172                 :            : 
     173   [ +  +  +  +  :         10 :   if VLIKELY (data_ != nullptr && count_ != 0 && pack_size_ != 0) {
          +  +  +  -  +  
                      + ]
     174                 :          7 :     std::memcpy(bytes.data() + kMagicNumberBeginSize + kVersionSize + sizeof(ObjectArray), data_,
     175                 :          7 :                 static_cast<size_t>(count_) * pack_size_);
     176                 :            :   }
     177                 :            : 
     178                 :         10 :   std::memcpy(bytes.data() + kMagicNumberBeginSize + kVersionSize + sizeof(ObjectArray) +
     179                 :         10 :                   (static_cast<size_t>(count_) * pack_size_),
     180                 :            :               &kMagicNumberEnd, kMagicNumberEndSize);
     181                 :            : 
     182                 :         10 :   return true;
     183                 :            : }
     184                 :            : 
     185                 :         16 : bool ObjectArray::check_valid(const Bytes& bytes) noexcept {
     186                 :            :   static constexpr size_t kMagicNumberBeginSize = sizeof(kMagicNumberBegin);
     187                 :            :   static constexpr size_t kVersionSize = sizeof(kWireVersion);
     188                 :            :   static constexpr size_t kMagicNumberEndSize = sizeof(kMagicNumberEnd);
     189                 :            : 
     190         [ +  + ]:         16 :   if VUNLIKELY (bytes.size() < kMagicNumberBeginSize + kVersionSize + sizeof(ObjectArray) + kMagicNumberEndSize) {
     191                 :          2 :     return false;
     192                 :            :   }
     193                 :            : 
     194                 :         14 :   uint32_t check_magic = 0;
     195                 :            : 
     196                 :         14 :   std::memcpy(&check_magic, bytes.begin(), kMagicNumberBeginSize);
     197                 :            : 
     198         [ +  + ]:         14 :   if VUNLIKELY (check_magic != kMagicNumberBegin) {
     199                 :          1 :     return false;
     200                 :            :   }
     201                 :            : 
     202                 :         13 :   uint32_t wire_version = 0;
     203                 :         13 :   std::memcpy(&wire_version, bytes.data() + kMagicNumberBeginSize, kVersionSize);
     204                 :            : 
     205         [ +  + ]:         13 :   if VUNLIKELY (version_major(wire_version) != version_major(kWireVersion)) {
     206                 :          2 :     return false;
     207                 :            :   }
     208                 :            : 
     209                 :         11 :   std::memcpy(&check_magic, bytes.end() - kMagicNumberEndSize, kMagicNumberEndSize);
     210                 :            : 
     211         [ +  + ]:         11 :   if VUNLIKELY (check_magic != kMagicNumberEnd) {
     212                 :          1 :     return false;
     213                 :            :   }
     214                 :            : 
     215                 :         10 :   return true;
     216                 :            : }
     217                 :            : 
     218                 :         21 : size_t ObjectArray::get_serialized_size() const noexcept {
     219                 :            :   static constexpr size_t kMagicNumberBeginSize = sizeof(kMagicNumberBegin);
     220                 :            :   static constexpr size_t kVersionSize = sizeof(kWireVersion);
     221                 :            :   static constexpr size_t kMagicNumberEndSize = sizeof(kMagicNumberEnd);
     222                 :            : 
     223                 :         21 :   return kMagicNumberBeginSize + kVersionSize + sizeof(ObjectArray) + (static_cast<size_t>(count_) * pack_size_) +
     224                 :         21 :          kMagicNumberEndSize;
     225                 :            : }
     226                 :            : 
     227   [ +  +  +  +  :         16 : bool ObjectArray::is_valid() const noexcept { return data_ != nullptr && count_ != 0 && pack_size_ != 0; }
                   +  - ]
     228                 :            : 
     229                 :         13 : bool ObjectArray::shallow_copy(const ObjectArray& target) noexcept {
     230         [ +  + ]:         13 :   if VUNLIKELY (this == &target) {
     231                 :          3 :     return false;
     232                 :            :   }
     233                 :            : 
     234   [ +  +  +  -  :         10 :   if (is_owner_ && data_ && capacity_ != 0) {
                   +  - ]
     235                 :          2 :     const auto current = reinterpret_cast<uintptr_t>(data_);
     236                 :          2 :     const auto source = reinterpret_cast<uintptr_t>(target.data_);
     237                 :            : 
     238   [ +  -  +  -  :          2 :     if VUNLIKELY (source >= current && source - current < capacity_) {
                   +  - ]
     239                 :          2 :       return false;
     240                 :            :     }
     241                 :            : 
     242                 :            :     Bytes::bytes_free(data_, capacity_);  // LCOV_EXCL_LINE GCOVR_EXCL_LINE
     243                 :            :   }
     244                 :            : 
     245                 :          8 :   header = target.header;
     246                 :            : 
     247                 :          8 :   update_time_ns_ = target.update_time_ns_;
     248                 :          8 :   std::memcpy(source_id_, target.source_id_, sizeof(source_id_));
     249                 :          8 :   channel_ = target.channel_;
     250                 :          8 :   freq_ = target.freq_;
     251                 :          8 :   count_ = target.count_;
     252                 :          8 :   pack_size_ = target.pack_size_;
     253                 :          8 :   reserved_buf_ = target.reserved_buf_;
     254                 :          8 :   reserved_buf2_ = target.reserved_buf2_;
     255                 :          8 :   reserved_buf3_ = target.reserved_buf3_;
     256                 :          8 :   reserved_buf4_ = target.reserved_buf4_;
     257                 :          8 :   reserved_buf5_ = target.reserved_buf5_;
     258                 :          8 :   is_owner_ = false;
     259                 :          8 :   capacity_ = 0;
     260                 :          8 :   data_ = target.data_;
     261                 :            : 
     262                 :          8 :   return true;
     263                 :            : }
     264                 :            : 
     265                 :          5 : bool ObjectArray::deep_copy(const ObjectArray& target) noexcept {
     266                 :          5 :   const size_t target_size = static_cast<size_t>(target.count_) * target.pack_size_;
     267                 :            : 
     268   [ +  +  +  -  :          5 :   if VLIKELY (data_ && is_owner_ && target.data_ && capacity_ != 0 && capacity_ == target_size) {
          +  +  +  -  +  
          +  +  -  +  +  
             -  +  -  + ]
     269                 :          0 :     const auto current = reinterpret_cast<uintptr_t>(data_);
     270                 :          0 :     const auto source = reinterpret_cast<uintptr_t>(target.data_);
     271                 :            : 
     272   [ #  #  #  #  :          0 :     if VUNLIKELY (source >= current && source - current < capacity_) {
                   #  # ]
     273                 :          0 :       return false;
     274                 :            :     }
     275                 :            : 
     276                 :          0 :     header = target.header;
     277                 :            : 
     278                 :          0 :     update_time_ns_ = target.update_time_ns_;
     279                 :          0 :     std::memcpy(source_id_, target.source_id_, sizeof(source_id_));
     280                 :          0 :     channel_ = target.channel_;
     281                 :          0 :     freq_ = target.freq_;
     282                 :          0 :     count_ = target.count_;
     283                 :          0 :     pack_size_ = target.pack_size_;
     284                 :          0 :     reserved_buf_ = target.reserved_buf_;
     285                 :          0 :     reserved_buf2_ = target.reserved_buf2_;
     286                 :          0 :     reserved_buf3_ = target.reserved_buf3_;
     287                 :          0 :     reserved_buf4_ = target.reserved_buf4_;
     288                 :          0 :     reserved_buf5_ = target.reserved_buf5_;
     289                 :            : 
     290                 :          0 :     std::memcpy(data_, target.data_, target_size);
     291                 :            : 
     292                 :          0 :     return true;
     293                 :            :   }
     294                 :            : 
     295         [ +  + ]:          5 :   if VUNLIKELY (!shallow_copy(target)) {
     296                 :          2 :     return false;
     297                 :            :   }
     298                 :            : 
     299   [ +  -  +  - ]:          3 :   if (data_ && target_size != 0) {
     300                 :          3 :     auto* target_data = data_;
     301                 :          3 :     capacity_ = target_size;
     302                 :          3 :     data_ = Bytes::bytes_malloc(capacity_);
     303                 :            : 
     304         [ -  + ]:          3 :     if VUNLIKELY (!data_) {
     305                 :          0 :       count_ = 0;
     306                 :          0 :       capacity_ = 0;
     307                 :          0 :       return false;
     308                 :            :     }
     309                 :            : 
     310                 :          3 :     std::memcpy(data_, target_data, target_size);
     311                 :          3 :     is_owner_ = true;
     312                 :            :   }
     313                 :            : 
     314                 :          3 :   return true;
     315                 :            : }
     316                 :            : 
     317                 :          4 : bool ObjectArray::move_copy(ObjectArray& target) noexcept {
     318         [ +  + ]:          4 :   if VUNLIKELY (!shallow_copy(target)) {
     319                 :          1 :     return false;
     320                 :            :   }
     321                 :            : 
     322                 :          3 :   is_owner_ = target.is_owner_;
     323                 :          3 :   capacity_ = target.capacity_;
     324                 :            : 
     325                 :          3 :   target.update_time_ns_ = 0;
     326                 :          3 :   std::memset(target.source_id_, 0, sizeof(target.source_id_));
     327                 :          3 :   target.channel_ = 0;
     328                 :          3 :   target.freq_ = 0;
     329                 :          3 :   target.count_ = 0;
     330                 :          3 :   target.pack_size_ = 0;
     331                 :          3 :   target.reserved_buf_ = 0;
     332                 :          3 :   target.reserved_buf2_ = 0;
     333                 :          3 :   target.reserved_buf3_ = 0;
     334                 :          3 :   target.reserved_buf4_ = 0;
     335                 :          3 :   target.reserved_buf5_ = 0;
     336                 :          3 :   target.is_owner_ = false;
     337                 :          3 :   target.data_ = nullptr;
     338                 :          3 :   target.capacity_ = 0;
     339                 :            : 
     340                 :            : #if defined(__GNUC__) && !defined(__clang__)
     341                 :            : #pragma GCC diagnostic push
     342                 :            : #pragma GCC diagnostic ignored "-Wclass-memaccess"
     343                 :            : #if __GNUC__ >= 11
     344                 :            : #pragma GCC diagnostic ignored "-Wstringop-overread"
     345                 :            : #endif
     346                 :            : #endif
     347                 :            : 
     348                 :          3 :   std::memset(&target.header, 0, sizeof(header));
     349                 :            : 
     350                 :            : #if defined(__GNUC__) && !defined(__clang__)
     351                 :            : #pragma GCC diagnostic pop
     352                 :            : #endif
     353                 :            : 
     354                 :          3 :   return true;
     355                 :            : }
     356                 :            : 
     357                 :         43 : bool ObjectArray::create(size_t count) noexcept {
     358         [ +  + ]:         43 :   if VUNLIKELY (count == 0) {
     359                 :          1 :     return false;
     360                 :            :   }
     361                 :            : 
     362         [ -  + ]:         42 :   if VUNLIKELY (count > std::numeric_limits<size_t>::max() / sizeof(Object)) {
     363                 :            :     return false;  // LCOV_EXCL_LINE GCOVR_EXCL_LINE
     364                 :            :   }
     365                 :            : 
     366   [ +  +  +  -  :         42 :   if (is_owner_ && data_ && capacity_ != 0) {
                   +  - ]
     367                 :          1 :     Bytes::bytes_free(data_, capacity_);
     368                 :            :   }
     369                 :            : 
     370                 :         42 :   pack_size_ = sizeof(Object);
     371                 :         42 :   capacity_ = count * pack_size_;
     372                 :         42 :   count_ = 0;
     373                 :         42 :   data_ = Bytes::bytes_malloc(capacity_);
     374                 :            : 
     375         [ -  + ]:         42 :   if VUNLIKELY (!data_) {
     376                 :          0 :     capacity_ = 0;
     377                 :          0 :     is_owner_ = false;
     378                 :          0 :     return false;
     379                 :            :   }
     380                 :            : 
     381                 :         42 :   is_owner_ = true;
     382                 :            : 
     383                 :         42 :   return true;
     384                 :            : }
     385                 :            : 
     386                 :          6 : void ObjectArray::clear() noexcept {
     387   [ +  +  +  -  :          6 :   if (is_owner_ && data_ && capacity_ != 0) {
                   +  - ]
     388                 :          4 :     Bytes::bytes_free(data_, capacity_);
     389                 :            :   }
     390                 :            : 
     391                 :          6 :   update_time_ns_ = 0;
     392                 :          6 :   std::memset(source_id_, 0, sizeof(source_id_));
     393                 :          6 :   channel_ = 0;
     394                 :          6 :   freq_ = 0;
     395                 :          6 :   count_ = 0;
     396                 :          6 :   pack_size_ = 0;
     397                 :          6 :   is_owner_ = false;
     398                 :          6 :   data_ = nullptr;
     399                 :          6 :   capacity_ = 0;
     400                 :            : 
     401                 :            : #if defined(__GNUC__) && !defined(__clang__)
     402                 :            : #pragma GCC diagnostic push
     403                 :            : #pragma GCC diagnostic ignored "-Wclass-memaccess"
     404                 :            : #if __GNUC__ >= 11
     405                 :            : #pragma GCC diagnostic ignored "-Wstringop-overread"
     406                 :            : #endif
     407                 :            : #endif
     408                 :            : 
     409                 :          6 :   std::memset(&header, 0, sizeof(header));
     410                 :            : 
     411                 :            : #if defined(__GNUC__) && !defined(__clang__)
     412                 :            : #pragma GCC diagnostic pop
     413                 :            : #endif
     414                 :          6 : }
     415                 :            : 
     416                 :         45 : bool ObjectArray::push_value(const Object& object) noexcept {
     417   [ +  +  -  +  :         45 :   if VUNLIKELY (!is_owner_ || !data_ || pack_size_ == 0 || capacity_ == 0) {
          +  +  -  +  +  
             +  -  +  +  
                      + ]
     418                 :          1 :     return false;
     419                 :            :   }
     420                 :            : 
     421         [ +  + ]:         44 :   if VUNLIKELY ((static_cast<size_t>(count_) + 1) * pack_size_ > capacity_) {
     422                 :          1 :     return false;
     423                 :            :   }
     424                 :            : 
     425         [ -  + ]:         43 :   if VUNLIKELY (pack_size_ != sizeof(Object)) {
     426                 :            :     return false;  // LCOV_EXCL_LINE GCOVR_EXCL_LINE
     427                 :            :   }
     428                 :            : 
     429                 :         43 :   std::memcpy(data_ + (static_cast<size_t>(count_) * pack_size_), &object, sizeof(Object));
     430                 :            : 
     431                 :         43 :   ++count_;
     432                 :            : 
     433                 :         43 :   return true;
     434                 :            : }
     435                 :            : 
     436                 :          2 : bool ObjectArray::set_value(uint32_t index, const Object& object) noexcept {
     437   [ +  -  -  +  :          2 :   if VUNLIKELY (!is_owner_ || !data_ || pack_size_ == 0 || capacity_ == 0) {
          +  -  -  +  +  
             -  -  +  -  
                      + ]
     438                 :          0 :     return false;
     439                 :            :   }
     440                 :            : 
     441         [ +  + ]:          2 :   if VUNLIKELY (index >= count_) {
     442                 :          1 :     return false;
     443                 :            :   }
     444                 :            : 
     445         [ -  + ]:          1 :   if VUNLIKELY (pack_size_ != sizeof(Object)) {
     446                 :            :     return false;  // LCOV_EXCL_LINE GCOVR_EXCL_LINE
     447                 :            :   }
     448                 :            : 
     449                 :          1 :   std::memcpy(data_ + (static_cast<size_t>(index) * pack_size_), &object, sizeof(Object));
     450                 :            : 
     451                 :          1 :   return true;
     452                 :            : }
     453                 :            : 
     454                 :          8 : bool ObjectArray::get_value(uint32_t index, Object& object) const noexcept {
     455   [ +  -  -  +  :          8 :   if VUNLIKELY (!data_ || pack_size_ == 0) {
                   -  + ]
     456                 :          0 :     object = Object{};
     457                 :          0 :     return false;
     458                 :            :   }
     459                 :            : 
     460         [ +  + ]:          8 :   if VUNLIKELY (index >= count_) {
     461                 :          2 :     object = Object{};
     462                 :          2 :     return false;
     463                 :            :   }
     464                 :            : 
     465         [ -  + ]:          6 :   if VUNLIKELY (pack_size_ != sizeof(Object)) {
     466                 :            :     object = Object{};  // LCOV_EXCL_LINE GCOVR_EXCL_LINE
     467                 :            :     return false;       // LCOV_EXCL_LINE GCOVR_EXCL_LINE
     468                 :            :   }
     469                 :            : 
     470                 :          6 :   std::memcpy(&object, data_ + (static_cast<size_t>(index) * pack_size_), sizeof(Object));
     471                 :            : 
     472                 :          6 :   return true;
     473                 :            : }
     474                 :            : 
     475                 :          2 : ObjectArray::Object ObjectArray::get_value(uint32_t index) const noexcept {
     476                 :          2 :   Object object;
     477                 :            : 
     478                 :          2 :   get_value(index, object);
     479                 :            : 
     480                 :          2 :   return object;
     481                 :            : }
     482                 :            : 
     483                 :          3 : bool ObjectArray::resize(uint32_t count) noexcept {
     484   [ +  +  -  +  :          3 :   if VUNLIKELY (!is_owner_ || !data_ || pack_size_ == 0 || capacity_ == 0) {
          +  +  -  +  +  
             +  -  +  +  
                      + ]
     485                 :          1 :     return false;
     486                 :            :   }
     487                 :            : 
     488         [ +  + ]:          2 :   if VUNLIKELY (static_cast<size_t>(count) * pack_size_ > capacity_) {
     489                 :          1 :     return false;
     490                 :            :   }
     491                 :            : 
     492                 :          1 :   count_ = count;
     493                 :            : 
     494                 :          1 :   return true;
     495                 :            : }
     496                 :            : 
     497                 :          3 : uint64_t ObjectArray::update_time_ns() const noexcept { return update_time_ns_; }
     498                 :            : 
     499                 :          8 : std::string_view ObjectArray::source_id() const noexcept {
     500                 :          8 :   return {source_id_, ::strnlen(source_id_, sizeof(source_id_))};
     501                 :            : }
     502                 :            : 
     503                 :          4 : uint32_t ObjectArray::channel() const noexcept { return channel_; }
     504                 :            : 
     505                 :          4 : uint32_t ObjectArray::freq() const noexcept { return freq_; }
     506                 :            : 
     507                 :         28 : uint32_t ObjectArray::count() const noexcept { return count_; }
     508                 :            : 
     509                 :         12 : uint32_t ObjectArray::pack_size() const noexcept { return pack_size_; }
     510                 :            : 
     511                 :         13 : const ObjectArray::Object* ObjectArray::objects(uint32_t index) const noexcept {
     512   [ +  +  -  +  :         13 :   if VUNLIKELY (!data_ || pack_size_ != sizeof(Object) || index >= count_) {
          +  +  +  +  +  
                      + ]
     513                 :          2 :     return nullptr;
     514                 :            :   }
     515                 :            : 
     516                 :         11 :   return reinterpret_cast<const Object*>(data_ + (static_cast<size_t>(index) * pack_size_));
     517                 :            : }
     518                 :            : 
     519                 :         21 : const uint8_t* ObjectArray::data() const noexcept { return data_; }
     520                 :            : 
     521                 :         14 : size_t ObjectArray::capacity() const noexcept { return capacity_; }
     522                 :            : 
     523                 :         19 : bool ObjectArray::is_owner() const noexcept { return is_owner_; }
     524                 :            : 
     525                 :          2 : void ObjectArray::set_update_time_ns(uint64_t update_time_ns) noexcept { update_time_ns_ = update_time_ns; }
     526                 :            : 
     527                 :          7 : void ObjectArray::set_source_id(std::string_view source_id) noexcept {
     528                 :          7 :   std::memset(source_id_, 0, sizeof(source_id_));
     529                 :            : 
     530                 :          7 :   size_t copy_size = source_id.size();
     531                 :            : 
     532         [ +  + ]:          7 :   if (copy_size >= sizeof(source_id_)) {
     533                 :          1 :     copy_size = sizeof(source_id_) - 1;
     534                 :            :   }
     535                 :            : 
     536         [ +  - ]:          7 :   if VLIKELY (copy_size != 0) {
     537                 :          7 :     std::memcpy(source_id_, source_id.data(), copy_size);
     538                 :            :   }
     539                 :          7 : }
     540                 :            : 
     541                 :          3 : void ObjectArray::set_channel(uint32_t channel) noexcept { channel_ = channel; }
     542                 :            : 
     543                 :          3 : void ObjectArray::set_freq(uint32_t freq) noexcept { freq_ = freq; }
     544                 :            : 
     545                 :            : }  // namespace zerocopy
     546                 :            : 
     547                 :            : }  // namespace vlink

Generated by: LCOV version 1.14