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