Changeset fdb45d6 in rtems


Ignore:
Timestamp:
Jun 24, 2015, 12:00:08 PM (4 years ago)
Author:
Sebastian Huber <sebastian.huber@…>
Branches:
4.11, master
Children:
f59f2fe9
Parents:
cb2b8f0
git-author:
Sebastian Huber <sebastian.huber@…> (06/24/15 12:00:08)
git-committer:
Sebastian Huber <sebastian.huber@…> (07/01/15 06:24:31)
Message:

score: Freechain handler API changes

Replace the extend function with an allocator since this fits better
to the current use case.

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • cpukit/posix/include/rtems/posix/keyimpl.h

    rcb2b8f0 rfdb45d6  
    157157}
    158158
    159 RTEMS_INLINE_ROUTINE POSIX_Keys_Key_value_pair *
    160 _POSIX_Keys_Key_value_pair_allocate( void )
    161 {
    162   return (POSIX_Keys_Key_value_pair *) _Freechain_Get( &_POSIX_Keys_Keypool );
    163 }
     159POSIX_Keys_Key_value_pair * _POSIX_Keys_Key_value_pair_allocate( void );
    164160
    165161RTEMS_INLINE_ROUTINE void _POSIX_Keys_Key_value_pair_free(
  • cpukit/posix/src/key.c

    rcb2b8f0 rfdb45d6  
    9393}
    9494
    95 static bool _POSIX_Keys_Keypool_extend( Freechain_Control *keypool )
     95static void _POSIX_Keys_Initialize_keypool( void )
    9696{
    97   size_t bump_count = _POSIX_Keys_Get_keypool_bump_count();
    98   bool ok = bump_count > 0;
    99 
    100   if ( ok ) {
    101     size_t size = bump_count * sizeof( POSIX_Keys_Key_value_pair );
    102     POSIX_Keys_Key_value_pair *nodes = _Workspace_Allocate( size );
    103 
    104     ok = nodes != NULL;
    105 
    106     if ( ok ) {
    107       _Chain_Initialize(
    108         &keypool->Freechain,
    109         nodes,
    110         bump_count,
    111         sizeof( *nodes )
    112       );
    113     }
    114   }
    115 
    116   return ok;
     97  _Freechain_Initialize(
     98    &_POSIX_Keys_Keypool,
     99    _Workspace_Allocate_or_fatal_error,
     100    _POSIX_Keys_Get_initial_keypool_size(),
     101    sizeof( POSIX_Keys_Key_value_pair )
     102  );
    117103}
    118104
    119 static void _POSIX_Keys_Initialize_keypool( void )
     105POSIX_Keys_Key_value_pair * _POSIX_Keys_Key_value_pair_allocate( void )
    120106{
    121   Freechain_Control *keypool = &_POSIX_Keys_Keypool;
    122   size_t initial_count = _POSIX_Keys_Get_initial_keypool_size();
    123 
    124   _Freechain_Initialize( keypool, _POSIX_Keys_Keypool_extend );
    125 
    126   if ( initial_count > 0 ) {
    127     size_t size = initial_count * sizeof( POSIX_Keys_Key_value_pair );
    128     POSIX_Keys_Key_value_pair *nodes =
    129       _Workspace_Allocate_or_fatal_error( size );
    130 
    131     _Chain_Initialize(
    132       &keypool->Freechain,
    133       nodes,
    134       initial_count,
    135       sizeof( *nodes )
    136     );
    137   }
     107  return (POSIX_Keys_Key_value_pair *) _Freechain_Get(
     108    &_POSIX_Keys_Keypool,
     109    _Workspace_Allocate,
     110    _POSIX_Keys_Get_keypool_bump_count(),
     111    sizeof( POSIX_Keys_Key_value_pair )
     112  );
    138113}
    139114
  • cpukit/score/include/rtems/score/freechain.h

    rcb2b8f0 rfdb45d6  
    1717#define _RTEMS_SCORE_FREECHAIN_H
    1818
    19 #include <stdbool.h>
    20 
     19#include <rtems/score/basedefs.h>
    2120#include <rtems/score/chain.h>
    2221
     
    3736 */
    3837
    39 typedef struct Freechain_Control Freechain_Control;
     38/**
     39 * @brief Allocator function.
     40 */
     41typedef void *( *Freechain_Allocator )( size_t size );
    4042
    4143/**
    42  * @brief Extends the freechain.
    43  *
    44  * @param[in] freechain The freechain control.
    45  *
    46  * @retval true The freechain contains now at least one node.
    47  * @retval false Otherwise.
     44 * @brief The freechain control.
    4845 */
    49 typedef bool ( *Freechain_Extend )( Freechain_Control *freechain );
    50 
    51 /**
    52  * @typedef Freechain_Control
    53  *
    54  * This is used to manage freechain's nodes.
    55  */
    56 struct Freechain_Control {
    57   Chain_Control     Freechain;
    58   Freechain_Extend  extend;
    59 };
     46typedef struct {
     47  /**
     48   * @brief Chain of free nodes.
     49   */
     50  Chain_Control Free;
     51} Freechain_Control;
    6052
    6153/**
     
    6658 * get more nodes.
    6759 *
    68  * @param[in,out] freechain The freechain control to initialize.
    69  * @param[in] extend The extend handler.  It is called by _Freechain_Get() in
    70  * case the freechain is empty.
     60 * @param[in] freechain The freechain control to initialize.
     61 * @param[in] allocator The allocator function.
     62 * @param[in] number_nodes The initial number of nodes.
     63 * @param[in] node_size The node size.
    7164 */
    7265void _Freechain_Initialize(
    73   Freechain_Control *freechain,
    74   Freechain_Extend   extend
     66  Freechain_Control   *freechain,
     67  Freechain_Allocator  allocator,
     68  size_t               number_nodes,
     69  size_t               node_size
    7570);
    7671
     
    7873 * @brief Gets a node from the freechain.
    7974 *
    80  * @param[in,out] freechain The freechain control.
     75 * @param[in] freechain The freechain control.
     76 * @param[in] allocator The allocator function.
     77 * @param[in] number_nodes_to_extend The number of nodes in case an extend is
     78 *   necessary due to an empty freechain.
     79 * @param[in] node_size The node size.
    8180 *
    8281 * @retval NULL The freechain is empty and the extend operation failed.
     
    8584 */
    8685void *_Freechain_Get(
    87   Freechain_Control *freechain
     86  Freechain_Control   *freechain,
     87  Freechain_Allocator  allocator,
     88  size_t               number_nodes_to_extend,
     89  size_t               node_size
    8890);
    8991
     
    9193 * @brief Puts a node back onto the freechain.
    9294 *
    93  * @param[in,out] freechain The freechain control.
     95 * @param[in] freechain The freechain control.
    9496 * @param[in] node The node to put back.
    9597 */
  • cpukit/score/src/freechain.c

    rcb2b8f0 rfdb45d6  
    2020
    2121#include <rtems/score/freechain.h>
     22#include <rtems/score/assert.h>
    2223#include <rtems/score/chainimpl.h>
    2324
    2425void _Freechain_Initialize(
    25     Freechain_Control *freechain,
    26     Freechain_Extend   extend
     26  Freechain_Control   *freechain,
     27  Freechain_Allocator  allocator,
     28  size_t               number_nodes,
     29  size_t               node_size
    2730)
    2831{
    29   _Chain_Initialize_empty( &freechain->Freechain );
    30   freechain->extend = extend;
     32  void *starting_address;
     33
     34  if ( number_nodes > 0 ) {
     35    starting_address = ( *allocator )( number_nodes * node_size );
     36    number_nodes *= ( starting_address != NULL );
     37  } else {
     38    starting_address = NULL;
     39  }
     40
     41  _Chain_Initialize(
     42    &freechain->Free,
     43    starting_address,
     44    number_nodes,
     45    node_size
     46  );
    3147}
    3248
    33 void *_Freechain_Get(Freechain_Control *freechain)
     49void *_Freechain_Get(
     50  Freechain_Control   *freechain,
     51  Freechain_Allocator  allocator,
     52  size_t               number_nodes_to_extend,
     53  size_t               node_size
     54)
    3455{
    35   if ( _Chain_Is_empty( &freechain->Freechain ) ) {
    36     if ( !( *freechain->extend )( freechain ) ) {
    37       return NULL;
    38     }
     56  _Assert( node_size >= sizeof( Chain_Node ) );
     57
     58  if ( _Chain_Is_empty( &freechain->Free ) && number_nodes_to_extend > 0 ) {
     59    void *starting_address;
     60
     61    starting_address = ( *allocator )( number_nodes_to_extend * node_size );
     62    number_nodes_to_extend *= ( starting_address != NULL );
     63
     64    _Chain_Initialize(
     65      &freechain->Free,
     66      starting_address,
     67      number_nodes_to_extend,
     68      node_size
     69    );
    3970  }
    4071
    41   return _Chain_Get_first_unprotected( &freechain->Freechain );
     72  return _Chain_Get_unprotected( &freechain->Free );
    4273}
    4374
    4475void _Freechain_Put( Freechain_Control *freechain, void *node )
    4576{
    46   _Chain_Prepend_unprotected( &freechain->Freechain, node );
     77  _Chain_Prepend_unprotected( &freechain->Free, node );
    4778}
  • testsuites/sptests/spfreechain01/init.c

    rcb2b8f0 rfdb45d6  
    1414#include <rtems/score/chainimpl.h>
    1515#include <rtems/score/freechain.h>
    16 #include <rtems/score/wkspace.h>
    17 #include <rtems/malloc.h>
    1816
    1917const char rtems_test_name[] = "SPFREECHAIN 1";
    2018
    21 /* forward declarations to avoid warnings */
    22 rtems_task Init(rtems_task_argument argument);
    23 bool my_freechain_extend_with_nothing( Freechain_Control *freechain );
    24 bool my_freechain_extend_heap( Freechain_Control *freechain );
    25 bool my_freechain_extend_workspace( Freechain_Control *freechain );
    26 void my_freechain_init_heap( Freechain_Control *freechain );
    27 void my_freechain_init_workspace( Freechain_Control *freechain );
    28 
    2919typedef struct {
    30   Freechain_Control super_fc;
    31   size_t bump_count;
    32 } MyFreechain;
    33 
    34 typedef struct {
    35   Chain_Node ch_node;
     20  Chain_Node Node;
    3621  int x;
    3722} test_node;
    3823
    39 bool my_freechain_extend_with_nothing( Freechain_Control *freechain )
     24static rtems_task Init(rtems_task_argument ignored)
    4025{
    41     return NULL;
    42 }
     26    Freechain_Control fc;
     27    test_node *node;
    4328
    44 /* user defined extend handle, it allocates memory on heap. */
    45 bool my_freechain_extend_heap( Freechain_Control *freechain )
    46 {
    47   MyFreechain *self = (MyFreechain *)freechain;
    48   size_t node_size = sizeof(test_node);
    49   size_t size = self->bump_count * node_size;
    50   int i;
    51   test_node *nodes = malloc(size);
    52 
    53   if (!nodes) {
    54     printf( "INIT - Unable to allocate free chain of size: %zd\n", size );
    55     return NULL;
    56   }
    57 
    58   puts( "INIT - Allocate node from heap in user defined freechain extend"
    59         " - OK" );
    60   for ( i = 0; i < self->bump_count; i++ ) {
    61       _Freechain_Put(freechain,
    62                           nodes + i);
    63   }
    64   return true;
    65 }
    66 
    67 /* user defined extend handle, it allocates memory on workspace. */
    68 bool my_freechain_extend_workspace( Freechain_Control *freechain )
    69 {
    70   MyFreechain *self = (MyFreechain *)freechain;
    71   size_t node_size = sizeof(test_node);
    72   size_t size = self->bump_count * node_size;
    73   int i;
    74   test_node *nodes = _Workspace_Allocate(size);
    75 
    76   if (!nodes) {
    77     printf( "INIT - Unable to allocate free chain of size: %zd\n", size );
    78     return NULL;
    79   }
    80 
    81   puts( "INIT - Allocate node from workspace in user defined freechain extend"
    82         " - OK" );
    83 
    84   for ( i = 0; i < self->bump_count; i++ ) {
    85       _Freechain_Put(freechain,
    86                           nodes + i);
    87   }
    88   return true;
    89 }
    90 
    91 void my_freechain_init_heap( Freechain_Control *freechain )
    92 {
    93   MyFreechain *self = (MyFreechain *)freechain;
    94   self->bump_count = 5;
    95   size_t size = self->bump_count * sizeof(test_node);
    96   test_node *nodes = malloc(size);
    97 
    98   _Chain_Initialize(
    99     &freechain->Freechain,
    100     nodes,
    101     self->bump_count,
    102     sizeof(test_node)
    103     );
    104 }
    105 
    106 void my_freechain_init_workspace( Freechain_Control *freechain )
    107 {
    108   MyFreechain *self = (MyFreechain *)freechain;
    109   self->bump_count = 7;
    110   size_t size = self->bump_count * sizeof(test_node);
    111   test_node *nodes = _Workspace_Allocate(size);
    112 
    113   _Chain_Initialize(
    114     &freechain->Freechain,
    115     nodes,
    116     self->bump_count,
    117     sizeof(test_node)
    118     );
    119 }
    120 
    121 rtems_task Init(
    122   rtems_task_argument ignored
    123                 )
    124 {
    12529    TEST_BEGIN();
    12630
    127     test_node *test_node_p;
    128     MyFreechain myfc;
    129     Freechain_Control *fc_p = (Freechain_Control *)&myfc;
    130     int i;
     31    _Freechain_Initialize(&fc, NULL, 0, sizeof(test_node));
     32    rtems_test_assert(_Chain_Is_empty(&fc.Free));
     33
     34    _Freechain_Initialize(&fc, malloc, 1, SIZE_MAX);
     35    rtems_test_assert(_Chain_Is_empty(&fc.Free));
     36
     37    rtems_test_assert(_Freechain_Get(&fc, NULL, 0, sizeof(test_node)) == NULL);
     38
     39    rtems_test_assert(_Freechain_Get(&fc, malloc, 1, SIZE_MAX) == NULL);
    13140
    13241    /* check whether freechain put and get works correctly*/
    133     _Freechain_Initialize(fc_p,
    134                           &my_freechain_extend_with_nothing);
    135     my_freechain_init_heap(fc_p);
    13642
    13743    puts( "INIT - Get node from freechain - OK" );
    138     test_node_p = (test_node *)_Freechain_Get(fc_p);
    139     test_node_p->x = 1;
     44    node = _Freechain_Get(&fc, malloc, 1, sizeof(test_node));
     45    node->x = 1;
    14046
    14147    puts( "INIT - Put node back to freechain - OK" );
    142     _Freechain_Put(fc_p, (void *)test_node_p);
     48    _Freechain_Put(&fc, node);
    14349
    14450    puts( "INIT - Verify freechain node put and get - OK" );
    145     test_node_p = (test_node *)_Freechain_Get(fc_p);
    146     if(test_node_p->x != 1) {
    147       puts( "INIT - ERROR ON FREECHAIN GET AND PUT" );
    148       rtems_test_exit(0);
    149     }
    150 
    151     /* check whether freechain extend handle on heap works correctly */
    152     _Freechain_Initialize(fc_p,
    153                           &my_freechain_extend_heap);
    154     my_freechain_init_heap(fc_p);
    155 
    156     puts( "INIT - Get more than intialized nodes from freechain on heap - OK" );
    157 
    158     for ( i = 0; i < myfc.bump_count * 2; i++ ) {
    159         test_node_p = (test_node *)_Freechain_Get(fc_p);
    160         if (!test_node_p) {
    161             puts( "INIT - Get node from freechain failed - FAILED" );
    162             rtems_test_exit(0);
    163         }
    164     }
    165 
    166     /* check whether freechain extend handle in workspace works correctly */
    167     _Freechain_Initialize(fc_p,
    168                           &my_freechain_extend_workspace);
    169     my_freechain_init_workspace(fc_p);
    170 
    171     puts( "INIT - Get more than intialized nodes from freechain in workspace"
    172           " - OK" );
    173 
    174     for ( i = 0; i < myfc.bump_count * 2; i++ ) {
    175         test_node_p = (test_node *)_Freechain_Get(fc_p);
    176         if (!test_node_p) {
    177             puts( "INIT - Get node from freechain failed - FAILED" );
    178             rtems_test_exit(0);
    179         }
    180     }
     51    node = _Freechain_Get(&fc, NULL, 0, sizeof(test_node));
     52    rtems_test_assert(node->x == 1);
    18153
    18254    TEST_END();
     
    18961#define CONFIGURE_APPLICATION_DOES_NOT_NEED_CLOCK_DRIVER
    19062
    191 #define CONFIGURE_MEMORY_OVERHEAD sizeof(test_node)
    19263#define CONFIGURE_INITIAL_EXTENSIONS RTEMS_TEST_INITIAL_EXTENSION
    19364
  • testsuites/sptests/spfreechain01/spfreechain01.scn

    rcb2b8f0 rfdb45d6  
    1 *** START OF RTEMS FREECHAIN API TEST ***
     1*** BEGIN OF TEST SPFREECHAIN 1 ***
    22INIT - Get node from freechain - OK
    3 INIT - Put node to freechain - OK
     3INIT - Put node back to freechain - OK
    44INIT - Verify freechain node put and get - OK
    5 INIT - Get more than intialized nodes from freechain on heap - OK
    6 INIT - Allocate node from heap in user defined freechain extend - OK
    7 INIT - Get more than intialized nodes from freechain in workspace - OK
    8 INIT - Allocate node from workspace in user defined freechain extend - OK
    9 *** END OF RTEMS FREECHAIN API TEST ***
     5*** END OF TEST SPFREECHAIN 1 ***
Note: See TracChangeset for help on using the changeset viewer.