Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
context.c
Go to the documentation of this file.
1/* -*- C -*-
2 * Serene programming language
3 * Copyright (C) 2019-2026 Sameer Rahmani <[email protected]>
4 *
5 * This library is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this library. If not, see <https://www.gnu.org/licenses/>.
17 */
18
19#include "serene/rt/context.h"
20
21#include "serene/rt/engine.h"
24#include "serene/utils.h"
25
26#ifdef SRN_WITH_SERENE
27# include <string.h>
28
29# include "serene/rt/namespaces.h"
30# include "serene/rt/strings.h"
31#endif
32
33#if defined(CTX_DEBUG)
34# define CTX_LOG(FMT, ...) DBG("CTX", FMT __VA_OPT__(, ) __VA_ARGS__)
35#else
36# define CTX_LOG(FMT, ...)
37#endif
38
40 PANIC_IF_NULL(engine);
41 srn_block_id_t block_id = srn_mm_allocate_block(engine->mm);
42 srn_context_t *ctx = srn_mm_allocate_in_block(engine->mm, block_id, srn_context_t);
43 PANIC_IF_NULL(ctx);
44 ctx->parent = nullptr;
45 ctx->engine = engine;
46 ctx->block_id = block_id;
47 return ctx;
48}
49
50/* srn_context_t *srn_context_make_subcontext(srn_context_t *parent, */
51/* srn_block_id_t block_id) { */
52/* PANIC_IF_NULL(parent); */
53/* srn_context_t *ctx = */
54/* srn_mm_allocate_in_block(parent->engine->mm, block_id, srn_context_t);
55 */
56/* PANIC_IF_NULL(ctx); */
57/* ctx->parent = parent; */
58/* ctx->engine = parent->engine; */
59/* ctx->block_id = parent->block_id; */
60
61/* return ctx; */
62/* } */
63
65 PANIC_IF_NULL(ctx);
66 if (ctx->block_id != SRN_BLOCK_NO_ID) {
67 CTX_LOG("Releasing block: %zu", ctx->block_id);
69 }
70 return 0;
71}
72
73void *srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment) {
74 PANIC_IF_NULL(ctx);
75 return srn_mm_allocate_in_block_aligned(ctx->engine->mm, ctx->block_id, size, alignment);
76}
77
78void srn_release(const srn_context_t *ctx, void *ptr) {
79 // No Op.
80 // This is just for future proofing
81}
82
83#ifdef SRN_WITH_SERENE
84srn_error_t *srn_context_register_namespace(srn_context_t *ctx, srn_namespace_t *ns) {
85 PANIC_IF_NULL(ctx);
86 PANIC_IF_NULL(ns);
87
88 srn_spinlock_lock(&ctx->engine->ns_lock);
89 // The key is the raw bytes of the namespace name (no struct header,
90 // no null terminator). The map copies the key into its pinned context, so
91 // a stack struct is enough here. Find by C string must use the same byte
92 // range as the key.
93 hmap_key_t k = {.data = (void *)ns->name->buffer, .len = srn_string_length(ns->name)};
94 // The new hmap_t shares structure with the previous version through the
95 // persistent CHAMT representation, so assigning the result back into the
96 // engine slot is a constant-time pointer/length update.
97 ctx->engine->namespaces = hmap_insert(&ctx->engine->namespaces, &k, (void *)ns);
98 srn_spinlock_unlock(&ctx->engine->ns_lock);
99 return nullptr;
100}
101
102srn_namespace_t *srn_context_find_namespace(srn_context_t *ctx, const char *name) {
103 PANIC_IF_NULL(ctx);
104 PANIC_IF_NULL(name);
105 hmap_key_t k = {.data = (void *)name, .len = strlen(name)};
106 // Snapshot the map struct under the lock, registration replaces it in the
107 // engine slot and an unsynchronized reader could see a torn root and
108 // length pair. CHAMT nodes are immutable and arena backed, so the walk
109 // itself is safe outside the critical region.
110 srn_spinlock_lock(&ctx->engine->ns_lock);
111 hmap_t namespaces = ctx->engine->namespaces;
112 srn_spinlock_unlock(&ctx->engine->ns_lock);
113 return (srn_namespace_t *)hmap_lookup(&namespaces, &k, nullptr);
114}
115#endif
#define CTX_LOG(FMT,...)
Definition context.c:36
srn_context_t * srn_context_make(srn_engine_t *engine)
Make an empty context, by allocating a new memory block.
Definition context.c:39
int srn_context_release(srn_context_t *ctx)
Definition context.c:64
void * srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment)
Definition context.c:73
void srn_release(const srn_context_t *ctx, void *ptr)
Definition context.c:78
size_t srn_block_id_t
The block id is effectively just an index in the blocks array in srn_mm_t.
Definition context.h:38
void srn_mm_release_block(srn_mm_t *mm, srn_block_id_t id)
Release the given block id and free the memory for later allocations.
Definition default.c:451
void * srn_mm_allocate_in_block_aligned(srn_mm_t *mm, srn_block_id_t block_id, size_t size, size_t alignment)
Allocate memory on a block with the given block_id.
Definition default.c:396
srn_block_id_t srn_mm_allocate_block(srn_mm_t *mm)
Allocate a new block in the memory manager and return its ID.
Definition default.c:426
hmap_t hmap_insert(const hmap_t *hmap, hmap_key_t *k, void *v)
Insert the given key k with the value v in the given hash hmap and return the new map.
Definition hashmap.c:661
void * hmap_lookup(const hmap_t *hmap, const hmap_key_t *k, void *default_value)
Lookup the given k in the given hmap and return the value if it's been found.
Definition hashmap.c:665
This is an implementation of Compressed Hash-Array Mapped Prefix-tree, which is a bit-partitioned,...
#define SRN_BLOCK_NO_ID
Definition interface.h:63
#define srn_mm_allocate_in_block(mm, id, T)
Definition interface.h:180
size_t srn_string_length(const srn_string_t *s)
Definition strings.c:34
Note: For key equality we use the memcpy function.
Definition hashmap.h:66
srn_engine_t * engine
Long term state of the compiler.
Definition context.h:49
srn_block_id_t block_id
Where to allocate memory from.
Definition context.h:53
struct srn_context_t * parent
Definition context.h:51
Engine is a structure to own the long living and main pieces of the compiler.
Definition engine.h:51
srn_mm_t * mm
Memory manager.
Definition engine.h:65
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:125
srn_string_t * name
Definition namespaces.h:45
uint8_t buffer[]
The buffer that holds the WTF8 sequence.
Definition strings.h:66
#define PANIC_IF_NULL(ptr)
Definition utils.h:66
static void srn_spinlock_lock(srn_spinlock_t *lock)
Definition utils.h:285
static void srn_spinlock_unlock(srn_spinlock_t *lock)
Definition utils.h:276