buffer redisign, now its a template

This commit is contained in:
Luna 2025-07-17 22:10:03 +02:00
parent f8f0ef364e
commit ec3d796774
5 changed files with 57 additions and 127 deletions

View file

@ -3,8 +3,9 @@ project(file_read)
set(CMAKE_CXX_STANDARD 23) set(CMAKE_CXX_STANDARD 23)
add_library(file_read STATIC add_library(file_read INTERFACE
src/file_deserialising.h src/file_deserialising.h
src/buffer.cpp
src/buffer.h src/buffer.h
src/file_read.h) src/file_read.h)
set_target_properties(file_read PROPERTIES LINKER_LANGUAGE CXX)

View file

@ -1,96 +0,0 @@
#include "buffer.h"
void buffer::write(char *data_in, size_t data_size)
{
if (data_size > allocated - size)
{
allocated += (size + data_size) + BUFFER_SIZE;
data = (char *)realloc(data, allocated);
allocations++;
if (!data)
{
throw std::runtime_error("Allocation failed");
}
}
std::memcpy(&data[size], data_in, data_size);
size += data_size;
}
void buffer::remove(int offset, int remove_size)
{
if (offset + remove_size > size)
return ;
char *new_data = (char *)calloc(allocated, sizeof(char));
if (!new_data)
{
throw std::runtime_error("Allocation failed");
}
if (offset > 0)
{
std::memcpy(new_data, data, offset);
}
int new_size = size - remove_size;
std::memcpy(&new_data[offset], &data[offset + remove_size], new_size - offset);
free(data);
data = new_data;
size = new_size;
allocations++;
}
void buffer::allocate(size_t s)
{
data = (char *)realloc(data, allocated + s);
if (!data)
{
throw std::runtime_error("Allocation failed");
}
allocated += s;
}
void buffer_unsigned::write(unsigned char *data_in, size_t data_size)
{
if (data_size > allocated - size)
{
allocated += (size + data_size) + BUFFER_SIZE;
data = (unsigned char *)realloc(data, allocated);
allocations++;
if (!data)
{
throw std::runtime_error("Allocation failed");
}
}
std::memcpy(&data[size], data_in, data_size);
size += data_size;
}
void buffer_unsigned::remove(int offset, int remove_size)
{
if (offset + remove_size > size)
return ;
unsigned char *new_data = (unsigned char *)calloc(allocated, sizeof(char));
if (!new_data)
{
throw std::runtime_error("Allocation failed");
}
if (offset > 0)
{
std::memcpy(new_data, data, offset);
}
int new_size = size - remove_size;
std::memcpy(&new_data[offset], &data[offset + remove_size], new_size - offset);
free(data);
data = new_data;
size = new_size;
allocations++;
}
void buffer_unsigned::allocate(size_t s)
{
data = (unsigned char *)realloc(data, allocated + s);
if (!data)
{
throw std::runtime_error("Allocation failed");
}
allocations++;
allocated += s;
}

View file

