1 /*
2  * Copyright (c) 2024 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 #ifndef DFX_ALLOCATOR_H
16 #define DFX_ALLOCATOR_H
17 
18 #include <sys/types.h>
19 
20 #define DFX_MEMPOOLS_NUM   7
21 #define DFX_MEMPOOL_TAG_SIZE 4
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 typedef struct DfxMempoolSt DfxMempool;
28 
29 typedef struct DfxBlockInfo {
30     struct DfxBlockInfo* next;
31     size_t freeBlocksCnt;
32 } BlockInfo;
33 
34 typedef struct DfxPageTag {
35     char tagInfo[DFX_MEMPOOL_TAG_SIZE];
36     uint32_t type;
37     union {
38         size_t mMapAllocSize;
39         DfxMempool* mempool;
40     };
41 } PageTag;
42 
43 typedef struct DfxPageInfo {
44     PageTag tag;
45     struct DfxPageInfo* prev;
46     struct DfxPageInfo* next;
47     BlockInfo* freeBlockList;
48     size_t freeBlocksCnt;
49 } PageInfo;
50 
51 struct DfxMempoolSt {
52     uint32_t type;
53     size_t blockSize;
54     size_t blocksPerPage;
55     size_t freePagesCnt;
56     PageInfo* pageList;
57 };
58 
59 typedef struct DfxAllocatorSt {
60     int initFlag;
61     PageInfo* pageList;
62     DfxMempool dfxMempoolBuf[DFX_MEMPOOLS_NUM];
63 } DfxAllocator;
64 
65 /**
66  * @brief Get allocator
67  *   an independent memory allocator
68  *   to perform memory operations
69  * @return DfxAllocator addr of allcator for get some info
70 */
71 DfxAllocator* GetDfxAllocator(void);
72 
73 /**
74  * @brief Change use custom allocator
75  *   an independent memory allocator
76  *   to perform memory operations
77 */
78 void RegisterAllocator(void);
79 
80 /**
81  * @brief restore default allocator
82  *   an independent memory allocator
83  *   to perform memory operations
84 */
85 void UnregisterAllocator(void);
86 
87 /**
88  * @brief IsDfxAllocatorMem
89  *   an independent memory allocator
90  *   to perform memory operations
91  * @return 1 : addr belong DfxMem, 0 :not
92 */
93 int IsDfxAllocatorMem(void* ptr);
94 
95 #ifdef __cplusplus
96 }
97 #endif
98 #endif
99