[843ad7b] | 1 | /* |
---|
[6f27ba8] | 2 | * COPYRIGHT (c) 1989-2011. |
---|
[843ad7b] | 3 | * On-Line Applications Research Corporation (OAR). |
---|
| 4 | * |
---|
| 5 | * The license and distribution terms for this file may be |
---|
| 6 | * found in the file LICENSE in this distribution or at |
---|
[c499856] | 7 | * http://www.rtems.org/license/LICENSE. |
---|
[843ad7b] | 8 | */ |
---|
| 9 | |
---|
[7d3f9c6] | 10 | #ifdef HAVE_CONFIG_H |
---|
| 11 | #include "config.h" |
---|
| 12 | #endif |
---|
| 13 | |
---|
[843ad7b] | 14 | #include <tmacros.h> |
---|
| 15 | #include "test_support.h" |
---|
| 16 | #include <rtems/libcsupport.h> |
---|
| 17 | #include <rtems/malloc.h> |
---|
[830c6ee] | 18 | #include <errno.h> |
---|
[843ad7b] | 19 | |
---|
[f8b2eb03] | 20 | const char rtems_test_name[] = "MALLOC 4"; |
---|
| 21 | |
---|
[07b93de] | 22 | /* configuration information */ |
---|
| 23 | /* At top of file to have access to configuration variables */ |
---|
| 24 | |
---|
[c4b8b147] | 25 | #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER |
---|
[07b93de] | 26 | #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER |
---|
| 27 | |
---|
| 28 | #define CONFIGURE_MAXIMUM_TASKS 1 |
---|
[f8b2eb03] | 29 | #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION |
---|
| 30 | |
---|
[07b93de] | 31 | #define CONFIGURE_RTEMS_INIT_TASKS_TABLE |
---|
| 32 | |
---|
| 33 | #define CONFIGURE_INIT |
---|
| 34 | #include <rtems/confdefs.h> |
---|
| 35 | /* end of configuration */ |
---|
| 36 | |
---|
[47a3cd8] | 37 | #ifndef CONFIGURE_MALLOC_BSP_SUPPORTS_SBRK |
---|
| 38 | void *rtems_heap_null_extend( |
---|
| 39 | Heap_Control *heap, |
---|
| 40 | size_t alloc_size |
---|
| 41 | ) |
---|
| 42 | { |
---|
| 43 | return rtems_heap_extend_via_sbrk( heap, alloc_size ); |
---|
| 44 | } |
---|
| 45 | #endif |
---|
| 46 | |
---|
[ede5a2a] | 47 | char Malloc_Heap[ 1024 ] CPU_STRUCTURE_ALIGNMENT; |
---|
[9deed9ed] | 48 | |
---|
| 49 | /* |
---|
| 50 | * Use volatile to prevent compiler optimizations due to the malloc() builtin. |
---|
| 51 | */ |
---|
| 52 | volatile int sbrk_count; |
---|
| 53 | |
---|
[07b93de] | 54 | Heap_Control TempHeap; |
---|
[843ad7b] | 55 | |
---|
| 56 | size_t offset; |
---|
[830c6ee] | 57 | |
---|
[843ad7b] | 58 | void * sbrk(ptrdiff_t incr) |
---|
| 59 | { |
---|
| 60 | void *p = (void *) -1; |
---|
| 61 | |
---|
| 62 | printf( "sbrk(%td)\n", incr ); |
---|
[830c6ee] | 63 | if ( sbrk_count == -1 ) { |
---|
[85387db5] | 64 | p = (void *) -2; |
---|
| 65 | sbrk_count = 0; |
---|
| 66 | } else if ( offset + incr <= sizeof(Malloc_Heap) ) { |
---|
| 67 | p = &Malloc_Heap[ offset ]; |
---|
| 68 | offset += incr; |
---|
[843ad7b] | 69 | } |
---|
| 70 | |
---|
[85387db5] | 71 | ++sbrk_count; |
---|
| 72 | |
---|
[843ad7b] | 73 | return p; |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | rtems_task Init( |
---|
| 77 | rtems_task_argument argument |
---|
| 78 | ) |
---|
| 79 | { |
---|
[47a3cd8] | 80 | Heap_Control *real_heap; |
---|
| 81 | Heap_Area area; |
---|
[85387db5] | 82 | void *p; |
---|
[843ad7b] | 83 | |
---|
[f8b2eb03] | 84 | TEST_BEGIN(); |
---|
[843ad7b] | 85 | |
---|
| 86 | /* Safe information on real heap */ |
---|
[47a3cd8] | 87 | real_heap = malloc_get_heap_pointer(); |
---|
| 88 | malloc_set_heap_pointer( &TempHeap ); |
---|
| 89 | |
---|
[85387db5] | 90 | rtems_heap_set_sbrk_amount( 0 ); |
---|
| 91 | |
---|
| 92 | puts( "No sbrk() amount" ); |
---|
[843ad7b] | 93 | |
---|
| 94 | sbrk_count = 0; |
---|
[ede5a2a] | 95 | offset = 256; |
---|
[47a3cd8] | 96 | area.begin = &Malloc_Heap [0]; |
---|
[85387db5] | 97 | area.size = offset; |
---|
[47a3cd8] | 98 | RTEMS_Malloc_Initialize( &area, 1, NULL ); |
---|
[85387db5] | 99 | |
---|
| 100 | errno = 0; |
---|
[ede5a2a] | 101 | p = malloc( 256 ); |
---|
[85387db5] | 102 | rtems_test_assert( p == NULL ); |
---|
| 103 | rtems_test_assert( errno == ENOMEM ); |
---|
| 104 | rtems_test_assert( sbrk_count == 0 ); |
---|
| 105 | |
---|
[ede5a2a] | 106 | rtems_heap_set_sbrk_amount( 256 ); |
---|
[85387db5] | 107 | |
---|
| 108 | puts( "Misaligned extend" ); |
---|
| 109 | |
---|
| 110 | sbrk_count = 0; |
---|
[ede5a2a] | 111 | offset = 256; |
---|
[85387db5] | 112 | area.begin = &Malloc_Heap [0]; |
---|
| 113 | area.size = offset; |
---|
[47a3cd8] | 114 | RTEMS_Malloc_Initialize( &area, 1, NULL ); |
---|
[85387db5] | 115 | |
---|
| 116 | p = malloc(1); |
---|
| 117 | rtems_test_assert( p != NULL ); |
---|
| 118 | rtems_test_assert( sbrk_count == 0 ); |
---|
| 119 | |
---|
[ede5a2a] | 120 | p = malloc(257); |
---|
[85387db5] | 121 | rtems_test_assert( p != NULL ); |
---|
| 122 | rtems_test_assert( sbrk_count == 1 ); |
---|
| 123 | |
---|
| 124 | puts( "Not enough sbrk() space" ); |
---|
| 125 | |
---|
| 126 | sbrk_count = 0; |
---|
[ede5a2a] | 127 | offset = 256; |
---|
[85387db5] | 128 | area.begin = &Malloc_Heap [0]; |
---|
| 129 | area.size = offset; |
---|
| 130 | RTEMS_Malloc_Initialize( &area, 1, NULL ); |
---|
| 131 | |
---|
| 132 | errno = 0; |
---|
| 133 | p = malloc( sizeof( Malloc_Heap ) ); |
---|
| 134 | rtems_test_assert( p == NULL ); |
---|
[830c6ee] | 135 | rtems_test_assert( errno == ENOMEM ); |
---|
[85387db5] | 136 | rtems_test_assert( sbrk_count == 1 ); |
---|
| 137 | |
---|
| 138 | puts( "Valid heap extend" ); |
---|
[830c6ee] | 139 | |
---|
[85387db5] | 140 | sbrk_count = 0; |
---|
[ede5a2a] | 141 | offset = 256; |
---|
[47a3cd8] | 142 | area.begin = &Malloc_Heap [0]; |
---|
[85387db5] | 143 | area.size = offset; |
---|
[47a3cd8] | 144 | RTEMS_Malloc_Initialize( &area, 1, NULL ); |
---|
[85387db5] | 145 | |
---|
[ede5a2a] | 146 | p = malloc( 128 ); |
---|
[85387db5] | 147 | rtems_test_assert( p != NULL ); |
---|
| 148 | rtems_test_assert( sbrk_count == 0 ); |
---|
| 149 | |
---|
[ede5a2a] | 150 | p = malloc( 128 ); |
---|
[85387db5] | 151 | rtems_test_assert( p != NULL ); |
---|
| 152 | rtems_test_assert( sbrk_count == 1 ); |
---|
| 153 | |
---|
| 154 | puts( "Invalid heap extend" ); |
---|
| 155 | |
---|
| 156 | sbrk_count = -1; |
---|
[ede5a2a] | 157 | offset = 256; |
---|
[85387db5] | 158 | area.begin = &Malloc_Heap [0]; |
---|
| 159 | area.size = offset; |
---|
[47a3cd8] | 160 | RTEMS_Malloc_Initialize( &area, 1, NULL ); |
---|
[85387db5] | 161 | |
---|
| 162 | errno = 0; |
---|
[ede5a2a] | 163 | p = malloc( 256 ); |
---|
[85387db5] | 164 | rtems_test_assert( p == NULL ); |
---|
| 165 | rtems_test_assert( errno == ENOMEM ); |
---|
| 166 | rtems_test_assert( sbrk_count == 2 ); |
---|
[843ad7b] | 167 | |
---|
| 168 | /* Restore information on real heap */ |
---|
[47a3cd8] | 169 | malloc_set_heap_pointer( real_heap ); |
---|
[843ad7b] | 170 | |
---|
[f8b2eb03] | 171 | TEST_END(); |
---|
[843ad7b] | 172 | |
---|
| 173 | rtems_test_exit(0); |
---|
| 174 | } |
---|
| 175 | |
---|
| 176 | /* end of file */ |
---|