source: rtems/cpukit/libcsupport/src/malloc_initialize.c @ 3c1be2f

4.115
Last change on this file since 3c1be2f was 3c1be2f, checked in by Joel Sherrill <joel.sherrill@…>, on 07/08/10 at 20:09:56

2010-07-08 Joel Sherrill <joel.sherrill@…>

  • libcsupport/src/malloc_initialize.c: Clean up sbrk path now that a test is available.
  • Property mode set to 100644
File size: 3.2 KB
Line 
1/**
2 * @file
3 *
4 * @brief Malloc initialization implementation.
5 */
6
7/*
8 *  COPYRIGHT (c) 1989-2007.
9 *  On-Line Applications Research Corporation (OAR).
10 *
11 *  The license and distribution terms for this file may be
12 *  found in the file LICENSE in this distribution or at
13 *  http://www.rtems.com/license/LICENSE.
14 *
15 *  $Id$
16 */
17
18#if HAVE_CONFIG_H
19#include "config.h"
20#endif
21
22#include <rtems.h>
23#include <rtems/malloc.h>
24#include <rtems/score/wkspace.h>
25#include "malloc_p.h"
26
27/* FIXME: Dummy function */
28#ifndef RTEMS_NEWLIB
29void RTEMS_Malloc_Initialize(
30  void *heap_begin,
31  uintptr_t heap_size,
32  size_t sbrk_amount
33)
34{
35}
36#else
37rtems_malloc_statistics_t rtems_malloc_statistics;
38extern bool rtems_unified_work_area;
39
40void RTEMS_Malloc_Initialize(
41  void *heap_begin,
42  uintptr_t heap_size,
43  size_t sbrk_amount
44)
45{
46  /*
47   *  If configured, initialize the statistics support
48  */
49  if ( rtems_malloc_statistics_helpers != NULL ) {
50    (*rtems_malloc_statistics_helpers->initialize)();
51  }
52
53  /*
54   *  Initialize the garbage collection list to start with nothing on it.
55   */
56  malloc_deferred_frees_initialize();
57
58  /*
59   *  Initialize the optional sbrk support for extending the heap
60   */
61  if ( rtems_malloc_sbrk_helpers != NULL ) {
62    heap_begin = (*rtems_malloc_sbrk_helpers->initialize)(
63      heap_begin,
64      sbrk_amount
65    );
66    heap_size  = (uintptr_t) sbrk_amount;
67  }
68
69  /*
70   *  If this system is configured to use the same heap for
71   *  the RTEMS Workspace and C Program Heap, then we need to
72   *  be very very careful about destroying the initialization
73   *  that has already been done.
74   */
75
76  /*
77   *  If the BSP is not clearing out the workspace, then it is most likely
78   *  not clearing out the initial memory for the heap.  There is no
79   *  standard supporting zeroing out the heap memory.  But much code
80   *  with UNIX history seems to assume that memory malloc'ed during
81   *  initialization (before any free's) is zero'ed.  This is true most
82   *  of the time under UNIX because zero'ing memory when it is first
83   *  given to a process eliminates the chance of a process seeing data
84   *  left over from another process.  This would be a security violation.
85   */
86
87  if (
88    !rtems_unified_work_area
89      && rtems_configuration_get_do_zero_of_workspace()
90  ) {
91     memset( heap_begin, 0, heap_size );
92  }
93
94  /*
95   *  Unfortunately we cannot use assert if this fails because if this
96   *  has failed we do not have a heap and if we do not have a heap
97   *  STDIO cannot work because there will be no buffers.
98   */
99
100  if ( !rtems_unified_work_area ) {
101    uintptr_t status = _Protected_heap_Initialize(
102      RTEMS_Malloc_Heap,
103      heap_begin,
104      heap_size,
105      CPU_HEAP_ALIGNMENT
106    );
107    if ( status == 0 ) {
108      rtems_fatal_error_occurred( RTEMS_NO_MEMORY );
109    }
110  }
111
112  MSBUMP( space_available, _Protected_heap_Get_size(RTEMS_Malloc_Heap) );
113
114  #if defined(RTEMS_HEAP_DEBUG)
115    if ( _Protected_heap_Walk( RTEMS_Malloc_Heap, 0, false ) ) {
116      printk( "Malloc heap not initialized correctly\n" );
117      rtems_print_buffer( heap_begin, 32 );
118      printk( "\n" );
119      rtems_print_buffer( (heap_begin + heap_size) - 48, 48 );
120      rtems_fatal_error_occurred( RTEMS_NO_MEMORY );
121    }
122  #endif
123}
124#endif
Note: See TracBrowser for help on using the repository browser.