-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathhash-utils.h
76 lines (63 loc) · 2.01 KB
/
hash-utils.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/* SPDX-License-Identifier: GPL-2.0-only */
/*
* Copyright Red Hat
*/
#ifndef HASH_UTILS_H
#define HASH_UTILS_H 1
#include "compiler.h"
#include "common.h"
#include "geometry.h"
#include "numeric.h"
#include "uds.h"
/* How various portions of a chunk name are apportioned. */
enum {
VOLUME_INDEX_BYTES_OFFSET = 0,
VOLUME_INDEX_BYTES_COUNT = 8,
CHAPTER_INDEX_BYTES_OFFSET = 8,
CHAPTER_INDEX_BYTES_COUNT = 6,
SAMPLE_BYTES_OFFSET = 14,
SAMPLE_BYTES_COUNT = 2,
};
static INLINE uint64_t
extract_chapter_index_bytes(const struct uds_chunk_name *name)
{
const byte *chapter_bits = &name->name[CHAPTER_INDEX_BYTES_OFFSET];
uint64_t bytes = (uint64_t) get_unaligned_be16(chapter_bits) << 32;
bytes |= get_unaligned_be32(chapter_bits + 2);
return bytes;
}
static INLINE uint64_t
extract_volume_index_bytes(const struct uds_chunk_name *name)
{
return get_unaligned_be64(&name->name[VOLUME_INDEX_BYTES_OFFSET]);
}
static INLINE uint32_t
extract_sampling_bytes(const struct uds_chunk_name *name)
{
return get_unaligned_be16(&name->name[SAMPLE_BYTES_OFFSET]);
}
/* Compute the chapter delta list for a given name. */
static INLINE unsigned int
hash_to_chapter_delta_list(const struct uds_chunk_name *name,
const struct geometry *geometry)
{
return (unsigned int) ((extract_chapter_index_bytes(name) >>
geometry->chapter_address_bits) &
((1 << geometry->chapter_delta_list_bits) - 1));
}
/* Compute the chapter delta address for a given name. */
static INLINE unsigned int
hash_to_chapter_delta_address(const struct uds_chunk_name *name,
const struct geometry *geometry)
{
return (unsigned int) (extract_chapter_index_bytes(name) &
((1 << geometry->chapter_address_bits) - 1));
}
static INLINE unsigned int name_to_hash_slot(const struct uds_chunk_name *name,
unsigned int slot_count)
{
return (unsigned int) (extract_chapter_index_bytes(name) % slot_count);
}
unsigned int __must_check compute_bits(unsigned int max_value);
void hash_utils_compile_time_assertions(void);
#endif /* HASH_UTILS_H */