1
0
mirror of https://github.com/Zygo/bees.git synced 2025-05-17 13:25:45 +02:00

fs: deprecate vector<char>

Use uint8_t when we mean uint8_t, i.e. vector<uint8_t> instead of
vector<char>.

Add a template parameter instead of vector so we can swap in a
non-copying data type.

Signed-off-by: Zygo Blaxell <bees@furryterror.org>
This commit is contained in:
Zygo Blaxell 2020-11-04 21:02:37 -05:00
parent 180bb60cde
commit f45e379802
3 changed files with 29 additions and 27 deletions

View File

@ -61,7 +61,7 @@ namespace crucible {
decltype(elem_cnt) get_elem_cnt() const;
decltype(elem_missed) get_elem_missed() const;
vector<char> m_data;
vector<uint8_t> m_data;
};
struct BtrfsIoctlLogicalInoArgs : public btrfs_ioctl_logical_ino_args {
@ -164,8 +164,8 @@ namespace crucible {
struct BtrfsIoctlSearchHeader : public btrfs_ioctl_search_header {
BtrfsIoctlSearchHeader();
vector<char> m_data;
size_t set_data(const vector<char> &v, size_t offset);
vector<uint8_t> m_data;
size_t set_data(const vector<uint8_t> &v, size_t offset);
bool operator<(const BtrfsIoctlSearchHeader &that) const;
};
@ -200,9 +200,9 @@ namespace crucible {
uint64_t btrfs_get_root_id(int fd);
uint64_t btrfs_get_root_transid(int fd);
template<class T>
template<class T, class V>
const T*
get_struct_ptr(vector<char> &v, size_t offset = 0)
get_struct_ptr(const V &v, size_t offset = 0)
{
// OK so sometimes btrfs overshoots a little
if (offset + sizeof(T) > v.size()) {
@ -212,11 +212,11 @@ namespace crucible {
return reinterpret_cast<const T*>(v.data() + offset);
}
template<class A, class R>
template<class A, class R, class V>
R
call_btrfs_get(R (*func)(const A*), vector<char> &v, size_t offset = 0)
call_btrfs_get(R (*func)(const A*), const V &v, size_t offset = 0)
{
return func(get_struct_ptr<A>(v, offset));
return func(get_struct_ptr<A, V>(v, offset));
}
template <class T> struct btrfs_get_le;
@ -237,13 +237,13 @@ namespace crucible {
uint8_t operator()(const void *p) { return get_unaligned_le8(p); }
};
template<class S, class T>
template<class S, class T, class V>
T
btrfs_get_member(T S::* member, vector<char> &v, size_t offset = 0)
btrfs_get_member(T S::* member, V &v, size_t offset = 0)
{
const S *sp = reinterpret_cast<const S*>(NULL);
const T *spm = &(sp->*member);
auto member_offset = reinterpret_cast<const char *>(spm) - reinterpret_cast<const char *>(sp);
const S *const sp = nullptr;
const T *const spm = &(sp->*member);
auto member_offset = reinterpret_cast<const uint8_t *>(spm) - reinterpret_cast<const uint8_t *>(sp);
return btrfs_get_le<T>()(get_struct_ptr<S>(v, offset + member_offset));
}
@ -256,7 +256,7 @@ namespace crucible {
unsigned long available() const;
};
ostream &hexdump(ostream &os, const vector<char> &v);
template<class V> ostream &hexdump(ostream &os, const V &v);
struct BtrfsIoctlFsInfoArgs : public btrfs_ioctl_fs_info_args_v2 {
BtrfsIoctlFsInfoArgs();

View File

@ -19,13 +19,13 @@ namespace crucible {
memset(that, 0, sizeof(Base));
}
// Copy a base class object (usually a C struct) into a vector<char>
// Copy a base class object (usually a C struct) into a vector<uint8_t>
template <class Base>
vector<char>
vector<uint8_t>
vector_copy_struct(Base *that)
{
const char *begin_that = reinterpret_cast<const char *>(static_cast<const Base *>(that));
return vector<char>(begin_that, begin_that + sizeof(Base));
const uint8_t *begin_that = reinterpret_cast<const uint8_t *>(static_cast<const Base *>(that));
return vector<uint8_t>(begin_that, begin_that + sizeof(Base));
}
// int->hex conversion with sprintf
@ -60,7 +60,7 @@ namespace crucible {
ptrdiff_t
pointer_distance(const P1 *a, const P2 *b)
{
return reinterpret_cast<const char *>(a) - reinterpret_cast<const char *>(b);
return reinterpret_cast<const uint8_t *>(a) - reinterpret_cast<const uint8_t *>(b);
}
};

View File

@ -140,7 +140,7 @@ namespace crucible {
BtrfsExtentSame::do_ioctl()
{
dest_count = m_info.size();
vector<char> ioctl_arg = vector_copy_struct<btrfs_ioctl_same_args>(this);
vector<uint8_t> ioctl_arg = vector_copy_struct<btrfs_ioctl_same_args>(this);
ioctl_arg.resize(sizeof(btrfs_ioctl_same_args) + dest_count * sizeof(btrfs_ioctl_same_extent_info), 0);
btrfs_ioctl_same_args *ioctl_ptr = reinterpret_cast<btrfs_ioctl_same_args *>(ioctl_arg.data());
size_t count = 0;
@ -677,7 +677,7 @@ namespace crucible {
THROW_CHECK1(out_of_range, m_min_count, m_min_count <= m_max_count);
auto extent_count = m_min_count;
vector<char> ioctl_arg = vector_copy_struct<fiemap>(this);
vector<uint8_t> ioctl_arg = vector_copy_struct<fiemap>(this);
ioctl_arg.resize(sizeof(fiemap) + extent_count * sizeof(fiemap_extent), 0);
@ -746,13 +746,13 @@ namespace crucible {
}
size_t
BtrfsIoctlSearchHeader::set_data(const vector<char> &v, size_t offset)
BtrfsIoctlSearchHeader::set_data(const vector<uint8_t> &v, size_t offset)
{
THROW_CHECK2(invalid_argument, offset, v.size(), offset + sizeof(btrfs_ioctl_search_header) <= v.size());
*static_cast<btrfs_ioctl_search_header *>(this) = *reinterpret_cast<const btrfs_ioctl_search_header *>(&v[offset]);
offset += sizeof(btrfs_ioctl_search_header);
THROW_CHECK2(invalid_argument, offset + len, v.size(), offset + len <= v.size());
m_data = vector<char>(&v[offset], &v[offset + len]);
m_data = vector<uint8_t>(&v[offset], &v[offset + len]);
return offset + len;
}
@ -765,7 +765,7 @@ namespace crucible {
// Keep the ioctl buffer from one run to the next to save on malloc costs
size_t target_buf_size = sizeof(btrfs_ioctl_search_args_v2) + m_buf_size;
vector<char> ioctl_arg = vector_copy_struct<btrfs_ioctl_search_key>(this);
vector<uint8_t> ioctl_arg = vector_copy_struct<btrfs_ioctl_search_key>(this);
ioctl_arg.resize(target_buf_size);
btrfs_ioctl_search_args_v2 *ioctl_ptr = reinterpret_cast<btrfs_ioctl_search_args_v2 *>(ioctl_arg.data());
@ -812,14 +812,16 @@ namespace crucible {
}
}
ostream &hexdump(ostream &os, const vector<char> &v)
template <class V>
ostream &
hexdump(ostream &os, const V &v)
{
os << "vector<char> { size = " << v.size() << ", data:\n";
os << "vector<uint8_t> { size = " << v.size() << ", data:\n";
for (size_t i = 0; i < v.size(); i += 8) {
string hex, ascii;
for (size_t j = i; j < i + 8; ++j) {
if (j < v.size()) {
unsigned char c = v[j];
uint8_t c = v[j];
char buf[8];
sprintf(buf, "%02x ", c);
hex += buf;