1 /*
2 * Copyright (C) 2022 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 FILLP_MEM_POOL_H
17 #define FILLP_MEM_POOL_H
18
19 #include "lf_ring.h"
20 #include "queue.h"
21 #include "log.h"
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 struct MemPool {
27 FILLP_UINT32 size; /* Total size */
28 FILLP_INT allocType;
29 void *allocArg;
30 void *memStart; /* Memory start */
31 FillpQueue q;
32 };
33
34 struct MemPoolItem {
35 void *p; /* data */
36 struct MemPool *pool;
37 };
38
MpItemEntry(void * ptr)39 static __inline struct MemPoolItem *MpItemEntry(void *ptr)
40 {
41 return (struct MemPoolItem *)((char *)(ptr) - sizeof(struct MemPoolItem));
42 };
43
44 /*******************************************************************************
45 Function : MpCalMemSize
46
47 Description : This function will be invoked to calculate the memory size
48 required for memory pool, based on size and num.
49
50 Input : itemSize - Size of item
51 itemNum - item number
52
53 Output : None
54
55 Return : Memory size of memory pool.
56 *******************************************************************************/
57 size_t MpCalMemSize(size_t itemSize, size_t itemNum);
58
59 /*******************************************************************************
60 Function : MpDestroyMemPool
61
62 Description : This function will be invoked to destroy the memory pool
63 created using MpCreateMemPool.
64
65 Input : pool - memory pool handle
66
67 Output : None
68
69 Return : None
70 *******************************************************************************/
71 void MpDestroyMemPool(struct MemPool *pool);
72
73 /*******************************************************************************
74 Function : MpCreateMemPool
75
76 Description : This function will be invoked to create memory pool, based on
77 size and num.
78
79 Input : name - memory pool name
80 itemSize - Size of item
81 itemNum - item number
82 allocType - type of alloc
83 allocArg - memory zone
84
85 Output : None
86
87 Return : Memory pool handle.
88 *******************************************************************************/
89 struct MemPool *MpCreateMemPool(FILLP_CHAR *name, FILLP_SIZE_T itemSize, FILLP_SIZE_T itemNum, FILLP_INT allocType);
90
91 void MpSetConsSafe(struct MemPool *mp, FILLP_BOOL consSafe);
92 void MpSetProdSafe(struct MemPool *mp, FILLP_BOOL prodSafe);
93
94 FillpErrorType MpMallocWait(struct MemPool *mp, void **pp);
95 void MpFreeWithPool(void *data, struct MemPool *pool);
96
97 #define MP_MALLOC(mp, pp) MpMallocWait((mp), (void **)(pp))
98
99 #ifdef __cplusplus
100 }
101 #endif
102
103 #endif /* FILLP_MEM_POOL_H */
104