diff --git a/include/pocketpy/common/memorypool.h b/include/pocketpy/common/memorypool.h index 49a542ff..e732d19f 100644 --- a/include/pocketpy/common/memorypool.h +++ b/include/pocketpy/common/memorypool.h @@ -1,12 +1,18 @@ #pragma once -#define kPoolExprBlockSize 128 -#define kPoolFrameBlockSize 80 +typedef struct FixedMemoryPool { + int BlockSize; + int BlockCount; -void MemoryPools__initialize(); -void MemoryPools__finalize(); + char* data; + char* data_end; + int exceeded_bytes; -void* PoolExpr_alloc(); -void PoolExpr_dealloc(void*); -void* PoolFrame_alloc(); -void PoolFrame_dealloc(void*); + char** _free_list; + char** _free_list_end; +} FixedMemoryPool; + +void FixedMemoryPool__ctor(FixedMemoryPool* self, int BlockSize, int BlockCount); +void FixedMemoryPool__dtor(FixedMemoryPool* self); +void* FixedMemoryPool__alloc(FixedMemoryPool* self); +void FixedMemoryPool__dealloc(FixedMemoryPool* self, void* p); \ No newline at end of file diff --git a/include/pocketpy/interpreter/vm.h b/include/pocketpy/interpreter/vm.h index 678cdd20..77df406c 100644 --- a/include/pocketpy/interpreter/vm.h +++ b/include/pocketpy/interpreter/vm.h @@ -1,5 +1,6 @@ #pragma once +#include "pocketpy/common/memorypool.h" #include "pocketpy/objects/codeobject.h" #include "pocketpy/pocketpy.h" #include "pocketpy/interpreter/heap.h" @@ -38,6 +39,7 @@ typedef struct VM { py_StackRef __curr_function; py_TValue __vectorcall_buffer[PK_MAX_CO_VARNAMES]; + FixedMemoryPool pool_frame; ManagedHeap heap; ValueStack stack; // put `stack` at the end for better cache locality } VM; diff --git a/src/common/memorypool.c b/src/common/memorypool.c index 76c24723..4104fa17 100644 --- a/src/common/memorypool.c +++ b/src/common/memorypool.c @@ -4,19 +4,7 @@ #include #include -typedef struct FixedMemoryPool { - int BlockSize; - int BlockCount; - - char* data; - char* data_end; - int exceeded_bytes; - - char** _free_list; - char** _free_list_end; -} FixedMemoryPool; - -static void FixedMemoryPool__ctor(FixedMemoryPool* self, int BlockSize, int BlockCount) { +void FixedMemoryPool__ctor(FixedMemoryPool* self, int BlockSize, int BlockCount) { self->BlockSize = BlockSize; self->BlockCount = BlockCount; self->exceeded_bytes = 0; @@ -29,12 +17,12 @@ static void FixedMemoryPool__ctor(FixedMemoryPool* self, int BlockSize, int Bloc } } -static void FixedMemoryPool__dtor(FixedMemoryPool* self) { +void FixedMemoryPool__dtor(FixedMemoryPool* self) { PK_FREE(self->_free_list); PK_FREE(self->data); } -static void* FixedMemoryPool__alloc(FixedMemoryPool* self) { +void* FixedMemoryPool__alloc(FixedMemoryPool* self) { if(self->_free_list_end != self->_free_list) { self->_free_list_end--; return *self->_free_list_end; @@ -44,7 +32,7 @@ static void* FixedMemoryPool__alloc(FixedMemoryPool* self) { } } -static void FixedMemoryPool__dealloc(FixedMemoryPool* self, void* p) { +void FixedMemoryPool__dealloc(FixedMemoryPool* self, void* p) { bool is_valid = (char*)p >= self->data && (char*)p < self->data_end; if(is_valid) { *self->_free_list_end = p; @@ -55,39 +43,10 @@ static void FixedMemoryPool__dealloc(FixedMemoryPool* self, void* p) { } } -static int FixedMemoryPool__used_bytes(FixedMemoryPool* self) { - return (self->_free_list_end - self->_free_list) * self->BlockSize; -} +// static int FixedMemoryPool__used_bytes(FixedMemoryPool* self) { +// return (self->_free_list_end - self->_free_list) * self->BlockSize; +// } -static int FixedMemoryPool__total_bytes(FixedMemoryPool* self) { - return self->BlockCount * self->BlockSize; -} - -static FixedMemoryPool PoolExpr; -static FixedMemoryPool PoolFrame; - -void MemoryPools__initialize(){ - FixedMemoryPool__ctor(&PoolExpr, kPoolExprBlockSize, 64); - FixedMemoryPool__ctor(&PoolFrame, kPoolFrameBlockSize, 128); -} - -void MemoryPools__finalize(){ - FixedMemoryPool__dtor(&PoolExpr); - FixedMemoryPool__dtor(&PoolFrame); -} - -void* PoolExpr_alloc() { - return FixedMemoryPool__alloc(&PoolExpr); -} - -void PoolExpr_dealloc(void* p) { - FixedMemoryPool__dealloc(&PoolExpr, p); -} - -void* PoolFrame_alloc() { - return FixedMemoryPool__alloc(&PoolFrame); -} - -void PoolFrame_dealloc(void* p) { - FixedMemoryPool__dealloc(&PoolFrame, p); -} +// static int FixedMemoryPool__total_bytes(FixedMemoryPool* self) { +// return self->BlockCount * self->BlockSize; +// } \ No newline at end of file diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index b6aee04c..eeefad2d 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -1,10 +1,10 @@ #include "pocketpy/compiler/compiler.h" #include "pocketpy/compiler/lexer.h" +#include "pocketpy/objects/base.h" #include "pocketpy/objects/codeobject.h" #include "pocketpy/objects/sourcedata.h" #include "pocketpy/objects/object.h" #include "pocketpy/common/sstream.h" -#include "pocketpy/common/memorypool.h" #include #include @@ -30,8 +30,6 @@ typedef struct ExprVt { void (*dtor)(Expr*); } ExprVt; -#define static_assert_expr_size(T) static_assert(sizeof(T) <= kPoolExprBlockSize, "") - #define vtcall(f, self, ctx) ((self)->vt->f((self), (ctx))) #define vtemit_(self, ctx) vtcall(emit_, (self), (ctx)) #define vtemit_del(self, ctx) ((self)->vt->emit_del ? vtcall(emit_del, self, ctx) : false) @@ -44,7 +42,7 @@ typedef struct ExprVt { do { \ if(self) { \ if((self)->vt->dtor) (self)->vt->dtor(self); \ - PoolExpr_dealloc(self); \ + PK_FREE(self); \ } \ } while(0) @@ -148,8 +146,7 @@ NameExpr* NameExpr__new(int line, py_Name name, NameScope scope) { .emit_del = NameExpr__emit_del, .emit_store = NameExpr__emit_store, .is_name = true}; - static_assert_expr_size(NameExpr); - NameExpr* self = PoolExpr_alloc(); + NameExpr* self = PK_MALLOC(sizeof(NameExpr)); self->vt = &Vt; self->line = line; self->name = name; @@ -186,8 +183,7 @@ StarredExpr* StarredExpr__new(int line, Expr* child, int level) { .emit_store = StarredExpr__emit_store, .is_starred = true, .dtor = StarredExpr__dtor}; - static_assert_expr_size(StarredExpr); - StarredExpr* self = PoolExpr_alloc(); + StarredExpr* self = PK_MALLOC(sizeof(StarredExpr)); self->vt = &Vt; self->line = line; self->child = child; @@ -216,8 +212,7 @@ static void UnaryExpr__emit_(Expr* self_, Ctx* ctx) { UnaryExpr* UnaryExpr__new(int line, Expr* child, Opcode opcode) { const static ExprVt Vt = {.emit_ = UnaryExpr__emit_, .dtor = UnaryExpr__dtor}; - static_assert_expr_size(UnaryExpr); - UnaryExpr* self = PoolExpr_alloc(); + UnaryExpr* self = PK_MALLOC(sizeof(UnaryExpr)); self->vt = &Vt; self->line = line; self->child = child; @@ -240,8 +235,7 @@ void FStringSpecExpr__emit_(Expr* self_, Ctx* ctx) { FStringSpecExpr* FStringSpecExpr__new(int line, Expr* child, c11_sv spec) { const static ExprVt Vt = {.emit_ = FStringSpecExpr__emit_, .dtor = UnaryExpr__dtor}; - static_assert_expr_size(FStringSpecExpr); - FStringSpecExpr* self = PoolExpr_alloc(); + FStringSpecExpr* self = PK_MALLOC(sizeof(FStringSpecExpr)); self->vt = &Vt; self->line = line; self->child = child; @@ -263,8 +257,7 @@ void RawStringExpr__emit_(Expr* self_, Ctx* ctx) { RawStringExpr* RawStringExpr__new(int line, c11_sv value, Opcode opcode) { const static ExprVt Vt = {.emit_ = RawStringExpr__emit_}; - static_assert_expr_size(RawStringExpr); - RawStringExpr* self = PoolExpr_alloc(); + RawStringExpr* self = PK_MALLOC(sizeof(RawStringExpr)); self->vt = &Vt; self->line = line; self->value = value; @@ -288,8 +281,7 @@ void ImagExpr__emit_(Expr* self_, Ctx* ctx) { ImagExpr* ImagExpr__new(int line, double value) { const static ExprVt Vt = {.emit_ = ImagExpr__emit_}; - static_assert_expr_size(ImagExpr); - ImagExpr* self = PoolExpr_alloc(); + ImagExpr* self = PK_MALLOC(sizeof(ImagExpr)); self->vt = &Vt; self->line = line; self->value = value; @@ -333,8 +325,7 @@ void LiteralExpr__emit_(Expr* self_, Ctx* ctx) { LiteralExpr* LiteralExpr__new(int line, const TokenValue* value) { const static ExprVt Vt = {.emit_ = LiteralExpr__emit_, .is_literal = true}; - static_assert_expr_size(LiteralExpr); - LiteralExpr* self = PoolExpr_alloc(); + LiteralExpr* self = PK_MALLOC(sizeof(LiteralExpr)); self->vt = &Vt; self->line = line; self->value = value; @@ -362,8 +353,7 @@ void Literal0Expr__emit_(Expr* self_, Ctx* ctx) { Literal0Expr* Literal0Expr__new(int line, TokenIndex token) { const static ExprVt Vt = {.emit_ = Literal0Expr__emit_}; - static_assert_expr_size(Literal0Expr); - Literal0Expr* self = PoolExpr_alloc(); + Literal0Expr* self = PK_MALLOC(sizeof(Literal0Expr)); self->vt = &Vt; self->line = line; self->token = token; @@ -403,8 +393,7 @@ void SliceExpr__emit_(Expr* self_, Ctx* ctx) { SliceExpr* SliceExpr__new(int line) { const static ExprVt Vt = {.dtor = SliceExpr__dtor, .emit_ = SliceExpr__emit_}; - static_assert_expr_size(SliceExpr); - SliceExpr* self = PoolExpr_alloc(); + SliceExpr* self = PK_MALLOC(sizeof(SliceExpr)); self->vt = &Vt; self->line = line; self->start = NULL; @@ -433,8 +422,7 @@ static void DictItemExpr__emit_(Expr* self_, Ctx* ctx) { static DictItemExpr* DictItemExpr__new(int line) { const static ExprVt Vt = {.dtor = DictItemExpr__dtor, .emit_ = DictItemExpr__emit_}; - static_assert_expr_size(DictItemExpr); - DictItemExpr* self = PoolExpr_alloc(); + DictItemExpr* self = PK_MALLOC(sizeof(DictItemExpr)); self->vt = &Vt; self->line = line; self->key = NULL; @@ -521,8 +509,7 @@ bool TupleExpr__emit_del(Expr* self_, Ctx* ctx) { } static SequenceExpr* SequenceExpr__new(int line, const ExprVt* vt, int count, Opcode opcode) { - static_assert_expr_size(SequenceExpr); - SequenceExpr* self = PoolExpr_alloc(); + SequenceExpr* self = PK_MALLOC(sizeof(SequenceExpr)); self->vt = vt; self->line = line; self->opcode = opcode; @@ -608,8 +595,7 @@ void CompExpr__emit_(Expr* self_, Ctx* ctx) { CompExpr* CompExpr__new(int line, Opcode op0, Opcode op1) { const static ExprVt Vt = {.dtor = CompExpr__dtor, .emit_ = CompExpr__emit_}; - static_assert_expr_size(CompExpr); - CompExpr* self = PoolExpr_alloc(); + CompExpr* self = PK_MALLOC(sizeof(CompExpr)); self->vt = &Vt; self->line = line; self->op0 = op0; @@ -633,8 +619,7 @@ static void LambdaExpr__emit_(Expr* self_, Ctx* ctx) { LambdaExpr* LambdaExpr__new(int line, int index) { const static ExprVt Vt = {.emit_ = LambdaExpr__emit_}; - static_assert_expr_size(LambdaExpr); - LambdaExpr* self = PoolExpr_alloc(); + LambdaExpr* self = PK_MALLOC(sizeof(LambdaExpr)); self->vt = &Vt; self->line = line; self->index = index; @@ -665,8 +650,7 @@ void LogicBinaryExpr__emit_(Expr* self_, Ctx* ctx) { LogicBinaryExpr* LogicBinaryExpr__new(int line, Opcode opcode) { const static ExprVt Vt = {.emit_ = LogicBinaryExpr__emit_, .dtor = LogicBinaryExpr__dtor}; - static_assert_expr_size(LogicBinaryExpr); - LogicBinaryExpr* self = PoolExpr_alloc(); + LogicBinaryExpr* self = PK_MALLOC(sizeof(LogicBinaryExpr)); self->vt = &Vt; self->line = line; self->lhs = NULL; @@ -705,8 +689,7 @@ GroupedExpr* GroupedExpr__new(int line, Expr* child) { .emit_ = GroupedExpr__emit_, .emit_del = GroupedExpr__emit_del, .emit_store = GroupedExpr__emit_store}; - static_assert_expr_size(GroupedExpr); - GroupedExpr* self = PoolExpr_alloc(); + GroupedExpr* self = PK_MALLOC(sizeof(GroupedExpr)); self->vt = &Vt; self->line = line; self->child = child; @@ -833,8 +816,7 @@ BinaryExpr* BinaryExpr__new(int line, TokenIndex op, bool inplace) { const static ExprVt Vt = {.emit_ = BinaryExpr__emit_, .dtor = BinaryExpr__dtor, .is_binary = true}; - static_assert_expr_size(BinaryExpr); - BinaryExpr* self = PoolExpr_alloc(); + BinaryExpr* self = PK_MALLOC(sizeof(BinaryExpr)); self->vt = &Vt; self->line = line; self->lhs = NULL; @@ -871,8 +853,7 @@ void TernaryExpr__emit_(Expr* self_, Ctx* ctx) { TernaryExpr* TernaryExpr__new(int line) { const static ExprVt Vt = {.dtor = TernaryExpr__dtor, .emit_ = TernaryExpr__emit_}; - static_assert_expr_size(TernaryExpr); - TernaryExpr* self = PoolExpr_alloc(); + TernaryExpr* self = PK_MALLOC(sizeof(TernaryExpr)); self->vt = &Vt; self->line = line; self->cond = NULL; @@ -942,8 +923,7 @@ SubscrExpr* SubscrExpr__new(int line) { .emit_del = SubscrExpr__emit_del, .is_subscr = true, }; - static_assert_expr_size(SubscrExpr); - SubscrExpr* self = PoolExpr_alloc(); + SubscrExpr* self = PK_MALLOC(sizeof(SubscrExpr)); self->vt = &Vt; self->line = line; self->lhs = NULL; @@ -1005,8 +985,7 @@ AttribExpr* AttribExpr__new(int line, Expr* child, py_Name name) { .emit_istore = AttribExpr__emit_istore, .dtor = AttribExpr__dtor, .is_attrib = true}; - static_assert_expr_size(AttribExpr); - AttribExpr* self = PoolExpr_alloc(); + AttribExpr* self = PK_MALLOC(sizeof(AttribExpr)); self->vt = &Vt; self->line = line; self->child = child; @@ -1078,8 +1057,7 @@ void CallExpr__emit_(Expr* self_, Ctx* ctx) { CallExpr* CallExpr__new(int line, Expr* callable) { const static ExprVt Vt = {.dtor = CallExpr__dtor, .emit_ = CallExpr__emit_}; - static_assert_expr_size(CallExpr); - CallExpr* self = PoolExpr_alloc(); + CallExpr* self = PK_MALLOC(sizeof(CallExpr)); self->vt = &Vt; self->line = line; self->callable = callable; diff --git a/src/interpreter/frame.c b/src/interpreter/frame.c index cd6e417c..64c7e5ee 100644 --- a/src/interpreter/frame.c +++ b/src/interpreter/frame.c @@ -1,4 +1,5 @@ #include "pocketpy/interpreter/frame.h" +#include "pocketpy/common/memorypool.h" #include "pocketpy/interpreter/vm.h" #include "pocketpy/objects/base.h" #include "pocketpy/objects/codeobject.h" @@ -42,8 +43,7 @@ Frame* Frame__new(const CodeObject* co, py_StackRef p0, py_StackRef locals, bool has_function) { - static_assert(sizeof(Frame) <= kPoolFrameBlockSize, "!(sizeof(Frame) <= kPoolFrameBlockSize)"); - Frame* self = PoolFrame_alloc(); + Frame* self = FixedMemoryPool__alloc(&pk_current_vm->pool_frame); self->f_back = NULL; self->ip = (Bytecode*)co->codes.data - 1; self->co = co; @@ -62,7 +62,7 @@ void Frame__delete(Frame* self) { self->uw_list = p->next; UnwindTarget__delete(p); } - PoolFrame_dealloc(self); + FixedMemoryPool__dealloc(&pk_current_vm->pool_frame, self); } int Frame__prepare_jump_exception_handler(Frame* self, ValueStack* _s) { diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 1d77862e..9275533d 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -1,4 +1,5 @@ #include "pocketpy/interpreter/vm.h" +#include "pocketpy/common/memorypool.h" #include "pocketpy/common/sstream.h" #include "pocketpy/common/utils.h" #include "pocketpy/interpreter/generator.h" @@ -75,6 +76,8 @@ void VM__ctor(VM* self) { self->__curr_class = NULL; self->__curr_function = NULL; + FixedMemoryPool__ctor(&self->pool_frame, sizeof(Frame), 32); + ManagedHeap__ctor(&self->heap); ValueStack__ctor(&self->stack); @@ -246,6 +249,7 @@ void VM__dtor(VM* self) { VM__pop_frame(self); ModuleDict__dtor(&self->modules); TypeList__dtor(&self->types); + FixedMemoryPool__dtor(&self->pool_frame); ValueStack__clear(&self->stack); } diff --git a/src/public/internal.c b/src/public/internal.c index 3e74eccd..5aa94d1e 100644 --- a/src/public/internal.c +++ b/src/public/internal.c @@ -27,7 +27,6 @@ void py_initialize() { static_assert(sizeof(py_TValue) == 16, "sizeof(py_TValue) != 16"); static_assert(offsetof(py_TValue, extra) == 4, "offsetof(py_TValue, extra) != 4"); - MemoryPools__initialize(); py_Name__initialize(); pk_current_vm = pk_all_vm[0] = &pk_default_vm; @@ -63,7 +62,6 @@ void py_finalize() { VM__dtor(&pk_default_vm); pk_current_vm = NULL; py_Name__finalize(); - MemoryPools__finalize(); } void py_switchvm(int index) {