source: rtems/cpukit/libcsupport/src/rtems_heap_extend_via_sbrk.c @ 80cf60e

5
Last change on this file since 80cf60e was 80cf60e, checked in by Sebastian Huber <sebastian.huber@…>, on Apr 15, 2020 at 7:48:32 AM

Canonicalize config.h include

Use the following variant which was already used by most source files:

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

  • Property mode set to 100644
File size: 1.3 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
32ptrdiff_t RTEMS_Malloc_Sbrk_amount;
33
34void *rtems_heap_extend_via_sbrk(
35  Heap_Control *heap,
36  size_t alloc_size
37)
38{
39  ptrdiff_t sbrk_amount = RTEMS_Malloc_Sbrk_amount;
40  ptrdiff_t sbrk_size = (ptrdiff_t) alloc_size;
41  ptrdiff_t misaligned = sbrk_amount != 0 ? sbrk_size % sbrk_amount : 0;
42  void *return_this = NULL;
43
44  if ( misaligned != 0 ) {
45    sbrk_size += sbrk_amount - misaligned;
46  }
47
48  if ( sbrk_size > 0 && sbrk_amount > 0 ) {
49    void *area_begin = sbrk( sbrk_size );
50
51    if ( area_begin != (void *) -1 ) {
52      bool ok = _Protected_heap_Extend( heap, area_begin, sbrk_size );
53
54      if ( ok ) {
55        return_this = _Protected_heap_Allocate( heap, alloc_size );
56      } else {
57        sbrk( -sbrk_size );
58      }
59    }
60  }
61
62  return return_this;
63}
Note: See TracBrowser for help on using the repository browser.