C API
Session resources guide
Choose session capacities and initialize a session in application-provided memory.The Session resources reference describes the structures and calls declared in <maelys/datalog_resources.h>. This guide shows how to choose session capacities and supply the memory for a session.
Configure and initialize a session
- Start with
RESOURCE_REQUEST_INIT. Set a capacity field and its matchingCAPACITY_*bit only when you need an explicit capacity; leave the other bits unset to use the loaded profile's defaults. - Pass the request to
session_config_set_resources(). It copies the known values into the configuration. Without an explicit backend selection, planning uses the reference backend. - Initialize a storage plan with
SESSION_PLAN_INIT, then callsession_storage_requirements_configured(). Check the status before using the returned byte count and alignment; query them at runtime rather than writing down a platform-specific size. - Allocate the measured storage with
malloc, verify its alignment, and give it tosession_init_configured(), with the same policy, index and configuration. Keep this storage alive, disjoint from other live storage and unmoved until the session has been released. Initialization recomputes the plan from those inputs. - Initialize an output with
RESOURCES_INIT, then callsession_get_resources()to read the capacities granted to the session. Release the session before freeing its storage.
Choose the four capacities
Set capacities before planning or creating the session. A bit that is absent from capacity_mask selects the loaded profile's default; a present bit with a zero field requests an exact zero capacity, not a default.
| Request field | Capacity | Presence bit |
|---|---|---|
input_facts | Input-snapshot facts, counted before deduplication. | CAPACITY_INPUT_FACTS |
derived_facts | The complete derived database, including helper facts. | CAPACITY_DERIVED_FACTS |
symbols | Dictionary entries, including roots required by the program. | CAPACITY_SYMBOLS |
text_bytes | Interned dictionary text, including terminating NUL bytes. | CAPACITY_TEXT_BYTES |
A configuration request is admitted against the policy and backend when the session is planned or created. A successful setter alone does not prove that the requested configuration can be used.
Fixed and elastic memory
MEMORY_FIXED is the initializer's memory mode. Backend-elastic memory and caller allocation require a backend that supports them; the reference backend remains fixed and does not supply that support. Do not treat fixed memory as elastic allocation accounting: session_get_allocation_stats() returns UNSUPPORTED for a fixed-memory session.
Without an explicit backend selection, planning uses the reference backend. An explicitly selected legacy (ABI 5) backend is refused with UNSUPPORTED. The requested alignment is _Alignof(max_align_t), which malloc guarantees; the example keeps an explicit check with a clear error message on stderr.
Complete example
This program requests capacity for sixteen input facts and leaves the other capacities at the loaded SMALL profile's defaults. It checks each call, allocates the measured session storage, reads the granted capacity and releases the session before freeing that storage.
Reference context
#include <maelys/datalog.h>
#include <maelys/datalog_builders.h>
#include <maelys/datalog_resources.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static maelys_datalog_policy_t *policy = NULL;
static maelys_datalog_session_config_t *config = NULL;
static maelys_datalog_session_t *session = NULL;
static void *storage = NULL;
static int cleanup_failed = 0;
static maelys_datalog_diagnostic_t diagnostic;
static void cleanup_status(maelys_datalog_status_t status) {
if (status != MAELYS_DATALOG_STATUS_OK) {
cleanup_failed = 1;
fprintf(stderr, "Context cleanup failed: %s\n", maelys_datalog_status_name(status));
}
}
static void cleanup_context(void) {
if (session != NULL) {
maelys_datalog_status_t status = maelys_datalog_session_free(session);
cleanup_status(status);
if (status != MAELYS_DATALOG_STATUS_OK) return;
session = NULL;
}
free(storage); storage = NULL;
if (config != NULL) { cleanup_status(maelys_datalog_session_config_free(config)); config = NULL; }
if (policy != NULL) { cleanup_status(maelys_datalog_policy_free(policy)); policy = NULL; }
}
static void check_status(maelys_datalog_status_t status) {
if (status != MAELYS_DATALOG_STATUS_OK) {
fprintf(stderr, "API call failed: %s\n", maelys_datalog_status_name(status));
exit(EXIT_FAILURE);
}
}
static void show_status(const char *operation, maelys_datalog_status_t status) {
printf("%s status: %s\n", operation, maelys_datalog_status_name(status));
check_status(status);
}
static void check_storage(void *memory, size_t alignment) {
if (memory == NULL) {
fprintf(stderr, "Session storage allocation failed.\n");
exit(EXIT_FAILURE);
}
if (alignment == 0 || (uintptr_t)memory % alignment != 0) {
fprintf(stderr, "Session storage does not meet the required alignment.\n");
exit(EXIT_FAILURE);
}
}
static void prepare_context(void) {
diagnostic = maelys_datalog_diagnostic_initializer();
if (atexit(cleanup_context) != 0) exit(EXIT_FAILURE);
const maelys_datalog_predicate_t predicates[] = {
MAELYS_DATALOG_EDB("user", 1), MAELYS_DATALOG_EDB("owns", 2),
MAELYS_DATALOG_EDB("blocked", 1), MAELYS_DATALOG_IDB_QUERY("allow", 2),
};
const maelys_datalog_domain_t domain =
MAELYS_DATALOG_DOMAIN_NO_ATOMS("documents", predicates);
check_status(maelys_datalog_domain_register(&domain));
const char *source = "allow(User, Doc) :- owns(User, Doc), not(blocked(User)).";
check_status(maelys_datalog_policy_load_inline("documents", "resources-guide",
source, strlen(source), &policy, &diagnostic));
check_status(maelys_datalog_session_config_create(&config));
}The five numbered comments follow the steps above. show_status displays the result of each call and then checks it; the context supplies the policy and cleanup.
// 1. Request the capacities you need.
maelys_datalog_session_resource_request_t request = MAELYS_DATALOG_RESOURCE_REQUEST_INIT;
request.capacity_mask = MAELYS_DATALOG_CAPACITY_INPUT_FACTS;
request.input_facts = 16;
// 2. Configure the session; other capacities keep the profile defaults.
show_status("configuration", maelys_datalog_session_config_set_resources(config, &request));
// 3. Measure the storage required by this policy and configuration.
maelys_datalog_session_storage_plan_t plan = MAELYS_DATALOG_SESSION_PLAN_INIT;
show_status("planning", maelys_datalog_session_storage_requirements_configured(policy, 0, config, &plan, &diagnostic));
printf("storage required: %s\n", plan.arena_bytes > 0 ? "yes" : "no");
printf("alignment is a power of two: %s\n", plan.arena_alignment > 0 &&
(plan.arena_alignment & (plan.arena_alignment - 1)) == 0 ? "yes" : "no");
// 4. Initialize the session in the memory supplied by the application.
storage = malloc(plan.arena_bytes);
check_storage(storage, plan.arena_alignment);
show_status("initialization", maelys_datalog_session_init_configured(
storage, plan.arena_bytes, policy, 0, config, &session, &diagnostic));
// 5. Read back the capacities granted to the session.
maelys_datalog_session_resources_t admitted = MAELYS_DATALOG_RESOURCES_INIT;
show_status("resources", maelys_datalog_session_get_resources(session, &admitted));
printf("requested input capacity granted: %s\n", admitted.input_facts == request.input_facts ? "yes" : "no");
// Release the session before releasing its storage.
show_status("release", maelys_datalog_session_free(session));
session = NULL;
free(storage); storage = NULL;Output · v0.26.0 · SMALL
configuration status: ok
planning status: ok
storage required: yes
alignment is a power of two: yes
initialization status: ok
resources status: ok
requested input capacity granted: yes
release status: okFull program
#include <maelys/datalog.h>
#include <maelys/datalog_builders.h>
#include <maelys/datalog_resources.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static maelys_datalog_policy_t *policy = NULL;
static maelys_datalog_session_config_t *config = NULL;
static maelys_datalog_session_t *session = NULL;
static void *storage = NULL;
static int cleanup_failed = 0;
static maelys_datalog_diagnostic_t diagnostic;
static void cleanup_status(maelys_datalog_status_t status) {
if (status != MAELYS_DATALOG_STATUS_OK) {
cleanup_failed = 1;
fprintf(stderr, "Context cleanup failed: %s\n", maelys_datalog_status_name(status));
}
}
static void cleanup_context(void) {
if (session != NULL) {
maelys_datalog_status_t status = maelys_datalog_session_free(session);
cleanup_status(status);
if (status != MAELYS_DATALOG_STATUS_OK) return;
session = NULL;
}
free(storage); storage = NULL;
if (config != NULL) { cleanup_status(maelys_datalog_session_config_free(config)); config = NULL; }
if (policy != NULL) { cleanup_status(maelys_datalog_policy_free(policy)); policy = NULL; }
}
static void check_status(maelys_datalog_status_t status) {
if (status != MAELYS_DATALOG_STATUS_OK) {
fprintf(stderr, "API call failed: %s\n", maelys_datalog_status_name(status));
exit(EXIT_FAILURE);
}
}
static void show_status(const char *operation, maelys_datalog_status_t status) {
printf("%s status: %s\n", operation, maelys_datalog_status_name(status));
check_status(status);
}
static void check_storage(void *memory, size_t alignment) {
if (memory == NULL) {
fprintf(stderr, "Session storage allocation failed.\n");
exit(EXIT_FAILURE);
}
if (alignment == 0 || (uintptr_t)memory % alignment != 0) {
fprintf(stderr, "Session storage does not meet the required alignment.\n");
exit(EXIT_FAILURE);
}
}
static void prepare_context(void) {
diagnostic = maelys_datalog_diagnostic_initializer();
if (atexit(cleanup_context) != 0) exit(EXIT_FAILURE);
const maelys_datalog_predicate_t predicates[] = {
MAELYS_DATALOG_EDB("user", 1), MAELYS_DATALOG_EDB("owns", 2),
MAELYS_DATALOG_EDB("blocked", 1), MAELYS_DATALOG_IDB_QUERY("allow", 2),
};
const maelys_datalog_domain_t domain =
MAELYS_DATALOG_DOMAIN_NO_ATOMS("documents", predicates);
check_status(maelys_datalog_domain_register(&domain));
const char *source = "allow(User, Doc) :- owns(User, Doc), not(blocked(User)).";
check_status(maelys_datalog_policy_load_inline("documents", "resources-guide",
source, strlen(source), &policy, &diagnostic));
check_status(maelys_datalog_session_config_create(&config));
}
int main(void) {
prepare_context();
// 1. Request the capacities you need.
maelys_datalog_session_resource_request_t request = MAELYS_DATALOG_RESOURCE_REQUEST_INIT;
request.capacity_mask = MAELYS_DATALOG_CAPACITY_INPUT_FACTS;
request.input_facts = 16;
// 2. Configure the session; other capacities keep the profile defaults.
show_status("configuration", maelys_datalog_session_config_set_resources(config, &request));
// 3. Measure the storage required by this policy and configuration.
maelys_datalog_session_storage_plan_t plan = MAELYS_DATALOG_SESSION_PLAN_INIT;
show_status("planning", maelys_datalog_session_storage_requirements_configured(policy, 0, config, &plan, &diagnostic));
printf("storage required: %s\n", plan.arena_bytes > 0 ? "yes" : "no");
printf("alignment is a power of two: %s\n", plan.arena_alignment > 0 &&
(plan.arena_alignment & (plan.arena_alignment - 1)) == 0 ? "yes" : "no");
// 4. Initialize the session in the memory supplied by the application.
storage = malloc(plan.arena_bytes);
check_storage(storage, plan.arena_alignment);
show_status("initialization", maelys_datalog_session_init_configured(
storage, plan.arena_bytes, policy, 0, config, &session, &diagnostic));
// 5. Read back the capacities granted to the session.
maelys_datalog_session_resources_t admitted = MAELYS_DATALOG_RESOURCES_INIT;
show_status("resources", maelys_datalog_session_get_resources(session, &admitted));
printf("requested input capacity granted: %s\n", admitted.input_facts == request.input_facts ? "yes" : "no");
// Release the session before releasing its storage.
show_status("release", maelys_datalog_session_free(session));
session = NULL;
free(storage); storage = NULL;
check_status(maelys_datalog_session_config_free(config));
config = NULL;
check_status(maelys_datalog_policy_free(policy));
policy = NULL;
check_status(maelys_datalog_diagnostic_clear(&diagnostic));
cleanup_context();
if (cleanup_failed) return EXIT_FAILURE;
return EXIT_SUCCESS;
}In Python and TypeScript
Both bindings can request sixteen input facts while retaining the profile defaults for the other capacities, then read back the capacity granted to the session. The snippets use the same document policy as the C example; their folded contexts provide the engine, domain and cleanup.
Python
Ruleset.prepare(capacities=…) takes a capacity request; Session.capacities reads back the granted capacities.
Python reference context
from maelys_datalog import Engine, Predicate, SessionCapacities
engine = Engine()
try:
engine.register_domain('documents', [
Predicate.edb('user', 1), Predicate.edb('owns', 2),
Predicate.edb('blocked', 1), Predicate.idb_query('allow', 2),
])
ruleset = engine.load_inline_ruleset('documents', 'resources-guide',
'allow(User, Doc) :- owns(User, Doc), not(blocked(User)).')
except BaseException:
engine.close()
raise
def cleanup():
engine.close()with ruleset.prepare(capacities=SessionCapacities(input_facts=16)) as session:
granted = session.capacities.input_facts
print(f'input facts capacity: {granted}')
if granted != 16:
raise AssertionError('Input fact capacity differs from the request')input facts capacity: 16
TypeScript
Ruleset.prepare(options) takes options.capacities, which accepts a SessionCapacities value. Session.capacities reads back the granted capacities. JavaScript uses the same API without the type annotations.
TypeScript reference context
import { Engine, Predicate, SessionCapacities } from '@maelys-dev/datalog/wasm'
const engine = await Engine.create({ profile: 'small' })
try {
engine.registerDomain('documents', [
Predicate.edb('user', 1), Predicate.edb('owns', 2),
Predicate.edb('blocked', 1), Predicate.idbQuery('allow', 2),
])
const ruleset = engine.loadInlineRuleset('documents', 'resources-guide',
'allow(User, Doc) :- owns(User, Doc), not(blocked(User)).')
/* API_REFERENCE_EXAMPLE */
} finally {
engine.close()
}const session = ruleset.prepare({ capacities: new SessionCapacities({ inputFacts: 16 }) })
try {
const granted = session.capacities.inputFacts
console.log('input facts capacity:', granted)
if (granted !== 16) throw new Error('Input fact capacity differs from the request')
} finally { session.close() }input facts capacity: 16