1060ff233Sopenharmony_ci/*
2060ff233Sopenharmony_ci * Copyright (C) 2022 Huawei Device Co., Ltd.
3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License.
5060ff233Sopenharmony_ci * You may obtain a copy of the License at
6060ff233Sopenharmony_ci *
7060ff233Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8060ff233Sopenharmony_ci *
9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and
13060ff233Sopenharmony_ci * limitations under the License.
14060ff233Sopenharmony_ci */
15060ff233Sopenharmony_ci
16060ff233Sopenharmony_ci#ifndef FILLP_DYMPOOL_H
17060ff233Sopenharmony_ci#define FILLP_DYMPOOL_H
18060ff233Sopenharmony_ci
19060ff233Sopenharmony_ci#include "hlist.h"
20060ff233Sopenharmony_ci#include "queue.h"
21060ff233Sopenharmony_ci#include "spunge_mem.h"
22060ff233Sopenharmony_ci
23060ff233Sopenharmony_ci#ifdef __cplusplus
24060ff233Sopenharmony_ciextern "C" {
25060ff233Sopenharmony_ci#endif
26060ff233Sopenharmony_ci
27060ff233Sopenharmony_citypedef struct InnerdympMemoryT {
28060ff233Sopenharmony_ci    struct HlistNode hnode;
29060ff233Sopenharmony_ci    int itemCnt;
30060ff233Sopenharmony_ci} DympMemory;
31060ff233Sopenharmony_ci
32060ff233Sopenharmony_cistatic __inline DympMemory *DympMemoryNodeEntry(struct HlistNode *node)
33060ff233Sopenharmony_ci{
34060ff233Sopenharmony_ci    return (DympMemory *)((char *)(node) - (uintptr_t)(&(((DympMemory *)0)->hnode)));
35060ff233Sopenharmony_ci}
36060ff233Sopenharmony_ci
37060ff233Sopenharmony_citypedef struct DympItemTypeStruct {
38060ff233Sopenharmony_ci    FillpQueue *mp; /* Queue of memory, for free */
39060ff233Sopenharmony_ci} DympItemType;
40060ff233Sopenharmony_ci
41060ff233Sopenharmony_ci#define DYMP_ITEM_DATA(_item) ((void *)((char *)(_item) + sizeof(DympItemType)))
42060ff233Sopenharmony_ci#define DYMP_GET_ITEM_FROM_DATA(_data) ((void *)((char *)(_data) - sizeof(DympItemType)))
43060ff233Sopenharmony_ci
44060ff233Sopenharmony_ci
45060ff233Sopenharmony_citypedef FILLP_INT (*DympoolCreateCb)(DympItemType *item);
46060ff233Sopenharmony_citypedef void (*DympoolDestroyCb)(DympItemType *item);
47060ff233Sopenharmony_citypedef struct DympoolItemOperaCb {
48060ff233Sopenharmony_ci    DympoolCreateCb createCb;
49060ff233Sopenharmony_ci    DympoolDestroyCb destroyCb;
50060ff233Sopenharmony_ci} DympoolItemOperaCbSt;
51060ff233Sopenharmony_ci
52060ff233Sopenharmony_citypedef struct DympoolTypeStrunct {
53060ff233Sopenharmony_ci    FillpQueue *mp;        /* Queue of memory alloc */
54060ff233Sopenharmony_ci    int itemSize;          /* Size of every memory item size */
55060ff233Sopenharmony_ci    int maxSize;           /* Max memory item size,and althrough it is the max size of queue */
56060ff233Sopenharmony_ci    int currentSize;       /* Current size of memory alloced */
57060ff233Sopenharmony_ci    int initSize;          /* Initial size when do create */
58060ff233Sopenharmony_ci    FILLP_BOOL autoExpand; /* If auto expand if no item can be alloced */
59060ff233Sopenharmony_ci    struct Hlist mlist;    /* List of alloced memory */
60060ff233Sopenharmony_ci    DympoolItemOperaCbSt itemOperaCb; /* item creation and destroy callback structure */
61060ff233Sopenharmony_ci} DympoolType;
62060ff233Sopenharmony_ci
63060ff233Sopenharmony_ciDympoolType *DympCreatePool(int initSize, int maxSize, int itemSize, FILLP_BOOL autoExpand,
64060ff233Sopenharmony_ci                            DympoolItemOperaCbSt *itemOperaCb);
65060ff233Sopenharmony_ci
66060ff233Sopenharmony_ci
67060ff233Sopenharmony_civoid DympDestroyPool(DympoolType *pool);
68060ff233Sopenharmony_civoid DympSetConsSafe(DympoolType *pool, FILLP_BOOL safe);
69060ff233Sopenharmony_civoid DympSetProdSafe(DympoolType *pool, FILLP_BOOL safe);
70060ff233Sopenharmony_ciint DympAskMoreMemory(DympoolType *pool, int stepSize, int throttleGrow);
71060ff233Sopenharmony_ciint DympAlloc(DympoolType *pool, void **data, int throttleGrow);
72060ff233Sopenharmony_civoid DympFree(void *data);
73060ff233Sopenharmony_ci#define DYMP_GET_CUR_SIZE(_pool) (((DympoolType *)(_pool))->currentSize)
74060ff233Sopenharmony_ci
75060ff233Sopenharmony_ci
76060ff233Sopenharmony_ci#ifdef __cplusplus
77060ff233Sopenharmony_ci}
78060ff233Sopenharmony_ci#endif
79060ff233Sopenharmony_ci
80060ff233Sopenharmony_ci
81060ff233Sopenharmony_ci#endif /* FILLP_DYMPOOL_H */
82