Skip to content

Commit 55b3711

Browse files
Refactored malloc.h and unistd.h compatibility.
- Added nvme/unistd.h and nvme/malloc.h - Updated code to use new headers instead of platform/includes.h - Removed malloc.h and unistd.h compatibility from windows.h
1 parent 19c56ae commit 55b3711

9 files changed

Lines changed: 91 additions & 47 deletions

File tree

libnvme/src/meson.build

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ headers = [
3737
'nvme/lib-types.h',
3838
'nvme/lib.h',
3939
'nvme/linux.h',
40+
'nvme/malloc.h',
4041
'nvme/nvme-cmds.h',
4142
'nvme/nvme-types.h',
4243
'nvme/stdlib.h',
4344
'nvme/tree.h',
4445
'nvme/types.h',
46+
'nvme/unistd.h',
4547
'nvme/util.h',
4648
]
4749

libnvme/src/nvme/lib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <nvme/lib-types.h>
1515
#include <nvme/stdlib.h>
16+
#include <nvme/unistd.h>
1617

1718
#include <platform/includes.h> /* for libnvme_fd_t */
1819

libnvme/src/nvme/malloc.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2+
/*
3+
* This file is part of libnvme.
4+
* Copyright (c) 2026 Micron Technology, Inc.
5+
*
6+
* Cross-platform compatibility for malloc.h.
7+
* Provides functionality that may be missing on some platforms.
8+
* Compatibility is not comprehensive. Only functionality required by
9+
* nvme-cli and libnvme is included.
10+
*
11+
* Authors: Broc Going <[email protected]>
12+
* Brandon Capener <[email protected]>
13+
*/
14+
#pragma once
15+
16+
#include <malloc.h>
17+
18+
#ifdef _WIN32
19+
20+
/* malloc_usable_size implementation for Windows */
21+
static inline size_t malloc_usable_size(void *ptr)
22+
{
23+
return _msize(ptr);
24+
}
25+
26+
#endif /* _WIN32 */

libnvme/src/nvme/stdlib.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* SPDX-License-Identifier: LGPL-2.1-or-later */
22
/*
3+
* This file is part of libnvme.
34
* Copyright (c) 2026 Micron Technology, Inc.
45
*
56
* Cross-platform compatibility for stdlib.h.

libnvme/src/nvme/unistd.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2+
/*
3+
* This file is part of libnvme.
4+
* Copyright (c) 2026 Micron Technology, Inc.
5+
*
6+
* Cross-platform compatibility for unistd.h.
7+
* Provides functionality that may be missing on some platforms.
8+
* Compatibility is not comprehensive. Only functionality required by
9+
* nvme-cli and libnvme is included.
10+
*
11+
* Authors: Broc Going <[email protected]>
12+
* Brandon Capener <[email protected]>
13+
*/
14+
#pragma once
15+
16+
#include <unistd.h>
17+
18+
#if defined(_WIN32) || defined(_WIN64)
19+
20+
#include <io.h>
21+
#include <sysinfoapi.h>
22+
23+
/* unistd.h POSIX compatibility */
24+
25+
#define STDERR_FILENO 2
26+
#define STDOUT_FILENO 1
27+
#define STDIN_FILENO 0
28+
29+
/* getpagesize implementation for Windows */
30+
static inline int getpagesize(void)
31+
{
32+
SYSTEM_INFO si;
33+
34+
GetSystemInfo(&si);
35+
return si.dwPageSize;
36+
}
37+
38+
/*
39+
* readlink stub - Windows doesn't have symbolic links in the same way
40+
* NOTE: This is only used by micron-nvme.c, and can be removed once that
41+
* has been refactored to not rely on Linux-specific sysfs paths.
42+
*/
43+
static inline ssize_t readlink(const char *path, char *buf, size_t bufsiz)
44+
{
45+
errno = ENOTSUP;
46+
return -1;
47+
}
48+
49+
/* fsync implementation for Windows */
50+
static inline int fsync(int fd)
51+
{
52+
return _commit(fd);
53+
}
54+
55+
#endif

libnvme/src/nvme/util.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
#include <errno.h>
1111
#include <fcntl.h>
12-
#include <malloc.h>
1312
#include <stdbool.h>
1413
#include <stdio.h>
1514
#include <string.h>
@@ -26,6 +25,7 @@
2625
#include <sys/stat.h>
2726
#include <sys/types.h>
2827

28+
#include <nvme/malloc.h>
2929
#include <nvme/stdlib.h>
3030

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

libnvme/src/platform/windows.h

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -167,50 +167,6 @@ static inline FILE *open_memstream(char **ptr, size_t *sizeloc)
167167
}
168168

169169

170-
/* malloc.h compatibility*/
171-
172-
static inline size_t malloc_usable_size(void *ptr)
173-
{
174-
return _msize(ptr);
175-
}
176-
177-
178-
/* unistd.h POSIX compatibility */
179-
180-
#define STDERR_FILENO 2
181-
#define STDOUT_FILENO 1
182-
#define STDIN_FILENO 0
183-
184-
/* getpagesize implementation for Windows */
185-
static inline DWORD getpagesize(void)
186-
{
187-
SYSTEM_INFO si;
188-
189-
GetSystemInfo(&si);
190-
return si.dwPageSize;
191-
}
192-
193-
/*
194-
* readlink stub - Windows doesn't have symbolic links in the same way
195-
* NOTE: This is only used by micron-nvme.c, and can be removed once that
196-
* has been refactored to not rely on Linux-specific sysfs paths.
197-
*/
198-
static inline int readlink(const char *path, char *buf, size_t bufsiz)
199-
{
200-
(void)path;
201-
(void)buf;
202-
(void)bufsiz;
203-
errno = EINVAL;
204-
return -1;
205-
}
206-
207-
/* fsync implementation for Windows */
208-
static inline int fsync(int fd)
209-
{
210-
return _commit(fd);
211-
}
212-
213-
214170
/* time.h POSIX compatibility */
215171

216172
static inline struct tm *gmtime_r(const time_t *timep, struct tm *result)

util/mem-windows.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@
99
#include <malloc.h>
1010
#include <string.h>
1111

12+
#include <memoryapi.h>
13+
1214
#include <nvme/stdlib.h>
13-
#include "platform/includes.h" /* unistd.h + compatibility */
15+
#include <nvme/unistd.h>
1416

1517
#include "mem.h"
1618
#include "common.h"

util/mem.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
#include <malloc.h>
33
#include <string.h>
44

5+
#include <nvme/malloc.h>
56
#include <nvme/stdlib.h>
6-
#include <platform/includes.h> /* getpagesize, malloc_usable_size */
7+
#include <nvme/unistd.h>
78

89
#include "mem.h"
910

0 commit comments

Comments
 (0)