Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Integrating a C library (zlib)

This is a guide to integrating C libraries in Acton code. We will use the zlib compression library, written in C, to build an Acton module that supports zlib compression and decompression.

We will only focus on the inflate and deflate functions in zlib. They are pure functions (meaning they only take some input and return some output, they do not have any side effects like writing to some shared state), that makes them easier to integrate than anything that does I/O. While zlib does expose functions to interact with files, we don't want to reimplement file related functionality since we already have this supported by the Acton stdlib.

Create new project

Let's start by making a new Acton project, let's call it acton-zlib. New projects are created with an example "Hello world" app. Let's remove it and start from scratch.

acton new acton-zlib
cd acton-zlib
rm src/*

Acton's low level build system - the Zig build system

The Acton compiler parses .act source code, runs through all its compilation passes with type checking, CPS conversion, lambda lifting etc and finally produces C code. Internally, Acton then uses the Zig build system to compile the generated C code to libraries and finally binary executables.

To add a C library dependency, it first needs to be buildable using the Zig build system, which means that it needs a build.zig file, the config file for the Zig build, somewhat similar to the CMakeLists.txt of CMake. Some projects have already adopted a build.zig in the upstream repo, like PCRE2 and the Boehm-Demers-Weiser GC (both of which are used by Acton). In some cases, there are forks of projects with build.zig added. Otherwise you will need to write one for yourself, which is usually simpler than it might first seem.

Add the zlib C library as a Zig dependency

In the case of zlib, there is already a repo available with a build.zig for zlib. Navigate to the Tags page, find 1.3.1 and the link to the source files, i.e. https://github.com/allyourcodebase/zlib/archive/refs/tags/1.3.1.tar.gz.

Add it to our acton-zlib project:

acton zig-pkg add https://github.com/allyourcodebase/zlib/archive/refs/tags/1.3.1.tar.gz zlib --artifact z

Note the --artifact z which is provided to instruct which library to link with. Headers from the zlib library, like zlib.h, will now become visible to C files in our project and the z library will be linked in with our executables. The easiest way to discover what the artifacts are called is by inspecting the build.zig file of the package. This particular zlib build.zig starts like this:

const std = @import("std");

pub fn build(b: *std.Build) void {
    const upstream = b.dependency("zlib", .{});
    const lib = b.addStaticLibrary(.{
        .name = "z",
        .target = b.standardTargetOptions(.{}),
        .optimize = b.standardOptimizeOption(.{}),
    });
    lib.linkLibC();
    lib.addCSourceFiles(.{
        .root = upstream.path(""),
        .files = &.{
            "adler32.c",
            "crc32.c",
...

It is the .name argument to addStaticLibrary that tells us the name of the artifact. Zig packages might expose multiple such artifacts, as is the case for mbedtls.

acton zig-pkg add will fetch the package from the provided URL and save the hash sum to Build.act, resulting in:

dependencies = {}

zig_dependencies = {
  "zlib": (
        url="https://github.com/allyourcodebase/zlib/archive/refs/tags/1.3.1.tar.gz",
        hash="122034ab2a12adf8016ffa76e48b4be3245ffd305193edba4d83058adbcfa749c107",
        artifacts=["z"]
    ),
}

Create zlib.act Acton module

Next up we need to create the Acton zlib module. Open src/zlib.act and add a compress and decompress function:

pure def compress(data: bytes) -> bytes:
    NotImplemented

pure def decompress(data: bytes) -> bytes:
    NotImplemented

The NotImplemented statement tells the compiler that the implementation is not written in Acton but rather external. When there is a .ext.c file, the compiler expects it to contain the implementations for the NotImplemented functions. Also note the explicit types. Normally the Acton compiler can infer types, but since there is no Acton code here, only C code, there is nothing to infer from.

Now create src/zlib.ext.c, which is where we will write the actual implementation of these functions. The file must always define an __ext_init__ function. The module's generated initialization function calls it once, when the module is loaded.

__ext_init__ is for setting up the C state that the module's C functions rely on, for example initializing a C library (libxml2 needs xmlInitParser()) or seeding a random number generator. It must not depend on Acton code: do not call functions or methods written in Acton, from this module or from any other module. __ext_init__ runs at the very start of the module's initialization, before the module initializes its imports, its classes and its module-level values. Acton code called from it could therefore find any of these not yet initialized. The rule applies only to __ext_init__. The C functions that implement the NotImplemented declarations are called from Acton code and always run after it.

There is nothing to set up for zlib, so let's just create an empty function, like so:

void zlibQ___ext_init__() {}

Next, we need to fill in the C functions that map to the Acton functions compress and decompress. By invoking acton build we can get the compiler to generate a skeleton for these. We will also get a large error message, since there is no actual implementation:

user@host$ acton build
... some large error message

Ignore the error and instead check the content of out/types/zlib.c and we will find the C functions we need, commented out:

#include "rts/common.h"
#include "out/types/zlib.h"
#include "src/zlib.ext.c"
B_bytes zlibQ_compress (B_bytes data);
/*
B_bytes zlibQ_compress (B_bytes data) {
    // NotImplemented
}
*/
B_bytes zlibQ_decompress (B_bytes data);
/*
B_bytes zlibQ_decompress (B_bytes data) {
    // NotImplemented
}
*/
int zlibQ_done$ = 0;
void zlibQ___init__ () {
    if (zlibQ_done$) return;
    zlibQ_done$ = 1;
    zlibQ___ext_init__ ();
}

