replace all free

This commit is contained in:
blueloveTH 2024-12-31 11:01:51 +08:00
parent fb8e963905
commit 6d2547723f
21 changed files with 38 additions and 37 deletions

View File

@ -45,6 +45,6 @@ typedef struct RefCounted {
do { \ do { \
if(--(obj)->rc.count == 0) { \ if(--(obj)->rc.count == 0) { \
(obj)->rc.dtor(obj); \ (obj)->rc.dtor(obj); \
free(obj); \ PK_FREE(obj); \
} \ } \
} while(0) } while(0)

View File

@ -66,7 +66,7 @@ NAME* METHOD(new)() {
void METHOD(delete)(NAME* self) { void METHOD(delete)(NAME* self) {
METHOD(dtor)(self); METHOD(dtor)(self);
free(self); PK_FREE(self);
} }
void METHOD(set)(NAME* self, K key, V value) { void METHOD(set)(NAME* self, K key, V value) {

View File

@ -46,12 +46,12 @@ bool c11__stable_sort(void* ptr_,
if(b_end > ptr + length * elem_size) b_end = ptr + length * elem_size; 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); bool ok = merge(a, a_end, b, b_end, tmp, elem_size, f_lt, extra);
if(!ok) { if(!ok) {
free(tmp); PK_FREE(tmp);
return false; return false;
} }
memcpy(a, tmp, b_end - a); memcpy(a, tmp, b_end - a);
} }
} }
free(tmp); PK_FREE(tmp);
return true; return true;
} }

View File

@ -169,14 +169,14 @@ static void MemoryPool__shrink_to_fit(MemoryPool* self) {
MemoryPoolArena* arena = (MemoryPoolArena*)node; MemoryPoolArena* arena = (MemoryPoolArena*)node;
if(MemoryPoolArena__full(arena)) { if(MemoryPoolArena__full(arena)) {
LinkedList__erase(&self->_arenas, node); LinkedList__erase(&self->_arenas, node);
free(arena); PK_FREE(arena);
}); });
} }
static void MemoryPool__dtor(MemoryPool* self) { static void MemoryPool__dtor(MemoryPool* self) {
LinkedList__apply(&self->_arenas, free(node);); LinkedList__apply(&self->_arenas, PK_FREE(node););
LinkedList__apply(&self->_empty_arenas, free(node);); LinkedList__apply(&self->_empty_arenas, PK_FREE(node););
} }
typedef struct FixedMemoryPool { typedef struct FixedMemoryPool {
@ -205,8 +205,8 @@ static void FixedMemoryPool__ctor(FixedMemoryPool* self, int BlockSize, int Bloc
} }
static void FixedMemoryPool__dtor(FixedMemoryPool* self) { static void FixedMemoryPool__dtor(FixedMemoryPool* self) {
free(self->_free_list); PK_FREE(self->_free_list);
free(self->data); PK_FREE(self->data);
} }
static void* FixedMemoryPool__alloc(FixedMemoryPool* self) { static void* FixedMemoryPool__alloc(FixedMemoryPool* self) {
@ -226,7 +226,7 @@ static void FixedMemoryPool__dealloc(FixedMemoryPool* self, void* p) {
self->_free_list_end++; self->_free_list_end++;
} else { } else {
self->exceeded_bytes -= self->BlockSize; self->exceeded_bytes -= self->BlockSize;
free(p); PK_FREE(p);
} }
} }

View File

@ -51,7 +51,7 @@ c11_string* c11_string__copy(c11_string* self) {
return retval; 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}; } c11_sv c11_string__sv(c11_string* self) { return (c11_sv){self->data, self->size}; }

View File

