Skip to content
Open
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
4 changes: 4 additions & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,12 @@

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

#ifndef min
#define min(x, y) ((x) > (y) ? (y) : (x))
#endif
#ifndef max
#define max(x, y) ((x) > (y) ? (x) : (y))
#endif

#ifdef __packed
#else /* __packed */
Expand Down
6 changes: 6 additions & 0 deletions libnvme/src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,16 @@ headers = [
'nvme/lib-types.h',
'nvme/lib.h',
'nvme/linux.h',
'nvme/malloc.h',
'nvme/mkdir.h',
'nvme/nvme-cmds.h',
'nvme/nvme-types.h',
'nvme/signal.h',
'nvme/stdio.h',
'nvme/stdlib.h',
'nvme/tree.h',
'nvme/types.h',
'nvme/unistd.h',
'nvme/util.h',
]

Expand Down
8 changes: 7 additions & 1 deletion libnvme/src/nvme/cleanup.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#ifndef __CLEANUP_H
#define __CLEANUP_H

#include <stdlib.h>
#include <nvme/stdlib.h>

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

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

static inline void libnvme_freep(void *p)
{
aligned_free(*(void **)p);
}
#define __cleanup_libnvme_free __cleanup(libnvme_freep)

#endif
4 changes: 2 additions & 2 deletions libnvme/src/nvme/fabrics.c
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ static int nvme_discovery_log(libnvme_ctrl_t ctrl,
if (numrec == 0)
break;

free(log);
__libnvme_free(log);
entries_size = sizeof(*log->entries) * numrec;
log = __libnvme_alloc(sizeof(*log) + entries_size);
if (!log) {
Expand Down Expand Up @@ -1475,7 +1475,7 @@ static int nvme_discovery_log(libnvme_ctrl_t ctrl,
}

out_free_log:
free(log);
__libnvme_free(log);
return err;
}

Expand Down
2 changes: 2 additions & 0 deletions libnvme/src/nvme/lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
#include <stdio.h>

#include <nvme/lib-types.h>
#include <nvme/stdlib.h>
#include <nvme/unistd.h>

enum libnvme_log_level {
LIBNVME_LOG_ERR = 0,
Expand Down
3 changes: 2 additions & 1 deletion libnvme/src/nvme/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@

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

#include <nvme/stdio.h>

#include <libnvme.h>

#include "cleanup.h"
Expand Down
19 changes: 19 additions & 0 deletions libnvme/src/nvme/malloc.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* This file is part of libnvme.
* Copyright (c) 2026 Micron Technology, Inc.
*
* Cross-platform compatibility for malloc.h.
* Provides functionality that may be missing on some platforms.
* Compatibility is not comprehensive. Only functionality required by
* nvme-cli and libnvme is included.
*
* Authors: Brandon Capener <[email protected]>
*/
#pragma once

#include <malloc.h>

#if defined(_WIN32)
#define malloc_usable_size _msize
#endif
19 changes: 19 additions & 0 deletions libnvme/src/nvme/mkdir.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* This file is part of libnvme.
* Copyright (c) 2026 Micron Technology, Inc.
*
* Cross-platform compatibility for mkdir (sys/stat.h).
*
* Authors: Brandon Busacker <[email protected]>
*/
#pragma once

#if defined(_WIN32)

#include <direct.h>

/* Windows mkdir doesn't take the mode parameter */
#define mkdir(path, mode) _mkdir(path)

#endif
2 changes: 2 additions & 0 deletions libnvme/src/nvme/private.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ bool _libnvme_ctrl_match_config(struct libnvme_ctrl *c,

void *__libnvme_alloc(size_t len);

void __libnvme_free(void *p);

void *__libnvme_realloc(void *p, size_t len);

void nvme_deconfigure_ctrl(struct libnvme_ctrl *c);
Expand Down
48 changes: 48 additions & 0 deletions libnvme/src/nvme/signal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* This file is part of libnvme.
* Copyright (c) 2026 Micron Technology, Inc.
*
* Cross-platform compatibility for signal.h.
* Provides functionality that may be missing on some platforms.
* Compatibility is not comprehensive. Only functionality required by
* nvme-cli and libnvme is included.
*
* Authors: Brandon Busacker <[email protected]>
*/
#pragma once

#include <signal.h>

#if defined(_WIN32)

/* signal.h POSIX compatibility - Windows doesn't have sigaction */

struct sigaction {
void (*sa_handler)(int);

Check failure on line 22 in libnvme/src/nvme/signal.h

View workflow job for this annotation

GitHub Actions / checkpatch review

WARNING: function definition argument 'int' should also have an identifier name
int sa_flags;
int sa_mask; /* simplified - normally sigset_t */
};

static inline int sigemptyset(int *set)
{
*set = 0;
return 0;
}

/*
* Simplified signal handling using Windows signal() function
* This is sufficient for handling SIGINT with no mask or flags.
*/
static inline int sigaction(int signum, const struct sigaction *act,
struct sigaction *oldact)
{
(void)oldact; /* ignore old action for simplicity */
if (act && act->sa_handler) {
signal(signum, act->sa_handler);
return 0;
}
return -1;
}

#endif
110 changes: 110 additions & 0 deletions libnvme/src/nvme/stdio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* This file is part of libnvme.
* Copyright (c) 2026 Micron Technology, Inc.
*
* Cross-platform compatibility for stdio.h.
* Provides functionality that may be missing on some platforms.
* Compatibility is not comprehensive. Only functionality required by
* nvme-cli and libnvme is included.
*
* Authors: Brandon Busacker <[email protected]>
*/
#pragma once

#include <stdio.h>

#if defined(_WIN32)

#include <errno.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>

/* stdio.h POSIX extensions */

/* dprintf implementation for Windows */
static inline int dprintf(int fd, const char *format, ...)
{
va_list args;
char buffer[4096];
int result;

va_start(args, format);
result = vsnprintf(buffer, sizeof(buffer), format, args);
va_end(args);
if (fd == STDERR_FILENO)
fputs(buffer, stderr);
else if (fd == STDOUT_FILENO)
fputs(buffer, stdout);
return result;
}

/* getline implementation for Windows */
static inline ssize_t getline(char **lineptr, size_t *n, FILE *stream)
{
char *bufptr = NULL;
char *p = bufptr;
size_t size;
int c;

if (lineptr == NULL || stream == NULL || n == NULL) {
errno = EINVAL;
return -1;
}

bufptr = *lineptr;
size = *n;

c = fgetc(stream);
if (c == EOF)
return -1;

if (bufptr == NULL) {
bufptr = (char *)malloc(128);
if (bufptr == NULL) {
errno = ENOMEM;
return -1;
}
size = 128;
}

p = bufptr;
while (c != EOF) {
if ((size_t)(p - bufptr) + 1 >= size) {
size_t pos = (size_t)(p - bufptr);

size = size + 128;
bufptr = (char *)realloc(bufptr, size);
if (bufptr == NULL) {
errno = ENOMEM;
return -1;
}
p = bufptr + pos;
}
*p++ = c;
if (c == '\n')
break;
c = fgetc(stream);
}

*p = '\0';
*lineptr = bufptr;
*n = size;

return p - bufptr;
}

/* open_memstream workaround for Windows - returns a temporary file instead */
static inline FILE *open_memstream(char **ptr, size_t *sizeloc)
{
FILE *f = tmpfile();

if (ptr)
*ptr = NULL;
if (sizeloc)
*sizeloc = 0;
return f;
}

#endif
55 changes: 55 additions & 0 deletions libnvme/src/nvme/stdlib.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* This file is part of libnvme.
* Copyright (c) 2026 Micron Technology, Inc.
*
* Cross-platform compatibility for stdlib.h.
* Provides functionality that may be missing on some platforms.
* Compatibility is not comprehensive. Only functionality required by
* nvme-cli and libnvme is included.
*
* Authors: Brandon Capener <[email protected]>
*/
#pragma once

#include <stdlib.h>

/*
* Cross-platform compatible free for aligned memory allocations.
* Use when posix_memalign is used to allocate memory.
*/
#if defined(_WIN32)
#define aligned_free _aligned_free
#else
#define aligned_free free
#endif

#if defined(_WIN32)

#include <errno.h>
#include <limits.h>
#include <malloc.h>

/* Aligned memory allocation function, use aligned_free to free. */
static inline int posix_memalign(void **memptr, size_t alignment, size_t size)
{
*memptr = _aligned_malloc(size, alignment);
return (*memptr == NULL) ? ENOMEM : 0;
}

/* reallocarray implementation for Windows */
static inline void *reallocarray(void *ptr, size_t nmemb, size_t size)
{
size_t total_size;

/* Check for multiplication overflow */
if (nmemb != 0 && size > SIZE_MAX / nmemb) {
errno = ENOMEM;
return NULL;
}

total_size = nmemb * size;
return realloc(ptr, total_size);
}

#endif
36 changes: 36 additions & 0 deletions libnvme/src/nvme/unistd.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* This file is part of libnvme.
* Copyright (c) 2026 Micron Technology, Inc.
*
* Cross-platform compatibility for unistd.h.
* Provides functionality that may be missing on some platforms.
* Compatibility is not comprehensive. Only functionality required by
* nvme-cli and libnvme is included.
*
* Authors: Brandon Busacker <[email protected]>
*/
#pragma once

#include <unistd.h>

#if defined(_WIN32)

#include <io.h>
#include <sysinfoapi.h>
#include <winsock2.h> /* for gethostname */

/* unistd.h POSIX compatibility */

#define fsync _commit

/* getpagesize implementation for Windows */
static inline int getpagesize(void)
{
SYSTEM_INFO si;

GetSystemInfo(&si);
return si.dwPageSize;
}

#endif
Loading
Loading