mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-22 12:30:19 +00:00
up
Update memory.h
This commit is contained in:
parent
ef3e172fbd
commit
12e07391ee
@ -38,6 +38,7 @@
|
|||||||
#define DEBUG_CEVAL_STEP 0
|
#define DEBUG_CEVAL_STEP 0
|
||||||
#define DEBUG_FULL_EXCEPTION 0
|
#define DEBUG_FULL_EXCEPTION 0
|
||||||
#define DEBUG_MEMORY_POOL 0
|
#define DEBUG_MEMORY_POOL 0
|
||||||
|
#define DEBUG_NO_MEMORY_POOL 0
|
||||||
#define DEBUG_NO_AUTO_GC 0
|
#define DEBUG_NO_AUTO_GC 0
|
||||||
#define DEBUG_GC_STATS 0
|
#define DEBUG_GC_STATS 0
|
||||||
|
|
||||||
|
30
src/memory.h
30
src/memory.h
@ -199,8 +199,9 @@ struct MemoryPool{
|
|||||||
Block _blocks[__MaxBlocks];
|
Block _blocks[__MaxBlocks];
|
||||||
Block* _free_list[__MaxBlocks];
|
Block* _free_list[__MaxBlocks];
|
||||||
int _free_list_size;
|
int _free_list_size;
|
||||||
|
bool dirty;
|
||||||
|
|
||||||
Arena(): _free_list_size(__MaxBlocks) {
|
Arena(): _free_list_size(__MaxBlocks), dirty(false){
|
||||||
for(int i=0; i<__MaxBlocks; i++){
|
for(int i=0; i<__MaxBlocks; i++){
|
||||||
_blocks[i].arena = this;
|
_blocks[i].arena = this;
|
||||||
_free_list[i] = &_blocks[i];
|
_free_list[i] = &_blocks[i];
|
||||||
@ -222,6 +223,7 @@ struct MemoryPool{
|
|||||||
if(empty()) throw std::runtime_error("Arena::alloc() called on empty arena");
|
if(empty()) throw std::runtime_error("Arena::alloc() called on empty arena");
|
||||||
#endif
|
#endif
|
||||||
_free_list_size--;
|
_free_list_size--;
|
||||||
|
if(_free_list_size == 0) dirty = true;
|
||||||
return _free_list[_free_list_size];
|
return _free_list[_free_list_size];
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,14 +238,14 @@ struct MemoryPool{
|
|||||||
|
|
||||||
DoubleLinkedList<Arena> _arenas;
|
DoubleLinkedList<Arena> _arenas;
|
||||||
DoubleLinkedList<Arena> _empty_arenas;
|
DoubleLinkedList<Arena> _empty_arenas;
|
||||||
DoubleLinkedList<Arena> _full_arenas;
|
|
||||||
|
|
||||||
static constexpr int FULL_ARENA_SIZE = 4;
|
|
||||||
|
|
||||||
template<typename __T>
|
template<typename __T>
|
||||||
void* alloc() { return alloc(sizeof(__T)); }
|
void* alloc() { return alloc(sizeof(__T)); }
|
||||||
|
|
||||||
void* alloc(size_t size){
|
void* alloc(size_t size){
|
||||||
|
#if DEBUG_NO_MEMORY_POOL
|
||||||
|
return malloc(size);
|
||||||
|
#endif
|
||||||
if(size > __BlockSize){
|
if(size > __BlockSize){
|
||||||
void* p = malloc(sizeof(void*) + size);
|
void* p = malloc(sizeof(void*) + size);
|
||||||
memset(p, 0, sizeof(void*));
|
memset(p, 0, sizeof(void*));
|
||||||
@ -252,11 +254,7 @@ struct MemoryPool{
|
|||||||
|
|
||||||
if(_arenas.empty()){
|
if(_arenas.empty()){
|
||||||
// std::cout << _arenas.size() << ',' << _empty_arenas.size() << ',' << _full_arenas.size() << std::endl;
|
// std::cout << _arenas.size() << ',' << _empty_arenas.size() << ',' << _full_arenas.size() << std::endl;
|
||||||
if(_full_arenas.empty()){
|
_arenas.push_back(new Arena());
|
||||||
_arenas.push_back(new Arena());
|
|
||||||
}else{
|
|
||||||
_arenas.move_all_back(_full_arenas);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
Arena* arena = _arenas.back();
|
Arena* arena = _arenas.back();
|
||||||
void* p = arena->alloc()->data;
|
void* p = arena->alloc()->data;
|
||||||
@ -268,6 +266,10 @@ struct MemoryPool{
|
|||||||
}
|
}
|
||||||
|
|
||||||
void dealloc(void* p){
|
void dealloc(void* p){
|
||||||
|
#if DEBUG_NO_MEMORY_POOL
|
||||||
|
free(p);
|
||||||
|
return;
|
||||||
|
#endif
|
||||||
#if DEBUG_MEMORY_POOL
|
#if DEBUG_MEMORY_POOL
|
||||||
if(p == nullptr) throw std::runtime_error("MemoryPool::dealloc() called on nullptr");
|
if(p == nullptr) throw std::runtime_error("MemoryPool::dealloc() called on nullptr");
|
||||||
#endif
|
#endif
|
||||||
@ -282,14 +284,9 @@ struct MemoryPool{
|
|||||||
arena->dealloc(block);
|
arena->dealloc(block);
|
||||||
}else{
|
}else{
|
||||||
arena->dealloc(block);
|
arena->dealloc(block);
|
||||||
if(arena->full() && _arenas.size()>2){
|
if(arena->full() && arena->dirty){
|
||||||
_arenas.erase(arena);
|
_arenas.erase(arena);
|
||||||
if(_full_arenas.size() < FULL_ARENA_SIZE){
|
delete arena;
|
||||||
// arena->tidy();
|
|
||||||
_full_arenas.push_back(arena);
|
|
||||||
}else{
|
|
||||||
delete arena;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -298,7 +295,6 @@ struct MemoryPool{
|
|||||||
~MemoryPool(){
|
~MemoryPool(){
|
||||||
_arenas.apply([](Arena* arena){ delete arena; });
|
_arenas.apply([](Arena* arena){ delete arena; });
|
||||||
_empty_arenas.apply([](Arena* arena){ delete arena; });
|
_empty_arenas.apply([](Arena* arena){ delete arena; });
|
||||||
_full_arenas.apply([](Arena* arena){ delete arena; });
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user