Copy the commented-out skeleton into our own src/zlib.ext.c. Just in order to get something that compiles, let's just quickly let the functions return the input data. Since both input and output are bytes, this should now compile (and work at run time).

B_bytes zlibQ_compress (B_bytes data) {
    return data;
}
B_bytes zlibQ_decompress (B_bytes data) {
    return data;
}
user@host:~/acton-zlib$ acton build
Building project in /Users/user/acton-zlib
  Compiling zlib.act for release
   Finished compilation in   0.005 s
  Compiling test_zlib.act for release
   Finished compilation in   0.019 s
  Final compilation step
user@host:~/acton-zlib$

Add a test module

Before we implement the body of the compress and decompress functions, we can write a small test module which will tell us when we've succeeded. We use some pre-known test data (which we could get from another language implementation):

import testing
import zlib

def _test_roundtrip():
    for x in range(100):
        i = "hello".encode()
        c = zlib.compress(i)
        d = zlib.decompress(c)
        testing.assertEqual(i, d)

def _test_compress():
    for x in range(100):
        i = "hello".encode()
        c = zlib.compress(i)
        testing.assertEqual(c, b'x\x9c\xcbH\xcd\xc9\xc9\x07\x00\x06,\x02\x15')

def _test_decompress():
    for x in range(1000):
        c = b'x\x9c\xcbH\xcd\xc9\xc9\x07\x00\x06,\x02\x15'
        d = zlib.decompress(c)
        testing.assertEqual(d, b'hello')

Note how we run a few test iterations to get slightly better timing measurements for performance testing. Run the test with acton test:

user@host:~/acton-zlib$ acton test

Tests - module test_zlib:
  decompress:            FAIL:  195 runs in 50.728ms
    testing.NotEqualError: Expected equal values but they are non-equal. A: b'x\x9c\xcbH\xcd\xc9\xc9\x07\x00\x06,\x02\x15' B: b'hello'
  compress:              FAIL:  197 runs in 50.886ms
    testing.NotEqualError: Expected equal values but they are non-equal. A: b'hello' B: b'x\x9c\xcbH\xcd\xc9\xc9\x07\x00\x06,\x02\x15'
  roundtrip:             OK:  226 runs in 50.890ms

2 out of 3 tests failed (26.354s)

