Skip to content

Commit 5cfa491

Browse files
committed
endian: provide endian helpers when missing
Not all platforms provide an endian.h. in those cases, define the byte-order helper functions. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 10ea91c commit 5cfa491

6 files changed

Lines changed: 40 additions & 3 deletions

File tree

libnvme/src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ else
2626
endif
2727
headers = [
2828
'nvme/accessors.h',
29+
'nvme/endian.h',
2930
'nvme/filters.h',
3031
'nvme/ioctl.h',
3132
'nvme/lib-types.h',

libnvme/src/nvme/endian.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// SPDX-License-Identifier: LGPL-2.1-or-later
2+
/*
3+
* This file is part of libnvme.
4+
* Copyright (c) 2026 SUSE Software Solutions
5+
*
6+
* Authors: Daniel Wagner <[email protected]>
7+
*/
8+
9+
#pragma once
10+
11+
#if defined(_WIN32) || defined(_WIN64)
12+
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
13+
#define htobe16(x) (x)
14+
#define htobe32(x) (x)
15+
#define htobe64(x) (x)
16+
#define htole16(x) __builtin_bswap16(x)
17+
#define htole32(x) __builtin_bswap32(x)
18+
#define htole64(x) __builtin_bswap64(x)
19+
#define le16toh(x) __builtin_bswap16(x)
20+
#define le32toh(x) __builtin_bswap32(x)
21+
#define le64toh(x) __builtin_bswap64(x)
22+
#else
23+
/* Little-endian (most common case for Windows) */
24+
#define htobe16(x) __builtin_bswap16(x)
25+
#define htobe32(x) __builtin_bswap32(x)
26+
#define htobe64(x) __builtin_bswap64(x)
27+
#define htole16(x) (x)
28+
#define htole32(x) (x)
29+
#define htole64(x) (x)
30+
#define le16toh(x) (x)
31+
#define le32toh(x) (x)
32+
#define le64toh(x) (x)
33+
#endif
34+
#else
35+
#include <endian.h>
36+
#endif

libnvme/src/nvme/mi-types.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
*/
88
#pragma once
99

10-
#include <endian.h>
1110
#include <stdint.h>
1211

12+
#include <nvme/endian.h>
1313
#include <nvme/nvme-types.h>
1414

1515
/**

libnvme/src/nvme/nvme-cmds.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010

1111
#pragma once
1212

13-
#include <endian.h>
1413
#include <errno.h>
1514
#include <string.h>
1615

16+
#include <nvme/endian.h>
1717
#include <nvme/ioctl.h>
1818
#include <nvme/nvme-types.h>
1919

libnvme/test/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ if openssl_dep.found()
194194
endif
195195

196196
foreach hdr : [
197+
'endian',
197198
'fabrics',
198199
'filters',
199200
'ioctl',

nvme.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
#define _NVME_H
1818

1919
#include <dirent.h>
20-
#include <endian.h>
2120
#include <stdbool.h>
2221
#include <stdint.h>
2322
#include <stdio.h>

0 commit comments

Comments
 (0)