mirror of
https://github.com/pocketpy/pocketpy
synced 2025-10-21 20:10:17 +00:00
up
This commit is contained in:
parent
facc873856
commit
4de81edd4a
131
src/memory.h
131
src/memory.h
@ -4,70 +4,6 @@
|
||||
|
||||
namespace pkpy{
|
||||
|
||||
template <typename T>
|
||||
struct shared_ptr {
|
||||
int* counter;
|
||||
|
||||
T* _t() const noexcept { return (T*)(counter + 1); }
|
||||
void _inc_counter() { if(counter) ++(*counter); }
|
||||
void _dec_counter() { if(counter && --(*counter) == 0) {((T*)(counter + 1))->~T(); free(counter);} }
|
||||
|
||||
public:
|
||||
shared_ptr() : counter(nullptr) {}
|
||||
shared_ptr(int* counter) : counter(counter) {}
|
||||
shared_ptr(const shared_ptr& other) : counter(other.counter) {
|
||||
_inc_counter();
|
||||
}
|
||||
shared_ptr(shared_ptr&& other) noexcept : counter(other.counter) {
|
||||
other.counter = nullptr;
|
||||
}
|
||||
~shared_ptr() { _dec_counter(); }
|
||||
|
||||
bool operator==(const shared_ptr& other) const { return counter == other.counter; }
|
||||
bool operator!=(const shared_ptr& other) const { return counter != other.counter; }
|
||||
bool operator<(const shared_ptr& other) const { return counter < other.counter; }
|
||||
bool operator>(const shared_ptr& other) const { return counter > other.counter; }
|
||||
bool operator<=(const shared_ptr& other) const { return counter <= other.counter; }
|
||||
bool operator>=(const shared_ptr& other) const { return counter >= other.counter; }
|
||||
bool operator==(std::nullptr_t) const { return counter == nullptr; }
|
||||
bool operator!=(std::nullptr_t) const { return counter != nullptr; }
|
||||
|
||||
shared_ptr& operator=(const shared_ptr& other) {
|
||||
_dec_counter();
|
||||
counter = other.counter;
|
||||
_inc_counter();
|
||||
return *this;
|
||||
}
|
||||
|
||||
shared_ptr& operator=(shared_ptr&& other) noexcept {
|
||||
_dec_counter();
|
||||
counter = other.counter;
|
||||
other.counter = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
T& operator*() const { return *_t(); }
|
||||
T* operator->() const { return _t(); }
|
||||
T* get() const { return _t(); }
|
||||
|
||||
int use_count() const {
|
||||
return counter ? *counter : 0;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
_dec_counter();
|
||||
counter = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename... Args>
|
||||
shared_ptr<T> make_sp(Args&&... args) {
|
||||
int* p = (int*)malloc(sizeof(int) + sizeof(T));
|
||||
*p = 1;
|
||||
new(p+1) T(std::forward<Args>(args)...);
|
||||
return shared_ptr<T>(p);
|
||||
}
|
||||
|
||||
struct LinkedListNode{
|
||||
LinkedListNode* prev;
|
||||
LinkedListNode* next;
|
||||
@ -301,4 +237,71 @@ inline MemoryPool<64> pool64;
|
||||
inline MemoryPool<128> pool128;
|
||||
// inline MemoryPool<256> pool256;
|
||||
|
||||
#define SP_MALLOC(size) pool64.alloc(size)
|
||||
#define SP_FREE(p) pool64.dealloc(p)
|
||||
|
||||
template <typename T>
|
||||
struct shared_ptr {
|
||||
int* counter;
|
||||
|
||||
T* _t() const noexcept { return (T*)(counter + 1); }
|
||||
void _inc_counter() { if(counter) ++(*counter); }
|
||||
void _dec_counter() { if(counter && --(*counter) == 0) {((T*)(counter + 1))->~T(); SP_FREE(counter);} }
|
||||
|
||||
public:
|
||||
shared_ptr() : counter(nullptr) {}
|
||||
shared_ptr(int* counter) : counter(counter) {}
|
||||
shared_ptr(const shared_ptr& other) : counter(other.counter) {
|
||||
_inc_counter();
|
||||
}
|
||||
shared_ptr(shared_ptr&& other) noexcept : counter(other.counter) {
|
||||
other.counter = nullptr;
|
||||
}
|
||||
~shared_ptr() { _dec_counter(); }
|
||||
|
||||
bool operator==(const shared_ptr& other) const { return counter == other.counter; }
|
||||
bool operator!=(const shared_ptr& other) const { return counter != other.counter; }
|
||||
bool operator<(const shared_ptr& other) const { return counter < other.counter; }
|
||||
bool operator>(const shared_ptr& other) const { return counter > other.counter; }
|
||||
bool operator<=(const shared_ptr& other) const { return counter <= other.counter; }
|
||||
bool operator>=(const shared_ptr& other) const { return counter >= other.counter; }
|
||||
bool operator==(std::nullptr_t) const { return counter == nullptr; }
|
||||
bool operator!=(std::nullptr_t) const { return counter != nullptr; }
|
||||
|
||||
shared_ptr& operator=(const shared_ptr& other) {
|
||||
_dec_counter();
|
||||
counter = other.counter;
|
||||
_inc_counter();
|
||||
return *this;
|
||||
}
|
||||
|
||||
shared_ptr& operator=(shared_ptr&& other) noexcept {
|
||||
_dec_counter();
|
||||
counter = other.counter;
|
||||
other.counter = nullptr;
|
||||
return *this;
|
||||
}
|
||||
|
||||
T& operator*() const { return *_t(); }
|
||||
T* operator->() const { return _t(); }
|
||||
T* get() const { return _t(); }
|
||||
|
||||
int use_count() const {
|
||||
return counter ? *counter : 0;
|
||||
}
|
||||
|
||||
void reset(){
|
||||
_dec_counter();
|
||||
counter = nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
template <typename T, typename... Args>
|
||||
shared_ptr<T> make_sp(Args&&... args) {
|
||||
int* p = (int*)SP_MALLOC(sizeof(int) + sizeof(T));
|
||||
*p = 1;
|
||||
new(p+1) T(std::forward<Args>(args)...);
|
||||
return shared_ptr<T>(p);
|
||||
}
|
||||
|
||||
}; // namespace pkpy
|
||||
|
12
src/obj.h
12
src/obj.h
@ -112,7 +112,11 @@ struct PyObject {
|
||||
virtual void _obj_gc_mark() = 0;
|
||||
|
||||
PyObject(Type type) : type(type) {}
|
||||
virtual ~PyObject() { delete _attr; }
|
||||
virtual ~PyObject() {
|
||||
if(_attr == nullptr) return;
|
||||
_attr->~NameDict();
|
||||
pool64.dealloc(_attr);
|
||||
}
|
||||
};
|
||||
|
||||
template<typename T>
|
||||
@ -127,11 +131,11 @@ struct Py_ : PyObject {
|
||||
|
||||
void _init() noexcept {
|
||||
if constexpr (std::is_same_v<T, Type> || std::is_same_v<T, DummyModule>) {
|
||||
_attr = new NameDict(8, kTypeAttrLoadFactor);
|
||||
_attr = new(pool64.alloc<NameDict>()) NameDict(8, kTypeAttrLoadFactor);
|
||||
}else if constexpr(std::is_same_v<T, DummyInstance>){
|
||||
_attr = new NameDict(8, kInstAttrLoadFactor);
|
||||
_attr = new(pool64.alloc<NameDict>()) NameDict(8, kInstAttrLoadFactor);
|
||||
}else if constexpr(std::is_same_v<T, Function> || std::is_same_v<T, NativeFunc>){
|
||||
_attr = new NameDict(8, kInstAttrLoadFactor);
|
||||
_attr = new(pool64.alloc<NameDict>()) NameDict(8, kInstAttrLoadFactor);
|
||||
}else{
|
||||
_attr = nullptr;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user