1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright 2023 Red Hat
6 #ifndef UDS_HASH_UTILS_H
7 #define UDS_HASH_UTILS_H
14 /* Utilities for extracting portions of a request name for various uses. */
16 /* How various portions of a record name are apportioned. */
18 VOLUME_INDEX_BYTES_OFFSET
= 0,
19 VOLUME_INDEX_BYTES_COUNT
= 8,
20 CHAPTER_INDEX_BYTES_OFFSET
= 8,
21 CHAPTER_INDEX_BYTES_COUNT
= 6,
22 SAMPLE_BYTES_OFFSET
= 14,
23 SAMPLE_BYTES_COUNT
= 2,
26 static inline u64
uds_extract_chapter_index_bytes(const struct uds_record_name
*name
)
28 const u8
*chapter_bits
= &name
->name
[CHAPTER_INDEX_BYTES_OFFSET
];
29 u64 bytes
= (u64
) get_unaligned_be16(chapter_bits
) << 32;
31 bytes
|= get_unaligned_be32(chapter_bits
+ 2);
35 static inline u64
uds_extract_volume_index_bytes(const struct uds_record_name
*name
)
37 return get_unaligned_be64(&name
->name
[VOLUME_INDEX_BYTES_OFFSET
]);
40 static inline u32
uds_extract_sampling_bytes(const struct uds_record_name
*name
)
42 return get_unaligned_be16(&name
->name
[SAMPLE_BYTES_OFFSET
]);
45 /* Compute the chapter delta list for a given name. */
46 static inline u32
uds_hash_to_chapter_delta_list(const struct uds_record_name
*name
,
47 const struct index_geometry
*geometry
)
49 return ((uds_extract_chapter_index_bytes(name
) >> geometry
->chapter_address_bits
) &
50 ((1 << geometry
->chapter_delta_list_bits
) - 1));
53 /* Compute the chapter delta address for a given name. */
54 static inline u32
uds_hash_to_chapter_delta_address(const struct uds_record_name
*name
,
55 const struct index_geometry
*geometry
)
57 return uds_extract_chapter_index_bytes(name
) & ((1 << geometry
->chapter_address_bits
) - 1);
60 static inline unsigned int uds_name_to_hash_slot(const struct uds_record_name
*name
,
61 unsigned int slot_count
)
63 return (unsigned int) (uds_extract_chapter_index_bytes(name
) % slot_count
);
66 #endif /* UDS_HASH_UTILS_H */