| /* |
| * Copyright (c) 2016 Intel Corporation |
| * |
| * SPDX-License-Identifier: Apache-2.0 |
| */ |
| |
| #include <ztest.h> |
| #include "test_mheap.h" |
| |
| /*test cases*/ |
| |
| /** |
| * @brief Test to demonstrate k_malloc() and k_free() API usage |
| * |
| * @ingroup kernel_heap_tests |
| * |
| * @details The test allocates 4 blocks from heap memory pool |
| * using k_malloc() API. It also tries to allocate a block of size |
| * 64 bytes which fails as all the memory is allocated up. It then |
| * validates k_free() API by freeing up all the blocks which were |
| * allocated from the heap memory. |
| * |
| * @see k_malloc() |
| */ |
| void test_mheap_malloc_free(void) |
| { |
| void *block[2 * BLK_NUM_MAX], *block_fail; |
| int nb; |
| |
| for (nb = 0; nb < ARRAY_SIZE(block); nb++) { |
| /** |
| * TESTPOINT: This routine provides traditional malloc() |
| * semantics. Memory is allocated from the heap memory pool. |
| */ |
| block[nb] = k_malloc(BLK_SIZE_MIN); |
| if (block[nb] == NULL) { |
| break; |
| } |
| } |
| |
| block_fail = k_malloc(BLK_SIZE_MIN); |
| /** TESTPOINT: Return NULL if fail.*/ |
| zassert_is_null(block_fail, NULL); |
| |
| for (int i = 0; i < nb; i++) { |
| /** |
| * TESTPOINT: This routine provides traditional free() |
| * semantics. The memory being returned must have been allocated |
| * from the heap memory pool. |
| */ |
| k_free(block[i]); |
| } |
| /** TESTPOINT: If ptr is NULL, no operation is performed.*/ |
| k_free(NULL); |
| } |
| |
| #define NMEMB 8 |
| #define SIZE 16 |
| #define BOUNDS (NMEMB * SIZE) |
| |
| /** |
| * @brief Test to demonstrate k_calloc() API functionality. |
| * |
| * @ingroup kernel_heap_tests |
| * |
| * @details The test validates k_calloc() API. The 8 blocks of memory of |
| * size 16 bytes are allocated by k_calloc() API. When allocated using |
| * k_calloc() the memory buffers have to be zeroed. Check is done, if the |
| * blocks are memset to 0 and read/write is allowed. The test is then |
| * teared up by freeing all the blocks allocated. |
| * |
| * @see k_calloc() |
| */ |
| void test_mheap_calloc(void) |
| { |
| char *mem; |
| |
| mem = k_calloc(NMEMB, SIZE); |
| |
| zassert_not_null(mem, "calloc operation failed"); |
| |
| /* Memory should be zeroed and not crash us if we read/write to it */ |
| for (int i = 0; i < BOUNDS; i++) { |
| zassert_equal(mem[i], 0, NULL); |
| mem[i] = 1; |
| } |
| |
| k_free(mem); |
| } |
| |
| void test_k_aligned_alloc(void) |
| { |
| void *r; |
| |
| /* |
| * Allow sizes that are not necessarily a multiple of the |
| * alignment. The backing allocator would naturally round up to |
| * some minimal block size. This would make k_aligned_alloc() |
| * more like posix_memalign() instead of aligned_alloc(), but |
| * the benefit is that k_malloc() can then just be a wrapper |
| * around k_aligned_alloc(). |
| */ |
| r = k_aligned_alloc(sizeof(void *), 1); |
| /* allocation succeeds */ |
| zassert_not_equal(NULL, r, "aligned alloc of 1 byte failed"); |
| /* r is suitably aligned */ |
| zassert_equal(0, (uintptr_t)r % sizeof(void *), |
| "%p not %u-byte-aligned", |
| r, sizeof(void *)); |
| k_free(r); |
| |
| /* allocate with > 8 byte alignment */ |
| r = k_aligned_alloc(16, 1); |
| /* allocation succeeds */ |
| zassert_not_equal(NULL, r, "16-byte-aligned alloc failed"); |
| /* r is suitably aligned */ |
| zassert_equal(0, (uintptr_t)r % 16, |
| "%p not 16-byte-aligned", r); |
| k_free(r); |
| } |