GCC Code Coverage Report


Directory: cvmfs/
File: cvmfs/network/sink_mem.cc
Date: 2026-04-26 02:35:59
Exec Total Coverage
Lines: 40 49 81.6%
Branches: 23 42 54.8%

Line Branch Exec Source
1 /**
2 * This file is part of the CernVM File System.
3 */
4
5 #include "sink_mem.h"
6
7 #include <cassert>
8 #include <cstring>
9 #include <string>
10
11 #include "util/smalloc.h"
12 #include "util/string.h"
13
14 namespace cvmfs {
15
16 MemSink::MemSink(size_t size)
17 : Sink(true), size_(size), pos_(0), max_size_(kMaxMemSize) {
18 data_ = static_cast<unsigned char *>(smalloc(size));
19 }
20
21 /**
22 * Appends data to the sink
23 * If the sink is too small and
24 * - the sink is the owner of data_: sink size is doubled
25 * - the sink is NOT the owner of data_: fails with -ENOSPC
26 *
27 * @returns on success: number of bytes written
28 * on failure: -errno.
29 */
30 2924 int64_t MemSink::Write(const void *buf, uint64_t sz) {
31
2/2
✓ Branch 0 taken 105 times.
✓ Branch 1 taken 2819 times.
2924 if (pos_ + sz > size_) {
32
2/2
✓ Branch 0 taken 70 times.
✓ Branch 1 taken 35 times.
105 if (is_owner_) {
33
2/2
✓ Branch 0 taken 35 times.
✓ Branch 1 taken 35 times.
70 const size_t new_size = pos_ + sz < size_ * 2 ? size_ * 2 : pos_ + sz + 1;
34 70 data_ = static_cast<unsigned char *>(srealloc(data_, new_size));
35 70 size_ = new_size;
36 } else {
37 35 return -ENOSPC;
38 }
39 }
40
41 2889 memcpy(data_ + pos_, buf, sz);
42 2889 pos_ += sz;
43 2889 return static_cast<int64_t>(sz);
44 }
45
46 /**
47 * Truncate all written data and start over at position zero.
48 *
49 * @returns Success = 0
50 * Failure = -errno
51 */
52 464 int MemSink::Reset() {
53
1/2
✓ Branch 0 taken 464 times.
✗ Branch 1 not taken.
464 if (is_owner_) {
54 464 free(data_);
55 464 data_ = NULL;
56 464 size_ = 0;
57 }
58
59 464 pos_ = 0;
60
61 464 return 0;
62 }
63
64 /**
65 * @returns true if the object is correctly initialized.
66 */
67 2095 bool MemSink::IsValid() {
68
3/4
✓ Branch 0 taken 1990 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 35 times.
✓ Branch 3 taken 1955 times.
1990 return (size_ == 0 && pos_ == 0 && data_ == NULL)
69
6/6
✓ Branch 0 taken 1990 times.
✓ Branch 1 taken 105 times.
✓ Branch 2 taken 105 times.
✓ Branch 3 taken 35 times.
✓ Branch 4 taken 70 times.
✓ Branch 5 taken 35 times.
4085 || (size_ > 0 && pos_ >= 0 && data_ != NULL);
70 }
71
72 /**
73 * Reserves new space in sinks that require reservation (see RequiresReserve)
74 *
75 * Successful if the requested size is smaller than already space reserved, or
76 * if the sink is the owner of the data and can allocate enough new space.
77 *
78 * @note If successful, always resets the current position to 0.
79 *
80 * Fails if
81 * 1) sink is not the owner of the data and more than the current size is
82 * requested
83 * 2) more space is requested than allowed (max_size_)
84 *
85 * @returns success = true
86 * failure = false
87 */
88 3287 bool MemSink::Reserve(size_t size) {
89
2/2
✓ Branch 0 taken 1385 times.
✓ Branch 1 taken 1902 times.
3287 if (size <= size_) {
90 1385 pos_ = 0;
91 1385 return true;
92 }
93
3/4
✓ Branch 0 taken 1867 times.
✓ Branch 1 taken 35 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1867 times.
1902 if (!is_owner_ || size > max_size_) {
94 35 return false;
95 }
96
97 1867 FreeData();
98
99 1867 size_ = size;
100 1867 pos_ = 0;
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1867 times.
1867 if (size == 0) {
102 data_ = NULL;
103 } else {
104 1867 data_ = static_cast<unsigned char *>(smalloc(size));
105 }
106 1867 return true;
107 }
108
109 /**
110 * Return a string representation describing the type of sink and its status
111 */
112 std::string MemSink::Describe() {
113 std::string result = "Memory sink with ";
114 result += "size: " + StringifyUint(size_);
115 result += " - current pos: " + StringifyUint(pos_);
116 return result;
117 }
118
119 /**
120 * Allows the sink to adopt data that was initialized outside this class.
121 * The sink can become the new owner of the data, or not.
122 */
123 140 void MemSink::Adopt(size_t size, size_t pos, unsigned char *data,
124 bool is_owner) {
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 140 times.
140 assert(size >= pos);
126
127 140 FreeData();
128
129 140 is_owner_ = is_owner;
130 140 size_ = size;
131 140 pos_ = pos;
132 140 data_ = data;
133 140 }
134
135 } // namespace cvmfs
136