devnum-util: add macros to safely convert dev_t to pointers and back
Sometimes it's nice being able to store dev_t as pointer values in hashmaps/tables, instead of having to allocate memory for them and using devt_hash_ops. After all dev_t is weird on Linux/glibc: glibc defines it as 64bit entity (which hence appears as something we cannot encode in a pointer value for compat with 32bit archs) but it actually is 32bit in the kernel apis. Hence we can safely cut off the upper 32bit, and still retain compat with all archs. But let's hide this in new macros, and validate this is all correct via a test.
This commit is contained in:
@@ -121,4 +121,21 @@ TEST(devnum_format_str) {
|
||||
test_devnum_format_str_one(makedev(4095, 1048575), "4095:1048575");
|
||||
}
|
||||
|
||||
TEST(devnum_to_ptr) {
|
||||
dev_t m = makedev(0, 0);
|
||||
ASSERT_EQ(major(m), 0U);
|
||||
ASSERT_EQ(minor(m), 0U);
|
||||
ASSERT_EQ(m, PTR_TO_DEVNUM(DEVNUM_TO_PTR(m)));
|
||||
|
||||
m = makedev(DEVNUM_MAJOR_MAX, DEVNUM_MINOR_MAX);
|
||||
ASSERT_EQ(major(m), DEVNUM_MAJOR_MAX);
|
||||
ASSERT_EQ(minor(m), DEVNUM_MINOR_MAX);
|
||||
ASSERT_EQ(m, PTR_TO_DEVNUM(DEVNUM_TO_PTR(m)));
|
||||
|
||||
m = makedev(5, 8);
|
||||
ASSERT_EQ(major(m), 5U);
|
||||
ASSERT_EQ(minor(m), 8U);
|
||||
ASSERT_EQ(m, PTR_TO_DEVNUM(DEVNUM_TO_PTR(m)));
|
||||
}
|
||||
|
||||
DEFINE_TEST_MAIN(LOG_INFO);
|
||||
|
||||
Reference in New Issue
Block a user