Serene Runtime 1.0.0
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 <[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// 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).
63/* [[gnu::nonnull(1)]] srn_context_t * */
64/* srn_context_make_subcontext(srn_context_t *parent, srn_block_id_t block_id);
65 */
66
67[[gnu::nonnull(1)]] int srn_context_release(srn_context_t *ctx);
68
69// -----------------------------------------------------------------------------
70// Memory Management
71// By default the context will carry around a memory block id and uses that
72// block to allocate memory. If in any case we need another block to allocate
73// memory in, then we need to provide the block id separately.
74// -----------------------------------------------------------------------------
75
76[[gnu::nonnull(1)]]
77void *srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment);
78
79[[gnu::nonnull(1, 2)]]
80void srn_release(const srn_context_t *ctx, void *ptr);
81
82#define ALLOC(ctx, T) (T *)srn_allocate(ctx, sizeof(T), alignof(T))
83#define ALLOCN(ctx, T, N) (T *)srn_allocate(ctx, sizeof(T) * (N), alignof(T))
84
85#ifdef SRN_WITH_SERENE
86// -----------------------------------------------------------------------------
87// Namespace operations
88// -----------------------------------------------------------------------------
89[[gnu::nonnull(1, 2)]]
90srn_error_t *srn_context_register_namespace(srn_context_t *ctx,
91 srn_namespace_t *ns);
92
93/// Look up a previously registered namespace by its name (a C string).
94/// Returns the namespace or nullptr if none is registered under `name`.
95[[gnu::nonnull(1, 2)]]
96srn_namespace_t *srn_context_find_namespace(srn_context_t *ctx,
97 const char *name);
98#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:38
int srn_context_release(srn_context_t *ctx)
Definition context.c:63
#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)
Definition context.c:72
SRN_HASH_TYPE srn_hash_t
Definition context.h:44
SRN_SEED_TYPE srn_seed_t
Definition context.h:45
void srn_release(const srn_context_t *ctx, void *ptr)
Definition context.c:77
#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:49