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

tstack / lnav / 17589970077-2502

09 Sep 2025 05:00PM UTC coverage: 65.196% (-5.0%) from 70.225%
17589970077-2502

push

github

tstack
[format] add fields for source file/line

Knowing the source file/line context in a log
message can help find log messages when using
log2src.

56 of 70 new or added lines in 2 files covered. (80.0%)

13954 existing lines in 210 files now uncovered.

45516 of 69814 relevant lines covered (65.2%)

404154.37 hits per line

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

91.07
/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 <string_view>
37
#include <vector>
38

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

48
class shared_buffer;
49

50
#define SHARED_BUFFER_TRACE 0
51

52
struct shared_buffer_ref {
53
    shared_buffer_ref(const char* data = nullptr, size_t len = 0)
145,019✔
54
        : sb_owner(nullptr), sb_data(data), sb_length(len)
145,019✔
55
    {
56
    }
145,019✔
57

58
    ~shared_buffer_ref() { this->disown(); }
607,493✔
59

60
    shared_buffer_ref(const shared_buffer_ref& other) = delete;
61

62
    shared_buffer_ref(shared_buffer_ref&& other) noexcept;
63

64
    shared_buffer_ref& operator=(const shared_buffer_ref& other) = delete;
65

66
    shared_buffer_ref clone() const
6,380✔
67
    {
68
        shared_buffer_ref retval;
6,380✔
69

70
        retval.copy_ref(*this);
6,380✔
71

72
        return retval;
6,380✔
UNCOV
73
    }
×
74

75
    shared_buffer_ref& operator=(shared_buffer_ref&& other) noexcept;
76

77
    bool empty() const
11,824✔
78
    {
79
        return this->sb_data == nullptr || this->sb_length == 0;
11,824✔
80
    }
81

82
    const char* get_data() const { return this->sb_data; }
1,035,272✔
83

84
    const char* get_data_at(off_t offset) const
302,510✔
85
    {
86
        return &this->sb_data[offset];
302,510✔
87
    }
88

89
    size_t length() const { return this->sb_length; }
760,772✔
90

91
    shared_buffer_ref& rtrim(bool pred(char))
40,909✔
92
    {
93
        while (this->sb_length > 0 && pred(this->sb_data[this->sb_length - 1]))
57,439✔
94
        {
95
            this->sb_length -= 1;
16,530✔
96
        }
97

98
        return *this;
40,909✔
99
    }
100

101
    bool contains(const char* ptr) const
5,006✔
102
    {
103
        const char* buffer_end = this->sb_data + this->sb_length;
5,006✔
104

105
        return (this->sb_data <= ptr && ptr < buffer_end);
5,006✔
106
    }
107

108
    const file_range::metadata& get_metadata() const { return this->sb_metadata; }
594✔
109

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

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

118
        return nullptr;
×
119
    }
120

121
    char* get_writable_data()
202✔
122
    {
123
        return this->get_writable_data(this->sb_length);
202✔
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
734,612✔
133
    {
134
        return string_fragment::from_bytes(this->sb_data, this->length());
734,612✔
135
    }
136

137
    std::string_view to_string_view(const line_range& lr) const
302,492✔
138
    {
139
        return std::string_view{
302,492✔
140
            this->get_data_at(lr.lr_start),
302,492✔
141
            static_cast<std::string_view::size_type>(lr.length()),
302,492✔
142
        };
302,492✔
143
    }
144

145
    std::string_view to_string_view() const
15✔
146
    {
147
        return std::string_view{
30✔
148
            this->sb_data,
15✔
149
            this->sb_length,
15✔
150
        };
15✔
151
    }
152

153
    using narrow_result = std::pair<const char*, size_t>;
154
    narrow_result narrow(size_t new_data, size_t new_length);
155

156
    void widen(narrow_result old_data_length);
157

158
    void share(shared_buffer& sb, const char* data, size_t len);
159

160
    bool subset(shared_buffer_ref& other, off_t offset, size_t len);
161

162
    void erase_ansi();
163

164
    bool take_ownership(size_t length);
165

166
    bool take_ownership() { return this->take_ownership(this->sb_length); }
1,776✔
167

168
    void disown();
169

170
private:
171
    void copy_ref(const shared_buffer_ref& other);
172

173
#if SHARED_BUFFER_TRACE
174
    auto_mem<char*> sb_backtrace;
175
#endif
176
    file_range::metadata sb_metadata;
177
    shared_buffer* sb_owner;
178
    const char* sb_data;
179
    size_t sb_length;
180
};
181

182
class shared_buffer {
183
public:
184
    ~shared_buffer() { this->invalidate_refs(); }
44,282✔
185

186
    void add_ref(shared_buffer_ref& ref) { this->sb_refs.push_back(&ref); }
97,962✔
187

188
    bool invalidate_refs()
52,025✔
189
    {
190
        bool retval = true;
52,025✔
191

192
        while (!this->sb_refs.empty()) {
53,801✔
193
            auto iter = this->sb_refs.begin();
1,776✔
194

195
            retval = retval && (*iter)->take_ownership();
1,776✔
196
        }
197

198
        return retval;
52,025✔
199
    }
200

201
    std::vector<shared_buffer_ref*> sb_refs;
202
};
203

204
inline std::string
205
to_string(const shared_buffer_ref& sbr)
4,065✔
206
{
207
    return {sbr.get_data(), sbr.length()};
12,195✔
208
}
209

210
#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