user@host:~/acton-zlib$ 

As expected, the roundtrip test goes through, since we just return the input data while the compress and decompress tests fail.

Implement the compress function

Now let's fill in the rest of the owl. Below is the body of the zlibQ_compress function. The bulk of this code is not particularly interesting to this guide as it has more to do with standard C usage of zlib, but a few things are worth noting.

#include <zlib.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>

static struct B_ValueError zlibQ_output_too_long_error =
    STATIC_EXCEPTION(B_ValueError, "Zlib output is too long");
static struct B_MemoryError zlibQ_allocation_failed_error =
    STATIC_EXCEPTION(B_MemoryError, "Unable to allocate zlib output");

B_bytes zlibQ_compress(B_bytes data) {
    if (data->nbytes == 0) {
        return data;
    }

    // Prepare the zlib stream
    z_stream stream;
    memset(&stream, 0, sizeof(stream));
    int ret = deflateInit(&stream, Z_DEFAULT_COMPRESSION);
    if (ret != Z_OK) {
        RAISE(B_ValueError, $FORMAT("Unable to compress data, init error: %d", ret));
    }

    stream.next_in = (Bytef *)data->str;
    int64_t input_left = data->nbytes;
    int64_t output_size = 64 * 1024;
    int64_t output_len = 0;
    Bytef *output_buffer = acton_malloc_atomic(output_size);
    if (output_buffer == NULL) {
        deflateEnd(&stream);
        RAISE_EXC(&zlibQ_allocation_failed_error);
    }

    // zlib's per-call counts are uInt, and total_out can wrap on Windows.
    // Feed bounded chunks and keep the full output length ourselves.
    while (1) {
        if (stream.avail_in == 0) {
            stream.avail_in = input_left > UINT_MAX ? UINT_MAX : (uInt)input_left;
            input_left -= stream.avail_in;
        }
        if (output_len == output_size) {
            if (output_size == MAX_STR_LEN) {
                deflateEnd(&stream);
                RAISE_EXC(&zlibQ_output_too_long_error);
            }
            output_size = output_size > MAX_STR_LEN / 2 ? MAX_STR_LEN : output_size * 2;
            output_buffer = acton_realloc(output_buffer, output_size);
            if (output_buffer == NULL) {
                deflateEnd(&stream);
                RAISE_EXC(&zlibQ_allocation_failed_error);
            }
        }
        int64_t output_left = output_size - output_len;
        uInt output_chunk = output_left > UINT_MAX ? UINT_MAX : (uInt)output_left;
        stream.avail_out = output_chunk;
        stream.next_out = output_buffer + output_len;

        ret = deflate(&stream, input_left == 0 ? Z_FINISH : Z_NO_FLUSH);
        output_len += output_chunk - stream.avail_out;
        if (ret == Z_STREAM_END) {
            break;
        }
        if (ret != Z_OK) {
            deflateEnd(&stream);
            RAISE(B_ValueError, $FORMAT("Unable to compress data, error: %d", ret));
        }
    }

    deflateEnd(&stream);
    return actBytesFromCStringLength((char *)output_buffer, output_len);
}

Memory management is always top of mind when writing C, as it the case here. We can allocate memory via the Acton GC-heap malloc or just plain malloc() (the non-GC heap, to be explicit). Since zlibQ_compress is pure, we have no state leaking out of the function other than via its return value. All return values must be allocated on the Acton GC heap, so we know we must use acton_malloc for any value that we return. Any other local variables within the function can use classic malloc, as long as we make sure to explicitly free it up. For class or actor methods, any allocation for class or actor attributes must be performed using the Acton GC malloc, since there is no destructor or similar where a free can be inserted, so using classic malloc would be bound to leak. Also note that in this particular case, we know that the returned bytes value itself is not going to contain any pointers, so by using acton_malloc_atomic we can get a chunk of memory that will not be internally scanned by the GC, which saves a bit of time and thus improves GC performance. If we allocate structs that do carry pointers, they must use the normal acton_malloc(). Storing pointers into such memory later needs a write barrier, see Storing pointers into GC memory.

