39#define XXH_STATIC_LINKING_ONLY
40#define XXH_ENABLE_AUTOVECTORIZE
45#define XXH_NO_LONG_LONG
47#define XXH_IMPLEMENTATION
48#include "third_party/xxhash.h"
50#if defined(__linux__) || defined(__GLIBC__)
51#include <sys/random.h>
52#elif defined(__APPLE__)
58#pragma comment(lib, "bcrypt.lib")
67 auto res = getrandom(&s,
sizeof(s), 0);
69 PANIC(
"Can't get a random number");
71#elif defined(__APPLE__)
72 arc4random_buf(&s,
sizeof(s));
74 NTSTATUS st = BCryptGenRandom(
nullptr, (PUCHAR)&s,
sizeof(s), BCRYPT_USE_SYSTEM_PREFERRED_RNG);
76 PANIC(
"Couldn't allocate a seed");
80 FILE *f = fopen(
"/dev/urandom",
"rb");
81 fread(&s,
sizeof(s), 1, f);
87 PANIC(
"Couldn't allocate a seed");
96 if (engine ==
nullptr) {
104#ifdef SRN_WITH_SERENE
109#ifdef SRN_WITH_SERENE
113 engine->namespaces = (
hmap_t){.len = 0, .root =
nullptr};
115 engine->keywords = (
hmap_t){.len = 0, .root =
nullptr};
126#ifdef SRN_WITH_SERENE
133 size_t length = (data ==
nullptr) ? 0 : len;
134 return XXH32(data, length, engine->
seed);
139 return atomic_fetch_add_explicit(&engine->
object_id_counter, 1, memory_order_relaxed);
142#ifdef SRN_WITH_SERENE
157 hmap_key_t lookup = {.data = (
void *)name, .len = name_len};
158 void *found =
hmap_lookup(ctx, &engine->keywords, &lookup,
nullptr);
159 if (found !=
nullptr) {
167 "Keyword name exceeds the string length limit");
176 size_t name_alloc_size =
sizeof(
srn_string_t) + name_len + 1;
179 name_str->
len = name_len;
180 name_str->
size = name_len + 1;
181 memcpy(name_str->
buffer, name, name_len);
182 name_str->
buffer[name_len] =
'\0';
199 k->len = name_str->
len;
200 engine->keywords =
hmap_insert(ctx, &engine->keywords, k, (
void *)v);
void * srn_mm_immortal_allocate_aligned(srn_mm_t *mm, size_t size, size_t alignment)
Allocate memory on the importal block which will never gets freed.
srn_engine_t * srn_engine_make(srn_mm_t *mm)
void srn_engine_shutdown(srn_engine_t *engine)
srn_hash_t srn_hash(const srn_engine_t *engine, const void *data, size_t len)
static srn_seed_t srn_generate_seed()
srn_object_id_t srn_allocate_object_id(srn_engine_t *engine)
#define ENGINE_FIRST_OBJECT_ID
Start allocating IDs from 100, earlier IDs are reserved.
@ STRING_LENGTH_LIMIT_EXCEEDED
AI Generated (🤦) Fiber subsystem overview.
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.
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.
This is an implementation of Compressed Hash-Array Mapped Prefix-tree, which is a bit-partitioned,...
#define srn_mm_immortal_allocate(mm, T)
int srn_jit_shutdown(srn_jit_t *jit)
srn_jit_t * srn_jit_make(srn_mm_t *mm)
void srn_sched_shutdown(srn_scheduler_t *sched)
The one stop tear down of the fiber subsystem, should be called once srn_sched_run has returned.
srn_scheduler_t * srn_sched_init(srn_engine_t *engine)
#define SRN_STRING_MAX_LEN
On top of my head, no other reason.
Note: For key equality we use the memcpy function.
void * data
len 0 -> data == nullptr
srn_engine_t * engine
Long term state of the compiler.
Engine is a structure to own the long living and main pieces of the compiler.
_Atomic srn_object_id_t object_id_counter
An unsigned counter to allocate object ids atomically.
srn_scheduler_t * scheduler
The fiber scheduler, set by srn_sched_init.
srn_seed_t seed
We use the seed for hashing and the value will be generated at random for each new context.
srn_mm_t * mm
Memory manager.
Main memory manager structure that will own all the allocated blocks and data.
size_t size
Size of the buffer.
uint8_t buffer[]
The buffer that holds the WTF8 sequence.
size_t len
length of the WTF-8 sequence in bytes
srn_metadata_t * metadata
union srn_value_t::@033047061046230251001111174367071167226300135003 as
IMPORTANT NOTE: The size of this union should never be larger than a word.
#define PANIC_IF_NULL(ptr)
static void srn_spinlock_lock(srn_spinlock_t *lock)
static void srn_spinlock_unlock(srn_spinlock_t *lock)
static void srn_spinlock_init(srn_spinlock_t *lock)