blueloveTH 1 год назад
Родитель
Сommit
69183e2d79

+ 0 - 33
include/pocketpy/common/any.h

@@ -1,33 +0,0 @@
-#pragma once
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-typedef struct c11_userdata{
-    void* _0;
-    void* _1;
-} c11_userdata;
-
-void c11_userdata__ctor(c11_userdata* self, void* ptr, int size);
-#define c11_userdata__as(T, self) (*( (T*)(self) ))
-
-#ifdef __cplusplus
-}
-
-namespace pkpy{
-    struct any: c11_userdata{
-        template<typename T>
-        any(T value){
-            c11_userdata__ctor(this, &value, sizeof(T));
-        }
-
-        any(){ }
-
-        template<typename T>
-        T as(){
-            return c11_userdata__as(T, this);
-        }
-    };
-}   // namespace pkpy
-#endif

+ 14 - 0
include/pocketpy/common/utils.h

@@ -40,6 +40,20 @@ extern const char* kPlatformStrings[];
 #define PK_NARGS(...) PK_NARGS_SEQ(__VA_ARGS__, 4, 3, 2, 1, 0)
 #define PK_NPTRS(...) PK_NARGS_SEQ(__VA_ARGS__, int****, int***, int**, int*, int)
 
+// ref counting
+typedef struct RefCounted {
+    int count;
+    void (*dtor)(void*);
+} RefCounted;
+
+#define PK_INCREF(obj) (obj)->rc.count++
+#define PK_DECREF(obj) do { \
+    if(--(obj)->rc.count == 0) { \
+        (obj)->rc.dtor(obj); \
+        free(obj); \
+    } \
+} while(0)
+
 #ifdef __cplusplus
 }
 #endif

+ 0 - 1
include/pocketpy/objects/codeobject.h

@@ -7,7 +7,6 @@
 #include "pocketpy/common/smallmap.h"
 #include "pocketpy/objects/base.h"
 #include "pocketpy/objects/sourcedata.h"
-#include "pocketpy/common/refcount.h"
 #include "pocketpy/pocketpy.h"
 
 #ifdef __cplusplus

+ 0 - 1
include/pocketpy/objects/sourcedata.h

@@ -3,7 +3,6 @@
 #include <stdbool.h>
 #include "pocketpy/common/str.h"
 #include "pocketpy/common/vector.h"
-#include "pocketpy/common/refcount.h"
 
 #ifdef __cplusplus
 extern "C" {

+ 0 - 7
src/common/any.c

@@ -1,7 +0,0 @@
-#include "pocketpy/common/any.h"
-
-#include <string.h>
-
-void c11_userdata__ctor(c11_userdata* self, void* ptr, int size){
-    memcpy(self, ptr, size);
-}