From f45e379802b11dc26bf37c3d0de6f2e231f3f143 Mon Sep 17 00:00:00 2001 From: Zygo Blaxell Date: Wed, 4 Nov 2020 21:02:37 -0500 Subject: [PATCH] fs: deprecate vector Use uint8_t when we mean uint8_t, i.e. vector instead of vector. Add a template parameter instead of vector so we can swap in a non-copying data type. Signed-off-by: Zygo Blaxell --- include/crucible/fs.h | 28 ++++++++++++++-------------- include/crucible/string.h | 10 +++++----- lib/fs.cc | 18 ++++++++++-------- 3 files changed, 29 insertions(+), 27 deletions(-) diff --git a/include/crucible/fs.h b/include/crucible/fs.h index 0e7e04d..25e3eda 100644 --- a/include/crucible/fs.h +++ b/include/crucible/fs.h @@ -61,7 +61,7 @@ namespace crucible { decltype(elem_cnt) get_elem_cnt() const; decltype(elem_missed) get_elem_missed() const; - vector m_data; + vector 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 m_data; - size_t set_data(const vector &v, size_t offset); + vector m_data; + size_t set_data(const vector &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 + template const T* - get_struct_ptr(vector &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(v.data() + offset); } - template + template R - call_btrfs_get(R (*func)(const A*), vector &v, size_t offset = 0) + call_btrfs_get(R (*func)(const A*), const V &v, size_t offset = 0) { - return func(get_struct_ptr(v, offset)); + return func(get_struct_ptr(v, offset)); } template struct btrfs_get_le; @@ -237,13 +237,13 @@ namespace crucible { uint8_t operator()(const void *p) { return get_unaligned_le8(p); } }; - template + template T - btrfs_get_member(T S::* member, vector &v, size_t offset = 0) + btrfs_get_member(T S::* member, V &v, size_t offset = 0) { - const S *sp = reinterpret_cast(NULL); - const T *spm = &(sp->*member); - auto member_offset = reinterpret_cast(spm) - reinterpret_cast(sp); + const S *const sp = nullptr; + const T *const spm = &(sp->*member); + auto member_offset = reinterpret_cast(spm) - reinterpret_cast(sp); return btrfs_get_le()(get_struct_ptr(v, offset + member_offset)); } @@ -256,7 +256,7 @@ namespace crucible { unsigned long available() const; }; - ostream &hexdump(ostream &os, const vector &v); + template ostream &hexdump(ostream &os, const V &v); struct BtrfsIoctlFsInfoArgs : public btrfs_ioctl_fs_info_args_v2 { BtrfsIoctlFsInfoArgs(); diff --git a/include/crucible/string.h b/include/crucible/string.h index 94fc724..3c20eda 100644 --- a/include/crucible/string.h +++ b/include/crucible/string.h @@ -19,13 +19,13 @@ namespace crucible { memset(that, 0, sizeof(Base)); } - // Copy a base class object (usually a C struct) into a vector + // Copy a base class object (usually a C struct) into a vector template - vector + vector vector_copy_struct(Base *that) { - const char *begin_that = reinterpret_cast(static_cast(that)); - return vector(begin_that, begin_that + sizeof(Base)); + const uint8_t *begin_that = reinterpret_cast(static_cast(that)); + return vector(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(a) - reinterpret_cast(b); + return reinterpret_cast(a) - reinterpret_cast(b); } }; diff --git a/lib/fs.cc b/lib/fs.cc index 401d1db..62c3403 100644 --- a/lib/fs.cc +++ b/lib/fs.cc @@ -140,7 +140,7 @@ namespace crucible { BtrfsExtentSame::do_ioctl() { dest_count = m_info.size(); - vector ioctl_arg = vector_copy_struct(this); + vector ioctl_arg = vector_copy_struct(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(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 ioctl_arg = vector_copy_struct(this); + vector ioctl_arg = vector_copy_struct(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 &v, size_t offset) + BtrfsIoctlSearchHeader::set_data(const vector &v, size_t offset) { THROW_CHECK2(invalid_argument, offset, v.size(), offset + sizeof(btrfs_ioctl_search_header) <= v.size()); *static_cast(this) = *reinterpret_cast(&v[offset]); offset += sizeof(btrfs_ioctl_search_header); THROW_CHECK2(invalid_argument, offset + len, v.size(), offset + len <= v.size()); - m_data = vector(&v[offset], &v[offset + len]); + m_data = vector(&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 ioctl_arg = vector_copy_struct(this); + vector ioctl_arg = vector_copy_struct(this); ioctl_arg.resize(target_buf_size); btrfs_ioctl_search_args_v2 *ioctl_ptr = reinterpret_cast(ioctl_arg.data()); @@ -812,14 +812,16 @@ namespace crucible { } } - ostream &hexdump(ostream &os, const vector &v) + template + ostream & + hexdump(ostream &os, const V &v) { - os << "vector { size = " << v.size() << ", data:\n"; + os << "vector { 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;