microui  4.1.0
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 // -----------------------------------------------------------------------------
21 // Includes
22 // -----------------------------------------------------------------------------
23 
24 #include "microui_heap.h"
25 #include "BESTFIT_ALLOCATOR.h"
26 
27 // --------------------------------------------------------------------------------
28 // Macros and Defines
29 // --------------------------------------------------------------------------------
30 
31 /*
32  * @brief The best fit allocator stores a main header of 68 bytes (0x44) in the heap.
33  */
34 #define BESTFITALLOCATOR_HEADER_SIZE (68)
35 
36 /*
37  * @brief The best fit allocator stores a header before an allocated block and a footer
38  * after. The header holds the block size plus the header and footer sizes. This macro
39  * retrieves the block full size (header + data + footer).
40  */
41 #define BESTFITALLOCATOR_BLOCK_SIZE(block) ((*(uint32_t *)((block) - sizeof(uint32_t))) & 0x7ffffff)
42 
43 // --------------------------------------------------------------------------------
44 // Private fields
45 // --------------------------------------------------------------------------------
46 
47 static BESTFIT_ALLOCATOR image_heap;
48 static uint32_t heap_size;
49 static uint32_t free_space;
50 static uint32_t allocated_blocks_number;
51 
52 // --------------------------------------------------------------------------------
53 // microui_heap.h functions
54 // --------------------------------------------------------------------------------
55 
56 uint32_t MICROUI_HEAP_total_space(void) {
57  return heap_size;
58 }
59 
60 uint32_t MICROUI_HEAP_free_space(void) {
61  return free_space;
62 }
63 
64 uint32_t MICROUI_HEAP_number_of_allocated_blocks(void) {
65  return allocated_blocks_number;
66 }
67 
68 // --------------------------------------------------------------------------------
69 // LLUI_DISPLAY_impl.h functions
70 // --------------------------------------------------------------------------------
71 
72 void LLUI_DISPLAY_IMPL_imageHeapInitialize(uint8_t *heap_start, uint8_t *heap_limit) {
73  heap_size = heap_limit - heap_start - BESTFITALLOCATOR_HEADER_SIZE;
74  free_space = heap_size;
75  BESTFIT_ALLOCATOR_new(&image_heap);
76  BESTFIT_ALLOCATOR_initialize(&image_heap, (int32_t)heap_start, (int32_t)heap_limit);
77 }
78 
79 uint8_t * LLUI_DISPLAY_IMPL_imageHeapAllocate(uint32_t size) {
80  uint8_t *addr = (uint8_t *)BESTFIT_ALLOCATOR_allocate(&image_heap, (int32_t)size);
81 
82  if ((uint8_t *)0 != addr) {
83  free_space -= BESTFITALLOCATOR_BLOCK_SIZE(addr);
84  allocated_blocks_number++;
85  }
86  return addr;
87 }
88 
89 void LLUI_DISPLAY_IMPL_imageHeapFree(uint8_t *block) {
90  free_space += BESTFITALLOCATOR_BLOCK_SIZE(block);
91  allocated_blocks_number--;
92  BESTFIT_ALLOCATOR_free(&image_heap, (void *)block);
93 }
94 
95 // --------------------------------------------------------------------------------
96 // EOF
97 // --------------------------------------------------------------------------------