lnav/src/shared_buffer.hh

211 lines
5.5 KiB
C++
Raw Permalink Normal View History

2014-02-01 15:41:11 +01:00
/**
* Copyright (c) 2014, Timothy Stack
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Timothy Stack nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
2022-03-16 23:38:08 +01:00
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2014-02-01 15:41:11 +01:00
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* @file shared_buffer.hh
*/
#ifndef shared_buffer_hh
#define shared_buffer_hh
2014-02-01 15:41:11 +01:00
2022-03-16 23:38:08 +01:00
#include <string>
#include <vector>
2014-02-01 15:41:11 +01:00
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
2022-07-29 09:07:11 +02:00
#include "base/attr_line.hh"
2022-04-13 01:07:13 +02:00
#include "base/auto_mem.hh"
#include "base/file_range.hh"
2022-05-11 06:58:32 +02:00
#include "base/intern_string.hh"
2019-05-08 14:30:59 +02:00
#include "base/lnav_log.hh"
2022-07-29 09:07:11 +02:00
#include "scn/util/string_view.h"
2014-02-01 15:41:11 +01:00
class shared_buffer;
struct shared_buffer_ref {
2014-02-01 15:41:11 +01:00
public:
2022-03-16 23:38:08 +01:00
shared_buffer_ref(char* data = nullptr, size_t len = 0)
2022-05-11 06:58:32 +02:00
: sb_owner(nullptr), sb_data(data), sb_length(len)
2022-03-16 23:38:08 +01:00
{
2022-05-11 06:58:32 +02:00
}
~shared_buffer_ref() { this->disown(); }
2014-02-01 15:41:11 +01:00
2022-03-16 23:38:08 +01:00
shared_buffer_ref(const shared_buffer_ref& other)
{
this->sb_owner = nullptr;
this->sb_data = nullptr;
2014-02-01 15:41:11 +01:00
this->sb_length = 0;
this->sb_metadata = file_range::metadata{};
2014-02-01 15:41:11 +01:00
this->copy_ref(other);
2022-05-11 06:58:32 +02:00
}
2014-02-01 15:41:11 +01:00
2022-03-16 23:38:08 +01:00
shared_buffer_ref(shared_buffer_ref&& other) noexcept;
2022-03-16 23:38:08 +01:00
shared_buffer_ref& operator=(const shared_buffer_ref& other)
{
2014-02-01 15:41:11 +01:00
if (this != &other) {
this->disown();
this->copy_ref(other);
}
return *this;
2022-05-11 06:58:32 +02:00
}
2014-02-01 15:41:11 +01:00
2022-03-16 23:38:08 +01:00
bool empty() const
{
return this->sb_data == nullptr || this->sb_length == 0;
2022-05-11 06:58:32 +02:00
}
2022-05-11 06:58:32 +02:00
const char* get_data() const { return this->sb_data; }
2014-02-01 15:41:11 +01:00
2022-03-16 23:38:08 +01:00
const char* get_data_at(off_t offset) const
{
return &this->sb_data[offset];
2022-05-11 06:58:32 +02:00
}
2022-05-11 06:58:32 +02:00
size_t length() const { return this->sb_length; }
2014-02-01 15:41:11 +01:00
2022-03-16 23:38:08 +01:00
shared_buffer_ref& rtrim(bool pred(char))
{
while (this->sb_length > 0 && pred(this->sb_data[this->sb_length - 1]))
{
this->sb_length -= 1;
}
return *this;
}
2022-03-16 23:38:08 +01:00
bool contains(const char* ptr) const
{
const char* buffer_end = this->sb_data + this->sb_length;
2014-10-29 14:19:10 +01:00
return (this->sb_data <= ptr && ptr < buffer_end);
2022-05-11 06:58:32 +02:00
}
2014-10-29 14:19:10 +01:00
file_range::metadata& get_metadata() { return this->sb_metadata; }
2022-03-16 23:38:08 +01:00
char* get_writable_data()
{
2014-02-01 15:41:11 +01:00
if (this->take_ownership()) {
2022-08-01 23:56:48 +02:00
return const_cast<char*>(this->sb_data);
2014-02-01 15:41:11 +01:00
}
return nullptr;
2022-05-11 06:58:32 +02:00
}
2022-05-23 05:44:18 +02:00
string_fragment to_string_fragment(off_t offset, size_t len) const
2022-05-11 06:58:32 +02:00
{
return string_fragment{
this->sb_data, (int) offset, (int) (offset + len)};
}
2014-02-01 15:41:11 +01:00
2022-05-23 05:44:18 +02:00
string_fragment to_string_fragment() const
{
return string_fragment::from_bytes(this->sb_data, this->length());
2022-05-23 05:44:18 +02:00
}
2022-07-29 09:07:11 +02:00
scn::string_view to_string_view(const line_range& lr) const
{
return scn::string_view{
this->get_data_at(lr.lr_start),
this->get_data_at(lr.lr_end),
};
}
2022-08-01 23:56:48 +02:00
using narrow_result = std::pair<const char*, size_t>;
narrow_result narrow(size_t new_data, size_t new_length);
void widen(narrow_result old_data_length);
2022-08-01 23:56:48 +02:00
void share(shared_buffer& sb, const char* data, size_t len);
2014-02-01 15:41:11 +01:00
2022-03-16 23:38:08 +01:00
bool subset(shared_buffer_ref& other, off_t offset, size_t len);
2014-02-01 15:41:11 +01:00
void erase_ansi();
2020-09-19 21:34:44 +02:00
bool take_ownership();
2014-02-01 15:41:11 +01:00
2020-09-19 21:34:44 +02:00
void disown();
2014-02-01 15:41:11 +01:00
private:
2022-03-16 23:38:08 +01:00
void copy_ref(const shared_buffer_ref& other);
2014-02-01 15:41:11 +01:00
2022-03-16 23:38:08 +01:00
auto_mem<char*> sb_backtrace;
file_range::metadata sb_metadata;
2022-03-16 23:38:08 +01:00
shared_buffer* sb_owner;
2022-08-01 23:56:48 +02:00
const char* sb_data;
2014-02-01 15:41:11 +01:00
size_t sb_length;
};
class shared_buffer {
public:
2022-05-11 06:58:32 +02:00
~shared_buffer() { this->invalidate_refs(); }
2014-02-01 15:41:11 +01:00
2022-05-11 06:58:32 +02:00
void add_ref(shared_buffer_ref& ref) { this->sb_refs.push_back(&ref); }
2014-02-01 15:41:11 +01:00
2022-03-16 23:38:08 +01:00
bool invalidate_refs()
{
2014-02-01 15:41:11 +01:00
bool retval = true;
2020-09-19 21:34:44 +02:00
while (!this->sb_refs.empty()) {
auto iter = this->sb_refs.begin();
retval = retval && (*iter)->take_ownership();
2014-02-01 15:41:11 +01:00
}
return retval;
2022-05-11 06:58:32 +02:00
}
2014-02-01 15:41:11 +01:00
2020-09-19 21:34:44 +02:00
std::vector<shared_buffer_ref*> sb_refs;
2014-02-01 15:41:11 +01:00
};
struct tmp_shared_buffer {
2022-03-16 23:38:08 +01:00
explicit tmp_shared_buffer(const char* str, size_t len = -1)
{
if (len == (size_t) -1) {
2014-02-01 15:41:11 +01:00
len = strlen(str);
}
2022-03-16 23:38:08 +01:00
this->tsb_ref.share(this->tsb_manager, (char*) str, len);
2014-02-01 15:41:11 +01:00
};
shared_buffer tsb_manager;
shared_buffer_ref tsb_ref;
};
2022-03-16 23:38:08 +01:00
inline std::string
to_string(const shared_buffer_ref& sbr)
{
return {sbr.get_data(), sbr.length()};
}
2014-02-01 15:41:11 +01:00
#endif