/*********************************************************
In some cases where the total memory usage is uncertain , Frequent use new Application memory , And then through the linked list
Indexing seems to be a very common practice . natural , It's also hard to do random positioning .
The following memory pool class uses the two-tier index table to partition the memory into large blocks , Any block only needs to be searched
lead 3 Once, you can locate it .
Number of indexes , Number of allocation units per index block , And the byte length of the allocation unit should be 2 The integer of
The next power ( For the sake of efficiency )
//by:www.frombyte.com zhangyu(zhangyu.blog.51cto.com)
*********************************************************/
class MemTable
{
public:
MemTable(void);
public:
~MemTable(void);
public:
void CREATE(MemTableIn *in_m);
void DEL();
LPSTR NEW();// Allocate one unit
LPSTR NEW_CONTINUEOUS(UINT n);// It is used to distribute several unit
UINT NEW(UINT n); // It is used to allocate several unit
LPSTR GET(UINT n);// To get the second n An assigned pointer address
int get_totle_unitnum();
public:
MemTableIn in;
LPSTR **pDouble_Indirect;
LPSTR lpBitmap;
LPSTR *pIndirect; LPSTR m_lpFirstFree;
int nFree[];//0 Represents the freedom of the secondary index ,1 Express 1 The freedom of level index ,2 Represents the block free index number
INT32 m_EndBlkUseredUnits;
int m_Vblkbytes;
UINT m_UnitTotalNum;
UINT m_log2Rindexs,m_log2Runits,m_log2Rbitmap,m_log2Lindexs,m_log2Lunits,m_log2Lbitmap;
UINT m_log2UnitBytes;
UINT m_index2ID,m_index1ID,m_UnitID;
};
 In some cases where the total memory usage is uncertain , Frequent use new Application memory , And then through the linked list 
Indexing seems to be a very common practice . natural , It's also hard to do random positioning .
The following memory pool class uses the two-tier index table to partition the memory into large blocks , Any block only needs to be searched
lead 3 Once, you can locate it .
Number of indexes , Number of allocation units per index block , And the byte length of the allocation unit should be 2 The integer of
The next power ( For the sake of efficiency )

