feat(internal): add packed_ptr template

This commit is contained in:
Marcus Holland-Moritz 2024-08-23 16:04:25 +02:00
parent 65f5c99592
commit b45d9c1870
3 changed files with 221 additions and 0 deletions

View File

@ -434,6 +434,7 @@ if(WITH_TESTS)
metadata_requirements_test metadata_requirements_test
options_test options_test
packed_int_vector_test packed_int_vector_test
packed_ptr_test
pcm_sample_transformer_test pcm_sample_transformer_test
pcmaudio_categorizer_test pcmaudio_categorizer_test
speedometer_test speedometer_test

View File

@ -0,0 +1,88 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of dwarfs.
*
* dwarfs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dwarfs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <stdexcept>
#include <type_traits>
namespace dwarfs::internal {
namespace detail {
template <typename T>
struct underlying_type {
using type = T;
};
// specialization for enums
template <typename T>
requires std::is_enum_v<T>
struct underlying_type<T> {
using type = std::underlying_type_t<T>;
};
} // namespace detail
template <typename T, size_t DataBits = 3, typename DataType = unsigned>
class packed_ptr {
public:
using data_type = DataType;
static constexpr size_t data_bits = DataBits;
static constexpr size_t data_mask = (1 << data_bits) - 1;
static_assert(
std::unsigned_integral<typename detail::underlying_type<data_type>::type>,
"data_type must be an unsigned integer type");
explicit packed_ptr(T* p = nullptr, data_type data = {})
: p_(build_packed_ptr(p, data)) {}
void set(T* p) { p_ = build_packed_ptr(p, get_data()); }
T* get() const { return reinterpret_cast<T*>(p_ & ~data_mask); }
T* operator->() const { return get(); }
T& operator*() const { return *get(); }
T& operator[](std::ptrdiff_t i) const { return get()[i]; }
data_type get_data() const { return static_cast<data_type>(p_ & data_mask); }
void set_data(data_type data) { p_ = build_packed_ptr(get(), data); }
private:
static uintptr_t build_packed_ptr(T* p, data_type data) {
auto value = reinterpret_cast<uintptr_t>(p);
auto data_value = static_cast<uintptr_t>(data);
if (value & data_mask) {
throw std::invalid_argument("pointer is not aligned");
}
if (data_value & ~data_mask) {
throw std::invalid_argument("data out of bounds");
}
return value | data_value;
}
uintptr_t p_;
};
} // namespace dwarfs::internal

132
test/packed_ptr_test.cpp Normal file
View File

@ -0,0 +1,132 @@
/* vim:set ts=2 sw=2 sts=2 et: */
/**
* \author Marcus Holland-Moritz (github@mhxnet.de)
* \copyright Copyright (c) Marcus Holland-Moritz
*
* This file is part of dwarfs.
*
* dwarfs is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* dwarfs is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
*/
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <dwarfs/internal/packed_ptr.h>
using namespace dwarfs::internal;
TEST(packed_ptr, initialize) {
{
packed_ptr<uint32_t> pp;
EXPECT_EQ(pp.get(), nullptr);
EXPECT_EQ(pp.get_data(), 0);
}
{
alignas(8) uint32_t i = 42;
packed_ptr<uint32_t> pp(&i);
EXPECT_EQ(pp.get(), &i);
EXPECT_EQ(pp.get_data(), 0);
}
{
packed_ptr<uint32_t> pp(nullptr, 0x7);
EXPECT_EQ(pp.get(), nullptr);
EXPECT_EQ(pp.get_data(), 0x7);
}
{
alignas(8) uint32_t i = 42;
packed_ptr<uint32_t> pp(&i, 0x7);
EXPECT_EQ(pp.get(), &i);
EXPECT_EQ(pp.get_data(), 0x7);
EXPECT_EQ(*pp, 42);
}
EXPECT_THAT(
[] { packed_ptr<uint32_t> pp(nullptr, 0x8); },
testing::ThrowsMessage<std::invalid_argument>("data out of bounds"));
EXPECT_THAT(
[] {
packed_ptr<uint32_t> pp(reinterpret_cast<uint32_t*>(0x100004), 0x7);
},
testing::ThrowsMessage<std::invalid_argument>("pointer is not aligned"));
}
TEST(packed_ptr, integral) {
using ptr_type = std::pair<int32_t, float>;
packed_ptr<ptr_type, 2, uint8_t> pp;
static_assert(std::is_same_v<decltype(pp.get_data()), uint8_t>);
static_assert(std::is_same_v<decltype(pp.get()), ptr_type*>);
static_assert(std::is_same_v<decltype(*pp), ptr_type&>);
static_assert(std::is_same_v<decltype(pp[10]), ptr_type&>);
static_assert(std::is_same_v<decltype(pp->first), int>);
static_assert(std::is_same_v<decltype(pp->second), float>);
alignas(4) ptr_type p = {42, 2.0f};
pp.set(&p);
EXPECT_EQ(pp.get(), &p);
EXPECT_EQ(pp.get_data(), 0);
EXPECT_EQ(pp->first, 42);
EXPECT_EQ(pp->second, 2.0f);
EXPECT_EQ(pp[0].first, 42);
EXPECT_EQ(pp[0].second, 2.0f);
EXPECT_THAT(
[&] { pp.set_data(0x4); },
testing::ThrowsMessage<std::invalid_argument>("data out of bounds"));
pp.set_data(0x3);
EXPECT_EQ(pp.get(), &p);
EXPECT_EQ(pp.get_data(), 0x3);
EXPECT_THAT(
[&] { pp.set(reinterpret_cast<ptr_type*>(0x100001)); },
testing::ThrowsMessage<std::invalid_argument>("pointer is not aligned"));
}
TEST(packed_ptr, enumeration) {
using ptr_type = std::pair<int32_t, float>;
enum class test_enum : unsigned { A = 1, B, C, D };
packed_ptr<ptr_type, 2, test_enum> pp;
static_assert(std::is_same_v<decltype(pp.get_data()), test_enum>);
static_assert(std::is_same_v<decltype(pp.get()), ptr_type*>);
static_assert(std::is_same_v<decltype(*pp), ptr_type&>);
static_assert(std::is_same_v<decltype(pp[10]), ptr_type&>);
static_assert(std::is_same_v<decltype(pp->first), int>);
alignas(4) ptr_type p = {42, 2.0f};
pp.set(&p);
EXPECT_EQ(pp.get(), &p);
EXPECT_EQ(pp.get_data(), static_cast<test_enum>(0));
pp.set_data(test_enum::B);
EXPECT_EQ(pp.get(), &p);
EXPECT_EQ(pp.get_data(), test_enum::B);
EXPECT_THAT(
[&] { pp.set_data(test_enum::D); },
testing::ThrowsMessage<std::invalid_argument>("data out of bounds"));
}