MPLABĀ® Harmony Graphics Suite  GFX v3.13.0
Legato API Documentation
legato_fixedheap.h
Go to the documentation of this file.
1 // DOM-IGNORE-BEGIN
2 /*******************************************************************************
3 * Copyright (C) 2020 Microchip Technology Inc. and its subsidiaries.
4 *
5 * Subject to your compliance with these terms, you may use Microchip software
6 * and any derivatives exclusively with Microchip products. It is your
7 * responsibility to comply with third party license terms applicable to your
8 * use of third party software (including open source software) that may
9 * accompany Microchip software.
10 *
11 * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER
12 * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED
13 * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A
14 * PARTICULAR PURPOSE.
15 *
16 * IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE,
17 * INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND
18 * WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS
19 * BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE
20 * FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN
21 * ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
22 * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
23 *******************************************************************************/
24 // DOM-IGNORE-END
25 
33 #ifndef LEGATO_FIXEDHEAP_H
34 #define LEGATO_FIXEDHEAP_H
35 
37 
38 #if LE_MEMORY_MANAGER_ENABLE == 1
39 
40 #if LE_FIXEDHEAP_ENABLE == 1
41 
42 #if LE_USE_DEBUG_ALLOCATOR == 1
43 #define LE_FHEAP_ALLOC(heap) leFixedHeap_Alloc(heap, __LINE__, __FUNCTION__, __FILE__)
44 #define LE_FHEAP_REALLOC(heap, ptr) leFixedHeap_Realloc(heap, ptr, __LINE__, __FUNCTION__, __FILE__)
45 #else
46 #define LE_FHEAP_ALLOC(heap) leFixedHeap_Alloc(heap)
47 #define LE_FHEAP_REALLOC(heap, ptr) leFixedHeap_Realloc(ptr)
48 #endif
49 
50 #if LE_FIXEDHEAP_DEBUG == 1
51 #define LE_FIXEDHEAP_HEADER_SIZE sizeof(leFixedHeapDebugHeader)
52 #define LE_FIXEDHEAP_FOOTER_SIZE 4
53 #define LE_FIXEDHEAP_CHECKSUM (-1)
54 
55 #define LE_FIXEDHEAP_BLOCK_FOOTER_PTR(size, blk) ((uint8_t*)blk + size - LE_FIXEDHEAP_FOOTER_SIZE)
56 
57 #else
58 #define LE_FIXEDHEAP_HEADER_SIZE 0
59 #define LE_FIXEDHEAP_FOOTER_SIZE 0
60 #endif
61 
62 #define LE_FIXEDHEAP_BLOCK_SIZE(size) (LE_FIXEDHEAP_HEADER_SIZE + size + LE_FIXEDHEAP_FOOTER_SIZE)
63 
64 #if LE_FIXEDHEAP_DEBUG == 1
65 // *****************************************************************************
66 /* Structure:
67  struct leFixedHeapDebugHeader
68 
69  Summary:
70  Defines a fixed heap block debug header
71 
72  uint32_t lineNumber - the line number of the allocation
73  const char* funcName - the function name of the allocation
74  const char* fileName - the file name of the allocation
75  leBool free - indicates if the block is free
76  uint32_t checksum - the block checksum
77 */
83 typedef struct leFixedHeapDebugHeader
84 {
85 #if LE_USE_ALLOCATION_TRACKING == 1
86  uint32_t lineNumber;
87  const char* funcName;
88  const char* fileName;
89 #endif
90 
91  leBool free;
92  uint32_t checksum;
93 } leFixedHeapDebugHeader;
94 #endif
95 
96 // *****************************************************************************
97 /* Structure:
98  struct leFixedHeapBlock
99 
100  Summary:
101  Defines a fixed heap block
102 
103  leFixedHeapDebugHeader debug - the block debug information
104  void* data - pointer to the block data, also acts as a linked list
105  pointer to free blocks
106 */
111 typedef struct leFixedHeapBlock
112 {
113 #if LE_FIXEDHEAP_DEBUG == 1
114  leFixedHeapDebugHeader debug;
115 #endif
116  void* data;
117 } leFixedHeapBlock;
118 
119 // *****************************************************************************
120 /* Structure:
121  struct leFixedHeap
122 
123  Summary:
124  Defines a fixed heap object
125 
126  leBool initialized - indicates whether the heap has been properly initialized
127  uint32_t numElements - the number of currently allocated blocks
128  uint32_t logicalBlockSize - the logical block size
129  uint32_t physicalBlockSize - the physical size of the blocks
130  uint32_t capacity - the total number of blocks in the heap
131  uint32_t maxUsage - usage watermark value
132  void* data - pointer to the actual heap data buffer
133  void* freeList - pointer to the next free block
134 */
139 typedef struct leFixedHeap
140 {
141  leBool initialized;
142  uint32_t numElements;
143  uint32_t logicalBlockSize;
144  uint32_t physicalBlockSize;
145  uint32_t capacity;
146  uint32_t maxUsage;
147  void* data;
148  void* freeList;
149 } leFixedHeap;
150 
151 // *****************************************************************************
152 /* Function:
153  leResult leFixedHeap_Init(leFixedHeap* heap,
154  uint32_t size,
155  uint32_t count,
156  uint8_t* data)
157 
158  Summary:
159  Initializes a fixed heap object struct.
160 
161  Description:
162  A fixed heap is a memory storage manager that relies on fixed blocks of
163  data to fulfull allocation requests. Fixed blocks do not suffer from
164  memory fragmentation and fulfill allocation requests faster than a
165  variable heap but often use memory less efficiently.
166 
167  Parameters:
168  leFixedHeap* heap - pointer to the heap object
169  uint32_t size - the size a heap block in bytes
170  uint32_t count - the number of blocks in the heap
171  uint8_t* data - pointer to the data buffer to use for the heap
172 
173  Returns:
174  leResult
175 */
189 leResult leFixedHeap_Init(leFixedHeap* heap,
190  uint32_t size,
191  uint32_t count,
192  uint8_t* data);
193 
194 // *****************************************************************************
195 /* Function:
196  void leFixedHeap_Destroy(leFixedHeap* heap)
197 
198  Summary:
199  Destroys a fixed heap object.
200 
201  Description:
202  Destroys a fixed heap object. All pointers that are allocated from this
203  heap should be considered invalid.
204 
205  Parameters:
206  leFixedHeap* heap - pointer to the heap object
207 
208  Returns:
209  leResult
210 */
222 void leFixedHeap_Destroy(leFixedHeap* heap);
223 
224 #if LE_USE_DEBUG_ALLOCATOR == 1
225 // *****************************************************************************
226 /* Function:
227  void* leFixedHeap_Alloc(leFixedHeap* heap,
228  uint32_t lineNum,
229  const char* funcName,
230  const char* fileName)
231 
232  Summary:
233  Allocates a block from a fixed heap.
234 
235  Description:
236  Allocates a block from a fixed heap. This is the debug version of this
237  function and requires additional parameters.
238 
239  Use the LE_MALLOC macro to automatically differentiate between
240  this function and its non-debug counterpart.
241 
242  Parameters:
243  leFixedHeap* heap - pointer to the heap object
244  uint32_t lineNum - the file line number where this allocation occured
245  const char* funcName - the function name where this allocation occured
246  const char* fileName - the file name where this allocation occured
247 
248  Returns:
249  leResult
250 */
267 void* leFixedHeap_Alloc(leFixedHeap* heap,
268  uint32_t lineNum,
269  const char* funcName,
270  const char* fileName);
271 #else
272 // *****************************************************************************
273 /* Function:
274  void* leFixedHeap_Alloc(leFixedHeap* heap)
275 
276  Summary:
277  Allocates a block from a fixed heap.
278 
279  Description:
280  Allocates a block from a fixed heap.
281 
282  Use the LE_MALLOC macro to automatically differentiate between
283  this function and its debug counterpart.
284 
285  Parameters:
286  leFixedHeap* heap - pointer to the heap object
287 
288  Returns:
289  leResult
290 */
291 
302 void* leFixedHeap_Alloc(leFixedHeap* heap);
303 #endif
304 
305 // *****************************************************************************
306 /* Function:
307  void* leFixedHeap_Free(leFixedHeap* heap, void* ptr)
308 
309  Summary:
310  Frees a block from a fixed heap.
311 
312  Description:
313  Frees a block from a fixed heap.
314 
315  Parameters:
316  leFixedHeap* heap - pointer to the heap object
317  void* ptr - the pointer to free
318 
319  Returns:
320  leResult
321 */
334 void leFixedHeap_Free(leFixedHeap* heap, void* ptr);
335 
336 // *****************************************************************************
337 /* Function:
338  leBool leFixedHeap_Contains(leFixedHeap* heap, void* ptr)
339 
340  Summary:
341  Queries a heap to see if it contains a block pointer.
342 
343  Description:
344  Queries a heap to see if it contains a block pointer.
345 
346  Parameters:
347  leFixedHeap* heap - pointer to the heap object
348  void* ptr - the pointer to look for
349 
350  Returns:
351  leResult
352 */
365 leBool leFixedHeap_Contains(leFixedHeap* heap, void* ptr);
366 
367 // *****************************************************************************
368 /* Function:
369  leResult leFixedHeap_Validate(leFixedHeap* heap)
370 
371  Summary:
372  Validates a heap.
373 
374  Description:
375  Validates a heap. Validation involves scanning the heap blocks to look
376  for errors. The amount of checking is dependent on the heap debug level.
377  When heap debugging is enabled the heap will use extra memory to
378  store block checksums. These checksums are verified to look for buffer
379  overruns or other errors.
380 
381  Parameters:
382  leFixedHeap* heap - pointer to the heap object
383 
384  Returns:
385  leResult
386 */
402 leResult leFixedHeap_Validate(leFixedHeap* heap);
403 
404 #ifdef _WIN32
405 // *****************************************************************************
406 /* Function:
407  leFixedHeap_Dump(leFixedHeap* heap, leBool dumpRecords)
408 
409  Summary:
410  Dumps the contents of a heap to STDOUT.
411 
412  Description:
413  Dumps the contents of a heap to STDOUT.
414 
415  Parameters:
416  leFixedHeap* heap - pointer to the heap object
417  leBool dumpRecords - set to true to dump the actual block data arrays
418 
419  Returns:
420  void
421 */
436 void leFixedHeap_Dump(leFixedHeap* heap, leBool dumpRecords);
437 #endif
438 
439 #endif
440 
441 #endif // LE_MEMORY_MANAGER_ENABLE
442 
443 #endif /* LEGATO_FIXEDHEAP_H */
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