Skip to content

Commit

Permalink
compile: avoid min macro pollution
Browse files Browse the repository at this point in the history
The generic min macro in util.h is too common and can conflict with
other libraries. Rename it to nvme_min to avoid pollution.

Before renaming the macro, the cpp's std::min will fail to compile.
```
./test/cpp.cc: In function ‘int min_compile_test()’:
../src/nvme/util.h:563:19: error: expected unqualified-id before ‘(’ token
  563 | #define min(x, y) ((x) > (y) ? (y) : (x))
```

Signed-off-by: Jian Zhang <[email protected]>
  • Loading branch information
zhangjian3032 committed Jan 10, 2025
1 parent e9c6fe6 commit 79a7219
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/nvme/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ static int read_ana_chunk(int fd, enum nvme_log_ana_lsp lsp, bool rae,
}

while (*read < to_read) {
__u32 len = min(log_end - *read, NVME_LOG_PAGE_PDU_SIZE);
__u32 len = nvme_min(log_end - *read, NVME_LOG_PAGE_PDU_SIZE);
int ret;

ret = nvme_get_log_ana(fd, lsp, rae, *read - log, len, *read);
Expand Down
2 changes: 1 addition & 1 deletion src/nvme/mi.c
Original file line number Diff line number Diff line change
Expand Up @@ -1021,7 +1021,7 @@ static int read_ana_chunk(nvme_mi_ctrl_t ctrl, enum nvme_log_ana_lsp lsp, bool r
}

while (*read < to_read) {
__u32 len = min(log_end - *read, NVME_LOG_PAGE_PDU_SIZE);
__u32 len = nvme_min(log_end - *read, NVME_LOG_PAGE_PDU_SIZE);
int ret;

ret = nvme_mi_admin_get_log_ana(ctrl, lsp, rae,
Expand Down
2 changes: 1 addition & 1 deletion src/nvme/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -1151,7 +1151,7 @@ void *__nvme_realloc(void *p, size_t len)
void *result = __nvme_alloc(len);

if (p && result) {
memcpy(result, p, min(old_len, len));
memcpy(result, p, nvme_min(old_len, len));
free(p);
}

Expand Down
2 changes: 1 addition & 1 deletion src/nvme/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ char *kv_keymatch(const char *kv, const char *key);
*/
char *startswith(const char *s, const char *prefix);

#define min(x, y) ((x) > (y) ? (y) : (x))
#define nvme_min(x, y) ((x) > (y) ? (y) : (x))

#define __round_mask(val, mult) ((__typeof__(val))((mult)-1))

Expand Down
14 changes: 14 additions & 0 deletions test/cpp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,20 @@
* Authors: Keith Busch <[email protected]>
*/

#include <algorithm>
#include <iostream>
#include <libnvme.h>

static int min_compile_test()
{
return std::min(1, 2);
}

static int max_compile_test()
{
return std::max(1, 2);
}

int main()
{
nvme_root_t r;
Expand Down Expand Up @@ -62,5 +73,8 @@ int main()
std::cout << "\n";
nvme_free_tree(r);

min_compile_test();
max_compile_test();

return 0;
}

0 comments on commit 79a7219

Please sign in to comment.