Serene Runtime 1.0.0
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
28#include "serene/rt/strings.h"
29#include <string.h>
30#endif
31
32#if defined(CTX_DEBUG)
33#define CTX_LOG(FMT, ...) DBG("CTX", FMT __VA_OPT__(, ) __VA_ARGS__)
34#else
35#define CTX_LOG(FMT, ...)
36#endif
37
39 PANIC_IF_NULL(engine);
40 srn_block_id_t block_id = srn_mm_allocate_block(engine->mm);
41 srn_context_t *ctx = srn_mm_allocate_in_block(engine->mm, block_id, srn_context_t);
42 PANIC_IF_NULL(ctx);
43 ctx->parent = nullptr;
44 ctx->engine = engine;
45 ctx->block_id = block_id;
46 return ctx;
47}
48
49/* srn_context_t *srn_context_make_subcontext(srn_context_t *parent, */
50/* srn_block_id_t block_id) { */
51/* PANIC_IF_NULL(parent); */
52/* srn_context_t *ctx = */
53/* srn_mm_allocate_in_block(parent->engine->mm, block_id, srn_context_t);
54 */
55/* PANIC_IF_NULL(ctx); */
56/* ctx->parent = parent; */
57/* ctx->engine = parent->engine; */
58/* ctx->block_id = parent->block_id; */
59
60/* return ctx; */
61/* } */
62
64 PANIC_IF_NULL(ctx);
65 if (ctx->block_id != SRN_BLOCK_NO_ID) {
66 CTX_LOG("Releasing block: %lu", ctx->block_id)
68 }
69 return 0;
70}
71
72void *srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment) {
73 PANIC_IF_NULL(ctx);
74 return srn_mm_allocate_in_block_aligned(ctx->engine->mm, ctx->block_id, size, alignment);
75}
76
77void srn_release(const srn_context_t *ctx, void *ptr) {
78 // No Op.
79 // This is just for future proofing
80}
81
82#ifdef SRN_WITH_SERENE
83srn_error_t *srn_context_register_namespace(srn_context_t *ctx, srn_namespace_t *ns) {
84 PANIC_IF_NULL(ctx);
85 PANIC_IF_NULL(ns);
86
87 srn_spinlock_lock(&ctx->engine->ns_lock);
88 // The key is the raw bytes of the namespace name (no struct header,
89 // no null terminator). Allocate it through the context so the hmap
90 // can hold on to the pointer after this function returns. Find by C
91 // string must use the same byte range as the key.
92 hmap_key_t *k = hmap_make_key(ctx, (void *)ns->name->buffer, srn_string_length(ns->name));
93 // The new hmap_t shares structure with the previous version through the
94 // persistent CHAMT representation, so assigning the result back into the
95 // engine slot is a constant-time pointer/length update.
96 ctx->engine->namespaces = hmap_insert(ctx, &ctx->engine->namespaces, k, (void *)ns);
97 srn_spinlock_unlock(&ctx->engine->ns_lock);
98 return nullptr;
99}
100
101srn_namespace_t *srn_context_find_namespace(srn_context_t *ctx, const char *name) {
102 PANIC_IF_NULL(ctx);
103 PANIC_IF_NULL(name);
104 hmap_key_t k = {.data = (void *)name, .len = strlen(name)};
105 return (srn_namespace_t *)hmap_lookup(ctx, &ctx->engine->namespaces, &k, nullptr);
106}
107#endif
#define CTX_LOG(FMT,...)
Definition context.c:35
srn_context_t * srn_context_make(srn_engine_t *engine)
Make an empty context, by allocating a new memory block.
Definition context.c:38
int srn_context_release(srn_context_t *ctx)
Definition context.c:63
void * srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment)
Definition context.c:72
void srn_release(const srn_context_t *ctx, void *ptr)
Definition context.c:77
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:388
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:347
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:364
void * hmap_lookup(srn_context_t *ctx, 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:622
hmap_t hmap_insert(srn_context_t *ctx, 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:618
hmap_key_t * hmap_make_key(srn_context_t *ctx, void *data, size_t len)
Create a new key out of the given data, with the given len.
Definition hashmap.c:627
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:166
size_t srn_string_length(const srn_string_t *s)
Definition strings.c:33
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:49
srn_mm_t * mm
Memory manager.
Definition engine.h:58
srn_string_t * name
Definition namespaces.h:49
uint8_t buffer[]
The buffer that holds the WTF8 sequence.
Definition strings.h:39
#define PANIC_IF_NULL(ptr)
Definition utils.h:64
static void srn_spinlock_lock(srn_spinlock_t *lock)
Definition utils.h:286
static void srn_spinlock_unlock(srn_spinlock_t *lock)
Definition utils.h:277