tools/testing/memblock/tests/alloc_helpers_api.c
Source file repositories/reference/linux-study-clean/tools/testing/memblock/tests/alloc_helpers_api.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/memblock/tests/alloc_helpers_api.c- Extension
.c- Size
- 10799 bytes
- Lines
- 415
- Domain
- Support Tooling And Documentation
- Bucket
- tools
- Inferred role
- Support Tooling And Documentation: implementation source
- Status
- source implementation candidate
Why This File Exists
Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
alloc_helpers_api.h
Detected Declarations
function alloc_from_simple_generic_checkfunction alloc_from_misaligned_generic_checkfunction alloc_from_top_down_high_addr_checkfunction alloc_from_top_down_no_space_above_checkfunction alloc_from_top_down_min_addr_cap_checkfunction alloc_from_bottom_up_high_addr_checkfunction alloc_from_bottom_up_no_space_above_checkfunction alloc_from_bottom_up_min_addr_cap_checkfunction alloc_from_simple_checkfunction alloc_from_misaligned_checkfunction alloc_from_high_addr_checkfunction alloc_from_no_space_above_checkfunction alloc_from_min_addr_cap_checkfunction memblock_alloc_helpers_checks
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0-or-later
#include "alloc_helpers_api.h"
/*
* A simple test that tries to allocate a memory region above a specified,
* aligned address:
*
* +
* | +-----------+ |
* | | rgn | |
* +----------+-----------+---------+
* ^
* |
* Aligned min_addr
*
* Expect to allocate a cleared region at the minimal memory address.
*/
static int alloc_from_simple_generic_check(void)
{
struct memblock_region *rgn = &memblock.reserved.regions[0];
void *allocated_ptr = NULL;
phys_addr_t size = SZ_16;
phys_addr_t min_addr;
PREFIX_PUSH();
setup_memblock();
min_addr = memblock_end_of_DRAM() - SMP_CACHE_BYTES;
allocated_ptr = memblock_alloc_from(size, SMP_CACHE_BYTES, min_addr);
ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, size);
ASSERT_EQ(rgn->size, size);
ASSERT_EQ(rgn->base, min_addr);
ASSERT_EQ(memblock.reserved.cnt, 1);
ASSERT_EQ(memblock.reserved.total_size, size);
test_pass_pop();
return 0;
}
/*
* A test that tries to allocate a memory region above a certain address.
* The minimal address here is not aligned:
*
* + +
* | + +---------+ |
* | | | rgn | |
* +------+------+---------+------------+
* ^ ^------.
* | |
* min_addr Aligned address
* boundary
*
* Expect to allocate a cleared region at the closest aligned memory address.
*/
static int alloc_from_misaligned_generic_check(void)
{
struct memblock_region *rgn = &memblock.reserved.regions[0];
void *allocated_ptr = NULL;
phys_addr_t size = SZ_32;
phys_addr_t min_addr;
PREFIX_PUSH();
setup_memblock();
/* A misaligned address */
min_addr = memblock_end_of_DRAM() - (SMP_CACHE_BYTES * 2 - 1);
allocated_ptr = memblock_alloc_from(size, SMP_CACHE_BYTES, min_addr);
ASSERT_NE(allocated_ptr, NULL);
ASSERT_MEM_EQ(allocated_ptr, 0, size);
ASSERT_EQ(rgn->size, size);
ASSERT_EQ(rgn->base, memblock_end_of_DRAM() - SMP_CACHE_BYTES);
ASSERT_EQ(memblock.reserved.cnt, 1);
ASSERT_EQ(memblock.reserved.total_size, size);
test_pass_pop();
return 0;
}
/*
Annotation
- Immediate include surface: `alloc_helpers_api.h`.
- Detected declarations: `function alloc_from_simple_generic_check`, `function alloc_from_misaligned_generic_check`, `function alloc_from_top_down_high_addr_check`, `function alloc_from_top_down_no_space_above_check`, `function alloc_from_top_down_min_addr_cap_check`, `function alloc_from_bottom_up_high_addr_check`, `function alloc_from_bottom_up_no_space_above_check`, `function alloc_from_bottom_up_min_addr_cap_check`, `function alloc_from_simple_check`, `function alloc_from_misaligned_check`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.