From 8a40caaf1044f9fe2c327c4d3c60a3310efd6142 Mon Sep 17 00:00:00 2001 From: Timerix Date: Wed, 26 Nov 2025 17:03:54 +0500 Subject: [PATCH] str_destroy and str_free --- README.md | 18 +++++++++++++----- include/tlibc/errors.h | 11 +++++++++++ include/tlibc/string/str.h | 13 +++++++++++-- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index e4a3a33..530dbf4 100644 --- a/README.md +++ b/README.md @@ -3,21 +3,29 @@ C library with collections, strings, exceptions, io... ## Build -1. Clone the repository. +1. Clone this repository. ``` git clone https://timerix.ddns.net/git/Timerix/tlibc.git ``` -2. Install [cbuild](https://timerix.ddns.net/git/Timerix/cbuild). + +2. Install [cbuild](https://timerix.ddns.net/git/Timerix/cbuild) version specified in `project.config`. + 3. Build static library ``` cd tlibc cbuild build_static_lib_dbg ``` + ## Usage -Include `tlibc/tlibc.h` and put this code at the beginning of `main()`. -``` - Deferral(32); +```c +#include "tlibc/tlibc.h" + +int main(){ + Deferral(32); // reserve memory for 32 defers try_fatal_void(tlibc_init()); Defer(tlibc_deinit()); + + Return 0; // call defers +} ``` diff --git a/include/tlibc/errors.h b/include/tlibc/errors.h index 64e0257..4585fe7 100755 --- a/include/tlibc/errors.h +++ b/include/tlibc/errors.h @@ -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 { diff --git a/include/tlibc/string/str.h b/include/tlibc/string/str.h index 78562e9..cc10f92 100755 --- a/include/tlibc/string/str.h +++ b/include/tlibc/string/str.h @@ -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) {