From 9c2b96e572082f31ce39798bc89c9fe1becd7c0b Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sat, 12 Jul 2025 21:42:18 +0800 Subject: [PATCH] add function wrapper for malloc/free --- include/pocketpy/pocketpy.h | 7 +++++++ include/pybind11/internal/error.h | 2 +- src/public/internal.c | 12 ++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/include/pocketpy/pocketpy.h b/include/pocketpy/pocketpy.h index 6ca2c4a7..4f90853e 100644 --- a/include/pocketpy/pocketpy.h +++ b/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. diff --git a/include/pybind11/internal/error.h b/include/pybind11/internal/error.h index 3aa9ad66..086fda0e 100644 --- a/include/pybind11/internal/error.h +++ b/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); } diff --git a/src/public/internal.c b/src/public/internal.c index 8431634d..7590a7f6 100644 --- a/src/public/internal.c +++ b/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; }