str_destroy and str_free

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

View File

@ -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
}
```

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) {