blueloveTH 1 年之前
父節點
當前提交
10842207ea

+ 2 - 1
include/pocketpy/common/any.hpp

@@ -5,6 +5,7 @@
 #include <typeindex>
 #include <typeindex>
 #include <cassert>
 #include <cassert>
 #include <utility>
 #include <utility>
+#include <cstring>
 
 
 namespace pkpy {
 namespace pkpy {
 
 
@@ -42,7 +43,7 @@ struct any{
         static_assert(!std::is_same_v<U, any>, "any(const any&) is deleted");
         static_assert(!std::is_same_v<U, any>, "any(const any&) is deleted");
         static_assert(sizeof(U) == sizeof(T));
         static_assert(sizeof(U) == sizeof(T));
         if constexpr (is_any_sso_v<U>){
         if constexpr (is_any_sso_v<U>){
-            memcpy(&data, &value, sizeof(U));
+            std::memcpy(&data, &value, sizeof(U));
         }else{
         }else{
             data = new U(std::forward<T>(value));
             data = new U(std::forward<T>(value));
         }
         }

+ 4 - 4
include/pocketpy/common/namedict.hpp

@@ -44,7 +44,7 @@ while(!_items[i].first.empty()) {           \
         _set_capacity_and_alloc_items(kInitialCapacity);
         _set_capacity_and_alloc_items(kInitialCapacity);
     }
     }
 
 
-    ~NameDictImpl(){ free(_items); }
+    ~NameDictImpl(){ std::free(_items); }
 
 
     uint16_t size() const { return _size; }
     uint16_t size() const { return _size; }
     uint16_t capacity() const { return _capacity; }
     uint16_t capacity() const { return _capacity; }
@@ -54,8 +54,8 @@ while(!_items[i].first.empty()) {           \
         _critical_size = val * _load_factor;
         _critical_size = val * _load_factor;
         _mask = val - 1;
         _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){
     void set(StrName key, T val){
@@ -83,7 +83,7 @@ while(!_items[i].first.empty()) {           \
             assert(!ok);
             assert(!ok);
             _items[j] = old_items[i];
             _items[j] = old_items[i];
         }
         }
-        free(old_items);
+        std::free(old_items);
     }
     }
 
 
     T try_get(StrName key) const{
     T try_get(StrName key) const{

+ 14 - 14
include/pocketpy/common/vector.hpp

@@ -4,8 +4,8 @@
 #include "pocketpy/common/types.hpp"
 #include "pocketpy/common/types.hpp"
 
 
 #include <cstring>
 #include <cstring>
-#include <memory>
 #include <cassert>
 #include <cassert>
+#include <memory>
 
 
 namespace pkpy{
 namespace pkpy{
 
 
@@ -17,14 +17,14 @@ struct array{
     using size_type = int;
     using size_type = int;
 
 
     array(): _data(nullptr), _size(0) {}
     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) {
     array(array&& other) noexcept: _data(other._data), _size(other._size) {
         other._data = nullptr;
         other._data = nullptr;
         other._size = 0;
         other._size = 0;
     }
     }
     array(const array& other) = delete;
     array(const array& other) = delete;
     array(explicit_copy_t, const array& other) {
     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;
         _size = other._size;
         for(int i=0; i<_size; i++) _data[i] = other._data[i];
         for(int i=0; i<_size; i++) _data[i] = other._data[i];
     }
     }
@@ -33,7 +33,7 @@ struct array{
     array& operator=(array&& other) noexcept{
     array& operator=(array&& other) noexcept{
         if(_data){
         if(_data){
             std::destroy(begin(), end());
             std::destroy(begin(), end());
-            free(_data);
+            std::free(_data);
         }
         }
         _data = other._data;
         _data = other._data;
         _size = other._size;
         _size = other._size;
@@ -70,7 +70,7 @@ struct array{
     ~array() {
     ~array() {
         if(_data){
         if(_data){
             std::destroy(begin(), end());
             std::destroy(begin(), end());
-            free(_data);
+            std::free(_data);
         }
         }
     }
     }
 };
 };
@@ -86,7 +86,7 @@ struct vector{
 
 
     vector(): _data(nullptr), _capacity(0), _size(0) {}
     vector(): _data(nullptr), _capacity(0), _size(0) {}
     vector(int size):
     vector(int size):
-        _data((T*)malloc(sizeof(T) * size)),
+        _data((T*)std::malloc(sizeof(T) * size)),
         _capacity(size), _size(size) {}
         _capacity(size), _size(size) {}
     vector(vector&& other) noexcept:
     vector(vector&& other) noexcept:
         _data(other._data), _capacity(other._capacity), _size(other._size) {
         _data(other._data), _capacity(other._capacity), _size(other._size) {
@@ -96,7 +96,7 @@ struct vector{
     }
     }
     vector(const vector& other) = delete;
     vector(const vector& other) = delete;
     vector(explicit_copy_t, const vector& other):
     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) {
         _capacity(other._size), _size(other._size) {
         for(int i=0; i<_size; i++) _data[i] = other._data[i];
         for(int i=0; i<_size; i++) _data[i] = other._data[i];
     }
     }
@@ -105,7 +105,7 @@ struct vector{
     vector& operator=(vector&& other) noexcept{
     vector& operator=(vector&& other) noexcept{
         if(_data){
         if(_data){
             std::destroy(begin(), end());
             std::destroy(begin(), end());
-            free(_data);
+            std::free(_data);
         }
         }
         new (this) vector(std::move(other));
         new (this) vector(std::move(other));
         return *this;
         return *this;
@@ -125,16 +125,16 @@ struct vector{
     void reserve(int cap){
     void reserve(int cap){
         if(cap < 4) cap = 4;    // minimum capacity
         if(cap < 4) cap = 4;    // minimum capacity
         if(cap <= capacity()) return;
         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>){
         if constexpr(is_trivially_relocatable_v<T>){
-            memcpy(new_data, _data, sizeof(T) * _size);
+            std::memcpy(new_data, _data, sizeof(T) * _size);
         }else{
         }else{
             for(int i=0; i<_size; i++){
             for(int i=0; i<_size; i++){
                 new(&new_data[i]) T(std::move(_data[i]));
                 new(&new_data[i]) T(std::move(_data[i]));
                 _data[i].~T();
                 _data[i].~T();
             }
             }
         }
         }
-        if(_data) free(_data);
+        if(_data) std::free(_data);
         _data = new_data;
         _data = new_data;
         _capacity = cap;
         _capacity = cap;
     }
     }
@@ -218,7 +218,7 @@ struct vector{
     ~vector(){
     ~vector(){
         if(_data){
         if(_data){
             std::destroy(begin(), end());
             std::destroy(begin(), end());
-            free(_data);
+            std::free(_data);
         }
         }
     }
     }
 };
 };
@@ -312,7 +312,7 @@ namespace pkpy {
         {
         {
             if constexpr (std::is_trivially_copyable_v<T>)
             if constexpr (std::is_trivially_copyable_v<T>)
             {
             {
-                memcpy(dest, src, sizeof(T) * n);
+                std::memcpy(dest, src, sizeof(T) * n);
             }
             }
             else
             else
             {
             {
@@ -327,7 +327,7 @@ namespace pkpy {
         {
         {
             if constexpr (is_trivially_relocatable_v<T>)
             if constexpr (is_trivially_relocatable_v<T>)
             {
             {
-                memcpy(dest, src, sizeof(T) * n);
+                std::memcpy(dest, src, sizeof(T) * n);
             }
             }
             else
             else
             {
             {

+ 1 - 1
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]);                                     \
             Struct& s = CAST(Struct&, args[0]);                                     \
             if(s.size != sizeof(wT)) vm->ValueError("size mismatch");               \
             if(s.size != sizeof(wT)) vm->ValueError("size mismatch");               \
             PyVar obj = vm->new_user_object<wT>();                                  \
             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;                                                             \
             return obj;                                                             \
         }, {}, BindType::STATICMETHOD);                                             \
         }, {}, BindType::STATICMETHOD);                                             \
         vm->bind_func(type, "tostruct", 1, [](VM* vm, ArgsView args){               \
         vm->bind_func(type, "tostruct", 1, [](VM* vm, ArgsView args){               \

+ 4 - 4
include/pocketpy/interpreter/cffi.hpp

@@ -71,17 +71,17 @@ struct Struct{
         if(size <= INLINE_SIZE){
         if(size <= INLINE_SIZE){
             p = _inlined;
             p = _inlined;
         }else{
         }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){
     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(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);
     static void _register(VM* vm, PyObject* mod, PyObject* type);
 };
 };

+ 1 - 1
include/pocketpy/interpreter/frame.hpp

@@ -45,7 +45,7 @@ struct ValueStack {
     PyVar& peek(int n){ return _sp[-n]; }
     PyVar& peek(int n){ return _sp[-n]; }
     PyVar peek(int n) const { return _sp[-n]; }
     PyVar peek(int n) const { return _sp[-n]; }
     void push(PyVar v){ *_sp++ = v; }
     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; }
     void pop(){ --_sp; }
     PyVar popx(){ --_sp; return *_sp; }
     PyVar popx(){ --_sp; return *_sp; }
     ArgsView view(int n){ return ArgsView(_sp-n, _sp); }
     ArgsView view(int n){ return ArgsView(_sp-n, _sp); }

+ 1 - 0
include/pocketpy/interpreter/vm.hpp

@@ -7,6 +7,7 @@
 #include "pocketpy/objects/builtins.hpp"
 #include "pocketpy/objects/builtins.hpp"
 #include "pocketpy/interpreter/gc.hpp"
 #include "pocketpy/interpreter/gc.hpp"
 #include "pocketpy/interpreter/frame.hpp"
 #include "pocketpy/interpreter/frame.hpp"
+#include "pocketpy/interpreter/profiler.hpp"
 
 
 namespace pkpy{
 namespace pkpy{
 
 

+ 1 - 1
include/pocketpy/objects/codeobject.hpp

@@ -32,7 +32,7 @@ struct Bytecode{
     uint16_t arg;
     uint16_t arg;
 
 
     void set_signed_arg(int 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;
         this->arg = (int16_t)arg;
     }
     }
 
 

+ 5 - 5
src/common/memorypool.cpp

@@ -171,11 +171,11 @@ struct MemoryPool{
     void* alloc(size_t size){
     void* alloc(size_t size){
         PK_GLOBAL_SCOPE_LOCK();
         PK_GLOBAL_SCOPE_LOCK();
 #if PK_DEBUG_NO_MEMORY_POOL
 #if PK_DEBUG_NO_MEMORY_POOL
-        return malloc(size);
+        return std::malloc(size);
 #endif
 #endif
         if(size > __BlockSize){
         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*);
             return (char*)p + sizeof(void*);
         }
         }
 
 
@@ -195,7 +195,7 @@ struct MemoryPool{
     void dealloc(void* p){
     void dealloc(void* p){
         PK_GLOBAL_SCOPE_LOCK();
         PK_GLOBAL_SCOPE_LOCK();
 #if PK_DEBUG_NO_MEMORY_POOL
 #if PK_DEBUG_NO_MEMORY_POOL
-        free(p);
+        std::free(p);
         return;
         return;
 #endif
 #endif
 #if PK_DEBUG_MEMORY_POOL
 #if PK_DEBUG_MEMORY_POOL
@@ -203,7 +203,7 @@ struct MemoryPool{
 #endif
 #endif
         Block* block = (Block*)((char*)p - sizeof(void*));
         Block* block = (Block*)((char*)p - sizeof(void*));
         if(block->arena == nullptr){
         if(block->arena == nullptr){
-            free(block);
+            std::free(block);
         }else{
         }else{
             Arena* arena = (Arena*)block->arena;
             Arena* arena = (Arena*)block->arena;
             if(arena->empty()){
             if(arena->empty()){

+ 5 - 5
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) {
     Str::Str(const Str& other): size(other.size), is_ascii(other.is_ascii) {
         PK_STR_ALLOCATE()
         PK_STR_ALLOCATE()
-        memcpy(data, other.data, size);
+        std::memcpy(data, other.data, size);
         data[size] = '\0';
         data[size] = '\0';
     }
     }
 
 
@@ -107,15 +107,15 @@ int utf8len(unsigned char c, bool suppress){
         size = other.size;
         size = other.size;
         is_ascii = other.is_ascii;
         is_ascii = other.is_ascii;
         PK_STR_ALLOCATE()
         PK_STR_ALLOCATE()
-        memcpy(data, other.data, size);
+        std::memcpy(data, other.data, size);
         data[size] = '\0';
         data[size] = '\0';
         return *this;
         return *this;
     }
     }
 
 
     Str Str::operator+(const Str& other) const {
     Str Str::operator+(const Str& other) const {
         Str ret(size + other.size, is_ascii && other.is_ascii);
         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';
         ret.data[ret.size] = '\0';
         return ret;
         return ret;
     }
     }
@@ -179,7 +179,7 @@ int utf8len(unsigned char c, bool suppress){
 
 
     Str Str::substr(int start, int len) const {
     Str Str::substr(int start, int len) const {
         Str ret(len, is_ascii);
         Str ret(len, is_ascii);
-        memcpy(ret.data, data + start, len);
+        std::memcpy(ret.data, data + start, len);
         ret.data[len] = '\0';
         ret.data[len] = '\0';
         return ret;
         return ret;
     }
     }

+ 2 - 2
src/interpreter/ceval.cpp

@@ -382,8 +382,8 @@ __NEXT_STEP:
     } DISPATCH()
     } DISPATCH()
     case OP_BUILD_BYTES: {
     case OP_BUILD_BYTES: {
         const Str& s = CAST(Str&, TOP());
         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));
         TOP() = VAR(Bytes(p, s.size));
     } DISPATCH()
     } DISPATCH()
     case OP_BUILD_TUPLE:{
     case OP_BUILD_TUPLE:{

+ 5 - 5
src/interpreter/cffi.cpp

@@ -138,18 +138,18 @@ void add_module_c(VM* vm){
     
     
     vm->bind_func(mod, "malloc", 1, [](VM* vm, ArgsView args){
     vm->bind_func(mod, "malloc", 1, [](VM* vm, ArgsView args){
         i64 size = CAST(i64, args[0]);
         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){
     vm->bind_func(mod, "free", 1, [](VM* vm, ArgsView args){
         void* p = CAST(void*, args[0]);
         void* p = CAST(void*, args[0]);
-        free(p);
+        std::free(p);
         return vm->None;
         return vm->None;
     });
     });
 
 
     vm->bind_func(mod, "memset", 3, [](VM* vm, ArgsView args){
     vm->bind_func(mod, "memset", 3, [](VM* vm, ArgsView args){
         void* p = CAST(void*, args[0]);
         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;
         return vm->None;
     });
     });
 
 
@@ -157,7 +157,7 @@ void add_module_c(VM* vm){
         void* dst = CAST(void*, args[0]);
         void* dst = CAST(void*, args[0]);
         void* src = CAST(void*, args[1]);
         void* src = CAST(void*, args[1]);
         i64 size = CAST(i64, args[2]);
         i64 size = CAST(i64, args[2]);
-        memcpy(dst, src, size);
+        std::memcpy(dst, src, size);
         return vm->None;
         return vm->None;
     });
     });
 
 
@@ -266,7 +266,7 @@ void add_module_c(VM* vm){
         obj_get_t<VoidP> voidp = PK_OBJ_GET(VoidP, args[0]);
         obj_get_t<VoidP> voidp = PK_OBJ_GET(VoidP, args[0]);
         std::string_view sv = CAST(Str&, args[1]).sv();
         std::string_view sv = CAST(Str&, args[1]).sv();
         char* target = (char*)voidp.ptr;
         char* target = (char*)voidp.ptr;
-        memcpy(target, sv.data(), sv.size());
+        std::memcpy(target, sv.data(), sv.size());
         target[sv.size()] = '\0';
         target[sv.size()] = '\0';
         return vm->None;
         return vm->None;
     });
     });

+ 3 - 3
src/interpreter/vm.cpp

@@ -393,7 +393,7 @@ namespace pkpy{
             }
             }
             assert(out_size >= 0);
             assert(out_size >= 0);
             source = Str(std::string_view((char*)out, out_size));
             source = Str(std::string_view((char*)out, out_size));
-            free(out);
+            std::free(out);
         }else{
         }else{
             source = it->second;
             source = it->second;
             _lazy_modules.erase(it);
             _lazy_modules.erase(it);
@@ -994,7 +994,7 @@ void VM::__prepare_py_call(PyVar* buffer, ArgsView args, ArgsView kwargs, const
 
 
     int i = 0;
     int i = 0;
     // prepare args
     // 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++];
     for(int index: decl->args) buffer[index] = args[i++];
     // prepare kwdefaults
     // prepare kwdefaults
     for(auto& kv: decl->kwargs) buffer[kv.index] = kv.value;
     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
                 //      ^p0                    ^p1      ^_sp
                 s_data.reset(_base + co->nlocals);
                 s_data.reset(_base + co->nlocals);
                 // initialize local variables to PY_NULL
                 // 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;
                 break;
             case FuncType::EMPTY:
             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"));
                 if(args.size() != fn.decl->args.size()) TypeError(_S(co->name, "() takes ", fn.decl->args.size(), " positional arguments but ", args.size(), " were given"));

+ 2 - 2
src/modules/base64.cpp

@@ -172,7 +172,7 @@ void add_module_base64(VM* vm){
     // b64encode
     // b64encode
     vm->bind_func(mod, "b64encode", 1, [](VM* vm, ArgsView args){
     vm->bind_func(mod, "b64encode", 1, [](VM* vm, ArgsView args){
         Bytes& b = CAST(Bytes&, args[0]);
         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);
         int size = base64_encode((const unsigned char*)b.data(), b.size(), (char*)p);
         return VAR(Bytes(p, size));
         return VAR(Bytes(p, size));
     });
     });
@@ -180,7 +180,7 @@ void add_module_base64(VM* vm){
     // b64decode
     // b64decode
     vm->bind_func(mod, "b64decode", 1, [](VM* vm, ArgsView args){
     vm->bind_func(mod, "b64decode", 1, [](VM* vm, ArgsView args){
         Bytes& b = CAST(Bytes&, args[0]);
         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);
         int size = base64_decode((const char*)b.data(), b.size(), p);
         return VAR(Bytes(p, size));
         return VAR(Bytes(p, size));
     });
     });

+ 1 - 1
src/modules/io.cpp

@@ -74,7 +74,7 @@ void FileIO::_register(VM* vm, PyObject* mod, PyObject* type){
         }else{
         }else{
             buffer_size = size;
             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);
         i64 actual_size = io_fread(buffer, 1, buffer_size, io.fp);
         assert(actual_size <= buffer_size);
         assert(actual_size <= buffer_size);
         // in text mode, CR may be dropped, which may cause `actual_size < buffer_size`
         // in text mode, CR may be dropped, which may cause `actual_size < buffer_size`

+ 5 - 5
src/objects/dict.cpp

@@ -9,7 +9,7 @@ namespace pkpy{
     }
     }
 
 
     void Dict::__alloc_items(){
     void Dict::__alloc_items(){
-        _items = (Item*)malloc(_capacity * sizeof(Item));
+        _items = (Item*)std::malloc(_capacity * sizeof(Item));
         for(int i=0; i<_capacity; i++){
         for(int i=0; i<_capacity; i++){
             _items[i].first = nullptr;
             _items[i].first = nullptr;
             _items[i].second = nullptr;
             _items[i].second = nullptr;
@@ -37,8 +37,8 @@ namespace pkpy{
         _head_idx = other._head_idx;
         _head_idx = other._head_idx;
         _tail_idx = other._tail_idx;
         _tail_idx = other._tail_idx;
         // copy items
         // 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){
     void Dict::set(VM* vm, PyVar key, PyVar val){
@@ -83,7 +83,7 @@ namespace pkpy{
             i = old_items[i].next;
             i = old_items[i].next;
         }
         }
 
 
-        free(old_items);
+        std::free(old_items);
     }
     }
 
 
 
 
@@ -169,6 +169,6 @@ namespace pkpy{
     }
     }
 
 
     Dict::~Dict(){
     Dict::~Dict(){
-        if(_items) free(_items);
+        if(_items) std::free(_items);
     }
     }
 }   // namespace pkpy
 }   // namespace pkpy

+ 2 - 2
src/objects/tuplelist.cpp

@@ -6,7 +6,7 @@ Tuple::Tuple(int n){
     if(n <= INLINED_SIZE){
     if(n <= INLINED_SIZE){
         this->_args = _inlined;
         this->_args = _inlined;
     }else{
     }else{
-        this->_args = (PyVar*)malloc(n * sizeof(PyVar));
+        this->_args = (PyVar*)std::malloc(n * sizeof(PyVar));
     }
     }
     this->_size = n;
     this->_size = n;
 }
 }
@@ -34,7 +34,7 @@ Tuple::Tuple(PyVar _0, PyVar _1, PyVar _2): Tuple(3){
     _args[2] = _2;
     _args[2] = _2;
 }
 }
 
 
-Tuple::~Tuple(){ if(!is_inlined()) free(_args); }
+Tuple::~Tuple(){ if(!is_inlined()) std::free(_args); }
 
 
 List ArgsView::to_list() const{
 List ArgsView::to_list() const{
     List ret(size());
     List ret(size());

+ 4 - 4
src/pocketpy.cpp

@@ -703,7 +703,7 @@ void __init_builtins(VM* _vm) {
     _vm->bind_func(VM::tp_str, "encode", 1, [](VM* vm, ArgsView args) {
     _vm->bind_func(VM::tp_str, "encode", 1, [](VM* vm, ArgsView args) {
         const Str& self = _CAST(Str&, args[0]);
         const Str& self = _CAST(Str&, args[0]);
         Bytes retval(self.length());
         Bytes retval(self.length());
-        memcpy(retval.data(), self.data, self.length());
+        std::memcpy(retval.data(), self.data, self.length());
         return VAR(std::move(retval));
         return VAR(std::move(retval));
     });
     });
 
 
@@ -1161,7 +1161,7 @@ void __init_builtins(VM* _vm) {
             vm->parse_int_slice(s, self.size(), start, stop, step);
             vm->parse_int_slice(s, self.size(), start, stop, step);
             int guess_max_size = abs(stop - start) / abs(step) + 1;
             int guess_max_size = abs(stop - start) / abs(step) + 1;
             if(guess_max_size > self.size()) guess_max_size = self.size();
             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
             int j = 0;      // actual size
             PK_SLICE_LOOP(i, start, stop, step) buffer[j++] = self[i];
             PK_SLICE_LOOP(i, start, stop, step) buffer[j++] = self[i];
             return VAR(Bytes(buffer, j));
             return VAR(Bytes(buffer, j));
@@ -1175,8 +1175,8 @@ void __init_builtins(VM* _vm) {
         const Bytes& a = _CAST(Bytes&, _0);
         const Bytes& a = _CAST(Bytes&, _0);
         const Bytes& b = CAST(Bytes&, _1);
         const Bytes& b = CAST(Bytes&, _1);
         Bytes retval(a.size() + b.size());
         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));
         return VAR(std::move(retval));
     });
     });
 
 

+ 1 - 1
src/pocketpy_c.cpp

@@ -554,7 +554,7 @@ bool pkpy_vectorcall(pkpy_vm* vm_handle, int argc) {
 }
 }
 /*****************************************************************/
 /*****************************************************************/
 void pkpy_free(void* p){
 void pkpy_free(void* p){
-    free(p);
+    std::free(p);
 }
 }
 
 
 pkpy_CName pkpy_name(const char* name){
 pkpy_CName pkpy_name(const char* name){