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 "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 55 of file utils.c.

55 {
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}
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 69 of file utils.c.

69 {
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 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 45 of file utils.c.

45 {
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}
#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 30 of file utils.c.

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