mirror of https://github.com/F-Stack/f-stack.git
305 lines
6.6 KiB
C
305 lines
6.6 KiB
C
|
|
|||
|
/**
|
|||
|
* Tencent is pleased to support the open source community by making MSEC available.
|
|||
|
*
|
|||
|
* Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
|
|||
|
*
|
|||
|
* Licensed under the GNU General Public License, Version 2.0 (the "License");
|
|||
|
* you may not use this file except in compliance with the License. You may
|
|||
|
* obtain a copy of the License at
|
|||
|
*
|
|||
|
* https://opensource.org/licenses/GPL-2.0
|
|||
|
*
|
|||
|
* Unless required by applicable law or agreed to in writing, software distributed under the
|
|||
|
* License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
|
|||
|
* either express or implied. See the License for the specific language governing permissions
|
|||
|
* and limitations under the License.
|
|||
|
*/
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @file mt_mbuf_pool.h
|
|||
|
* @info <EFBFBD>߳<EFBFBD>ͬ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϣbuf<EFBFBD><EFBFBD>
|
|||
|
**/
|
|||
|
|
|||
|
#ifndef __MT_MBUF_POOL_H__
|
|||
|
#define __MT_MBUF_POOL_H__
|
|||
|
|
|||
|
#include <netinet/in.h>
|
|||
|
#include <queue>
|
|||
|
#include "hash_list.h"
|
|||
|
|
|||
|
namespace NS_MICRO_THREAD {
|
|||
|
|
|||
|
using std::queue;
|
|||
|
|
|||
|
enum BUFF_TYPE
|
|||
|
{
|
|||
|
BUFF_UNDEF = 0, ///< δ<><CEB4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
BUFF_RECV = 1, ///< <20><><EFBFBD><EFBFBD>buff
|
|||
|
BUFF_SEND = 2, ///< <20><><EFBFBD><EFBFBD>buff
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>ϢͶ<EFBFBD>ݵ<EFBFBD>buffer<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
typedef TAILQ_ENTRY(MtMsgBuf) MsgBufLink;
|
|||
|
typedef TAILQ_HEAD(__MtbuffTailq, MtMsgBuf) MsgBufQueue;
|
|||
|
class MtMsgBuf
|
|||
|
{
|
|||
|
private:
|
|||
|
int _max_len; // <20><><EFBFBD><EFBFBD><EFBFBD>Ŀռ䳤<D5BC><E4B3A4>
|
|||
|
int _msg_len; // ʵ<>ʵ<EFBFBD><CAB5><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>
|
|||
|
int _buf_type; // buff<66>Ƿ<EFBFBD><C7B7>ͻ<EFBFBD><CDBB>ǽ<EFBFBD><C7BD><EFBFBD>
|
|||
|
int _recv_len; // <20>ѽ<EFBFBD><D1BD>յ<EFBFBD><D5B5><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>
|
|||
|
int _send_len; // <20>ѷ<EFBFBD><D1B7>͵<EFBFBD><CDB5><EFBFBD>Ϣ<EFBFBD><CFA2><EFBFBD><EFBFBD>
|
|||
|
void* _msg_buff; // buffer ʵ<><CAB5>ͷָ<CDB7><D6B8>
|
|||
|
|
|||
|
public:
|
|||
|
|
|||
|
MsgBufLink _entry;
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>캯<EFBFBD><EFBFBD>, ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
MtMsgBuf(int max_len) {
|
|||
|
_max_len = max_len;
|
|||
|
_msg_len = 0;
|
|||
|
_buf_type = BUFF_UNDEF;
|
|||
|
_recv_len = 0;
|
|||
|
_send_len = 0;
|
|||
|
_msg_buff = malloc(max_len);
|
|||
|
};
|
|||
|
|
|||
|
~MtMsgBuf() {
|
|||
|
if (_msg_buff) {
|
|||
|
free(_msg_buff);
|
|||
|
_msg_buff = NULL;
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
|
|||
|
*/
|
|||
|
void SetBuffType(BUFF_TYPE type) {
|
|||
|
_buf_type = (int)type;
|
|||
|
};
|
|||
|
BUFF_TYPE GetBuffType() {
|
|||
|
return (BUFF_TYPE)_buf_type;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD>ýӿ<EFBFBD>, <EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>ʼ״̬
|
|||
|
*/
|
|||
|
void Reset() {
|
|||
|
_msg_len = 0;
|
|||
|
_recv_len = 0;
|
|||
|
_send_len = 0;
|
|||
|
_buf_type = BUFF_UNDEF;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
|
|||
|
*/
|
|||
|
void SetMsgLen(int msg_len) {
|
|||
|
_msg_len = msg_len;
|
|||
|
};
|
|||
|
int GetMsgLen() {
|
|||
|
return _msg_len;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>bufferָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ
|
|||
|
*/
|
|||
|
int GetMaxLen() {
|
|||
|
return _max_len;
|
|||
|
};
|
|||
|
void* GetMsgBuff() {
|
|||
|
return _msg_buff;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>м<EFBFBD>״̬<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
int GetHaveSndLen() {
|
|||
|
return _send_len;
|
|||
|
};
|
|||
|
void SetHaveSndLen(int snd_len) {
|
|||
|
_send_len = snd_len;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>м<EFBFBD>״̬<EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
int GetHaveRcvLen() {
|
|||
|
return _recv_len;
|
|||
|
};
|
|||
|
void SetHaveRcvLen(int rcv_len) {
|
|||
|
_recv_len = rcv_len;
|
|||
|
};
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD>buffer, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳ<EFBFBD><EFBFBD><EFBFBD>ɿ<EFBFBD><EFBFBD>ж<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
class MsgBufMap : public HashKey
|
|||
|
{
|
|||
|
public:
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ϣbuff<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @param buff_size <EFBFBD><EFBFBD>mapԪ<EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff<EFBFBD>ռ<EFBFBD><EFBFBD><EFBFBD>Сֵ
|
|||
|
* @param max_free <EFBFBD>ö<EFBFBD><EFBFBD>й<EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD>free<EFBFBD><EFBFBD>Ŀ
|
|||
|
*/
|
|||
|
MsgBufMap(int buff_size, int max_free) {
|
|||
|
_max_buf_size = buff_size;
|
|||
|
_max_free = max_free;
|
|||
|
this->SetDataPtr(this);
|
|||
|
_queue_num = 0;
|
|||
|
TAILQ_INIT(&_msg_queue);
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ϣbuff<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĺ<EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD>, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>key<EFBFBD><EFBFBD>Ϣ
|
|||
|
* @param buff_size <EFBFBD><EFBFBD>mapԪ<EFBFBD><EFBFBD><EFBFBD>ϵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff, <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buff<EFBFBD>ռ<EFBFBD><EFBFBD><EFBFBD>Сֵ
|
|||
|
*/
|
|||
|
explicit MsgBufMap(int buff_size) {
|
|||
|
_max_buf_size = buff_size;
|
|||
|
TAILQ_INIT(&_msg_queue);
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ϣbuff<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
~MsgBufMap() {
|
|||
|
MtMsgBuf* ptr = NULL;
|
|||
|
MtMsgBuf* tmp = NULL;
|
|||
|
TAILQ_FOREACH_SAFE(ptr, &_msg_queue, _entry, tmp)
|
|||
|
{
|
|||
|
TAILQ_REMOVE(&_msg_queue, ptr, _entry);
|
|||
|
delete ptr;
|
|||
|
_queue_num--;
|
|||
|
}
|
|||
|
|
|||
|
TAILQ_INIT(&_msg_queue);
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ϢbuffԪ<EFBFBD><EFBFBD>
|
|||
|
* @return msgbufָ<EFBFBD><EFBFBD>, ʧ<EFBFBD><EFBFBD>ΪNULL
|
|||
|
*/
|
|||
|
MtMsgBuf* GetMsgBuf(){
|
|||
|
MtMsgBuf* ptr = NULL;
|
|||
|
if (!TAILQ_EMPTY(&_msg_queue)) {
|
|||
|
ptr = TAILQ_FIRST(&_msg_queue);
|
|||
|
TAILQ_REMOVE(&_msg_queue, ptr, _entry);
|
|||
|
_queue_num--;
|
|||
|
} else {
|
|||
|
ptr = new MtMsgBuf(_max_buf_size);
|
|||
|
}
|
|||
|
|
|||
|
return ptr;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ͷ<EFBFBD><EFBFBD><EFBFBD>ϢbuffԪ<EFBFBD><EFBFBD>
|
|||
|
* @param msgbufָ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void FreeMsgBuf(MtMsgBuf* ptr){
|
|||
|
if (_queue_num >= _max_free) {
|
|||
|
delete ptr;
|
|||
|
} else {
|
|||
|
ptr->Reset();
|
|||
|
TAILQ_INSERT_TAIL(&_msg_queue, ptr, _entry);
|
|||
|
_queue_num++;
|
|||
|
}
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ڵ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>hash<EFBFBD>㷨, <EFBFBD><EFBFBD>ȡkey<EFBFBD><EFBFBD>hashֵ
|
|||
|
* @return <EFBFBD>ڵ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>hashֵ
|
|||
|
*/
|
|||
|
virtual uint32_t HashValue(){
|
|||
|
return _max_buf_size;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ڵ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>cmp<EFBFBD><EFBFBD><EFBFBD><EFBFBD>, ͬһͰID<EFBFBD><EFBFBD>, <EFBFBD><EFBFBD>key<EFBFBD>Ƚ<EFBFBD>
|
|||
|
* @return <EFBFBD>ڵ<EFBFBD>Ԫ<EFBFBD>ص<EFBFBD>hashֵ
|
|||
|
*/
|
|||
|
virtual int HashCmp(HashKey* rhs){
|
|||
|
return this->_max_buf_size - (int)rhs->HashValue();
|
|||
|
};
|
|||
|
|
|||
|
private:
|
|||
|
int _max_free; ///< <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><D0B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
int _max_buf_size; ///< <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buffsize
|
|||
|
int _queue_num; ///< <20><><EFBFBD>ж<EFBFBD><D0B6>и<EFBFBD><D0B8><EFBFBD>
|
|||
|
MsgBufQueue _msg_queue; ///< ʵ<>ʵĿ<CAB5><C4BF>ж<EFBFBD><D0B6><EFBFBD>
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
/**
|
|||
|
* @brief ȫ<EFBFBD>ֵ<EFBFBD>buffer<EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD>, ͳһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>buffer
|
|||
|
*/
|
|||
|
class MsgBuffPool
|
|||
|
{
|
|||
|
public:
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ϣbuff<EFBFBD><EFBFBD>ȫ<EFBFBD>ֹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
|
|||
|
* @return ȫ<EFBFBD>־<EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
static MsgBuffPool* Instance (void);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ϣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӿ<EFBFBD>
|
|||
|
*/
|
|||
|
static void Destroy(void);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ϣbuff<EFBFBD><EFBFBD>ȫ<EFBFBD>ֹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD>и<EFBFBD><EFBFBD><EFBFBD>
|
|||
|
* @param max_free <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>б<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ, <EFBFBD><EFBFBD>Ҫ<EFBFBD>ڷ<EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>ǰ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void SetMaxFreeNum(int max_free) {
|
|||
|
_max_free = max_free;
|
|||
|
};
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>ȡ<EFBFBD><EFBFBD>ϢbuffԪ<EFBFBD><EFBFBD>
|
|||
|
* @return msgbufָ<EFBFBD><EFBFBD>, ʧ<EFBFBD><EFBFBD>ΪNULL
|
|||
|
*/
|
|||
|
MtMsgBuf* GetMsgBuf(int max_size);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD>ͷ<EFBFBD><EFBFBD><EFBFBD>ϢbuffԪ<EFBFBD><EFBFBD>
|
|||
|
* @param msgbufָ<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
void FreeMsgBuf(MtMsgBuf* msg_buf);
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ϣbuff<EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
~MsgBuffPool();
|
|||
|
|
|||
|
private:
|
|||
|
|
|||
|
/**
|
|||
|
* @brief <EFBFBD><EFBFBD>Ϣbuff<EFBFBD>Ĺ<EFBFBD><EFBFBD>캯<EFBFBD><EFBFBD>
|
|||
|
*/
|
|||
|
explicit MsgBuffPool(int max_free = 300);
|
|||
|
|
|||
|
static MsgBuffPool * _instance; ///< <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
int _max_free; ///< <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ŀ
|
|||
|
HashList* _hash_map; ///< <20><>size hashmap <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD><D0B6><EFBFBD>
|
|||
|
|
|||
|
};
|
|||
|
|
|||
|
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
#endif
|
|||
|
|
|||
|
|