Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions libnvme/src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -160,3 +160,14 @@ install_headers(
subdir: 'nvme',
install_mode: mode,
)

# Install platform abstraction headers
install_headers([
'platform/includes.h',
'platform/linux.h',
'platform/windows.h',
'platform/types.h'
],
subdir: 'platform',
install_mode: mode,
)
2 changes: 1 addition & 1 deletion libnvme/src/nvme/accessors.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <linux/types.h> /* __u32, __u64, etc. */
#include <platform/types.h> /* __u32, __u64, etc. */

/* Forward declarations. These are internal (opaque) structs. */
struct nvme_path;
Expand Down
5 changes: 1 addition & 4 deletions libnvme/src/nvme/cleanup.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@
#define __CLEANUP_H

#include <dirent.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/socket.h>
#include <sys/types.h>
#include <platform/includes.h>

#include "fabrics.h"

Expand Down
3 changes: 2 additions & 1 deletion libnvme/src/nvme/cmds.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

#pragma once

#include <endian.h>
#include <errno.h>
#include <string.h>

#include <platform/includes.h>

#include <nvme/ioctl.h>
#include <nvme/types.h>

Expand Down
2 changes: 1 addition & 1 deletion libnvme/src/nvme/lib-types.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
*/
#pragma once

#include <linux/types.h>
#include <platform/types.h>

struct nvme_global_ctx;
struct nvme_transport_handle;
Expand Down
3 changes: 2 additions & 1 deletion libnvme/src/nvme/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@

#include <stdbool.h>
#include <stdio.h>
#include <syslog.h>

#include <platform/includes.h>

#include <nvme/lib-types.h>

Expand Down
5 changes: 2 additions & 3 deletions libnvme/src/nvme/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@

#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <syslog.h>
#include <time.h>
#include <unistd.h>

#include <platform/includes.h>

#include <libnvme.h>

Expand Down
2 changes: 1 addition & 1 deletion libnvme/src/nvme/mi-mctp-compat.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef _MI_MCTP_COMPAT_H
#define _MI_MCTP_COMPAT_H

#include <linux/types.h>
#include <platform/types.h>

/* As of kernel v5.15, these AF_MCTP-related definitions are provided by
* linux/mctp.h. However, we provide a set here while that header percolates
Expand Down
6 changes: 1 addition & 5 deletions libnvme/src/nvme/mi-mctp.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <platform/includes.h>

#if HAVE_LINUX_MCTP_H
#include <linux/mctp.h>
Expand Down
3 changes: 2 additions & 1 deletion libnvme/src/nvme/mi.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@
*/
#pragma once

#include <endian.h>
#include <stdint.h>

#include <platform/includes.h>

#include <nvme/tree.h>

/**
Expand Down
4 changes: 1 addition & 3 deletions libnvme/src/nvme/nbft.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <stdio.h>
#include <stdlib.h>

#include <arpa/inet.h>
#include <platform/includes.h>

#include <ccan/endian/endian.h>

Expand All @@ -19,8 +19,6 @@
#include "private.h"
#include "compiler_attributes.h"

#define MIN(a, b) (((a) < (b)) ? (a) : (b))

static __u8 csum(const __u8 *buffer, ssize_t length)
{
int n;
Expand Down
2 changes: 1 addition & 1 deletion libnvme/src/nvme/nbft.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

#include <stdbool.h>

#include <linux/types.h>
#include <platform/types.h>
#include <sys/types.h>

#include <nvme/lib-types.h>
Expand Down
7 changes: 1 addition & 6 deletions libnvme/src/nvme/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,9 @@
*/
#pragma once

#include <errno.h>
#include <ifaddrs.h>
#include <poll.h>

#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <platform/includes.h>

#include <ccan/list/list.h>

Expand Down
6 changes: 2 additions & 4 deletions libnvme/src/nvme/tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,17 @@
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <ifaddrs.h>
#include <libgen.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <arpa/inet.h>
#include <netdb.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <platform/includes.h>

#include <ccan/endian/endian.h>
#include <ccan/list/list.h>

Expand Down
2 changes: 1 addition & 1 deletion libnvme/src/nvme/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#include <stdint.h>
#include <stdio.h>

#include <linux/types.h>
#include <platform/types.h>

/**
* DOC: types.h
Expand Down
5 changes: 2 additions & 3 deletions libnvme/src/nvme/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,11 @@
#include <string.h>
#include <unistd.h>

#include <arpa/inet.h>
#include <netdb.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/types.h>

#include <platform/includes.h>

#include <ccan/endian/endian.h>
#include <ccan/minmax/minmax.h>

Expand Down
18 changes: 18 additions & 0 deletions libnvme/src/platform/includes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* This file is part of libnvme.
* Copyright (c) 2025 Micron Technology, Inc.
*
* Common platform-specific includes and definitions.
*/