Acton's own boxed scalars (int, float, complex and the fixed-width integer types) are allocated pointer-free in the same way, as are ranges and the digits of bigint values. So an integer that holds the address of an object does not keep the object alive. Keep a pointer in a cptr instead, see Keeping a C pointer in an Acton value.

actBytesFromCStringLength((char *)output_buffer, output_len) returns exactly the bytes zlib wrote, including any embedded NUL bytes.

Also note how we convert Zlib errors to Acton exceptions where necessary.

Running the test, the compress test now passes while roundtrip has stopped working (since decompress is not implemented yet):

user@host:~/acton-zlib$ acton test
Tests - module test_zlib:
  decompress:            FAIL:  158 runs in 50.175ms
    testing.NotEqualError: Expected equal values but they are non-equal. A: b'x\x9c\xcbH\xcd\xc9\xc9\x07\x00\x06,\x02\x15' B: b'hello'
  compress:              OK:  167 runs in 50.225ms
  roundtrip:             FAIL:  147 runs in 50.266ms
    testing.NotEqualError: Expected equal values but they are non-equal. A: b'hello' B: b'x\x9c\xcbH\xcd\xc9\xc9\x07\x00\x06,\x02\x15'

2 out of 3 tests failed (0.941s)

user@host:~/acton-zlib$ 

Implement the decompress function

Much like the compress function, the decompress function mostly relates to how zlib itself and its interface works. We use the same wrappers and transform errors to exceptions.

B_bytes zlibQ_decompress(B_bytes data) {
    if (data->nbytes == 0) {
        return data;
    }

    // Prepare the zlib stream
    z_stream stream;
    memset(&stream, 0, sizeof(stream));
    int ret = inflateInit(&stream);
    if (ret != Z_OK) {
        RAISE(B_ValueError, $FORMAT("Unable to decompress data, init error: %d", ret));
    }

    stream.next_in = (Bytef *)data->str;
    int64_t input_left = data->nbytes;
    int64_t output_size = 64 * 1024;
    int64_t output_len = 0;
    Bytef *output_buffer = acton_malloc_atomic(output_size);
    if (output_buffer == NULL) {
        inflateEnd(&stream);
        RAISE_EXC(&zlibQ_allocation_failed_error);
    }

    // Keep the full output length independently of zlib's per-call counts.
    while (1) {
        if (stream.avail_in == 0) {
            stream.avail_in = input_left > UINT_MAX ? UINT_MAX : (uInt)input_left;
            input_left -= stream.avail_in;
        }
        if (output_len == output_size) {
            if (output_size == MAX_STR_LEN) {
                inflateEnd(&stream);
                RAISE_EXC(&zlibQ_output_too_long_error);
            }
            output_size = output_size > MAX_STR_LEN / 2 ? MAX_STR_LEN : output_size * 2;
            output_buffer = acton_realloc(output_buffer, output_size);
            if (output_buffer == NULL) {
                inflateEnd(&stream);
                RAISE_EXC(&zlibQ_allocation_failed_error);
            }
        }
        int64_t output_left = output_size - output_len;
        uInt output_chunk = output_left > UINT_MAX ? UINT_MAX : (uInt)output_left;
        stream.avail_out = output_chunk;
        stream.next_out = output_buffer + output_len;

        ret = inflate(&stream, Z_NO_FLUSH);
        output_len += output_chunk - stream.avail_out;
        if (ret == Z_STREAM_END) {
            break;
        }
        if (ret != Z_OK) {
            inflateEnd(&stream);
            RAISE(B_ValueError, $FORMAT("Unable to decompress data, error: %d", ret));
        }
    }

    inflateEnd(&stream);
    return actBytesFromCStringLength((char *)output_buffer, output_len);
}

