mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-23 21:10:19 +00:00
replace all malloc
This commit is contained in:
parent
2ef24dd1d9
commit
fb8e963905
@ -18,6 +18,13 @@
|
||||
#define PK_GC_MIN_THRESHOLD 16384
|
||||
#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 ***************/
|
||||
// Do not edit the following settings unless you know what you are doing
|
||||
#define PK_DEBUG_CEVAL_STEP 0
|
||||
|
@ -59,7 +59,7 @@ void METHOD(ctor)(NAME* self) {
|
||||
void METHOD(dtor)(NAME* self) { c11_vector__dtor(self); }
|
||||
|
||||
NAME* METHOD(new)() {
|
||||
NAME* self = malloc(sizeof(NAME));
|
||||
NAME* self = PK_MALLOC(sizeof(NAME));
|
||||
METHOD(ctor)(self);
|
||||
return self;
|
||||
}
|
||||
|
@ -1,4 +1,5 @@
|
||||
#include "pocketpy/common/algorithm.h"
|
||||
#include "pocketpy/config.h"
|
||||
#include <string.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),
|
||||
void* extra) {
|
||||
// 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(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;
|
||||
|
@ -136,7 +136,7 @@ static void MemoryPool__ctor(MemoryPool* self) {
|
||||
static void* MemoryPool__alloc(MemoryPool* self) {
|
||||
MemoryPoolArena* arena;
|
||||
if(self->_arenas.length == 0){
|
||||
arena = malloc(sizeof(MemoryPoolArena));
|
||||
arena = PK_MALLOC(sizeof(MemoryPoolArena));
|
||||
MemoryPoolArena__ctor(arena);
|
||||
LinkedList__push_back(&self->_arenas, (LinkedListNode*)arena);
|
||||
} else {
|
||||
@ -195,9 +195,9 @@ static void FixedMemoryPool__ctor(FixedMemoryPool* self, int BlockSize, int Bloc
|
||||
self->BlockSize = BlockSize;
|
||||
self->BlockCount = BlockCount;
|
||||
self->exceeded_bytes = 0;
|
||||
self->data = malloc(BlockSize * BlockCount);
|
||||
self->data = PK_MALLOC(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;
|
||||
for(int i = 0; i < BlockCount; i++) {
|
||||
self->_free_list[i] = self->data + i * BlockSize;
|
||||
@ -215,7 +215,7 @@ static void* FixedMemoryPool__alloc(FixedMemoryPool* self) {
|
||||
return *self->_free_list_end;
|
||||
} else {
|
||||
self->exceeded_bytes += self->BlockSize;
|
||||
return malloc(self->BlockSize);
|
||||
return PK_MALLOC(self->BlockSize);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -39,7 +39,7 @@ SourceData_ SourceData__rcnew(const char* source,
|
||||
const char* filename,
|
||||
enum py_CompileMode mode,
|
||||
bool is_dynamic) {
|
||||
SourceData_ self = malloc(sizeof(struct SourceData));
|
||||
SourceData_ self = PK_MALLOC(sizeof(struct SourceData));
|
||||
SourceData__ctor(self, source, filename, mode, is_dynamic);
|
||||
self->rc.count = 1;
|
||||
self->rc.dtor = (void (*)(void*))SourceData__dtor;
|
||||
|
@ -11,7 +11,7 @@
|
||||
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* retval = malloc(sizeof(c11_string) + size + 1);
|
||||
c11_string* retval = PK_MALLOC(sizeof(c11_string) + size + 1);
|
||||
c11_string__ctor2(retval, data, size);
|
||||
return retval;
|
||||
}
|
||||
@ -46,7 +46,7 @@ void c11_string__ctor3(c11_string* self, int size) {
|
||||
|
||||
c11_string* c11_string__copy(c11_string* self) {
|
||||
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);
|
||||
return retval;
|
||||
}
|
||||
|
@ -41,7 +41,7 @@ py_Name py_namev(c11_sv name) {
|
||||
// generate new index
|
||||
if(_interned.length > 65530) c11__abort("py_Name index overflow");
|
||||
// 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);
|
||||
p[name.size] = '\0';
|
||||
c11_vector__push(char*, &_r_interned, p);
|
||||
|
@ -526,7 +526,7 @@ static SequenceExpr* SequenceExpr__new(int line, const ExprVt* vt, int count, Op
|
||||
self->vt = vt;
|
||||
self->line = line;
|
||||
self->opcode = opcode;
|
||||
self->items = malloc(sizeof(Expr*) * count);
|
||||
self->items = PK_MALLOC(sizeof(Expr*) * count);
|
||||
self->itemCount = count;
|
||||
return self;
|
||||
}
|
||||
@ -1359,7 +1359,7 @@ static NameScope name_scope(Compiler* self) {
|
||||
}
|
||||
|
||||
Error* SyntaxError(Compiler* self, const char* fmt, ...) {
|
||||
Error* err = malloc(sizeof(Error));
|
||||
Error* err = PK_MALLOC(sizeof(Error));
|
||||
err->src = self->src;
|
||||
PK_INCREF(self->src);
|
||||
Token* t = self->i == self->tokens_length ? prev() : curr();
|
||||
|
@ -186,7 +186,7 @@ static bool is_possible_number_char(char c) {
|
||||
|
||||
/******************************/
|
||||
static Error* LexerError(Lexer* self, const char* fmt, ...) {
|
||||
Error* err = malloc(sizeof(Error));
|
||||
Error* err = PK_MALLOC(sizeof(Error));
|
||||
err->src = self->src;
|
||||
PK_INCREF(self->src);
|
||||
err->lineno = self->current_line;
|
||||
|
@ -28,7 +28,7 @@ NameDict* FastLocals__to_namedict(py_TValue* locals, const CodeObject* co) {
|
||||
}
|
||||
|
||||
UnwindTarget* UnwindTarget__new(UnwindTarget* next, int iblock, int offset) {
|
||||
UnwindTarget* self = malloc(sizeof(UnwindTarget));
|
||||
UnwindTarget* self = PK_MALLOC(sizeof(UnwindTarget));
|
||||
self->next = next;
|
||||
self->iblock = iblock;
|
||||
self->offset = offset;
|
||||
|
@ -95,7 +95,7 @@ PyObject* PyObject__new(py_Type type, int slots, int size) {
|
||||
self = PoolObject_alloc();
|
||||
self->gc_is_large = false;
|
||||
} else {
|
||||
self = malloc(size);
|
||||
self = PK_MALLOC(size);
|
||||
self->gc_is_large = true;
|
||||
}
|
||||
self->type = type;
|
||||
|
@ -26,7 +26,7 @@ py_TypeInfo* TypeList__emplace(TypeList* self){
|
||||
int offset = self->length & (CHUNK_SIZE - 1);
|
||||
assert(chunk < 256);
|
||||
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);
|
||||
}
|
||||
self->length++;
|
||||
|
@ -17,7 +17,7 @@ static char* pk_default_importfile(const char* path) {
|
||||
fseek(f, 0, SEEK_END);
|
||||
long size = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
char* buffer = malloc(size + 1);
|
||||
char* buffer = PK_MALLOC(size + 1);
|
||||
size = fread(buffer, 1, size, f);
|
||||
buffer[size] = 0;
|
||||
fclose(f);
|
||||
|
@ -157,7 +157,7 @@ static bool io_FileIO_read(int argc, py_Ref argv) {
|
||||
int actual_size = fread(dst, 1, size, ud->file);
|
||||
py_bytes_resize(py_retval(), actual_size);
|
||||
} else {
|
||||
void* dst = malloc(size);
|
||||
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);
|
||||
|
@ -41,7 +41,7 @@ typedef struct {
|
||||
|
||||
static void PickleObject__ctor(PickleObject* self) {
|
||||
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);
|
||||
c11_smallmap_p2i__ctor(&self->memo);
|
||||
c11_vector__ctor(&self->codes, sizeof(char));
|
||||
|
@ -225,7 +225,7 @@ static bool Random_choices(int argc, py_Ref argv) {
|
||||
if(!py_checktype(py_arg(3), tp_int)) return false;
|
||||
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)) {
|
||||
for(int i = 0; i < length; i++)
|
||||
cum_weights[i] = i + 1;
|
||||
|
@ -24,7 +24,7 @@ static void FuncDecl__dtor(FuncDecl* self) {
|
||||
}
|
||||
|
||||
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.dtor = (void (*)(void*))FuncDecl__dtor;
|
||||
CodeObject__ctor(&self->code, src, name);
|
||||
|
@ -35,14 +35,14 @@ void ModuleDict__set(ModuleDict* self, const char* key, py_TValue val) {
|
||||
if(self->left) {
|
||||
ModuleDict__set(self->left, key, val);
|
||||
} else {
|
||||
self->left = malloc(sizeof(ModuleDict));
|
||||
self->left = PK_MALLOC(sizeof(ModuleDict));
|
||||
ModuleDict__ctor(self->left, key, val);
|
||||
}
|
||||
} else if(cmp > 0) {
|
||||
if(self->right) {
|
||||
ModuleDict__set(self->right, key, val);
|
||||
} else {
|
||||
self->right = malloc(sizeof(ModuleDict));
|
||||
self->right = PK_MALLOC(sizeof(ModuleDict));
|
||||
ModuleDict__ctor(self->right, key, val);
|
||||
}
|
||||
} else {
|
||||
|
@ -64,7 +64,7 @@ void py_finalize() {
|
||||
void py_switchvm(int index) {
|
||||
if(index < 0 || index >= 16) c11__abort("invalid 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));
|
||||
VM__ctor(pk_all_vm[index]);
|
||||
} else {
|
||||
|
@ -78,7 +78,7 @@ typedef struct {
|
||||
static void Dict__ctor(Dict* self, uint32_t capacity, int entries_capacity) {
|
||||
self->length = 0;
|
||||
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));
|
||||
c11_vector__ctor(&self->entries, sizeof(DictEntry));
|
||||
c11_vector__reserve(&self->entries, entries_capacity);
|
||||
@ -154,7 +154,7 @@ __RETRY:
|
||||
}
|
||||
|
||||
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;
|
||||
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->entries = c11_vector__copy(&self->entries);
|
||||
// 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));
|
||||
return true;
|
||||
}
|
||||
|
@ -218,7 +218,7 @@ char* py_formatexc() {
|
||||
}
|
||||
|
||||
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);
|
||||
dup[res->size] = '\0';
|
||||
c11_string__delete(res);
|
||||
|
Loading…
x
Reference in New Issue
Block a user