This commit is contained in:
Valk Richard Li 2020-04-20 06:11:52 -07:00 committed by GitHub
parent 650f9fb4a3
commit 3154076c92
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 19 additions and 38 deletions

View File

@ -137,72 +137,53 @@ struct gc_unit
struct memory_block struct memory_block
{ {
gc_unit space[NAS_POOL_SIZE]; gc_unit space[NAS_POOL_SIZE];
memory_block* next;
}; };
class memory_block_list class memory_block_list
{ {
private: private:
memory_block* head; std::vector<memory_block*> mem_page;
int mem_size; int mem_size;
int blk_size;
public: public:
memory_block_list() memory_block_list()
{ {
mem_size=0; mem_size=0;
blk_size=1; memory_block* tmp=new memory_block;
head=new memory_block; mem_page.push_back(tmp);
head->next=NULL;
return; return;
} }
~memory_block_list() ~memory_block_list()
{ {
mem_size=0; mem_size=0;
blk_size=0; int page_size=mem_page.size();
memory_block* ptr=head; for(int i=0;i<page_size;++i)
while(ptr) delete mem_page[i];
{
memory_block* tmp_ptr=ptr;
ptr=ptr->next;
delete tmp_ptr;
}
return; return;
} }
void clear() void clear()
{ {
memory_block* ptr=head; int page_size=mem_page.size();
while(ptr) for(int i=0;i<page_size;++i)
{ delete mem_page[i];
memory_block* tmp_ptr=ptr; mem_page.clear();
ptr=ptr->next;
delete tmp_ptr;
}
mem_size=0; mem_size=0;
blk_size=1; memory_block* tmp=new memory_block;
head=new memory_block; mem_page.push_back(tmp);
head->next=NULL;
return; return;
} }
gc_unit& operator[](int address) gc_unit& operator[](int address)
{ {
int block_num=address/NAS_POOL_SIZE; int page_num=address/NAS_POOL_SIZE;
int block_plc=address%NAS_POOL_SIZE; int block_plc=address%NAS_POOL_SIZE;
memory_block* ptr=head; return mem_page[page_num]->space[block_plc];
for(int i=0;i<block_num;++i)
ptr=ptr->next;
return ptr->space[block_plc];
} }
void push_back() void push_back()
{ {
++mem_size; ++mem_size;
if(mem_size>blk_size*NAS_POOL_SIZE) if(mem_size>mem_page.size()*NAS_POOL_SIZE)
{ {
memory_block* ptr=head; memory_block* tmp=new memory_block;
while(ptr->next) mem_page.push_back(tmp);
ptr=ptr->next;
ptr->next=new memory_block;
ptr->next->next=NULL;
++blk_size;
} }
return; return;
} }
@ -212,7 +193,7 @@ public:
} }
int capacity() int capacity()
{ {
return NAS_POOL_SIZE*blk_size; return NAS_POOL_SIZE*mem_page.size();
} }
}; };
#endif #endif