1 /*
2  * Copyright (c) 2020-2021 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 MMESSAGE_POOL_H
17 #define MMESSAGE_POOL_H
18 #include <list>
19 #include <string>
20 #include <pthread.h>
21 #include "fsm_message.h"
22 
23 namespace OHOS {
24 struct MsgPrivateInfo {
25     bool isReplied;
26     MsgInfo *replyMsg;
27     bool isCondErr;
28 };
29 
30 class MMessagePool {
31 public:
32     explicit MMessagePool(std::string name);
33     ~MMessagePool();
34     int32_t Init(uint32_t msgNum, uint32_t msgPayloadLen);
35     MsgInfo *GetMsg(const MsgInfo &msg);
36     int32_t PutMsg(MsgInfo &msg);
37     MsgInfo *GetEmptyMsg();
38     void Dump(void);
39 
40 private:
41     int32_t Deinit(void);
42     int32_t MallocMemPool(uint32_t msgNum, uint32_t msgPayloadLen);
43     void FreeMemPool(void);
44 
45 private:
46     std::list<MsgInfo *> m_msgFreeList;
47     std::list<MsgInfo *> m_msgBusyList;
48     uint32_t m_maxMsgNum = 0;
49     uint32_t m_maxMsgPayloadLen = 0;
50     std::string m_name;
51     MsgInfo *m_msgMemList = nullptr;
52     MsgPrivateInfo *m_msgPrivList = nullptr;
53     uint8_t *m_msgPayloadList = nullptr;
54     pthread_mutex_t m_listLock = PTHREAD_MUTEX_INITIALIZER;
55     bool m_isInit = false;
56 };
57 }
58 #endif
59