[70432fc] | 1 | /* SPDX-License-Identifier: BSD-2-Clause */ |
---|
| 2 | |
---|
[17c6ad6a] | 3 | /** |
---|
| 4 | * @file |
---|
| 5 | * |
---|
| 6 | * @brief RTEMS Extend Heap via Sbrk |
---|
| 7 | * @ingroup MallocSupport |
---|
| 8 | */ |
---|
| 9 | |
---|
[47a3cd8] | 10 | /* |
---|
[bcef89f2] | 11 | * Copyright (c) 2012 embedded brains GmbH & Co. KG |
---|
[47a3cd8] | 12 | * |
---|
[70432fc] | 13 | * Redistribution and use in source and binary forms, with or without |
---|
| 14 | * modification, are permitted provided that the following conditions |
---|
| 15 | * are met: |
---|
| 16 | * 1. Redistributions of source code must retain the above copyright |
---|
| 17 | * notice, this list of conditions and the following disclaimer. |
---|
| 18 | * 2. Redistributions in binary form must reproduce the above copyright |
---|
| 19 | * notice, this list of conditions and the following disclaimer in the |
---|
| 20 | * documentation and/or other materials provided with the distribution. |
---|
| 21 | * |
---|
| 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
| 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
---|
| 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
---|
| 25 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
---|
| 26 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
---|
| 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
| 28 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
---|
| 29 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
---|
| 30 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
---|
| 31 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
---|
| 32 | * POSSIBILITY OF SUCH DAMAGE. |
---|
[47a3cd8] | 33 | */ |
---|
| 34 | |
---|
[80cf60e] | 35 | #ifdef HAVE_CONFIG_H |
---|
| 36 | #include "config.h" |
---|
[47a3cd8] | 37 | #endif |
---|
| 38 | |
---|
| 39 | #include <unistd.h> |
---|
| 40 | |
---|
| 41 | #include <rtems/malloc.h> |
---|
| 42 | |
---|
| 43 | #include "malloc_p.h" |
---|
| 44 | |
---|
[6c66bbb] | 45 | static ptrdiff_t RTEMS_Malloc_Sbrk_amount; |
---|
| 46 | |
---|
| 47 | void rtems_heap_set_sbrk_amount( ptrdiff_t sbrk_amount ) |
---|
| 48 | { |
---|
| 49 | RTEMS_Malloc_Sbrk_amount = sbrk_amount; |
---|
| 50 | } |
---|
[47a3cd8] | 51 | |
---|
| 52 | void *rtems_heap_extend_via_sbrk( |
---|
| 53 | Heap_Control *heap, |
---|
| 54 | size_t alloc_size |
---|
| 55 | ) |
---|
| 56 | { |
---|
| 57 | ptrdiff_t sbrk_amount = RTEMS_Malloc_Sbrk_amount; |
---|
| 58 | ptrdiff_t sbrk_size = (ptrdiff_t) alloc_size; |
---|
[b71336c] | 59 | ptrdiff_t misaligned = sbrk_amount != 0 ? sbrk_size % sbrk_amount : 0; |
---|
[47a3cd8] | 60 | void *return_this = NULL; |
---|
| 61 | |
---|
| 62 | if ( misaligned != 0 ) { |
---|
| 63 | sbrk_size += sbrk_amount - misaligned; |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | if ( sbrk_size > 0 && sbrk_amount > 0 ) { |
---|
| 67 | void *area_begin = sbrk( sbrk_size ); |
---|
| 68 | |
---|
| 69 | if ( area_begin != (void *) -1 ) { |
---|
| 70 | bool ok = _Protected_heap_Extend( heap, area_begin, sbrk_size ); |
---|
| 71 | |
---|
| 72 | if ( ok ) { |
---|
| 73 | return_this = _Protected_heap_Allocate( heap, alloc_size ); |
---|
| 74 | } else { |
---|
| 75 | sbrk( -sbrk_size ); |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | return return_this; |
---|
| 81 | } |
---|