@ -4,6 +4,7 @@
#include <cstring> #include <cstring>
#define BUFFER_SIZE 1024 #define BUFFER_SIZE 1024
template<typename T>
struct buffer struct buffer
{ {
buffer() buffer()
@ -13,17 +14,16 @@ struct buffer
allocated = 0; allocated = 0;
allocations = 0; allocations = 0;
} }
char *data; T *data;
size_t size; size_t size;
size_t allocated; size_t allocated;
int allocations; int allocations;
void write(char *data_in, size_t data_size); void write(T *data_in, size_t data_size);
void remove(int offset, int remove_size); void remove(int offset, int remove_size);
void allocate(size_t s); void allocate(size_t s);
buffer(const buffer&) = delete; buffer(const buffer&) = delete;
buffer & operator=(const buffer &) = delete;
~buffer() ~buffer()
{ {
if (allocated > 0) if (allocated > 0)
@ -31,28 +31,53 @@ struct buffer
} }
}; };
struct buffer_unsigned template<typename T>
void buffer<T>::write(T *data_in, size_t data_size)
{ {
buffer_unsigned() if (data_size > allocated - size)
{ {
data = nullptr; allocated += (size + data_size) + BUFFER_SIZE;
size = 0; data = (T *)realloc(data, allocated);
allocated = 0; allocations++;
allocations = 0; if (!data)
{
throw std::runtime_error("Allocation failed");
}
} }
unsigned char *data; std::memcpy(&data[size], data_in, data_size);
size_t size; size += data_size;
size_t allocated; }
int allocations;
void write(unsigned char *data_in, size_t data_size); template<typename T>
void remove(int offset, int remove_size); void buffer<T>::remove(int offset, int remove_size)
void allocate(size_t s); {
if (offset + remove_size > size)
buffer_unsigned(const buffer_unsigned&) = delete; return ;
~buffer_unsigned() T *new_data = (T *)calloc(allocated, sizeof(T));
if (!new_data)
{ {
if (allocated > 0) throw std::runtime_error("Allocation failed");
free(data);
} }
}; if (offset > 0)
{
std::memcpy(new_data, data, offset);
}
int new_size = size - remove_size;
std::memcpy(&new_data[offset], &data[offset + remove_size], new_size - offset);
free(data);
data = new_data;
size = new_size;
allocations++;
}
template<typename T>
void buffer<T>::allocate(size_t s)
{
data = (T *)realloc(data, allocated + s);
if (!data)
{
throw std::runtime_error("Allocation failed");
}
allocations++;
allocated += s;
}

View file

@ -28,10 +28,10 @@
struct parsing_buffer struct parsing_buffer
{ {
parsing_buffer(buffer &b) parsing_buffer(buffer<char> &b)
:buf(b) :buf(b)
{} {}
buffer &buf; buffer<char> &buf;
char *point; char *point;
int consumed_size; int consumed_size;
}; };
@ -79,7 +79,7 @@ inline void read_type<double>(double &val, char *data)
} }
template<> template<>
inline void read_type<buffer>(buffer &val, char *data) inline void read_type<buffer<char>>(buffer<char> &val, char *data)
{ {
val.allocated = val.size + 1; val.allocated = val.size + 1;
val.data = (char *)calloc(val.size + 1,sizeof(char)); val.data = (char *)calloc(val.size + 1,sizeof(char));
@ -92,13 +92,13 @@ struct read_var
static void call(T& val, parsing_buffer* v) static void call(T& val, parsing_buffer* v)
{ {
int size = 0; int size = 0;
if constexpr (typeid(T) == typeid(buffer)) if constexpr (typeid(T) == typeid(buffer<char>))
size = val.size; size = val.size;
else else
size = sizeof(T); size = sizeof(T);
if (size + v->consumed_size > v->buf.size) if (size + v->consumed_size > v->buf.size)
{ {
if constexpr (typeid(T) == typeid(buffer)) if constexpr (typeid(T) == typeid(buffer<char>))
{ {
val.size = v->buf.size; val.size = v->buf.size;
} }

View file

@ -79,7 +79,7 @@ class file_reader
size_t total_read_size = 0; size_t total_read_size = 0;
const_for_<size_tuple>([&](auto i) const_for_<size_tuple>([&](auto i)
{ {
if constexpr (typeid(std::get<i.value>(in)) == typeid(buffer)) if constexpr (typeid(std::get<i.value>(in)) == typeid(buffer<char>))
size += std::get<i.value>(in).size; size += std::get<i.value>(in).size;
else else
size += sizeof(std::get<i.value>(in)); size += sizeof(std::get<i.value>(in));
@ -188,7 +188,7 @@ class file_reader
return std::make_pair(size, ret); return std::make_pair(size, ret);
} }
private: private:
buffer buf; buffer<char> buf;
int fd; int fd;
off_t file_size; off_t file_size;
off_t file_size_remaining; off_t file_size_remaining;