str_destroy and str_free

This commit is contained in:
2025-11-26 17:03:54 +05:00
parent 4c97787c27
commit 8a40caaf10
3 changed files with 35 additions and 7 deletions

View File

@@ -33,6 +33,16 @@ void Error_addCallPos(Error* e, ErrorCallPos p);
str Error_toStr(Error* e);
void Error_printAndExit(Error* e) ATTRIBUTE_NORETURN;
/*
Define in header, declare somewhere in source.
Create a function like
```
void init_your_lib() {
ErrorCodePage_register(YOUR_PAGE);
}
```
and call it in main().
*/
#define ErrorCodePage_name(name) ErrorCodePage_##name
#define ErrorCodePage_declare(name) extern u16 ErrorCodePage_name(name);
#define ErrorCodePage_define(name) u16 ErrorCodePage_name(name) = 0;
@@ -46,6 +56,7 @@ typedef enum TlibcError {
ErrorCodePage_declare(TLIBC);
ErrorCodePage_declare(LIBC_ERRNO);
typedef struct Result_ {
Error* error;
union {

View File

@@ -24,8 +24,17 @@ static inline str str_from_cstr(cstr s_ptr){
return str_construct((void*)s_ptr, strlen(s_ptr), true);
}
static inline void str_free(str s){
free(s.data);
/// destroy str with .data allocated on heap
static inline void str_destroy(str self){
free(self.data);
}
/// destroy str allocated on heap with .data allocated on heap
static inline void str_free(str* self){
if(!self)
return;
free(self->data);
free(self);
}
static inline Array(char) str_castTo_Array_char(str s) {