Skip to content

Commit b7b325c

Browse files
compat: Adds more platform compatibility headers and mem compatibility.
- Adds compatibility headers that provides functionality missing from Windows headers and cross-platform compatibility macros. - Adds memory management compatibility for aligned memory allocation and freeing. Windows has a seprate aligned free that needs to be used for aligned memory. Signed-off-by: Broc Going <[email protected]> Signed-off-by: Brandon Capener <[email protected]>
1 parent d391152 commit b7b325c

38 files changed

Lines changed: 671 additions & 215 deletions

common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99

1010
#define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
1111

12+
#ifndef min
1213
#define min(x, y) ((x) > (y) ? (y) : (x))
14+
#endif
15+
#ifndef max
1316
#define max(x, y) ((x) > (y) ? (x) : (y))
17+
#endif
1418

1519
#ifdef __packed
1620
#else /* __packed */

libnvme/src/meson.build

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,16 @@ headers = [
3232
'nvme/lib-types.h',
3333
'nvme/lib.h',
3434
'nvme/linux.h',
35+
'nvme/malloc.h',
36+
'nvme/mkdir.h',
3537
'nvme/nvme-cmds.h',
3638
'nvme/nvme-types.h',
39+
'nvme/signal.h',
40+
'nvme/stdio.h',
41+
'nvme/stdlib.h',
3742
'nvme/tree.h',
3843
'nvme/types.h',
44+
'nvme/unistd.h',
3945
'nvme/util.h',
4046
]
4147

libnvme/src/nvme/cleanup.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#ifndef __CLEANUP_H
33
#define __CLEANUP_H
44

5-
#include <stdlib.h>
5+
#include <nvme/stdlib.h>
66

77
#define __cleanup(fn) __attribute__((cleanup(fn)))
88

@@ -22,4 +22,10 @@ static inline void freep(void *p)
2222
}
2323
#define __cleanup_free __cleanup(freep)
2424

25+
static inline void libnvme_freep(void *p)
26+
{
27+
aligned_free(*(void **)p);
28+
}
29+
#define __cleanup_libnvme_free __cleanup(libnvme_freep)
30+
2531
#endif

libnvme/src/nvme/fabrics.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1418,7 +1418,7 @@ static int nvme_discovery_log(libnvme_ctrl_t ctrl,
14181418
if (numrec == 0)
14191419
break;
14201420

1421-
free(log);
1421+
__libnvme_free(log);
14221422
entries_size = sizeof(*log->entries) * numrec;
14231423
log = __libnvme_alloc(sizeof(*log) + entries_size);
14241424
if (!log) {
@@ -1475,7 +1475,7 @@ static int nvme_discovery_log(libnvme_ctrl_t ctrl,
14751475
}
14761476

14771477
out_free_log:
1478-
free(log);
1478+
__libnvme_free(log);
14791479
return err;
14801480
}
14811481

libnvme/src/nvme/lib.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
#include <stdio.h>
1313

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

1618
enum libnvme_log_level {
1719
LIBNVME_LOG_ERR = 0,

libnvme/src/nvme/log.c

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

1111
#include <stdarg.h>
1212
#include <stdbool.h>
13-
#include <stdio.h>
1413
#include <time.h>
1514
#include <unistd.h>
1615

16+
#include <nvme/stdio.h>
17+
1718
#include <libnvme.h>
1819

1920
#include "cleanup.h"

libnvme/src/nvme/malloc.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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: Brandon Capener <[email protected]>
12+
*/
13+
#pragma once
14+
15+
#include <malloc.h>
16+
17+
#if defined(_WIN32)
18+
#define malloc_usable_size _msize
19+
#endif

libnvme/src/nvme/mkdir.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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 mkdir (sys/stat.h).
7+
*
8+
* Authors: Brandon Busacker <[email protected]>
9+
*/
10+
#pragma once
11+
12+
#if defined(_WIN32)
13+
14+
#include <direct.h>
15+
16+
/* Windows mkdir doesn't take the mode parameter */
17+
#define mkdir(path, mode) _mkdir(path)
18+
19+
#endif

libnvme/src/nvme/private.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,8 @@ bool _libnvme_ctrl_match_config(struct libnvme_ctrl *c,
425425

426426
void *__libnvme_alloc(size_t len);
427427

428+
void __libnvme_free(void *p);
429+
428430
void *__libnvme_realloc(void *p, size_t len);
429431

430432
void nvme_deconfigure_ctrl(struct libnvme_ctrl *c);

libnvme/src/nvme/signal.h

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 signal.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: Brandon Busacker <[email protected]>
12+
*/
13+
#pragma once
14+
15+
#include <signal.h>
16+
17+
#if defined(_WIN32)
18+
19+
/* signal.h POSIX compatibility - Windows doesn't have sigaction */
20+
21+
struct sigaction {
22+
void (*sa_handler)(int);
23+
int sa_flags;
24+
int sa_mask; /* simplified - normally sigset_t */
25+
};
26+
27+
static inline int sigemptyset(int *set)
28+
{
29+
*set = 0;
30+
return 0;
31+
}
32+
33+
/*
34+
* Simplified signal handling using Windows signal() function
35+
* This is sufficient for handling SIGINT with no mask or flags.
36+
*/
37+
static inline int sigaction(int signum, const struct sigaction *act,
38+
struct sigaction *oldact)
39+
{
40+
(void)oldact; /* ignore old action for simplicity */
41+
if (act && act->sa_handler) {
42+
signal(signum, act->sa_handler);
43+
return 0;
44+
}
45+
return -1;
46+
}
47+
48+
#endif

0 commit comments

Comments
 (0)