1
0
Эх сурвалжийг харах

add function wrapper for malloc/free

blueloveTH 7 сар өмнө
parent
commit
9c2b96e572

+ 7 - 0
include/pocketpy/pocketpy.h

@@ -127,6 +127,13 @@ PK_API int py_gc_collect();
 /// Setup the callbacks for the current VM.
 PK_API py_Callbacks* py_callbacks();
 
+/// Wrapper for `PK_MALLOC(size)`.
+PK_API void* py_malloc(size_t size);
+/// Wrapper for `PK_REALLOC(ptr, size)`.
+PK_API void* py_realloc(void* ptr, size_t size);
+/// Wrapper for `PK_FREE(ptr)`.
+PK_API void py_free(void* ptr);
+
 /// Begin the watchdog with `timeout` in milliseconds.
 /// `PK_ENABLE_WATCHDOG` must be defined to `1` to use this feature.
 /// You need to call `py_watchdog_end()` later.

+ 1 - 1
include/pybind11/internal/error.h

@@ -14,7 +14,7 @@ public:
     // get the python  exception object
     object& exception() { return m_exception; }
 
-    ~python_error() { std::free(m_what); }
+    ~python_error() { py_free(m_what); }
 
     bool match(py_Type type) const { return py_isinstance(m_exception.ptr(), type); }
 

+ 12 - 0
src/public/internal.c

@@ -47,6 +47,18 @@ void py_initialize() {
     pk_initialized = true;
 }
 
+void* py_malloc(size_t size) {
+    return PK_MALLOC(size);
+}
+
+void* py_realloc(void* ptr, size_t size) {
+    return PK_REALLOC(ptr, size);
+}
+
+void py_free(void* ptr) {
+    PK_FREE(ptr);
+}
+
 py_GlobalRef py_True() { return &_True; }
 
 py_GlobalRef py_False() { return &_False; }