source: rtems/cpukit/libcsupport/src/malloc.c @ 4088d01d

4.104.114.9
Last change on this file since 4088d01d was 4088d01d, checked in by Ralf Corsepius <ralf.corsepius@…>, on Sep 1, 2008 at 11:42:19 AM

Convert using "bool".

  • Property mode set to 100644
File size: 2.3 KB
Line 
1/*
2 *  RTEMS Malloc Family Implementation
3 *
4 *
5 *  COPYRIGHT (c) 1989-2007.
6 *  On-Line Applications Research Corporation (OAR).
7 *
8 *  The license and distribution terms for this file may be
9 *  found in the file LICENSE in this distribution or at
10 *  http://www.rtems.com/license/LICENSE.
11 *
12 *  $Id$
13 */
14
15#if HAVE_CONFIG_H
16#include "config.h"
17#endif
18
19#ifdef RTEMS_NEWLIB
20#include <stdlib.h>
21#include <errno.h>
22
23#include "malloc_p.h"
24
25void *malloc(
26  size_t  size
27)
28{
29  void        *return_this;
30
31  MSBUMP(malloc_calls, 1);
32
33  /*
34   *  If some free's have been deferred, then do them now.
35   */
36  malloc_deferred_frees_process();
37
38  /*
39   * Validate the parameters
40   */
41  if ( !size )
42    return (void *) 0;
43
44  /*
45   *  Do not attempt to allocate memory if not in correct system state.
46   */
47  if ( _System_state_Is_up(_System_state_Get()) &&
48       !malloc_is_system_state_OK() )
49    return NULL;
50
51  /*
52   *  Walk the heap and verify its integrity
53   */
54  #if defined(RTEMS_HEAP_DEBUG)
55    _Protected_heap_Walk( &RTEMS_Malloc_Heap, 0, false );
56  #endif
57
58  #if defined(RTEMS_MALLOC_BOUNDARY_HELPERS)
59    /*
60     *  If the support for a boundary area at the end of the heap
61     *  block allocated is turned on, then adjust the size.
62     */
63    if (rtems_malloc_boundary_helpers)
64      size += (*rtems_malloc_boundary_helpers->overhead)();
65  #endif
66
67  /*
68   * Try to give a segment in the current heap if there is not
69   * enough space then try to grow the heap.
70   * If this fails then return a NULL pointer.
71   */
72
73  return_this = _Protected_heap_Allocate( &RTEMS_Malloc_Heap, size );
74
75  if ( !return_this ) {
76    if (rtems_malloc_sbrk_helpers) 
77      return_this = (*rtems_malloc_sbrk_helpers->extend)( size );
78    if ( !return_this ) {
79      errno = ENOMEM;
80      return (void *) 0;
81    }
82  }
83
84  /*
85   *  If the user wants us to dirty the allocated memory, then do it.
86   */
87  if ( rtems_malloc_dirty_helper )
88    (*rtems_malloc_dirty_helper)( return_this, size );
89
90  /*
91   *  If configured, update the statistics
92   */
93  if ( rtems_malloc_statistics_helpers )
94    (*rtems_malloc_statistics_helpers->at_malloc)(return_this);
95
96  #if defined(RTEMS_MALLOC_BOUNDARY_HELPERS)
97    /*
98     * If configured, set the boundary area
99     */
100    if (rtems_malloc_boundary_helpers)
101      (*rtems_malloc_boundary_helpers->at_malloc)(return_this, size);
102  #endif
103
104  return return_this;
105}
106
107#endif
Note: See TracBrowser for help on using the repository browser.