1 /******************************************************************************
2 * Copyright (c) 2022 Telink Semiconductor (Shanghai) Co., Ltd. ("TELINK")
3 * All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *****************************************************************************/
18 /*************************************************************************************************/
19 /*!
20 * \file my_heap.c
21 *
22 * \brief Heap service.
23 *
24 * Copyright (c) 2009-2018 Arm Ltd. All Rights Reserved.
25 *
26 * Copyright (c) 2019-2020 Packetcraft, Inc.
27 *
28 * Licensed under the Apache License, Version 2.0 (the "License");
29 * you may not use this file except in compliance with the License.
30 * You may obtain a copy of the License at
31 *
32 * http://www.apache.org/licenses/LICENSE-2.0
33 *
34 * Unless required by applicable law or agreed to in writing, software
35 * distributed under the License is distributed on an "AS IS" BASIS,
36 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
37 * See the License for the specific language governing permissions and
38 * limitations under the License.
39 */
40 /*************************************************************************************************/
41
42 #include "common\compiler.h"
43 #include "common\types.h"
44
45 /**************************************************************************************************
46 Macros
47 **************************************************************************************************/
48 #define HEAP_MEM_SIZE_CFG 3072 // 3K Heap
49
50 /**************************************************************************************************
51 Global Variables
52 **************************************************************************************************/
53
54 /* ! Free memory for pool buffers. */
55 _attribute_data_retention_ static u8 myBufMem[HEAP_MEM_SIZE_CFG];
56
57 _attribute_data_retention_ static u8 *SystemHeapStart = myBufMem;
58 _attribute_data_retention_ static u32 SystemHeapSize = HEAP_MEM_SIZE_CFG;
59
60 /* !
61 * \brief Reserve heap memory.
62 *
63 * \param size Number of bytes of heap memory used.
64 */
myHeapAlloc(u32 size)65 void myHeapAlloc(u32 size)
66 {
67 /* Round up to nearest multiple of 4 for word alignment */
68 size = (size + 3) & ~3;
69
70 SystemHeapStart += size;
71 SystemHeapSize -= size;
72 }
73
74 /*!
75 * \brief Get next available heap memory.
76 *
77 * \return Address of the start of heap memory.
78 */
myHeapGetFreeStartAddress(void)79 void *myHeapGetFreeStartAddress(void)
80 {
81 return (void *)SystemHeapStart;
82 }
83
84 /* !
85 * \brief Get heap available.
86 *
87 * \return Number of bytes of heap memory available.
88 */
myHeapCountAvailable(void)89 u32 myHeapCountAvailable(void)
90 {
91 return SystemHeapSize;
92 }
93
94 /* !
95 * \brief Get heap used.
96 *
97 * \return Number of bytes of heap memory used.
98 */
myHeapCountUsed(void)99 u32 myHeapCountUsed(void)
100 {
101 return HEAP_MEM_SIZE_CFG - SystemHeapSize;
102 }
103