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