From 69183e2d792ecc3c3570a9104f0f5bde258d2034 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Mon, 1 Jul 2024 02:22:03 +0800 Subject: [PATCH] some fix --- include/pocketpy/common/any.h | 33 --------------------------- include/pocketpy/common/utils.h | 14 ++++++++++++ include/pocketpy/objects/codeobject.h | 1 - include/pocketpy/objects/sourcedata.h | 1 - src/common/any.c | 7 ------ 5 files changed, 14 insertions(+), 42 deletions(-) delete mode 100644 include/pocketpy/common/any.h delete mode 100644 src/common/any.c diff --git a/include/pocketpy/common/any.h b/include/pocketpy/common/any.h deleted file mode 100644 index 3e7e904b..00000000 --- a/include/pocketpy/common/any.h +++ /dev/null @@ -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 - any(T value){ - c11_userdata__ctor(this, &value, sizeof(T)); - } - - any(){ } - - template - T as(){ - return c11_userdata__as(T, this); - } - }; -} // namespace pkpy -#endif \ No newline at end of file diff --git a/include/pocketpy/common/utils.h b/include/pocketpy/common/utils.h index 4e903fc7..3d25f481 100644 --- a/include/pocketpy/common/utils.h +++ b/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 \ No newline at end of file diff --git a/include/pocketpy/objects/codeobject.h b/include/pocketpy/objects/codeobject.h index c7c487cb..8531ee5c 100644 --- a/include/pocketpy/objects/codeobject.h +++ b/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 diff --git a/include/pocketpy/objects/sourcedata.h b/include/pocketpy/objects/sourcedata.h index 554b0539..b1a88ba0 100644 --- a/include/pocketpy/objects/sourcedata.h +++ b/include/pocketpy/objects/sourcedata.h @@ -3,7 +3,6 @@ #include #include "pocketpy/common/str.h" #include "pocketpy/common/vector.h" -#include "pocketpy/common/refcount.h" #ifdef __cplusplus extern "C" { diff --git a/src/common/any.c b/src/common/any.c deleted file mode 100644 index af22bd94..00000000 --- a/src/common/any.c +++ /dev/null @@ -1,7 +0,0 @@ -#include "pocketpy/common/any.h" - -#include - -void c11_userdata__ctor(c11_userdata* self, void* ptr, int size){ - memcpy(self, ptr, size); -}