Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:49

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup sptests
0007  *
0008  * @brief Test that the task stack allocator works.
0009  */
0010 
0011 /*
0012  * Copyright (c) 2011 embedded brains GmbH & Co. KG
0013  *
0014  * Redistribution and use in source and binary forms, with or without
0015  * modification, are permitted provided that the following conditions
0016  * are met:
0017  * 1. Redistributions of source code must retain the above copyright
0018  *    notice, this list of conditions and the following disclaimer.
0019  * 2. Redistributions in binary form must reproduce the above copyright
0020  *    notice, this list of conditions and the following disclaimer in the
0021  *    documentation and/or other materials provided with the distribution.
0022  *
0023  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0024  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0025  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0026  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0027  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0028  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0029  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0030  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0031  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0032  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0033  * POSSIBILITY OF SUCH DAMAGE.
0034  */
0035 
0036 #ifdef HAVE_CONFIG_H
0037 #include "config.h"
0038 #endif
0039 
0040 #include <tmacros.h>
0041 
0042 const char rtems_test_name[] = "SPSTKALLOC 2";
0043 
0044 #include <stdio.h>
0045 #include <inttypes.h>
0046 
0047 #include <rtems/malloc.h>
0048 #include <rtems/score/heapimpl.h>
0049 
0050 #define TASK_COUNT 5
0051 
0052 #define STACK_HEAP_PAGE_SIZE 1024
0053 
0054 static Heap_Control task_stack_heap;
0055 
0056 static void task_stack_init(size_t stack_space_size);
0057 
0058 static void *task_stack_allocate(size_t stack_size);
0059 
0060 static void *task_stack_allocate_for_idle(uint32_t unused, size_t *stack_size);
0061 
0062 static void task_stack_free(void *addr);
0063 
0064 static void print_info(void)
0065 {
0066   Heap_Information_block info;
0067 
0068   _Heap_Get_information(&task_stack_heap, &info);
0069 
0070   printf(
0071     "used blocks = %" PRIuPTR ", "
0072     "largest used block = %" PRIuPTR ", "
0073     "used space = %" PRIuPTR "\n"
0074     "free blocks = %" PRIuPTR ", "
0075     "largest free block = %" PRIuPTR ", "
0076     "free space = %" PRIuPTR "\n",
0077     info.Used.number,
0078     info.Used.largest,
0079     info.Used.total,
0080     info.Free.number,
0081     info.Free.largest,
0082     info.Free.total
0083   );
0084 }
0085 
0086 static rtems_task Init(rtems_task_argument argument)
0087 {
0088   rtems_status_code sc = RTEMS_SUCCESSFUL;
0089   rtems_name name = rtems_build_name('S', 'T', 'K', 'A');
0090   rtems_id id = RTEMS_ID_NONE;
0091   int i = 0;
0092 
0093   TEST_BEGIN();
0094 
0095   print_info();
0096 
0097   for (i = 2; i < TASK_COUNT; ++i) {
0098     sc = rtems_task_create(
0099       name,
0100       RTEMS_MINIMUM_PRIORITY,
0101       RTEMS_MINIMUM_STACK_SIZE,
0102       RTEMS_DEFAULT_MODES,
0103       RTEMS_DEFAULT_ATTRIBUTES,
0104       &id
0105     );
0106     directive_failed(sc, "rtems_task_create");
0107   }
0108 
0109   sc = rtems_task_create(
0110     name,
0111     RTEMS_MINIMUM_PRIORITY,
0112     2 * RTEMS_MINIMUM_STACK_SIZE,
0113     RTEMS_DEFAULT_MODES,
0114     RTEMS_DEFAULT_ATTRIBUTES,
0115     &id
0116   );
0117   fatal_directive_status(sc, RTEMS_UNSATISFIED, "rtems_task_create");
0118 
0119   sc = rtems_task_create(
0120     name,
0121     RTEMS_MINIMUM_PRIORITY,
0122     RTEMS_MINIMUM_STACK_SIZE,
0123     RTEMS_DEFAULT_MODES,
0124     RTEMS_DEFAULT_ATTRIBUTES,
0125     &id
0126   );
0127   directive_failed(sc, "rtems_task_create");
0128 
0129   print_info();
0130 
0131   TEST_END();
0132 
0133   rtems_test_exit(0);
0134 }
0135 
0136 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0137 
0138 /*
0139  * Avoid a dependency on errno which might be a thread-local object.  This test
0140  * assumes that no thread-local storage object is present.
0141  */
0142 #define CONFIGURE_APPLICATION_DISABLE_FILESYSTEM
0143 
0144 /*
0145  * This test requires full control over the present thread-local objects.  This
0146  * is necessary for the custom stack allocator below.  In certain Newlib
0147  * configurations, the Newlib reentrancy support may add thread-local objects.
0148  */
0149 #define CONFIGURE_DISABLE_NEWLIB_REENTRANCY
0150 
0151 #define CONFIGURE_MAXIMUM_TASKS TASK_COUNT
0152 
0153 #define CONFIGURE_TASK_STACK_ALLOCATOR_INIT task_stack_init
0154 #define CONFIGURE_TASK_STACK_ALLOCATOR task_stack_allocate
0155 #define CONFIGURE_TASK_STACK_ALLOCATOR_FOR_IDLE task_stack_allocate_for_idle
0156 #define CONFIGURE_TASK_STACK_DEALLOCATOR task_stack_free
0157 #define CONFIGURE_TASK_STACK_ALLOCATOR_AVOIDS_WORK_SPACE
0158 #define CONFIGURE_TASK_STACK_FROM_ALLOCATOR(stack_size) \
0159   ((stack_size) + HEAP_BLOCK_HEADER_SIZE + STACK_HEAP_PAGE_SIZE - 1)
0160 
0161 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0162 
0163 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0164 
0165 #define CONFIGURE_INIT
0166 
0167 #include <rtems/confdefs.h>
0168 
0169 static char task_stack_space
0170   [CONFIGURE_TASK_STACK_FROM_ALLOCATOR(_CONFIGURE_STACK_SPACE_SIZE)];
0171 
0172 static void task_stack_init(size_t stack_space_size)
0173 {
0174   bool ok = _Heap_Initialize(
0175     &task_stack_heap,
0176     task_stack_space,
0177     sizeof(task_stack_space),
0178     STACK_HEAP_PAGE_SIZE
0179   );
0180 
0181   rtems_test_assert(stack_space_size == _CONFIGURE_STACK_SPACE_SIZE);
0182   rtems_test_assert(ok);
0183 }
0184 
0185 static void *task_stack_allocate(size_t stack_size)
0186 {
0187   return _Heap_Allocate(&task_stack_heap, stack_size);
0188 }
0189 
0190 static void *task_stack_allocate_for_idle(uint32_t unused, size_t *stack_size)
0191 {
0192   return rtems_heap_allocate_aligned_with_boundary(
0193     *stack_size,
0194     CPU_STACK_ALIGNMENT,
0195     0
0196   );
0197 }
0198 
0199 static void task_stack_free(void *addr)
0200 {
0201   _Heap_Free(&task_stack_heap, addr);
0202 }