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 <lxsameer@lxsameer.com>
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_allocate_or_panic(const srn_context_t *ctx, size_t size, size_t alignment) {
79 auto x = srn_allocate(ctx, size, alignment);
81 return x;
82}
83
84void srn_release(const srn_context_t *ctx, void *ptr) {
85 // No Op.
86 // This is just for future proofing
87}
88
89#ifdef SRN_WITH_SERENE
90srn_namespace_t *srn_context_find_namespace(srn_context_t *ctx, const char *name) {
91 PANIC_IF_NULL(ctx);
92 PANIC_IF_NULL(name);
93 auto max_len = ctx->engine->config.limits.ns_name_max_len;
94 // The key must cover the same bytes `srn_namespace_get_or_create` inserted
95 // under, the name without its null terminator.
96 hmap_key_t k = {.data = (void *)name, .len = strnlen(name, max_len)};
97 // Every published head is immutable, so an acquire load of the pointer is
98 // all a reader needs. No lock is taken.
99 hmap_t *namespaces = atomic_load_explicit(&ctx->engine->namespaces, memory_order_acquire);
100 return (srn_namespace_t *)hmap_lookup(namespaces, &k, nullptr);
101}
102#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)
Allocate size bytes aligned to alignment from the context's memory block.
Definition context.c:73
void * srn_allocate_or_panic(const srn_context_t *ctx, size_t size, size_t alignment)
Allocate size bytes aligned to alignment from the context's memory block.
Definition context.c:78
void srn_release(const srn_context_t *ctx, void *ptr)
Definition context.c:84
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:468
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:411
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:443
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:68
#define srn_mm_allocate_in_block(mm, id, T)
Definition interface.h:185
A namespace is an isolation for names nad technically a compilation unit.
const size_t len
Note: For key equality we use the memcpy function.
Definition hashmap.h:66
srn_limits_config_t limits
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:54
srn_configuration_t config
The runtime's tunable knobs, the single source for every configurable value (see configuration....
Definition engine.h:65
srn_mm_t * mm
Memory manager.
Definition engine.h:68
size_t ns_name_max_len
Longest namespace name accepted, in bytes.
#define PANIC_IF_NULL(ptr)
Definition utils.h:66