Compare commits

..

No commits in common. "447c15bc467546aa80245a7c5bc15d46d1349661" and "f5805888bcd909113286d44542c204189d73889a" have entirely different histories.

6 changed files with 14 additions and 91 deletions

View File

@ -5,13 +5,13 @@
typedef struct ErrorCallPos { typedef struct ErrorCallPos {
i32 line; i32 line;
cstr file; str file;
cstr func; str func;
} ErrorCallPos; } ErrorCallPos;
#define ErrorCallPos_here() (ErrorCallPos){\ #define ErrorCallPos_here() (ErrorCallPos){\
.line = __LINE__,\ .line = __LINE__,\
.file = __FILE__,\ .file = str_construct((char*)(void*)__FILE__, sizeof(__FILE__), true),\
.func = __func__\ .func = str_construct((char*)(void*)__func__, sizeof(__func__), true)\
} }
typedef struct Error { typedef struct Error {
@ -21,7 +21,7 @@ typedef struct Error {
List call_stack; List call_stack;
} Error; } Error;
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p); Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos);
void Error_destroy(Error* e); void Error_destroy(Error* e);
void Error_addCallPos(Error* e, ErrorCallPos p); void Error_addCallPos(Error* e, ErrorCallPos p);
str Error_toStr(Error* e); str Error_toStr(Error* e);
@ -56,7 +56,6 @@ typedef struct Result_ {
#define try_fatal(VAR, RSLT_CALL, DEFER_CODE) \ #define try_fatal(VAR, RSLT_CALL, DEFER_CODE) \
Result_ VAR = RSLT_CALL;\ Result_ VAR = RSLT_CALL;\
if(VAR.error){\ if(VAR.error){\
Error_addCallPos(VAR.error, ErrorCallPos_here());\
DEFER_CODE;\ DEFER_CODE;\
Error_printAndExit(VAR.error);\ Error_printAndExit(VAR.error);\
}; };

View File

@ -16,7 +16,7 @@ void StringBuilder_destroy(StringBuilder* b);
/// @param count set to -1 to clear StringBuilder /// @param count set to -1 to clear StringBuilder
void StringBuilder_removeFromEnd(StringBuilder* b, u32 count); void StringBuilder_removeFromEnd(StringBuilder* b, u32 count);
void StringBuilder_append_char(StringBuilder* b, char c); void StringBuilder_append_char(StringBuilder* b, char c);
void StringBuilder_append_cstr(StringBuilder* b, cstr s); void StringBuilder_append_cstr(StringBuilder* b, char* s);
void StringBuilder_append_str(StringBuilder* b, str s); void StringBuilder_append_str(StringBuilder* b, str s);
void StringBuilder_append_i64(StringBuilder* b, i64 a); void StringBuilder_append_i64(StringBuilder* b, i64 a);
void StringBuilder_append_u64(StringBuilder* b, u64 a); void StringBuilder_append_u64(StringBuilder* b, u64 a);

View File

@ -1,30 +0,0 @@
#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

View File

@ -3,12 +3,11 @@
#define ERRMSG_LENGTH 1024 #define ERRMSG_LENGTH 1024
Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p){ Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos){
Error* e = (Error*)malloc(sizeof(Error)); Error* e = (Error*)malloc(sizeof(Error));
e->msg = str_construct((char*)(void*)msg, strlen(msg), true); e->msg = str_construct((char*)(void*)msg, strlen(msg), true);
e->is_msg_on_heap = is_msg_on_heap; e->is_msg_on_heap = is_msg_on_heap;
e->call_stack = List_alloc(ErrorCallPos, 16); e->call_stack = List_alloc(ErrorCallPos, 1);
List_push(&e->call_stack, ErrorCallPos, p);
return e; return e;
} }
@ -31,12 +30,12 @@ str Error_toStr(Error* e){
for(u32 i = 0; i < len; i++){ for(u32 i = 0; i < len; i++){
ErrorCallPos* ep = (ErrorCallPos*)e->call_stack.data + i; ErrorCallPos* ep = (ErrorCallPos*)e->call_stack.data + i;
StringBuilder_append_str(&b, STR("\n at ")); StringBuilder_append_str(&b, STR("\n at "));
StringBuilder_append_cstr(&b, ep->file); StringBuilder_append_str(&b, ep->file);
StringBuilder_append_char(&b, ':'); StringBuilder_append_char(&b, ':');
StringBuilder_append_i64(&b, ep->line); StringBuilder_append_i64(&b, ep->line);
StringBuilder_append_char(&b, ' '); StringBuilder_append_char(&b, ' ');
StringBuilder_append_cstr(&b, ep->func); StringBuilder_append_str(&b, ep->func);
StringBuilder_append_str(&b, STR("()")); StringBuilder_append_str(&b, STR("()\n"));
} }
return StringBuilder_getStr(&b); return StringBuilder_getStr(&b);
@ -44,6 +43,7 @@ str Error_toStr(Error* e){
void Error_printAndExit(Error* e){ void Error_printAndExit(Error* e){
Error_addCallPos(e, ErrorCallPos_here());\
str e_str = Error_toStr(e); str e_str = Error_toStr(e);
printfe("%s\n", e_str.data); printfe("%s\n", e_str.data);
free(e_str.data); free(e_str.data);

View File

@ -33,8 +33,8 @@ void StringBuilder_append_str(StringBuilder* b, str s){
List_push_size(&b->buffer, s.data, s.size); List_push_size(&b->buffer, s.data, s.size);
} }
void StringBuilder_append_cstr(StringBuilder* b, cstr s){ void StringBuilder_append_cstr(StringBuilder* b, char* s){
StringBuilder_append_str(b, str_construct((char*)(void*)s, strlen(s), true)); StringBuilder_append_str(b, str_construct(s, strlen(s), true));
} }
void StringBuilder_append_i64(StringBuilder* b, i64 n){ void StringBuilder_append_i64(StringBuilder* b, i64 n){

View File

@ -1,46 +0,0 @@
// 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);
}