Serene Runtime 1.0.0
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 "config.h"
#include "serene/rt/context.h"
#include "serene/rt/mm/interface.h"
#include <string.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.
 
char * srn_copy_bytes (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 ( srn_context_t * ctx,
const char * src,
size_t len,
size_t alignment )

Definition at line 42 of file utils.c.

42 {
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}
void * srn_allocate(const srn_context_t *ctx, size_t size, size_t alignment)
Definition context.c:72
#define PANIC_IF_NULL(ptr)
Definition utils.h:64
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 56 of file utils.c.

56 {
57 PANIC_IF_NULL(mm);
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}
int n
Definition acutest.h:538
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
Here is the call 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 __builtin_debugtrap();
36 __builtin_unreachable();
37#else
38 abort();
39#endif
40}
int const char * file
Definition acutest.h:788
void int line
Definition acutest.h:743