118 lines
2.7 KiB
C++
118 lines
2.7 KiB
C++
#include <fxos/util.h>
|
|
#include <filesystem>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <ctime>
|
|
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
#include <sys/stat.h>
|
|
|
|
namespace fs = std::filesystem;
|
|
|
|
Buffer::Buffer():
|
|
size {0}, data {nullptr}, path {"(none)"}
|
|
{
|
|
}
|
|
|
|
/* Empty buffer initialized with given byte */
|
|
Buffer::Buffer(size_t bufsize, int fill)
|
|
{
|
|
size = bufsize;
|
|
char *buffer = new char[size];
|
|
memset(buffer, fill, size);
|
|
|
|
data = std::shared_ptr<char>(buffer);
|
|
path = "(anonymous)";
|
|
}
|
|
|
|
/* Buffer initialized from file */
|
|
Buffer::Buffer(std::string filepath, ssize_t bufsize, int fill)
|
|
{
|
|
char const *path = filepath.c_str();
|
|
|
|
int fd = open(path, O_RDONLY);
|
|
if(!fd) throw std::runtime_error(format("cannot open '%s'", path));
|
|
|
|
struct stat statbuf;
|
|
ssize_t x = fstat(fd, &statbuf);
|
|
|
|
if(x < 0)
|
|
{
|
|
close(fd);
|
|
throw std::runtime_error(format("cannot stat '%s'", path));
|
|
}
|
|
|
|
size = (bufsize < 0) ? statbuf.st_size : bufsize;
|
|
size_t size_to_read = std::min(size, (size_t)statbuf.st_size);
|
|
|
|
/* Read buffer and fill whatever is left */
|
|
data = std::shared_ptr<char>(new char[size]);
|
|
memset(data.get(), fill, size);
|
|
x = read(fd, data.get(), size_to_read);
|
|
|
|
close(fd);
|
|
if(x != (ssize_t)size_to_read)
|
|
{
|
|
throw std::runtime_error(format(
|
|
"error while reading '%s'", path));
|
|
}
|
|
|
|
this->path = filepath;
|
|
}
|
|
|
|
Buffer::Buffer(std::string filepath, std::vector<std::string> &folders,
|
|
ssize_t size, int fill):
|
|
Buffer()
|
|
{
|
|
for(auto &f: folders)
|
|
{
|
|
fs::path p = fs::path(f) / fs::path(filepath);
|
|
if(fs::exists(p))
|
|
{
|
|
/* Hopefully this will use the move assignment */
|
|
*this = Buffer(p, size, fill);
|
|
return;
|
|
}
|
|
}
|
|
|
|
char const *path = filepath.c_str();
|
|
throw std::runtime_error(format("cannot find '%s' in library", path));
|
|
}
|
|
|
|
/* Create a buffer by copying and resizing another buffer */
|
|
Buffer::Buffer(Buffer const &other, size_t new_size, int fill):
|
|
Buffer(new_size, fill)
|
|
{
|
|
memcpy(data.get(), other.data.get(), std::min(new_size, other.size));
|
|
}
|
|
|
|
/* Generic timer which returns times in ns using CLOCK_REALTIME */
|
|
struct timespec timer_start(void)
|
|
{
|
|
struct timespec start;
|
|
clock_gettime(CLOCK_REALTIME, &start);
|
|
return start;
|
|
}
|
|
long long timer_end(struct timespec start)
|
|
{
|
|
struct timespec end;
|
|
clock_gettime(CLOCK_REALTIME, &end);
|
|
|
|
long long ns = 1000000000 * (end.tv_sec - start.tv_sec);
|
|
ns += end.tv_nsec - start.tv_nsec;
|
|
return ns;
|
|
}
|
|
|
|
/* Format ns durations to us, ms or s depending on the value */
|
|
std::string timer_format(long long duration)
|
|
{
|
|
if(duration < 2000) return format("%lld ns", duration);
|
|
duration /= 1000;
|
|
if(duration < 2000) return format("%lld us", duration);
|
|
duration /= 1000;
|
|
if(duration < 2000) return format("%lld ms", duration);
|
|
duration /= 1000;
|
|
return format("%lld s", duration);
|
|
}
|