@ -13,7 +13,7 @@ static c11_vector /*T=char* */ _r_interned;
void py_Name__initialize() { void py_Name__initialize() {
c11_smallmap_s2n__ctor(&_interned); c11_smallmap_s2n__ctor(&_interned);
for(int i = 0; i < _r_interned.length; i++) { 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)); c11_vector__ctor(&_r_interned, sizeof(c11_sv));
@ -26,7 +26,7 @@ void py_Name__initialize() {
void py_Name__finalize() { void py_Name__finalize() {
// free all char* // free all char*
for(int i = 0; i < _r_interned.length; i++) { 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_smallmap_s2n__dtor(&_interned);
c11_vector__dtor(&_r_interned); c11_vector__dtor(&_r_interned);

View File

@ -3,6 +3,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "pocketpy/common/utils.h" #include "pocketpy/common/utils.h"
#include "pocketpy/config.h"
void c11_vector__ctor(c11_vector* self, int elem_size) { void c11_vector__ctor(c11_vector* self, int elem_size) {
self->data = NULL; self->data = NULL;
@ -12,7 +13,7 @@ void c11_vector__ctor(c11_vector* self, int elem_size) {
} }
void c11_vector__dtor(c11_vector* self) { void c11_vector__dtor(c11_vector* self) {
if(self->data) free(self->data); if(self->data) PK_FREE(self->data);
self->data = NULL; self->data = NULL;
self->length = 0; self->length = 0;
self->capacity = 0; self->capacity = 0;

View File

@ -464,7 +464,7 @@ void SequenceExpr__dtor(Expr* self_) {
for(int i = 0; i < self->itemCount; i++) { for(int i = 0; i < self->itemCount; i++) {
vtdelete(self->items[i]); vtdelete(self->items[i]);
} }
free(self->items); PK_FREE(self->items);
} }
bool TupleExpr__emit_store(Expr* self_, Ctx* ctx) { bool TupleExpr__emit_store(Expr* self_, Ctx* ctx) {
@ -1318,11 +1318,11 @@ static void Compiler__dtor(Compiler* self) {
// free tokens // free tokens
for(int i = 0; i < self->tokens_length; i++) { for(int i = 0; i < self->tokens_length; i++) {
if(self->tokens[i].value.index == TokenValue_STR) { if(self->tokens[i].value.index == TokenValue_STR) {
// free internal string // PK_FREE internal string
c11_string__delete(self->tokens[i].value._str); c11_string__delete(self->tokens[i].value._str);
} }
} }
free(self->tokens); PK_FREE(self->tokens);
// free contexts // free contexts
c11__foreach(Ctx, &self->contexts, ctx) Ctx__dtor(ctx); c11__foreach(Ctx, &self->contexts, ctx) Ctx__dtor(ctx);
c11_vector__dtor(&self->contexts); c11_vector__dtor(&self->contexts);

View File

@ -35,7 +35,7 @@ UnwindTarget* UnwindTarget__new(UnwindTarget* next, int iblock, int offset) {
return self; return self;
} }
void UnwindTarget__delete(UnwindTarget* self) { free(self); } void UnwindTarget__delete(UnwindTarget* self) { PK_FREE(self); }
Frame* Frame__new(const CodeObject* co, Frame* Frame__new(const CodeObject* co,
py_GlobalRef module, py_GlobalRef module,

View File

@ -10,7 +10,7 @@ void TypeList__ctor(TypeList* self) {
void TypeList__dtor(TypeList* self) { void TypeList__dtor(TypeList* self) {
for (int i = 0; i < self->length; i++) { 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]);
} }
} }

View File

@ -572,7 +572,7 @@ void PyObject__delete(PyObject* self) {
if(ti->dtor) ti->dtor(PyObject__userdata(self)); if(ti->dtor) ti->dtor(PyObject__userdata(self));
if(self->slots == -1) NameDict__dtor(PyObject__dict(self)); if(self->slots == -1) NameDict__dtor(PyObject__dict(self));
if(self->gc_is_large) { if(self->gc_is_large) {
free(self); PK_FREE(self);
} else { } else {
PoolObject_dealloc(self); PoolObject_dealloc(self);
} }

View File

@ -160,7 +160,7 @@ static bool io_FileIO_read(int argc, py_Ref argv) {
void* dst = PK_MALLOC(size); void* dst = PK_MALLOC(size);
int actual_size = fread(dst, 1, size, ud->file); int actual_size = fread(dst, 1, size, ud->file);
py_newstrv(py_retval(), (c11_sv){dst, actual_size}); py_newstrv(py_retval(), (c11_sv){dst, actual_size});
free(dst); PK_FREE(dst);
} }
return true; return true;
} }

View File

@ -48,7 +48,7 @@ static void PickleObject__ctor(PickleObject* self) {
} }
static void PickleObject__dtor(PickleObject* self) { static void PickleObject__dtor(PickleObject* self) {
free(self->used_types); PK_FREE(self->used_types);
c11_smallmap_p2i__dtor(&self->memo); c11_smallmap_p2i__dtor(&self->memo);
c11_vector__dtor(&self->codes); c11_vector__dtor(&self->codes);
} }

View File

@ -233,21 +233,21 @@ static bool Random_choices(int argc, py_Ref argv) {
py_TValue* w; py_TValue* w;
int wlen = pk_arrayview(weights, &w); int wlen = pk_arrayview(weights, &w);
if(wlen == -1) { if(wlen == -1) {
free(cum_weights); PK_FREE(cum_weights);
return TypeError("choices(): weights must be a list or tuple"); return TypeError("choices(): weights must be a list or tuple");
} }
if(wlen != length) { if(wlen != length) {
free(cum_weights); PK_FREE(cum_weights);
return ValueError("len(weights) != len(population)"); return ValueError("len(weights) != len(population)");
} }
if(!py_castfloat(&w[0], &cum_weights[0])) { if(!py_castfloat(&w[0], &cum_weights[0])) {
free(cum_weights); PK_FREE(cum_weights);
return false; return false;
} }
for(int i = 1; i < length; i++) { for(int i = 1; i < length; i++) {
py_f64 tmp; py_f64 tmp;
if(!py_castfloat(&w[i], &tmp)) { if(!py_castfloat(&w[i], &tmp)) {
free(cum_weights); PK_FREE(cum_weights);
return false; return false;
} }
cum_weights[i] = cum_weights[i - 1] + tmp; 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]; py_f64 total = cum_weights[length - 1];
if(total <= 0) { if(total <= 0) {
free(cum_weights); PK_FREE(cum_weights);
return ValueError("total of weights must be greater than zero"); 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); py_list_setitem(py_retval(), i, p + index);
} }
free(cum_weights); PK_FREE(cum_weights);
return true; return true;
} }

View File

@ -8,7 +8,7 @@ static bool traceback_format_exc(int argc, py_Ref argv) {
py_newnone(py_retval()); py_newnone(py_retval());
} else { } else {
py_newstr(py_retval(), s); py_newstr(py_retval(), s);
free(s); PK_FREE(s);
} }
return true; return true;
} }

View File

@ -17,11 +17,11 @@ void ModuleDict__ctor(ModuleDict* self, const char* path, py_TValue module) {
void ModuleDict__dtor(ModuleDict* self) { void ModuleDict__dtor(ModuleDict* self) {
if(self->left) { if(self->left) {
ModuleDict__dtor(self->left); ModuleDict__dtor(self->left);
free(self->left); PK_FREE(self->left);
} }
if(self->right) { if(self->right) {
ModuleDict__dtor(self->right); ModuleDict__dtor(self->right);
free(self->right); PK_FREE(self->right);
} }
} }

View File

@ -30,7 +30,7 @@ bool _py_compile(CodeObject* out,
PK_DECREF(src); PK_DECREF(src);
PK_DECREF(err->src); PK_DECREF(err->src);
free(err); PK_FREE(err);
return false; return false;
} }
PK_DECREF(src); PK_DECREF(src);

View File

@ -51,7 +51,7 @@ void py_finalize() {
// TODO: refactor VM__ctor and VM__dtor // TODO: refactor VM__ctor and VM__dtor
pk_current_vm = vm; pk_current_vm = vm;
VM__dtor(vm); VM__dtor(vm);
free(vm); PK_FREE(vm);
} }
} }
pk_current_vm = &pk_default_vm; pk_current_vm = &pk_default_vm;

