Serene Runtime 1.0.0-dev
C runtime for the Serene programming language
Loading...
Searching...
No Matches
utils.c File Reference
#include "serene/utils.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include "config.h"
#include "serene/rt/context.h"
#include "serene/rt/mm/interface.h"
Include dependency graph for utils.c:

Go to the source code of this file.

Functions

void srn_panic (const char *msg, const char *file, size_t line, const char *fn)
 TODO(lxsameer): unwind the stack.
uint64_t srn_now_ns (void)
 The current time on the monotonic clock, in nanoseconds.
char * srn_copy_bytes (const srn_context_t *ctx, const char *src, size_t len, size_t alignment)
char * srn_dup_str (srn_mm_t *mm, const char *s)
 Copy a null terminated C string into a fresh allocation made through mm.

Function Documentation

◆ srn_copy_bytes()

char * srn_copy_bytes ( const srn_context_t * ctx,
const char * src,
size_t len,
size_t alignment )

Definition at line 56 of file utils.c.

56 {
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}
void * srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment)
Definition context.c:73
#define PANIC_IF_NULL(ptr)
Definition utils.h:66
Here is the call graph for this function:
Here is the caller graph for this function:

◆ 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.

Returns nullptr on allocation failure.

Definition at line 70 of file utils.c.

70 {
71 PANIC_IF_NULL(mm);
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}
int n
Definition acutest.h:525
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
Here is the call graph for this function:

◆ srn_now_ns()

uint64_t srn_now_ns ( void )

The current time on the monotonic clock, in nanoseconds.

Panics if the clock read fails – CLOCK_MONOTONIC with a valid buffer cannot fail on supported targets, so a failure means something is deeply wrong.

Definition at line 46 of file utils.c.

46 {
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}
#define PANIC_IF(cond, msg)
Definition utils.h:59
#define SRN_NS_PER_SEC
nanoseconds in one second
Definition utils.h:306
Here is the caller graph for this function:

◆ srn_panic()

void srn_panic ( const char * msg,
const char * file,
size_t line,
const char * fn )

TODO(lxsameer): unwind the stack.

Definition at line 31 of file utils.c.

31 {
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}
int const char * file
Definition acutest.h:779
void int line
Definition acutest.h:732