MapleAlloc - Maple Programming Help

Home : Support : Online Help : Programming : OpenMaple : C Application Programming Interface : Memory and Foreign Object Management : OpenMaple/C/MapleAlloc

MapleAlloc

allocate memory in external code

MapleDispose

free memory in external code

 Calling Sequence MapleAlloc(kv, n) MapleDispose(kv, s)

Parameters

 kv - kernel handle of type MKernelVector n - number of bytes to allocate s - type ALGEB object

Description

 • These functions can be used in external code with OpenMaple or define_external.
 • MapleAlloc allocates n bytes of memory and returns a pointer to it.  MapleDispose frees this memory so it can be reused for other purposes.
 • MapleAlloc is used to access temporary memory that will be cleaned up by the Maple garbage collector.  The memory returned by MapleAlloc cannot be protected from gc. To allocate memory that persists, use standard non-Maple memory allocation methods in combination with a MaplePointer.
 • MapleDispose releases the memory allocated to the structure s. It must be used only on data structures created using MapleAlloc.

Examples

 #include #include "maplec.h" ALGEB M_DECL MyConcat( MKernelVector kv, ALGEB *args ) { char *name, *tmp; FLOAT64 f; size_t len; if( MapleNumArgs(kv,(ALGEB)args) != 2 ) { MapleRaiseError(kv,"two arguments expected"); return( NULL ); } name = MapleToString(kv,args[1]); f = MapleToFloat64(kv,args[2]); len = strlen(name); tmp = (char*)MapleAlloc(kv,(len+30)*sizeof(char)); sprintf(tmp,"%s%f",name,f); return( ToMapleName(kv,tmp,FALSE) ); }

Execute the external function from Maple.

 > $\mathrm{with}\left(\mathrm{ExternalCalling}\right):$
 > $\mathrm{dll}≔\mathrm{ExternalLibraryName}\left("HelpExamples"\right):$
 > $\mathrm{fcat}≔\mathrm{DefineExternal}\left("MyConcat",\mathrm{dll}\right):$
 > $l≔\mathrm{fcat}\left(\mathrm{foo},1.1\right)$
 ${l}{≔}{\mathrm{foo1.100000}}$ (1)
 > $\mathrm{assign}\left(\mathrm{eval}\left(l,1\right),17\right)$
 > $l$
 ${17}$ (2)

Note: The name returned is local (it does not match the global name).

 > $\mathrm{foo1.100000}$
 ${\mathrm{foo1.100000}}$ (3)
 > $\mathrm{dismantle}[\mathrm{hex}]\left(\mathrm{eval}\left(l,1\right)\right)$
 NAME(100007F32CA4454D6,5): foo1.100000
 > $\mathrm{dismantle}[\mathrm{hex}]\left(\mathrm{foo1.100000}\right)$
 NAME(100007F32CA447A4E,5): foo1.100000