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

fs: use Spanner to refer to ioctl arg buffer instead of making vector copies

This avoids some allocations and copying.

Signed-off-by: Zygo Blaxell <bees@furryterror.org>
This commit is contained in:
Zygo Blaxell 2020-11-05 00:40:54 -05:00
parent 333aea9822
commit c0149d72b7
2 changed files with 11 additions and 9 deletions

View File

@ -2,6 +2,7 @@
#define CRUCIBLE_FS_H #define CRUCIBLE_FS_H
#include "crucible/error.h" #include "crucible/error.h"
#include "crucible/spanner.h"
// Terribly Linux-specific FS-wrangling functions // Terribly Linux-specific FS-wrangling functions
@ -164,7 +165,7 @@ namespace crucible {
struct BtrfsIoctlSearchHeader : public btrfs_ioctl_search_header { struct BtrfsIoctlSearchHeader : public btrfs_ioctl_search_header {
BtrfsIoctlSearchHeader(); BtrfsIoctlSearchHeader();
vector<uint8_t> m_data; Spanner<const uint8_t> m_data;
size_t set_data(const vector<uint8_t> &v, size_t offset); size_t set_data(const vector<uint8_t> &v, size_t offset);
bool operator<(const BtrfsIoctlSearchHeader &that) const; bool operator<(const BtrfsIoctlSearchHeader &that) const;
}; };
@ -187,6 +188,7 @@ namespace crucible {
void next_min(const BtrfsIoctlSearchHeader& ref); void next_min(const BtrfsIoctlSearchHeader& ref);
size_t m_buf_size; size_t m_buf_size;
vector<uint8_t> m_ioctl_arg;
set<BtrfsIoctlSearchHeader> m_result; set<BtrfsIoctlSearchHeader> m_result;
}; };

View File

@ -752,7 +752,7 @@ namespace crucible {
*static_cast<btrfs_ioctl_search_header *>(this) = *reinterpret_cast<const btrfs_ioctl_search_header *>(&v[offset]); *static_cast<btrfs_ioctl_search_header *>(this) = *reinterpret_cast<const btrfs_ioctl_search_header *>(&v[offset]);
offset += sizeof(btrfs_ioctl_search_header); offset += sizeof(btrfs_ioctl_search_header);
THROW_CHECK2(invalid_argument, offset + len, v.size(), offset + len <= v.size()); THROW_CHECK2(invalid_argument, offset + len, v.size(), offset + len <= v.size());
m_data = vector<uint8_t>(&v[offset], &v[offset + len]); m_data = Spanner<const uint8_t>(&v[offset], &v[offset + len]);
return offset + len; return offset + len;
} }
@ -765,15 +765,15 @@ namespace crucible {
// Keep the ioctl buffer from one run to the next to save on malloc costs // 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; size_t target_buf_size = sizeof(btrfs_ioctl_search_args_v2) + m_buf_size;
vector<uint8_t> ioctl_arg = vector_copy_struct<btrfs_ioctl_search_key>(this); m_ioctl_arg = vector_copy_struct<btrfs_ioctl_search_key>(this);
ioctl_arg.resize(target_buf_size); m_ioctl_arg.resize(target_buf_size);
btrfs_ioctl_search_args_v2 *ioctl_ptr = reinterpret_cast<btrfs_ioctl_search_args_v2 *>(ioctl_arg.data());
ioctl_ptr->buf_size = m_buf_size;
m_result.clear(); m_result.clear();
btrfs_ioctl_search_args_v2 *ioctl_ptr = reinterpret_cast<btrfs_ioctl_search_args_v2 *>(m_ioctl_arg.data());
ioctl_ptr->buf_size = m_buf_size;
// Don't bother supporting V1. Kernels that old have other problems. // Don't bother supporting V1. Kernels that old have other problems.
int rv = ioctl(fd, BTRFS_IOC_TREE_SEARCH_V2, ioctl_ptr); int rv = ioctl(fd, BTRFS_IOC_TREE_SEARCH_V2, ioctl_ptr);
if (rv != 0) { if (rv != 0) {
@ -785,7 +785,7 @@ namespace crucible {
size_t offset = pointer_distance(ioctl_ptr->buf, ioctl_ptr); size_t offset = pointer_distance(ioctl_ptr->buf, ioctl_ptr);
for (decltype(nr_items) i = 0; i < nr_items; ++i) { for (decltype(nr_items) i = 0; i < nr_items; ++i) {
BtrfsIoctlSearchHeader item; BtrfsIoctlSearchHeader item;
offset = item.set_data(ioctl_arg, offset); offset = item.set_data(m_ioctl_arg, offset);
m_result.insert(item); m_result.insert(item);
} }