MPLABĀ® Harmony Graphics Suite  GFX v3.13.0
Legato API Documentation
legato_variableheap.h
Go to the documentation of this file.
1 /*******************************************************************************
2 * Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries.
3 *
4 * Subject to your compliance with these terms, you may use Microchip software
5 * and any derivatives exclusively with Microchip products. It is your
6 * responsibility to comply with third party license terms applicable to your
7 * use of third party software (including open source software) that may
8 * accompany Microchip software.
9 *
10 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
11 * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
12 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
13 * PARTICULAR PURPOSE.
14 *
15 * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
16 * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
17 * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
18 * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
19 * FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
20 * ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
21 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
22 *******************************************************************************/
29 #ifndef LEGATO_VARIABLEHEAP_H
30 #define LEGATO_VARIABLEHEAP_H
31 
33 
34 #if LE_MEMORY_MANAGER_ENABLE == 1
35 
36 #if LE_USE_DEBUG_ALLOCATOR == 1
37 #define LE_VHEAP_ALLOC(heap, size) leVariableHeap_Alloc(heap, size, __LINE__, __FUNCTION__, __FILE__)
38 #define LE_VHEAP_REALLOC(heap, ptr, size) leVariableHeap_Realloc(heap, ptr, size, __LINE__, __FUNCTION__, __FILE__)
39 #else
40 #define LE_VHEAP_ALLOC(heap, size) leVariableHeap_Alloc(heap, size)
41 #define LE_VHEAP_REALLOC(heap, ptr, size) leVariableHeap_Realloc(ptr, size)
42 #endif
43 
44 // *****************************************************************************
45 /* Structure:
46  struct leVariableHeap
47 
48  Summary:
49  Defines a variable heap object. A variable heap is an allocator is
50  capable of allocating blocks of a variable size. This heap is flexible
51  in capability but suffers from fragmentation and possibly slower allocation
52  times than a fixed heap. Good for larger allocation sizes.
53 
54  leBool initialized - indicates whether the heap has been properly initialized
55  size_t size - the overall size of the heap
56  size_t used - the amount of space the heap is currently using
57  size_t maxUsage - the highest amount of usage the heap has seen
58  void* data - pointer to the heap data
59  void* allocList - a linked list of allocation data blocks
60  void* freeList - a linked list of free data blocks
61  size_t allocBlockCount - the number of blocks currently allocated
62  size_t freeBlockCount - the number of blocks in the free list
63 */
71 typedef struct leVariableHeap
72 {
73  leBool initialized;
74  size_t size;
75  size_t used;
76  size_t maxUsage;
77 
78  void* data;
79 
80  void* allocList;
81  void* freeList;
82 
83  size_t allocBlockCount;
84  size_t freeBlockCount;
85 } leVariableHeap;
86 
87 // *****************************************************************************
102 leResult leVariableHeap_Init(leVariableHeap* heap,
103  void* data,
104  size_t size);
105 
106 // *****************************************************************************
117 void leVariableHeap_Destroy(leVariableHeap* heap);
118 
119 #if LE_USE_DEBUG_ALLOCATOR == 1
120 // *****************************************************************************
121 /* Function:
122  void* leVariableHeap_Alloc(leVariableHeap* heap,
123  size_t size,
124  size_t lineNum,
125  const char* funcName,
126  const char* fileName)
127 
128  Summary:
129  Allocates a block from a variable heap.
130 
131  Description:
132  Allocates a block from a variable heap. This is the debug version of this
133  function and requires additional parameters.
134 
135  Use the LE_MALLOC macro to automatically differentiate between
136  this function and its non-debug counterpart.
137 
138  Parameters:
139  leVariableHeap* heap - pointer to the heap object
140  size_t size - the size
141  size_t lineNum - the file line number where this allocation occured
142  const char* funcName - the function name where this allocation occured
143  const char* fileName - the file name where this allocation occured
144 
145  Returns:
146  leResult
147 */
148 void* leVariableHeap_Alloc(leVariableHeap* heap,
149  size_t size,
150  size_t lineNum,
151  const char* funcName,
152  const char* fileName);
153 #else
154 // *****************************************************************************
166 void* leVariableHeap_Alloc(leVariableHeap* heap,
167  size_t size);
168 #endif
169 
170 #if LE_USE_DEBUG_ALLOCATOR == 1
171 
186 void* leVariableHeap_Realloc(leVariableHeap* heap,
187  void* ptr,
188  size_t size,
189  size_t lineNum,
190  const char* funcName,
191  const char* fileName);
192 
193 #else
194 
206 void* leVariableHeap_Realloc(leVariableHeap* heap,
207  void* ptr,
208  size_t size);
209 #endif
210 // *****************************************************************************
223 void leVariableHeap_Free(leVariableHeap* heap,
224  void* ptr);
225 
226 // *****************************************************************************
240 leBool leVariableHeap_Contains(leVariableHeap* heap,
241  void* ptr);
242 
243 // *****************************************************************************
258 leResult leVariableHeap_Validate(leVariableHeap* heap);
259 
260 // *****************************************************************************
273 size_t leVariableHeap_SizeOf(leVariableHeap* heap,
274  void* ptr);
275 
276 // *****************************************************************************
277 
290 void leVariableHeap_Dump(leVariableHeap* heap,
291  leBool dumpRecords);
292 
293 #endif // LE_MEMORY_MANAGER_ENABLE
294 
295 #endif /* LEGATO_VARIABLEHEAP_H */
296 
Common macros and definitions used by Legato.
leResult
This enum represents function call results.
Definition: legato_common.h:123
leBool
This enum represents booleans.
Definition: legato_common.h:146