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. use ccan/minmax in the source files to avoid pollution.

the ccan/minmax will check the type of the input parameters, so cast the
`u8* - u8*` to `int` to avoid the type mismatch.

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 13, 2025
1 parent e9c6fe6 commit 9d3a939
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/nvme/ioctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include <sys/time.h>

#include <ccan/build_assert/build_assert.h>
#include <ccan/ccan/minmax/minmax.h>
#include <ccan/endian/endian.h>

#include "ioctl.h"
Expand Down Expand Up @@ -503,7 +504,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 = min_t(int, log_end - *read, NVME_LOG_PAGE_PDU_SIZE);
int ret;

ret = nvme_get_log_ana(fd, lsp, rae, *read - log, len, *read);
Expand Down
3 changes: 2 additions & 1 deletion src/nvme/mi.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <unistd.h>

#include <ccan/array_size/array_size.h>
#include <ccan/ccan/minmax/minmax.h>
#include <ccan/endian/endian.h>

#include "log.h"
Expand Down Expand Up @@ -1021,7 +1022,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 = min_t(int, log_end - *read, NVME_LOG_PAGE_PDU_SIZE);
int ret;

ret = nvme_mi_admin_get_log_ana(ctrl, lsp, rae,
Expand Down
1 change: 1 addition & 0 deletions src/nvme/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <netdb.h>
#include <unistd.h>

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

#include "cleanup.h"
Expand Down
2 changes: 0 additions & 2 deletions src/nvme/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,6 @@ 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 __round_mask(val, mult) ((__typeof__(val))((mult)-1))

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

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

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

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

min_compile_test();

return 0;
}

0 comments on commit 9d3a939

Please sign in to comment.