Alignment
provide.foundation.file.alignment
¶
Memory and file alignment utilities for binary I/O and mmap operations.
Provides functions for aligning offsets to power-of-2 boundaries, which is critical for: - Memory-mapped file operations (mmap requires page alignment) - Binary file formats and protocols - Database and index structures - Network packet alignment
Functions¶
align_offset
¶
Align offset to specified boundary.
Aligns an offset up to the next boundary. The alignment must be a power of 2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
int
|
The offset to align (in bytes) |
required |
alignment
|
int
|
Alignment boundary in bytes (must be power of 2) |
DEFAULT_ALIGNMENT
|
Returns:
| Type | Description |
|---|---|
int
|
Aligned offset (>= input offset) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If alignment is not a power of 2 or is <= 0 |
Examples:
>>> align_offset(10, 16)
16
>>> align_offset(16, 16)
16
>>> align_offset(17, 16)
32
>>> align_offset(0, 16)
0
Notes
Uses bit manipulation for efficiency: aligned = (offset + alignment - 1) & ~(alignment - 1)
Source code in provide/foundation/file/alignment.py
align_to_page
¶
Align offset to page boundary for optimal mmap performance.
Page alignment is required for memory-mapped file operations on most systems. Common page sizes: - 4KB (4096 bytes): Most x86_64 systems, Linux, Windows - 16KB (16384 bytes): Apple Silicon (M1/M2/M3), some ARM64 systems
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
int
|
The offset to align (in bytes) |
required |
page_size
|
int
|
Page size in bytes (default: 4096) |
PAGE_SIZE_4K
|
Returns:
| Type | Description |
|---|---|
int
|
Page-aligned offset (>= input offset) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If page_size is not a power of 2 |
Examples:
>>> align_to_page(100)
4096
>>> align_to_page(4096)
4096
>>> align_to_page(4097)
8192
>>> align_to_page(100, page_size=16384)
16384
See Also
get_system_page_size() for detecting the system's page size
Source code in provide/foundation/file/alignment.py
calculate_padding
¶
Calculate padding bytes needed to align to boundary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
current_offset
|
int
|
Current offset position (in bytes) |
required |
alignment
|
int
|
Desired alignment boundary (in bytes) |
DEFAULT_ALIGNMENT
|
Returns:
| Type | Description |
|---|---|
int
|
Number of padding bytes needed (0 if already aligned) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If alignment is not a power of 2 or is <= 0 |
Examples:
>>> calculate_padding(10, 16)
6
>>> calculate_padding(16, 16)
0
>>> calculate_padding(17, 16)
15
>>> calculate_padding(100, 64)
28
Notes
This is useful when writing binary formats where you need to insert padding bytes to maintain alignment.
Source code in provide/foundation/file/alignment.py
get_system_page_size
¶
Get the system's page size.
Returns:
| Type | Description |
|---|---|
int
|
Page size in bytes (typically 4096 or 16384) |
Examples:
Notes
Uses os.sysconf('SC_PAGE_SIZE') on Unix-like systems. Falls back to PAGE_SIZE_4K if detection fails.
Source code in provide/foundation/file/alignment.py
is_aligned
¶
Check if offset is aligned to boundary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
offset
|
int
|
The offset to check (in bytes) |
required |
alignment
|
int
|
Alignment boundary in bytes |
DEFAULT_ALIGNMENT
|
Returns:
| Type | Description |
|---|---|
bool
|
True if offset is aligned to the boundary |
Raises:
| Type | Description |
|---|---|
ValueError
|
If alignment is not a power of 2 or is <= 0 |
Examples:
>>> is_aligned(16, 16)
True
>>> is_aligned(17, 16)
False
>>> is_aligned(0, 16)
True
>>> is_aligned(4096, 4096)
True
Notes
Uses bit manipulation for efficiency: is_aligned = (offset & (alignment - 1)) == 0
Source code in provide/foundation/file/alignment.py
is_power_of_two
¶
Check if a value is a power of 2.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int
|
Value to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if value is a power of 2 |
Examples:
>>> is_power_of_two(16)
True
>>> is_power_of_two(17)
False
>>> is_power_of_two(4096)
True
>>> is_power_of_two(0)
False
Notes
Uses bit manipulation: (value & (value - 1)) == 0