View File

@ -155,7 +155,7 @@ __SUCCESS:
c11_string__delete(filename); c11_string__delete(filename);
c11_string__delete(slashed_path); c11_string__delete(slashed_path);
if(need_free) free((void*)data); if(need_free) PK_FREE((void*)data);
return ok ? 1 : -1; return ok ? 1 : -1;
} }

View File

@ -87,7 +87,7 @@ static void Dict__ctor(Dict* self, uint32_t capacity, int entries_capacity) {
static void Dict__dtor(Dict* self) { static void Dict__dtor(Dict* self) {
self->length = 0; self->length = 0;
self->capacity = 0; self->capacity = 0;
free(self->indices); PK_FREE(self->indices);
c11_vector__dtor(&self->entries); c11_vector__dtor(&self->entries);
} }
@ -176,7 +176,7 @@ static void Dict__compact_entries(Dict* self) {
self->indices[i]._[j] = mappings[idx]; self->indices[i]._[j] = mappings[idx];
} }
} }
free(mappings); PK_FREE(mappings);
} }
static bool Dict__set(Dict* self, py_TValue* key, py_TValue* val) { static bool Dict__set(Dict* self, py_TValue* key, py_TValue* val) {

View File

@ -164,7 +164,7 @@ void py_printexc() {
if(!msg) return; if(!msg) return;
pk_current_vm->callbacks.print(msg); pk_current_vm->callbacks.print(msg);
pk_current_vm->callbacks.print("\n"); pk_current_vm->callbacks.print("\n");
free(msg); PK_FREE(msg);
} }
static void c11_sbuf__write_exc(c11_sbuf* self, py_Ref exc) { static void c11_sbuf__write_exc(c11_sbuf* self, py_Ref exc) {