microui  3.1.0
microui
LLUI_DISPLAY_HEAP_impl.c
Go to the documentation of this file.
1 /*
2  * C
3  *
4  * Copyright 2021-2023 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 
73 void LLUI_DISPLAY_IMPL_image_heap_initialize(uint8_t* heap_start, uint8_t* heap_limit) {
74  heap_size = heap_limit - heap_start - BESTFITALLOCATOR_HEADER_SIZE;
75  free_space = heap_size;
76  BESTFIT_ALLOCATOR_new(&image_heap);
77  BESTFIT_ALLOCATOR_initialize(&image_heap, (int32_t)heap_start, (int32_t)heap_limit);
78 }
79 
80 uint8_t* LLUI_DISPLAY_IMPL_image_heap_allocate(uint32_t size) {
81  uint8_t* addr = (uint8_t*)BESTFIT_ALLOCATOR_allocate(&image_heap, (int32_t)size);
82 
83  if ((uint8_t*)0 != addr) {
84  free_space -= BESTFITALLOCATOR_BLOCK_SIZE(addr);
85  allocated_blocks_number++;
86  }
87  return addr;
88 }
89 
90 void LLUI_DISPLAY_IMPL_image_heap_free(uint8_t* block) {
91  free_space += BESTFITALLOCATOR_BLOCK_SIZE(block);
92  allocated_blocks_number--;
93  BESTFIT_ALLOCATOR_free(&image_heap, (void*)block);
94 }
95 
96 // --------------------------------------------------------------------------------
97 // EOF
98 // --------------------------------------------------------------------------------
99