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