source: rtems/cpukit/libcsupport/src/rtems_heap_extend_via_sbrk.c

Last change on this file was 6c66bbb, checked in by Sebastian Huber <sebastian.huber@…>, on Apr 20, 2021 at 7:58:06 AM

malloc: Hide RTEMS_Malloc_Sbrk_amount

Move RTEMS_Malloc_Sbrk_amount to the only implementation file which uses
it and make it private to hide implementation-details from an API
header.

  • Property mode set to 100644
File size: 1.4 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief RTEMS Extend Heap via Sbrk
5 *  @ingroup MallocSupport
6 */
7
8/*
9 * Copyright (c) 2012 embedded brains GmbH.  All rights reserved.
10 *
11 *  embedded brains GmbH
12 *  Obere Lagerstr. 30
13 *  82178 Puchheim
14 *  Germany
15 *  <rtems@embedded-brains.de>
16 *
17 * The license and distribution terms for this file may be
18 * found in the file LICENSE in this distribution or at
19 * http://www.rtems.org/license/LICENSE.
20 */
21
22#ifdef HAVE_CONFIG_H
23#include "config.h"
24#endif
25
26#include <unistd.h>
27
28#include <rtems/malloc.h>
29
30#include "malloc_p.h"
31
32static ptrdiff_t RTEMS_Malloc_Sbrk_amount;
33
34void rtems_heap_set_sbrk_amount( ptrdiff_t sbrk_amount )
35{
36  RTEMS_Malloc_Sbrk_amount = sbrk_amount;
37}
38
39void *rtems_heap_extend_via_sbrk(
40  Heap_Control *heap,
41  size_t alloc_size
42)
43{
44  ptrdiff_t sbrk_amount = RTEMS_Malloc_Sbrk_amount;
45  ptrdiff_t sbrk_size = (ptrdiff_t) alloc_size;
46  ptrdiff_t misaligned = sbrk_amount != 0 ? sbrk_size % sbrk_amount : 0;
47  void *return_this = NULL;
48
49  if ( misaligned != 0 ) {
50    sbrk_size += sbrk_amount - misaligned;
51  }
52
53  if ( sbrk_size > 0 && sbrk_amount > 0 ) {
54    void *area_begin = sbrk( sbrk_size );
55
56    if ( area_begin != (void *) -1 ) {
57      bool ok = _Protected_heap_Extend( heap, area_begin, sbrk_size );
58
59      if ( ok ) {
60        return_this = _Protected_heap_Allocate( heap, alloc_size );
61      } else {
62        sbrk( -sbrk_size );
63      }
64    }
65  }
66
67  return return_this;
68}
Note: See TracBrowser for help on using the repository browser.