| 1 | // Copyright (c) 2018-2022, The Monero Project |
| 2 | |
| 3 | // All rights reserved. |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without modification, are |
| 6 | // permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // 1. Redistributions of source code must retain the above copyright notice, this list of |
| 9 | // conditions and the following disclaimer. |
| 10 | // |
| 11 | // 2. Redistributions in binary form must reproduce the above copyright notice, this list |
| 12 | // of conditions and the following disclaimer in the documentation and/or other |
| 13 | // materials provided with the distribution. |
| 14 | // |
| 15 | // 3. Neither the name of the copyright holder nor the names of its contributors may be |
| 16 | // used to endorse or promote products derived from this software without specific |
| 17 | // prior written permission. |
| 18 | // |
| 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY |
| 20 | // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
| 21 | // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL |
| 22 | // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 24 | // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 26 | // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF |
| 27 | // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | #pragma once |
| 29 | |
| 30 | #include <cstddef> |
| 31 | #include <cstring> |
| 32 | #include <lmdb.h> |
| 33 | #include <type_traits> |
| 34 | #include <utility> |
| 35 | |
| 36 | #include "span.h" |
| 37 | |
| 38 | /*! Calculates types and offset of struct field. Use in template arguments for |
| 39 | `table::get_value`, `value_iterator::get_value`, |
| 40 | `value_stream::make_iterator`, or `value_stream::make_range`. */ |
| 41 | #define MONERO_FIELD(obj, field) \ |
| 42 | obj , decltype(std::declval<obj>().field) , offsetof(obj, field) |
| 43 | |
| 44 | //! Expands to `lmdb::less` for the value `field` within `obj`. |
| 45 | #define MONERO_SORT_BY(obj, field) \ |
| 46 | &::lmdb::less< \ |
| 47 | lmdb::native_type<decltype(std::declval<obj>().field)>, \ |
| 48 | offsetof(obj, field) \ |
| 49 | > |
| 50 | |
| 51 | //! Expands to `lmdb::compare` for the value `field` within `obj`. |
| 52 | #define MONERO_COMPARE(obj, field) \ |
| 53 | &::lmdb::compare< \ |
| 54 | decltype(std::declval<obj>().field), \ |
| 55 | offsetof(obj, field) \ |
| 56 | > |
| 57 | |
| 58 | namespace lmdb |
| 59 | { |
| 60 | //! Prevent instantiation of `std::underlying_type<T>` when `T` is not enum. |
| 61 | template<typename T> |
| 62 | struct identity |
| 63 | { |
| 64 | using type = T; |
| 65 | }; |
| 66 | |
| 67 | /*! |
| 68 | Get the native type for enums, or return `T` unchanged. Useful for |
| 69 | merging generated machine code for templated functions that use enums |
| 70 | with identical size-widths without relying on aggressive identical |
| 71 | comdat folding (ICF) support in linker. So with enum defintion |
| 72 | `enum class enum_foo : unsigned long {};` will always yield |
| 73 | `assert(&func_foo<unsigned long> == &func_foo<native_type<enum_foo>>)`. |
| 74 | */ |
| 75 | template<typename T> |
| 76 | using native_type = typename std::conditional< |
| 77 | std::is_enum<T>::value, std::underlying_type<T>, identity<T> |
| 78 | >::type::type; |
| 79 | |
| 80 | //! \return `value` as its native type. |
| 81 | template<typename T, typename U = typename std::underlying_type<T>::type> |
| 82 | inline constexpr U to_native(T value) noexcept |
| 83 | { |
| 84 | return U(value); |
| 85 | } |
| 86 | |
| 87 | //! \return `value` bytes in a LMDB `MDB_val` object. |
| 88 | template<typename T> |
| 89 | inline MDB_val to_val(T&& value) noexcept |
| 90 | { |
| 91 | // lmdb does not touch user data, so const_cast is acceptable |
| 92 | static_assert(!std::is_rvalue_reference<T&&>(), "cannot use temporary value"); |
| 93 | void const* const temp = reinterpret_cast<void const*>(std::addressof(value)); |
| 94 | return MDB_val{sizeof(value), const_cast<void*>(temp)}; |
| 95 | } |
| 96 | |
| 97 | //! \return A span over the same chunk of memory as `value`. |
| 98 | inline constexpr epee::span<const std::uint8_t> to_byte_span(MDB_val value) noexcept |
| 99 | { |
| 100 | return {static_cast<const std::uint8_t*>(value.mv_data), value.mv_size}; |
| 101 | } |
| 102 | |
| 103 | /*! |
| 104 | A LMDB comparison function that uses `operator<`. |
| 105 | |
| 106 | \tparam T has a defined `operator<` . |
| 107 | \tparam offset to `T` within the value. |
| 108 | |
| 109 | \return -1 if `left < right`, 1 if `right < left`, and 0 otherwise. |
| 110 | */ |
| 111 | template<typename T, std::size_t offset = 0> |
| 112 | inline int less(MDB_val const* left, MDB_val const* right) noexcept |
| 113 | { |
| 114 | static_assert(std::is_trivially_copyable<T>(), "memcpy will not work"); |
| 115 | if (!left || !right || left->mv_size < sizeof(T) + offset || right->mv_size < sizeof(T) + offset) |
| 116 | { |
| 117 | assert("invalid use of custom comparison" == 0); |
| 118 | return -1; |
| 119 | } |
| 120 | |
| 121 | T left_val; |
| 122 | T right_val; |
| 123 | std::memcpy(std::addressof(left_val), static_cast<char*>(left->mv_data) + offset, sizeof(T)); |
| 124 | std::memcpy(std::addressof(right_val), static_cast<char*>(right->mv_data) + offset, sizeof(T)); |
| 125 | return left_val < right_val ? -1 : bool(right_val < left_val); |
| 126 | } |
| 127 | |
| 128 | /*! |
| 129 | A LMDB comparison function that uses `std::memcmp`. |
| 130 | |
| 131 | \toaram T has standard layout and an alignment of 1 |
| 132 | \tparam offset to `T` within the value. |
| 133 | |
| 134 | \return The result of `std::memcmp` over the value. |
| 135 | */ |
| 136 | template<typename T, std::size_t offset = 0> |
| 137 | inline int compare(MDB_val const* left, MDB_val const* right) noexcept |
| 138 | { |
| 139 | static_assert(std::is_standard_layout<T>() && alignof(T) == 1, "memcmp will not work"); |
| 140 | if (!left || !right || left->mv_size < sizeof(T) + offset || right->mv_size < sizeof(T) + offset) |
| 141 | { |
| 142 | assert("invalid use of custom comparison" == 0); |
| 143 | return -1; |
| 144 | } |
| 145 | return std::memcmp( |
| 146 | static_cast<char*>(left->mv_data) + offset, |
| 147 | static_cast<char*>(right->mv_data) + offset, |
| 148 | sizeof(T) |
| 149 | ); |
| 150 | } |
| 151 | } // lmdb |