Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
 
#pragma once
 
#include <type_traits>
 
#include <gsl/gsl>
 
#include "core/common/status.h"
#include "core/common/common.h"
 
namespace onnxruntime {
namespace utils {
 
/**
 * Copies elements and swaps their byte orders.
 *
 * This is a low-level function - please be sure to pass in valid arguments.
 * In particular:
 * - source_bytes and destination_bytes should have the same size, which should
 *   be a multiple of element_size_in_bytes.
 * - element_size_in_bytes should be greater than zero.
 * - source_bytes and destination_bytes should not overlap.
 *
 * @param element_size_in_bytes The size of an individual element, in bytes.
 * @param source_bytes The source byte span.
 * @param destination_bytes The destination byte span.
 */
void SwapByteOrderCopy(size_t element_size_in_bytes,
                       gsl::span<const unsigned char> source_bytes,
                       gsl::span<unsigned char> destination_bytes);
 
/**
 * Swaps the byte order of the elements in the given byte span in place.
 *
 * This is a low-level function - please be sure to pass in valid arguments.
 * In particular:
 * - bytes should have a size that is a multiple of element_size_in_bytes.
 * - element_size_in_bytes should be greater than zero.
 * - bytes should not overlap with itself.
 *
 * @param element_size_in_bytes The size of an individual element, in bytes.
 * @param source_bytes The source byte span.
 */
void SwapByteOrderInplace(size_t element_size_in_bytes,
                          gsl::span<std::byte> bytes);
 
namespace detail {
 
/**
 * Copies between two buffers where one is little-endian and the other has
 * native endian-ness.
 */
Status CopyLittleEndian(size_t element_size_in_bytes,
                        gsl::span<const unsigned char> source_bytes,
                        gsl::span<unsigned char> destination_bytes);
 
}  // namespace detail
 
/**
 * Reads from a little-endian source.
 */
common::Status ReadLittleEndian(size_t element_size,
                                gsl::span<const unsigned char> source_bytes,
                                gsl::span<unsigned char> destination_bytes);
 
/**
 * Reads from a little-endian source.
 */
template <typename T>
common::Status ReadLittleEndian(gsl::span<const unsigned char> source_bytes, gsl::span<T> destination) {
  static_assert(std::is_trivially_copyable<T>::value, "T must be trivially copyable");
  const auto destination_bytes = gsl::make_span(reinterpret_cast<unsigned char*>(destination.data()),
                                                destination.size_bytes());
  return ReadLittleEndian(sizeof(T), source_bytes, destination_bytes);
}
 
/**
 * Writes to a little-endian destination.
 */
common::Status WriteLittleEndian(size_t element_size,
                                 gsl::span<const unsigned char> source_bytes,
                                 gsl::span<unsigned char> destination_bytes);
 
/**
 * Writes to a little-endian destination.
 */
template <typename T>
common::Status WriteLittleEndian(gsl::span<const T> source, gsl::span<unsigned char> destination_bytes) {
  static_assert(std::is_trivially_copyable<T>::value, "T must be trivially copyable");
  const auto source_bytes = gsl::make_span(reinterpret_cast<const unsigned char*>(source.data()), source.size_bytes());
  return WriteLittleEndian(sizeof(T), source_bytes, destination_bytes);
}
 
}  // namespace utils
}  // namespace onnxruntime