189 lines
3.5 KiB
C++
189 lines
3.5 KiB
C++
/************************************************
|
|
* memory table storage, element address is not fixed,
|
|
* the current pointer to the latest element
|
|
*
|
|
* create zoufuzhou 20191001
|
|
*
|
|
************************************************/
|
|
|
|
#ifndef _MEM_TABLE_CACHE_HPP_
|
|
#define _MEM_TABLE_CACHE_HPP_
|
|
|
|
|
|
#include <glob/GlobMem.h>
|
|
|
|
template <typename T> class CMemTable
|
|
{
|
|
typedef struct{
|
|
unsigned int maxsize;
|
|
unsigned int size;
|
|
unsigned int count;
|
|
}HEAD;
|
|
//Local variable
|
|
private:
|
|
unsigned int m_getcnt;
|
|
|
|
//Memory variable
|
|
private:
|
|
HEAD* p_head;
|
|
T* p_ptr;
|
|
|
|
private:
|
|
void countMemory()
|
|
{
|
|
p_head->count++;
|
|
if(p_head->count <= 1)
|
|
{
|
|
p_head->count = 1;
|
|
m_getcnt = 1;
|
|
}
|
|
};
|
|
unsigned int countLocal()
|
|
{
|
|
m_getcnt++;
|
|
if(m_getcnt <=1)
|
|
{
|
|
m_getcnt = 1;
|
|
}
|
|
|
|
unsigned int cn = p_head->count - m_getcnt;
|
|
if(cn > p_head->maxsize)
|
|
{
|
|
cn = p_head->maxsize;
|
|
m_getcnt = p_head->count-p_head->maxsize;
|
|
}
|
|
else if(cn < 0 )
|
|
{
|
|
m_getcnt = p_head->count;
|
|
cn = 0;
|
|
}
|
|
return cn;
|
|
};
|
|
void init(const string& tableName)
|
|
{
|
|
char* ptr = (char*)GlobMem::GetInstancePtr()->GetTablePtr(tableName);
|
|
if(ptr == nullptr)
|
|
{
|
|
p_ptr = nullptr;
|
|
p_head = nullptr;
|
|
|
|
assert(true);
|
|
return;
|
|
}
|
|
p_head = (HEAD*)(ptr);
|
|
p_ptr = (T*)(ptr+sizeof(HEAD));
|
|
};
|
|
|
|
public:
|
|
CMemTable(const string& tableName)
|
|
{
|
|
this->init(tableName);
|
|
assert(p_head->maxsize>1);
|
|
|
|
m_getcnt = p_head->count;
|
|
};
|
|
|
|
CMemTable(const string& tableName,unsigned int maxsize)
|
|
{
|
|
assert(maxsize>1);
|
|
this->init(tableName);
|
|
|
|
p_head->maxsize = maxsize;
|
|
if(p_head->size > p_head->maxsize)
|
|
{
|
|
p_head->size = p_head->maxsize;
|
|
}
|
|
|
|
m_getcnt = p_head->count;
|
|
};
|
|
|
|
~CMemTable(){};
|
|
|
|
T* operator()(void)
|
|
{
|
|
return p_ptr;
|
|
};
|
|
|
|
T* getNext()
|
|
{
|
|
if(this->isNew() == false)
|
|
{
|
|
return nullptr;
|
|
}
|
|
else
|
|
{
|
|
unsigned int cn = countLocal();
|
|
if(cn >= 0)
|
|
{
|
|
return this->operator[](cn);
|
|
}
|
|
else
|
|
{
|
|
return nullptr;
|
|
}
|
|
|
|
}
|
|
};
|
|
|
|
T* operator[](unsigned int i)
|
|
{
|
|
if(p_ptr == nullptr)return nullptr;
|
|
return p_ptr+i;
|
|
};
|
|
|
|
void push(T* t)
|
|
{
|
|
if(p_ptr == nullptr)return;
|
|
if( p_head->maxsize == 1)
|
|
{
|
|
memcpy(p_ptr,t,sizeof(T));
|
|
p_head->size = p_head->maxsize;
|
|
}
|
|
else if (p_head->size < p_head->maxsize)
|
|
{
|
|
memmove(p_ptr+1,p_ptr,sizeof(T)*p_head->size);
|
|
memcpy(p_ptr,t,sizeof(T));
|
|
p_head->size++;
|
|
}
|
|
else
|
|
{
|
|
memmove(&p_ptr[1],p_ptr,sizeof(T)*(p_head->maxsize-1));
|
|
memcpy(p_ptr,t,sizeof(T));
|
|
p_head->size = p_head->maxsize;
|
|
}
|
|
this->countMemory();
|
|
};
|
|
|
|
unsigned int count(void)
|
|
{
|
|
return this->size();
|
|
};
|
|
|
|
unsigned int size(void)
|
|
{
|
|
if(p_head == nullptr)return 0;
|
|
return p_head->size;
|
|
};
|
|
|
|
bool isNew(void)
|
|
{
|
|
if(m_getcnt < p_head->count)
|
|
{
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
return false;
|
|
}
|
|
};
|
|
|
|
void clear(void)
|
|
{
|
|
if(p_head == nullptr)return;
|
|
p_head->size = 0;
|
|
p_head->count = 0;
|
|
m_getcnt = p_head->count;
|
|
};
|
|
};
|
|
#endif
|