• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

tstack / lnav / 11872214087-1756

16 Nov 2024 06:12PM UTC coverage: 70.243% (+0.5%) from 69.712%
11872214087-1756

push

github

tstack
[build] disable regex101

46266 of 65866 relevant lines covered (70.24%)

467515.63 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.0
/src/shared_buffer.hh
1
/**
2
 * Copyright (c) 2014, Timothy Stack
3
 *
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * * Redistributions of source code must retain the above copyright notice, this
10
 * list of conditions and the following disclaimer.
11
 * * Redistributions in binary form must reproduce the above copyright notice,
12
 * this list of conditions and the following disclaimer in the documentation
13
 * and/or other materials provided with the distribution.
14
 * * Neither the name of Timothy Stack nor the names of its contributors
15
 * may be used to endorse or promote products derived from this software
16
 * without specific prior written permission.
17
 *
18
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
 *
29
 * @file shared_buffer.hh
30
 */
31

32
#ifndef shared_buffer_hh
33
#define shared_buffer_hh
34

35
#include <string>
36
#include <vector>
37

38
#include <stdlib.h>
39
#include <string.h>
40
#include <sys/types.h>
41

42
#include "base/auto_mem.hh"
43
#include "base/file_range.hh"
44
#include "base/intern_string.hh"
45
#include "base/line_range.hh"
46
#include "base/lnav_log.hh"
47
#include "scn/util/string_view.h"
48

49
class shared_buffer;
50

51
#define SHARED_BUFFER_TRACE 0
52

53
struct shared_buffer_ref {
54
public:
55
    shared_buffer_ref(char* data = nullptr, size_t len = 0)
105,976✔
56
        : sb_owner(nullptr), sb_data(data), sb_length(len)
105,976✔
57
    {
58
    }
105,976✔
59

60
    ~shared_buffer_ref() { this->disown(); }
456,042✔
61

62
    shared_buffer_ref(const shared_buffer_ref& other) = delete;
63

64
    shared_buffer_ref(shared_buffer_ref&& other) noexcept;
65

66
    shared_buffer_ref& operator=(const shared_buffer_ref& other) = delete;
67

68
    shared_buffer_ref clone() const
6,177✔
69
    {
70
        shared_buffer_ref retval;
6,177✔
71

72
        retval.copy_ref(*this);
6,177✔
73

74
        return retval;
6,177✔
75
    }
×
76

77
    shared_buffer_ref& operator=(shared_buffer_ref&& other);
78

79
    bool empty() const
6,288✔
80
    {
81
        return this->sb_data == nullptr || this->sb_length == 0;
6,288✔
82
    }
83

84
    const char* get_data() const { return this->sb_data; }
642,098✔
85

86
    const char* get_data_at(off_t offset) const
378,751✔
87
    {
88
        return &this->sb_data[offset];
378,751✔
89
    }
90

91
    size_t length() const { return this->sb_length; }
601,670✔
92

93
    shared_buffer_ref& rtrim(bool pred(char))
32,301✔
94
    {
95
        while (this->sb_length > 0 && pred(this->sb_data[this->sb_length - 1]))
42,782✔
96
        {
97
            this->sb_length -= 1;
10,481✔
98
        }
99

100
        return *this;
32,301✔
101
    }
102

103
    bool contains(const char* ptr) const
21,670✔
104
    {
105
        const char* buffer_end = this->sb_data + this->sb_length;
21,670✔
106

107
        return (this->sb_data <= ptr && ptr < buffer_end);
21,670✔
108
    }
109

110
    file_range::metadata& get_metadata() { return this->sb_metadata; }
85,083✔
111

112
    char* get_writable_data(size_t length)
104✔
113
    {
114
        if (this->take_ownership(length)) {
104✔
115
            return const_cast<char*>(this->sb_data);
104✔
116
        }
117

118
        return nullptr;
×
119
    }
120

121
    char* get_writable_data()
104✔
122
    {
123
        return this->get_writable_data(this->sb_length);
104✔
124
    }
125

126
    string_fragment to_string_fragment(off_t offset, size_t len) const
×
127
    {
128
        return string_fragment{
×
129
            this->sb_data, (int) offset, (int) (offset + len)};
×
130
    }
131

132
    string_fragment to_string_fragment() const
579,887✔
133
    {
134
        return string_fragment::from_bytes(this->sb_data, this->length());
579,887✔
135
    }
136

137
    scn::string_view to_string_view(const line_range& lr) const
189,367✔
138
    {
139
        return scn::string_view{
189,367✔
140
            this->get_data_at(lr.lr_start),
189,367✔
141
            this->get_data_at(lr.lr_end),
189,367✔
142
        };
189,367✔
143
    }
144

145
    using narrow_result = std::pair<const char*, size_t>;
146
    narrow_result narrow(size_t new_data, size_t new_length);
147

148
    void widen(narrow_result old_data_length);
149

150
    void share(shared_buffer& sb, const char* data, size_t len);
151

152
    bool subset(shared_buffer_ref& other, off_t offset, size_t len);
153

154
    void erase_ansi();
155

156
    bool take_ownership(size_t length);
157

158
    bool take_ownership() { return this->take_ownership(this->sb_length); }
1,662✔
159

160
    void disown();
161

162
private:
163
    void copy_ref(const shared_buffer_ref& other);
164

165
#if SHARED_BUFFER_TRACE
166
    auto_mem<char*> sb_backtrace;
167
#endif
168
    file_range::metadata sb_metadata;
169
    shared_buffer* sb_owner;
170
    const char* sb_data;
171
    size_t sb_length;
172
};
173

174
class shared_buffer {
175
public:
176
    ~shared_buffer() { this->invalidate_refs(); }
32,426✔
177

178
    void add_ref(shared_buffer_ref& ref) { this->sb_refs.push_back(&ref); }
70,691✔
179

180
    bool invalidate_refs()
37,688✔
181
    {
182
        bool retval = true;
37,688✔
183

184
        while (!this->sb_refs.empty()) {
39,350✔
185
            auto iter = this->sb_refs.begin();
1,662✔
186

187
            retval = retval && (*iter)->take_ownership();
1,662✔
188
        }
189

190
        return retval;
37,688✔
191
    }
192

193
    std::vector<shared_buffer_ref*> sb_refs;
194
};
195

196
inline std::string
197
to_string(const shared_buffer_ref& sbr)
2,184✔
198
{
199
    return {sbr.get_data(), sbr.length()};
2,184✔
200
}
201

202
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc