Changeset efbf6c9 in rtems


Ignore:
Timestamp:
Mar 9, 2016, 5:25:38 PM (4 years ago)
Author:
Joel Sherrill <joel@…>
Branches:
master
Children:
22bbb9f1
Parents:
6db5ea03
git-author:
Joel Sherrill <joel@…> (03/09/16 17:25:38)
git-committer:
Joel Sherrill <joel@…> (03/10/16 16:34:27)
Message:

libbsp/shared/console.c: Clean up memory allocation for per device data

File:
1 edited

Legend:

Unmodified
Added
Removed
  • c/src/lib/libbsp/shared/console.c

    r6db5ea03 refbf6c9  
    88
    99/*
    10  *  COPYRIGHT (c) 1989-2011.
     10 *  COPYRIGHT (c) 1989-2011, 2016.
    1111 *  On-Line Applications Research Corporation (OAR).
    1212 *
     
    4747    return;
    4848
     49  /*
     50   * Allocate memory for the table of device pointers.
     51   */
    4952  Console_Port_Count = Console_Configuration_Count;
    5053  Console_Port_Tbl   = malloc( Console_Port_Count * sizeof( console_tbl * ) );
     
    5255    bsp_fatal( BSP_FATAL_CONSOLE_NO_MEMORY_0 );
    5356
    54   for (i=0 ; i < Console_Port_Count ; i++)
     57  /*
     58   * Allocate memory for the table of device specific data pointers.
     59   */
     60  Console_Port_Data  = calloc( Console_Port_Count, sizeof( console_data ) );
     61  if ( Console_Port_Data == NULL ) {
     62    bsp_fatal( BSP_FATAL_CONSOLE_NO_MEMORY_3 );
     63  }
     64
     65  /*
     66   * Fill in the Console Table
     67   */
     68  for (i=0 ; i < Console_Port_Count ; i++) {
    5569    Console_Port_Tbl[i] = &Console_Configuration_Ports[i];
     70  }
    5671}
    5772
     
    7085  int  i;
    7186
     87  /*
     88   * Initialize the console data elements
     89   */
    7290  console_initialize_data();
    7391
    7492  /*
    75    *  console_initialize has been invoked so it is now too late to
     93   *  console_initialize() has been invoked so it is now too late to
    7694   *  register devices.
    7795   */
     
    87105  Console_Port_Tbl = realloc(
    88106    Console_Port_Tbl,
    89     Console_Port_Count * sizeof( console_tbl * )
     107    Console_Port_Count * sizeof(console_tbl *)
    90108  );
    91109  if ( Console_Port_Tbl == NULL ) {
     
    93111  }
    94112
    95   Console_Port_Data  = calloc( Console_Port_Count, sizeof( console_data ) );
     113  /*
     114   * Since we can only add devices before console_initialize(),
     115   * the data area will contain no information and must be zero
     116   * before it is used. So extend the area and zero it out.
     117   */
     118  Console_Port_Data = realloc(
     119    Console_Port_Data,
     120    Console_Port_Count * sizeof(console_tbl *)
     121  );
    96122  if ( Console_Port_Data == NULL ) {
    97123    bsp_fatal( BSP_FATAL_CONSOLE_NO_MEMORY_2 );
    98124  }
     125  memset(&Console_Port_Data, '\0', Console_Port_Count * sizeof(console_tbl *));
    99126
    100127  /*
    101128   *  Now add the new devices at the end.
    102129   */
    103 
    104130  for (i=0 ; i < number_of_ports ; i++) {
    105     Console_Port_Tbl[old_number_of_ports + i] = &new_ports[i];
     131    Console_Port_Tbl[old_number_of_ports + i]  = &new_ports[i];
    106132  }
    107133}
     
    250276  /*
    251277   * If we have no devices which were registered earlier then we
    252    * must still initialize pointers and set Console_Port_Data.
    253    */
    254   if ( ! Console_Port_Tbl ) {
     278   * must still initialize pointers for Console_Port_Tbl and
     279   * Console_Port_Data.
     280   */
     281  if ( !Console_Port_Tbl ) {
    255282    console_initialize_data();
    256     Console_Port_Data  = calloc( Console_Port_Count, sizeof( console_data ) );
    257     if ( Console_Port_Data == NULL ) {
    258       bsp_fatal( BSP_FATAL_CONSOLE_NO_MEMORY_3 );
    259     }
    260283  }
    261284
Note: See TracChangeset for help on using the changeset viewer.