mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-23 04:50:17 +00:00
some fix
This commit is contained in:
parent
2dfb6ed07a
commit
10842207ea
@ -5,6 +5,7 @@
|
||||
#include <typeindex>
|
||||
#include <cassert>
|
||||
#include <utility>
|
||||
#include <cstring>
|
||||
|
||||
namespace pkpy {
|
||||
|
||||
@ -42,7 +43,7 @@ struct any{
|
||||
static_assert(!std::is_same_v<U, any>, "any(const any&) is deleted");
|
||||
static_assert(sizeof(U) == sizeof(T));
|
||||
if constexpr (is_any_sso_v<U>){
|
||||
memcpy(&data, &value, sizeof(U));
|
||||
std::memcpy(&data, &value, sizeof(U));
|
||||
}else{
|
||||
data = new U(std::forward<T>(value));
|
||||
}
|
||||
|
@ -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{
|
||||
|
@ -4,8 +4,8 @@
|
||||
#include "pocketpy/common/types.hpp"
|
||||
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <cassert>
|
||||
#include <memory>
|
||||
|
||||
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<T>){
|
||||
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<T>)
|
||||
{
|
||||
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<T>)
|
||||
{
|
||||
memcpy(dest, src, sizeof(T) * n);
|
||||
std::memcpy(dest, src, sizeof(T) * n);
|
||||
}
|
||||
else
|
||||
{
|
||||
|
@ -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<wT>(); \
|
||||
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){ \
|
||||
|
@ -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);
|
||||
};
|
||||
|
@ -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); }
|
||||
|
@ -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{
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
||||
|
@ -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()){
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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:{
|
||||
|
@ -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> 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;
|
||||
});
|
||||
|
@ -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"));
|
||||
|
@ -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));
|
||||
});
|
||||
|
@ -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`
|
||||
|
@ -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
|
@ -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());
|
||||
|
@ -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));
|
||||
});
|
||||
|
||||
|
@ -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){
|
||||
|
Loading…
x
Reference in New Issue
Block a user