CPP The contents are as follows :

 /**
* ffs - Find the first set bit in an int
* @x:
*
* Description... The highest value used to count an integer is 1 Bit , How many people are there in the back .
* Another way : Start at the top 1, find 1 after , Look at the binary data 1000....000 yes 2 Several power
*
* Returns:
*/
int ffs(int x)
{
int r = ; if (!x)
return ;
if (!(x & 0xffff)) {
x >>= ;
r += ;
}
if (!(x & 0xff)) {
x >>= ;
r += ;
}
if (!(x & 0xf)) {
x >>= ;
r += ;
}
if (!(x & )) {
x >>= ;
r += ;
}
if (!(x & )) {
x >>= ;
r += ;
}
return r;
}
LPSTR MemTree::GET(MemTreeHead *pHead,UINT n)
{
int t;
LPSTR lpt;
int i,ii;
// Judge whether to store directly
if(n<m.rootDirectUnitNum)
return pHead->lpRootUnit + n*m.Vsizeof;
else
t=n-m.rootDirectUnitNum; for(i=;i<DEEP;i++)
{
if(t<TBT[i][])
break;
t-=TBT[i][];
}
//i It's depth ,t It's deep n
lpt=pHead->pROOT_INDEX[i-];
int D;
for(ii=;ii<i;ii++)
{
D=t /TBT[i][ii];
t=t % TBT[i][ii];
lpt=*(LPSTR*)(lpt+sizeof(LPSTR)*D);
}
return (lpt + t*m.Vsizeof); } MemTable::MemTable(void)
{
} MemTable::~MemTable(void)
{
// Release all space
for(int i=;i<in.nIndexNum;i++)
{
LPSTR *pp=pDouble_Indirect[i];
if(pp==NULL)
break;
for(int ii=;ii<in.nIndexNum;ii++)
{
LPSTR p=pp[ii];
if(p==NULL)
break;
else
delete [] p;
}
delete [] pp;
}
delete [] pDouble_Indirect;
}
void MemTable::CREATE(MemTableIn *in_m)
{
//1、 Initialize some reference blocks
memset(&in,,sizeof(in));
in=*in_m;
m_UnitTotalNum=;
nFree[]=nFree[]=nFree[]=; m_Vblkbytes= in.nUnitBytes *in.nUnitPerIndex;
m_log2Runits=ffs(in.nUnitPerIndex)-;
m_log2Rindexs=ffs(in.nIndexNum)-;
m_log2UnitBytes=ffs(in.nUnitBytes)-; m_log2Lindexs=sizeof(UINT)*-m_log2Rindexs;
m_log2Lunits=sizeof(UINT)*-m_log2Runits; //2、 Initialize secondary index table
pDouble_Indirect=new LPSTR* [in.nIndexNum];
memset(pDouble_Indirect,,in.nIndexNum*sizeof(LPSTR));
nFree[]=in.nIndexNum;
}
LPSTR MemTable::NEW()
{
LPSTR lpReturn;
if(nFree[]==)// I ran out of blocks
{
if(nFree[]==)
{
if(nFree[]==)
return NULL;// Write the log : Reach the maximum allocated quantity pIndirect=pDouble_Indirect[in.nIndexNum - nFree[]]=new LPSTR [in.nIndexNum];
memset(pIndirect,,in.nIndexNum*sizeof(LPSTR));
nFree[]=in.nIndexNum-; lpReturn=pIndirect[]=new char[m_Vblkbytes];
memset(lpReturn,,m_Vblkbytes);
nFree[]=in.nUnitPerIndex-;
m_lpFirstFree = lpReturn + in.nUnitBytes;
nFree[]--; }
else
{
lpReturn=pIndirect[in.nIndexNum - nFree[]]=new char[m_Vblkbytes];
memset(lpReturn,,m_Vblkbytes);
nFree[]--;
nFree[]=in.nUnitPerIndex-;
m_lpFirstFree = lpReturn + in.nUnitBytes;
}
}
else
{
lpReturn=m_lpFirstFree;
nFree[]--;
m_lpFirstFree += in.nUnitBytes;
}
m_UnitTotalNum++;
return lpReturn; }//by:www.frombyte.com zhangyu(zhangyu.blog.51cto.com)
UINT MemTable::NEW(UINT n)
{
UINT nReturn=m_UnitTotalNum;
for(int i=;i<n;i++)
NEW();
return nReturn; }
LPSTR MemTable::NEW_CONTINUEOUS(UINT n)
{
LPSTR lpReturn;
if(n>in.nUnitPerIndex)
return NULL; if(nFree[]>=n)
{
nFree[]-=n;
lpReturn=m_lpFirstFree;
m_UnitTotalNum+=n;
m_lpFirstFree += (n*in.nUnitBytes);
}
else
{
m_UnitTotalNum+=nFree[];// The remaining space is reserved 、 Ignore
nFree[]=;
lpReturn=NEW();
nFree[] -= (n-);
m_lpFirstFree += ((n-)*in.nUnitBytes);
m_UnitTotalNum += (n-);
}
return lpReturn;
}
LPSTR MemTable::GET(UINT n)
{ //by:www.frombyte.com zhangyu(zhangyu.blog.51cto.com)
if(n>=m_UnitTotalNum)
return NULL;// Write the log : Exceeding the scope
m_UnitID=n<< m_log2Lunits >>m_log2Lunits;
m_index1ID=n >> m_log2Runits;
m_index2ID=m_index1ID >> m_log2Rindexs;
m_index1ID=m_index1ID <<m_log2Lindexs >>m_log2Lindexs; return (pDouble_Indirect[m_index2ID][m_index1ID] + (m_UnitID<<m_log2UnitBytes)); }
void MemTable::DEL()
{ }

