replace all malloc

This commit is contained in:
blueloveTH 2024-12-31 10:58:48 +08:00
parent 2ef24dd1d9
commit fb8e963905
21 changed files with 36 additions and 28 deletions

View File

@ -18,6 +18,13 @@
#define PK_GC_MIN_THRESHOLD 16384 #define PK_GC_MIN_THRESHOLD 16384
#endif #endif
// Memory allocation functions
#ifndef PK_MALLOC
#define PK_MALLOC(size) malloc(size)
#define PK_REALLOC(ptr, size) realloc(ptr, size)
#define PK_FREE(ptr) free(ptr)
#endif
/*************** debug settings ***************/ /*************** debug settings ***************/
// Do not edit the following settings unless you know what you are doing // Do not edit the following settings unless you know what you are doing
#define PK_DEBUG_CEVAL_STEP 0 #define PK_DEBUG_CEVAL_STEP 0

View File

@ -59,7 +59,7 @@ void METHOD(ctor)(NAME* self) {
void METHOD(dtor)(NAME* self) { c11_vector__dtor(self); } void METHOD(dtor)(NAME* self) { c11_vector__dtor(self); }
NAME* METHOD(new)() { NAME* METHOD(new)() {
NAME* self = malloc(sizeof(NAME)); NAME* self = PK_MALLOC(sizeof(NAME));
METHOD(ctor)(self); METHOD(ctor)(self);
return self; return self;
} }

View File

@ -1,4 +1,5 @@
#include "pocketpy/common/algorithm.h" #include "pocketpy/common/algorithm.h"
#include "pocketpy/config.h"
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
@ -38,7 +39,7 @@ bool c11__stable_sort(void* ptr_,
int (*f_lt)(const void* a, const void* b, void* extra), int (*f_lt)(const void* a, const void* b, void* extra),
void* extra) { void* extra) {
// merge sort // merge sort
char *ptr = ptr_, *tmp = malloc(length * elem_size); char *ptr = ptr_, *tmp = PK_MALLOC(length * elem_size);
for(int seg = 1; seg < length; seg *= 2) { for(int seg = 1; seg < length; seg *= 2) {
for(char* a = ptr; a < ptr + (length - seg) * elem_size; a += 2 * seg * elem_size) { for(char* a = ptr; a < ptr + (length - seg) * elem_size; a += 2 * seg * elem_size) {
char *b = a + seg * elem_size, *a_end = b, *b_end = b + seg * elem_size; char *b = a + seg * elem_size, *a_end = b, *b_end = b + seg * elem_size;

View File

@ -136,7 +136,7 @@ static void MemoryPool__ctor(MemoryPool* self) {
static void* MemoryPool__alloc(MemoryPool* self) { static void* MemoryPool__alloc(MemoryPool* self) {
MemoryPoolArena* arena; MemoryPoolArena* arena;
if(self->_arenas.length == 0){ if(self->_arenas.length == 0){
arena = malloc(sizeof(MemoryPoolArena)); arena = PK_MALLOC(sizeof(MemoryPoolArena));
MemoryPoolArena__ctor(arena); MemoryPoolArena__ctor(arena);
LinkedList__push_back(&self->_arenas, (LinkedListNode*)arena); LinkedList__push_back(&self->_arenas, (LinkedListNode*)arena);
} else { } else {
@ -195,9 +195,9 @@ static void FixedMemoryPool__ctor(FixedMemoryPool* self, int BlockSize, int Bloc
self->BlockSize = BlockSize; self->BlockSize = BlockSize;
self->BlockCount = BlockCount; self->BlockCount = BlockCount;
self->exceeded_bytes = 0; self->exceeded_bytes = 0;
self->data = malloc(BlockSize * BlockCount); self->data = PK_MALLOC(BlockSize * BlockCount);
self->data_end = self->data + BlockSize * BlockCount; self->data_end = self->data + BlockSize * BlockCount;
self->_free_list = malloc(sizeof(void*) * BlockCount); self->_free_list = PK_MALLOC(sizeof(void*) * BlockCount);
self->_free_list_end = self->_free_list; self->_free_list_end = self->_free_list;
for(int i = 0; i < BlockCount; i++) { for(int i = 0; i < BlockCount; i++) {
self->_free_list[i] = self->data + i * BlockSize; self->_free_list[i] = self->data + i * BlockSize;
@ -215,7 +215,7 @@ static void* FixedMemoryPool__alloc(FixedMemoryPool* self) {
return *self->_free_list_end; return *self->_free_list_end;
} else { } else {
self->exceeded_bytes += self->BlockSize; self->exceeded_bytes += self->BlockSize;
return malloc(self->BlockSize); return PK_MALLOC(self->BlockSize);
} }
} }

View File

@ -39,7 +39,7 @@ SourceData_ SourceData__rcnew(const char* source,
const char* filename, const char* filename,
enum py_CompileMode mode, enum py_CompileMode mode,
bool is_dynamic) { bool is_dynamic) {
SourceData_ self = malloc(sizeof(struct SourceData)); SourceData_ self = PK_MALLOC(sizeof(struct SourceData));
SourceData__ctor(self, source, filename, mode, is_dynamic); SourceData__ctor(self, source, filename, mode, is_dynamic);
self->rc.count = 1; self->rc.count = 1;
self->rc.dtor = (void (*)(void*))SourceData__dtor; self->rc.dtor = (void (*)(void*))SourceData__dtor;

View File

@ -11,7 +11,7 @@
c11_string* c11_string__new(const char* data) { return c11_string__new2(data, strlen(data)); } c11_string* c11_string__new(const char* data) { return c11_string__new2(data, strlen(data)); }
c11_string* c11_string__new2(const char* data, int size) { c11_string* c11_string__new2(const char* data, int size) {
c11_string* retval = malloc(sizeof(c11_string) + size + 1); c11_string* retval = PK_MALLOC(sizeof(c11_string) + size + 1);
c11_string__ctor2(retval, data, size); c11_string__ctor2(retval, data, size);
return retval; return retval;
} }
@ -46,7 +46,7 @@ void c11_string__ctor3(c11_string* self, int size) {
c11_string* c11_string__copy(c11_string* self) { c11_string* c11_string__copy(c11_string* self) {
int total_size = sizeof(c11_string) + self->size + 1; int total_size = sizeof(c11_string) + self->size + 1;
c11_string* retval = malloc(total_size); c11_string* retval = PK_MALLOC(total_size);
memcpy(retval, self, total_size); memcpy(retval, self, total_size);
return retval; return retval;
} }

View File

@ -41,7 +41,7 @@ py_Name py_namev(c11_sv name) {
// generate new index // generate new index
if(_interned.length > 65530) c11__abort("py_Name index overflow"); if(_interned.length > 65530) c11__abort("py_Name index overflow");
// NOTE: we must allocate the string in the heap so iterators are not invalidated // NOTE: we must allocate the string in the heap so iterators are not invalidated
char* p = malloc(name.size + 1); char* p = PK_MALLOC(name.size + 1);
memcpy(p, name.data, name.size); memcpy(p, name.data, name.size);
p[name.size] = '\0'; p[name.size] = '\0';
c11_vector__push(char*, &_r_interned, p); c11_vector__push(char*, &_r_interned, p);

View File

@ -526,7 +526,7 @@ static SequenceExpr* SequenceExpr__new(int line, const ExprVt* vt, int count, Op
self->vt = vt; self->vt = vt;
self->line = line; self->line = line;
self->opcode = opcode; self->opcode = opcode;
self->items = malloc(sizeof(Expr*) * count); self->items = PK_MALLOC(sizeof(Expr*) * count);
self->itemCount = count; self->itemCount = count;
return self; return self;
} }
@ -1359,7 +1359,7 @@ static NameScope name_scope(Compiler* self) {
} }
Error* SyntaxError(Compiler* self, const char* fmt, ...) { Error* SyntaxError(Compiler* self, const char* fmt, ...) {
Error* err = malloc(sizeof(Error)); Error* err = PK_MALLOC(sizeof(Error));
err->src = self->src; err->src = self->src;
PK_INCREF(self->src); PK_INCREF(self->src);
Token* t = self->i == self->tokens_length ? prev() : curr(); Token* t = self->i == self->tokens_length ? prev() : curr();

View File

@ -186,7 +186,7 @@ static bool is_possible_number_char(char c) {
/******************************/ /******************************/
static Error* LexerError(Lexer* self, const char* fmt, ...) { static Error* LexerError(Lexer* self, const char* fmt, ...) {
Error* err = malloc(sizeof(Error)); Error* err = PK_MALLOC(sizeof(Error));
err->src = self->src; err->src = self->src;
PK_INCREF(self->src); PK_INCREF(self->src);
err->lineno = self->current_line; err->lineno = self->current_line;

View File

@ -28,7 +28,7 @@ NameDict* FastLocals__to_namedict(py_TValue* locals, const CodeObject* co) {
} }
UnwindTarget* UnwindTarget__new(UnwindTarget* next, int iblock, int offset) { UnwindTarget* UnwindTarget__new(UnwindTarget* next, int iblock, int offset) {
UnwindTarget* self = malloc(sizeof(UnwindTarget)); UnwindTarget* self = PK_MALLOC(sizeof(UnwindTarget));
self->next = next; self->next = next;
self->iblock = iblock; self->iblock = iblock;
self->offset = offset; self->offset = offset;

View File

@ -95,7 +95,7 @@ PyObject* PyObject__new(py_Type type, int slots, int size) {
self = PoolObject_alloc(); self = PoolObject_alloc();
self->gc_is_large = false; self->gc_is_large = false;
} else { } else {
self = malloc(size); self = PK_MALLOC(size);
self->gc_is_large = true; self->gc_is_large = true;
} }
self->type = type; self->type = type;

View File

@ -26,7 +26,7 @@ py_TypeInfo* TypeList__emplace(TypeList* self){
int offset = self->length & (CHUNK_SIZE - 1); int offset = self->length & (CHUNK_SIZE - 1);
assert(chunk < 256); assert(chunk < 256);
if(self->chunks[chunk] == NULL){ if(self->chunks[chunk] == NULL){
self->chunks[chunk] = malloc(sizeof(py_TypeInfo) * CHUNK_SIZE); self->chunks[chunk] = PK_MALLOC(sizeof(py_TypeInfo) * CHUNK_SIZE);
memset(self->chunks[chunk], 0, sizeof(py_TypeInfo) * CHUNK_SIZE); memset(self->chunks[chunk], 0, sizeof(py_TypeInfo) * CHUNK_SIZE);
} }
self->length++; self->length++;

View File

@ -17,7 +17,7 @@ static char* pk_default_importfile(const char* path) {
fseek(f, 0, SEEK_END); fseek(f, 0, SEEK_END);
long size = ftell(f); long size = ftell(f);
fseek(f, 0, SEEK_SET); fseek(f, 0, SEEK_SET);
char* buffer = malloc(size + 1); char* buffer = PK_MALLOC(size + 1);
size = fread(buffer, 1, size, f); size = fread(buffer, 1, size, f);
buffer[size] = 0; buffer[size] = 0;
fclose(f); fclose(f);

View File

@ -157,7 +157,7 @@ static bool io_FileIO_read(int argc, py_Ref argv) {
int actual_size = fread(dst, 1, size, ud->file); int actual_size = fread(dst, 1, size, ud->file);
py_bytes_resize(py_retval(), actual_size); py_bytes_resize(py_retval(), actual_size);
} else { } else {
void* dst = 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); free(dst);

View File

@ -41,7 +41,7 @@ typedef struct {
static void PickleObject__ctor(PickleObject* self) { static void PickleObject__ctor(PickleObject* self) {
self->used_types_length = pk_current_vm->types.length; self->used_types_length = pk_current_vm->types.length;
self->used_types = malloc(self->used_types_length); self->used_types = PK_MALLOC(self->used_types_length);
memset(self->used_types, 0, self->used_types_length); memset(self->used_types, 0, self->used_types_length);
c11_smallmap_p2i__ctor(&self->memo); c11_smallmap_p2i__ctor(&self->memo);
c11_vector__ctor(&self->codes, sizeof(char)); c11_vector__ctor(&self->codes, sizeof(char));

View File

@ -225,7 +225,7 @@ static bool Random_choices(int argc, py_Ref argv) {
if(!py_checktype(py_arg(3), tp_int)) return false; if(!py_checktype(py_arg(3), tp_int)) return false;
py_i64 k = py_toint(py_arg(3)); py_i64 k = py_toint(py_arg(3));
py_f64* cum_weights = malloc(sizeof(py_f64) * length); py_f64* cum_weights = PK_MALLOC(sizeof(py_f64) * length);
if(py_isnone(weights)) { if(py_isnone(weights)) {
for(int i = 0; i < length; i++) for(int i = 0; i < length; i++)
cum_weights[i] = i + 1; cum_weights[i] = i + 1;

View File

@ -24,7 +24,7 @@ static void FuncDecl__dtor(FuncDecl* self) {
} }
FuncDecl_ FuncDecl__rcnew(SourceData_ src, c11_sv name) { FuncDecl_ FuncDecl__rcnew(SourceData_ src, c11_sv name) {
FuncDecl* self = malloc(sizeof(FuncDecl)); FuncDecl* self = PK_MALLOC(sizeof(FuncDecl));
self->rc.count = 1; self->rc.count = 1;
self->rc.dtor = (void (*)(void*))FuncDecl__dtor; self->rc.dtor = (void (*)(void*))FuncDecl__dtor;
CodeObject__ctor(&self->code, src, name); CodeObject__ctor(&self->code, src, name);

View File

@ -35,14 +35,14 @@ void ModuleDict__set(ModuleDict* self, const char* key, py_TValue val) {
if(self->left) { if(self->left) {
ModuleDict__set(self->left, key, val); ModuleDict__set(self->left, key, val);
} else { } else {
self->left = malloc(sizeof(ModuleDict)); self->left = PK_MALLOC(sizeof(ModuleDict));
ModuleDict__ctor(self->left, key, val); ModuleDict__ctor(self->left, key, val);
} }
} else if(cmp > 0) { } else if(cmp > 0) {
if(self->right) { if(self->right) {
ModuleDict__set(self->right, key, val); ModuleDict__set(self->right, key, val);
} else { } else {
self->right = malloc(sizeof(ModuleDict)); self->right = PK_MALLOC(sizeof(ModuleDict));
ModuleDict__ctor(self->right, key, val); ModuleDict__ctor(self->right, key, val);
} }
} else { } else {

View File

@ -64,7 +64,7 @@ void py_finalize() {
void py_switchvm(int index) { void py_switchvm(int index) {
if(index < 0 || index >= 16) c11__abort("invalid vm index"); if(index < 0 || index >= 16) c11__abort("invalid vm index");
if(!pk_all_vm[index]) { if(!pk_all_vm[index]) {
pk_current_vm = pk_all_vm[index] = malloc(sizeof(VM)); pk_current_vm = pk_all_vm[index] = PK_MALLOC(sizeof(VM));
memset(pk_current_vm, 0, sizeof(VM)); memset(pk_current_vm, 0, sizeof(VM));
VM__ctor(pk_all_vm[index]); VM__ctor(pk_all_vm[index]);
} else { } else {

View File

@ -78,7 +78,7 @@ typedef struct {
static void Dict__ctor(Dict* self, uint32_t capacity, int entries_capacity) { static void Dict__ctor(Dict* self, uint32_t capacity, int entries_capacity) {
self->length = 0; self->length = 0;
self->capacity = capacity; self->capacity = capacity;
self->indices = malloc(self->capacity * sizeof(DictIndex)); self->indices = PK_MALLOC(self->capacity * sizeof(DictIndex));
memset(self->indices, -1, self->capacity * sizeof(DictIndex)); memset(self->indices, -1, self->capacity * sizeof(DictIndex));
c11_vector__ctor(&self->entries, sizeof(DictEntry)); c11_vector__ctor(&self->entries, sizeof(DictEntry));
c11_vector__reserve(&self->entries, entries_capacity); c11_vector__reserve(&self->entries, entries_capacity);
@ -154,7 +154,7 @@ __RETRY:
} }
static void Dict__compact_entries(Dict* self) { static void Dict__compact_entries(Dict* self) {
int* mappings = malloc(self->entries.length * sizeof(int)); int* mappings = PK_MALLOC(self->entries.length * sizeof(int));
int n = 0; int n = 0;
for(int i = 0; i < self->entries.length; i++) { for(int i = 0; i < self->entries.length; i++) {
@ -442,7 +442,7 @@ static bool dict_copy(int argc, py_Ref argv) {
new_dict->length = self->length; new_dict->length = self->length;
new_dict->entries = c11_vector__copy(&self->entries); new_dict->entries = c11_vector__copy(&self->entries);
// copy indices // copy indices
new_dict->indices = malloc(new_dict->capacity * sizeof(DictIndex)); new_dict->indices = PK_MALLOC(new_dict->capacity * sizeof(DictIndex));
memcpy(new_dict->indices, self->indices, new_dict->capacity * sizeof(DictIndex)); memcpy(new_dict->indices, self->indices, new_dict->capacity * sizeof(DictIndex));
return true; return true;
} }

View File

@ -218,7 +218,7 @@ char* py_formatexc() {
} }
c11_string* res = c11_sbuf__submit(&ss); c11_string* res = c11_sbuf__submit(&ss);
char* dup = malloc(res->size + 1); char* dup = PK_MALLOC(res->size + 1);
memcpy(dup, res->data, res->size); memcpy(dup, res->data, res->size);
dup[res->size] = '\0'; dup[res->size] = '\0';
c11_string__delete(res); c11_string__delete(res);