1 /* 2 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef UI_FONT_ALLOCATOR_H 17 #define UI_FONT_ALLOCATOR_H 18 19 #include <stdint.h> 20 21 namespace OHOS { 22 class UIFontAllocator { 23 static constexpr uint8_t UI_FONT_MEM_ALIGNMENT = 4; 24 25 struct Chunk { 26 uint32_t next; 27 uint32_t prev; 28 bool used; 29 }; 30 31 public: 32 UIFontAllocator(); 33 34 ~UIFontAllocator(); 35 36 void SetRamAddr(uint8_t* ram, uint32_t size); 37 38 void SetMinChunkSize(uint32_t size); 39 40 uint32_t GetSize(void *addr); 41 42 /** 43 * @brief allocate memory 44 * 45 * @param size 46 * @return memory address 47 */ 48 void *Allocate(uint32_t size); 49 50 void Free(void* addr); 51 52 private: AlignSize(uint32_t size)53 uint32_t AlignSize(uint32_t size) 54 { 55 return (size + UI_FONT_MEM_ALIGNMENT - 1U) & ~(UI_FONT_MEM_ALIGNMENT - 1U); 56 } 57 void CombineFree(Chunk* cache); 58 59 uint8_t* ram_; 60 uint32_t ramSize_; 61 uint32_t freeSize_; 62 uint32_t minSize_; 63 struct Chunk* end_; 64 struct Chunk* free_; 65 }; 66 } // namespace OHOS 67 #endif /* UI_FONT_ALLOCATOR_H */ 68