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.

Dependency Surface

Detected Declarations

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

Implementation Notes