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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! # Utility Functions for Bitwise Buddy Math
//!
//! This module provides the core bitwise and mathematical foundations required by the
//! buddy allocator. It handles fast power-of-two calculations, block size lookups,
//! alignment verification, and buddy address computation.
//!
//! All functions are decorated with `#[inline]` to ensure the compiler can optimize away
//! any function call overhead, generating direct assembly instructions.
/// Calculates the companion "buddy" address of a given block address at a specific order.
///
/// In a binary buddy allocator, a block and its buddy differ by exactly one bit—the bit
/// representing the size of the block at that order. XORing the block address with the
/// order size flips this bit, instantly revealing the buddy's address.
///
/// # Mathematical Principle
/// For a block at address $n$ and order $o$, its buddy is:
/// $$buddy = n \oplus (1 \ll o)$$
///
/// # Examples
/// If we have a block at address `0` at Order 0 (size 1):
/// * `0 ^ (1 << 0) => 0 ^ 1 = 1` (Page 0 and Page 1 are buddies).
/// * If we have a block at address `0` at Order 1 (size 2):
/// * `0 ^ (1 << 1) => 0 ^ 2 = 2` (Block 0-1 and Block 2-3 are buddies).
/// Computes the span/size of a single memory block at the specified order level.
///
/// Since the buddy system works in powers of two, the size at any given order
/// is computed as $2^{\text{order}}$.
/// Calculates $2^{\text{power}}$ using an efficient left-shift operation.
///
/// This is mathematically equivalent to $1 \ll \text{power}$.
///
/// # Safety / Limits
/// The caller must ensure that `power` does not exceed 63, otherwise this operation
/// will trigger an overflow / panic in debug mode, or wrap-around in release mode.
/// Determines whether a physical address/index `n` is aligned to the boundary of a specific order.
///
/// For a block to be valid at `order`, its address must be a multiple of the block's size
/// ($2^{\text{order}}$). This means the lowest `order` bits of the address must all be zeros.
///
/// # Bitwise Masking Mechanism
/// 1. We start with a full mask of 1s: `u64::MAX` (`0xFFFFFFFFFFFFFFFF`).
/// 2. Shifting it left by `order` creates a mask like `0xFFFFFFFFFFFFFFF0` (for order 4).
/// 3. Inverting this mask with `!` gives `0x000000000000000F`, isolating the lower offset bits.
/// 4. Performing a bitwise AND (`n & rm`) checks if any of these offset bits are set.
/// If the result is `0`, the address is perfectly aligned.
/// Helper utility to convert a target maximum order ceiling into a relative "reduction index".
///
/// This defines how many layers below `ORDER_COUNT` a memory block's merging potential must stop.