commit 82e616a4b672e5265c685aec155f1fe590aa2fdf
parent d82f8bc4a3a4b11c0b2d018eefe665f6d52f64f0
Author: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Date: Thu, 16 Mar 2023 12:33:22 +0800
bind: make use of kernel32 memory allocation
This reduces the reliance on the C code, allowing Go to manage
memory.
Signed-off-by: Jack Mordaunt <jackmordaunt.dev@gmail.com>
Diffstat:
3 files changed, 10 insertions(+), 8 deletions(-)
diff --git a/internal/bind/proc.go b/internal/bind/proc.go
@@ -8,13 +8,15 @@ import (
)
var (
+ kernel32 = windows.NewLazySystemDLL("kernel32.dll")
+ procMalloc = kernel32.NewProc("GlobalAlloc")
+ procFree = kernel32.NewProc("GlobalFree")
+
modcombase = windows.NewLazySystemDLL("combase.dll")
procRegisterClassObject = modcombase.NewProc("RoRegisterClassObject")
toast = windows.NewLazyDLL("toast.dll")
procSetActivationCallback = toast.NewProc("SetActivationCallback")
- procMalloc = toast.NewProc("Malloc")
- procFree = toast.NewProc("Free")
procNewClassFactory = toast.NewProc("NewClassFactory")
procRegisterClassFactory = toast.NewProc("RegisterClassFactory")
)
diff --git a/internal/c-lib/toast.c b/internal/c-lib/toast.c
@@ -46,7 +46,7 @@ STDMETHODCALLTYPE
Impl_IGeneric_Release(Impl_IGeneric* _this)
{
LONG64 dwNewRefCount = InterlockedDecrement64(&(_this->dwRefCount));
- if (!dwNewRefCount) free(_this);
+ if (!dwNewRefCount) GlobalFree(_this);
return dwNewRefCount;
}
@@ -153,7 +153,7 @@ Impl_IClassFactory_CreateInstance(
else
{
BOOL bOk = FALSE;
- if (!(thisobj = malloc(sizeof(Impl_IGeneric)))) hr = E_OUTOFMEMORY;
+ if (!(thisobj = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, sizeof(Impl_IGeneric)))) hr = E_OUTOFMEMORY;
else
{
thisobj->lpVtbl = &Impl_INotificationActivationCallback_Vtbl;
@@ -215,21 +215,21 @@ HRESULT Init(RO_INIT_TYPE roInit) {
// This allows the consumer to handle uninitialization without relying on a third-party.
__declspec(dllexport)
void Deinit() {
- RoUninitialize();
+ RoUninitialize();
}
// Malloc exports memory allocation. This allows the caller to get C memory without importing C.
__declspec(dllexport)
void*
Malloc(size_t size) {
- return malloc(size);
+ return GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, size);
}
// Free exports memory deallocation. This allows the caller to free C memory without importing C.
__declspec(dllexport)
void
Free(void* object) {
- free(object);
+ GlobalFree(object);
}
// NewClassFactory allocates a ClassFactory.
@@ -238,7 +238,7 @@ __declspec(dllexport)
Impl_IGeneric*
NewClassFactory() {
Impl_IGeneric* pClassFactory;
- pClassFactory = malloc(sizeof(Impl_IGeneric));
+ pClassFactory = GlobalAlloc(GMEM_FIXED|GMEM_ZEROINIT, sizeof(Impl_IGeneric));
if (pClassFactory == NULL) {
return NULL;
}
diff --git a/toast.dll b/toast.dll
Binary files differ.