Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
utils.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/utils.h"
20
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24#include <time.h>
25
26#include "serene/rt/context.h"
28
29/// TODO(lxsameer): unwind the stack
30void srn_panic(const char *msg, const char *file, size_t line, const char *fn) {
31 (void)fprintf(stderr, "PANIC: %s %s:%zu (%s)\n", msg, file, line, fn);
32 (void)fflush(nullptr);
33#if SERENE_DEBUG
34# if defined(__has_builtin) && __has_builtin(__builtin_debugtrap)
35 __builtin_debugtrap();
36# else
37 __builtin_trap(); // gcc: SIGILL into the debugger; no resumable trap
38# endif
39 __builtin_unreachable();
40#else
41 abort();
42#endif
43}
44
45uint64_t srn_now_ns(void) {
46 struct timespec ts;
47 // CLOCK_MONOTONIC with a valid buffer can only fail EINVAL (clock
48 // unsupported) or EFAULT (bad pointer), neither possible here -- so a failure
49 // should never happen.
50 int rc = clock_gettime(CLOCK_MONOTONIC, &ts);
51 PANIC_IF(rc != 0, "clock_gettime(CLOCK_MONOTONIC) failed");
52 return ((uint64_t)ts.tv_sec * SRN_NS_PER_SEC) + (uint64_t)ts.tv_nsec;
53}
54
55char *srn_copy_bytes(const srn_context_t *ctx, const char *src, size_t len, size_t alignment) {
56 PANIC_IF_NULL(ctx);
57
58 char *dst = srn_allocate(ctx, len, alignment);
59
60 // It will panic earlier anyway.
61 if (dst == nullptr) {
62 return nullptr;
63 }
64
65 memcpy(dst, src, len);
66 return dst;
67}
68
69char *srn_dup_str(srn_mm_t *mm, const char *s) {
70 PANIC_IF_NULL(mm);
72 size_t n = strlen(s);
73 char *copy = srn_mm_malloc(mm, n + 1);
74 if (copy == nullptr) {
75 return nullptr;
76 }
77 memcpy(copy, s, n + 1);
78 return copy;
79}
int const char * file
Definition acutest.h:779
int n
Definition acutest.h:525
void int line
Definition acutest.h:732
void * srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment)
Definition context.c:73
void * srn_mm_malloc(srn_mm_t *mm, size_t size)
Generic allocations that do not participate in the block based pools.
Definition default.c:155
Main memory manager structure that will own all the allocated blocks and data.
Definition interface.h:110
uint64_t srn_now_ns(void)
The current time on the monotonic clock, in nanoseconds.
Definition utils.c:45
char * srn_copy_bytes(const srn_context_t *ctx, const char *src, size_t len, size_t alignment)
Definition utils.c:55
char * srn_dup_str(srn_mm_t *mm, const char *s)
Copy a null terminated C string into a fresh allocation made through mm.
Definition utils.c:69
void srn_panic(const char *msg, const char *file, size_t line, const char *fn)
TODO(lxsameer): unwind the stack.
Definition utils.c:30
#define PANIC_IF_NULL(ptr)
Definition utils.h:66
#define PANIC_IF(cond, msg)
Definition utils.h:59
#define SRN_NS_PER_SEC
nanoseconds in one second
Definition utils.h:306