Serene Runtime 1.0.0
C runtime for the Serene programming language
Loading...
Searching...
No Matches
maps.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#pragma once
19
20#include "serene/rt/core.h"
22#include "serene/rt/protocols.h"
23#include "serene/utils.h"
24
25typedef struct srn_context_t srn_context_t;
26
27/// A persistent, immutable map. Backed by the CHAMT in
28/// serene/rt/impl/hashmap.h. Keys and values are both srn_value_t*.
32
33// ---------------------------------------------------------------------------
34// Lifetime contract for keys and values
35//
36// srn_map_assoc does NOT copy `key` or `value`. The underlying hmap stores
37// the caller-supplied srn_value_t* directly: the key sits inside an
38// hmap_key_t.data pointer, and the value occupies the entry's value slot.
39// Both pointers must remain valid for at least as long as the map itself.
40//
41// In practice this means: every key and value placed in a map must live in
42// a memory chain whose lifetime is equal to or longer than the chain that
43// holds the map. Interned values (keywords, namespaces) live in
44// engine immortal memory and are always safe. Values allocated inside a
45// short lived chain (per form, per call) must not be stored in a map whose
46// chain outlasts them, or the map will dangle.
47//
48// The runtime does not detect violations of this rule. A dangling key or
49// value silently corrupts later lookups and iterations.
50// ---------------------------------------------------------------------------
51
52[[gnu::nonnull(1)]] srn_value_t *srn_map_empty(srn_context_t *ctx,
53 srn_metadata_t *metadata);
54
55[[gnu::nonnull(1, 3, 4, 5)]] srn_value_t *
57 srn_value_t *key, srn_value_t *value);
This is an implementation of Compressed Hash-Array Mapped Prefix-tree, which is a bit-partitioned,...
srn_value_t * srn_map_assoc(srn_context_t *ctx, srn_metadata_t *metadata, srn_value_t *map, srn_value_t *key, srn_value_t *value)
Definition maps.c:66
srn_value_t * srn_map_empty(srn_context_t *ctx, srn_metadata_t *metadata)
Definition maps.c:59
A persistent, immutable map.
Definition maps.h:29
hmap_t inner
Definition maps.h:30