From 10842207ea443e474cf10c4801eb8ca53106e0a4 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 2 Jun 2024 17:09:42 +0800 Subject: [PATCH] some fix --- include/pocketpy/common/any.hpp | 3 ++- include/pocketpy/common/namedict.hpp | 8 +++---- include/pocketpy/common/vector.hpp | 28 +++++++++++------------ include/pocketpy/interpreter/bindings.hpp | 2 +- include/pocketpy/interpreter/cffi.hpp | 8 +++---- include/pocketpy/interpreter/frame.hpp | 2 +- include/pocketpy/interpreter/vm.hpp | 1 + include/pocketpy/objects/codeobject.hpp | 2 +- src/common/memorypool.cpp | 10 ++++---- src/common/str.cpp | 10 ++++---- src/interpreter/ceval.cpp | 4 ++-- src/interpreter/cffi.cpp | 10 ++++---- src/interpreter/vm.cpp | 6 ++--- src/modules/base64.cpp | 4 ++-- src/modules/io.cpp | 2 +- src/objects/dict.cpp | 10 ++++---- src/objects/tuplelist.cpp | 4 ++-- src/pocketpy.cpp | 8 +++---- src/pocketpy_c.cpp | 2 +- 19 files changed, 63 insertions(+), 61 deletions(-) diff --git a/include/pocketpy/common/any.hpp b/include/pocketpy/common/any.hpp index 68f02b60..5b14b533 100644 --- a/include/pocketpy/common/any.hpp +++ b/include/pocketpy/common/any.hpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace pkpy { @@ -42,7 +43,7 @@ struct any{ static_assert(!std::is_same_v, "any(const any&) is deleted"); static_assert(sizeof(U) == sizeof(T)); if constexpr (is_any_sso_v){ - memcpy(&data, &value, sizeof(U)); + std::memcpy(&data, &value, sizeof(U)); }else{ data = new U(std::forward(value)); } diff --git a/include/pocketpy/common/namedict.hpp b/include/pocketpy/common/namedict.hpp index cf72c158..a6c088c8 100644 --- a/include/pocketpy/common/namedict.hpp +++ b/include/pocketpy/common/namedict.hpp @@ -44,7 +44,7 @@ while(!_items[i].first.empty()) { \ _set_capacity_and_alloc_items(kInitialCapacity); } - ~NameDictImpl(){ free(_items); } + ~NameDictImpl(){ std::free(_items); } uint16_t size() const { return _size; } uint16_t capacity() const { return _capacity; } @@ -54,8 +54,8 @@ while(!_items[i].first.empty()) { \ _critical_size = val * _load_factor; _mask = val - 1; - _items = (Item*)malloc(_capacity * sizeof(Item)); - memset(_items, 0, _capacity * sizeof(Item)); + _items = (Item*)std::malloc(_capacity * sizeof(Item)); + std::memset(_items, 0, _capacity * sizeof(Item)); } void set(StrName key, T val){ @@ -83,7 +83,7 @@ while(!_items[i].first.empty()) { \ assert(!ok); _items[j] = old_items[i]; } - free(old_items); + std::free(old_items); } T try_get(StrName key) const{ diff --git a/include/pocketpy/common/vector.hpp b/include/pocketpy/common/vector.hpp index 9c841ac0..5ca72a60 100644 --- a/include/pocketpy/common/vector.hpp +++ b/include/pocketpy/common/vector.hpp @@ -4,8 +4,8 @@ #include "pocketpy/common/types.hpp" #include -#include #include +#include namespace pkpy{ @@ -17,14 +17,14 @@ struct array{ using size_type = int; array(): _data(nullptr), _size(0) {} - array(int size): _data((T*)malloc(sizeof(T) * size)), _size(size) {} + array(int size): _data((T*)std::malloc(sizeof(T) * size)), _size(size) {} array(array&& other) noexcept: _data(other._data), _size(other._size) { other._data = nullptr; other._size = 0; } array(const array& other) = delete; array(explicit_copy_t, const array& other) { - _data = (T*)malloc(sizeof(T) * other._size); + _data = (T*)std::malloc(sizeof(T) * other._size); _size = other._size; for(int i=0; i<_size; i++) _data[i] = other._data[i]; } @@ -33,7 +33,7 @@ struct array{ array& operator=(array&& other) noexcept{ if(_data){ std::destroy(begin(), end()); - free(_data); + std::free(_data); } _data = other._data; _size = other._size; @@ -70,7 +70,7 @@ struct array{ ~array() { if(_data){ std::destroy(begin(), end()); - free(_data); + std::free(_data); } } }; @@ -86,7 +86,7 @@ struct vector{ vector(): _data(nullptr), _capacity(0), _size(0) {} vector(int size): - _data((T*)malloc(sizeof(T) * size)), + _data((T*)std::malloc(sizeof(T) * size)), _capacity(size), _size(size) {} vector(vector&& other) noexcept: _data(other._data), _capacity(other._capacity), _size(other._size) { @@ -96,7 +96,7 @@ struct vector{ } vector(const vector& other) = delete; vector(explicit_copy_t, const vector& other): - _data((T*)malloc(sizeof(T) * other._size)), + _data((T*)std::malloc(sizeof(T) * other._size)), _capacity(other._size), _size(other._size) { for(int i=0; i<_size; i++) _data[i] = other._data[i]; } @@ -105,7 +105,7 @@ struct vector{ vector& operator=(vector&& other) noexcept{ if(_data){ std::destroy(begin(), end()); - free(_data); + std::free(_data); } new (this) vector(std::move(other)); return *this; @@ -125,16 +125,16 @@ struct vector{ void reserve(int cap){ if(cap < 4) cap = 4; // minimum capacity if(cap <= capacity()) return; - T* new_data = (T*)malloc(sizeof(T) * cap); + T* new_data = (T*)std::malloc(sizeof(T) * cap); if constexpr(is_trivially_relocatable_v){ - memcpy(new_data, _data, sizeof(T) * _size); + std::memcpy(new_data, _data, sizeof(T) * _size); }else{ for(int i=0; i<_size; i++){ new(&new_data[i]) T(std::move(_data[i])); _data[i].~T(); } } - if(_data) free(_data); + if(_data) std::free(_data); _data = new_data; _capacity = cap; } @@ -218,7 +218,7 @@ struct vector{ ~vector(){ if(_data){ std::destroy(begin(), end()); - free(_data); + std::free(_data); } } }; @@ -312,7 +312,7 @@ namespace pkpy { { if constexpr (std::is_trivially_copyable_v) { - memcpy(dest, src, sizeof(T) * n); + std::memcpy(dest, src, sizeof(T) * n); } else { @@ -327,7 +327,7 @@ namespace pkpy { { if constexpr (is_trivially_relocatable_v) { - memcpy(dest, src, sizeof(T) * n); + std::memcpy(dest, src, sizeof(T) * n); } else { diff --git a/include/pocketpy/interpreter/bindings.hpp b/include/pocketpy/interpreter/bindings.hpp index 62da2dcb..8a044133 100644 --- a/include/pocketpy/interpreter/bindings.hpp +++ b/include/pocketpy/interpreter/bindings.hpp @@ -161,7 +161,7 @@ PyObject* VM::bind_field(PyObject* obj, const char* name, F T::*field){ Struct& s = CAST(Struct&, args[0]); \ if(s.size != sizeof(wT)) vm->ValueError("size mismatch"); \ PyVar obj = vm->new_user_object(); \ - memcpy(&_CAST(wT&, obj), s.p, sizeof(wT)); \ + std::memcpy(&_CAST(wT&, obj), s.p, sizeof(wT)); \ return obj; \ }, {}, BindType::STATICMETHOD); \ vm->bind_func(type, "tostruct", 1, [](VM* vm, ArgsView args){ \ diff --git a/include/pocketpy/interpreter/cffi.hpp b/include/pocketpy/interpreter/cffi.hpp index d122b178..fd9e7456 100644 --- a/include/pocketpy/interpreter/cffi.hpp +++ b/include/pocketpy/interpreter/cffi.hpp @@ -71,17 +71,17 @@ struct Struct{ if(size <= INLINE_SIZE){ p = _inlined; }else{ - p = (char*)malloc(size); + p = (char*)std::malloc(size); } - if(zero_init) memset(p, 0, size); + if(zero_init) std::memset(p, 0, size); } Struct(void* p, int size): Struct(size, false){ - if(p != nullptr) memcpy(this->p, p, size); + if(p != nullptr) std::memcpy(this->p, p, size); } Struct(const Struct& other): Struct(other.p, other.size){} - ~Struct(){ if(p!=_inlined) free(p); } + ~Struct(){ if(p!=_inlined) std::free(p); } static void _register(VM* vm, PyObject* mod, PyObject* type); }; diff --git a/include/pocketpy/interpreter/frame.hpp b/include/pocketpy/interpreter/frame.hpp index 7e9d7666..1e0638c9 100644 --- a/include/pocketpy/interpreter/frame.hpp +++ b/include/pocketpy/interpreter/frame.hpp @@ -45,7 +45,7 @@ struct ValueStack { PyVar& peek(int n){ return _sp[-n]; } PyVar peek(int n) const { return _sp[-n]; } void push(PyVar v){ *_sp++ = v; } - void push(std::nullptr_t) { memset(_sp++, 0, sizeof(PyVar)); } + void push(std::nullptr_t) { std::memset(_sp++, 0, sizeof(PyVar)); } void pop(){ --_sp; } PyVar popx(){ --_sp; return *_sp; } ArgsView view(int n){ return ArgsView(_sp-n, _sp); } diff --git a/include/pocketpy/interpreter/vm.hpp b/include/pocketpy/interpreter/vm.hpp index 7a4a5768..961d1136 100644 --- a/include/pocketpy/interpreter/vm.hpp +++ b/include/pocketpy/interpreter/vm.hpp @@ -7,6 +7,7 @@ #include "pocketpy/objects/builtins.hpp" #include "pocketpy/interpreter/gc.hpp" #include "pocketpy/interpreter/frame.hpp" +#include "pocketpy/interpreter/profiler.hpp" namespace pkpy{ diff --git a/include/pocketpy/objects/codeobject.hpp b/include/pocketpy/objects/codeobject.hpp index 736417ae..8e0a107d 100644 --- a/include/pocketpy/objects/codeobject.hpp +++ b/include/pocketpy/objects/codeobject.hpp @@ -32,7 +32,7 @@ struct Bytecode{ uint16_t arg; void set_signed_arg(int arg){ - if(arg < INT16_MIN || arg > INT16_MAX) throw std::runtime_error("byte.arg overflow"); + assert(arg >= INT16_MIN && arg <= INT16_MAX); this->arg = (int16_t)arg; } diff --git a/src/common/memorypool.cpp b/src/common/memorypool.cpp index 1844836b..6bbfc8ef 100644 --- a/src/common/memorypool.cpp +++ b/src/common/memorypool.cpp @@ -171,11 +171,11 @@ struct MemoryPool{ void* alloc(size_t size){ PK_GLOBAL_SCOPE_LOCK(); #if PK_DEBUG_NO_MEMORY_POOL - return malloc(size); + return std::malloc(size); #endif if(size > __BlockSize){ - void* p = malloc(sizeof(void*) + size); - memset(p, 0, sizeof(void*)); + void* p = std::malloc(sizeof(void*) + size); + std::memset(p, 0, sizeof(void*)); return (char*)p + sizeof(void*); } @@ -195,7 +195,7 @@ struct MemoryPool{ void dealloc(void* p){ PK_GLOBAL_SCOPE_LOCK(); #if PK_DEBUG_NO_MEMORY_POOL - free(p); + std::free(p); return; #endif #if PK_DEBUG_MEMORY_POOL @@ -203,7 +203,7 @@ struct MemoryPool{ #endif Block* block = (Block*)((char*)p - sizeof(void*)); if(block->arena == nullptr){ - free(block); + std::free(block); }else{ Arena* arena = (Arena*)block->arena; if(arena->empty()){ diff --git a/src/common/str.cpp b/src/common/str.cpp index 66a614d6..dfe90c9c 100644 --- a/src/common/str.cpp +++ b/src/common/str.cpp @@ -71,7 +71,7 @@ int utf8len(unsigned char c, bool suppress){ Str::Str(const Str& other): size(other.size), is_ascii(other.is_ascii) { PK_STR_ALLOCATE() - memcpy(data, other.data, size); + std::memcpy(data, other.data, size); data[size] = '\0'; } @@ -107,15 +107,15 @@ int utf8len(unsigned char c, bool suppress){ size = other.size; is_ascii = other.is_ascii; PK_STR_ALLOCATE() - memcpy(data, other.data, size); + std::memcpy(data, other.data, size); data[size] = '\0'; return *this; } Str Str::operator+(const Str& other) const { Str ret(size + other.size, is_ascii && other.is_ascii); - memcpy(ret.data, data, size); - memcpy(ret.data + size, other.data, other.size); + std::memcpy(ret.data, data, size); + std::memcpy(ret.data + size, other.data, other.size); ret.data[ret.size] = '\0'; return ret; } @@ -179,7 +179,7 @@ int utf8len(unsigned char c, bool suppress){ Str Str::substr(int start, int len) const { Str ret(len, is_ascii); - memcpy(ret.data, data + start, len); + std::memcpy(ret.data, data + start, len); ret.data[len] = '\0'; return ret; } diff --git a/src/interpreter/ceval.cpp b/src/interpreter/ceval.cpp index 3f3bc987..a1c0edbf 100644 --- a/src/interpreter/ceval.cpp +++ b/src/interpreter/ceval.cpp @@ -382,8 +382,8 @@ __NEXT_STEP: } DISPATCH() case OP_BUILD_BYTES: { const Str& s = CAST(Str&, TOP()); - unsigned char* p = (unsigned char*)malloc(s.size); - memcpy(p, s.data, s.size); + unsigned char* p = (unsigned char*)std::malloc(s.size); + std::memcpy(p, s.data, s.size); TOP() = VAR(Bytes(p, s.size)); } DISPATCH() case OP_BUILD_TUPLE:{ diff --git a/src/interpreter/cffi.cpp b/src/interpreter/cffi.cpp index 5e8296d1..ecaade5e 100644 --- a/src/interpreter/cffi.cpp +++ b/src/interpreter/cffi.cpp @@ -138,18 +138,18 @@ void add_module_c(VM* vm){ vm->bind_func(mod, "malloc", 1, [](VM* vm, ArgsView args){ i64 size = CAST(i64, args[0]); - return VAR(malloc(size)); + return VAR(std::malloc(size)); }); vm->bind_func(mod, "free", 1, [](VM* vm, ArgsView args){ void* p = CAST(void*, args[0]); - free(p); + std::free(p); return vm->None; }); vm->bind_func(mod, "memset", 3, [](VM* vm, ArgsView args){ void* p = CAST(void*, args[0]); - memset(p, CAST(int, args[1]), CAST(size_t, args[2])); + std::memset(p, CAST(int, args[1]), CAST(size_t, args[2])); return vm->None; }); @@ -157,7 +157,7 @@ void add_module_c(VM* vm){ void* dst = CAST(void*, args[0]); void* src = CAST(void*, args[1]); i64 size = CAST(i64, args[2]); - memcpy(dst, src, size); + std::memcpy(dst, src, size); return vm->None; }); @@ -266,7 +266,7 @@ void add_module_c(VM* vm){ obj_get_t voidp = PK_OBJ_GET(VoidP, args[0]); std::string_view sv = CAST(Str&, args[1]).sv(); char* target = (char*)voidp.ptr; - memcpy(target, sv.data(), sv.size()); + std::memcpy(target, sv.data(), sv.size()); target[sv.size()] = '\0'; return vm->None; }); diff --git a/src/interpreter/vm.cpp b/src/interpreter/vm.cpp index 466428c5..362f589d 100644 --- a/src/interpreter/vm.cpp +++ b/src/interpreter/vm.cpp @@ -393,7 +393,7 @@ namespace pkpy{ } assert(out_size >= 0); source = Str(std::string_view((char*)out, out_size)); - free(out); + std::free(out); }else{ source = it->second; _lazy_modules.erase(it); @@ -994,7 +994,7 @@ void VM::__prepare_py_call(PyVar* buffer, ArgsView args, ArgsView kwargs, const int i = 0; // prepare args - memset(buffer, 0, co->nlocals * sizeof(PyVar)); + std::memset(buffer, 0, co->nlocals * sizeof(PyVar)); for(int index: decl->args) buffer[index] = args[i++]; // prepare kwdefaults for(auto& kv: decl->kwargs) buffer[kv.index] = kv.value; @@ -1085,7 +1085,7 @@ PyVar VM::vectorcall(int ARGC, int KWARGC, bool op_call){ // ^p0 ^p1 ^_sp s_data.reset(_base + co->nlocals); // initialize local variables to PY_NULL - memset(p1, 0, (char*)s_data._sp - (char*)p1); + std::memset(p1, 0, (char*)s_data._sp - (char*)p1); break; case FuncType::EMPTY: if(args.size() != fn.decl->args.size()) TypeError(_S(co->name, "() takes ", fn.decl->args.size(), " positional arguments but ", args.size(), " were given")); diff --git a/src/modules/base64.cpp b/src/modules/base64.cpp index b1106359..8d111955 100644 --- a/src/modules/base64.cpp +++ b/src/modules/base64.cpp @@ -172,7 +172,7 @@ void add_module_base64(VM* vm){ // b64encode vm->bind_func(mod, "b64encode", 1, [](VM* vm, ArgsView args){ Bytes& b = CAST(Bytes&, args[0]); - unsigned char* p = (unsigned char*)malloc(b.size() * 2); + unsigned char* p = (unsigned char*)std::malloc(b.size() * 2); int size = base64_encode((const unsigned char*)b.data(), b.size(), (char*)p); return VAR(Bytes(p, size)); }); @@ -180,7 +180,7 @@ void add_module_base64(VM* vm){ // b64decode vm->bind_func(mod, "b64decode", 1, [](VM* vm, ArgsView args){ Bytes& b = CAST(Bytes&, args[0]); - unsigned char* p = (unsigned char*)malloc(b.size()); + unsigned char* p = (unsigned char*)std::malloc(b.size()); int size = base64_decode((const char*)b.data(), b.size(), p); return VAR(Bytes(p, size)); }); diff --git a/src/modules/io.cpp b/src/modules/io.cpp index fd948f12..8c264267 100644 --- a/src/modules/io.cpp +++ b/src/modules/io.cpp @@ -74,7 +74,7 @@ void FileIO::_register(VM* vm, PyObject* mod, PyObject* type){ }else{ buffer_size = size; } - unsigned char* buffer = (unsigned char*)malloc(buffer_size); + unsigned char* buffer = (unsigned char*)std::malloc(buffer_size); i64 actual_size = io_fread(buffer, 1, buffer_size, io.fp); assert(actual_size <= buffer_size); // in text mode, CR may be dropped, which may cause `actual_size < buffer_size` diff --git a/src/objects/dict.cpp b/src/objects/dict.cpp index 52711469..cfde252d 100644 --- a/src/objects/dict.cpp +++ b/src/objects/dict.cpp @@ -9,7 +9,7 @@ namespace pkpy{ } void Dict::__alloc_items(){ - _items = (Item*)malloc(_capacity * sizeof(Item)); + _items = (Item*)std::malloc(_capacity * sizeof(Item)); for(int i=0; i<_capacity; i++){ _items[i].first = nullptr; _items[i].second = nullptr; @@ -37,8 +37,8 @@ namespace pkpy{ _head_idx = other._head_idx; _tail_idx = other._tail_idx; // copy items - _items = (Item*)malloc(_capacity * sizeof(Item)); - memcpy(_items, other._items, _capacity * sizeof(Item)); + _items = (Item*)std::malloc(_capacity * sizeof(Item)); + std::memcpy(_items, other._items, _capacity * sizeof(Item)); } void Dict::set(VM* vm, PyVar key, PyVar val){ @@ -83,7 +83,7 @@ namespace pkpy{ i = old_items[i].next; } - free(old_items); + std::free(old_items); } @@ -169,6 +169,6 @@ namespace pkpy{ } Dict::~Dict(){ - if(_items) free(_items); + if(_items) std::free(_items); } } // namespace pkpy \ No newline at end of file diff --git a/src/objects/tuplelist.cpp b/src/objects/tuplelist.cpp index 222d1d57..75a3b58a 100644 --- a/src/objects/tuplelist.cpp +++ b/src/objects/tuplelist.cpp @@ -6,7 +6,7 @@ Tuple::Tuple(int n){ if(n <= INLINED_SIZE){ this->_args = _inlined; }else{ - this->_args = (PyVar*)malloc(n * sizeof(PyVar)); + this->_args = (PyVar*)std::malloc(n * sizeof(PyVar)); } this->_size = n; } @@ -34,7 +34,7 @@ Tuple::Tuple(PyVar _0, PyVar _1, PyVar _2): Tuple(3){ _args[2] = _2; } -Tuple::~Tuple(){ if(!is_inlined()) free(_args); } +Tuple::~Tuple(){ if(!is_inlined()) std::free(_args); } List ArgsView::to_list() const{ List ret(size()); diff --git a/src/pocketpy.cpp b/src/pocketpy.cpp index 5fdf70f6..e72ef811 100644 --- a/src/pocketpy.cpp +++ b/src/pocketpy.cpp @@ -703,7 +703,7 @@ void __init_builtins(VM* _vm) { _vm->bind_func(VM::tp_str, "encode", 1, [](VM* vm, ArgsView args) { const Str& self = _CAST(Str&, args[0]); Bytes retval(self.length()); - memcpy(retval.data(), self.data, self.length()); + std::memcpy(retval.data(), self.data, self.length()); return VAR(std::move(retval)); }); @@ -1161,7 +1161,7 @@ void __init_builtins(VM* _vm) { vm->parse_int_slice(s, self.size(), start, stop, step); int guess_max_size = abs(stop - start) / abs(step) + 1; if(guess_max_size > self.size()) guess_max_size = self.size(); - unsigned char* buffer = (unsigned char*)malloc(guess_max_size); + unsigned char* buffer = (unsigned char*)std::malloc(guess_max_size); int j = 0; // actual size PK_SLICE_LOOP(i, start, stop, step) buffer[j++] = self[i]; return VAR(Bytes(buffer, j)); @@ -1175,8 +1175,8 @@ void __init_builtins(VM* _vm) { const Bytes& a = _CAST(Bytes&, _0); const Bytes& b = CAST(Bytes&, _1); Bytes retval(a.size() + b.size()); - memcpy(retval.data(), a.data(), a.size()); - memcpy(retval.data() + a.size(), b.data(), b.size()); + std::memcpy(retval.data(), a.data(), a.size()); + std::memcpy(retval.data() + a.size(), b.data(), b.size()); return VAR(std::move(retval)); }); diff --git a/src/pocketpy_c.cpp b/src/pocketpy_c.cpp index 1eb05162..5c45c385 100644 --- a/src/pocketpy_c.cpp +++ b/src/pocketpy_c.cpp @@ -554,7 +554,7 @@ bool pkpy_vectorcall(pkpy_vm* vm_handle, int argc) { } /*****************************************************************/ void pkpy_free(void* p){ - free(p); + std::free(p); } pkpy_CName pkpy_name(const char* name){