Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
context.h
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// IMPORTANT NOTES:
20// - When allocating memory via a context it, we're targeting a normal block in
21// the memory manager, and eventually the block will be released, so don't rely
22// on the data in the block to stick around. If you need immortal data, just
23// explicitly allocate it using the memory manager.
24
25#pragma once
26
27#include <stdbool.h>
28#include <stddef.h>
29#include <stdint.h>
30
31typedef struct srn_engine_t srn_engine_t;
32
33#ifdef SRN_WITH_SERENE
34typedef struct srn_error_t srn_error_t;
35typedef struct srn_namespace_t srn_namespace_t;
36#endif
37
38typedef size_t srn_block_id_t;
39typedef uintptr_t object_id_t;
40
41#define SRN_HASH_TYPE uint32_t
42#define SRN_SEED_TYPE uint32_t
43
46
47typedef struct srn_context_t {
48 /// Long term state of the compiler
50
52 /// Where to allocate memory from.
55
56// -----------------------------------------------------------------------------
57// LifeCycle
58// -----------------------------------------------------------------------------
59
60/// Make an empty context, by allocating a new memory block. The returning
61/// context is a root context (no parent).
62[[gnu::nonnull(1)]]
64/* [[gnu::nonnull(1)]] srn_context_t * */
65/* srn_context_make_subcontext(srn_context_t *parent, srn_block_id_t block_id);
66 */
67
68[[gnu::nonnull(1)]]
70
71// -----------------------------------------------------------------------------
72// Memory Management
73// By default the context will carry around a memory block id and uses that
74// block to allocate memory. If in any case we need another block to allocate
75// memory in, then we need to provide the block id separately.
76// -----------------------------------------------------------------------------
77
78/**
79 * Allocate `size` bytes aligned to `alignment` from the context's memory block.
80 * Returns a pointer to the allocated region, or NULL on failure.
81 */
82[[gnu::nonnull(1)]]
83void *srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment);
84
85/**
86 * Allocate `size` bytes aligned to `alignment` from the context's memory block.
87 * Panics if the allocation fails. Never returns NULL.
88 */
89[[gnu::nonnull(1)]]
90void *srn_allocate_or_panic(const srn_context_t *ctx, size_t size, size_t alignment);
91
92[[gnu::nonnull(1, 2)]]
93void srn_release(const srn_context_t *ctx, void *ptr);
94
95#define ALLOC(ctx, T) (T *)srn_allocate(ctx, sizeof(T), alignof(T))
96#define ALLOCN(ctx, T, N) (T *)srn_allocate(ctx, sizeof(T) * (N), alignof(T))
97
98#define ALLOC_OR_PANIC(ctx, T) (T *)srn_allocate_or_panic(ctx, sizeof(T), alignof(T))
99#define ALLOCN_OR_PANIC(ctx, T, N) (T *)srn_allocate_or_panic(ctx, sizeof(T) * (N), alignof(T))
100
101#ifdef SRN_WITH_SERENE
102// -----------------------------------------------------------------------------
103// Namespace operations
104// -----------------------------------------------------------------------------
105/// Look up a previously registered namespace by its name (a C string).
106/// Returns the namespace or nullptr if none is registered under `name`.
107[[gnu::nonnull(1, 2)]]
108srn_namespace_t *srn_context_find_namespace(srn_context_t *ctx, const char *name);
109#endif
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
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
#define SRN_SEED_TYPE
Definition context.h:42
uintptr_t object_id_t
Definition context.h:39
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
SRN_HASH_TYPE srn_hash_t
Definition context.h:44
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
SRN_SEED_TYPE srn_seed_t
Definition context.h:45
void srn_release(const srn_context_t *ctx, void *ptr)
Definition context.c:84
#define SRN_HASH_TYPE
Definition context.h:41
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
A runtime error, a tag classifying the failure and a human-readable message.
Definition errors.h:173