Skip to content

Commit

Permalink
make some small fixed and using preclare
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuRicky committed Feb 29, 2020
1 parent 5740c36 commit 364279f
Show file tree
Hide file tree
Showing 16 changed files with 267 additions and 14 deletions.
1 change: 1 addition & 0 deletions Channel.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Channel.h"
#include "EventLoop.h"
#include "Logging.h"
#include "MemoryPool.h"

typedef std::shared_ptr<Channel> SP_Channel;

Expand Down
1 change: 0 additions & 1 deletion Channel.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <functional>
#include <memory>
#include <sys/epoll.h>
#include "MemoryPool.h"

class EventLoop;
typedef std::shared_ptr<EventLoop> SP_EventLoop;
Expand Down
1 change: 1 addition & 0 deletions Epoll.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "Channel.h"
#include "Http_conn.h"
#include "Epoll.h"
#include "MemoryPool.h"

typedef std::shared_ptr<Epoll> SP_Epoll;

Expand Down
4 changes: 2 additions & 2 deletions Epoll.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
#include <unordered_map>
#include <vector>
#include <memory>
#include "MemoryPool.h"

#define MAXFDS 10000
#define EVENTS 4096
#define EPOLLWAIT_TIME -1

class Channel;
class Http_conn;
typedef std::shared_ptr<Channel> SP_Channel;

std::vector<SE> events;
std::unordered_map<int,SP_Channel>Channelmap;
std::unordered_map<int,SP_Channel> Channelmap;

class Epoll{
public:
Expand Down
1 change: 1 addition & 0 deletions EventLoop.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "EventLoop.h"
#include "Epoll.h"
#include "TimeManger.h"
#include "MutexLock.h"

bool EventLoop::quit = false;

Expand Down
2 changes: 1 addition & 1 deletion EventLoop.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <queue>
#include <memory>
#include <functional>
#include "MutexLock.h"

class MutexLock;
class Channel;
class Epoll;
class TimerManager;
Expand Down
1 change: 1 addition & 0 deletions Http_conn.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#pragma once
153 changes: 153 additions & 0 deletions LFUCache.cc
Original file line number Diff line number Diff line change
@@ -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<Key>;
head=last=newElement<Node<Key>>();
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<KeyList>;
head=newElement<Node<KeyList>>();
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<KeyList>;
nxt=newElement<Node<KeyList>>();
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>;
key_node nowk=newElement<Node<Key>>();
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;
}
79 changes: 79 additions & 0 deletions LFUCache.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#pragma once

#include <unordered_map>
#include <string>

using std::string;

class MutexLock;

template<typename T>
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>* 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<KeyList>* freq_node;

class LFUCache{
private:
freq_node head;
int capacity;
MutexLock mutex;

std::unordered_map<string,key_node> kmap; //key到keynode的映射
std::unordered_map<string,freq_node> 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();
1 change: 1 addition & 0 deletions MemoryPool.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "MemoryPool.h"
#include "Epoll.h"
#include "TimerManager.h"
#include "MutexLock.h"

MemoryPool::MemoryPool(){

Expand Down
5 changes: 4 additions & 1 deletion MemoryPool.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,21 @@
#include <stdint.h>
#include <stdarg.h>
#include <utility>
#include "MutexLock.h"
#include <functional>

#define BlockSize 4096

class MutexLock;

/*
* used to allocate memory
*/
struct Slot{//signal slot
Slot* next;
};



class MemoryPool{
public:
MemoryPool();
Expand Down
4 changes: 4 additions & 0 deletions Server.cpp
Original file line number Diff line number Diff line change
@@ -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<EventLoop>(), deleteElement<EventLoop>),
Expand Down
21 changes: 14 additions & 7 deletions Server.h
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
#pragma once

#include "Packet.h"
#include "Http_conn.h"
#include "EventLoop.h"
#include "ThreadpoolEventLoop.h"
#include "Logging.h"
#include <unordered_map>
#include <memory>

class Http_conn{
class Http_conn;
class EventLoop;
class Channel;
class ThreadpoolEventLoop;
typedef std::shared_ptr<Http_conn> SP_Http_conn;
typedef std::shared_ptr<EventLoop> SP_EventLoop;
typedef std::shared_ptr<Channel> SP_Channel;
typedef std::unique_ptr<ThreadpoolEventLoop> UP_ThreadpoolEventLoop;

class Server{
public:
Http_conn(SP_Channel channel);
~Http_conn();
Server(SP_Channel channel);
~Server();
void start();

private:
Expand Down
4 changes: 4 additions & 0 deletions Thread.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
#include "Thread.h"

typedef std::shared_ptr<Thread> SP_Thread;
typedef std::unique_ptr<Thread, decltype(deleteElement<Thread>)*> UP_Thread;


Thread::Thread(const ThreadFunc&& Func, const std::string& n)
: started_(false), joined_(false),
pthreadId(0), func(Func), name_(n)
Expand Down
2 changes: 0 additions & 2 deletions Thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,3 @@ class Thread:noncopyable{
std::string name_;
};

typedef std::shared_ptr<Thread> SP_Thread;
typedef std::unique_ptr<Thread, decltype(deleteElement<Thread>)*> UP_Thread;
1 change: 1 addition & 0 deletions ThreadEventLoop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
#pragma once

0 comments on commit 364279f

Please sign in to comment.