add function wrapper for malloc/free

This commit is contained in:
blueloveTH 2025-07-12 21:42:18 +08:00
parent 39958a92d2
commit 9c2b96e572
3 changed files with 20 additions and 1 deletions

View File

@ -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.

View File

@ -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); }

View File

@ -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; }