Serene Runtime
1.0.0
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
24
#include "config.h"
25
#include "
serene/rt/context.h
"
26
#include "
serene/rt/mm/interface.h
"
27
28
#include <string.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
__builtin_debugtrap();
36
__builtin_unreachable();
37
#else
38
abort();
39
#endif
40
}
41
42
char
*
srn_copy_bytes
(
srn_context_t
*ctx,
const
char
*src,
size_t
len,
size_t
alignment) {
43
PANIC_IF_NULL
(ctx);
44
45
char
*dst =
srn_allocate
(ctx, len, alignment);
46
47
// It will panic earlier anyway.
48
if
(dst ==
nullptr
) {
49
return
nullptr
;
50
}
51
52
memcpy(dst, src, len);
53
return
dst;
54
}
55
56
char
*
srn_dup_str
(
srn_mm_t
*mm,
const
char
*s) {
57
PANIC_IF_NULL
(mm);
58
PANIC_IF_NULL
(s);
59
size_t
n
= strlen(s);
60
char
*copy =
srn_mm_allocate
(mm,
n
+ 1);
61
if
(copy ==
nullptr
) {
62
return
nullptr
;
63
}
64
memcpy(copy, s,
n
+ 1);
65
return
copy;
66
}
file
int const char * file
Definition
acutest.h:788
n
int n
Definition
acutest.h:538
line
void int line
Definition
acutest.h:743
srn_allocate
void * srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment)
Definition
context.c:72
context.h
srn_mm_allocate
void * srn_mm_allocate(srn_mm_t *mm, size_t size)
Generic allocations that do not participate in the block based pools.
Definition
default.c:141
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:112
srn_copy_bytes
char * srn_copy_bytes(srn_context_t *ctx, const char *src, size_t len, size_t alignment)
Definition
utils.c:42
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:56
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:64
utils.c
Serene Runtime is free software, licensed under the
GNU LGPL v3
License.