1 /* 2 * Copyright (c) 2015, Freescale Semiconductor, Inc. 3 * Copyright 2016-2019, 2022, 2024 NXP 4 * All rights reserved. 5 * 6 * SPDX-License-Identifier: BSD-3-Clause 7 */ 8 #if defined(__GNUC__) 9 #include <errno.h> 10 #include <stdint.h> 11 #include <stddef.h> 12 #endif 13 14 #if defined(__GNUC__) 15 /*! 16 * @brief Function to override ARMGCC default function _sbrk 17 * 18 * _sbrk is called by malloc. ARMGCC default _sbrk compares "SP" register and 19 * heap end, if heap end is larger than "SP", then _sbrk returns error and 20 * memory allocation failed. This function changes to compare __HeapLimit with 21 * heap end. 22 */ 23 void * _sbrk(ptrdiff_t incr); _sbrk(ptrdiff_t incr)24void * _sbrk(ptrdiff_t incr) 25 { 26 extern char end __asm("end"); 27 extern char heap_limit __asm("__HeapLimit"); 28 static char *heap_end; 29 char *prev_heap_end; 30 void *ret; 31 32 if (heap_end == NULL) 33 { 34 heap_end = &end; 35 } 36 37 prev_heap_end = heap_end; 38 39 if ((uintptr_t)heap_end + (uintptr_t)incr > (uintptr_t)(&heap_limit)) 40 { 41 errno = ENOMEM; 42 43 ret = (void *)-1; 44 } 45 else 46 { 47 heap_end = (char *)((uintptr_t)heap_end + (uintptr_t)incr); 48 49 ret = (void *)prev_heap_end; 50 } 51 52 return ret; 53 } 54 #endif 55