]> git.proxmox.com Git - ceph.git/blame - ceph/src/seastar/include/seastar/core/byteorder.hh
bump version to 19.2.0-pve1
[ceph.git] / ceph / src / seastar / include / seastar / core / byteorder.hh
CommitLineData
11fdf7f2
TL
1/*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18/*
19 * Copyright (C) 2015 Scylladb, Ltd.
20 */
21
22#pragma once
23
f51cf556 24#ifndef SEASTAR_MODULE
11fdf7f2 25#include <algorithm>
1e59de90 26#include <boost/endian/conversion.hpp>
11fdf7f2 27#include <seastar/core/unaligned.hh>
f51cf556
TL
28#include <seastar/util/modules.hh>
29#endif
11fdf7f2
TL
30
31namespace seastar {
32
f51cf556
TL
33SEASTAR_MODULE_EXPORT_BEGIN
34
1e59de90
TL
35template <typename T>
36inline T cpu_to_le(T x) noexcept {
37 return boost::endian::native_to_little(x);
38}
39template <typename T>
40inline T le_to_cpu(T x) noexcept {
41 return boost::endian::little_to_native(x);
42}
f67539c2 43
1e59de90
TL
44template <typename T>
45inline T cpu_to_be(T x) noexcept {
46 return boost::endian::native_to_big(x);
47}
48template <typename T>
49inline T be_to_cpu(T x) noexcept {
50 return boost::endian::big_to_native(x);
51}
11fdf7f2
TL
52
53template <typename T>
f67539c2 54inline T cpu_to_le(const unaligned<T>& v) noexcept {
11fdf7f2
TL
55 return cpu_to_le(T(v));
56}
57
58template <typename T>
f67539c2 59inline T le_to_cpu(const unaligned<T>& v) noexcept {
11fdf7f2
TL
60 return le_to_cpu(T(v));
61}
62
63template <typename T>
64inline
65T
f67539c2 66read_le(const char* p) noexcept {
11fdf7f2
TL
67 T datum;
68 std::copy_n(p, sizeof(T), reinterpret_cast<char*>(&datum));
69 return le_to_cpu(datum);
70}
71
72template <typename T>
73inline
74void
f67539c2 75write_le(char* p, T datum) noexcept {
11fdf7f2
TL
76 datum = cpu_to_le(datum);
77 std::copy_n(reinterpret_cast<const char*>(&datum), sizeof(T), p);
78}
79
80template <typename T>
81inline
82T
f67539c2 83read_be(const char* p) noexcept {
11fdf7f2
TL
84 T datum;
85 std::copy_n(p, sizeof(T), reinterpret_cast<char*>(&datum));
86 return be_to_cpu(datum);
87}
88
89template <typename T>
90inline
91void
f67539c2 92write_be(char* p, T datum) noexcept {
11fdf7f2
TL
93 datum = cpu_to_be(datum);
94 std::copy_n(reinterpret_cast<const char*>(&datum), sizeof(T), p);
95}
96
97template <typename T>
98inline
99T
f67539c2 100consume_be(const char*& p) noexcept {
11fdf7f2
TL
101 auto ret = read_be<T>(p);
102 p += sizeof(T);
103 return ret;
104}
105
106template <typename T>
107inline
108void
f67539c2 109produce_be(char*& p, T datum) noexcept {
11fdf7f2
TL
110 write_be<T>(p, datum);
111 p += sizeof(T);
112}
113
f51cf556
TL
114SEASTAR_MODULE_EXPORT_END
115
11fdf7f2 116}