1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2010-2014 Intel Corporation
10 #include <rte_common.h>
11 #include <rte_random.h>
12 #include <rte_memcpy.h>
17 * Set this to the maximum buffer size you want to test. If it is 0, then the
18 * values in the buf_sizes[] array below will be used.
20 #define TEST_VALUE_RANGE 0
22 /* List of buffer sizes to test */
23 #if TEST_VALUE_RANGE == 0
24 static size_t buf_sizes[] = {
25 0, 1, 7, 8, 9, 15, 16, 17, 31, 32, 33, 63, 64, 65, 127, 128, 129, 255,
26 256, 257, 320, 384, 511, 512, 513, 1023, 1024, 1025, 1518, 1522, 1600,
27 2048, 3072, 4096, 5120, 6144, 7168, 8192
29 /* MUST be as large as largest packet size above */
30 #define SMALL_BUFFER_SIZE 8192
31 #else /* TEST_VALUE_RANGE != 0 */
32 static size_t buf_sizes[TEST_VALUE_RANGE];
33 #define SMALL_BUFFER_SIZE TEST_VALUE_RANGE
34 #endif /* TEST_VALUE_RANGE == 0 */
36 /* Data is aligned on this many bytes (power of 2) */
37 #define ALIGNMENT_UNIT 32
41 * Create two buffers, and initialise one with random values. These are copied
42 * to the second buffer and then compared to see if the copy was successful.
43 * The bytes outside the copied area are also checked to make sure they were not
47 test_single_memcpy(unsigned int off_src, unsigned int off_dst, size_t size)
50 uint8_t dest[SMALL_BUFFER_SIZE + ALIGNMENT_UNIT];
51 uint8_t src[SMALL_BUFFER_SIZE + ALIGNMENT_UNIT];
55 for (i = 0; i < SMALL_BUFFER_SIZE + ALIGNMENT_UNIT; i++) {
57 src[i] = (uint8_t) rte_rand();
61 ret = rte_memcpy(dest + off_dst, src + off_src, size);
62 if (ret != (dest + off_dst)) {
63 printf("rte_memcpy() returned %p, not %p\n",
67 /* Check nothing before offset is affected */
68 for (i = 0; i < off_dst; i++) {
70 printf("rte_memcpy() failed for %u bytes (offsets=%u,%u): "
71 "[modified before start of dst].\n",
72 (unsigned)size, off_src, off_dst);
77 /* Check everything was copied */
78 for (i = 0; i < size; i++) {
79 if (dest[i + off_dst] != src[i + off_src]) {
80 printf("rte_memcpy() failed for %u bytes (offsets=%u,%u): "
81 "[didn't copy byte %u].\n",
82 (unsigned)size, off_src, off_dst, i);
87 /* Check nothing after copy was affected */
88 for (i = size; i < SMALL_BUFFER_SIZE; i++) {
89 if (dest[i + off_dst] != 0) {
90 printf("rte_memcpy() failed for %u bytes (offsets=%u,%u): "
91 "[copied too many].\n",
92 (unsigned)size, off_src, off_dst);
100 * Check functionality for various buffer sizes and data offsets/alignments.
105 unsigned int off_src, off_dst, i;
106 unsigned int num_buf_sizes = sizeof(buf_sizes) / sizeof(buf_sizes[0]);
109 for (off_src = 0; off_src < ALIGNMENT_UNIT; off_src++) {
110 for (off_dst = 0; off_dst < ALIGNMENT_UNIT; off_dst++) {
111 for (i = 0; i < num_buf_sizes; i++) {
112 ret = test_single_memcpy(off_src, off_dst,
133 REGISTER_TEST_COMMAND(memcpy_autotest, test_memcpy);