Server API

Gliimly can be used in extended-mode, where non-Gliimly code or libraries can be linked with your application.

Such code can be from a library (see --llflag and --cflag options in gg), or can be written directly as C code, i.e. files with .c and .h extension together with your Gliimly application. To do this, use call-extended statement.

Any function with C linkage can be used provided:
When allocating strings in extended code, you must use Gliimly memory management functions. These functions are based on standard C library (such as malloc or free), but are not compatible with them because Gliimly manages such memory on top of the standard C library.

The functions you can use are:

You can use gg_malloc(), gg_calloc() and gg_realloc() to create new Gliimly-compatible memory - and assuming you have set the last byte of any such memory to a null byte, the resulting memory will be properly sized for Gliimly usage.

If you have memory that's already provided from elsewhere, you can use gg_strdup() or gg_strdupl() to create a copy of it that's compatible with Gliimly.

If Gliimly memory you created with these functions has extra unused bytes, you can use either gg_realloc() to reduce its footprint, or you can use gg_mem_set_len() to set its length.

Note that if you use C code included with a Gliimly project, you must include "gliim.h" file in each of them. You do not need to manually include any other ".h" files (header files), as they will be automatically picked up.
Examples
Place the following files in a separate directory for demonstration purposes.

In this example, "example.gliim" will use C functions from "example.c", and "example.h" will have declarations of those functions. File "example.c" implements a factorial function, as well as a function that will store the factorial result in an output message that's allocated and passed back to your Gliimly code:
 #include "gliim.h"

 void get_factorial(gg_num f, gg_num *res)
 {
     *res = 1;
     gg_num i;
     for (i = 2; i <= f; i++) {
         *res *= i;
     }
 }

 #define MEMSIZE 200
 void fact_msg (gg_num i, char **res)
 {
     // gliim rule: outgoing string must NOT free or realloc its incoming value
     // all else is allowed
     char *r = gg_malloc (MEMSIZE);
     gg_num f;
     get_factorial (i, &f);
     gg_num bw = snprintf(r, MEMSIZE, "Factorial value (message from C function) is %ld", f) + 1;
     // reduce memory footprint to match the memory used (including the null byte)
     // you can also use gg_mem_set_len() with bw as length for higher performance 
     // (but also higher memory usage)
     *res = gg_realloc (gg_mem_get_id(r), bw);
 }

File "example.h" declares the above functions:
 void get_factorial(gg_num f, gg_num *res);
 void fact_msg (gg_num i, char **res);

File "example.gliim" will call the above functions and display the results:
 extended-mode

 begin-handler /example

     set-number fact
     call-extended get_factorial (10, &fact)
     @Factorial is <<p-num fact>>

     set-string res
     call-extended fact_msg (10, &res)
     p-out res
     @
 end-handler

Create application "example":
sudo mgrg -i -u $(whoami) example

Make the application:
gg -q

Run it:
gg -r --req="/example" --exec --silent

The output is, as expected:
Factorial is 3628800
Factorial value (message from C function) is 362880

See also
API
Client-API  
Server-API  
See all
documentation


Copyright (c) 2019-2024 Gliim LLC. All contents on this web site is "AS IS" without warranties or guarantees of any kind.