Server API

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

Such code can be from a library (see --llflag and --cflag options in rim), or can be written directly as C code, i.e. files with .c and .h extension together with your RimStone 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 RimStone memory management functions. These functions are based on standard C library (such as malloc or free), but are not compatible with them because RimStone manages such memory on top of the standard C library.

The functions you can use are:

You can use rim_malloc(), rim_calloc() and rim_realloc() to create new RimStone-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 RimStone usage.

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

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

Note that if you use C code included with a RimStone project, you must include "rim.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.rim" 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 RimStone code:
#include "rim.h"

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

#define MEMSIZE 200
void fact_msg (rim_num i, char **res)
{
    // rimstone rule: outgoing string must NOT free or realloc its incoming value
    // all else is allowed
    char *r = rim_malloc (MEMSIZE);
    rim_num f;
    get_factorial (i, &f);
    rim_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 rim_mem_set_len() with bw as length for better performance 
    // (but also higher memory usage)
    *res = rim_realloc (rim_mem_get_id(r), bw);
}

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

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

begin-handler /example public

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

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

Create application "example":
rim -k example

Make the application:
rim -q

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

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

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


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