WSDeallocator (C 函数)

是一个 WSTP 类型,描述一个指向取 void * 作为自变量的函数的函数指针,并返回实现内存释放器的 void 类型.

更多信息

  • 内存释放器函数必需线程安全.
  • WSTP 的标头文件 wstp.h 已对 WSDeallocator() 进行声明.

范例

基本范例  (1)

#include <stdlib.h>
#include "wstp.h"

/* AppAllocator implements a custom memory allocator */
void * AppAllocator(unsigned long size)
{
    return malloc(size);
}


/* AppDeallocator implements a custom memory deallocator */
void AppDeallocator(void *m)
{
    free(m);
}


int main(int argc, char **argv)
{
    WSENV env;
    WSLINK link;
    WSEnvironmentParameter p;
    int error;

    p = WSNewParameters(WSREVISION, WSAPIREVISION);
    if(p == (WSEnvironmentParameter)0)
    { /* Unable to initialize a parameters object */ }

    WSSetAllocParameter(p, AppAllocator, AppDeallocator);

    env = WSInitialize(p);
    if(env == (WSENV)0)
    { /* unable to initialize the WSTP environment */ }

    WSReleaseParameters(p);

    link = WSOpenArgcArgv(env, argc, argv, &error);
    if(link == (WSLINK)0)
    { /* unable to create the link */ }

    /* ... */

    WSClose(link);
    WSDeinitialize(env);

    return 0;
}