source: rtems/cpukit/libcsupport/src/malloc_sbrk_helpers.c @ 1986152

4.104.115
Last change on this file since 1986152 was bd5984de, checked in by Joel Sherrill <joel.sherrill@…>, on 09/17/08 at 18:37:55

2008-09-17 Joel Sherrill <joel.sherrill@…>

  • libcsupport/src/free.c, libcsupport/src/malloc.c, libcsupport/src/malloc_initialize.c, libcsupport/src/malloc_p.h, libcsupport/src/malloc_sbrk_helpers.c, libcsupport/src/malloc_statistics_helpers.c, libcsupport/src/malloc_walk.c, libcsupport/src/mallocfreespace.c, libcsupport/src/mallocinfo.c, libcsupport/src/realloc.c, libcsupport/src/rtems_memalign.c, sapi/include/confdefs.h, score/inline/rtems/score/thread.inl: Add support for optionally having a unified work area. In other words, the RTEMS Workspace and C Program Heap are the same pool of memory.
  • Property mode set to 100644
File size: 2.3 KB
Line 
1/*
2 *  RTEMS Malloc -- SBRK Support Plugin
3 *
4 *  COPYRIGHT (c) 1989-2007.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.rtems.com/license/LICENSE.
10 *
11 *  $Id$
12 */
13
14#if HAVE_CONFIG_H
15#include "config.h"
16#endif
17
18#include <rtems.h>
19#include <rtems/malloc.h>
20#include "malloc_p.h"
21
22#include <errno.h>
23
24size_t  RTEMS_Malloc_Sbrk_amount;
25
26void *malloc_sbrk_initialize(
27  void  *starting_address,
28  size_t length
29)
30{
31  uintptr_t     old_address;
32  uintptr_t     uaddress;
33
34  RTEMS_Malloc_Sbrk_amount = length;
35
36  /*
37   * If the starting address is 0 then we are to attempt to
38   * get length worth of memory using sbrk. Make sure we
39   * align the address that we get back.
40   */
41
42  if (!starting_address) {
43    uaddress = (uintptr_t)sbrk(length);
44
45    if (uaddress == (uintptr_t) -1) {
46      rtems_fatal_error_occurred( RTEMS_NO_MEMORY );
47      /* DOES NOT RETURN!!! */
48    }
49
50    if (uaddress & (CPU_HEAP_ALIGNMENT-1)) {
51      old_address = uaddress;
52      uaddress = (uaddress + CPU_HEAP_ALIGNMENT) & ~(CPU_HEAP_ALIGNMENT-1);
53
54      /*
55       * adjust the length by whatever we aligned by
56       */
57      length -= uaddress - old_address;
58    }
59
60    starting_address = (void *)uaddress;
61  }
62  return starting_address;
63}
64
65void *malloc_sbrk_extend_and_allocate(
66  size_t size
67)
68{
69  uint32_t  sbrk_amount;
70  void     *starting_address;
71  uint32_t  the_size;
72  void     *return_this;
73
74  /*
75   *  Round to the "requested sbrk amount" so hopefully we won't have
76   *  to grow again for a while.  This effectively does sbrk() calls
77   *  in "page" amounts.
78   */
79
80  sbrk_amount = RTEMS_Malloc_Sbrk_amount;
81
82  if ( sbrk_amount == 0 )
83    return (void *) 0;
84
85  the_size = ((size + sbrk_amount) / sbrk_amount * sbrk_amount);
86
87  starting_address = (void *) sbrk(the_size);
88  if ( starting_address == (void*) -1 )
89    return (void *) 0;
90
91  if ( !_Protected_heap_Extend(
92          RTEMS_Malloc_Heap, starting_address, the_size) ) {
93    sbrk(-the_size);
94    errno = ENOMEM;
95    return (void *) 0;
96  }
97
98  MSBUMP(space_available, the_size);
99
100  return_this = _Protected_heap_Allocate( RTEMS_Malloc_Heap, size );
101  return return_this;
102}
103
104
105rtems_malloc_sbrk_functions_t rtems_malloc_sbrk_helpers_table = {
106  malloc_sbrk_initialize,
107  malloc_sbrk_extend_and_allocate
108};
109
110
Note: See TracBrowser for help on using the repository browser.