Final test

user@host:~/acton-zlib$ acton test

Tests - module test_zlib:
  decompress:            OK:   42 runs in 51.065ms
  compress:              OK:   25 runs in 50.032ms
  roundtrip:             OK:   24 runs in 50.053ms

All 3 tests passed (0.738s)

user@host:~/acton-zlib$ 

And with that, we're done! A simple wrapper around zlib, which is also available on GitHub if you want to study it further.

Storing pointers into GC memory

In generational mode (ACTON_GC_MODE=generational, see build options) a minor collection does not scan the whole heap. It rescans only the marked objects of the heap blocks that the program recorded as written since the previous collection, through a write barrier. Generated Acton code has the barrier built in. C code that stores a pointer to GC memory into GC memory must add it itself; a missing barrier lets the collector free an object that is still in use. The macros are in rts/wbar.h, which rts/common.h includes:

  1. After storing a GC pointer v into a field of a GC object obj, also a cptr or ?cptr field, call ACTON_WBAR(obj, &obj->field, v), or write the store as ACTON_STORE(obj, obj->field, v). obj is the start of the object that holds the field; pass NULL if the code only has a pointer to a struct inside it.
  2. Before copying, moving or sorting pointers in bulk into a GC object (memcpy, memmove, a copy loop, reversing or sorting an array in place), call ACTON_WBAR_RANGE(obj, dst, nbytes) over the destination.
  3. No barrier is needed for stores into an object that the same function allocated and has held in a local variable since, for stores of NULL, None, integers or static objects, and for stores into stack variables, C globals, malloc memory or memory from acton_malloc_atomic. An object reached through a new object is not new itself: filling the array of a list returned by B_listD_new needs barriers. A barrier is always safe.
  4. The result of acton_realloc is not a new object: it may be the old one.
  5. Memory that a C library writes pointers into by itself, such as libuv handles and requests, comes from acton_malloc_rescanned(), which every collection rescans. Threads that the collector does not stop, like libuv's thread pool, must not write GC pointers into GC memory at all.
  6. Test with acton test --gc-check (or ACTON_GC_CHECK=1), which runs the collector's write-barrier checker at every collection and exits with status 70 when it finds a store without its barrier.
B_NoneType fooQ_ThingD_set_name(fooQ_Thing self, B_str name) {
    ACTON_STORE(self, self->name, name);
    return B_None;
}

Keeping a C pointer in an Acton value

C code often keeps a pointer from one call to the next: the handle of a C library, or state that it allocated. It keeps it in a cptr, the builtin type for a C pointer, or in a ?cptr when there may be no pointer. A cptr holds an address. C code never gives Acton code a cptr that is NULL, but --db and deserialize give one back as NULL, as the last item below says. A ?cptr holds an address or None, and C code sees None as NULL. Only a module with a C extension, which is a module with NotImplemented bodies, can name the type, also in ?cptr. Other modules cannot name it, but they can hold a cptr or a ?cptr that they get, as an attribute of a class or the result of a function, and test a ?cptr with is None.

class Parser(object):
    _state: ?cptr

    def __init__(self):
        self._state = None
        self._open()

    def _open(self) -> None:
        NotImplemented

    def is_open(self) -> bool:
        return self._state is not None

In C, a cptr and a ?cptr are a void * in fields, parameters and results, spelled $WORD in the generated code, and None is NULL:

