From 364279f860e228d626b85423591f06468c1a5d2b Mon Sep 17 00:00:00 2001 From: ubuntu Date: Sat, 29 Feb 2020 23:06:56 +0800 Subject: [PATCH] make some small fixed and using preclare --- Channel.cpp | 1 + Channel.h | 1 - Epoll.cpp | 1 + Epoll.h | 4 +- EventLoop.cpp | 1 + EventLoop.h | 2 +- Http_conn.h | 1 + LFUCache.cc | 153 ++++++++++++++++++++++++++++++++++++++++++++++ LFUCache.h | 79 ++++++++++++++++++++++++ MemoryPool.cpp | 1 + MemoryPool.h | 5 +- Server.cpp | 4 ++ Server.h | 21 ++++--- Thread.cpp | 4 ++ Thread.h | 2 - ThreadEventLoop.h | 1 + 16 files changed, 267 insertions(+), 14 deletions(-) create mode 100644 Http_conn.h create mode 100644 LFUCache.cc create mode 100644 LFUCache.h diff --git a/Channel.cpp b/Channel.cpp index d4b71c5..9d90431 100644 --- a/Channel.cpp +++ b/Channel.cpp @@ -1,6 +1,7 @@ #include "Channel.h" #include "EventLoop.h" #include "Logging.h" +#include "MemoryPool.h" typedef std::shared_ptr SP_Channel; diff --git a/Channel.h b/Channel.h index 13a8e65..ba61425 100644 --- a/Channel.h +++ b/Channel.h @@ -3,7 +3,6 @@ #include #include #include -#include "MemoryPool.h" class EventLoop; typedef std::shared_ptr SP_EventLoop; diff --git a/Epoll.cpp b/Epoll.cpp index ab94ad0..1725582 100644 --- a/Epoll.cpp +++ b/Epoll.cpp @@ -1,6 +1,7 @@ #include "Channel.h" #include "Http_conn.h" #include "Epoll.h" +#include "MemoryPool.h" typedef std::shared_ptr SP_Epoll; diff --git a/Epoll.h b/Epoll.h index e066036..ec79366 100644 --- a/Epoll.h +++ b/Epoll.h @@ -4,7 +4,6 @@ #include #include #include -#include "MemoryPool.h" #define MAXFDS 10000 #define EVENTS 4096 @@ -12,9 +11,10 @@ class Channel; class Http_conn; +typedef std::shared_ptr SP_Channel; std::vector events; -std::unordered_mapChannelmap; +std::unordered_map Channelmap; class Epoll{ public: diff --git a/EventLoop.cpp b/EventLoop.cpp index a5a3c3c..1f0f706 100644 --- a/EventLoop.cpp +++ b/EventLoop.cpp @@ -1,6 +1,7 @@ #include "EventLoop.h" #include "Epoll.h" #include "TimeManger.h" +#include "MutexLock.h" bool EventLoop::quit = false; diff --git a/EventLoop.h b/EventLoop.h index aecef3d..64a77d5 100644 --- a/EventLoop.h +++ b/EventLoop.h @@ -3,8 +3,8 @@ #include #include #include -#include "MutexLock.h" +class MutexLock; class Channel; class Epoll; class TimerManager; diff --git a/Http_conn.h b/Http_conn.h new file mode 100644 index 0000000..6f70f09 --- /dev/null +++ b/Http_conn.h @@ -0,0 +1 @@ +#pragma once diff --git a/LFUCache.cc b/LFUCache.cc new file mode 100644 index 0000000..8e12035 --- /dev/null +++ b/LFUCache.cc @@ -0,0 +1,153 @@ +#include "LFUCache.h" +#include "MutexLock.h" +#include "Conf.h" +#include "MemoryPool.h" + +void KeyList::init(int fq) +{ + freq=fq; + //head=last=new Node; + head=last=newElement>(); + head->setNext(NULL); +} + +void KeyList::destory(){ + while(head){ + key_node pre=head; + head=head->getNext(); + //delete pre; + deleteElement(pre); + } +} + +int KeyList::getFreq(){ + return freq; +} + +void KeyList::add(key_node &node){ + if(head->getNext()){ + head->getNext()->setPre(node); + //printf("is not one\n"); + } + else + last=node; + node->setNext(head->getNext()); + node->setPre(head); + head->setNext(node); + //printf("last key=%d\n",last->getValue().key); +} + +void KeyList::del(key_node &node){ + node->getPre()->setNext(node->getNext()); + if(node->getNext()) + node->getNext()->setPre(node->getPre()); + else + last=node->getPre(); +} + +bool KeyList::isEmpty(){ + return head==last; +} + +key_node KeyList::getLast(){ + return last; +} + +void LFUCache::init(){ + capacity=getconf().getcapacity(); + //head=new Node; + head=newElement>(); + head->getValue().init(0); + head->setNext(NULL); +} + +LFUCache::~LFUCache(){ + while(head){ + freq_node pre=head; + head=head->getNext(); + pre->getValue().destory(); + //delete pre; + deleteElement(pre); + } +} + +void LFUCache::addfreq(key_node &nowk,freq_node &nowf){ + freq_node nxt; + if(!nowf->getNext()||nowf->getNext()->getValue().getFreq()!=nowf->getValue().getFreq()+1){ + //插入freqnode + //printf("new freqnode!\n"); + //nxt=new Node; + nxt=newElement>(); + nxt->getValue().init(nowf->getValue().getFreq()+1); + if(nowf->getNext()) + nowf->getNext()->setPre(nxt); + nxt->setNext(nowf->getNext()); + nowf->setNext(nxt); + nxt->setPre(nowf); + } + else + nxt=nowf->getNext(); + fmap[nowk->getValue().key]=nxt; + //移动keynode + if(nowf!=head){ + nowf->getValue().del(nowk); + //printf("nowf is not head!\n"); + } + nxt->getValue().add(nowk); + if(nowf!=head&&nowf->getValue().isEmpty()) + del(nowf); +} + +bool LFUCache::get(string &key,string &v){ + if(!capacity) + return false; + MutexLockGuard lock(mutex); + if(fmap.find(key)!=fmap.end()){ + //命中 + key_node nowk=kmap[key]; + freq_node nowf=fmap[key]; + v+=nowk->getValue().value; + addfreq(nowk,nowf); + return true; + } + return false; +} + +void LFUCache::set(string &key,string &v){ + if(!capacity) + return; + //printf("kmapsize=%d capacity=%d\n",kmap.size(),capacity); + MutexLockGuard lock(mutex); + if(kmap.size()==capacity){ + freq_node headnxt=head->getNext(); + key_node last=headnxt->getValue().getLast(); + headnxt->getValue().del(last); + //printf("key=%d\n",last->getValue().key); + kmap.erase(last->getValue().key); + fmap.erase(last->getValue().key); + //delete last; + deleteElement(last); + if(headnxt->getValue().isEmpty()) + del(headnxt); + } + //key_node nowk=new Node; + key_node nowk=newElement>(); + nowk->getValue().key=key; + nowk->getValue().value=v; + addfreq(nowk,head); + kmap[key]=nowk; +} + +void LFUCache::del(freq_node &node){ + node->getPre()->setNext(node->getNext()); + if(node->getNext()) + node->getNext()->setPre(node->getPre()); + node->getValue().destory(); + //delete node; + deleteElement(node); +} + +LFUCache& getCache(){ + static LFUCache cache; + return cache; +} diff --git a/LFUCache.h b/LFUCache.h new file mode 100644 index 0000000..b4910cd --- /dev/null +++ b/LFUCache.h @@ -0,0 +1,79 @@ +#pragma once + +#include +#include + +using std::string; + +class MutexLock; + +template +class Node{ +private: + T value; + Node* pre; + Node* next; + +public: + void setPre(Node *p){ + pre=p; + } + void setNext(Node *n){ + next=n; + } + Node* getPre(){ + return pre; + } + Node* getNext(){ + return next; + } + T& getValue(){ + return value; + } + +}; + +struct Key{ + string key,value; +}; + +typedef Node* key_node; + +class KeyList{ +private: + int freq; + key_node head; + key_node last; + +public: + void init(int fq); + void destory(); + int getFreq(); + void add(key_node &node); + void del(key_node &node); + bool isEmpty(); + key_node getLast(); +}; + +typedef Node* freq_node; + +class LFUCache{ +private: + freq_node head; + int capacity; + MutexLock mutex; + + std::unordered_map kmap; //key到keynode的映射 + std::unordered_map fmap; //key到freqnode的映射 + + void addfreq(key_node &nowk,freq_node &nowf); + void del(freq_node &node); + +public: + void init(); + ~LFUCache(); + bool get(string &key,string &value); //通过key返回value并进行LFU操作 + void set(string &key,string &value); //更新LFU缓存 +}; + +LFUCache& getCache(); diff --git a/MemoryPool.cpp b/MemoryPool.cpp index 04afb3c..4af3db6 100644 --- a/MemoryPool.cpp +++ b/MemoryPool.cpp @@ -1,6 +1,7 @@ #include "MemoryPool.h" #include "Epoll.h" #include "TimerManager.h" +#include "MutexLock.h" MemoryPool::MemoryPool(){ diff --git a/MemoryPool.h b/MemoryPool.h index edce7ef..f49260c 100644 --- a/MemoryPool.h +++ b/MemoryPool.h @@ -6,11 +6,12 @@ #include #include #include -#include "MutexLock.h" #include #define BlockSize 4096 +class MutexLock; + /* * used to allocate memory */ @@ -18,6 +19,8 @@ struct Slot{//signal slot Slot* next; }; + + class MemoryPool{ public: MemoryPool(); diff --git a/Server.cpp b/Server.cpp index 4677de2..25690e3 100644 --- a/Server.cpp +++ b/Server.cpp @@ -1,4 +1,8 @@ #include "Server.h" +#include "Http_conn.h" +#include "EventLoop.h" +#include "ThreadpoolEventLoop.h" +#include "Logging.h" Server::Server(const char* port, int threadnum) : loop(newElement(), deleteElement), diff --git a/Server.h b/Server.h index c9a7cdc..e1e1b55 100644 --- a/Server.h +++ b/Server.h @@ -1,15 +1,22 @@ #pragma once #include "Packet.h" -#include "Http_conn.h" -#include "EventLoop.h" -#include "ThreadpoolEventLoop.h" -#include "Logging.h" +#include +#include -class Http_conn{ +class Http_conn; +class EventLoop; +class Channel; +class ThreadpoolEventLoop; +typedef std::shared_ptr SP_Http_conn; +typedef std::shared_ptr SP_EventLoop; +typedef std::shared_ptr SP_Channel; +typedef std::unique_ptr UP_ThreadpoolEventLoop; + +class Server{ public: - Http_conn(SP_Channel channel); - ~Http_conn(); + Server(SP_Channel channel); + ~Server(); void start(); private: diff --git a/Thread.cpp b/Thread.cpp index 1822737..7bac942 100644 --- a/Thread.cpp +++ b/Thread.cpp @@ -1,5 +1,9 @@ #include "Thread.h" +typedef std::shared_ptr SP_Thread; +typedef std::unique_ptr)*> UP_Thread; + + Thread::Thread(const ThreadFunc&& Func, const std::string& n) : started_(false), joined_(false), pthreadId(0), func(Func), name_(n) diff --git a/Thread.h b/Thread.h index f60410b..f0406b0 100644 --- a/Thread.h +++ b/Thread.h @@ -42,5 +42,3 @@ class Thread:noncopyable{ std::string name_; }; -typedef std::shared_ptr SP_Thread; -typedef std::unique_ptr)*> UP_Thread; diff --git a/ThreadEventLoop.h b/ThreadEventLoop.h index e69de29..6f70f09 100644 --- a/ThreadEventLoop.h +++ b/ThreadEventLoop.h @@ -0,0 +1 @@ +#pragma once