#pragma once

#include "platform/types.h"

/* Platform-specific includes */
#ifdef _WIN32
#include "platform/windows.h"
#else
#include "platform/linux.h"
#endif
30 changes: 30 additions & 0 deletions libnvme/src/platform/linux.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* This file is part of libnvme.
* Copyright (c) 2026 Micron Technology, Inc.
*
* Linux platform-specific definitions and includes.
*/

#pragma once

/* Linux-specific includes */
#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <poll.h>
#include <stdlib.h>
#include <unistd.h>

#include <syslog.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <sys/uio.h>

#include <arpa/inet.h>
#include <ifaddrs.h>
#include <netdb.h>
#include <netinet/in.h>
47 changes: 47 additions & 0 deletions libnvme/src/platform/types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* This file is part of libnvme.
* Copyright (c) 2026 Micron Technology, Inc.
*
* Platform compatibility for linux/types.h
*/
#ifndef _PLATFORM_TYPES_H
#define _PLATFORM_TYPES_H

#ifdef _WIN32

#include <stdint.h>

/* Standard type definitions for linux/types.h compatibility */
typedef uint8_t __u8;
typedef uint16_t __u16;
typedef uint32_t __u32;
typedef uint64_t __u64;
typedef int8_t __s8;
typedef int16_t __s16;
typedef int32_t __s32;
typedef int64_t __s64;

/* Little-endian types (Windows is little-endian) */
typedef __u16 __le16;
typedef __u32 __le32;
typedef __u64 __le64;
typedef __s16 __le16s;
typedef __s32 __le32s;
typedef __s64 __le64s;

/* Big-endian types for completeness */
typedef __u16 __be16;
typedef __u32 __be32;
typedef __u64 __be64;
typedef __s16 __be16s;
typedef __s32 __be32s;
typedef __s64 __be64s;

#else

#include <linux/types.h>

#endif

#endif /* _PLATFORM_TYPES_H */
64 changes: 64 additions & 0 deletions libnvme/src/platform/windows.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* This file is part of libnvme.
* Copyright (c) 2026 Micron Technology, Inc.
*
* Windows platform-specific definitions and includes.
*/

#pragma once

/* Windows-specific includes - winsock2 before windows.h to avoid warnings */
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#undef WIN32_LEAN_AND_MEAN

#include <bcrypt.h>
#include <direct.h>
#include <errno.h>
#include <fcntl.h>
#include <io.h>
#include <process.h>
#include <stdio.h>
#include <time.h>


/* endian.h compatibility */

#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define htobe16(x) (x)
#define htobe32(x) (x)
#define htobe64(x) (x)
#define htole16(x) __builtin_bswap16(x)
#define htole32(x) __builtin_bswap32(x)
#define htole64(x) __builtin_bswap64(x)
#define le16toh(x) __builtin_bswap16(x)
#define le32toh(x) __builtin_bswap32(x)
#define le64toh(x) __builtin_bswap64(x)
#else
/* Little-endian (most common case for Windows) */
#define htobe16(x) __builtin_bswap16(x)
#define htobe32(x) __builtin_bswap32(x)
#define htobe64(x) __builtin_bswap64(x)
#define htole16(x) (x)
#define htole32(x) (x)
#define htole64(x) (x)
#define le16toh(x) (x)
#define le32toh(x) (x)
#define le64toh(x) (x)
#endif


/* sys/param.h compatibility */

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((a) > (b) ? (a) : (b))


/*
* More compatibility definitions and method implementations will be needed
* as Windows support is developed. For now, this file serves as a base
* implementation and an example of the proposed structure.
*/
2 changes: 1 addition & 1 deletion libnvme/tools/generator/generate-accessors.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ python3 generate-accessors.py person.h
#include <string.h>
#include <stdbool.h>
#include <stdint.h>
#include <linux/types.h> /* __u32, __u64, etc. */
#include <platform/types.h> /* __u32, __u64, etc. */

/* Forward declarations. These are internal (opaque) structs. */
struct person;
Expand Down
2 changes: 1 addition & 1 deletion libnvme/tools/generator/generate-accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ def main():
f'#include <string.h>\n'
f'#include <stdbool.h>\n'
f'#include <stdint.h>\n'
f'#include <linux/types.h> /* __u32, __u64, etc. */\n'
f'#include <platform/types.h> /* __u32, __u64, etc. */\n'
f'\n'
)
f.write('/* Forward declarations. These are internal (opaque) structs. */\n')
Expand Down
Loading
Loading