|
| 1 | +// SPDX-License-Identifier: BSD-3-Clause |
| 2 | +// |
| 3 | +// Copyright(c) 2025 Intel Corporation. |
| 4 | + |
| 5 | +#define DATA_SIZE 5 |
| 6 | +#define ALIGNED_SIZE ALIGN_UP(DATA_SIZE, sizeof(int)) |
| 7 | + |
| 8 | +#include <zephyr/ztest.h> |
| 9 | +#include <sof/batch.h> |
| 10 | +#include <sof/common.h> |
| 11 | +#include <sof/list.h> |
| 12 | +#include <stdlib.h> |
| 13 | +#include <string.h> |
| 14 | + |
| 15 | +void *__wrap_rzalloc(uint32_t flags, size_t bytes) |
| 16 | +{ |
| 17 | + (void)flags; |
| 18 | + |
| 19 | + void *ret = malloc(bytes); |
| 20 | + |
| 21 | + if (ret) |
| 22 | + memset(ret, 0, bytes); |
| 23 | + |
| 24 | + return ret; |
| 25 | +} |
| 26 | + |
| 27 | +ZTEST(batch_suite, test_batch_wrong_size) |
| 28 | +{ |
| 29 | + struct list_item head = LIST_INIT(head); |
| 30 | + /* new batch of 2 blocks */ |
| 31 | + uint8_t *block1 = batch_alloc(&head, DATA_SIZE); |
| 32 | + /* should fail because of a different size */ |
| 33 | + uint8_t *block2 = batch_alloc(&head, DATA_SIZE + 1); |
| 34 | + /* second block in the first batch */ |
| 35 | + uint8_t *block3 = batch_alloc(&head, DATA_SIZE); |
| 36 | + /* new batch of 4 blocks */ |
| 37 | + uint8_t *block4 = batch_alloc(&head, DATA_SIZE); |
| 38 | + /* should fail because of a different size */ |
| 39 | + uint8_t *block5 = batch_alloc(&head, DATA_SIZE * 2); |
| 40 | + |
| 41 | + zassert_not_null(block1); |
| 42 | + zassert_is_null(block2); |
| 43 | + zassert_not_null(block3); |
| 44 | + zassert_not_null(block4); |
| 45 | + zassert_is_null(block5); |
| 46 | + |
| 47 | + zassert_not_ok(batch_free(&head, block1 + 1)); |
| 48 | + zassert_ok(batch_free(&head, block1)); |
| 49 | + zassert_not_ok(batch_free(&head, block3 + 1)); |
| 50 | + zassert_ok(batch_free(&head, block3)); |
| 51 | + zassert_not_ok(batch_free(&head, block4 + 1)); |
| 52 | + zassert_ok(batch_free(&head, block4)); |
| 53 | +} |
| 54 | + |
| 55 | +ZTEST(batch_suite, test_batch) |
| 56 | +{ |
| 57 | + struct list_item head = LIST_INIT(head); |
| 58 | + void *blocks[62]; /* 2 + 4 + 8 + 16 + 32 */ |
| 59 | + unsigned int k = 0; |
| 60 | + |
| 61 | + /* Loop over all powers: 2^1..2^5 */ |
| 62 | + for (unsigned int i = 1; i <= 5; i++) { |
| 63 | + unsigned int n = 1 << i; |
| 64 | + uint8_t *start; |
| 65 | + |
| 66 | + for (unsigned int j = 0; j < n; j++) { |
| 67 | + uint8_t *block = batch_alloc(&head, DATA_SIZE); |
| 68 | + |
| 69 | + zassert_not_null(block, "allocation failed loop %u iter %u", i, j); |
| 70 | + |
| 71 | + if (!j) |
| 72 | + start = block; |
| 73 | + else |
| 74 | + zassert_equal(block, start + ALIGNED_SIZE * j, "wrong pointer"); |
| 75 | + |
| 76 | + blocks[k++] = block; |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + while (k--) |
| 81 | + zassert_ok(batch_free(&head, blocks[k]), "free failed"); |
| 82 | +} |
| 83 | + |
| 84 | +ZTEST_SUITE(batch_suite, NULL, NULL, NULL, NULL, NULL); |
0 commit comments