Compare commits
2 Commits
f5805888bc
...
447c15bc46
| Author | SHA1 | Date | |
|---|---|---|---|
| 447c15bc46 | |||
| d3a8c03fe5 |
@ -5,13 +5,13 @@
|
||||
|
||||
typedef struct ErrorCallPos {
|
||||
i32 line;
|
||||
str file;
|
||||
str func;
|
||||
cstr file;
|
||||
cstr func;
|
||||
} ErrorCallPos;
|
||||
#define ErrorCallPos_here() (ErrorCallPos){\
|
||||
.line = __LINE__,\
|
||||
.file = str_construct((char*)(void*)__FILE__, sizeof(__FILE__), true),\
|
||||
.func = str_construct((char*)(void*)__func__, sizeof(__func__), true)\
|
||||
.file = __FILE__,\
|
||||
.func = __func__\
|
||||
}
|
||||
|
||||
typedef struct Error {
|
||||
@ -21,7 +21,7 @@ typedef struct Error {
|
||||
List call_stack;
|
||||
} Error;
|
||||
|
||||
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos);
|
||||
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p);
|
||||
void Error_destroy(Error* e);
|
||||
void Error_addCallPos(Error* e, ErrorCallPos p);
|
||||
str Error_toStr(Error* e);
|
||||
@ -56,6 +56,7 @@ typedef struct Result_ {
|
||||
#define try_fatal(VAR, RSLT_CALL, DEFER_CODE) \
|
||||
Result_ VAR = RSLT_CALL;\
|
||||
if(VAR.error){\
|
||||
Error_addCallPos(VAR.error, ErrorCallPos_here());\
|
||||
DEFER_CODE;\
|
||||
Error_printAndExit(VAR.error);\
|
||||
};
|
||||
|
||||
@ -16,7 +16,7 @@ void StringBuilder_destroy(StringBuilder* b);
|
||||
/// @param count set to -1 to clear StringBuilder
|
||||
void StringBuilder_removeFromEnd(StringBuilder* b, u32 count);
|
||||
void StringBuilder_append_char(StringBuilder* b, char c);
|
||||
void StringBuilder_append_cstr(StringBuilder* b, char* s);
|
||||
void StringBuilder_append_cstr(StringBuilder* b, cstr s);
|
||||
void StringBuilder_append_str(StringBuilder* b, str s);
|
||||
void StringBuilder_append_i64(StringBuilder* b, i64 a);
|
||||
void StringBuilder_append_u64(StringBuilder* b, u64 a);
|
||||
|
||||
30
include/tlibc/time.h
Normal file
30
include/tlibc/time.h
Normal file
@ -0,0 +1,30 @@
|
||||
#pragma once
|
||||
|
||||
#if __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#include "std.h"
|
||||
|
||||
/// nanoseconds
|
||||
typedef u64 nsec_t;
|
||||
/// microseconds
|
||||
typedef u64 usec_t;
|
||||
/// miliseconds
|
||||
typedef u64 msec_t;
|
||||
|
||||
/// system time now in nanoseconds
|
||||
///@return u64 will overflow in 13 years
|
||||
nsec_t getTimeNsec();
|
||||
|
||||
/// system time now in microseconds
|
||||
///@return u64 will overflow in 58494 years
|
||||
usec_t getTimeUsec();
|
||||
|
||||
void sleepNsec(nsec_t time);
|
||||
void sleepUsec(usec_t time);
|
||||
void sleepMsec(msec_t time);
|
||||
|
||||
#if __cplusplus
|
||||
}
|
||||
#endif
|
||||
12
src/errors.c
12
src/errors.c
@ -3,11 +3,12 @@
|
||||
|
||||
#define ERRMSG_LENGTH 1024
|
||||
|
||||
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos){
|
||||
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p){
|
||||
Error* e = (Error*)malloc(sizeof(Error));
|
||||
e->msg = str_construct((char*)(void*)msg, strlen(msg), true);
|
||||
e->is_msg_on_heap = is_msg_on_heap;
|
||||
e->call_stack = List_alloc(ErrorCallPos, 1);
|
||||
e->call_stack = List_alloc(ErrorCallPos, 16);
|
||||
List_push(&e->call_stack, ErrorCallPos, p);
|
||||
return e;
|
||||
}
|
||||
|
||||
@ -30,12 +31,12 @@ str Error_toStr(Error* e){
|
||||
for(u32 i = 0; i < len; i++){
|
||||
ErrorCallPos* ep = (ErrorCallPos*)e->call_stack.data + i;
|
||||
StringBuilder_append_str(&b, STR("\n at "));
|
||||
StringBuilder_append_str(&b, ep->file);
|
||||
StringBuilder_append_cstr(&b, ep->file);
|
||||
StringBuilder_append_char(&b, ':');
|
||||
StringBuilder_append_i64(&b, ep->line);
|
||||
StringBuilder_append_char(&b, ' ');
|
||||
StringBuilder_append_str(&b, ep->func);
|
||||
StringBuilder_append_str(&b, STR("()\n"));
|
||||
StringBuilder_append_cstr(&b, ep->func);
|
||||
StringBuilder_append_str(&b, STR("()"));
|
||||
}
|
||||
|
||||
return StringBuilder_getStr(&b);
|
||||
@ -43,7 +44,6 @@ str Error_toStr(Error* e){
|
||||
|
||||
|
||||
void Error_printAndExit(Error* e){
|
||||
Error_addCallPos(e, ErrorCallPos_here());\
|
||||
str e_str = Error_toStr(e);
|
||||
printfe("%s\n", e_str.data);
|
||||
free(e_str.data);
|
||||
|
||||
@ -33,8 +33,8 @@ void StringBuilder_append_str(StringBuilder* b, str s){
|
||||
List_push_size(&b->buffer, s.data, s.size);
|
||||
}
|
||||
|
||||
void StringBuilder_append_cstr(StringBuilder* b, char* s){
|
||||
StringBuilder_append_str(b, str_construct(s, strlen(s), true));
|
||||
void StringBuilder_append_cstr(StringBuilder* b, cstr s){
|
||||
StringBuilder_append_str(b, str_construct((char*)(void*)s, strlen(s), true));
|
||||
}
|
||||
|
||||
void StringBuilder_append_i64(StringBuilder* b, i64 n){
|
||||
|
||||
46
src/time.c
Normal file
46
src/time.c
Normal file
@ -0,0 +1,46 @@
|
||||
// posix version definition to use clock_gettime
|
||||
#ifndef _XOPEN_SOURCE
|
||||
#if __STDC_VERSION__ >= 199901L
|
||||
#define _XOPEN_SOURCE 600
|
||||
#else
|
||||
#define _XOPEN_SOURCE 500
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "tlibc/time.h"
|
||||
#include <time.h>
|
||||
#include <assert.h>
|
||||
|
||||
#define K 1000
|
||||
#define M 1000000
|
||||
#define G 1000000000
|
||||
|
||||
nsec_t getTimeNsec(){
|
||||
struct timespec t;
|
||||
assert(clock_gettime(CLOCK_REALTIME, &t) == 0);
|
||||
u64 n = t.tv_sec * G + t.tv_nsec;
|
||||
return n;
|
||||
}
|
||||
|
||||
usec_t getTimeUsec(){
|
||||
struct timespec t;
|
||||
assert(clock_gettime(CLOCK_REALTIME, &t) == 0);
|
||||
u64 n = t.tv_sec * M + t.tv_nsec / K;
|
||||
return n;
|
||||
}
|
||||
|
||||
void sleepNsec(nsec_t time){
|
||||
struct timespec t = {
|
||||
.tv_sec = time / G,
|
||||
.tv_nsec = time % G
|
||||
};
|
||||
assert(nanosleep(&t, NULL) == 0);
|
||||
}
|
||||
|
||||
void sleepUsec(usec_t time){
|
||||
sleepNsec(time * K);
|
||||
}
|
||||
|
||||
void sleepMsec(msec_t time){
|
||||
sleepNsec(time * M);
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user