c++ Memory optimization : Second level indirect index mode memory pool more related articles

  1. SQL Server Index design of memory optimization table

    Tested version :SQL Server 2017 Hash index can be created on memory optimized table (Hash Index) And memory optimized non aggregation (NONCLUSTERED) Indexes , These two types of indexes are also memory optimized , Called memory optimized index , Heji ...

  2. linux Manual release of memory optimization linux Memory

    First introduce free command Linux free The command is used to display memory status . free The instruction shows the memory usage , Including physical memory , Virtual swap file memory , Shared memory section , And the buffer used by the system core . grammar : free [- ...

  3. try SQLSERVER2014 Memory optimization table for

    try SQLSERVER2014 Memory optimization table for SQL Server 2014 Memory engine in ( code-named Hekaton) take OLTP To a new level . Now? , The storage engine has been integrated into the current database management system , And using advanced memory technology ...

  4. SQLServer2014 Memory optimization table evaluation

    SQLServer2014 Memory optimization table evaluation classification : SQL Memory tables 2014-06-20 11:49 1619 Human reading Comment on (11) Collection report Catalog (?)[-] SQLServer2014 Basic requirements for the use of Inside ...

  5. SQLSERVER2014 Memory optimization table for

    SQL Server 2014 Memory engine in ( code-named Hekaton) take OLTP To a new level . Now? , The storage engine has been integrated into the current database management system , And using advanced memory technology to support large-scale OLTP The workload . Even so , ...

  6. try SQLServer 2014 Memory optimization table for

    SQL Server2014 Storage engine : Row storage engine , Column storage engine , Memory engine SQL Server 2014 Memory engine in ( code-named Hekaton) take OLTP To a new level . Now? , The storage engine has been integrated into the current data ...

  7. try SQLServer 2014 Memory optimization table for ( Reprint )

    SQL Server2014 Storage engine : Row storage engine , Column storage engine , Memory engine SQL Server 2014 Memory engine in ( code-named Hekaton) take OLTP To a new level . Now? , The storage engine has been integrated into the current data ...

  8. Android performance optimization - Explain the details of memory optimization

    Preface APP Memory usage , It is an important index to evaluate the performance of an application . Although the memory of smart phones is getting bigger and bigger now , But a good application should make the most of its efficiency , Keep improving . In this article we will focus on Android Memory optimization . The first part of this article is ...

  9. android Develop how to do memory optimization

    Many people think that JAVA Program , Because of the garbage collection mechanism , There should be no memory leaks . In fact, if I In a program , An object is no longer used , But because there are still references to it , The garbage collector can't recycle it , Of course, the memory occupied by the object cannot be used , That's it ...

Random recommendation

  1. HDU 3328 Flipper Stack simulation

    First of all, I want to say , It took me a long time to understand the problem of poor English ...... There are several cards in the input , Then enter the initial state of these cards ( Face up or face down ), Then enter the mode of operation ,R Turn the deck on the right ,L Turn the deck on the left , Until the end of the pile ...

  2. C Security issues and pointer misuse

    Welcome to my personal blog :www.wuyudong.com, More wonderful articles to share with you Pointer declaration and initialization 1. Improper pointer declaration Consider the following statement : int* ptr1, ptr2; // ptr1 For the pointer ,pt ...

  3. C++ Learning notes 4

    1. Containers = data structure + Algorithm . It's equivalent to designing a special device for storing complex data . Used to transfer complex data in development . 2. Template functions can only be written in header files . You can't make a statement alone . 3.STL Container classes fall into three categories : (1). Sequence containers ...

  4. Algorithm template ——Dinic Network maximum flow 1

    Realization function : Same as sap Network maximum flow For the first time today Dinic, The biggest characteristic of feeling is —— Quite colloquial , Quite easy to understand , And it doesn't affect complexity at all , It's just a few lines of code at most The main principle is to use it every time spfa With O(n) The time complexity of ...

  5. IIS And Apache prohibit IP Address direct access to the website

    One .IIS Preventing malicious domain names from pointing to solutions First click " Start "-" Set up "-" Control panel "-" Management tools ", find "Inter ...

  6. How do I turn a blog into PDF Of

    Preface Only a bald head can make it stronger Some readers have asked me before :"3y Do you have an electronic version of your blog ? I'd like an electronic version of ". I said, :" No! , I didn't get an electronic version of , I have an article navigation page here , You can go to the article navigation ...

  7. java_ Reptiles _ Crawl the real video address from the Tencent video playback interface

    Because I want to climb a little bit of postgraduate entrance examination in WeChat official account It took almost a day to get this reptile done ( It's not really a reptile , Even if it's an address parser that can process in batch , Half a reptile ) Not much to say , Get to the point ( This article is suitable for java Basic students , There is no basic client ...

  8. When the return value is json When the string How to get json Array

    json data format {"IPPORT":"192.168.0.12","time":"2017-04-05 09:12:06&quo ...

  9. Error:fatal: Not a git repository (or any of the parent directories): .git

    Execute in the project directory git init command . Be accomplished .

  10. Windows build Nexus3 Private clothes

    1. Nexus brief introduction Nexus Is a powerful Maven Warehouse Manager , It greatly simplifies the maintenance of its own internal warehouse and the access of its external warehouse ; utilize Nexus You can have full control of access and deployment in just one place ...