-
Notifications
You must be signed in to change notification settings - Fork 131
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
compile: avoid min macro pollution #942
base: master
Are you sure you want to change the base?
compile: avoid min macro pollution #942
Conversation
src/nvme/util.h
Outdated
@@ -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)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should not expose this macro at all. And there should be already the min macro from the ccan 'library'. Can you drop this one and include the ccan/minmax.h header in the c files?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, we can, but we need cast the type, see updated code.
test/cpp.cc
Outdated
static int max_compile_test() | ||
{ | ||
return std::max(1, 2); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is unrelated with the above change.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yup, I think min and max should always appear in pairs, so I just added it, remove now.
79a7219
to
a485847
Compare
src/nvme/ioctl.c
Outdated
@@ -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((int)(log_end - *read), NVME_LOG_PAGE_PDU_SIZE); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be better to use min_t
here:
`min_t(int, log_end - *read, NVME_LOG_PAGE_PDU_SIZE);`
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
thanks, make sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, thinko on my side. The final type is __u32
, so we should also cast to __u32
and not int
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the result of subtracting two u8* pointers is a signed integer, use int
also makes sense
static int min_compile_test() | ||
{ | ||
return std::min(1, 2); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My point is that the whole change to test/cpp.c
should be in a different patch. I don't mind if you add the min
and max
to catch build errors, just in a separate patch.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it.
9d3a939
to
3d9d863
Compare
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. 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]>
3d9d863
to
5a9d3a4
Compare
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.