microui  1.1.1
microui
LLUI_DISPLAY_HEAP_impl.c
Go to the documentation of this file.
1 /*
2  * C
3  *
4  * Copyright 2021-2022 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 
21 // -----------------------------------------------------------------------------
22 // Includes
23 // -----------------------------------------------------------------------------
24 
25 #include "microui_heap.h"
26 #include "BESTFIT_ALLOCATOR.h"
27 
28 #ifdef __cplusplus
29 extern "C" {
30 #endif
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 
78 void LLUI_DISPLAY_IMPL_image_heap_initialize(uint8_t* heap_start, uint8_t* heap_limit) {
79  heap_size = heap_limit - heap_start - BESTFITALLOCATOR_HEADER_SIZE;
80  free_space = heap_size;
81  BESTFIT_ALLOCATOR_new(&image_heap);
82  BESTFIT_ALLOCATOR_initialize(&image_heap, (int32_t)heap_start, (int32_t)heap_limit);
83 }
84 
85 uint8_t* LLUI_DISPLAY_IMPL_image_heap_allocate(uint32_t size) {
86  uint8_t* addr = (uint8_t*)BESTFIT_ALLOCATOR_allocate(&image_heap, (int32_t)size);
87 
88  if ((uint8_t*)0 != addr) {
89  free_space -= BESTFITALLOCATOR_BLOCK_SIZE(addr);
90  allocated_blocks_number++;
91  }
92  return addr;
93 }
94 
95 void LLUI_DISPLAY_IMPL_image_heap_free(uint8_t* block) {
96  free_space += BESTFITALLOCATOR_BLOCK_SIZE(block);
97  allocated_blocks_number--;
98  BESTFIT_ALLOCATOR_free(&image_heap, (void*)block);
99 }
100 
101 // --------------------------------------------------------------------------------
102 // EOF
103 // --------------------------------------------------------------------------------
104 
105 #ifdef __cplusplus
106 }
107 #endif