Changeset 9d1f3943 in rtems


Ignore:
Timestamp:
Feb 25, 2016, 8:02:43 AM (4 years ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
master
Children:
67de508
Parents:
839d0a74
git-author:
Sebastian Huber <sebastian.huber@…> (02/25/16 08:02:43)
git-committer:
Sebastian Huber <sebastian.huber@…> (02/25/16 10:35:54)
Message:

malloc: Add _Malloc_System_state()

Replace malloc_is_system_state_OK() with _Malloc_System_state() to allow
early allocations, e.g. in bsp_start(). Here the _Thread_Executing is
NULL, thus an _API_Mutex_Lock() would lead to a NULL pointer access.

Move malloc() support code to general case
rtems_heap_allocate_aligned_with_boundary(). Use
rtems_heap_allocate_aligned_with_boundary() to avoid duplicated code.

Files:
1 deleted
7 edited

Legend:

Unmodified
Added
Removed
  • cpukit/libcsupport/Makefile.am

    r839d0a74 r9d1f3943  
    8888    src/posix_memalign.c \
    8989    src/rtems_memalign.c src/malloc_deferred.c \
    90     src/malloc_dirtier.c src/malloc_p.h src/rtems_malloc.c \
     90    src/malloc_dirtier.c src/malloc_p.h \
    9191    src/rtems_heap_extend_via_sbrk.c \
    9292    src/rtems_heap_null_extend.c \
  • cpukit/libcsupport/src/free.c

    r839d0a74 r9d1f3943  
    2323#include <stdlib.h>
    2424
    25 #include <rtems/score/sysstate.h>
    26 
    27 #include "malloc_p.h"
    28 
    2925void free(
    3026  void *ptr
     
    3733   *  Do not attempt to free memory if in a critical section or ISR.
    3834   */
    39   if ( !malloc_is_system_state_OK() ) {
    40       malloc_deferred_free(ptr);
     35  if ( _Malloc_System_state() != MALLOC_SYSTEM_STATE_NORMAL ) {
     36      _Malloc_Deferred_free(ptr);
    4137      return;
    4238  }
  • cpukit/libcsupport/src/malloc.c

    r839d0a74 r9d1f3943  
    3232
    3333  /*
    34    *  If some free's have been deferred, then do them now.
    35    */
    36   malloc_deferred_frees_process();
    37 
    38   /*
    3934   * Validate the parameters
    4035   */
     
    4237    return (void *) 0;
    4338
    44   /*
    45    *  Do not attempt to allocate memory if not in correct system state.
    46    */
    47   if ( !malloc_is_system_state_OK() )
    48     return NULL;
    49 
    50   /*
    51    * Try to give a segment in the current heap if there is not
    52    * enough space then try to grow the heap.
    53    * If this fails then return a NULL pointer.
    54    */
    55 
    56   return_this = _Protected_heap_Allocate( RTEMS_Malloc_Heap, size );
    57 
     39  return_this = rtems_heap_allocate_aligned_with_boundary( size, 0, 0 );
    5840  if ( !return_this ) {
    59     return_this = (*rtems_malloc_extend_handler)( RTEMS_Malloc_Heap, size );
    60     if ( !return_this ) {
    61       errno = ENOMEM;
    62       return (void *) 0;
    63     }
     41    errno = ENOMEM;
     42    return (void *) 0;
    6443  }
    65 
    66   /*
    67    *  If the user wants us to dirty the allocated memory, then do it.
    68    */
    69   if ( rtems_malloc_dirty_helper )
    70     (*rtems_malloc_dirty_helper)( return_this, size );
    7144
    7245  return return_this;
  • cpukit/libcsupport/src/malloc_deferred.c

    r839d0a74 r9d1f3943  
    3535RTEMS_INTERRUPT_LOCK_DEFINE( static, _Malloc_GC_lock, "Malloc GC" )
    3636
    37 bool malloc_is_system_state_OK(void)
     37Malloc_System_state _Malloc_System_state( void )
    3838{
    39   return !_System_state_Is_up( _System_state_Get() )
    40     || _Thread_Dispatch_is_enabled();
     39  System_state_Codes state = _System_state_Get();
     40
     41  if ( _System_state_Is_up( state ) ) {
     42    if ( _Thread_Dispatch_is_enabled() ) {
     43      return MALLOC_SYSTEM_STATE_NORMAL;
     44    } else {
     45      return MALLOC_SYSTEM_STATE_NO_ALLOCATION;
     46    }
     47  } else if ( _System_state_Is_before_multitasking( state ) ) {
     48    return MALLOC_SYSTEM_STATE_NORMAL;
     49  } else {
     50    return MALLOC_SYSTEM_STATE_NO_PROTECTION;
     51  }
    4152}
    4253
     
    5364}
    5465
    55 void malloc_deferred_frees_process( void )
     66static void _Malloc_Process_deferred_frees( void )
    5667{
    5768  rtems_chain_node *to_be_freed;
     
    6576}
    6677
    67 void malloc_deferred_free( void *p )
     78void *rtems_heap_allocate_aligned_with_boundary(
     79  uintptr_t size,
     80  uintptr_t alignment,
     81  uintptr_t boundary
     82)
     83{
     84  Heap_Control *heap = RTEMS_Malloc_Heap;
     85  void *p;
     86
     87  switch ( _Malloc_System_state() ) {
     88    case MALLOC_SYSTEM_STATE_NORMAL:
     89      _Malloc_Process_deferred_frees();
     90      p = _Protected_heap_Allocate_aligned_with_boundary(
     91        heap,
     92        size,
     93        alignment,
     94        boundary
     95      );
     96      break;
     97    case MALLOC_SYSTEM_STATE_NO_PROTECTION:
     98      p = _Heap_Allocate_aligned_with_boundary(
     99        heap,
     100        size,
     101        alignment,
     102        boundary
     103      );
     104      break;
     105    default:
     106      /*
     107       *  Do not attempt to allocate memory if not in correct system state.
     108       */
     109      return NULL;
     110  }
     111
     112  if ( p == NULL && alignment == 0 && boundary == 0 ) {
     113    p = (*rtems_malloc_extend_handler)( heap, size );
     114  }
     115
     116  /*
     117   *  If the user wants us to dirty the allocated memory, then do it.
     118   */
     119  if ( p != NULL && rtems_malloc_dirty_helper != NULL )
     120    (*rtems_malloc_dirty_helper)( p, size );
     121
     122  return p;
     123}
     124
     125void _Malloc_Deferred_free( void *p )
    68126{
    69127  rtems_interrupt_lock_context lock_context;
  • cpukit/libcsupport/src/malloc_p.h

    r839d0a74 r9d1f3943  
    1818#endif /* __cplusplus */
    1919
    20 /*
    21  *  Process deferred free operations
    22  */
    23 bool malloc_is_system_state_OK(void);
    24 void malloc_deferred_frees_process(void);
    25 void malloc_deferred_free(void *);
     20typedef enum {
     21  MALLOC_SYSTEM_STATE_NORMAL,
     22  MALLOC_SYSTEM_STATE_NO_PROTECTION,
     23  MALLOC_SYSTEM_STATE_NO_ALLOCATION
     24} Malloc_System_state;
     25
     26Malloc_System_state _Malloc_System_state( void );
     27
     28void _Malloc_Deferred_free( void * );
    2629
    2730#ifdef __cplusplus
  • cpukit/libcsupport/src/rtems_memalign.c

    r839d0a74 r9d1f3943  
    4242
    4343  /*
    44    *  Do not attempt to allocate memory if not in correct system state.
    45    */
    46   if ( !malloc_is_system_state_OK() )
    47     return EINVAL;
    48 
    49   /*
    50    *  If some free's have been deferred, then do them now.
    51    */
    52   malloc_deferred_frees_process();
    53 
    54   /*
    5544   *  Perform the aligned allocation requested
    5645   */
    57   return_this = _Protected_heap_Allocate_aligned(
    58     RTEMS_Malloc_Heap,
    59     size,
    60     alignment
    61   );
     46  return_this = rtems_heap_allocate_aligned_with_boundary( size, alignment, 0 );
    6247  if ( !return_this )
    6348    return ENOMEM;
  • testsuites/libtests/malloctest/init.c

    r839d0a74 r9d1f3943  
    33 *  On-Line Applications Research Corporation (OAR).
    44 *
    5  *  Copyright (c) 2009, 2010 embedded brains GmbH.
     5 *  Copyright (c) 2009, 2016 embedded brains GmbH.
    66 *
    77 *  The license and distribution terms for this file may be
     
    2323#include <rtems/score/protectedheap.h>
    2424#include <rtems/malloc.h>
     25#include <rtems/sysinit.h>
    2526
    2627const char rtems_test_name[] = "MALLOCTEST";
     
    13701371  directive_failed( status, "rtems_task_delete of RTEMS_SELF" );
    13711372}
     1373
     1374static void test_early_malloc( void )
     1375{
     1376  void *p;
     1377  char *q;
     1378
     1379  p = malloc( 1 );
     1380  rtems_test_assert( p != NULL );
     1381
     1382  free( p );
     1383
     1384  q = calloc( 1, 1 );
     1385  rtems_test_assert( q != NULL );
     1386  rtems_test_assert( p != q );
     1387  rtems_test_assert( q[0] == 0 );
     1388
     1389  free( q );
     1390}
     1391
     1392RTEMS_SYSINIT_ITEM(
     1393  test_early_malloc,
     1394  RTEMS_SYSINIT_INITIAL_EXTENSIONS,
     1395  RTEMS_SYSINIT_ORDER_FIRST
     1396);
Note: See TracChangeset for help on using the changeset viewer.