Compare commits

..

4 Commits

7 changed files with 40 additions and 12 deletions

View File

@@ -38,12 +38,12 @@ static inline Array_ Array_copy(Array_ src){
#define struct_castTo_Array(STRUCT_PTR) Array_construct_size((STRUCT_PTR), sizeof(*STRUCT_PTR)) #define struct_castTo_Array(STRUCT_PTR) Array_construct_size((STRUCT_PTR), sizeof(*STRUCT_PTR))
///@return a[0..n] ///@return a[0..i-1]
static inline Array(u8) Array_sliceBefore(Array(u8) a, u32 n){ static inline Array(u8) Array_sliceTo(Array(u8) a, u32 i){
return Array_construct_size(a.data, n); return Array_construct_size(a.data, i);
} }
///@return a[n...] ///@return a[i...]
static inline Array(u8) Array_sliceAfter(Array(u8) a, u32 n){ static inline Array(u8) Array_sliceFrom(Array(u8) a, u32 i){
return Array_construct_size((u8*)a.data + n, a.size - n); return Array_construct_size((u8*)a.data + i, a.size - i);
} }

View File

@@ -25,7 +25,7 @@ Error* Error_create(const char* msg, bool is_msg_on_heap, ErrorCallPos p);
void Error_free(Error* e); void Error_free(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);
void Error_printAndExit(Error* e) __attribute__ ((__noreturn__)); void Error_printAndExit(Error* e) ATTRIBUTE_NORETURN;
typedef struct Result_ { typedef struct Result_ {
Error* error; Error* error;
@@ -38,8 +38,13 @@ typedef struct Result_ {
}; };
} Result_; } Result_;
#define Result(T) Result_ ///Use this macro only to specify function return type.
/// To declare variable, use ResultVar().
/// Warning can be suppressed by IGNORE_RESULT
#define Result(T) Result_ ATTRIBUTE_WARN_UNUSED_RESULT
#define ResultVar(T) Result_
///USAGE: IGNORE_RESULT trySomething();
#define IGNORE_RESULT Result_ __ignored_##__LINE__ ATTRIBUTE_UNUSED =
#define RESULT_ERROR(MSG, IS_MSG_ON_HEAP) (Result_){ .error = Error_create(MSG, IS_MSG_ON_HEAP, ErrorCallPos_here()) } #define RESULT_ERROR(MSG, IS_MSG_ON_HEAP) (Result_){ .error = Error_create(MSG, IS_MSG_ON_HEAP, ErrorCallPos_here()) }
#define RESULT_ERROR_FMT(FORMAT, ARGS...) RESULT_ERROR(sprintf_malloc(4096, FORMAT ,##ARGS), true) #define RESULT_ERROR_FMT(FORMAT, ARGS...) RESULT_ERROR(sprintf_malloc(4096, FORMAT ,##ARGS), true)

View File

@@ -64,6 +64,27 @@ typedef const char* cstr;
/// @warning pointer can be null /// @warning pointer can be null
#define NULLABLE(NAME) NAME #define NULLABLE(NAME) NAME
#define ATTRIBUTE_PACKED \
__attribute__((__packed__))
#define ATTRIBUTE_ALIGNED(BYTE_N) \
__attribute__((aligned(BYTE_N)))
#define ATTRIBUTE_NORETURN \
__attribute__ ((__noreturn__))
#define ATTRIBUTE_WARN_UNUSED_RESULT \
__attribute__((warn_unused_result))
#define ATTRIBUTE_UNUSED \
__attribute__ ((unused))
///@brief https://gcc.gnu.org/onlinedocs/gcc-15.2.0/gcc/Common-Function-Attributes.html#index-format-function-attribute
///@param FMT_ARG_INDEX Index of format literal argument. First argument is 1.
///@param VARIADIC_ARGS_INDEX Index of variadic arguments.
#define ATTRIBUTE_CHECK_FORMAT_PRINTF(FMT_ARG_INDEX, VARIADIC_ARGS_INDEX) \
__attribute__((__format__(__printf__, FMT_ARG_INDEX, VARIADIC_ARGS_INDEX)))
#if __cplusplus #if __cplusplus
} }

View File

@@ -5,5 +5,5 @@
char* _strcat_malloc(size_t n, cstr str0, ...); char* _strcat_malloc(size_t n, cstr str0, ...);
char* _vstrcat_malloc(size_t n, cstr str0, va_list argv); char* _vstrcat_malloc(size_t n, cstr str0, va_list argv);
char* NULLABLE(sprintf_malloc)(size_t buffer_size, cstr format, ...) __attribute__((__format__(__printf__, 2, 3))); char* NULLABLE(sprintf_malloc)(size_t buffer_size, cstr format, ...) ATTRIBUTE_CHECK_FORMAT_PRINTF(2, 3);
char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv); char* NULLABLE(vsprintf_malloc)(size_t buffer_size, cstr format, va_list argv);

View File

@@ -12,6 +12,8 @@ typedef u64 nsec_t;
typedef u64 usec_t; typedef u64 usec_t;
/// miliseconds /// miliseconds
typedef u64 msec_t; typedef u64 msec_t;
/// seconds
typedef u64 sec_t;
/// system time now in nanoseconds /// system time now in nanoseconds
///@return u64 will overflow in 13 years ///@return u64 will overflow in 13 years

View File

@@ -88,7 +88,7 @@ void* NULLABLE(HashMap_tryGetPtr)(HashMap_* ptr, str key){
static void __HashMap_expand(HashMap_* ptr){ static void __HashMap_expand(HashMap_* ptr){
u32 height_expanded_n = ptr->height_n + 1; u32 height_expanded_n = ptr->height_n + 1;
assert(height_expanded_n < Array_len(&__HashMap_heights, u32) && "HashMap IS FULL! Fix your code."); assert(height_expanded_n < Array_len(__HashMap_heights, u32) && "HashMap IS FULL! Fix your code.");
// alloc new HashMapBucket array // alloc new HashMapBucket array
u32 height_expanded = ((u32*)__HashMap_heights.data)[height_expanded_n]; u32 height_expanded = ((u32*)__HashMap_heights.data)[height_expanded_n];

View File

@@ -27,7 +27,7 @@ str Error_toStr(Error* e){
u32 len = List_len(&e->call_stack, ErrorCallPos); u32 len = List_len(&e->call_stack, ErrorCallPos);
StringBuilder b = StringBuilder_alloc(e->msg.size + 80 * len); StringBuilder b = StringBuilder_alloc(e->msg.size + 80 * len);
StringBuilder_append_str(&b, STR("Error: ")); StringBuilder_append_str(&b, STR("Catched Error: "));
StringBuilder_append_str(&b, e->msg); StringBuilder_append_str(&b, e->msg);
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;