|
| 1 | +// Copyright 2022 Sebastian Kuzminsky <seb@highlab.com> |
| 2 | +// |
| 3 | +// This program is free software; you can redistribute it and/or |
| 4 | +// modify it under the terms of version 2 of the GNU General |
| 5 | +// Public License as published by the Free Software Foundation. |
| 6 | +// |
| 7 | +// This program is distributed in the hope that it will be useful, |
| 8 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | +// GNU General Public License for more details. |
| 11 | +// |
| 12 | +// You should have received a copy of the GNU General Public License |
| 13 | +// along with this program; if not, write to the Free Software |
| 14 | +// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 15 | + |
| 16 | +component test_shmem_rtcomp "Validate rtapi's shmem initialization behavior"; |
| 17 | + |
| 18 | +option extra_setup; |
| 19 | +option extra_cleanup; |
| 20 | +option singleton; |
| 21 | + |
| 22 | +pin out bit ready = 0 "Goes true when shmem is initialized"; |
| 23 | + |
| 24 | +function _; |
| 25 | +license "GPL"; |
| 26 | + |
| 27 | +;; |
| 28 | + |
| 29 | +#include <rtapi.h> |
| 30 | + |
| 31 | +#define SHMEM_KEY 9999 |
| 32 | +#define SHMEM_SIZE (4096 * 100) // in bytes |
| 33 | + |
| 34 | +int shmem_id = -1; |
| 35 | +void *mem = NULL; |
| 36 | + |
| 37 | +int shmem_alloc(void) { |
| 38 | + int i; |
| 39 | + int r; |
| 40 | + uint8_t *p; |
| 41 | + |
| 42 | + rtapi_print_msg(RTAPI_MSG_ERR, "allocating shmem\n"); |
| 43 | + |
| 44 | + shmem_id = rtapi_shmem_new(SHMEM_KEY, comp_id, SHMEM_SIZE); |
| 45 | + if (shmem_id < 0) { |
| 46 | + rtapi_print_msg(RTAPI_MSG_ERR, "failed to make new rtapi shmem\n"); |
| 47 | + return -EINVAL; |
| 48 | + } |
| 49 | + |
| 50 | + r = rtapi_shmem_getptr(shmem_id, &mem); |
| 51 | + if (r < 0) { |
| 52 | + rtapi_print_msg(RTAPI_MSG_ERR, "failed to get new rtapi shmem\n"); |
| 53 | + return -EINVAL; |
| 54 | + } |
| 55 | + if (mem == NULL) { |
| 56 | + rtapi_print_msg(RTAPI_MSG_ERR, "new rtapi shmem is NULL??\n"); |
| 57 | + return -EINVAL; |
| 58 | + } |
| 59 | + |
| 60 | + p = mem; |
| 61 | + for (i = 0; i < SHMEM_SIZE; ++i) { |
| 62 | + if (p[i] != 0x00) { |
| 63 | + rtapi_print_msg(RTAPI_MSG_ERR, "shmem is not initialized to 0x00!\n"); |
| 64 | + return -EINVAL; |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + rtapi_print_msg(RTAPI_MSG_ERR, "allocated shmem came initialized to all bytes zero\n"); |
| 69 | + |
| 70 | + return 0; |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +void shmem_free(void) { |
| 75 | + if (shmem_id >= 0) { |
| 76 | + rtapi_print_msg(RTAPI_MSG_ERR, "freeing shmem\n"); |
| 77 | + rtapi_shmem_delete(shmem_id, comp_id); |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +EXTRA_SETUP() { |
| 83 | + int i; |
| 84 | + int r; |
| 85 | + uint8_t *p; |
| 86 | + |
| 87 | + r = shmem_alloc(); |
| 88 | + if (r < 0) { |
| 89 | + rtapi_print_msg(RTAPI_MSG_ERR, "failed to alloc shmem\n"); |
| 90 | + return -EINVAL; |
| 91 | + } |
| 92 | + |
| 93 | + // dirty the shmem |
| 94 | + p = mem; |
| 95 | + for (i = 0; i < SHMEM_SIZE; ++i) { |
| 96 | + p[i] = i % 256; |
| 97 | + } |
| 98 | + |
| 99 | + shmem_free(); |
| 100 | + |
| 101 | + r = shmem_alloc(); |
| 102 | + if (r < 0) { |
| 103 | + rtapi_print_msg(RTAPI_MSG_ERR, "failed to alloc shmem\n"); |
| 104 | + return -EINVAL; |
| 105 | + } |
| 106 | + |
| 107 | + return 0; |
| 108 | +} |
| 109 | + |
| 110 | + |
| 111 | +EXTRA_CLEANUP() { |
| 112 | + shmem_free(); |
| 113 | +} |
| 114 | + |
| 115 | + |
| 116 | +FUNCTION(_) { |
| 117 | + ready = 1; |
| 118 | +} |
| 119 | + |
0 commit comments