1  /**
2  * @file    heap.c
3  * @brief   System level setup help
4  */
5 
6 /*******************************************************************************
7  * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a
10  * copy of this software and associated documentation files (the "Software"),
11  * to deal in the Software without restriction, including without limitation
12  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
13  * and/or sell copies of the Software, and to permit persons to whom the
14  * Software is furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included
17  * in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
20  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
23  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
24  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25  * OTHER DEALINGS IN THE SOFTWARE.
26  *
27  * Except as contained in this notice, the name of Maxim Integrated
28  * Products, Inc. shall not be used except as stated in the Maxim Integrated
29  * Products, Inc. Branding Policy.
30  *
31  * The mere transfer of this software does not imply any licenses
32  * of trade secrets, proprietary technology, copyrights, patents,
33  * trademarks, maskwork rights, or any other form of intellectual
34  * property whatsoever. Maxim Integrated Products, Inc. retains all
35  * ownership rights.
36  *
37  * $Date: 2018-12-18 15:37:22 -0600 (Tue, 18 Dec 2018) $
38  * $Revision: 40072 $
39  *
40  ******************************************************************************/
41 
42 /* **** Includes **** */
43 #include <stdint.h>
44 #include <errno.h>
45 #include <unistd.h>
46 
47 /**
48  * @brief  sbrk
49  * @detail Increase program data space
50  * @detail Malloc and related functions depend on this
51  */
52 
53 /* **** declarations **** */
54 static char *heap_end = 0;
55 extern unsigned int __HeapBase;
56 extern unsigned int __HeapLimit;
57 
58 /* **** functions **** */
_sbrk(int incr)59 caddr_t _sbrk(int incr)
60 {
61     char *prev_heap_end;
62 
63     if (heap_end == 0) {
64         heap_end = (caddr_t)&__HeapBase;
65     }
66     prev_heap_end = heap_end;
67 
68     if ((unsigned int)(heap_end + incr) > (unsigned int)&__HeapLimit) {
69         errno = ENOMEM;
70         return  (caddr_t) -1;
71     }
72 
73     heap_end += incr;
74 
75     return (caddr_t) prev_heap_end;
76 }
77 
78