Back to home page

LXR

 
 

    


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

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /*
0004  *  COPYRIGHT (c) 1989-2011.
0005  *  On-Line Applications Research Corporation (OAR).
0006  *
0007  * Redistribution and use in source and binary forms, with or without
0008  * modification, are permitted provided that the following conditions
0009  * are met:
0010  * 1. Redistributions of source code must retain the above copyright
0011  *    notice, this list of conditions and the following disclaimer.
0012  * 2. Redistributions in binary form must reproduce the above copyright
0013  *    notice, this list of conditions and the following disclaimer in the
0014  *    documentation and/or other materials provided with the distribution.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0017  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0018  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0019  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0020  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0021  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0022  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0023  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0024  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0025  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0026  * POSSIBILITY OF SUCH DAMAGE.
0027  */
0028 
0029 #ifdef HAVE_CONFIG_H
0030 #include "config.h"
0031 #endif
0032 
0033 #include <tmacros.h>
0034 #include "test_support.h"
0035 #include <rtems/libcsupport.h>
0036 #include <rtems/malloc.h>
0037 #include <errno.h>
0038 
0039 const char rtems_test_name[] = "MALLOC 4";
0040 
0041 /* configuration information */
0042 /* At top of file to have access to configuration variables */
0043 
0044 #define CONFIGURE_APPLICATION_NEEDS_SIMPLE_CONSOLE_DRIVER
0045 #define CONFIGURE_APPLICATION_NEEDS_CLOCK_DRIVER
0046 
0047 #define CONFIGURE_MAXIMUM_TASKS             1
0048 #define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
0049 
0050 #define CONFIGURE_RTEMS_INIT_TASKS_TABLE
0051 
0052 #define CONFIGURE_INIT
0053 #include <rtems/confdefs.h>
0054 /* end of configuration */
0055 
0056 #ifndef CONFIGURE_MALLOC_BSP_SUPPORTS_SBRK
0057 void *rtems_heap_null_extend(
0058   Heap_Control *heap,
0059   size_t alloc_size
0060 )
0061 {
0062   return rtems_heap_extend_via_sbrk( heap, alloc_size );
0063 }
0064 #endif
0065 
0066 char Malloc_Heap[ 1024 ] CPU_STRUCTURE_ALIGNMENT;
0067 
0068 /*
0069  * Use volatile to prevent compiler optimizations due to the malloc() builtin.
0070  */
0071 volatile int sbrk_count;
0072 
0073 Heap_Control TempHeap;
0074 
0075 size_t offset;
0076 
0077 void * sbrk(ptrdiff_t incr)
0078 {
0079   void *p = (void *) -1;
0080 
0081   printf( "sbrk(%td)\n", incr );
0082   if ( sbrk_count == -1 ) {
0083     p = (void *) -2;
0084     sbrk_count = 0;
0085   } else if ( offset + incr <= sizeof(Malloc_Heap) ) {
0086     p = &Malloc_Heap[ offset ];
0087     offset += incr;
0088   }
0089 
0090   ++sbrk_count;
0091 
0092   return p;
0093 }
0094 
0095 rtems_task Init(
0096   rtems_task_argument argument
0097 )
0098 {
0099   Heap_Control *real_heap;
0100   const Memory_Information *mem;
0101   Memory_Area *area;
0102   size_t i;
0103 
0104   void *p;
0105 
0106   TEST_BEGIN();
0107 
0108   mem = _Memory_Get();
0109 
0110   for ( i = 0; i < _Memory_Get_count( mem ); ++i ) {
0111     area = _Memory_Get_area( mem, i );
0112     _Memory_Initialize( area, NULL, NULL );
0113   }
0114 
0115   area = _Memory_Get_area( mem, 0 );
0116 
0117   /* Safe information on real heap */
0118   real_heap = malloc_get_heap_pointer();
0119   malloc_set_heap_pointer( &TempHeap );
0120 
0121   rtems_heap_set_sbrk_amount( 0 );
0122 
0123   puts( "No sbrk() amount" );
0124 
0125   sbrk_count = 0;
0126   offset     = 256;
0127   _Memory_Initialize_by_size( area, &Malloc_Heap[ 0 ], offset );
0128   _Malloc_Initialize();
0129 
0130   errno = 0;
0131   p = malloc( 256 );
0132   rtems_test_assert( p == NULL );
0133   rtems_test_assert( errno == ENOMEM );
0134   rtems_test_assert( sbrk_count == 0 );
0135 
0136   rtems_heap_set_sbrk_amount( 256 );
0137 
0138   puts( "Misaligned extend" );
0139 
0140   sbrk_count = 0;
0141   offset     = 256;
0142   _Memory_Initialize_by_size( area, &Malloc_Heap[ 0 ], offset );
0143   _Malloc_Initialize();
0144 
0145   p = malloc(1);
0146   rtems_test_assert( p != NULL );
0147   rtems_test_assert( sbrk_count == 0 );
0148 
0149   p = malloc(257);
0150   rtems_test_assert( p != NULL );
0151   rtems_test_assert( sbrk_count == 1 );
0152 
0153   puts( "Not enough sbrk() space" );
0154 
0155   sbrk_count = 0;
0156   offset     = 256;
0157   _Memory_Initialize_by_size( area, &Malloc_Heap[ 0 ], offset );
0158   _Malloc_Initialize();
0159 
0160   errno = 0;
0161   p = malloc( sizeof( Malloc_Heap ) );
0162   rtems_test_assert( p == NULL );
0163   rtems_test_assert( errno == ENOMEM );
0164   rtems_test_assert( sbrk_count == 1 );
0165 
0166   puts( "Valid heap extend" );
0167 
0168   sbrk_count = 0;
0169   offset     = 256;
0170   _Memory_Initialize_by_size( area, &Malloc_Heap[ 0 ], offset );
0171   _Malloc_Initialize();
0172 
0173   p = malloc( 128 );
0174   rtems_test_assert( p != NULL );
0175   rtems_test_assert( sbrk_count == 0 );
0176 
0177   p = malloc( 128 );
0178   rtems_test_assert( p != NULL );
0179   rtems_test_assert( sbrk_count == 1 );
0180 
0181   puts( "Invalid heap extend" );
0182 
0183   sbrk_count = -1;
0184   offset     = 256;
0185   _Memory_Initialize_by_size( area, &Malloc_Heap[ 0 ], offset );
0186   _Malloc_Initialize();
0187 
0188   errno = 0;
0189   p = malloc( 256 );
0190   rtems_test_assert( p == NULL );
0191   rtems_test_assert( errno == ENOMEM );
0192   rtems_test_assert( sbrk_count == 2 );
0193 
0194   /* Restore information on real heap */
0195   malloc_set_heap_pointer( real_heap );
0196 
0197   TEST_END();
0198 
0199   rtems_test_exit(0);
0200 }
0201 
0202 /* end of file */