diff --git a/include/pocketpy/common/utils.h b/include/pocketpy/common/utils.h index d0855b85..ed017423 100644 --- a/include/pocketpy/common/utils.h +++ b/include/pocketpy/common/utils.h @@ -45,6 +45,6 @@ typedef struct RefCounted { do { \ if(--(obj)->rc.count == 0) { \ (obj)->rc.dtor(obj); \ - free(obj); \ + PK_FREE(obj); \ } \ } while(0) diff --git a/include/pocketpy/xmacros/smallmap.h b/include/pocketpy/xmacros/smallmap.h index dfb95c29..7826a9ee 100644 --- a/include/pocketpy/xmacros/smallmap.h +++ b/include/pocketpy/xmacros/smallmap.h @@ -66,7 +66,7 @@ NAME* METHOD(new)() { void METHOD(delete)(NAME* self) { METHOD(dtor)(self); - free(self); + PK_FREE(self); } void METHOD(set)(NAME* self, K key, V value) { diff --git a/src/common/algorithm.c b/src/common/algorithm.c index 025dfa4d..edbc2b6e 100644 --- a/src/common/algorithm.c +++ b/src/common/algorithm.c @@ -46,12 +46,12 @@ bool c11__stable_sort(void* ptr_, if(b_end > ptr + length * elem_size) b_end = ptr + length * elem_size; bool ok = merge(a, a_end, b, b_end, tmp, elem_size, f_lt, extra); if(!ok) { - free(tmp); + PK_FREE(tmp); return false; } memcpy(a, tmp, b_end - a); } } - free(tmp); + PK_FREE(tmp); return true; } diff --git a/src/common/memorypool.c b/src/common/memorypool.c index bf1c212f..5c09861b 100644 --- a/src/common/memorypool.c +++ b/src/common/memorypool.c @@ -169,14 +169,14 @@ static void MemoryPool__shrink_to_fit(MemoryPool* self) { MemoryPoolArena* arena = (MemoryPoolArena*)node; if(MemoryPoolArena__full(arena)) { LinkedList__erase(&self->_arenas, node); - free(arena); + PK_FREE(arena); }); } static void MemoryPool__dtor(MemoryPool* self) { - LinkedList__apply(&self->_arenas, free(node);); - LinkedList__apply(&self->_empty_arenas, free(node);); + LinkedList__apply(&self->_arenas, PK_FREE(node);); + LinkedList__apply(&self->_empty_arenas, PK_FREE(node);); } typedef struct FixedMemoryPool { @@ -205,8 +205,8 @@ static void FixedMemoryPool__ctor(FixedMemoryPool* self, int BlockSize, int Bloc } static void FixedMemoryPool__dtor(FixedMemoryPool* self) { - free(self->_free_list); - free(self->data); + PK_FREE(self->_free_list); + PK_FREE(self->data); } static void* FixedMemoryPool__alloc(FixedMemoryPool* self) { @@ -226,7 +226,7 @@ static void FixedMemoryPool__dealloc(FixedMemoryPool* self, void* p) { self->_free_list_end++; } else { self->exceeded_bytes -= self->BlockSize; - free(p); + PK_FREE(p); } } diff --git a/src/common/str.c b/src/common/str.c index bc7645d4..ca8fbbf7 100644 --- a/src/common/str.c +++ b/src/common/str.c @@ -51,7 +51,7 @@ c11_string* c11_string__copy(c11_string* self) { return retval; } -void c11_string__delete(c11_string* self) { free(self); } +void c11_string__delete(c11_string* self) { PK_FREE(self); } c11_sv c11_string__sv(c11_string* self) { return (c11_sv){self->data, self->size}; } diff --git a/src/common/strname.c b/src/common/strname.c index b586ffbf..8922a37b 100644 --- a/src/common/strname.c +++ b/src/common/strname.c @@ -13,7 +13,7 @@ static c11_vector /*T=char* */ _r_interned; void py_Name__initialize() { c11_smallmap_s2n__ctor(&_interned); for(int i = 0; i < _r_interned.length; i++) { - free(c11__at(char*, &_r_interned, i)); + PK_FREE(c11__at(char*, &_r_interned, i)); } c11_vector__ctor(&_r_interned, sizeof(c11_sv)); @@ -26,7 +26,7 @@ void py_Name__initialize() { void py_Name__finalize() { // free all char* for(int i = 0; i < _r_interned.length; i++) { - free(c11__getitem(char*, &_r_interned, i)); + PK_FREE(c11__getitem(char*, &_r_interned, i)); } c11_smallmap_s2n__dtor(&_interned); c11_vector__dtor(&_r_interned); diff --git a/src/common/vector.c b/src/common/vector.c index af2b2451..f5c23df1 100644 --- a/src/common/vector.c +++ b/src/common/vector.c @@ -3,6 +3,7 @@ #include #include #include "pocketpy/common/utils.h" +#include "pocketpy/config.h" void c11_vector__ctor(c11_vector* self, int elem_size) { self->data = NULL; @@ -12,7 +13,7 @@ void c11_vector__ctor(c11_vector* self, int elem_size) { } void c11_vector__dtor(c11_vector* self) { - if(self->data) free(self->data); + if(self->data) PK_FREE(self->data); self->data = NULL; self->length = 0; self->capacity = 0; diff --git a/src/compiler/compiler.c b/src/compiler/compiler.c index 747faa82..cdae54c2 100644 --- a/src/compiler/compiler.c +++ b/src/compiler/compiler.c @@ -464,7 +464,7 @@ void SequenceExpr__dtor(Expr* self_) { for(int i = 0; i < self->itemCount; i++) { vtdelete(self->items[i]); } - free(self->items); + PK_FREE(self->items); } bool TupleExpr__emit_store(Expr* self_, Ctx* ctx) { @@ -1318,11 +1318,11 @@ static void Compiler__dtor(Compiler* self) { // free tokens for(int i = 0; i < self->tokens_length; i++) { if(self->tokens[i].value.index == TokenValue_STR) { - // free internal string + // PK_FREE internal string c11_string__delete(self->tokens[i].value._str); } } - free(self->tokens); + PK_FREE(self->tokens); // free contexts c11__foreach(Ctx, &self->contexts, ctx) Ctx__dtor(ctx); c11_vector__dtor(&self->contexts); diff --git a/src/interpreter/frame.c b/src/interpreter/frame.c index 07c1830b..cd6e417c 100644 --- a/src/interpreter/frame.c +++ b/src/interpreter/frame.c @@ -35,7 +35,7 @@ UnwindTarget* UnwindTarget__new(UnwindTarget* next, int iblock, int offset) { return self; } -void UnwindTarget__delete(UnwindTarget* self) { free(self); } +void UnwindTarget__delete(UnwindTarget* self) { PK_FREE(self); } Frame* Frame__new(const CodeObject* co, py_GlobalRef module, diff --git a/src/interpreter/typeinfo.c b/src/interpreter/typeinfo.c index 460de30d..361f48e4 100644 --- a/src/interpreter/typeinfo.c +++ b/src/interpreter/typeinfo.c @@ -10,7 +10,7 @@ void TypeList__ctor(TypeList* self) { void TypeList__dtor(TypeList* self) { for (int i = 0; i < self->length; i++) { - if(self->chunks[i]) free(self->chunks[i]); + if(self->chunks[i]) PK_FREE(self->chunks[i]); } } diff --git a/src/interpreter/vm.c b/src/interpreter/vm.c index 904e49c3..5c94ca93 100644 --- a/src/interpreter/vm.c +++ b/src/interpreter/vm.c @@ -572,7 +572,7 @@ void PyObject__delete(PyObject* self) { if(ti->dtor) ti->dtor(PyObject__userdata(self)); if(self->slots == -1) NameDict__dtor(PyObject__dict(self)); if(self->gc_is_large) { - free(self); + PK_FREE(self); } else { PoolObject_dealloc(self); } diff --git a/src/modules/os.c b/src/modules/os.c index 798e1e5e..5de8a018 100644 --- a/src/modules/os.c +++ b/src/modules/os.c @@ -160,7 +160,7 @@ static bool io_FileIO_read(int argc, py_Ref argv) { void* dst = PK_MALLOC(size); int actual_size = fread(dst, 1, size, ud->file); py_newstrv(py_retval(), (c11_sv){dst, actual_size}); - free(dst); + PK_FREE(dst); } return true; } diff --git a/src/modules/pickle.c b/src/modules/pickle.c index 00dfbe33..c19fabfa 100644 --- a/src/modules/pickle.c +++ b/src/modules/pickle.c @@ -48,7 +48,7 @@ static void PickleObject__ctor(PickleObject* self) { } static void PickleObject__dtor(PickleObject* self) { - free(self->used_types); + PK_FREE(self->used_types); c11_smallmap_p2i__dtor(&self->memo); c11_vector__dtor(&self->codes); } diff --git a/src/modules/random.c b/src/modules/random.c index 82bb8212..82660349 100644 --- a/src/modules/random.c +++ b/src/modules/random.c @@ -233,21 +233,21 @@ static bool Random_choices(int argc, py_Ref argv) { py_TValue* w; int wlen = pk_arrayview(weights, &w); if(wlen == -1) { - free(cum_weights); + PK_FREE(cum_weights); return TypeError("choices(): weights must be a list or tuple"); } if(wlen != length) { - free(cum_weights); + PK_FREE(cum_weights); return ValueError("len(weights) != len(population)"); } if(!py_castfloat(&w[0], &cum_weights[0])) { - free(cum_weights); + PK_FREE(cum_weights); return false; } for(int i = 1; i < length; i++) { py_f64 tmp; if(!py_castfloat(&w[i], &tmp)) { - free(cum_weights); + PK_FREE(cum_weights); return false; } cum_weights[i] = cum_weights[i - 1] + tmp; @@ -256,7 +256,7 @@ static bool Random_choices(int argc, py_Ref argv) { py_f64 total = cum_weights[length - 1]; if(total <= 0) { - free(cum_weights); + PK_FREE(cum_weights); return ValueError("total of weights must be greater than zero"); } @@ -269,7 +269,7 @@ static bool Random_choices(int argc, py_Ref argv) { py_list_setitem(py_retval(), i, p + index); } - free(cum_weights); + PK_FREE(cum_weights); return true; } diff --git a/src/modules/traceback.c b/src/modules/traceback.c index 85af9b22..0bbd0246 100644 --- a/src/modules/traceback.c +++ b/src/modules/traceback.c @@ -8,7 +8,7 @@ static bool traceback_format_exc(int argc, py_Ref argv) { py_newnone(py_retval()); } else { py_newstr(py_retval(), s); - free(s); + PK_FREE(s); } return true; } diff --git a/src/objects/namedict.c b/src/objects/namedict.c index 0b258981..5ece63cf 100644 --- a/src/objects/namedict.c +++ b/src/objects/namedict.c @@ -17,11 +17,11 @@ void ModuleDict__ctor(ModuleDict* self, const char* path, py_TValue module) { void ModuleDict__dtor(ModuleDict* self) { if(self->left) { ModuleDict__dtor(self->left); - free(self->left); + PK_FREE(self->left); } if(self->right) { ModuleDict__dtor(self->right); - free(self->right); + PK_FREE(self->right); } } diff --git a/src/public/exec.c b/src/public/exec.c index 54589ce8..de5a09fc 100644 --- a/src/public/exec.c +++ b/src/public/exec.c @@ -30,7 +30,7 @@ bool _py_compile(CodeObject* out, PK_DECREF(src); PK_DECREF(err->src); - free(err); + PK_FREE(err); return false; } PK_DECREF(src); diff --git a/src/public/internal.c b/src/public/internal.c index 286d6a40..7ddf0c5d 100644 --- a/src/public/internal.c +++ b/src/public/internal.c @@ -51,7 +51,7 @@ void py_finalize() { // TODO: refactor VM__ctor and VM__dtor pk_current_vm = vm; VM__dtor(vm); - free(vm); + PK_FREE(vm); } } pk_current_vm = &pk_default_vm; diff --git a/src/public/modules.c b/src/public/modules.c index a05e54f1..7fd13ebd 100644 --- a/src/public/modules.c +++ b/src/public/modules.c @@ -155,7 +155,7 @@ __SUCCESS: c11_string__delete(filename); c11_string__delete(slashed_path); - if(need_free) free((void*)data); + if(need_free) PK_FREE((void*)data); return ok ? 1 : -1; } diff --git a/src/public/py_dict.c b/src/public/py_dict.c index 655e457e..c1720dff 100644 --- a/src/public/py_dict.c +++ b/src/public/py_dict.c @@ -87,7 +87,7 @@ static void Dict__ctor(Dict* self, uint32_t capacity, int entries_capacity) { static void Dict__dtor(Dict* self) { self->length = 0; self->capacity = 0; - free(self->indices); + PK_FREE(self->indices); c11_vector__dtor(&self->entries); } @@ -176,7 +176,7 @@ static void Dict__compact_entries(Dict* self) { self->indices[i]._[j] = mappings[idx]; } } - free(mappings); + PK_FREE(mappings); } static bool Dict__set(Dict* self, py_TValue* key, py_TValue* val) { diff --git a/src/public/py_exception.c b/src/public/py_exception.c index 90e9c2c6..22ab7da8 100644 --- a/src/public/py_exception.c +++ b/src/public/py_exception.c @@ -164,7 +164,7 @@ void py_printexc() { if(!msg) return; pk_current_vm->callbacks.print(msg); pk_current_vm->callbacks.print("\n"); - free(msg); + PK_FREE(msg); } static void c11_sbuf__write_exc(c11_sbuf* self, py_Ref exc) {