B_NoneType fooQ_ParserD__open(fooQ_Parser self) {
    struct foo_state *state = acton_malloc(sizeof(*state));
    foo_state_init(state);
    ACTON_STORE(self, self->_state, state);
    return B_None;
}
  • Only C code makes a cptr, and Acton code writes None for no pointer. A C function that can give NULL declares its result ?cptr, and one that accepts NULL declares its parameter ?cptr. A cptr that C code returns or passes to Acton code is never NULL. A cptr parameter of a C function can still be NULL, when Acton code passes a cptr that a restore gave back.
  • p is None and p is not None test a ?cptr. Inside if p is not None:, a local variable or a parameter p is a cptr and can be passed where a cptr is wanted. An attribute such as self._state stays a ?cptr there. A state variable of an actor is a cptr there too, but a proc that the branch calls can set it to None. So copy an attribute or a state variable to a local variable first.
  • == and != compare addresses, and None equals only None. A cptr has no hash and no order, so it is no dict key or set element. Acton code cannot turn it into an integer, or make one from an integer or a string.
  • str(p) is cptr(...), without the address, also in a list, a tuple, a dict and a log record, and a ?cptr that is None prints as None. A cptr seen as a value, as in a list[value], prints as every value does: <cptr object at 0x...>, the address of its box. bool of a cptr is True.
  • Store into a cptr or ?cptr field with ACTON_STORE, as rule 1 above says. A pointer to C memory needs no barrier, but the barrier is never wrong, and a store of NULL needs none. Generated code stores a pointer with the barrier, like an object reference.
  • A cptr to GC memory keeps that memory alive wherever the value is: in an attribute, a local, a list or tuple, generic code or a message. A cptr or ?cptr gets a box only in a container, in generic code and as the result of a proc or an action, and the box is memory that the collector scans. A ?cptr that is None is None there, not a box. C code that takes such a value out of a list unboxes it with fromB_cptr(x), which gives NULL for None, and C code that puts one in boxes it with toB_cptr(p), which gives None for NULL. The same holds for the argument of an action value that C code calls through __asyn__, for a result that C code passes on with $R_CONT, and for a C method whose parameter or result a parent class declares as a type variable.
  • A cptr frees nothing. C memory is freed by C code: in a close method, in __cleanup__ or in a finalizer that the C code registers.
  • --db and deserialize give back every pointer as NULL, since an address means nothing in another process. A ?cptr comes back as None, which C code takes as a closed or not yet opened resource: an actor can open it again in __resume__, a class on its next use. A cptr comes back NULL, against its type, wherever it was saved: in an attribute, in an argument of a message that one actor sent to another, and in a closure, which includes a timer and a local variable that lives across an await. So keep a pointer that a restore may meet in a ?cptr. A plain cptr suits a value that C has just made and that Acton code uses before its message ends or awaits, and an argument of a message that C code sends from the event loop, which a restore never runs again. An actor that holds a cptr in an attribute gets it back NULL. Its own __resume__ has to notice that, give up and report it through a callback of its owner, since neither the compiler nor the RTS does that for it.

Converting C buffers to Acton strings and bytes

The string constructors validate UTF-8 and count Unicode characters. Use an explicit byte length when the input can contain NUL characters:

FunctionInputStorage
actStrFromCString(s)NUL-terminated UTF-8Shared
actStrFromCStringCopy(s)NUL-terminated UTF-8Copied
actStrFromCStringLength(s, n)n UTF-8 bytes, followed by a NUL terminatorShared
actStrFromCStringLengthCopy(s, n)n UTF-8 bytes; no terminator requiredCopied

Shared input must remain immutable and alive for as long as the returned string exists. String literals and completed buffers allocated with acton_malloc_atomic() are suitable. Use Copy for stack buffers, buffers that will be modified or freed, and memory owned by a C library that may release it. The length excludes the final terminator; NUL bytes within that length remain part of the Acton string. Empty strings and one-byte ASCII strings may reuse immutable singleton values.

For bytes, actBytesFromCString(s) borrows a NUL-terminated buffer up to its first NUL. Use actBytesFromCStringLength(s, n) for binary data. It borrows exactly n bytes, including embedded NUL bytes, and accepts an unterminated buffer. Both require immutable storage that lives as long as the result.

Use actBytesFromCStringCopy(s) or actBytesFromCStringLengthCopy(s, n) when the input can change or be freed. Both length variants take an int64_t.