367 lines
6.9 KiB
C++
367 lines
6.9 KiB
C++
/**
|
|
* @file dl_buffer.h
|
|
* @brief 表示一段内存
|
|
*
|
|
* @version 1.0
|
|
* @author lveyou
|
|
* @date 23-03-17
|
|
*
|
|
* @note 使用malloc和free
|
|
* @note 废弃,改为vector<uint8_t>
|
|
* @note Ref为引用,View为视图
|
|
*/
|
|
#pragma once
|
|
|
|
#include <vector>
|
|
#include <string>
|
|
#include <span>
|
|
#include <cstring>
|
|
|
|
#include "base/dl_concept.h"
|
|
#include "io/dl_log.h"
|
|
#include "memory/dl_buffer_pool.h"
|
|
|
|
namespace dl
|
|
{
|
|
constexpr size_t BUFFER_SSO_SIZE = 16;
|
|
|
|
class Buffer
|
|
{
|
|
public:
|
|
std::byte* data() { return _data ? _data : _ssoData; }
|
|
const std::byte* data() const { return _data ? _data : _ssoData; }
|
|
|
|
size_t size() const { return _length; }
|
|
|
|
void resize(size_t len)
|
|
{
|
|
if (!_data)
|
|
{
|
|
if (len <= BUFFER_SSO_SIZE)
|
|
{
|
|
_length = len;
|
|
return;
|
|
}
|
|
|
|
if (_length == 0)
|
|
{
|
|
_data = g_bufferPool.Malloc(len, _index, _size);
|
|
_length = len;
|
|
}
|
|
else
|
|
{
|
|
std::byte temp[BUFFER_SSO_SIZE];
|
|
memcpy(temp, _ssoData, _length);
|
|
|
|
_data = g_bufferPool.Malloc(len, _index, _size);
|
|
memset(_data, 0, len);
|
|
memcpy(_data, temp, _length);
|
|
_length = len;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (len <= _size)
|
|
{
|
|
_length = len;
|
|
return;
|
|
}
|
|
size_t old_length = _length;
|
|
_length = len;
|
|
_data = g_bufferPool.Realloc(_data, _length, _index, _size);
|
|
memset(_data + old_length, 0, _length - old_length);
|
|
}
|
|
}
|
|
void reserve(size_t size)
|
|
{
|
|
size_t len = _length;
|
|
resize(size);
|
|
_length = len;
|
|
}
|
|
bool empty() { return _length == 0; }
|
|
|
|
void clear()
|
|
{
|
|
_length = 0;
|
|
}
|
|
auto begin() { return _data ? _data : _ssoData; }
|
|
auto end() { return (_data ? _data : _ssoData) + _length; }
|
|
|
|
void append(void* p, size_t len)
|
|
{
|
|
if (p == nullptr || len == 0)
|
|
return;
|
|
size_t old_length = _length;
|
|
resize(_length + len);
|
|
memcpy(data() + old_length, p, len);
|
|
}
|
|
|
|
std::byte& operator[](size_t index)
|
|
{
|
|
return data()[index];
|
|
}
|
|
|
|
Buffer& operator=(const Buffer& b)
|
|
{
|
|
resize(b._length);
|
|
memcpy(data(), b.data(), b.size());
|
|
return *this;
|
|
}
|
|
|
|
Buffer() :
|
|
_data{ nullptr },
|
|
_length{0}
|
|
{
|
|
memset(_ssoData, 0, BUFFER_SSO_SIZE);
|
|
}
|
|
|
|
Buffer(size_t len) : Buffer()
|
|
{
|
|
resize(len);
|
|
}
|
|
|
|
Buffer(const Buffer& b) : Buffer()
|
|
{
|
|
resize(b._length);
|
|
memcpy(data(), b.data(), b.size());
|
|
}
|
|
|
|
Buffer(const std::byte* beg, const std::byte* end) : Buffer()
|
|
{
|
|
resize(end - beg);
|
|
memcpy(data(), beg, _length);
|
|
}
|
|
|
|
~Buffer()
|
|
{
|
|
if (!_data)
|
|
return;
|
|
g_bufferPool.Free(_data, _index, _size);
|
|
}
|
|
private:
|
|
std::byte* _data;
|
|
size_t _length;
|
|
union
|
|
{
|
|
struct
|
|
{
|
|
size_t _index; // 池索引
|
|
size_t _size; // 实际大小
|
|
};
|
|
std::byte _ssoData[BUFFER_SSO_SIZE];
|
|
};
|
|
};
|
|
|
|
class BufferView
|
|
{
|
|
public:
|
|
std::byte* data() { return _data; }
|
|
const std::byte* data() const { return _data; }
|
|
|
|
size_t size() const { return _length; }
|
|
|
|
bool empty() { return _length == 0; }
|
|
|
|
auto begin() { return _data; }
|
|
auto end() { return _data + _length; }
|
|
|
|
std::byte& operator[](size_t index)
|
|
{
|
|
return _data[index];
|
|
}
|
|
|
|
BufferView() :
|
|
_data{ nullptr },
|
|
_length{ 0 }
|
|
{}
|
|
|
|
BufferView(const std::byte* beg, const std::byte* end)
|
|
{
|
|
_length = end - beg;
|
|
_data = (std::byte*)beg;
|
|
}
|
|
|
|
BufferView(const std::byte* beg, size_t len)
|
|
{
|
|
_length = len;
|
|
_data = (std::byte*)beg;
|
|
}
|
|
|
|
BufferView(const Buffer& b)
|
|
{
|
|
_data = (std::byte*)b.data();
|
|
_length = b.size();
|
|
}
|
|
private:
|
|
std::byte* _data;
|
|
size_t _length;
|
|
};
|
|
|
|
//! 创建buffer视图(右值不能默认转换)
|
|
inline BufferView CreateBufferView(const Buffer& buf)
|
|
{
|
|
return buf;
|
|
}
|
|
|
|
// Buffer -> BufferView 隐式转换,要求buf不是右值
|
|
// BufferView -> Buffer 使用 CreateBuffer{ buf_view }
|
|
|
|
|
|
|
|
//! 创建buffer视图
|
|
inline BufferView CreateBufferView(std::string_view str)
|
|
{
|
|
return { (std::byte*)str.data(), str.size() };
|
|
}
|
|
//! 创建buffer视图
|
|
inline BufferView CreateBufferView(char* p, size_t i)
|
|
{
|
|
return { (std::byte*)p, i };
|
|
}
|
|
inline BufferView CreateBufferView(unsigned char* p, size_t i)
|
|
{
|
|
return { (std::byte*)p, i };
|
|
}
|
|
inline BufferView CreateBufferView(BufferView buf, size_t i)
|
|
{
|
|
return { buf.data(), i };
|
|
}
|
|
template<typename T, size_t N>
|
|
BufferView CreateBufferView(const std::array<T, N>& v)
|
|
{
|
|
return { (std::byte*)v.data(), v.size() };
|
|
}
|
|
template<typename T>
|
|
BufferView CreateBufferView(const std::vector<T>& v)
|
|
{
|
|
return { (std::byte*)v.data(), v.size() };
|
|
}
|
|
|
|
template<IsPod T>
|
|
BufferView CreateBufferView(const T& a)
|
|
{
|
|
return { (std::byte*)&a, (std::byte*)(&a + 1) };
|
|
}
|
|
|
|
//! 追加数据
|
|
inline void BufferAppend(Buffer& buf, BufferView buf_app)
|
|
{
|
|
size_t size_old = buf.size();
|
|
size_t size_app = buf_app.size();
|
|
buf.resize(size_old + size_app);
|
|
memcpy(buf.data() + size_old, buf_app.data(), size_app);
|
|
}
|
|
|
|
//! 创建副本
|
|
inline Buffer CreateBuffer(BufferView buf)
|
|
{
|
|
return { buf.data(), buf.data() + buf.size() };
|
|
}
|
|
|
|
//! 从16进制字符串创建buffer
|
|
Buffer CreateBufferHex(std::string_view str);
|
|
|
|
//! 从16进制字符串创建array
|
|
//template<size_t N>
|
|
//std::array<std::byte, N> CreateArrayHex(std::string_view str)
|
|
//{
|
|
// std::array<std::byte, N> ret = {};
|
|
// Buffer buf = CreateBufferHex(str);
|
|
// if (buf.size() != N)
|
|
// log_warn("长度不匹配!");
|
|
// memcpy(ret.data(), buf.data(), (std::min)(buf.size(), N));
|
|
// return ret;
|
|
//}
|
|
//
|
|
//template<size_t N>
|
|
//void from_string_hex(std::string_view str, std::array<std::byte, N>& arr)
|
|
//{
|
|
// arr = CreateArrayHex<N>(str);
|
|
//}
|
|
|
|
template<IsPod T>
|
|
inline void BufferCast(BufferView buf, T& a)
|
|
{
|
|
if (sizeof(T) != buf.size())
|
|
{
|
|
log_err("数据大小不匹配:{}->{}", buf.size(), sizeof(T));
|
|
return;
|
|
}
|
|
memcpy(&a, buf.data(), sizeof(T));
|
|
}
|
|
|
|
//! 弱引用(设计为可修改,毕竟直接引用指针)
|
|
//struct BufferRef
|
|
//{
|
|
// BufferRef(Buffer& buf)
|
|
// {
|
|
// _p = buf.data();
|
|
// _size = buf.size();
|
|
// }
|
|
// BufferRef(unsigned char* p, size_t size)
|
|
// {
|
|
// _p = (Byte*)p;
|
|
// _size = size;
|
|
// }
|
|
// BufferRef(std::vector<char>& vec)
|
|
// {
|
|
// _p = (Byte*)vec.data();
|
|
// _size = vec.size();
|
|
// }
|
|
//
|
|
// Byte* data() { return _p; }
|
|
// size_t size() { return _size; }
|
|
// bool empty() { return _size == 0 || !_p; }
|
|
//private:
|
|
// Byte* _p;
|
|
// size_t _size;
|
|
//};
|
|
//
|
|
////! Buffer转16进制
|
|
//std::string toString(const Buffer& buf, bool add_tag = false);
|
|
//
|
|
//
|
|
//Buffer createBuffer(unsigned char* p, size_t size);
|
|
//Buffer createBuffer(std::vector<char>& vec);
|
|
//Buffer createBuffer(const std::string& path_name);
|
|
//
|
|
////! 弱引用不可修改
|
|
//struct BufferView
|
|
//{
|
|
// BufferView(BufferRef buf)
|
|
// {
|
|
// _p = buf.data();
|
|
// _size = buf.size();
|
|
// }
|
|
// BufferView(Buffer& buf)
|
|
// {
|
|
// _p = buf.data();
|
|
// _size = buf.size();
|
|
// }
|
|
// BufferView(const unsigned char* p, size_t size)
|
|
// {
|
|
// _p = (const Byte*)p;
|
|
// _size = size;
|
|
// }
|
|
// BufferView(const std::vector<char>& vec)
|
|
// {
|
|
// _p = (const Byte*)vec.data();
|
|
// _size = vec.size();
|
|
// }
|
|
// BufferView(const std::string& str)
|
|
// {
|
|
// _p = (const Byte*)str.data();
|
|
// _size = str.size();
|
|
// }
|
|
//
|
|
// const Byte* data() { return _p; }
|
|
// size_t size() { return _size; }
|
|
// bool empty() { return _size == 0 || !_p; }
|
|
//private:
|
|
// const Byte* _p;
|
|
// size_t _size;
|
|
//};
|
|
|
|
}
|
|
|