tools/testing/memblock/tests/alloc_api.c
Source file repositories/reference/linux-study-clean/tools/testing/memblock/tests/alloc_api.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/memblock/tests/alloc_api.c- Extension
.c- Size
- 22397 bytes
- Lines
- 885
- 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_api.h
Detected Declarations
function get_memblock_alloc_namefunction alloc_top_down_simple_checkfunction alloc_top_down_disjoint_checkfunction blockfunction blockfunction alloc_top_down_second_fit_checkfunction alloc_in_between_generic_checkfunction alloc_small_gaps_generic_checkfunction alloc_all_reserved_generic_checkfunction alloc_no_space_generic_checkfunction alloc_limited_space_generic_checkfunction registeredfunction memoryfunction alloc_bottom_up_simple_checkfunction alloc_bottom_up_disjoint_checkfunction blockfunction blockfunction alloc_bottom_up_second_fit_checkfunction alloc_simple_checkfunction alloc_disjoint_checkfunction alloc_before_checkfunction alloc_after_checkfunction alloc_in_between_checkfunction alloc_second_fit_checkfunction alloc_small_gaps_checkfunction alloc_all_reserved_checkfunction alloc_no_space_checkfunction alloc_limited_space_checkfunction alloc_no_memory_checkfunction alloc_too_large_checkfunction memblock_alloc_checks_internalfunction memblock_alloc_checks
Annotated Snippet
// SPDX-License-Identifier: GPL-2.0-or-later
#include "alloc_api.h"
static int alloc_test_flags = TEST_F_NONE;
static inline const char * const get_memblock_alloc_name(int flags)
{
if (flags & TEST_F_RAW)
return "memblock_alloc_raw";
return "memblock_alloc";
}
static inline void *run_memblock_alloc(phys_addr_t size, phys_addr_t align)
{
if (alloc_test_flags & TEST_F_RAW)
return memblock_alloc_raw(size, align);
return memblock_alloc(size, align);
}
/*
* A simple test that tries to allocate a small memory region.
* Expect to allocate an aligned region near the end of the available memory.
*/
static int alloc_top_down_simple_check(void)
{
struct memblock_region *rgn = &memblock.reserved.regions[0];
void *allocated_ptr = NULL;
phys_addr_t size = SZ_2;
phys_addr_t expected_start;
PREFIX_PUSH();
setup_memblock();
expected_start = memblock_end_of_DRAM() - SMP_CACHE_BYTES;
allocated_ptr = run_memblock_alloc(size, SMP_CACHE_BYTES);
ASSERT_NE(allocated_ptr, NULL);
assert_mem_content(allocated_ptr, size, alloc_test_flags);
ASSERT_EQ(rgn->size, size);
ASSERT_EQ(rgn->base, expected_start);
ASSERT_EQ(memblock.reserved.cnt, 1);
ASSERT_EQ(memblock.reserved.total_size, size);
test_pass_pop();
return 0;
}
/*
* A test that tries to allocate memory next to a reserved region that starts at
* the misaligned address. Expect to create two separate entries, with the new
* entry aligned to the provided alignment:
*
* +
* | +--------+ +--------|
* | | rgn2 | | rgn1 |
* +------------+--------+---------+--------+
* ^
* |
* Aligned address boundary
*
* The allocation direction is top-down and region arrays are sorted from lower
* to higher addresses, so the new region will be the first entry in
* memory.reserved array. The previously reserved region does not get modified.
* Region counter and total size get updated.
*/
static int alloc_top_down_disjoint_check(void)
{
/* After allocation, this will point to the "old" region */
struct memblock_region *rgn1 = &memblock.reserved.regions[1];
struct memblock_region *rgn2 = &memblock.reserved.regions[0];
struct region r1;
void *allocated_ptr = NULL;
phys_addr_t r2_size = SZ_16;
/* Use custom alignment */
phys_addr_t alignment = SMP_CACHE_BYTES * 2;
phys_addr_t total_size;
phys_addr_t expected_start;
PREFIX_PUSH();
setup_memblock();
r1.base = memblock_end_of_DRAM() - SZ_2;
r1.size = SZ_2;
total_size = r1.size + r2_size;
expected_start = memblock_end_of_DRAM() - alignment;
Annotation
- Immediate include surface: `alloc_api.h`.
- Detected declarations: `function get_memblock_alloc_name`, `function alloc_top_down_simple_check`, `function alloc_top_down_disjoint_check`, `function block`, `function block`, `function alloc_top_down_second_fit_check`, `function alloc_in_between_generic_check`, `function alloc_small_gaps_generic_check`, `function alloc_all_reserved_generic_check`, `function alloc_no_space_generic_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.