NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
buffer_utils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
27 
28 namespace storage {
29 
30 std::string bytes_to_hex_str(char const *bytes, size_t byte_count)
31 {
32  std::string str{};
33  str.reserve(byte_count * 2);
34  bytes_to_hex_str(bytes, byte_count, str);
35  return str;
36 }
37 
38 void bytes_to_hex_str(char const *bytes, size_t byte_count, std::string &str)
39 {
40  static char constexpr char_table
41  [16]{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
42  for (auto *byte = bytes; byte != bytes + byte_count; ++byte) {
43  uint8_t nibbles[2];
44  nibbles[0] = static_cast<uint8_t>(*byte) >> 4;
45  nibbles[1] = static_cast<uint8_t>(*byte) & 0xf;
46  str.push_back(char_table[nibbles[0]]);
47  str.push_back(char_table[nibbles[1]]);
48  }
49 }
50 
51 size_t aligned_size(size_t alignment, size_t size)
52 {
53  if (alignment == 0)
54  return size;
55 
56  auto remainder = size % alignment;
57  return alignment * ((size / alignment) + (remainder == 0 ? 0 : 1));
58 }
59 
60 } /* namespace storage */
std::string bytes_to_hex_str(char const *bytes, size_t byte_count)
size_t aligned_size(size_t alignment, size_t size)