WSDeallocator (C Function)

is a WSTP type that describes a function pointer to a function taking a void * as an argument and having a return type of void that implements a memory deallocator.

Details

  • The memory deallocator function must be thread-safe.
  • WSDeallocator() is declared in the WSTP header file wstp.h.

Examples

Basic Examples  (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;
}