microui  14.1.1
microui
LLUI_DISPLAY_HEAP_impl.c
Go to the documentation of this file.
1 /*
2  * C
3  *
4  * Copyright 2021-2024 MicroEJ Corp. All rights reserved.
5  * Use of this source code is governed by a BSD-style license that can be found with this software.
6  */
7 
20 #include "ui_configuration.h"
21 
22 #if defined(UI_FEATURE_ALLOCATOR) && defined(UI_FEATURE_ALLOCATOR_BESTFIT) && \
23  (UI_FEATURE_ALLOCATOR == UI_FEATURE_ALLOCATOR_BESTFIT)
24 
25 // -----------------------------------------------------------------------------
26 // Includes
27 // -----------------------------------------------------------------------------
28 
29 #include "microui_heap.h"
30 #include "BESTFIT_ALLOCATOR.h"
31 
32 // --------------------------------------------------------------------------------
33 // Macros and Defines
34 // --------------------------------------------------------------------------------
35 
36 /*
37  * @brief The best fit allocator stores a main header of 68 bytes (0x44) in the heap.
38  */
39 #define BESTFITALLOCATOR_HEADER_SIZE (68)
40 
41 /*
42  * @brief The best fit allocator stores a header before an allocated block and a footer
43  * after. The header holds the block size plus the header and footer sizes. This macro
44  * retrieves the block full size (header + data + footer).
45  */
46 #define BESTFITALLOCATOR_BLOCK_SIZE(block) ((*(uint32_t *)((block) - sizeof(uint32_t))) & 0x7ffffff)
47 
48 // --------------------------------------------------------------------------------
49 // Private fields
50 // --------------------------------------------------------------------------------
51 
52 static BESTFIT_ALLOCATOR image_heap;
53 static uint32_t heap_size;
54 static uint32_t free_space;
55 static uint32_t allocated_blocks_number;
56 
57 // --------------------------------------------------------------------------------
58 // microui_heap.h functions
59 // --------------------------------------------------------------------------------
60 
61 uint32_t MICROUI_HEAP_total_space(void) {
62  return heap_size;
63 }
64 
65 uint32_t MICROUI_HEAP_free_space(void) {
66  return free_space;
67 }
68 
69 uint32_t MICROUI_HEAP_number_of_allocated_blocks(void) {
70  return allocated_blocks_number;
71 }
72 
73 // --------------------------------------------------------------------------------
74 // LLUI_DISPLAY_impl.h functions
75 // --------------------------------------------------------------------------------
76 
77 void LLUI_DISPLAY_IMPL_imageHeapInitialize(uint8_t *heap_start, uint8_t *heap_limit) {
78  heap_size = heap_limit - heap_start - BESTFITALLOCATOR_HEADER_SIZE;
79  free_space = heap_size;
80  BESTFIT_ALLOCATOR_new(&image_heap);
81  BESTFIT_ALLOCATOR_initialize(&image_heap, (int32_t)heap_start, (int32_t)heap_limit);
82 }
83 
84 uint8_t * LLUI_DISPLAY_IMPL_imageHeapAllocate(uint32_t size) {
85  uint8_t *addr = (uint8_t *)BESTFIT_ALLOCATOR_allocate(&image_heap, (int32_t)size);
86 
87  if (NULL != addr) {
88  free_space -= BESTFITALLOCATOR_BLOCK_SIZE(addr);
89  allocated_blocks_number++;
90  }
91  return addr;
92 }
93 
94 void LLUI_DISPLAY_IMPL_imageHeapFree(uint8_t *block) {
95  free_space += BESTFITALLOCATOR_BLOCK_SIZE(block);
96  allocated_blocks_number--;
97  BESTFIT_ALLOCATOR_free(&image_heap, (void *)block);
98 }
99 
100 // --------------------------------------------------------------------------------
101 // EOF
102 // --------------------------------------------------------------------------------
103 
104 #endif // UI_FEATURE_ALLOCATOR
Provides some API to analyse the MicroUI image heap. The default allocator available in the Graphics ...
MicroEJ MicroUI library low level API: enable some features according to the hardware capabilities.