Skip to content

Commit 19c56ae

Browse files
Refactored stdlib.h compatiblity.
- Added nvme/stdlib.h - Renamed platform_aligned_free to aligned_free - Updated code to use new header - Removed stdlib compatiblity from windows.h and linux.h
1 parent a0c5221 commit 19c56ae

16 files changed

Lines changed: 81 additions & 65 deletions

File tree

libnvme/src/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ headers = [
3939
'nvme/linux.h',
4040
'nvme/nvme-cmds.h',
4141
'nvme/nvme-types.h',
42+
'nvme/stdlib.h',
4243
'nvme/tree.h',
4344
'nvme/types.h',
4445
'nvme/util.h',

libnvme/src/nvme/lib.h

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

1414
#include <nvme/lib-types.h>
15+
#include <nvme/stdlib.h>
1516

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

libnvme/src/nvme/stdlib.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2+
/*
3+
* Copyright (c) 2026 Micron Technology, Inc.
4+
*
5+
* Cross-platform compatibility for stdlib.h.
6+
* Provides functionality that may be missing on some platforms.
7+
* Compatibility is not comprehensive. Only functionality required by
8+
* nvme-cli and libnvme is included.
9+
*
10+
* Authors: Broc Going <[email protected]>
11+
* Brandon Capener <[email protected]>
12+
*/
13+
14+
#pragma once
15+
16+
#include <stdlib.h>
17+
18+
#if defined(_WIN32) || defined(_WIN64)
19+
20+
#include <malloc.h>
21+
22+
/* Aligned memory allocation function, use aligned_free to free. */
23+
static inline int posix_memalign(void **memptr, size_t alignment, size_t size)
24+
{
25+
*memptr = _aligned_malloc(size, alignment);
26+
return (*memptr == NULL) ? ENOMEM : 0;
27+
}
28+
29+
/* reallocarray implementation for Windows */
30+
static inline void *reallocarray(void *ptr, size_t nmemb, size_t size)
31+
{
32+
size_t total_size;
33+
34+
/* Check for multiplication overflow */
35+
if (nmemb != 0 && size > SIZE_MAX / nmemb) {
36+
errno = ENOMEM;
37+
return NULL;
38+
}
39+
40+
total_size = nmemb * size;
41+
return realloc(ptr, total_size);
42+
}
43+
44+
#endif
45+
46+
/*
47+
* Cross-platform compatible free for aligned memory allocations.
48+
* Use when posix_memalign is used to allocate memory.
49+
*/
50+
static inline void aligned_free(void *p)
51+
{
52+
#if defined(_WIN32) || defined(_WIN64)
53+
_aligned_free(p);
54+
#else
55+
free(p);
56+
#endif
57+
}

libnvme/src/nvme/util.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
#include <malloc.h>
1313
#include <stdbool.h>
1414
#include <stdio.h>
15-
#include <stdlib.h>
1615
#include <string.h>
1716
#include <unistd.h>
1817

@@ -27,6 +26,8 @@
2726
#include <sys/stat.h>
2827
#include <sys/types.h>
2928

29+
#include <nvme/stdlib.h>
30+
3031
#include <ccan/endian/endian.h>
3132
#include <ccan/minmax/minmax.h>
3233

@@ -983,7 +984,7 @@ void *__libnvme_realloc(void *p, size_t len)
983984

984985
void __libnvme_free(void *p)
985986
{
986-
platform_aligned_free(p);
987+
aligned_free(p);
987988
}
988989

989990
#ifdef CONFIG_FABRICS

libnvme/src/platform/linux.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,3 @@ static inline int random_uuid(unsigned char *uuid, size_t len)
6868
close(f);
6969
return ret;
7070
}
71-
72-
/*
73-
* Platform-specific free for aligned memory allocations.
74-
* Use when posix_memalign is used to allocate memory.
75-
*/
76-
static inline void platform_aligned_free(void *p)
77-
{
78-
free(p);
79-
}

libnvme/src/platform/windows.h

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

169169

170-
/* stdlib.h compatibility */
171-
172-
/* Aligned memory allocation function, use platform_aligned_free to free. */
173-
static inline int posix_memalign(void **memptr, size_t alignment, size_t size)
174-
{
175-
*memptr = _aligned_malloc(size, alignment);
176-
return (*memptr == NULL) ? ENOMEM : 0;
177-
}
178-
179-
/*
180-
* Platform-specific free for aligned memory allocations.
181-
* Use when posix_memalign is used to allocate memory.
182-
*/
183-
static inline void platform_aligned_free(void *p)
184-
{
185-
_aligned_free(p);
186-
}
187-
188-
/* reallocarray implementation for Windows */
189-
static inline void *reallocarray(void *ptr, size_t nmemb, size_t size)
190-
{
191-
size_t total_size;
192-
193-
/* Check for multiplication overflow */
194-
if (nmemb != 0 && size > SIZE_MAX / nmemb) {
195-
errno = ENOMEM;
196-
return NULL;
197-
}
198-
199-
total_size = nmemb * size;
200-
return realloc(ptr, total_size);
201-
}
202-
203-
204170
/* malloc.h compatibility*/
205171

206172
static inline size_t malloc_usable_size(void *ptr)

nvme-rpmb.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ static int read_file(const char *file, unsigned char **data, unsigned int *len)
194194
err = -errno;
195195
fprintf(stderr, "Failed to read data from file"
196196
" %s with %s\n", file, libnvme_strerror(errno));
197-
platform_aligned_free(buf);
197+
aligned_free(buf);
198198
goto out;
199199
}
200200
*data = buf;

plugins/memblaze/memblaze-nvme.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -861,7 +861,7 @@ static int mb_selective_download(int argc, char **argv, struct command *acmd, st
861861
}
862862

863863
out_free:
864-
platform_aligned_free(fw_buf);
864+
aligned_free(fw_buf);
865865
out_close:
866866
close(fw_fd);
867867
out:

plugins/micron/micron-nvme.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -686,7 +686,7 @@ static int micron_selective_download(int argc, char **argv,
686686
}
687687

688688
out_free:
689-
platform_aligned_free(fw_buf);
689+
aligned_free(fw_buf);
690690
out:
691691
close(fw_fd);
692692
return err;

plugins/netapp/netapp-nvme.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -831,12 +831,12 @@ static int netapp_ontapdevices_get_info(struct libnvme_transport_handle *hdl,
831831
fprintf(stderr, "Unable to identify namespace descriptor for %s (%s)\n",
832832
dev, err < 0 ? libnvme_strerror(-err) :
833833
libnvme_status_to_string(err, false));
834-
platform_aligned_free(nsdescs);
834+
aligned_free(nsdescs);
835835
return 0;
836836
}
837837

838838
memcpy(item->uuid, nsdescs + sizeof(struct nvme_ns_id_desc), sizeof(item->uuid));
839-
platform_aligned_free(nsdescs);
839+
aligned_free(nsdescs);
840840

841841
err = nvme_get_ontap_c2_log(hdl, item->nsid, item->log_data, ONTAP_C2_LOG_SIZE);
842842
if (err) {

0 commit comments

Comments
 (0)