xref: /f-stack/app/micro_thread/mt_mbuf_pool.cpp (revision df6ad731)
1 
2 /**
3  * Tencent is pleased to support the open source community by making MSEC available.
4  *
5  * Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
6  *
7  * Licensed under the GNU General Public License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License. You may
9  * obtain a copy of the License at
10  *
11  *     https://opensource.org/licenses/GPL-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software distributed under the
14  * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
15  * either express or implied. See the License for the specific language governing permissions
16  * and limitations under the License.
17  */
18 
19 
20 /**
21  *  @file mt_mbuf_pool.cpp
22  *  @info ΢�߳���Ϣbuf�ع���ʵ��
23  *  @time 20130924
24  **/
25 
26 #include "micro_thread.h"
27 #include "mt_mbuf_pool.h"
28 
29 using namespace std;
30 using namespace NS_MICRO_THREAD;
31 
32 
33 /**
34  * @brief ���ӹ���ȫ�ַ��ʽӿ�
35  * @return ȫ�־��ָ��
36  */
37 MsgBuffPool* MsgBuffPool::_instance = NULL;
38 MsgBuffPool* MsgBuffPool::Instance (void)
39 {
40     if (NULL == _instance)
41     {
42         _instance = new MsgBuffPool;
43     }
44 
45     return _instance;
46 }
47 
48 /**
49  * @brief ���ӹ���ȫ�ֵ����ٽӿ�
50  */
51 void MsgBuffPool::Destroy()
52 {
53     if( _instance != NULL )
54     {
55         delete _instance;
56         _instance = NULL;
57     }
58 }
59 
60 
61 /**
62  * @brief ��Ϣbuff�Ĺ��캯��
63  */
64 MsgBuffPool::MsgBuffPool(int max_free)
65 {
66     _max_free = max_free;
67     _hash_map = new HashList(10000);
68 }
69 
70 /**
71  * @brief ��Ϣbuff����������
72  */
73 MsgBuffPool::~MsgBuffPool()
74 {
75     if (!_hash_map) {
76         return;
77     }
78 
79     MsgBufMap* msg_map = NULL;
80     HashKey* hash_item = _hash_map->HashGetFirst();
81     while (hash_item)
82     {
83         _hash_map->HashRemove(hash_item);
84         msg_map = dynamic_cast<MsgBufMap*>(hash_item);
85         delete msg_map;
86 
87         hash_item = _hash_map->HashGetFirst();
88     }
89 
90     delete _hash_map;
91     _hash_map = NULL;
92 }
93 
94 /**
95  *  @brief ��ȡ��ϢbuffԪ��
96  *  @return msgbufָ��, ʧ��ΪNULL
97  */
98 MtMsgBuf* MsgBuffPool::GetMsgBuf(int max_size)
99 {
100     if (!_hash_map) {
101         MTLOG_ERROR("MsgBuffPoll not init! hash %p,", _hash_map);
102         return NULL;
103     }
104 
105     MsgBufMap* msg_map = NULL;
106     MsgBufMap msg_key(max_size);
107     HashKey* hash_item = _hash_map->HashFind(&msg_key);
108     if (hash_item) {
109         msg_map = (MsgBufMap*)hash_item->GetDataPtr();
110         if (msg_map) {
111             return msg_map->GetMsgBuf();
112         } else {
113             MTLOG_ERROR("Hash item: %p, msg_map: %p impossible, clean it", hash_item, msg_map);
114             _hash_map->HashRemove(hash_item);
115             delete hash_item;
116             return NULL;
117         }
118     } else {
119         msg_map = new MsgBufMap(max_size, _max_free);
120         if (!msg_map) {
121             MTLOG_ERROR("maybe no more memory, failed. size: %d", max_size);
122             return NULL;
123         }
124         _hash_map->HashInsert(msg_map);
125 
126         return msg_map->GetMsgBuf();
127     }
128 }
129 
130 /**
131  *  @brief ��ȡ��ϢbuffԪ��
132  *  @return msgbufָ��, ʧ��ΪNULL
133  */
134 void MsgBuffPool::FreeMsgBuf(MtMsgBuf* msg_buf)
135 {
136     if (!_hash_map || !msg_buf) {
137         MTLOG_ERROR("MsgBuffPoll not init or input error! hash %p, msg_buf %p", _hash_map, msg_buf);
138         delete msg_buf;
139         return;
140     }
141     msg_buf->Reset();
142 
143     MsgBufMap* msg_map = NULL;
144     MsgBufMap msg_key(msg_buf->GetMaxLen());
145     HashKey* hash_item = _hash_map->HashFind(&msg_key);
146     if (hash_item) {
147         msg_map = (MsgBufMap*)hash_item->GetDataPtr();
148     }
149     if (!hash_item || !msg_map) {
150         MTLOG_ERROR("MsgBuffPoll find no queue, maybe error: %d", msg_buf->GetMaxLen());
151         delete msg_buf;
152         return;
153     }
154     msg_map->FreeMsgBuf(msg_buf);
155 
156     return;
157 }
158 
159 
160