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_DYMPOOL_H
17 #define FILLP_DYMPOOL_H
18
19 #include "hlist.h"
20 #include "queue.h"
21
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25
26 typedef struct InnerdympMemoryT {
27 struct HlistNode hnode;
28 int itemCnt;
29 } DympMemory;
30
DympMemoryNodeEntry(struct HlistNode * node)31 static __inline DympMemory *DympMemoryNodeEntry(struct HlistNode *node)
32 {
33 return (DympMemory *)((char *)(node) - (uintptr_t)(&(((DympMemory *)0)->hnode)));
34 }
35
36 typedef struct DympItemTypeStruct {
37 FillpQueue *mp; /* Queue of memory, for free */
38 } DympItemType;
39
40 #define DYMP_ITEM_DATA(_item) ((void *)((char *)(_item) + sizeof(DympItemType)))
41 #define DYMP_GET_ITEM_FROM_DATA(_data) ((void *)((char *)(_data) - sizeof(DympItemType)))
42
43
44 typedef FILLP_INT (*DympoolCreateCb)(DympItemType *item);
45 typedef void (*DympoolDestroyCb)(DympItemType *item);
46 typedef struct DympoolItemOperaCb {
47 DympoolCreateCb createCb;
48 DympoolDestroyCb destroyCb;
49 } DympoolItemOperaCbSt;
50
51 typedef struct DympoolTypeStrunct {
52 FillpQueue *mp; /* Queue of memory alloc */
53 int itemSize; /* Size of every memory item size */
54 int maxSize; /* Max memory item size,and althrough it is the max size of queue */
55 int currentSize; /* Current size of memory alloced */
56 int initSize; /* Initial size when do create */
57 FILLP_BOOL autoExpand; /* If auto expand if no item can be alloced */
58 struct Hlist mlist; /* List of alloced memory */
59 DympoolItemOperaCbSt itemOperaCb; /* item creation and destroy callback structure */
60 } DympoolType;
61
62 DympoolType *DympCreatePool(int initSize, int maxSize, int itemSize, FILLP_BOOL autoExpand,
63 DympoolItemOperaCbSt *itemOperaCb);
64
65
66 void DympDestroyPool(DympoolType *pool);
67 void DympSetConsSafe(DympoolType *pool, FILLP_BOOL safe);
68 void DympSetProdSafe(DympoolType *pool, FILLP_BOOL safe);
69 int DympAskMoreMemory(DympoolType *pool, int stepSize, int throttleGrow);
70 int DympAlloc(DympoolType *pool, void **data, int throttleGrow);
71 void DympFree(void *data);
72 #define DYMP_GET_CUR_SIZE(_pool) (((DympoolType *)(_pool))->currentSize)
73
74
75 #ifdef __cplusplus
76 }
77 #endif
78
79
80 #endif /* FILLP_DYMPOOL_H */
81