From 4f6cf9da514a261f5ae6d2b180e47b3afd55a350 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 20 Jan 2023 15:45:40 +0600 Subject: [PATCH 01/55] static inline __kpVU_i --- src/kprint/kprint.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/kprint/kprint.h b/src/kprint/kprint.h index 60a7ab0..6948a00 100644 --- a/src/kprint/kprint.h +++ b/src/kprint/kprint.h @@ -23,7 +23,7 @@ typedef union { static inline __kp_value_union __kpVU_f(float64 f) { return (__kp_value_union){ .f64=f }; } -inline __kp_value_union __kpVU_i(int64 f) { return (__kp_value_union){ .i64=f }; } +static inline __kp_value_union __kpVU_i(int64 f) { return (__kp_value_union){ .i64=f }; } #define __kpVU_selectType(V) _Generic(V, float: __kpVU_f, double: __kpVU_f, default: __kpVU_i)(V) From 73b199828cda6ba5a7663d01ac928d996eccd23f Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 20 Jan 2023 15:48:55 +0600 Subject: [PATCH 02/55] working dir ./bin --- .vscode/launch.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index df30fe5..253788a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,10 +5,10 @@ "name": "(gdb) Debug", "type": "cppdbg", "request": "launch", - "program": "${workspaceFolder}/bin/kerep.com", + "program": "$kerep.com", "preLaunchTask": "build_exec_dbg", "stopAtEntry": false, - "cwd": "${fileDirname}", + "cwd": "${fileDirname}/bin", "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "gdb", From cef4fe40024d4d785ec8dea9c5c50f914b965230 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 20 Jan 2023 16:42:19 +0600 Subject: [PATCH 03/55] file_readAll fix --- src/Filesystem/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Filesystem/file.c b/src/Filesystem/file.c index 08e53fb..7dcdd74 100644 --- a/src/Filesystem/file.c +++ b/src/Filesystem/file.c @@ -86,7 +86,7 @@ Maybe file_readAll(File* file, char** allBytes){ uint64 i=0; while(true){ rezult=fgetc(file); - if(rezult!=EOF){ + if(rezult==EOF){ if(ferror(file)) safethrow(ERR_IO,; StringBuilder_free(sb)); break; From 3a20caf380cb60fce36c48a0aba2a5fa1ac1a16a Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 20 Jan 2023 19:00:14 +0600 Subject: [PATCH 04/55] buffer overflow fix --- src/Filesystem/file.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Filesystem/file.c b/src/Filesystem/file.c index 7dcdd74..1c56911 100644 --- a/src/Filesystem/file.c +++ b/src/Filesystem/file.c @@ -91,7 +91,7 @@ Maybe file_readAll(File* file, char** allBytes){ safethrow(ERR_IO,; StringBuilder_free(sb)); break; } - buffer[i]=(char)rezult; + buffer[i%sizeof(buffer)]=(char)rezult; i++; if(!(i%sizeof(buffer))) StringBuilder_append_string(sb,bufStr); From 29a5b222868f6b6efd01b313971b5e8b85052ab4 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 20 Jan 2023 19:57:37 +0600 Subject: [PATCH 05/55] some Array improvements --- src/Array/Array.c | 10 ++++++++++ src/Array/Array.h | 4 ++++ src/Array/Array_declare.h | 2 +- src/String/string.c | 1 + src/String/string.h | 2 ++ src/base/endian.h | 10 ++++++++++ src/base/type_system/init.c | 27 ++++++++++++++------------- 7 files changed, 42 insertions(+), 14 deletions(-) diff --git a/src/Array/Array.c b/src/Array/Array.c index c669459..f7d4030 100644 --- a/src/Array/Array.c +++ b/src/Array/Array.c @@ -14,3 +14,13 @@ Array_define(int64) Array_define(uint64) Array_define(Unitype) + +void Array_Unitype_free_(Array_Unitype* array, bool freeMembers){ + if(freeMembers) for (int32 i; ilength; i++) + Unitype_free(array->values[i]); + if(array->allocatedOnHeap) + free(array->values); +} + +void __Array_Unitype_free_(void* ar) +{ Array_Unitype_free_(ar, true); } diff --git a/src/Array/Array.h b/src/Array/Array.h index 016d0ed..5c8f388 100644 --- a/src/Array/Array.h +++ b/src/Array/Array.h @@ -22,6 +22,10 @@ Array_declare(uint64) Array_declare(Unitype) +/// use this function instead of auto generated +void Array_Unitype_free_(Array_Unitype* array, bool freeMembers); +void __Array_Unitype_free_(void* ar); + #if __cplusplus } #endif \ No newline at end of file diff --git a/src/Array/Array_declare.h b/src/Array/Array_declare.h index e7139b3..2c7a37f 100644 --- a/src/Array/Array_declare.h +++ b/src/Array/Array_declare.h @@ -31,7 +31,7 @@ static inline Array_##type Array_##type##_fromBuffer(type* buffer, uint32 buffer };\ }\ \ -static inline void Array_##type##_freeValues(Array_##type* array){\ +static inline void Array_##type##_free(Array_##type* array){\ if(array->allocatedOnHeap)\ free(array->values);\ } diff --git a/src/String/string.c b/src/String/string.c index 81d61de..9864fb1 100644 --- a/src/String/string.c +++ b/src/String/string.c @@ -1,6 +1,7 @@ #include "string.h" ktid_define(string); +Array_define(string); // copies str content to new char pointer value (adding '\0' at the end) char* string_extract(string str){ diff --git a/src/String/string.h b/src/String/string.h index 0b8716e..e934376 100644 --- a/src/String/string.h +++ b/src/String/string.h @@ -5,6 +5,7 @@ extern "C" { #endif #include "../base/base.h" +#include "../Array/Array.h" // my fixed length string struct // doesn't store '\0' at the end @@ -13,6 +14,7 @@ typedef struct string{ uint64 length; // amount of chars in ptr value } string; ktid_declare(string); +Array_declare(string); static const string stringNull={NULL,0}; diff --git a/src/base/endian.h b/src/base/endian.h index 9d9d1e7..5fb2603 100644 --- a/src/base/endian.h +++ b/src/base/endian.h @@ -1,3 +1,9 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + #include "std.h" PACK_ENUM(Endian, @@ -7,3 +13,7 @@ PACK_ENUM(Endian, ); Endian getEndian(); + +#if __cplusplus +} +#endif diff --git a/src/base/type_system/init.c b/src/base/type_system/init.c index 4f1a2fd..0c85ce9 100644 --- a/src/base/type_system/init.c +++ b/src/base/type_system/init.c @@ -29,18 +29,18 @@ void ktDescriptors_initKerepTypes(){ // base type arrays - kt_register(Array_char, (freeMembers_t)Array_char_freeValues, NULL); - kt_register(Array_bool, (freeMembers_t)Array_bool_freeValues, NULL); - kt_register(Array_float32, (freeMembers_t)Array_float32_freeValues, NULL); - kt_register(Array_float64, (freeMembers_t)Array_float64_freeValues, NULL); - kt_register(Array_int8, (freeMembers_t)Array_int8_freeValues, NULL); - kt_register(Array_uint8, (freeMembers_t)Array_uint8_freeValues, NULL); - kt_register(Array_int16, (freeMembers_t)Array_int16_freeValues, NULL); - kt_register(Array_uint16, (freeMembers_t)Array_uint16_freeValues, NULL); - kt_register(Array_int32, (freeMembers_t)Array_int32_freeValues, NULL); - kt_register(Array_uint32, (freeMembers_t)Array_uint32_freeValues, NULL); - kt_register(Array_int64, (freeMembers_t)Array_int64_freeValues, NULL); - kt_register(Array_uint64, (freeMembers_t)Array_uint64_freeValues, NULL); + kt_register(Array_char, (freeMembers_t)Array_char_free, NULL); + kt_register(Array_bool, (freeMembers_t)Array_bool_free, NULL); + kt_register(Array_float32, (freeMembers_t)Array_float32_free, NULL); + kt_register(Array_float64, (freeMembers_t)Array_float64_free, NULL); + kt_register(Array_int8, (freeMembers_t)Array_int8_free, NULL); + kt_register(Array_uint8, (freeMembers_t)Array_uint8_free, NULL); + kt_register(Array_int16, (freeMembers_t)Array_int16_free, NULL); + kt_register(Array_uint16, (freeMembers_t)Array_uint16_free, NULL); + kt_register(Array_int32, (freeMembers_t)Array_int32_free, NULL); + kt_register(Array_uint32, (freeMembers_t)Array_uint32_free, NULL); + kt_register(Array_int64, (freeMembers_t)Array_int64_free, NULL); + kt_register(Array_uint64, (freeMembers_t)Array_uint64_free, NULL); // base type autoarrs kt_register(Autoarr_char, ____Autoarr_free_char, NULL); @@ -58,7 +58,7 @@ void ktDescriptors_initKerepTypes(){ // Unitype kt_register(Unitype, __UnitypePtr_free, NULL); - kt_register(Array_Unitype, (freeMembers_t)Array_Unitype_freeValues, NULL); + kt_register(Array_Unitype, __Array_Unitype_free_, NULL); kt_register(Autoarr_Unitype, ____Autoarr_free_Unitype_, NULL); // replacing autogenerated freear() function to custom Autoarr_Unitype* _uar=Autoarr_create(Unitype, 1, 1); @@ -81,6 +81,7 @@ void ktDescriptors_initKerepTypes(){ // string kt_register(string, NULL, NULL); + kt_register(Array_string, (freeMembers_t)Array_string_free, NULL); kt_register(Autoarr_string, ____Autoarr_free_string, NULL); // StringBuilder kt_register(StringBuilder, __StringBuilder_free, NULL); From 6c5358d1fb1b38d191208e09413d3107c1660775 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Sat, 4 Feb 2023 16:35:28 +0600 Subject: [PATCH 06/55] fixed small bug --- src/Array/Array.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Array/Array.c b/src/Array/Array.c index f7d4030..e6b3069 100644 --- a/src/Array/Array.c +++ b/src/Array/Array.c @@ -16,7 +16,7 @@ Array_define(uint64) Array_define(Unitype) void Array_Unitype_free_(Array_Unitype* array, bool freeMembers){ - if(freeMembers) for (int32 i; ilength; i++) + if(freeMembers) for (int32 i=0; ilength; i++) Unitype_free(array->values[i]); if(array->allocatedOnHeap) free(array->values); From 52c2d2f85568bc6e50a795b0beacce5789e16ebb Mon Sep 17 00:00:00 2001 From: timerix Date: Tue, 7 Feb 2023 12:55:30 +0600 Subject: [PATCH 07/55] cbuild update --- cbuild | 2 +- default.config | 25 ++++++++++++++++++------- 2 files changed, 19 insertions(+), 8 deletions(-) diff --git a/cbuild b/cbuild index 47808ee..15a9661 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit 47808ee989c85b6d011851dbec24c39580b55cbb +Subproject commit 15a9661c467facf65b2155eab3fad1ecf5f0c945 diff --git a/default.config b/default.config index 23c2c06..3719daa 100644 --- a/default.config +++ b/default.config @@ -1,5 +1,5 @@ #!/bin/bash -CBUILD_VERSION=3 +CBUILD_VERSION=4 CONFIG_VERSION=4 PROJECT=kerep @@ -26,7 +26,7 @@ case $TASK in CPP_ARGS="$C_ARGS" LINKER_ARGS="" TASK_SCRIPT=cbuild/default_tasks/build_exec.sh - PRE_TASK_SCRIPT=tasks/pre_build.sh + PRE_TASK_SCRIPT= POST_TASK_SCRIPT= ;; build_exec_dbg) @@ -34,7 +34,7 @@ case $TASK in CPP_ARGS="$C_ARGS" LINKER_ARGS="" TASK_SCRIPT=cbuild/default_tasks/build_exec.sh - PRE_TASK_SCRIPT=tasks/pre_build.sh + PRE_TASK_SCRIPT= POST_TASK_SCRIPT= ;; build_shared_lib) @@ -42,7 +42,7 @@ case $TASK in CPP_ARGS="$C_ARGS" LINKER_ARGS="-Wl,-soname,$SHARED_LIB_FILE" TASK_SCRIPT=cbuild/default_tasks/build_shared_lib.sh - PRE_TASK_SCRIPT=tasks/pre_build.sh + PRE_TASK_SCRIPT= POST_TASK_SCRIPT= ;; build_shared_lib_dbg) @@ -50,21 +50,21 @@ case $TASK in CPP_ARGS="$C_ARGS" LINKER_ARGS="-Wl,-soname,$SHARED_LIB_FILE" TASK_SCRIPT=cbuild/default_tasks/build_shared_lib.sh - PRE_TASK_SCRIPT=tasks/pre_build.sh + PRE_TASK_SCRIPT= POST_TASK_SCRIPT= ;; build_static_lib) C_ARGS="-O2 -fpic" CPP_ARGS="$C_ARGS" TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh - PRE_TASK_SCRIPT=tasks/pre_build.sh + PRE_TASK_SCRIPT= POST_TASK_SCRIPT= ;; build_static_lib_dbg) C_ARGS="-O0 -g" CPP_ARGS="$C_ARGS" TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh - PRE_TASK_SCRIPT=tasks/pre_build.sh + PRE_TASK_SCRIPT= POST_TASK_SCRIPT= ;; exec) @@ -75,3 +75,14 @@ case $TASK in TASK_SCRIPT=cbuild/default_tasks/valgrind.sh ;; esac + +case $OS in + WINDOWS) + ;; + LINUX) + ;; + *) + printf "${RED}operating system $OS has no configuration variants\n" + exit 1 + ;; +esac From 75f023a4bda1ac1511c7fd90b0cd9c2bece00be0 Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 8 Feb 2023 03:04:04 +0600 Subject: [PATCH 08/55] updated visual studio project --- kerep.vcxproj | 72 ++++++++++++++++++++++------ src/base/std.h | 2 +- src/base/type_system/base_toString.c | 5 +- src/kprint/kprintf.c | 2 +- 4 files changed, 63 insertions(+), 18 deletions(-) diff --git a/kerep.vcxproj b/kerep.vcxproj index 33807a4..2a8ffb6 100644 --- a/kerep.vcxproj +++ b/kerep.vcxproj @@ -19,46 +19,88 @@ + + + - + + + + + + + + + + + + + + + + + + + + + - - - - - - - - + + + + + - + + + + + + + + + + + + + + + + + + + + + + + @@ -156,7 +198,7 @@ stdc17 Speed - /Zc:twoPhase- /MP -fms-compatibility-version=14 %(AdditionalOptions) + /Zc:twoPhase- /MP -fms-compatibility-version=14 -Wno-clang-analyzer-valist.Uninitialized %(AdditionalOptions) Default stdcpp17 None @@ -182,7 +224,7 @@ stdc17 Speed - /Zc:twoPhase- /MP -fms-compatibility-version=14 %(AdditionalOptions) + /Zc:twoPhase- /MP -fms-compatibility-version=14 -Wno-clang-analyzer-valist.Uninitialized %(AdditionalOptions) MultiThreadedDLL Default stdcpp17 @@ -208,7 +250,7 @@ stdc17 Speed - /Zc:twoPhase- /MP -fms-compatibility-version=14 %(AdditionalOptions) + /Zc:twoPhase- /MP -fms-compatibility-version=14 -Wno-clang-analyzer-valist.Uninitialized %(AdditionalOptions) Default stdcpp17 None @@ -235,7 +277,7 @@ stdc17 Speed - /Zc:twoPhase- /MP -fms-compatibility-version=14 %(AdditionalOptions) + /Zc:twoPhase- /MP -fms-compatibility-version=14 -Wno-clang-analyzer-valist.Uninitialized %(AdditionalOptions) MultiThreadedDLL Default stdcpp17 @@ -252,4 +294,4 @@ - + \ No newline at end of file diff --git a/src/base/std.h b/src/base/std.h index bc45b55..9f6b7d9 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -67,7 +67,7 @@ typedef uint8 bool; #endif #ifndef sprintf_s - #define sprintf_s(BUF, BUFSIZE, FORMAT, ...) sprintf(BUF, FORMAT, ## __VA_ARGS__) + //#define sprintf_s(BUF, BUFSIZE, FORMAT, ...) sprintf(BUF, FORMAT, ## __VA_ARGS__) #endif diff --git a/src/base/type_system/base_toString.c b/src/base/type_system/base_toString.c index 0e33986..68c5324 100644 --- a/src/base/type_system/base_toString.c +++ b/src/base/type_system/base_toString.c @@ -67,7 +67,10 @@ char* toString_uint(uint64 n, bool withPostfix, bool uppercase){ throw("too big precision");\ if(precision==0)\ precision=toString_float_default_precision;\ - int cn=sprintf(str, "%.*f", precision, n);\ + int cn=IFMSC(\ + sprintf_s(str, bufsize, "%.*f", precision, n),\ + sprintf(str, "%.*f", precision, n)\ + );\ /* remove trailing zeroes except .0*/\ while(str[cn-1]=='0' && str[cn-2]!='.')\ cn--;\ diff --git a/src/kprint/kprintf.c b/src/kprint/kprintf.c index baba98e..261c649 100644 --- a/src/kprint/kprintf.c +++ b/src/kprint/kprintf.c @@ -133,7 +133,7 @@ void kprintf(const char* format, ...){ putc(c,stdout); } #if defined(_WIN64) || defined(_WIN32) - end_iteration: + end_iteration:; #endif } va_end(vl); From 6f7eb2ba50d2ae2b73f9abdf741bb46c57d0c84d Mon Sep 17 00:00:00 2001 From: timerix Date: Thu, 9 Feb 2023 00:16:47 +0600 Subject: [PATCH 09/55] launch.json --- .vscode/launch.json | 8 ++++---- .vscode/tasks.json | 26 -------------------------- 2 files changed, 4 insertions(+), 30 deletions(-) diff --git a/.vscode/launch.json b/.vscode/launch.json index 253788a..887a884 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,10 +5,10 @@ "name": "(gdb) Debug", "type": "cppdbg", "request": "launch", - "program": "$kerep.com", + "program": "${workspaceFolder}/bin/kerep.com", + "cwd": "${workspaceFolder}/bin", "preLaunchTask": "build_exec_dbg", "stopAtEntry": false, - "cwd": "${fileDirname}/bin", "externalConsole": false, "MIMode": "gdb", "miDebuggerPath": "gdb", @@ -31,8 +31,8 @@ "request": "launch", "preLaunchTask": "build_exec_dbg", "program": "${workspaceFolder}/bin/kerep.com", + "cwd": "${workspaceFolder}/bin", "stopAtEntry": false, - "cwd": "${workspaceFolder}", "externalConsole": false, "miDebuggerPath": "/usr/bin/gdb", "MIMode": "gdb", @@ -48,8 +48,8 @@ "type": "cppvsdbg", "request": "launch", "preLaunchTask": "build_dbg", - "cwd": "${workspaceFolder}\\bin", "program": "${workspaceFolder}\\bin\\kerep.com", + "cwd": "${workspaceFolder}\\bin", "stopAtEntry": false, "console": "integratedTerminal" } diff --git a/.vscode/tasks.json b/.vscode/tasks.json index 7fb803b..f24315b 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -3,32 +3,6 @@ "version": "2.0.0", "tasks": [ - { - "label": "build_exec", - "detail": "build project", - "type": "cppbuild", - "command": "make", - "args": [ - "build_exec" - ], - "options": { - "cwd": "${workspaceFolder}" - }, - "problemMatcher": ["$gcc"], - "group": { - "kind": "build", - "isDefault": true - }, - "presentation": { - "echo": true, - "reveal": "always", - "focus": true, - "panel": "shared", - "showReuseMessage": false, - "clear": true - } - }, - { "label": "build_exec_dbg", "detail": "build project with debug symbols", From 835bc5e8349f0aab3fdad068549553670ad371bd Mon Sep 17 00:00:00 2001 From: timerix Date: Thu, 9 Feb 2023 01:42:10 +0600 Subject: [PATCH 10/55] filesystem rework --- src/Array/Array.c | 2 +- src/Filesystem/dir.c | 51 +++++++++++++++++++++++++++++++++++- src/Filesystem/dir.h | 18 ++++++++++--- src/Filesystem/file.c | 26 +++++++++++++++++- src/Filesystem/file.h | 12 +++++---- src/Filesystem/io_includes.h | 15 +++++++++++ src/Filesystem/path.c | 19 ++++++++++++++ src/Filesystem/path.h | 2 ++ src/base/cptr.c | 35 +++++++++++++++++++++++++ src/base/cptr.h | 9 +++++++ 10 files changed, 177 insertions(+), 12 deletions(-) create mode 100644 src/Filesystem/io_includes.h diff --git a/src/Array/Array.c b/src/Array/Array.c index f7d4030..e6b3069 100644 --- a/src/Array/Array.c +++ b/src/Array/Array.c @@ -16,7 +16,7 @@ Array_define(uint64) Array_define(Unitype) void Array_Unitype_free_(Array_Unitype* array, bool freeMembers){ - if(freeMembers) for (int32 i; ilength; i++) + if(freeMembers) for (int32 i=0; ilength; i++) Unitype_free(array->values[i]); if(array->allocatedOnHeap) free(array->values); diff --git a/src/Filesystem/dir.c b/src/Filesystem/dir.c index 840a3f3..a46164d 100644 --- a/src/Filesystem/dir.c +++ b/src/Filesystem/dir.c @@ -1 +1,50 @@ -#include "dir.h" \ No newline at end of file +#include "filesystem.h" +#include "io_includes.h" + +bool dir_exists(char* path){ +#if KFS_USE_WINDOWS_H + DWORD dwAttrib = GetFileAttributes(path); + return (bool)( + (dwAttrib != INVALID_FILE_ATTRIBUTES) & // file exists + (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // file is a directory +#else + struct stat stats; + int rez=stat(path, &stats); + return (bool)( + (rez!=-1) & // file exists + (S_ISDIR(stats.st_mode))); // file is a directory +#endif +} + +Maybe dir_create(char* path){ + if (dir_exists(path)) + return MaybeNull; + char* parentDir=path_parentDir(path); + dir_create(parentDir); + free(parentDir); + throw(ERR_NOTIMPLEMENTED); + return MaybeNull; +} + +Maybe dir_delete(char* path){ + throw(ERR_NOTIMPLEMENTED); + return MaybeNull; +} + +Maybe dir_getFiles(char* path, bool recursive){ + throw(ERR_NOTIMPLEMENTED); + return MaybeNull; +} +Maybe dir_getDirs(char* path, bool recursive){ + throw(ERR_NOTIMPLEMENTED); + return MaybeNull; +} + +Maybe dir_findFiles(char* path, char* searchPattern, bool recursive){ + throw(ERR_NOTIMPLEMENTED); + return MaybeNull; +} +Maybe dir_findDirs(char* path, char* searchPattern, bool recursive){ + throw(ERR_NOTIMPLEMENTED); + return MaybeNull; +} diff --git a/src/Filesystem/dir.h b/src/Filesystem/dir.h index 111e4ca..df9a664 100644 --- a/src/Filesystem/dir.h +++ b/src/Filesystem/dir.h @@ -7,11 +7,21 @@ extern "C" { #include "../base/base.h" #include "file.h" -typedef char* DirPath; -Array_declare(DirPath); +bool dir_exists(char* path); +///@return Maybe +Maybe dir_create(char* path); +///@return Maybe +Maybe dir_delete(char* path); -Array_FilePath dir_getFiles(DirPath path); -Array_FilePath dir_findFiles(DirPath path, FilePath searchPattern); +///@return Maybe +Maybe dir_getFiles(char* path, bool recursive); +///@return Maybe +Maybe dir_getDirs(char* path, bool recursive); + +///@return Maybe +Maybe dir_findFiles(char* path, char* searchPattern, bool recursive); +///@return Maybe +Maybe dir_findDirs(char* path, char* searchPattern, bool recursive); #if __cplusplus } diff --git a/src/Filesystem/file.c b/src/Filesystem/file.c index 1c56911..e368d85 100644 --- a/src/Filesystem/file.c +++ b/src/Filesystem/file.c @@ -1,8 +1,32 @@ #include "file.h" #include "../String/StringBuilder.h" +#include "io_includes.h" ktid_define(File); +bool file_exists(char* path){ + + // tryLast(path_throwIfEscapes(path)); + +#if KFS_USE_WINDOWS_H + DWORD dwAttrib = GetFileAttributes(path); + return (bool)( + (dwAttrib != INVALID_FILE_ATTRIBUTES) & // file exists + !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // file is not directory +#else + struct stat stats; + int rez=stat(path, &stats); + return (bool)( + (rez!=-1) & // file exists + !(S_ISDIR(stats.st_mode))); // file is not directory +#endif +} + +Maybe file_delete(char* path, bool recursive){ + throw(ERR_NOTIMPLEMENTED); + return MaybeNull; +} + char* FileOpenMode_toStr(FileOpenMode m){ char* p; switch(m){ @@ -18,7 +42,7 @@ char* FileOpenMode_toStr(FileOpenMode m){ return p; } -Maybe file_open(FilePath path, FileOpenMode mode){ +Maybe file_open(char* path, FileOpenMode mode){ File* file=fopen(path, FileOpenMode_toStr(mode)); if(!file) safethrow(cptr_concat("can't open file ", (char*)path),;); diff --git a/src/Filesystem/file.h b/src/Filesystem/file.h index da0f45a..8498b52 100644 --- a/src/Filesystem/file.h +++ b/src/Filesystem/file.h @@ -8,11 +8,14 @@ extern "C" { #include "../Array/Array.h" #include "../String/string.h" -typedef char* FilePath; -Array_declare(FilePath); typedef FILE File; ktid_declare(File); +bool file_exists(char* path); + +///@return Maybe +Maybe file_delete(char* path, bool recursive); + PACK_ENUM(FileOpenMode, // open a file for reading FileOpenMode_Read=1, @@ -24,14 +27,13 @@ PACK_ENUM(FileOpenMode, FileOpenMode_ReadWrite=FileOpenMode_Read|FileOpenMode_Write, // opens file for readng/writing additional data to the end / creates new file FileOpenMode_ReadAppend=FileOpenMode_Read|FileOpenMode_Append - ) - +) /// @brief opens file /// @param path path to file /// @param mode Read/Write/Append/ReadWrite/ReadAppend /// @return Maybe -Maybe file_open(FilePath path, FileOpenMode mode); +Maybe file_open(char* path, FileOpenMode mode); /// @brief closes file descriptor /// @return Maybe diff --git a/src/Filesystem/io_includes.h b/src/Filesystem/io_includes.h new file mode 100644 index 0000000..033adf6 --- /dev/null +++ b/src/Filesystem/io_includes.h @@ -0,0 +1,15 @@ +#include "../base/std.h" + +#if defined(_WIN64) || defined(_WIN32) +#define KFS_USE_WINDOWS_H 1 +#else +#define KFS_USE_WINDOWS_H 0 +#endif + +#if KFS_USE_WINDOWS_H +#include +#else +#include +#include +#include +#endif diff --git a/src/Filesystem/path.c b/src/Filesystem/path.c index 0a8757b..0ebf386 100644 --- a/src/Filesystem/path.c +++ b/src/Filesystem/path.c @@ -53,3 +53,22 @@ Maybe path_throwIfEscapes(char* path){ safethrow(cptr_concat("path <",path,"> uses <..>, that's not allowed"),); return MaybeNull; } + +char* path_parentDir(char* dir){ + throw(ERR_NOTIMPLEMENTED); + char* copy=cptr_copy(dir); + uint32 length=cptr_length(copy); + int i=cptr_lastIndexOfChar(copy,path_sep); + if(i==length-1){ + copy[length-1]=0; + i=cptr_lastIndexOfChar(copy,path_sep); + } + if(i==-1){ + free(copy); + copy=malloc(3); + copy[0]='.'; + copy[1]=path_sep; + copy[2]=0; + } + return copy; +} diff --git a/src/Filesystem/path.h b/src/Filesystem/path.h index 7c3b049..d39c97e 100644 --- a/src/Filesystem/path.h +++ b/src/Filesystem/path.h @@ -31,6 +31,8 @@ char* path_fixSeparators(char* path); /// @return Maybe Maybe path_throwIfEscapes(char* path); +char* path_parentDir(char* dir); + #if __cplusplus } #endif \ No newline at end of file diff --git a/src/base/cptr.c b/src/base/cptr.c index f098fdc..7042a9d 100644 --- a/src/base/cptr.c +++ b/src/base/cptr.c @@ -57,6 +57,41 @@ uint32 cptr_indexOf(char* ptr, char* fragment){ if(fragment[fi]==0) return si-fi+1; } + else fi=0; + } + return -1; +} +uint32 cptr_indexOfChar(char* ptr, char fragment){ + char sc=*ptr; + for(int si=0; sc!=0; si++){ + sc=ptr[si]; + if(sc==fragment){ + return si; + } + } + return -1; +} +uint32 cptr_lastIndexOf(char* ptr, char* fragment){ + char sc=*ptr; + int fi_last=cptr_length(fragment)-1; + for(int si=cptr_length(ptr)-1, fi=fi_last; si>=0; si--){ + sc=ptr[si]; + if(sc==fragment[fi]){ + if(fi==0) + return si; + fi--; + } + else fi=fi_last; + } + return -1; +} +uint32 cptr_lastIndexOfChar(char* ptr, char fragment){ + char sc=*ptr; + for(int si=cptr_length(ptr)-1; si>=0; si--){ + sc=ptr[si]; + if(sc==fragment){ + return si; + } } return -1; } diff --git a/src/base/cptr.h b/src/base/cptr.h index 255ee0a..9b7babb 100644 --- a/src/base/cptr.h +++ b/src/base/cptr.h @@ -25,6 +25,15 @@ bool cptr_endsWith(char* ptr, char* fragment); /// @brief search for in /// @return index of first inclusion or -1 if not found uint32 cptr_indexOf(char* ptr, char* fragment); +/// @brief search for in +/// @return index of first inclusion or -1 if not found +uint32 cptr_indexOfChar(char* ptr, char fragment); +/// @brief search for in +/// @return index of last inclusion or -1 if not found +uint32 cptr_lastIndexOf(char* ptr, char* fragment); +/// @brief search for in +/// @return index of last inclusion or -1 if not found +uint32 cptr_lastIndexOfChar(char* ptr, char fragment); static inline bool cptr_contains(char* ptr, char* fragment){ // if(cptr_indexOf(ptr, fragment)==-1) From 609dfcf3ed6d454599165e8dd443ba30b95771b2 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 10 Feb 2023 12:58:17 +0600 Subject: [PATCH 11/55] dir_create --- cbuild | 2 +- src/Filesystem/dir.c | 47 ++++++++++++++++++++++++++++++------------- src/Filesystem/dir.h | 14 ++++++------- src/Filesystem/file.c | 16 +++++++++------ src/Filesystem/file.h | 6 +++--- src/Filesystem/path.c | 10 ++++----- src/Filesystem/path.h | 4 ++-- src/base/errors.h | 42 ++++++++++++++++++++++++++------------ 8 files changed, 89 insertions(+), 52 deletions(-) diff --git a/cbuild b/cbuild index 15a9661..1b38b43 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit 15a9661c467facf65b2155eab3fad1ecf5f0c945 +Subproject commit 1b38b43c547246ca358a676cee5eda85598ab949 diff --git a/src/Filesystem/dir.c b/src/Filesystem/dir.c index a46164d..78f9cb0 100644 --- a/src/Filesystem/dir.c +++ b/src/Filesystem/dir.c @@ -1,7 +1,14 @@ #include "filesystem.h" #include "io_includes.h" +#include "../kprint/kprint.h" -bool dir_exists(char* path){ +bool dir_exists(const char* path){ + if(path[0]=='.'){ + if(path[1]==0 || (path[1]==path_sep && path[1]==0)) + return true; // dir . or ./ always exists + // else if(path[1]=='.' && path[2]==path_sep) + //TODO path_resolve because windows doesnt recognize .\ pattern + } #if KFS_USE_WINDOWS_H DWORD dwAttrib = GetFileAttributes(path); return (bool)( @@ -16,35 +23,47 @@ bool dir_exists(char* path){ #endif } -Maybe dir_create(char* path){ +Maybe dir_create(const char* path){ if (dir_exists(path)) return MaybeNull; char* parentDir=path_parentDir(path); dir_create(parentDir); free(parentDir); +#if KFS_USE_WINDOWS_H + if(!CreateDirectory(path, NULL)) +#else + if(mkdir(path, 0777) == -1) +#endif + { + char err[512]; + IFWIN( + sprintf_s(err, 512, "can't create dicectory <%s>", path), + sprintf(err, "can't create dicectory <%s>", path)); + safethrow(err,;); + } + + return MaybeNull; +} + +Maybe dir_delete(const char* path){ throw(ERR_NOTIMPLEMENTED); return MaybeNull; } -Maybe dir_delete(char* path){ +Maybe dir_getFiles(const char* path, bool recursive){ + throw(ERR_NOTIMPLEMENTED); + return MaybeNull; +} +Maybe dir_getDirs(const char* path, bool recursive){ throw(ERR_NOTIMPLEMENTED); return MaybeNull; } -Maybe dir_getFiles(char* path, bool recursive){ +Maybe dir_findFiles(const char* path, char* searchPattern, bool recursive){ throw(ERR_NOTIMPLEMENTED); return MaybeNull; } -Maybe dir_getDirs(char* path, bool recursive){ - throw(ERR_NOTIMPLEMENTED); - return MaybeNull; -} - -Maybe dir_findFiles(char* path, char* searchPattern, bool recursive){ - throw(ERR_NOTIMPLEMENTED); - return MaybeNull; -} -Maybe dir_findDirs(char* path, char* searchPattern, bool recursive){ +Maybe dir_findDirs(const char* path, char* searchPattern, bool recursive){ throw(ERR_NOTIMPLEMENTED); return MaybeNull; } diff --git a/src/Filesystem/dir.h b/src/Filesystem/dir.h index df9a664..3821229 100644 --- a/src/Filesystem/dir.h +++ b/src/Filesystem/dir.h @@ -7,21 +7,21 @@ extern "C" { #include "../base/base.h" #include "file.h" -bool dir_exists(char* path); +bool dir_exists(const char* path); ///@return Maybe -Maybe dir_create(char* path); +Maybe dir_create(const char* path); ///@return Maybe -Maybe dir_delete(char* path); +Maybe dir_delete(const char* path); ///@return Maybe -Maybe dir_getFiles(char* path, bool recursive); +Maybe dir_getFiles(const char* path, bool recursive); ///@return Maybe -Maybe dir_getDirs(char* path, bool recursive); +Maybe dir_getDirs(const char* path, bool recursive); ///@return Maybe -Maybe dir_findFiles(char* path, char* searchPattern, bool recursive); +Maybe dir_findFiles(const char* path, char* searchPattern, bool recursive); ///@return Maybe -Maybe dir_findDirs(char* path, char* searchPattern, bool recursive); +Maybe dir_findDirs(const char* path, char* searchPattern, bool recursive); #if __cplusplus } diff --git a/src/Filesystem/file.c b/src/Filesystem/file.c index e368d85..cbedced 100644 --- a/src/Filesystem/file.c +++ b/src/Filesystem/file.c @@ -1,12 +1,16 @@ -#include "file.h" +#include "filesystem.h" #include "../String/StringBuilder.h" #include "io_includes.h" ktid_define(File); -bool file_exists(char* path){ - - // tryLast(path_throwIfEscapes(path)); +bool file_exists(const char* path){ + if(path[0]=='.'){ + if(path[1]==0 || (path[1]==path_sep && path[2]==0)) + return false; // . or ./ is not a file + // else if(path[1]=='.' && path[2]==path_sep) + //TODO path_resolve because windows doesnt recognize .\ pattern + } #if KFS_USE_WINDOWS_H DWORD dwAttrib = GetFileAttributes(path); @@ -22,7 +26,7 @@ bool file_exists(char* path){ #endif } -Maybe file_delete(char* path, bool recursive){ +Maybe file_delete(const char* path, bool recursive){ throw(ERR_NOTIMPLEMENTED); return MaybeNull; } @@ -42,7 +46,7 @@ char* FileOpenMode_toStr(FileOpenMode m){ return p; } -Maybe file_open(char* path, FileOpenMode mode){ +Maybe file_open(const char* path, FileOpenMode mode){ File* file=fopen(path, FileOpenMode_toStr(mode)); if(!file) safethrow(cptr_concat("can't open file ", (char*)path),;); diff --git a/src/Filesystem/file.h b/src/Filesystem/file.h index 8498b52..febdbb0 100644 --- a/src/Filesystem/file.h +++ b/src/Filesystem/file.h @@ -11,10 +11,10 @@ extern "C" { typedef FILE File; ktid_declare(File); -bool file_exists(char* path); +bool file_exists(const char* path); ///@return Maybe -Maybe file_delete(char* path, bool recursive); +Maybe file_delete(const char* path, bool recursive); PACK_ENUM(FileOpenMode, // open a file for reading @@ -33,7 +33,7 @@ PACK_ENUM(FileOpenMode, /// @param path path to file /// @param mode Read/Write/Append/ReadWrite/ReadAppend /// @return Maybe -Maybe file_open(char* path, FileOpenMode mode); +Maybe file_open(const char* path, FileOpenMode mode); /// @brief closes file descriptor /// @return Maybe diff --git a/src/Filesystem/path.c b/src/Filesystem/path.c index 0ebf386..5cd2d6e 100644 --- a/src/Filesystem/path.c +++ b/src/Filesystem/path.c @@ -37,7 +37,7 @@ char* __path_concat(uint16 n, ...){ return output; } -char* path_fixSeparators(char* path){ +char* path_fixSeparators(const char* path){ char* pathCopy=cptr_copy(path); char c; while((c=*pathCopy)){ @@ -48,14 +48,13 @@ char* path_fixSeparators(char* path){ return pathCopy; } -Maybe path_throwIfEscapes(char* path){ +Maybe path_throwIfEscapes(const char* path){ if(cptr_contains(path,"..")) safethrow(cptr_concat("path <",path,"> uses <..>, that's not allowed"),); return MaybeNull; } char* path_parentDir(char* dir){ - throw(ERR_NOTIMPLEMENTED); char* copy=cptr_copy(dir); uint32 length=cptr_length(copy); int i=cptr_lastIndexOfChar(copy,path_sep); @@ -65,10 +64,9 @@ char* path_parentDir(char* dir){ } if(i==-1){ free(copy); - copy=malloc(3); + copy=malloc(2); copy[0]='.'; - copy[1]=path_sep; - copy[2]=0; + copy[1]=0; } return copy; } diff --git a/src/Filesystem/path.h b/src/Filesystem/path.h index d39c97e..b9ab2ab 100644 --- a/src/Filesystem/path.h +++ b/src/Filesystem/path.h @@ -22,14 +22,14 @@ char* __path_concat(uint16 n, ...); /// @brief fixes path separators /// @param cstr where can be /// @return new cstr with correct separators -char* path_fixSeparators(char* path); +char* path_fixSeparators(const char* path); #define path_resolve(PATH_PARTS...) path_fixSeparators(path_concat(PATH_PARTS)) /// @brief calls safethrow() if finds escape sequense in path /// @param path cstr where can be <..> /// @return Maybe -Maybe path_throwIfEscapes(char* path); +Maybe path_throwIfEscapes(const char* path); char* path_parentDir(char* dir); diff --git a/src/base/errors.h b/src/base/errors.h index 7c46733..90d9751 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -50,24 +50,40 @@ char* __unknownErr( ); )(E) #if __cplusplus - #define throw_id(E) __EXIT(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__))) - #define throw_msg(E) __EXIT(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__))) +#define throw_id(E) __EXIT(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__))) +#define throw_msg(E) __EXIT(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__))) + +#define safethrow_id(E, FREEMEM) { FREEMEM;\ + __RETURN_EXCEPTION(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__)));\ +} +#define safethrow_msg(E, FREEMEM) { FREEMEM;\ + __RETURN_EXCEPTION(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__)));\ +} + +#define try_cpp(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){\ + freeMem;\ + _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ + return _rezult;\ +} + #else - #define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))) - #define safethrow(E, FREEMEM) { FREEMEM; __RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))); } +#define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))) + +#define safethrow(E, FREEMEM) { FREEMEM;\ + __RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)));\ +} #define try(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){\ - freeMem;\ - _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ - return _rezult;\ - } + freeMem;\ + _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ + return _rezult;\ +} +#endif #define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\ - _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ - __EXIT(_rezult.errmsg);\ - } - -#endif + _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ + __EXIT(_rezult.errmsg);\ +} #if __cplusplus } From 305854e72108f1af474dfbea1e8cd0d941d619c6 Mon Sep 17 00:00:00 2001 From: timerix Date: Sat, 11 Feb 2023 12:19:05 +0600 Subject: [PATCH 12/55] i32 --- src/Array/Array.c | 22 ++--- src/Array/Array.h | 20 ++-- src/Array/Array_declare.h | 6 +- src/Autoarr/Autoarr.c | 20 ++-- src/Autoarr/Autoarr.h | 20 ++-- src/Autoarr/Autoarr_KVPair_exported.c | 10 +- src/Autoarr/Autoarr_Unitype.h | 6 +- src/Autoarr/Autoarr_Unitype_exported.c | 10 +- src/Autoarr/Autoarr_declare.h | 20 ++-- src/Autoarr/Autoarr_define.h | 14 +-- src/DtsodParser/DtsodV24_deserialize.c | 12 +-- src/DtsodParser/DtsodV24_exported.c | 4 +- src/DtsodParser/DtsodV24_serialize.c | 20 ++-- src/Filesystem/dir.c | 2 +- src/Filesystem/file.c | 24 ++--- src/Filesystem/file.h | 8 +- src/Filesystem/path.c | 16 ++-- src/Filesystem/path.h | 2 +- src/HashFunctions/hash.c | 20 ++-- src/HashFunctions/hash.h | 4 +- src/Hashtable/Hashtable.c | 28 +++--- src/Hashtable/Hashtable.h | 8 +- src/SearchTree/SearchTree.c | 20 ++-- src/String/StringBuilder.c | 26 ++--- src/String/StringBuilder.h | 8 +- src/String/string.c | 4 +- src/String/string.h | 2 +- src/base/cptr.c | 42 ++++----- src/base/cptr.h | 16 ++-- src/base/endian.c | 2 +- src/base/errors.c | 4 +- src/base/errors.h | 4 +- src/base/optime.h | 12 +-- src/base/std.h | 23 ++--- src/base/type_system/base_toString.c | 94 +++++++++---------- src/base/type_system/base_toString.h | 40 ++++---- src/base/type_system/init.c | 60 ++++++------ src/base/type_system/ktDescriptor.h | 4 +- src/base/type_system/kt_functions.c | 22 ++--- src/base/type_system/kt_functions.h | 22 ++--- src/base/type_system/ktid.h | 2 +- src/base/type_system/unitype.c | 8 +- src/base/type_system/unitype.h | 12 +-- src/kprint/README.md | 20 ++-- src/kprint/kprint.c | 28 +++--- src/kprint/kprint.h | 24 ++--- src/kprint/kprint_format.h | 2 +- src/kprint/kprint_format.md | 20 ++-- src/kprint/kprintf.c | 22 ++--- src/random/krandom.h | 18 ++-- src/random/splitmix64/splitmix64.c | 6 +- src/random/splitmix64/splitmix64.h | 6 +- src/random/xoroshiro/32bitValue/xoroshiro64.h | 10 +- .../xoroshiro/32bitValue/xoroshiro64star.c | 16 ++-- .../32bitValue/xoroshiro64starstar.c | 10 +- .../xoroshiro/64bitValue/xoroshiro128.h | 10 +- .../xoroshiro/64bitValue/xoroshiro128plus.c | 16 ++-- .../64bitValue/xoroshiro128plusplus.c | 12 +-- .../64bitValue/xoroshiro128starstar.c | 12 +-- src/random/xoshiro/32bitValue/xoshiro128.h | 12 +-- .../xoshiro/32bitValue/xoshiro128plus.c | 14 +-- .../xoshiro/32bitValue/xoshiro128plusplus.c | 8 +- .../xoshiro/32bitValue/xoshiro128starstar.c | 8 +- src/random/xoshiro/64bitValue/xoshiro256.h | 10 +- .../xoshiro/64bitValue/xoshiro256plus.c | 10 +- .../xoshiro/64bitValue/xoshiro256plusplus.c | 10 +- .../xoshiro/64bitValue/xoshiro256starstar.c | 10 +- tests/main.cpp | 2 +- tests/test_autoarr-vs-vector.cpp | 12 +-- tests/test_autoarr.c | 20 ++-- tests/test_dtsod.c | 4 +- tests/test_hash_functions.c | 12 +-- tests/test_hashtable.c | 18 ++-- tests/test_kprint.c | 4 +- tests/test_kprint_colors.c | 4 +- tests/test_rng_algorithms.c | 8 +- tests/test_searchtree.c | 6 +- 77 files changed, 565 insertions(+), 562 deletions(-) diff --git a/src/Array/Array.c b/src/Array/Array.c index e6b3069..c6a3145 100644 --- a/src/Array/Array.c +++ b/src/Array/Array.c @@ -2,21 +2,21 @@ Array_define(char) Array_define(bool) -Array_define(float32) -Array_define(float64) -Array_define(int8) -Array_define(uint8) -Array_define(int16) -Array_define(uint16) -Array_define(int32) -Array_define(uint32) -Array_define(int64) -Array_define(uint64) +Array_define(f32) +Array_define(f64) +Array_define(i8) +Array_define(u8) +Array_define(i16) +Array_define(u16) +Array_define(i32) +Array_define(u32) +Array_define(i64) +Array_define(u64) Array_define(Unitype) void Array_Unitype_free_(Array_Unitype* array, bool freeMembers){ - if(freeMembers) for (int32 i=0; ilength; i++) + if(freeMembers) for (i32 i=0; ilength; i++) Unitype_free(array->values[i]); if(array->allocatedOnHeap) free(array->values); diff --git a/src/Array/Array.h b/src/Array/Array.h index 5c8f388..31b00f6 100644 --- a/src/Array/Array.h +++ b/src/Array/Array.h @@ -9,16 +9,16 @@ extern "C" { Array_declare(char) Array_declare(bool) -Array_declare(float32) -Array_declare(float64) -Array_declare(int8) -Array_declare(uint8) -Array_declare(int16) -Array_declare(uint16) -Array_declare(int32) -Array_declare(uint32) -Array_declare(int64) -Array_declare(uint64) +Array_declare(f32) +Array_declare(f64) +Array_declare(i8) +Array_declare(u8) +Array_declare(i16) +Array_declare(u16) +Array_declare(i32) +Array_declare(u32) +Array_declare(i64) +Array_declare(u64) Array_declare(Unitype) diff --git a/src/Array/Array_declare.h b/src/Array/Array_declare.h index 2c7a37f..089874d 100644 --- a/src/Array/Array_declare.h +++ b/src/Array/Array_declare.h @@ -9,13 +9,13 @@ extern "C" { #define Array_declare(type)\ typedef struct Array_##type{\ type* values;\ - uint32 length;\ + u32 length;\ bool allocatedOnHeap;\ } Array_##type;\ \ ktid_declare(Array_##type);\ \ -static inline Array_##type Array_##type##_allocValues(uint32 length){\ +static inline Array_##type Array_##type##_allocValues(u32 length){\ return (Array_##type) {\ .values=(type*)malloc(sizeof(type)*length),\ .length=length,\ @@ -23,7 +23,7 @@ static inline Array_##type Array_##type##_allocValues(uint32 length){\ };\ }\ \ -static inline Array_##type Array_##type##_fromBuffer(type* buffer, uint32 bufferLength, bool allocatedOnHeap){\ +static inline Array_##type Array_##type##_fromBuffer(type* buffer, u32 bufferLength, bool allocatedOnHeap){\ return (Array_##type) {\ .values=buffer,\ .length=bufferLength,\ diff --git a/src/Autoarr/Autoarr.c b/src/Autoarr/Autoarr.c index 274f777..84c8c21 100644 --- a/src/Autoarr/Autoarr.c +++ b/src/Autoarr/Autoarr.c @@ -2,13 +2,13 @@ Autoarr_define(char) Autoarr_define(bool) -Autoarr_define(float32) -Autoarr_define(float64) -Autoarr_define(uint8) -Autoarr_define(int8) -Autoarr_define(uint16) -Autoarr_define(int16) -Autoarr_define(uint32) -Autoarr_define(int32) -Autoarr_define(uint64) -Autoarr_define(int64) +Autoarr_define(f32) +Autoarr_define(f64) +Autoarr_define(u8) +Autoarr_define(i8) +Autoarr_define(u16) +Autoarr_define(i16) +Autoarr_define(u32) +Autoarr_define(i32) +Autoarr_define(u64) +Autoarr_define(i64) diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h index e22059a..23e6f11 100644 --- a/src/Autoarr/Autoarr.h +++ b/src/Autoarr/Autoarr.h @@ -10,16 +10,16 @@ extern "C" { Autoarr_declare(char) Autoarr_declare(bool) -Autoarr_declare(float32) -Autoarr_declare(float64) -Autoarr_declare(int8) -Autoarr_declare(uint8) -Autoarr_declare(int16) -Autoarr_declare(uint16) -Autoarr_declare(int32) -Autoarr_declare(uint32) -Autoarr_declare(int64) -Autoarr_declare(uint64) +Autoarr_declare(f32) +Autoarr_declare(f64) +Autoarr_declare(i8) +Autoarr_declare(u8) +Autoarr_declare(i16) +Autoarr_declare(u16) +Autoarr_declare(i32) +Autoarr_declare(u32) +Autoarr_declare(i64) +Autoarr_declare(u64) #if __cplusplus diff --git a/src/Autoarr/Autoarr_KVPair_exported.c b/src/Autoarr/Autoarr_KVPair_exported.c index 8bfbd56..77a6f69 100644 --- a/src/Autoarr/Autoarr_KVPair_exported.c +++ b/src/Autoarr/Autoarr_KVPair_exported.c @@ -5,7 +5,7 @@ extern "C" { #include "Autoarr.h" #include "../Hashtable/KeyValuePair.h" -EXPORT void CALL kerep_Autoarr_KVPair_create(uint16 max_blocks_count, uint16 max_block_length, Autoarr_KVPair** output){ +EXPORT void CALL kerep_Autoarr_KVPair_create(u16 max_blocks_count, u16 max_block_length, Autoarr_KVPair** output){ *output=Autoarr_create(KVPair, max_blocks_count, max_block_length); } @@ -13,7 +13,7 @@ EXPORT void CALL kerep_Autoarr_KVPair_free(Autoarr_KVPair* ar){ Autoarr_free(ar, true); } -EXPORT void CALL kerep_Autoarr_KVPair_get(Autoarr_KVPair* ar, uint32 index, KVPair* output){ +EXPORT void CALL kerep_Autoarr_KVPair_get(Autoarr_KVPair* ar, u32 index, KVPair* output){ *output=Autoarr_get(ar, index); } @@ -21,15 +21,15 @@ EXPORT void CALL kerep_Autoarr_KVPair_add(Autoarr_KVPair* ar, KVPair element){ Autoarr_add(ar, element); } -EXPORT void CALL kerep_Autoarr_KVPair_set(Autoarr_KVPair* ar, uint32 index, KVPair element){ +EXPORT void CALL kerep_Autoarr_KVPair_set(Autoarr_KVPair* ar, u32 index, KVPair element){ Autoarr_set(ar, index, element); } -EXPORT void CALL kerep_Autoarr_KVPair_length(Autoarr_KVPair* ar, uint32* output){ +EXPORT void CALL kerep_Autoarr_KVPair_length(Autoarr_KVPair* ar, u32* output){ *output=Autoarr_length(ar); } -EXPORT void CALL kerep_Autoarr_KVPair_max_length(Autoarr_KVPair* ar, uint32* output){ +EXPORT void CALL kerep_Autoarr_KVPair_max_length(Autoarr_KVPair* ar, u32* output){ *output=Autoarr_max_length(ar); } diff --git a/src/Autoarr/Autoarr_Unitype.h b/src/Autoarr/Autoarr_Unitype.h index cf49c7c..fdcd4e5 100644 --- a/src/Autoarr/Autoarr_Unitype.h +++ b/src/Autoarr/Autoarr_Unitype.h @@ -16,12 +16,12 @@ void ____Autoarr_free_Unitype_(void* ar); #define Autoarr_foreach(ar,elem,codeblock)({\ if(ar->blocks_count>0) {\ typeof(**ar->values) elem;\ - for(uint32 blockI=0;blockIblocks_count-1;blockI++)\ - for(uint32 elemI=0;elemImax_block_length;elemI++){\ + for(u32 blockI=0;blockIblocks_count-1;blockI++)\ + for(u32 elemI=0;elemImax_block_length;elemI++){\ elem=ar->values[blockI][elemI];\ (codeblock);\ }\ - for(uint32 elemI=0;elemIblock_length;elemI++){\ + for(u32 elemI=0;elemIblock_length;elemI++){\ elem=ar->values[ar->blocks_count-1][elemI];\ (codeblock);\ }\ diff --git a/src/Autoarr/Autoarr_Unitype_exported.c b/src/Autoarr/Autoarr_Unitype_exported.c index 2aded2e..6db8a6d 100644 --- a/src/Autoarr/Autoarr_Unitype_exported.c +++ b/src/Autoarr/Autoarr_Unitype_exported.c @@ -4,7 +4,7 @@ extern "C" { #include "Autoarr.h" -EXPORT void CALL kerep_Autoarr_Unitype_create(uint16 max_blocks_count, uint16 max_block_length, Autoarr_Unitype** output){ +EXPORT void CALL kerep_Autoarr_Unitype_create(u16 max_blocks_count, u16 max_block_length, Autoarr_Unitype** output){ *output=Autoarr_create(Unitype, max_blocks_count, max_block_length); } @@ -12,7 +12,7 @@ EXPORT void CALL kerep_Autoarr_Unitype_free(Autoarr_Unitype* ar){ Autoarr_free(ar, true); } -EXPORT void CALL kerep_Autoarr_Unitype_get(Autoarr_Unitype* ar, uint32 index, Unitype* output){ +EXPORT void CALL kerep_Autoarr_Unitype_get(Autoarr_Unitype* ar, u32 index, Unitype* output){ *output=Autoarr_get(ar, index); } @@ -20,15 +20,15 @@ EXPORT void CALL kerep_Autoarr_Unitype_add(Autoarr_Unitype* ar, Unitype element) Autoarr_add(ar, element); } -EXPORT void CALL kerep_Autoarr_Unitype_set(Autoarr_Unitype* ar, uint32 index, Unitype element){ +EXPORT void CALL kerep_Autoarr_Unitype_set(Autoarr_Unitype* ar, u32 index, Unitype element){ Autoarr_set(ar, index, element); } -EXPORT void CALL kerep_Autoarr_Unitype_length(Autoarr_Unitype* ar, uint32* output){ +EXPORT void CALL kerep_Autoarr_Unitype_length(Autoarr_Unitype* ar, u32* output){ *output=Autoarr_length(ar); } -EXPORT void CALL kerep_Autoarr_Unitype_max_length(Autoarr_Unitype* ar, uint32* output){ +EXPORT void CALL kerep_Autoarr_Unitype_max_length(Autoarr_Unitype* ar, u32* output){ *output=Autoarr_max_length(ar); } diff --git a/src/Autoarr/Autoarr_declare.h b/src/Autoarr/Autoarr_declare.h index dc16829..d449b5b 100644 --- a/src/Autoarr/Autoarr_declare.h +++ b/src/Autoarr/Autoarr_declare.h @@ -12,25 +12,25 @@ struct Autoarr_##type;\ \ typedef struct {\ void (*add)(struct Autoarr_##type* ar, type element);\ - type (*get)(struct Autoarr_##type* ar, uint32 index);\ - type* (*getptr)(struct Autoarr_##type* ar, uint32 index);\ - void (*set)(struct Autoarr_##type* ar, uint32 index, type element);\ + type (*get)(struct Autoarr_##type* ar, u32 index);\ + type* (*getptr)(struct Autoarr_##type* ar, u32 index);\ + void (*set)(struct Autoarr_##type* ar, u32 index, type element);\ void (*freear)(struct Autoarr_##type* ar, bool freePtr);\ type* (*toArray)(struct Autoarr_##type* ar);\ } __functions_list_t_##type;\ \ typedef struct Autoarr_##type{\ - uint16 blocks_count;\ - uint16 max_blocks_count;\ - uint16 block_length;\ - uint16 max_block_length;\ + u16 blocks_count;\ + u16 max_blocks_count;\ + u16 block_length;\ + u16 max_block_length;\ type** values;\ __functions_list_t_##type* functions;\ } Autoarr_##type;\ \ ktid_declare(Autoarr_##type);\ \ -Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length);\ +Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length);\ void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr);\ void ____Autoarr_free_##type(void* ar); @@ -52,10 +52,10 @@ void ____Autoarr_free_##type(void* ar); autoarr->functions->toArray(autoarr) #define Autoarr_length(autoarr) \ - (uint32)(!autoarr->blocks_count ? 0 : \ + (u32)(!autoarr->blocks_count ? 0 : \ autoarr->max_block_length*(autoarr->blocks_count-1)+autoarr->block_length) #define Autoarr_max_length(autoarr)\ - (uint32)(autoarr->max_block_length*autoarr->max_blocks_count) + (u32)(autoarr->max_block_length*autoarr->max_blocks_count) #define Autoarr_pop(AR){\ if(AR->block_length==1){\ diff --git a/src/Autoarr/Autoarr_define.h b/src/Autoarr/Autoarr_define.h index 32b665a..9e82e4d 100644 --- a/src/Autoarr/Autoarr_define.h +++ b/src/Autoarr/Autoarr_define.h @@ -26,23 +26,23 @@ create_block:\ ar->block_length++;\ }\ \ -type __Autoarr_get_##type(Autoarr_##type* ar, uint32 index){\ +type __Autoarr_get_##type(Autoarr_##type* ar, u32 index){\ if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\ return ar->values[index/ar->max_block_length][index%ar->max_block_length];\ }\ \ -type* __Autoarr_getptr_##type(Autoarr_##type* ar, uint32 index){\ +type* __Autoarr_getptr_##type(Autoarr_##type* ar, u32 index){\ if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\ return ar->values[index/ar->max_block_length]+(index%ar->max_block_length);\ }\ \ -void __Autoarr_set_##type(Autoarr_##type* ar, uint32 index, type element){\ +void __Autoarr_set_##type(Autoarr_##type* ar, u32 index, type element){\ if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\ ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\ }\ \ void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr){\ - for(uint16 i=0; iblocks_count;i++)\ + for(u16 i=0; iblocks_count;i++)\ free(ar->values[i]); \ free(ar->values);\ if(freePtr) free(ar);\ @@ -52,9 +52,9 @@ void ____Autoarr_free_##type(void* ar){\ }\ \ type* __Autoarr_toArray_##type(Autoarr_##type* ar){\ - uint32 length=Autoarr_length(ar);\ + u32 length=Autoarr_length(ar);\ type* array=malloc(length * sizeof(type));\ - for(uint32 i=0; irows[h]; } diff --git a/src/DtsodParser/DtsodV24_serialize.c b/src/DtsodParser/DtsodV24_serialize.c index f68e706..0ec6444 100644 --- a/src/DtsodParser/DtsodV24_serialize.c +++ b/src/DtsodParser/DtsodV24_serialize.c @@ -4,18 +4,18 @@ typedef struct SerializeSharedData{ StringBuilder* sh_builder; - uint8 sh_tabs; + u8 sh_tabs; } SerializeSharedData; #define b shared->sh_builder #define tabs shared->sh_tabs -Maybe __serialize(StringBuilder* _b, uint8 _tabs, Hashtable* dtsod); +Maybe __serialize(StringBuilder* _b, u8 _tabs, Hashtable* dtsod); #define addc(C) StringBuilder_append_char(b,C) void __AppendTabs(SerializeSharedData* shared) { - for (uint8 t = 0; t < tabs; t++) + for (u8 t = 0; t < tabs; t++) addc( '\t'); }; #define AppendTabs() __AppendTabs(shared) @@ -23,15 +23,15 @@ void __AppendTabs(SerializeSharedData* shared) { Maybe __AppendValue(SerializeSharedData* shared, Unitype u); #define AppendValue(UNI) __AppendValue(shared, UNI) Maybe __AppendValue(SerializeSharedData* shared, Unitype u){ - if(u.typeId==ktid_name(int64)){ - StringBuilder_append_int64(b,u.Int64); + if(u.typeId==ktid_name(i64)){ + StringBuilder_append_i64(b,u.Int64); } - else if(u.typeId==ktid_name(uint64)){ - StringBuilder_append_uint64(b,u.UInt64); + else if(u.typeId==ktid_name(u64)){ + StringBuilder_append_u64(b,u.UInt64); addc('u'); } - else if(u.typeId==ktid_name(float64)){ - StringBuilder_append_float64(b,u.Float64); + else if(u.typeId==ktid_name(f64)){ + StringBuilder_append_f64(b,u.Float64); addc('f'); } else if(u.typeId==ktid_ptrName(char)){ @@ -102,7 +102,7 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){ return MaybeNull; }; -Maybe __serialize(StringBuilder* _b, uint8 _tabs, Hashtable* dtsod){ +Maybe __serialize(StringBuilder* _b, u8 _tabs, Hashtable* dtsod){ SerializeSharedData _shared={ .sh_builder=_b, .sh_tabs=_tabs diff --git a/src/Filesystem/dir.c b/src/Filesystem/dir.c index 78f9cb0..c98e732 100644 --- a/src/Filesystem/dir.c +++ b/src/Filesystem/dir.c @@ -16,7 +16,7 @@ bool dir_exists(const char* path){ (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // file is a directory #else struct stat stats; - int rez=stat(path, &stats); + i32 rez=stat(path, &stats); return (bool)( (rez!=-1) & // file exists (S_ISDIR(stats.st_mode))); // file is a directory diff --git a/src/Filesystem/file.c b/src/Filesystem/file.c index cbedced..7e7c496 100644 --- a/src/Filesystem/file.c +++ b/src/Filesystem/file.c @@ -19,7 +19,7 @@ bool file_exists(const char* path){ !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // file is not directory #else struct stat stats; - int rez=stat(path, &stats); + i32 rez=stat(path, &stats); return (bool)( (rez!=-1) & // file exists !(S_ISDIR(stats.st_mode))); // file is not directory @@ -68,36 +68,36 @@ Maybe file_close(File* file){ safethrow(ERR_IO,;); Maybe file_writeChar(File* file, char byte){ - int rezult=fputc(byte, file); + i32 rezult=fputc(byte, file); ioWriteCheck(); return MaybeNull; } -Maybe file_writeBuffer(File* file, char* buffer, uint64 length){ - int rezult=0; - for(uint64 i=0; i -Maybe file_writeBuffer(File* file, char* buffer, uint64 length); +Maybe file_writeBuffer(File* file, char* buffer, u64 length); /// @brief writes all cstring array content to file /// @param cptr zero-terminated cstring @@ -63,12 +63,12 @@ Maybe file_readChar(File* file); /// @brief reads byte array of specofied length /// @param buffer buffer that will be filled with file bytes /// @param length buffer length -/// @return Maybe total number of successfully read bytes (<=length) -Maybe file_readBuffer(File* file, char* buffer, uint64 length); +/// @return Maybe total number of successfully read bytes (<=length) +Maybe file_readBuffer(File* file, char* buffer, u64 length); /// @brief reads all bytes from file /// @param allBytes ptr to the file's content will be pushed there -/// @return Maybe total number of successfully read bytes +/// @return Maybe total number of successfully read bytes Maybe file_readAll(File* file, char** allBytes); #if __cplusplus diff --git a/src/Filesystem/path.c b/src/Filesystem/path.c index 5cd2d6e..b3d046e 100644 --- a/src/Filesystem/path.c +++ b/src/Filesystem/path.c @@ -1,16 +1,16 @@ #include "filesystem.h" -char* __path_concat(uint16 n, ...){ +char* __path_concat(u16 n, ...){ char** parts=(char**)malloc(n*sizeof(char*)); - uint32* lengths=malloc(n*sizeof(uint32)); - uint32 totalLength=0; + u32* lengths=malloc(n*sizeof(u32)); + u32 totalLength=0; // reading args from va_list va_list vl; va_start(vl, n); - for(uint16 i=0; i between them /// @return new cstr #define path_concat(PATH_PARTS...) __path_concat(count_args(PATH_PARTS), PATH_PARTS) diff --git a/src/HashFunctions/hash.c b/src/HashFunctions/hash.c index 9035ef0..3f2761b 100644 --- a/src/HashFunctions/hash.c +++ b/src/HashFunctions/hash.c @@ -1,14 +1,14 @@ #include "hash.h" -uint32 hash_sdbm32(uint32 oldhash, void* buf, uint32 len){ - uint8* ubuf=(uint8*)buf; - register uint32 hash=oldhash; +u32 hash_sdbm32(u32 oldhash, void* buf, u32 len){ + u8* ubuf=(u8*)buf; + register u32 hash=oldhash; for (; len ; len--, ubuf++) hash=(hash<<6)+(hash<<16)-hash+*ubuf; return hash; } -static const uint32 crc_32_tab[]={ +static const u32 crc_32_tab[]={ 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, @@ -75,19 +75,19 @@ static const uint32 crc_32_tab[]={ 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d }; -uint32 hash_crc32(uint32 oldhash, void* buf, uint32 len){ - uint8* ubuf=(uint8*)buf; - register uint32 crc=oldhash; +u32 hash_crc32(u32 oldhash, void* buf, u32 len){ + u8* ubuf=(u8*)buf; + register u32 crc=oldhash; for (; len; --len, ++ubuf) crc=crc_32_tab[(crc^(*ubuf)) & 0xff] ^ (crc>>8); return ~crc; } -// bool hashf_crc32c(char *name, uint32 *crc, long *charcnt) { +// bool hashf_crc32c(char *name, u32 *crc, long *charcnt) { // register FILE *fin; -// register uint32 oldcrc32; -// register int c; +// register u32 oldcrc32; +// register i32 c; // oldcrc32 = 0xFFFFFFFF; *charcnt = 0; // if ((fin=fopen(name, "r"))==NULL) { diff --git a/src/HashFunctions/hash.h b/src/HashFunctions/hash.h index 753ed11..c4b71ef 100644 --- a/src/HashFunctions/hash.h +++ b/src/HashFunctions/hash.h @@ -9,8 +9,8 @@ extern "C" { #define hashb(FUNC, BUF, LEN) FUNC(0xFFFFFFFF, BUF, LEN) #define hashs(FUNC, STR) FUNC(0xFFFFFFFF, STR, cptr_length(STR)) -uint32 hash_sdbm32(uint32 oldhash, void* buf, uint32 len); -uint32 hash_crc32(uint32 oldhash, void* buf, uint32 len); +u32 hash_sdbm32(u32 oldhash, void* buf, u32 len); +u32 hash_crc32(u32 oldhash, void* buf, u32 len); #if __cplusplus } diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index edab896..82e92ca 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -3,7 +3,7 @@ ktid_define(Hashtable); // amount of rows -static const uint16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521}; +static const u16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521}; #define HT_HEIN_MIN 0 #define HT_HEIN_MAX 6 @@ -14,14 +14,14 @@ Hashtable* Hashtable_create(){ Hashtable* ht=malloc(sizeof(Hashtable)); ht->hein=HT_HEIN_MIN; ht->rows=malloc(HT_HEIGHTS[HT_HEIN_MIN]*sizeof(Autoarr(KVPair)*)); - for(uint16 i=0;irows[i]=Autoarr_create(KVPair,ARR_BC,ARR_BL); return ht; } void __Hashtable_free(void* _ht){ Hashtable* ht=_ht; - for(uint16 i=0;ihein];i++) + for(u16 i=0;ihein];i++) Autoarr_free(ht->rows[i], true); free(ht->rows); } @@ -30,22 +30,22 @@ void Hashtable_free(Hashtable* ht){ free(ht); } -uint16 Hashtable_height(Hashtable* ht) { return HT_HEIGHTS[ht->hein]; } +u16 Hashtable_height(Hashtable* ht) { return HT_HEIGHTS[ht->hein]; } void Hashtable_expand(Hashtable* ht){ if(ht->hein>=HT_HEIN_MAX) throw(ERR_MAXLENGTH); Autoarr(KVPair)** newrows=malloc(HT_HEIGHTS[++ht->hein]*sizeof(Autoarr(KVPair)*)); - for(uint16 i=0;ihein];i++) + for(u16 i=0;ihein];i++) newrows[i]=Autoarr_create(KVPair,ARR_BC,ARR_BL); - for(uint16 i=0;ihein-1];i++){ + for(u16 i=0;ihein-1];i++){ Autoarr(KVPair)* ar=ht->rows[i]; - uint32 arlen=Autoarr_length(ar); - for(uint32 k=0;khein]; + u16 newrown=hashs(hash_sdbm32, p.key)%HT_HEIGHTS[ht->hein]; Autoarr(KVPair)* newar=newrows[newrown]; Autoarr_add(newar,p); } @@ -58,7 +58,7 @@ void Hashtable_expand(Hashtable* ht){ } Autoarr(KVPair)* getrow(Hashtable* ht, char* key, bool can_expand){ - uint32 hash=hashs(hash_sdbm32, key); + u32 hash=hashs(hash_sdbm32, key); Autoarr(KVPair)* ar=ht->rows[hash%HT_HEIGHTS[ht->hein]]; if(can_expand && Autoarr_length(ar)==Autoarr_max_length(ar)) Hashtable_expand(ht); @@ -74,8 +74,8 @@ void Hashtable_add(Hashtable* ht, char* key, Unitype u){ // returns null or pointer to value in hashtable Unitype* Hashtable_getptr(Hashtable* ht, char* key){ Autoarr(KVPair)* ar=getrow(ht,key,false); - uint32 arlen=Autoarr_length(ar); - for(uint32 i=0;ikey)) return &p->value; } @@ -84,8 +84,8 @@ Unitype* Hashtable_getptr(Hashtable* ht, char* key){ Unitype Hashtable_get(Hashtable* ht, char* key){ Autoarr(KVPair)* ar=getrow(ht,key,false); - uint32 arlen=Autoarr_length(ar); - for(uint32 i=0;irows[h];\ Autoarr_foreach(AR, EL, codeblock);\ }\ diff --git a/src/SearchTree/SearchTree.c b/src/SearchTree/SearchTree.c index 2a0617f..81b7083 100644 --- a/src/SearchTree/SearchTree.c +++ b/src/SearchTree/SearchTree.c @@ -14,13 +14,13 @@ void __STNode_free(void* _node){ STNode* node=_node; if (!node) throw(ERR_NULLPTR); if(node->branches){ - for(uint8 n32 = 0;n32<8;n32++){ + for(u8 n32 = 0;n32<8;n32++){ STNode*** ptrn32=(STNode***)node->branches[n32]; if(ptrn32){ - for(uint8 n4 = 0;n4<8;n4++){ + for(u8 n4 = 0;n4<8;n4++){ STNode** ptrn4=ptrn32[n4]; if (ptrn4){ - for(uint8 rem=0;rem<4;rem++){ + for(u8 rem=0;rem<4;rem++){ STNode* ptrrem=ptrn4[rem]; if(ptrrem) STNode_free(ptrrem); @@ -41,9 +41,9 @@ void STNode_free(STNode* node){ free(node); } -typedef struct {uint8 n32, n4, rem;} indexes3; +typedef struct {u8 n32, n4, rem;} indexes3; -indexes3 splitindex(uint8 i){ +indexes3 splitindex(u8 i){ return (indexes3){ .n32=i/32, .n4=i%32/4, @@ -60,20 +60,20 @@ void ST_pushString(STNode* node_first, string key, Unitype value){ if (!node_first) throw(ERR_NULLPTR); STNode* node_last=node_first; while(key.length--){ - indexes3 i3=splitindex((uint8)*key.ptr); + indexes3 i3=splitindex((u8)*key.ptr); if(!node_last->branches){ node_last->branches=(STNode****)malloc(8*sizeof(STNode***)); - for(uint8 i=0;i<8;i++) + for(u8 i=0;i<8;i++) node_last->branches[i]=(STNode***)NULL; } if(!node_last->branches[i3.n32]){ node_last->branches[i3.n32]=(STNode***)malloc(8*sizeof(STNode**)); - for(uint8 i=0;i<8;i++) + for(u8 i=0;i<8;i++) node_last->branches[i3.n32][i]=(STNode**)NULL; } if(!node_last->branches[i3.n32][i3.n4]){ node_last->branches[i3.n32][i3.n4]=(STNode**)malloc(4*sizeof(STNode*)); - for(uint8 i=0;i<4;i++) + for(u8 i=0;i<4;i++) node_last->branches[i3.n32][i3.n4][i]=(STNode*)NULL; } if(!node_last->branches[i3.n32][i3.n4][i3.rem]) @@ -93,7 +93,7 @@ Unitype ST_pullString(STNode* node_first, string key){ if (!node_first) throw(ERR_NULLPTR); STNode* node_last=node_first; while (key.length--){ - indexes3 i3=splitindex((uint8)*key.ptr); + indexes3 i3=splitindex((u8)*key.ptr); if(!node_last->branches) return UniNull; STNode*** ptrn32=(STNode***)node_last->branches[i3.n32]; if(!ptrn32) return UniNull; diff --git a/src/String/StringBuilder.c b/src/String/StringBuilder.c index e3c3244..3ee6aa8 100644 --- a/src/String/StringBuilder.c +++ b/src/String/StringBuilder.c @@ -11,16 +11,16 @@ ktid_define(StringBuilder); void complete_buf(StringBuilder* b){ if(!b->compl_bufs) b->compl_bufs=Autoarr_create(string,BL_C,BL_L); - uint32 len=Autoarr_length(b->curr_buf); + u32 len=Autoarr_length(b->curr_buf); if(!len) return; string str={.length=len, .ptr=malloc(len)}; - uint32 i=0; + u32 i=0; Autoarr_foreach(b->curr_buf, c, ({ str.ptr[i++]=c; })); Autoarr_add(b->compl_bufs,str); Autoarr_free(b->curr_buf, true); - b->curr_buf=Autoarr_create(int8,BL_C,BL_L); + b->curr_buf=Autoarr_create(i8,BL_C,BL_L); } void try_complete_buf(StringBuilder* b){ @@ -32,7 +32,7 @@ void try_complete_buf(StringBuilder* b){ StringBuilder* StringBuilder_create(){ StringBuilder* b=malloc(sizeof(StringBuilder)); b->compl_bufs=NULL; - b->curr_buf=Autoarr_create(int8,BL_C,BL_L); + b->curr_buf=Autoarr_create(i8,BL_C,BL_L); return b; } @@ -48,15 +48,15 @@ void StringBuilder_free(StringBuilder* b){ string StringBuilder_build(StringBuilder* b){ complete_buf(b); - uint32 len=0; + u32 len=0; Autoarr_foreach(b->compl_bufs, cs, ({ len+=cs.length; })); string str= { .length=len, .ptr=malloc(len+1) }; str.ptr[len]='\0'; - uint32 i=0; + u32 i=0; Autoarr_foreach(b->compl_bufs, cs, ({ - for(uint32 n=0;ncurr_buf,s.ptr[i]); } -void StringBuilder_append_int64(StringBuilder* b, int64 a){ +void StringBuilder_append_i64(StringBuilder* b, i64 a){ try_complete_buf(b); - uint8 i=0; + u8 i=0; if(a==0){ Autoarr_add(b->curr_buf,'0'); return; @@ -121,9 +121,9 @@ void StringBuilder_append_int64(StringBuilder* b, int64 a){ free(rev.ptr); } -void StringBuilder_append_uint64(StringBuilder* b, uint64 a){ +void StringBuilder_append_u64(StringBuilder* b, u64 a){ try_complete_buf(b); - uint8 i=0; + u8 i=0; if(a==0){ Autoarr_add(b->curr_buf,'0'); return; @@ -138,7 +138,7 @@ void StringBuilder_append_uint64(StringBuilder* b, uint64 a){ free(rev.ptr); } -void StringBuilder_append_float64(StringBuilder* b, double a){ +void StringBuilder_append_f64(StringBuilder* b, f64 a){ try_complete_buf(b); char buf[32]; IFMSC( diff --git a/src/String/StringBuilder.h b/src/String/StringBuilder.h index 8072cab..227df3f 100644 --- a/src/String/StringBuilder.h +++ b/src/String/StringBuilder.h @@ -11,7 +11,7 @@ Autoarr_declare(string) typedef struct StringBuilder{ Autoarr(string)* compl_bufs; - Autoarr(int8)* curr_buf; + Autoarr(i8)* curr_buf; } StringBuilder; ktid_declare(StringBuilder); @@ -28,9 +28,9 @@ void StringBuilder_rmchar(StringBuilder* b); void StringBuilder_append_char(StringBuilder* b, char c); void StringBuilder_append_cptr(StringBuilder* b, char* s); void StringBuilder_append_string(StringBuilder* b, string s); -void StringBuilder_append_int64(StringBuilder* b, int64 a); -void StringBuilder_append_uint64(StringBuilder* b, uint64 a); -void StringBuilder_append_float64(StringBuilder* b, double a); +void StringBuilder_append_i64(StringBuilder* b, i64 a); +void StringBuilder_append_u64(StringBuilder* b, u64 a); +void StringBuilder_append_f64(StringBuilder* b, f64 a); #if __cplusplus } diff --git a/src/String/string.c b/src/String/string.c index 9864fb1..5d2246a 100644 --- a/src/String/string.c +++ b/src/String/string.c @@ -19,7 +19,7 @@ string string_copy(string src){ string nstr; nstr.length=src.length; nstr.ptr=malloc(nstr.length); - for(uint32 i=0;i=0; si--){ + i32 fi_last=cptr_length(fragment)-1; + for(i32 si=cptr_length(ptr)-1, fi=fi_last; si>=0; si--){ sc=ptr[si]; if(sc==fragment[fi]){ if(fi==0) @@ -85,9 +85,9 @@ uint32 cptr_lastIndexOf(char* ptr, char* fragment){ } return -1; } -uint32 cptr_lastIndexOfChar(char* ptr, char fragment){ +u32 cptr_lastIndexOfChar(char* ptr, char fragment){ char sc=*ptr; - for(int si=cptr_length(ptr)-1; si>=0; si--){ + for(i32 si=cptr_length(ptr)-1; si>=0; si--){ sc=ptr[si]; if(sc==fragment){ return si; @@ -96,24 +96,24 @@ uint32 cptr_lastIndexOfChar(char* ptr, char fragment){ return -1; } -void memcopy(void* from, void* to, uint32 size){ +void memcopy(void* from, void* to, u32 size){ if(from==NULL || to==NULL) throw(ERR_NULLPTR); - for(uint32 i=0; i in /// @return index of first inclusion or -1 if not found -uint32 cptr_indexOf(char* ptr, char* fragment); +u32 cptr_indexOf(char* ptr, char* fragment); /// @brief search for in /// @return index of first inclusion or -1 if not found -uint32 cptr_indexOfChar(char* ptr, char fragment); +u32 cptr_indexOfChar(char* ptr, char fragment); /// @brief search for in /// @return index of last inclusion or -1 if not found -uint32 cptr_lastIndexOf(char* ptr, char* fragment); +u32 cptr_lastIndexOf(char* ptr, char* fragment); /// @brief search for in /// @return index of last inclusion or -1 if not found -uint32 cptr_lastIndexOfChar(char* ptr, char fragment); +u32 cptr_lastIndexOfChar(char* ptr, char fragment); static inline bool cptr_contains(char* ptr, char* fragment){ // if(cptr_indexOf(ptr, fragment)==-1) @@ -42,9 +42,9 @@ static inline bool cptr_contains(char* ptr, char* fragment){ return cptr_indexOf(ptr, fragment) +1; } -void memcopy(void* from, void* to, uint32 size); +void memcopy(void* from, void* to, u32 size); -char* __cptr_concat(uint16 n, ...); +char* __cptr_concat(u16 n, ...); #define cptr_concat(STR...) __cptr_concat(count_args(STR), STR) #if __cplusplus diff --git a/src/base/endian.c b/src/base/endian.c index b6e25f6..282a53b 100644 --- a/src/base/endian.c +++ b/src/base/endian.c @@ -2,7 +2,7 @@ static const union { - uint16 number; + u16 number; Endian bytes[2]; } _endian_union={ .number=0x0102 }; diff --git a/src/base/errors.c b/src/base/errors.c index dacf45b..a93aff6 100644 --- a/src/base/errors.c +++ b/src/base/errors.c @@ -23,7 +23,7 @@ char* errname(ErrorId err){ #define ERRMSG_MAXLENGTH 1024 -char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname){ +char* __genErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname){ size_t bufsize=ERRMSG_MAXLENGTH; char* rezult=malloc(bufsize); IFMSC( @@ -33,7 +33,7 @@ char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* return rezult; } -char* __extendErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname){ +char* __extendErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname){ size_t bufsize=cptr_length(errmsg)+ERRMSG_MAXLENGTH; char* rezult=malloc(bufsize); IFMSC( diff --git a/src/base/errors.h b/src/base/errors.h index 90d9751..bb1dce6 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -17,8 +17,8 @@ PACK_ENUM(ErrorId, char* errname(ErrorId err); -char* __genErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname); -char* __extendErrMsg(const char* errmsg, const char* srcfile, int line, const char* funcname); +char* __genErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname); +char* __extendErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname); typedef struct Maybe{ Unitype value; diff --git a/src/base/optime.h b/src/base/optime.h index be45fe4..8f5bd2d 100644 --- a/src/base/optime.h +++ b/src/base/optime.h @@ -4,7 +4,7 @@ #define __optime_print(opname, t)\ char tnames[3][3]={"s\0","ms","us"};\ - int tni=0;\ + i32 tni=0;\ if(t>1000000){\ t/=1000000;\ tni=0;\ @@ -17,25 +17,25 @@ #ifdef CLOCK_REALTIME /// executes codeblock and prints execution time -/// uint64 op_i is counter of the internal loop +/// u64 op_i is counter of the internal loop /// uses non-standard high-precision clock #define optime(opname,repeats,codeblock) ({\ struct timespec start, stop;\ clock_gettime(CLOCK_REALTIME, &start);\ - for(uint64 op_i=0;op_i<(uint64)repeats;op_i++)\ + for(u64 op_i=0;op_i<(u64)repeats;op_i++)\ (codeblock);\ clock_gettime(CLOCK_REALTIME, &stop);\ - double t=(double)(stop.tv_sec-start.tv_sec)*1000000+(double)(stop.tv_nsec-start.tv_nsec)/1000;\ + f64 t=(f64)(stop.tv_sec-start.tv_sec)*1000000+(f64)(stop.tv_nsec-start.tv_nsec)/1000;\ __optime_print(opname,t)\ }) #else /// uses standard low precision clock #define optime(opname,repeats,codeblock) ({\ clock_t start=clock();\ - for(uint64 op_i=0;op_i<(uint64)repeats;op_i++)\ + for(u64 op_i=0;op_i<(u64)repeats;op_i++)\ (codeblock);\ clock_t stop=clock();\ - double t=(double)(stop-start)/CLOCKS_PER_SEC*1000000;\ + f64 t=(f64)(stop-start)/CLOCKS_PER_SEC*1000000;\ __optime_print(opname,t)\ }) #endif diff --git a/src/base/std.h b/src/base/std.h index 9f6b7d9..d102a70 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -12,22 +12,23 @@ extern "C" { #include #include -typedef int8_t int8; -typedef uint8_t uint8; -typedef int16_t int16; -typedef uint16_t uint16; -typedef int32_t int32; -typedef uint32_t uint32; -typedef int64_t int64; -typedef uint64_t uint64; -typedef float float32; -typedef double float64; +typedef int8_t i8; +typedef uint8_t u8; +typedef int16_t i16; +typedef uint16_t u16; +typedef int32_t i32; +typedef uint32_t u32; +typedef int64_t i64; +typedef uint64_t u64; +typedef float f32; +typedef double f64; + // Usually bool from stdbool.h is defined as macro, // so in other macros like ktid_##TYPE it will be replaced by _Bool. // ktid__Bool will be created instead of ktid_bool // In C++ bool is a keyword, so there is no need to redefine it. #if !__cplusplus -typedef uint8 bool; +typedef u8 bool; #define true 1 #define false 0 #endif diff --git a/src/base/type_system/base_toString.c b/src/base/type_system/base_toString.c index 68c5324..42deca9 100644 --- a/src/base/type_system/base_toString.c +++ b/src/base/type_system/base_toString.c @@ -2,7 +2,7 @@ #include "../base.h" #include "../../kprint/kprint_format.h" -char* __toString_char(void* c, uint32 fmt) { +char* __toString_char(void* c, u32 fmt) { //*c=char if(kp_fmt_dataFormat(fmt)==kp_c){ char* cc=malloc(2); @@ -17,9 +17,9 @@ char* __toString_char(void* c, uint32 fmt) { else throw(ERR_FORMAT); } -char* __toString_bool(void* c, uint32 fmt) { +char* __toString_bool(void* c, u32 fmt) { static const char _strbool[4][6]={ "false", "true\0", "False", "True\0" }; - uint8 strind=*(bool*)c==1 + kp_fmt_isUpper(fmt)*2; + u8 strind=*(bool*)c==1 + kp_fmt_isUpper(fmt)*2; char* rez=malloc(6); rez[0]=_strbool[strind][0]; rez[1]=_strbool[strind][1]; @@ -30,10 +30,10 @@ char* __toString_bool(void* c, uint32 fmt) { return rez; } -char* toString_int(int64 n){ - int64 d=n<0 ? -1*n : n; +char* toString_i64(i64 n){ + i64 d=n<0 ? -1*n : n; char str[32]; - uint8 i=sizeof(str); + u8 i=sizeof(str); str[--i]=0; if(d==0) str[--i]='0'; @@ -46,9 +46,9 @@ char* toString_int(int64 n){ return cptr_copy((char*)str+i); } -char* toString_uint(uint64 n, bool withPostfix, bool uppercase){ +char* toString_u64(u64 n, bool withPostfix, bool uppercase){ char str[32]; - uint8 i=sizeof(str); + u8 i=sizeof(str); str[--i]=0; if(withPostfix) str[--i]= uppercase ? 'U' : 'u'; @@ -67,7 +67,7 @@ char* toString_uint(uint64 n, bool withPostfix, bool uppercase){ throw("too big precision");\ if(precision==0)\ precision=toString_float_default_precision;\ - int cn=IFMSC(\ + i32 cn=IFMSC(\ sprintf_s(str, bufsize, "%.*f", precision, n),\ sprintf(str, "%.*f", precision, n)\ );\ @@ -80,37 +80,37 @@ char* toString_uint(uint64 n, bool withPostfix, bool uppercase){ return cptr_copy(str);\ } -char* toString_float32(float32 n, uint8 precision, bool withPostfix, bool uppercase) - _toString_float_impl(48, toString_float32_max_precision) +char* toString_f32(f32 n, u8 precision, bool withPostfix, bool uppercase) + _toString_float_impl(48, toString_f32_max_precision) -char* toString_float64(float64 n, uint8 precision, bool withPostfix, bool uppercase) - _toString_float_impl(512, toString_float64_max_precision) +char* toString_f64(f64 n, u8 precision, bool withPostfix, bool uppercase) + _toString_float_impl(512, toString_f64_max_precision) #define byte_to_bits(byte) {\ - str[cn++]='0' + (uint8)((byte>>7)&1); /* 8th bit */\ - str[cn++]='0' + (uint8)((byte>>6)&1); /* 7th bit */\ - str[cn++]='0' + (uint8)((byte>>5)&1); /* 6th bit */\ - str[cn++]='0' + (uint8)((byte>>4)&1); /* 5th bit */\ - str[cn++]='0' + (uint8)((byte>>3)&1); /* 4th bit */\ - str[cn++]='0' + (uint8)((byte>>2)&1); /* 3th bit */\ - str[cn++]='0' + (uint8)((byte>>1)&1); /* 2th bit */\ - str[cn++]='0' + (uint8)((byte>>0)&1); /* 1th bit */\ + str[cn++]='0' + (u8)((byte>>7)&1); /* 8th bit */\ + str[cn++]='0' + (u8)((byte>>6)&1); /* 7th bit */\ + str[cn++]='0' + (u8)((byte>>5)&1); /* 6th bit */\ + str[cn++]='0' + (u8)((byte>>4)&1); /* 5th bit */\ + str[cn++]='0' + (u8)((byte>>3)&1); /* 4th bit */\ + str[cn++]='0' + (u8)((byte>>2)&1); /* 3th bit */\ + str[cn++]='0' + (u8)((byte>>1)&1); /* 2th bit */\ + str[cn++]='0' + (u8)((byte>>0)&1); /* 1th bit */\ } -char* toString_bin(void* _bytes, uint32 size, bool inverse, bool withPrefix){ +char* toString_bin(void* _bytes, u32 size, bool inverse, bool withPrefix){ char* bytes=_bytes; char* str=malloc(size*8 + (withPrefix?2:0) +1); - uint32 cn=0; // char number + u32 cn=0; // char number if(withPrefix){ str[cn++]='0'; str[cn++]='b'; } if(inverse){ // byte number - for(int32 bn=size-1; bn>=0; bn--) + for(i32 bn=size-1; bn>=0; bn--) byte_to_bits(bytes[bn]) } else { - for(int32 bn=0; bn=0; bn--){ + for(i32 bn=size-1; bn>=0; bn--){ unsigned char byte=bytes[bn]; str[cn++]=_4bitsHex(byte/16, uppercase); str[cn++]=_4bitsHex(byte%16, uppercase); @@ -152,7 +152,7 @@ char* toString_hex(void* _bytes, uint32 size, bool inverse, bool withPrefix, boo } // right to left else { - for(int32 bn=0; bntypeId); char* valuestr=type.toString(_u, fmt); @@ -30,11 +30,11 @@ char* sprintuni(Unitype v){ ktDescriptor type=ktDescriptor_get(v.typeId); if(v.typeId==ktid_Null) sprintf_s(buf, BUFSIZE, "{Null}"); - else if(v.typeId==ktid_name(float64)) + else if(v.typeId==ktid_name(f64)) sprintf_s(buf, BUFSIZE, "{%s : %lf}", type.name,v.Float64); - else if(v.typeId==ktid_name(bool) || v.typeId==ktid_name(uint64)) + else if(v.typeId==ktid_name(bool) || v.typeId==ktid_name(u64)) sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%llu", "%lu") "}", type.name,v.UInt64); - else if(v.typeId==ktid_name(int64)) + else if(v.typeId==ktid_name(i64)) sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%lld", "%ld") "}", type.name,v.Int64); else if(v.typeId==ktid_ptrName(char)){ size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2; diff --git a/src/base/type_system/unitype.h b/src/base/type_system/unitype.h index e86eedc..d73859d 100644 --- a/src/base/type_system/unitype.h +++ b/src/base/type_system/unitype.h @@ -8,9 +8,9 @@ extern "C" { typedef struct Unitype{ union { - int64 Int64; - uint64 UInt64; - double Float64; + i64 Int64; + u64 UInt64; + f64 Float64; bool Bool; void* VoidPtr; char Bytes[8]; @@ -24,9 +24,9 @@ ktid_declare(Unitype); #define __UniDef(FIELD, TYPE, VAL) (Unitype){\ .FIELD=VAL, .typeId=ktid_name(TYPE), .allocatedInHeap=false} -#define UniInt64(VAL) __UniDef(Int64, int64, VAL) -#define UniUInt64(VAL) __UniDef(UInt64, uint64, VAL) -#define UniFloat64(VAL) __UniDef(Float64, float64, VAL) +#define UniInt64(VAL) __UniDef(Int64, i64, VAL) +#define UniUInt64(VAL) __UniDef(UInt64, u64, VAL) +#define UniFloat64(VAL) __UniDef(Float64, f64, VAL) #define UniBool(VAL) __UniDef(Bool, bool, VAL) #define UniStackPtr(TYPE, VAL) (Unitype){\ diff --git a/src/kprint/README.md b/src/kprint/README.md index cb78373..98464ef 100644 --- a/src/kprint/README.md +++ b/src/kprint/README.md @@ -2,16 +2,16 @@ It is just my cross-plaform variant of printf. Unlike in standard printf, `%l...` and `%ll...` placeholders dont depend on size of `long int` and `long long int`. And you can change terminal colors by unix codes (`\e[92m`) even on Windows. -| type | placeholder | -|-------------------------|-------------------------| -| int8 / int16 / int32 | %i / %d | -| int64 | %li / %ld / %lld / %lli | -| uint8 / uint16 / uint32 | %u | -| uint64 | %lu / %llu | -| float32 / float64 | %f | -| char | %c | -| char[] | %s | -| void\* | %p / %x | +| type | placeholder | +|----------------|-------------------------| +| i8 / i16 / i32 | %i / %d | +| i64 | %li / %ld / %lld / %lli | +| u8 / u16 / u32 | %u | +| u64 | %lu / %llu | +| f32 / f64 | %f | +| char | %c | +| char[] | %s | +| void\* | %p / %x |
diff --git a/src/kprint/kprint.c b/src/kprint/kprint.c index 2fda00f..809de58 100644 --- a/src/kprint/kprint.c +++ b/src/kprint/kprint.c @@ -9,11 +9,11 @@ ktid __typeFromFormat(kp_fmt f){ case kp_i: case kp_h: case kp_b: - return ktid_name(int64); + return ktid_name(i64); case kp_u: - return ktid_name(uint64); + return ktid_name(u64); case kp_f: - return ktid_name(float64); + return ktid_name(f64); case kp_c: return ktid_char; case kp_s: @@ -34,17 +34,17 @@ Maybe __next_toString(kp_fmt f, __kp_value_union* object){ return SUCCESS(UniHeapPtr(char, typeDesc.toString(object, f))); } -Maybe check_argsN(uint8 n){ +Maybe check_argsN(u8 n){ if(n%2 != 0) safethrow("kprint recieved non-even number of arguments",;); if(n > 32) safethrow("kprint recieved >32 number of arguments",;); return MaybeNull; } -Maybe __ksprint(uint8 n, kp_fmt* formats, __kp_value_union* objects){ +Maybe __ksprint(u8 n, kp_fmt* formats, __kp_value_union* objects){ try(check_argsN(n), _,;); n/=2; StringBuilder* strb=StringBuilder_create(); - for(uint8 i=0; i>24; + u8 fg=(f&0x0f000000)>>24; if(fg<8) fg+=30; else fg+=90-8; printf("\e[%um", fg); } if(kp_fmt_bgColorSet(f)){ - uint8 bg=(f&0x00f00000)>>20; + u8 bg=(f&0x00f00000)>>20; if(bg<8) bg+=40; else bg+=100-8; printf("\e[%um", bg); @@ -159,13 +159,13 @@ void kprint_setColor(kp_fmt f){ } #endif -/* Maybe ksprint_ar(uint32 count, kp_fmt format, ktid typeId, void* array){ +/* Maybe ksprint_ar(u32 count, kp_fmt format, ktid typeId, void* array){ ktDescriptor typeDesc=ktDescriptor_get(format.typeId); if(!typeDesc.toString) safethrow("type descriptor doesnt have toString() func",;); StringBuilder* strb=StringBuilder_create(); StringBuilder_append_char(strb, '['); - for (uint16 e=1; e @@ -80,7 +80,7 @@ Maybe __ksprint(uint8 n, kp_fmt* formats, __kp_value_union* objects); ) /*-Wint-conversion warning was produced during value to __kp_value_union conversion*/ -Maybe __kfprint(FILE* fd, uint8 n, kp_fmt* formats, __kp_value_union* objects); +Maybe __kfprint(FILE* fd, u8 n, kp_fmt* formats, __kp_value_union* objects); /// @param FD FILE* /// @param ARGS kp_fmt, value, kp_fmt, value... @@ -89,7 +89,7 @@ Maybe __kfprint(FILE* fd, uint8 n, kp_fmt* formats, __kp_value_union* objects); __kfprint(FD, count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes))\ ) -void __kprint(uint8 n, kp_fmt* formats, __kp_value_union* objects); +void __kprint(u8 n, kp_fmt* formats, __kp_value_union* objects); ///can use non-catchable throw !!! ///@param ARGS kp_fmt, value, kp_fmt, value... diff --git a/src/kprint/kprint_format.h b/src/kprint/kprint_format.h index 9efceae..2c8113d 100644 --- a/src/kprint/kprint_format.h +++ b/src/kprint/kprint_format.h @@ -8,7 +8,7 @@ extern "C" { #include "../base/type_system/ktid.h" /// kprint_format -typedef uint32 kp_fmt; +typedef u32 kp_fmt; PACK_ENUM(kp_dataFmt, // 00000000 00000000 00000000 00000000 diff --git a/src/kprint/kprint_format.md b/src/kprint/kprint_format.md index f0b2f7d..e4d811b 100644 --- a/src/kprint/kprint_format.md +++ b/src/kprint/kprint_format.md @@ -54,15 +54,17 @@ bgColorSet─┘││ │ bgColor └data format ## Data format -| format | possible flags | data types | hex value | bin value | -|-----------|----------------|------------|-----------|-----------| -| kp_i | | int8... int64 | 0x00000000 | 00000000 00000000... | -| kp_u | Postfix, Upper | uint8... uint64 | 0x00010000 | 00000000 00000001... | -| kp_h | Prefix, Upper | any | 0x00020000 | 00000000 00000010... | -| kp_b | Prefix | any | 0x00030000 | 00000000 00000011... | -| kp_f | Postfix, Upper | float32, float64 | 0x00040000 | 00000000 00000100... | -| kp_c | | char | 0x00050000 | 00000000 00000101... | -| kp_sing | | char* | 0x00060000 | 00000000 00000110... | +| format | possible flags | data types | hex value | bin value | +|-----------|----------------|------------|------------|-----------| +| kp_i | | i8... i64 | 0x00000000 | 00000000 00000000... | +| kp_u | Postfix, Upper | u8... u64 | 0x00010000 | 00000000 00000001... | +| kp_h | Prefix, Upper | any | 0x00020000 | 00000000 00000010... | +| kp_b | Prefix | any | 0x00030000 | 00000000 00000011... | +| kp_f | Postfix, Upper | f32, f64 | 0x00040000 | 00000000 00000100... | +| kp_c | | char | 0x00050000 | 00000000 00000101... | +| kp_string | | char* | 0x00060000 | 00000000 00000110... | + +P.S. `any` means you must add `kpid` to `kp_fmt` if data type is not base type ### *Flags* | flag | hex value | bin value | diff --git a/src/kprint/kprintf.c b/src/kprint/kprintf.c index 261c649..52e8201 100644 --- a/src/kprint/kprintf.c +++ b/src/kprint/kprintf.c @@ -5,7 +5,7 @@ #if defined(_WIN64) || defined(_WIN32) #include -WORD unixColorToWin(uint8 c){ +WORD unixColorToWin(u8 c){ switch(c){ //foreground case 30: return 0; @@ -49,7 +49,7 @@ WORD unixColorToWin(uint8 c){ void kprintf(const char* format, ...){ va_list vl; va_start(vl, format); - uint32 i=0; + u32 i=0; for(char c=format[i++]; c!=0; c=format[i++]){ if(c=='%'){ char* argstr=NULL; @@ -58,18 +58,18 @@ void kprintf(const char* format, ...){ format_escape_seq: switch (c) { case 'u': - argstr=toString_uint( - l ? va_arg(vl, uint64) : va_arg(vl, uint32) + argstr=toString_u64( + l ? va_arg(vl, u64) : va_arg(vl, u32) ,0,0); break; case 'i': case 'd': - argstr=toString_int( - l ? va_arg(vl, int64) : va_arg(vl, int32) + argstr=toString_i64( + l ? va_arg(vl, i64) : va_arg(vl, i32) ); break; case 'f': - // float32 is promoted to float64 when passed through '...' - argstr=toString_float64(va_arg(vl, float64), toString_float_default_precision,0,0); + // f32 is promoted to f64 when passed through '...' + argstr=toString_f64(va_arg(vl, f64), toString_float_default_precision,0,0); break; case 'l': l=true; @@ -78,7 +78,7 @@ void kprintf(const char* format, ...){ break; case 'p': case 'x': ; - uint64 px=va_arg(vl, uint64); + u64 px=va_arg(vl, u64); argstr=toString_hex(&px,getEndian()==LittleEndian,sizeof(px),1,0); break; case 's': ; @@ -108,8 +108,8 @@ void kprintf(const char* format, ...){ IFWIN( ({ if((c=format[i++])=='['){ - uint8 colorUnix=0; - for(int8 n=0; n<6 && c!=0; n++){ + u8 colorUnix=0; + for(i8 n=0; n<6 && c!=0; n++){ c=format[i++]; switch (c){ case '0': case '1': case '2': case '3': case '4': diff --git a/src/random/krandom.h b/src/random/krandom.h index d78b7d6..2072260 100644 --- a/src/random/krandom.h +++ b/src/random/krandom.h @@ -12,8 +12,8 @@ extern "C" { /* You can choose any algorithm that has required functions: - some_alg32_statePtr some_alg32_init(uint32 seed); - uint32 some_alg32_next(some_alg32_statePtr); + some_alg32_statePtr some_alg32_init(u32 seed); + u32 some_alg32_next(some_alg32_statePtr); void some_alg32_free(some_alg32_statePtr); #define KRAND_ALG32_init some_alg32_init @@ -51,19 +51,19 @@ typedef void* krand_statePtr; #define __krand_next_definition(VALUE_SIZE) { return from+KRAND_ALG##VALUE_SIZE##_next(state)%(to-from); } // ready-to-use functions -static inline int8 krand_next8 (krand_statePtr state, int8 from, int8 to) __krand_next_definition(32) -static inline int16 krand_next16(krand_statePtr state, int16 from, int16 to) __krand_next_definition(32) -static inline int32 krand_next32(krand_statePtr state, int32 from, int32 to) __krand_next_definition(32) -static inline int64 krand_next64(krand_statePtr state, int64 from, int64 to) __krand_next_definition(64) +static inline i8 krand_next8 (krand_statePtr state, i8 from, i8 to) __krand_next_definition(32) +static inline i16 krand_next16(krand_statePtr state, i16 from, i16 to) __krand_next_definition(32) +static inline i32 krand_next32(krand_statePtr state, i32 from, i32 to) __krand_next_definition(32) +static inline i64 krand_next64(krand_statePtr state, i64 from, i64 to) __krand_next_definition(64) // divides random number by 2^64 to return a value between 0 and 1 -static inline float32 krand_nextFloat32(krand_statePtr state) {return (uint32)KRAND_ALG32_next(state)/0xffffffff; } -static inline float64 krand_nextFloat64(krand_statePtr state) {return KRAND_ALG64_next(state)/0xffffffff; } +static inline f32 krand_nextFloat32(krand_statePtr state) {return (u32)KRAND_ALG32_next(state)/0xffffffff; } +static inline f64 krand_nextFloat64(krand_statePtr state) {return KRAND_ALG64_next(state)/0xffffffff; } ///@param chance (0-1.0) is probability of success static inline bool fate(krand_statePtr state,float chance){ - int limit=1/chance + 0.01f; + i32 limit=1/chance + 0.01f; return KRAND_ALG32_next(state)%limit == 0; } diff --git a/src/random/splitmix64/splitmix64.c b/src/random/splitmix64/splitmix64.c index 591167c..c267b9e 100644 --- a/src/random/splitmix64/splitmix64.c +++ b/src/random/splitmix64/splitmix64.c @@ -13,18 +13,18 @@ generator. // The state can be seeded with any (upto) 64 bit integer value. -void* splitmix64_init(uint64 seed){ +void* splitmix64_init(u64 seed){ splitmix64_state* state=malloc(sizeof(splitmix64_state)); *state=seed; return state; } -uint64 splitmix64_next(void* _state) { +u64 splitmix64_next(void* _state) { splitmix64_state* state=_state; // increment the state variable *state += 0x9e3779b97f4a7c15; // copy the state to a working variable - uint64 z = *state; + u64 z = *state; // xor the variable with the variable right bit shifted 30 then multiply by a constant z = (z ^ (z>>30)) * 0xbf58476d1ce4e5b9; // xor the variable with the variable right bit shifted 27 then multiply by a constant diff --git a/src/random/splitmix64/splitmix64.h b/src/random/splitmix64/splitmix64.h index 8cf8104..3ffeb1a 100644 --- a/src/random/splitmix64/splitmix64.h +++ b/src/random/splitmix64/splitmix64.h @@ -6,13 +6,13 @@ extern "C" { #include "../../base/base.h" -typedef uint64 splitmix64_state; +typedef u64 splitmix64_state; typedef void* splitmix64_statePtr; -splitmix64_statePtr splitmix64_init(uint64 seed); +splitmix64_statePtr splitmix64_init(u64 seed); static inline splitmix64_statePtr splitmix64_initFromTime(void) { return splitmix64_init(time(NULL)); } -uint64 splitmix64_next(splitmix64_statePtr); +u64 splitmix64_next(splitmix64_statePtr); static inline void splitmix64_free(splitmix64_statePtr state) { free(state); } diff --git a/src/random/xoroshiro/32bitValue/xoroshiro64.h b/src/random/xoroshiro/32bitValue/xoroshiro64.h index 6377bba..680a926 100644 --- a/src/random/xoroshiro/32bitValue/xoroshiro64.h +++ b/src/random/xoroshiro/32bitValue/xoroshiro64.h @@ -8,12 +8,12 @@ extern "C" { #include "../../splitmix64/splitmix64.h" typedef union { - uint64 merged; - uint32 s[2]; + u64 merged; + u32 s[2]; } xoroshiro64_state; typedef void* xoroshiro64_statePtr; -xoroshiro64_statePtr xoroshiro64_init(uint64 seed); +xoroshiro64_statePtr xoroshiro64_init(u64 seed); #define xoroshiro64star_init xoroshiro64_init #define xoroshiro64starstar_init xoroshiro64_init @@ -21,8 +21,8 @@ static inline xoroshiro64_statePtr xoroshiro64_initFromTime(void) { return xoros #define xoroshiro64star_initFromTime xoroshiro64_initFromTime #define xoroshiro64starstar_initFromTime xoroshiro64_initFromTime -uint32 xoroshiro64star_next(xoroshiro64_statePtr); -uint32 xoroshiro64starstar_next(xoroshiro64_statePtr); +u32 xoroshiro64star_next(xoroshiro64_statePtr); +u32 xoroshiro64starstar_next(xoroshiro64_statePtr); static inline void xoroshiro64_free(xoroshiro64_statePtr state) { free(state); diff --git a/src/random/xoroshiro/32bitValue/xoroshiro64star.c b/src/random/xoroshiro/32bitValue/xoroshiro64star.c index 7c2fd3c..ca57d35 100644 --- a/src/random/xoroshiro/32bitValue/xoroshiro64star.c +++ b/src/random/xoroshiro/32bitValue/xoroshiro64star.c @@ -10,8 +10,8 @@ See . */ /* This is xoroshiro64* 1.0, our best and fastest 32-bit small-state -generator for 32-bit floating-point numbers. We suggest to use its -upper bits for floating-point generation, as it is slightly faster than +generator for 32-bit floating-poi32 numbers. We suggest to use its +upper bits for floating-poi32 generation, as it is slightly faster than xoroshiro64**. It passes all tests we are aware of except for linearity tests, as the lowest six bits have low linear complexity, so if low linear complexity is not considered an issue (as it is usually the @@ -23,15 +23,15 @@ right shifts to extract subsets of bits. The state must be seeded so that it is not everywhere zero. */ -static inline uint32 rotl(const uint32 x, int k) { +static inline u32 rotl(const u32 x, i32 k) { return (x << k) | (x >> (32 - k)); } -uint32 xoroshiro64star_next(void* _state) { +u32 xoroshiro64star_next(void* _state) { xoroshiro64_state* state=_state; - const uint32 s0 = state->s[0]; - uint32 s1 = state->s[1]; - const uint32 result = s0 * 0x9E3779BB; + const u32 s0 = state->s[0]; + u32 s1 = state->s[1]; + const u32 result = s0 * 0x9E3779BB; s1 ^= s0; state->s[0] = rotl(s0, 26) ^ s1 ^ (s1 << 9); // a, b @@ -40,7 +40,7 @@ uint32 xoroshiro64star_next(void* _state) { return result; } -void* xoroshiro64_init(uint64 seed){ +void* xoroshiro64_init(u64 seed){ xoroshiro64_state* state=malloc(sizeof(xoroshiro64_state)); splitmix64_state* splitmix=splitmix64_init(seed); state->merged=splitmix64_next(splitmix); diff --git a/src/random/xoroshiro/32bitValue/xoroshiro64starstar.c b/src/random/xoroshiro/32bitValue/xoroshiro64starstar.c index df4432e..40d93fc 100644 --- a/src/random/xoroshiro/32bitValue/xoroshiro64starstar.c +++ b/src/random/xoroshiro/32bitValue/xoroshiro64starstar.c @@ -19,15 +19,15 @@ See . */ The state must be seeded so that it is not everywhere zero. */ -static inline uint32 rotl(const uint32 x, int k) { +static inline u32 rotl(const u32 x, i32 k) { return (x << k) | (x >> (32 - k)); } -uint32 xoroshiro64starstar_next(void* _state) { +u32 xoroshiro64starstar_next(void* _state) { xoroshiro64_state* state=_state; - const uint32 s0 = state->s[0]; - uint32 s1 = state->s[1]; - const uint32 result = rotl(s0 * 0x9E3779BB, 5) * 5; + const u32 s0 = state->s[0]; + u32 s1 = state->s[1]; + const u32 result = rotl(s0 * 0x9E3779BB, 5) * 5; s1 ^= s0; state->s[0] = rotl(s0, 26) ^ s1 ^ (s1 << 9); // a, b diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128.h b/src/random/xoroshiro/64bitValue/xoroshiro128.h index 63ed537..33b7116 100644 --- a/src/random/xoroshiro/64bitValue/xoroshiro128.h +++ b/src/random/xoroshiro/64bitValue/xoroshiro128.h @@ -9,11 +9,11 @@ extern "C" { typedef union { - uint32 s[2]; + u32 s[2]; } xoroshiro128_state; typedef void* xoroshiro128_statePtr; -xoroshiro128_statePtr xoroshiro128_init(uint64 seed); +xoroshiro128_statePtr xoroshiro128_init(u64 seed); #define xoroshiro128plus_init xoroshiro128_init #define xoroshiro128plusplus_init xoroshiro128_init #define xoroshiro128starstar_init xoroshiro128_init @@ -23,9 +23,9 @@ static inline xoroshiro128_statePtr xoroshiro128_initFromTime(void) { return xor #define xoroshiro128plusplus_initFromTime xoroshiro128_initFromTime #define xoroshiro128starstar_initFromTime xoroshiro128_initFromTime -uint64 xoroshiro128plus_next(xoroshiro128_statePtr); -uint64 xoroshiro128plusplus_next(xoroshiro128_statePtr); -uint64 xoroshiro128starstar_next(xoroshiro128_statePtr); +u64 xoroshiro128plus_next(xoroshiro128_statePtr); +u64 xoroshiro128plusplus_next(xoroshiro128_statePtr); +u64 xoroshiro128starstar_next(xoroshiro128_statePtr); static inline void xoroshiro128_free(xoroshiro128_statePtr state) { free(state); diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128plus.c b/src/random/xoroshiro/64bitValue/xoroshiro128plus.c index e01e0b5..05760ec 100644 --- a/src/random/xoroshiro/64bitValue/xoroshiro128plus.c +++ b/src/random/xoroshiro/64bitValue/xoroshiro128plus.c @@ -9,9 +9,9 @@ See . */ #include "xoroshiro128.h" /* This is xoroshiro128+ 1.0, our best and fastest small-state generator - for floating-point numbers, but its state space is large enough only + for floating-poi32 numbers, but its state space is large enough only for mild parallelism. We suggest to use its upper bits for - floating-point generation, as it is slightly faster than + floating-poi32 generation, as it is slightly faster than xoroshiro128++/xoroshiro128**. It passes all tests we are aware of except for the four lower bits, which might fail linearity tests (and just those), so if low linear complexity is not considered an issue (as @@ -32,15 +32,15 @@ See . */ better results in our test than the 2016 version (a=55, b=14, c=36). */ -static inline uint64 rotl(const uint64 x, int k) { +static inline u64 rotl(const u64 x, i32 k) { return (x << k) | (x >> (64 - k)); } -uint64 xoroshiro128plus_next(void* _state){ +u64 xoroshiro128plus_next(void* _state){ xoroshiro128_state* state=_state; - const uint64 s0 = state->s[0]; - uint64 s1 = state->s[1]; - const uint64 result = s0 + s1; + const u64 s0 = state->s[0]; + u64 s1 = state->s[1]; + const u64 result = s0 + s1; s1 ^= s0; state->s[0] = rotl(s0, 24) ^ s1 ^ (s1 << 16); // a, b @@ -49,7 +49,7 @@ uint64 xoroshiro128plus_next(void* _state){ return result; } -void* xoroshiro128_init(uint64 seed){ +void* xoroshiro128_init(u64 seed){ xoroshiro128_state* state=malloc(sizeof(xoroshiro128_state)); splitmix64_state* splitmix=splitmix64_init(seed); state->s[0]=splitmix64_next(splitmix); diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c b/src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c index c01fb30..43e110e 100644 --- a/src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c +++ b/src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c @@ -13,7 +13,7 @@ See . */ tests we are aware of, but its state space is large enough only for mild parallelism. - For generating just floating-point numbers, xoroshiro128+ is even + For generating just floating-poi32 numbers, xoroshiro128+ is even faster (but it has a very mild bias, see notes in the comments). The state must be seeded so that it is not everywhere zero. If you have @@ -21,15 +21,15 @@ See . */ output to fill s. */ -static inline uint64 rotl(const uint64 x, int k) { +static inline u64 rotl(const u64 x, i32 k) { return (x << k) | (x >> (64 - k)); } -uint64 xoroshiro128plusplus_next(void* _state){ +u64 xoroshiro128plusplus_next(void* _state){ xoroshiro128_state* state=_state; - const uint64 s0 = state->s[0]; - uint64 s1 = state->s[1]; - const uint64 result = rotl(s0 + s1, 17) + s0; + const u64 s0 = state->s[0]; + u64 s1 = state->s[1]; + const u64 result = rotl(s0 + s1, 17) + s0; s1 ^= s0; state->s[0] = rotl(s0, 49) ^ s1 ^ (s1 << 21); // a, b diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128starstar.c b/src/random/xoroshiro/64bitValue/xoroshiro128starstar.c index 3e9ac72..5e25851 100644 --- a/src/random/xoroshiro/64bitValue/xoroshiro128starstar.c +++ b/src/random/xoroshiro/64bitValue/xoroshiro128starstar.c @@ -13,7 +13,7 @@ See . */ tests we are aware of, but its state space is large enough only for mild parallelism. - For generating just floating-point numbers, xoroshiro128+ is even + For generating just floating-poi32 numbers, xoroshiro128+ is even faster (but it has a very mild bias, see notes in the comments). The state must be seeded so that it is not everywhere zero. If you have @@ -21,15 +21,15 @@ See . */ output to fill s. */ -static inline uint64 rotl(const uint64 x, int k) { +static inline u64 rotl(const u64 x, i32 k) { return (x << k) | (x >> (64 - k)); } -uint64 xoroshiro128starstar_next(void* _state){ +u64 xoroshiro128starstar_next(void* _state){ xoroshiro128_state* state=_state; - const uint64 s0 = state->s[0]; - uint64 s1 = state->s[1]; - const uint64 result = rotl(s0 * 5, 7) * 9; + const u64 s0 = state->s[0]; + u64 s1 = state->s[1]; + const u64 result = rotl(s0 * 5, 7) * 9; s1 ^= s0; state->s[0] = rotl(s0, 24) ^ s1 ^ (s1 << 16); // a, b diff --git a/src/random/xoshiro/32bitValue/xoshiro128.h b/src/random/xoshiro/32bitValue/xoshiro128.h index 02d52e8..daf2333 100644 --- a/src/random/xoshiro/32bitValue/xoshiro128.h +++ b/src/random/xoshiro/32bitValue/xoshiro128.h @@ -9,12 +9,12 @@ extern "C" { typedef union { - uint64 merged[2]; - uint32 s[4]; + u64 merged[2]; + u32 s[4]; } xoshiro128_state; typedef void* xoshiro128_statePtr; -xoshiro128_statePtr xoshiro128_init(uint64 seed); +xoshiro128_statePtr xoshiro128_init(u64 seed); #define xoshiro128plus_init xoshiro128_init #define xoshiro128plusplus_init xoshiro128_init #define xoshiro128starstar_init xoshiro128_init @@ -24,9 +24,9 @@ static inline xoshiro128_statePtr xoshiro128_initFromTime(void) { return xoshiro #define xoshiro128plusplus_initFromTime xoshiro128_initFromTime #define xoshiro128starstar_initFromTime xoshiro128_initFromTime -uint32 xoshiro128plus_next(xoshiro128_statePtr); -uint32 xoshiro128plusplus_next(xoshiro128_statePtr); -uint32 xoshiro128starstar_next(xoshiro128_statePtr); +u32 xoshiro128plus_next(xoshiro128_statePtr); +u32 xoshiro128plusplus_next(xoshiro128_statePtr); +u32 xoshiro128starstar_next(xoshiro128_statePtr); static inline void xoshiro128_free(xoshiro128_statePtr state) { free(state); diff --git a/src/random/xoshiro/32bitValue/xoshiro128plus.c b/src/random/xoshiro/32bitValue/xoshiro128plus.c index 8cafc8b..f68b3d5 100644 --- a/src/random/xoshiro/32bitValue/xoshiro128plus.c +++ b/src/random/xoshiro/32bitValue/xoshiro128plus.c @@ -9,8 +9,8 @@ See . */ #include "xoshiro128.h" /* This is xoshiro128+ 1.0, our best and fastest 32-bit generator for 32-bit - floating-point numbers. We suggest to use its upper bits for - floating-point generation, as it is slightly faster than xoshiro128**. + floating-poi32 numbers. We suggest to use its upper bits for + floating-poi32 generation, as it is slightly faster than xoshiro128**. It passes all tests we are aware of except for linearity tests, as the lowest four bits have low linear complexity, so if low linear complexity is not considered an issue (as it is usually @@ -22,15 +22,15 @@ See . */ The state must be seeded so that it is not everywhere zero. */ -static inline uint32 rotl(const uint32 x, int k) { +static inline u32 rotl(const u32 x, i32 k) { return (x << k) | (x >> (32 - k)); } -uint32 xoshiro128plus_next(void* _state){ +u32 xoshiro128plus_next(void* _state){ xoshiro128_state* state=_state; - const uint32 result = state->s[0] + state->s[3]; + const u32 result = state->s[0] + state->s[3]; - const uint32 t = state->s[1] << 9; + const u32 t = state->s[1] << 9; state->s[2] ^= state->s[0]; state->s[3] ^= state->s[1]; @@ -44,7 +44,7 @@ uint32 xoshiro128plus_next(void* _state){ return result; } -void* xoshiro128_init(uint64 seed){ +void* xoshiro128_init(u64 seed){ xoshiro128_state* state=malloc(sizeof(xoshiro128_state)); splitmix64_state* splitmix=splitmix64_init(seed); state->merged[0]=splitmix64_next(splitmix); diff --git a/src/random/xoshiro/32bitValue/xoshiro128plusplus.c b/src/random/xoshiro/32bitValue/xoshiro128plusplus.c index e9be19d..8fea492 100644 --- a/src/random/xoshiro/32bitValue/xoshiro128plusplus.c +++ b/src/random/xoshiro/32bitValue/xoshiro128plusplus.c @@ -19,15 +19,15 @@ See . */ The state must be seeded so that it is not everywhere zero. */ -static inline uint32 rotl(const uint32 x, int k) { +static inline u32 rotl(const u32 x, i32 k) { return (x << k) | (x >> (32 - k)); } -uint32 xoshiro128plusplus_next(void* _state){ +u32 xoshiro128plusplus_next(void* _state){ xoshiro128_state* state=_state; - const uint32 result = rotl(state->s[0] + state->s[3], 7) + state->s[0]; + const u32 result = rotl(state->s[0] + state->s[3], 7) + state->s[0]; - const uint32 t = state->s[1] << 9; + const u32 t = state->s[1] << 9; state->s[2] ^= state->s[0]; state->s[3] ^= state->s[1]; diff --git a/src/random/xoshiro/32bitValue/xoshiro128starstar.c b/src/random/xoshiro/32bitValue/xoshiro128starstar.c index 40a53d2..87a20bc 100644 --- a/src/random/xoshiro/32bitValue/xoshiro128starstar.c +++ b/src/random/xoshiro/32bitValue/xoshiro128starstar.c @@ -22,15 +22,15 @@ See . */ The state must be seeded so that it is not everywhere zero. */ -static inline uint32 rotl(const uint32 x, int k) { +static inline u32 rotl(const u32 x, i32 k) { return (x << k) | (x >> (32 - k)); } -uint32 xoshiro128starstar_next(void* _state){ +u32 xoshiro128starstar_next(void* _state){ xoshiro128_state* state=_state; - const uint32 result = rotl(state->s[1] * 5, 7) * 9; + const u32 result = rotl(state->s[1] * 5, 7) * 9; - const uint32 t = state->s[1] << 9; + const u32 t = state->s[1] << 9; state->s[2] ^= state->s[0]; state->s[3] ^= state->s[1]; diff --git a/src/random/xoshiro/64bitValue/xoshiro256.h b/src/random/xoshiro/64bitValue/xoshiro256.h index 479c6aa..263d75e 100644 --- a/src/random/xoshiro/64bitValue/xoshiro256.h +++ b/src/random/xoshiro/64bitValue/xoshiro256.h @@ -9,11 +9,11 @@ extern "C" { typedef union { - uint64 s[4]; + u64 s[4]; } xoshiro256_state; typedef void* xoshiro256_statePtr; -xoshiro256_statePtr xoshiro256_init(uint64 seed); +xoshiro256_statePtr xoshiro256_init(u64 seed); #define xoshiro256plus_init xoshiro256_init #define xoshiro256plusplus_init xoshiro256_init #define xoshiro256starstar_init xoshiro256_init @@ -23,9 +23,9 @@ static inline xoshiro256_statePtr xoshiro256_initFromTime(void) { return xoshiro #define xoshiro256plusplus_initFromTime xoshiro256_initFromTime #define xoshiro256starstar_initFromTime xoshiro256_initFromTime -uint64 xoshiro256plus_next(xoshiro256_statePtr); -uint64 xoshiro256plusplus_next(xoshiro256_statePtr); -uint64 xoshiro256starstar_next(xoshiro256_statePtr); +u64 xoshiro256plus_next(xoshiro256_statePtr); +u64 xoshiro256plusplus_next(xoshiro256_statePtr); +u64 xoshiro256starstar_next(xoshiro256_statePtr); static inline void xoshiro256_free(xoshiro256_statePtr state) { free(state); diff --git a/src/random/xoshiro/64bitValue/xoshiro256plus.c b/src/random/xoshiro/64bitValue/xoshiro256plus.c index 066ef8a..ee7ae32 100644 --- a/src/random/xoshiro/64bitValue/xoshiro256plus.c +++ b/src/random/xoshiro/64bitValue/xoshiro256plus.c @@ -24,15 +24,15 @@ See . */ output to fill s. */ -static inline uint64 rotl(const uint64 x, int k) { +static inline u64 rotl(const u64 x, i32 k) { return (x << k) | (x >> (64 - k)); } -uint64 xoshiro256plus_next(void* _state){ +u64 xoshiro256plus_next(void* _state){ xoshiro256_state* state=_state; - const uint64 result = state->s[0] + state->s[3]; + const u64 result = state->s[0] + state->s[3]; - const uint64 t = state->s[1] << 17; + const u64 t = state->s[1] << 17; state->s[2] ^= state->s[0]; state->s[3] ^= state->s[1]; @@ -46,7 +46,7 @@ uint64 xoshiro256plus_next(void* _state){ return result; } -void* xoshiro256_init(uint64 seed){ +void* xoshiro256_init(u64 seed){ xoshiro256_state* state=malloc(sizeof(xoshiro256_state)); splitmix64_state* splitmix=splitmix64_init(seed); state->s[0]=splitmix64_next(splitmix); diff --git a/src/random/xoshiro/64bitValue/xoshiro256plusplus.c b/src/random/xoshiro/64bitValue/xoshiro256plusplus.c index 77ca943..d28eb7c 100644 --- a/src/random/xoshiro/64bitValue/xoshiro256plusplus.c +++ b/src/random/xoshiro/64bitValue/xoshiro256plusplus.c @@ -13,20 +13,20 @@ See . */ enough for any parallel application, and it passes all tests we are aware of. - For generating just floating-point numbers, xoshiro256+ is even faster. + For generating just floating-poi32 numbers, xoshiro256+ is even faster. The state must be seeded so that it is not everywhere zero. If you have a 64-bit seed, we suggest to seed a splitmix64 generator and use its output to fill s. */ -static inline uint64 rotl(const uint64 x, int k) { +static inline u64 rotl(const u64 x, i32 k) { return (x << k) | (x>>(64 - k)); } -uint64 xoshiro256plusplus_next(void* _state) { +u64 xoshiro256plusplus_next(void* _state) { xoshiro256_state* state=_state; - const uint64 result=rotl(state->s[0] + state->s[3], 23) + state->s[0]; - const uint64 t=state->s[1] << 17; + const u64 result=rotl(state->s[0] + state->s[3], 23) + state->s[0]; + const u64 t=state->s[1] << 17; state->s[2] ^= state->s[0]; state->s[3] ^= state->s[1]; state->s[1] ^= state->s[2]; diff --git a/src/random/xoshiro/64bitValue/xoshiro256starstar.c b/src/random/xoshiro/64bitValue/xoshiro256starstar.c index 7829096..804007c 100644 --- a/src/random/xoshiro/64bitValue/xoshiro256starstar.c +++ b/src/random/xoshiro/64bitValue/xoshiro256starstar.c @@ -13,21 +13,21 @@ See . */ large enough for any parallel application, and it passes all tests we are aware of. - For generating just floating-point numbers, xoshiro256+ is even faster. + For generating just floating-poi32 numbers, xoshiro256+ is even faster. The state must be seeded so that it is not everywhere zero. If you have a 64-bit seed, we suggest to seed a splitmix64 generator and use its output to fill s. */ -static inline uint64 rotl(const uint64 x, int k) { +static inline u64 rotl(const u64 x, i32 k) { return (x << k) | (x >> (64 - k)); } -uint64 xoshiro256starstar_next(void* _state){ +u64 xoshiro256starstar_next(void* _state){ xoshiro256_state* state=_state; - const uint64 result = rotl(state->s[1] * 5, 7) * 9; + const u64 result = rotl(state->s[1] * 5, 7) * 9; - const uint64 t = state->s[1] << 17; + const u64 t = state->s[1] << 17; state->s[2] ^= state->s[0]; state->s[3] ^= state->s[1]; diff --git a/tests/main.cpp b/tests/main.cpp index 9c43add..14e310c 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,6 +1,6 @@ #include "tests.h" -int main(){ +i32 main(){ if(!setlocale(LC_ALL, "C.UTF8")) kprintf("\e[93msetlocale failed\n"); ktDescriptors_beginInit(); diff --git a/tests/test_autoarr-vs-vector.cpp b/tests/test_autoarr-vs-vector.cpp index 1ea747d..c85675b 100644 --- a/tests/test_autoarr-vs-vector.cpp +++ b/tests/test_autoarr-vs-vector.cpp @@ -2,16 +2,16 @@ #include "../src/Autoarr/Autoarr.h" #include -int64 _autoarrVsVector(uint16 blockCount, uint16 blockLength){ - uint32 count=blockLength*blockCount; - kprintf("\e[94mblock count: %u block length: %u count: " IFWIN("%llu", "%lu") "\n", blockCount, blockLength, (uint64)count); - Autoarr_int64* ar=Autoarr_create(int64, blockCount, blockLength); - std::vector vec=std::vector(); +i64 _autoarrVsVector(u16 blockCount, u16 blockLength){ + u32 count=blockLength*blockCount; + kprintf("\e[94mblock count: %u block length: %u count: " IFWIN("%llu", "%lu") "\n", blockCount, blockLength, (u64)count); + Autoarr_i64* ar=Autoarr_create(i64, blockCount, blockLength); + std::vector vec=std::vector(); optime("Autoarr_add", count, Autoarr_add(ar, op_i)); optime("vector_push_back", count, vec.push_back(op_i)); - int64 t=0; + i64 t=0; optime("Autoarr_get", count, t=Autoarr_get(ar, op_i)); optime("vector_get", count, diff --git a/tests/test_autoarr.c b/tests/test_autoarr.c index 2b3d24d..3e468fb 100644 --- a/tests/test_autoarr.c +++ b/tests/test_autoarr.c @@ -1,8 +1,8 @@ #include "tests.h" #include "../src/Autoarr/Autoarr.h" -static void printautoarr(Autoarr(uint16)* ar){ - kprintf("\e[94mAutoarr(uint16): " +static void printautoarr(Autoarr(u16)* ar){ + kprintf("\e[94mAutoarr(u16): " IFWIN("%llu", "%lu") "\n max_blocks_count: %u\n" " blocks_count: %u\n" @@ -10,7 +10,7 @@ static void printautoarr(Autoarr(uint16)* ar){ " block_length: %u\n" " max_length: %u\n" " length: %u\n", - sizeof(Autoarr(uint16)), + sizeof(Autoarr(u16)), ar->max_blocks_count, ar->blocks_count, ar->max_block_length, @@ -19,18 +19,18 @@ static void printautoarr(Autoarr(uint16)* ar){ Autoarr_length(ar)); } -static void fillar(Autoarr(uint16)* ar){ - for (uint16 i=0;irows[h]; - uint32 l=Autoarr_length(ar); + u32 l=Autoarr_length(ar); lgs[l]++; } - for(uint32 i=0; i0) { char* str0=char_multiply(' ',i>=100?0:(i>=10?1:2)); char* str1=char_multiply(' ',lgs[i]>=100?0:(lgs[i]>=10?1:2)); @@ -36,7 +36,7 @@ void printrowgraph(Hashtable* ht){ } } -char* genkey(uint32 i){ +char* genkey(u32 i){ char* key=malloc(12); IFMSC( sprintf_s(key,12,"key_%u",i), @@ -46,13 +46,13 @@ char* genkey(uint32 i){ } void fill(Hashtable* ht){ - for(uint32 i=0;i<100000;i++) + for(u32 i=0;i<100000;i++) Hashtable_add(ht,genkey(i),UniUInt64(i)); } Unitype gett(Hashtable* ht){ Unitype u; - for(uint32 i=0;i<100000;i++){ + for(u32 i=0;i<100000;i++){ char* key=genkey(i); u=Hashtable_get(ht,key); free(key); diff --git a/tests/test_kprint.c b/tests/test_kprint.c index 587ff59..9da6553 100644 --- a/tests/test_kprint.c +++ b/tests/test_kprint.c @@ -5,7 +5,7 @@ void test_kprint(){ //int kprint(kp_fgCyan| kp_i,-8888, kp_c,' ', kp_i,0, kp_c,' ', kp_i,1234567890987654321LL,kp_s,"\n"); - //uint + //u kprint(kp_fgGreen| kp_u|kp_post,-8888, kp_c|kp_post|kp_upper,' ', kp_u,0, kp_c,' ', kp_u,1234567890987654321LL, kp_c,'\n'); @@ -15,7 +15,7 @@ void test_kprint(){ kp_f,-1.0f, kp_c,' ', kp_f,0.0f, kp_c,' ', kp_f,1.0f, kp_c,'\n', kp_f|kp_post,0.000020004f, kp_c,' ', kp_f|kp_post|kp_upper,4000.0109f, kp_c,'\n'); - //double + //f64 kprint(kp_fgYellowD| kp_f,-4000.0109, kp_c,' ', kp_f,-0.000020004, kp_c,'\n', kp_f,-1.0, kp_c,' ', kp_f,0.0, kp_c,' ', kp_f,1.0, kp_c,'\n', diff --git a/tests/test_kprint_colors.c b/tests/test_kprint_colors.c index b397e21..4c97447 100644 --- a/tests/test_kprint_colors.c +++ b/tests/test_kprint_colors.c @@ -16,13 +16,13 @@ void test_kprint_colors(){ /* IFWIN( ({ HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); - for(uint8 col=0; col<255; col++){ + for(u8 col=0; col<255; col++){ SetConsoleTextAttribute(hConsole, col); kprintf("%u ",col); } }), ({ - for(uint8 col=0; col<255; col++) + for(u8 col=0; col<255; col++) kprintf("\e[%um%u ", col, col); }) ); diff --git a/tests/test_rng_algorithms.c b/tests/test_rng_algorithms.c index 1af5ef5..23e8ab9 100644 --- a/tests/test_rng_algorithms.c +++ b/tests/test_rng_algorithms.c @@ -5,18 +5,18 @@ #define test_alg(ALG, VALUE_SIZE, EXPECTED_FROM_ZERO){\ kprintf("\e[94mrng algorithm: \e[96m" #ALG "\n");\ void* s= ALG##_init(0);\ - uint##VALUE_SIZE r=ALG##_next(s);\ + u##VALUE_SIZE r=ALG##_next(s);\ kprintf("\e[97m next from zero seed:");\ if(r!=EXPECTED_FROM_ZERO){\ - kprintf("\e[91m " IFWIN("%llu\n","%lu\n"), (uint64)r);\ + kprintf("\e[91m " IFWIN("%llu\n","%lu\n"), (u64)r);\ throw(ERR_UNEXPECTEDVAL);\ }\ - kprintf("\e[92m " IFWIN("%llu\n","%lu\n"), (uint64)r);\ + kprintf("\e[92m " IFWIN("%llu\n","%lu\n"), (u64)r);\ ALG##_free(s);\ s= ALG##_initFromTime();\ r=ALG##_next(s);\ ALG##_free(s);\ - kprintf("\e[97m next from time seed:\e[92m " IFWIN("%llu\n","%lu\n"), (uint64)r);\ + kprintf("\e[97m next from time seed:\e[92m " IFWIN("%llu\n","%lu\n"), (u64)r);\ } void test_rng_algorithms(){ diff --git a/tests/test_searchtree.c b/tests/test_searchtree.c index 656a96a..d315e50 100644 --- a/tests/test_searchtree.c +++ b/tests/test_searchtree.c @@ -9,13 +9,13 @@ void printstnode(STNode* node){ kprintf("\n"); // prints pointers to all existing branches /* kprintf(" branches: %p\n", node->branches); - if(node->branches) for(uint8 i=0;i<8;i++){ + if(node->branches) for(u8 i=0;i<8;i++){ kprintf(" \e[90m[%u]=%p\n",i,node->branches[i]); if(node->branches[i]) - for (uint8 ii = 0; ii < 8; ii++){ + for (u8 ii = 0; ii < 8; ii++){ kprintf(" \e[90m[%u]=%p\n",ii,node->branches[i][ii]); if(node->branches[i][ii]) - for (uint8 iii = 0; iii < 4; iii++) + for (u8 iii = 0; iii < 4; iii++) kprintf(" \e[90m[%u]=%p\n",iii,node->branches[i][ii][iii]); } From 95fec8d1662fffbafecbfacdf60e297da54dc56a Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Mon, 13 Feb 2023 00:34:32 +0600 Subject: [PATCH 13/55] breaking changes in type system --- src/Array/Array.c | 1 + src/Array/Array.h | 1 + src/Array/Array_declare.h | 48 ++++---- src/Array/Array_define.h | 4 +- src/Autoarr/Autoarr.c | 1 + src/Autoarr/Autoarr.h | 2 +- src/Autoarr/Autoarr_Unitype.h | 26 ++-- src/Autoarr/Autoarr_declare.h | 72 ++++++----- src/Autoarr/Autoarr_define.h | 126 +++++++++---------- src/DtsodParser/DtsodV24_deserialize.c | 8 +- src/DtsodParser/DtsodV24_serialize.c | 6 +- src/Filesystem/file.c | 10 +- src/Filesystem/file.h | 4 +- src/Hashtable/Hashtable.c | 4 +- src/Hashtable/Hashtable.h | 18 +-- src/Hashtable/KeyValuePair.c | 2 +- src/Hashtable/KeyValuePair.h | 5 +- src/SearchTree/SearchTree.c | 5 +- src/SearchTree/SearchTree.h | 7 +- src/String/StringBuilder.c | 4 +- src/String/StringBuilder.h | 7 +- src/String/string.c | 5 +- src/String/string.h | 10 +- src/base/endian.h | 5 +- src/base/errors.h | 52 ++++---- src/base/optime.h | 52 ++++---- src/base/std.h | 49 ++++---- src/base/type_system/README.md | 14 ++- src/base/type_system/base_toString.c | 162 ++++++++++++++----------- src/base/type_system/base_toString.h | 4 +- src/base/type_system/init.c | 113 +++++++++-------- src/base/type_system/ktDescriptor.h | 28 ++++- src/base/type_system/kt_functions.c | 67 +++++----- src/base/type_system/kt_functions.h | 41 +++---- src/base/type_system/ktid.h | 11 +- src/base/type_system/type_system.h | 3 + src/base/type_system/typedef_macros.h | 14 +++ src/base/type_system/unitype.c | 108 +++++++++++------ src/base/type_system/unitype.h | 19 +-- src/kprint/README.md | 2 +- src/kprint/kprint.c | 20 +-- src/kprint/kprint.h | 68 +++++------ src/kprint/kprint_colors.h | 4 +- src/kprint/kprint_format.h | 2 +- tests/test_hash_functions.c | 54 ++++----- tests/test_kprint_colors.c | 10 +- tests/test_rng_algorithms.c | 30 ++--- tests/test_type_system.c | 4 +- 48 files changed, 704 insertions(+), 608 deletions(-) create mode 100644 src/base/type_system/typedef_macros.h diff --git a/src/Array/Array.c b/src/Array/Array.c index c6a3145..2d293c2 100644 --- a/src/Array/Array.c +++ b/src/Array/Array.c @@ -12,6 +12,7 @@ Array_define(i32) Array_define(u32) Array_define(i64) Array_define(u64) +Array_define(Pointer) Array_define(Unitype) diff --git a/src/Array/Array.h b/src/Array/Array.h index 31b00f6..0f16588 100644 --- a/src/Array/Array.h +++ b/src/Array/Array.h @@ -19,6 +19,7 @@ Array_declare(i32) Array_declare(u32) Array_declare(i64) Array_declare(u64) +Array_declare(Pointer) Array_declare(Unitype) diff --git a/src/Array/Array_declare.h b/src/Array/Array_declare.h index 089874d..0c18b52 100644 --- a/src/Array/Array_declare.h +++ b/src/Array/Array_declare.h @@ -6,34 +6,32 @@ extern "C" { #include "../base/base.h" -#define Array_declare(type)\ -typedef struct Array_##type{\ - type* values;\ - u32 length;\ - bool allocatedOnHeap;\ -} Array_##type;\ +#define Array_declare(type) \ +STRUCT(Array_##type, \ + type* values; \ + u32 length; \ + bool allocatedOnHeap; \ +) \ \ -ktid_declare(Array_##type);\ +static inline Array_##type Array_##type##_allocValues(u32 length){ \ + return (Array_##type) { \ + .values=(type*)malloc(sizeof(type)*length), \ + .length=length, \ + .allocatedOnHeap=true \ + }; \ +} \ \ -static inline Array_##type Array_##type##_allocValues(u32 length){\ - return (Array_##type) {\ - .values=(type*)malloc(sizeof(type)*length),\ - .length=length,\ - .allocatedOnHeap=true\ - };\ -}\ +static inline Array_##type Array_##type##_fromBuffer(type* buffer, u32 bufferLength, bool allocatedOnHeap){ \ + return (Array_##type) { \ + .values=buffer, \ + .length=bufferLength, \ + .allocatedOnHeap=allocatedOnHeap \ + }; \ +} \ \ -static inline Array_##type Array_##type##_fromBuffer(type* buffer, u32 bufferLength, bool allocatedOnHeap){\ - return (Array_##type) {\ - .values=buffer,\ - .length=bufferLength,\ - .allocatedOnHeap=allocatedOnHeap\ - };\ -}\ -\ -static inline void Array_##type##_free(Array_##type* array){\ - if(array->allocatedOnHeap)\ - free(array->values);\ +static inline void Array_##type##_free(Array_##type* array){ \ + if(array->allocatedOnHeap) \ + free(array->values); \ } #if __cplusplus diff --git a/src/Array/Array_define.h b/src/Array/Array_define.h index 6a16f9b..f15533c 100644 --- a/src/Array/Array_define.h +++ b/src/Array/Array_define.h @@ -6,8 +6,8 @@ extern "C" { #include "../base/base.h" -#define Array_define(type)\ -ktid_define(Array_##type); +#define Array_define(type) \ +kt_define(Array_##type, (freeMembers_t)Array_##type##_free, NULL); #if __cplusplus } diff --git a/src/Autoarr/Autoarr.c b/src/Autoarr/Autoarr.c index 84c8c21..b8a57e4 100644 --- a/src/Autoarr/Autoarr.c +++ b/src/Autoarr/Autoarr.c @@ -12,3 +12,4 @@ Autoarr_define(u32) Autoarr_define(i32) Autoarr_define(u64) Autoarr_define(i64) +Autoarr_define(Pointer) diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h index 23e6f11..e75da8f 100644 --- a/src/Autoarr/Autoarr.h +++ b/src/Autoarr/Autoarr.h @@ -20,7 +20,7 @@ Autoarr_declare(i32) Autoarr_declare(u32) Autoarr_declare(i64) Autoarr_declare(u64) - +Autoarr_declare(Pointer) #if __cplusplus } diff --git a/src/Autoarr/Autoarr_Unitype.h b/src/Autoarr/Autoarr_Unitype.h index fdcd4e5..645f4e3 100644 --- a/src/Autoarr/Autoarr_Unitype.h +++ b/src/Autoarr/Autoarr_Unitype.h @@ -13,19 +13,19 @@ Autoarr_declare(Unitype) void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr); void ____Autoarr_free_Unitype_(void* ar); -#define Autoarr_foreach(ar,elem,codeblock)({\ - if(ar->blocks_count>0) {\ - typeof(**ar->values) elem;\ - for(u32 blockI=0;blockIblocks_count-1;blockI++)\ - for(u32 elemI=0;elemImax_block_length;elemI++){\ - elem=ar->values[blockI][elemI];\ - (codeblock);\ - }\ - for(u32 elemI=0;elemIblock_length;elemI++){\ - elem=ar->values[ar->blocks_count-1][elemI];\ - (codeblock);\ - }\ - }\ +#define Autoarr_foreach(ar,elem,codeblock)({ \ + if(ar->blocks_count>0) { \ + typeof(**ar->values) elem; \ + for(u32 blockI=0;blockIblocks_count-1;blockI++) \ + for(u32 elemI=0;elemImax_block_length;elemI++){ \ + elem=ar->values[blockI][elemI]; \ + (codeblock); \ + } \ + for(u32 elemI=0;elemIblock_length;elemI++){ \ + elem=ar->values[ar->blocks_count-1][elemI]; \ + (codeblock); \ + } \ + } \ }) #if __cplusplus diff --git a/src/Autoarr/Autoarr_declare.h b/src/Autoarr/Autoarr_declare.h index d449b5b..2f01431 100644 --- a/src/Autoarr/Autoarr_declare.h +++ b/src/Autoarr/Autoarr_declare.h @@ -6,64 +6,62 @@ extern "C" { #include "../base/base.h" -#define Autoarr_declare(type)\ +#define Autoarr_declare(type) \ \ -struct Autoarr_##type;\ +struct Autoarr_##type; \ \ -typedef struct {\ - void (*add)(struct Autoarr_##type* ar, type element);\ - type (*get)(struct Autoarr_##type* ar, u32 index);\ - type* (*getptr)(struct Autoarr_##type* ar, u32 index);\ - void (*set)(struct Autoarr_##type* ar, u32 index, type element);\ - void (*freear)(struct Autoarr_##type* ar, bool freePtr);\ - type* (*toArray)(struct Autoarr_##type* ar);\ -} __functions_list_t_##type;\ +STRUCT(__functions_list_t_##type, \ + void (*add)(struct Autoarr_##type* ar, type element); \ + type (*get)(struct Autoarr_##type* ar, u32 index); \ + type* (*getptr)(struct Autoarr_##type* ar, u32 index); \ + void (*set)(struct Autoarr_##type* ar, u32 index, type element); \ + void (*freear)(struct Autoarr_##type* ar, bool freePtr); \ + type* (*toArray)(struct Autoarr_##type* ar); \ +) \ \ -typedef struct Autoarr_##type{\ - u16 blocks_count;\ - u16 max_blocks_count;\ - u16 block_length;\ - u16 max_block_length;\ - type** values;\ - __functions_list_t_##type* functions;\ -} Autoarr_##type;\ +STRUCT(Autoarr_##type, \ + u16 blocks_count; \ + u16 max_blocks_count; \ + u16 block_length; \ + u16 max_block_length; \ + type** values; \ + __functions_list_t_##type* functions; \ +) \ \ -ktid_declare(Autoarr_##type);\ -\ -Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length);\ -void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr);\ +Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length); \ +void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr); \ void ____Autoarr_free_##type(void* ar); #define Autoarr(type) Autoarr_##type -#define Autoarr_create(type, max_blocks_count, max_block_length)\ +#define Autoarr_create(type, max_blocks_count, max_block_length) \ __Autoarr_create_##type(max_blocks_count, max_block_length) -#define Autoarr_add(autoarr, element)\ +#define Autoarr_add(autoarr, element) \ autoarr->functions->add(autoarr, element) -#define Autoarr_get(autoarr, index)\ +#define Autoarr_get(autoarr, index) \ autoarr->functions->get(autoarr,index) -#define Autoarr_getptr(autoarr, index)\ +#define Autoarr_getptr(autoarr, index) \ autoarr->functions->getptr(autoarr,index) -#define Autoarr_set(autoarr, index, element)\ +#define Autoarr_set(autoarr, index, element) \ autoarr->functions->set(autoarr, index, element) -#define Autoarr_free(autoarr, freePtr)\ +#define Autoarr_free(autoarr, freePtr) \ autoarr->functions->freear(autoarr, freePtr) -#define Autoarr_toArray(autoarr)\ +#define Autoarr_toArray(autoarr) \ autoarr->functions->toArray(autoarr) #define Autoarr_length(autoarr) \ (u32)(!autoarr->blocks_count ? 0 : \ autoarr->max_block_length*(autoarr->blocks_count-1)+autoarr->block_length) -#define Autoarr_max_length(autoarr)\ +#define Autoarr_max_length(autoarr) \ (u32)(autoarr->max_block_length*autoarr->max_blocks_count) -#define Autoarr_pop(AR){\ - if(AR->block_length==1){\ - AR->blocks_count--;\ - AR->block_length=AR->max_block_length;\ - free(AR->values[AR->blocks_count]);\ - }\ - else AR->block_length--;\ +#define Autoarr_pop(AR){ \ + if(AR->block_length==1){ \ + AR->blocks_count--; \ + AR->block_length=AR->max_block_length; \ + free(AR->values[AR->blocks_count]); \ + } \ + else AR->block_length--; \ } #if __cplusplus diff --git a/src/Autoarr/Autoarr_define.h b/src/Autoarr/Autoarr_define.h index 9e82e4d..d37c2bc 100644 --- a/src/Autoarr/Autoarr_define.h +++ b/src/Autoarr/Autoarr_define.h @@ -6,79 +6,79 @@ extern "C" { #include "../base/base.h" -#define Autoarr_define(type)\ +#define Autoarr_define(type) \ \ -ktid_define(Autoarr_##type);\ +kt_define(Autoarr_##type, ____Autoarr_free_##type, NULL); \ \ -void __Autoarr_add_##type(Autoarr_##type* ar, type element){\ - if(!ar->values){\ - ar->values=malloc(ar->max_blocks_count*sizeof(type*));\ - goto create_block;\ - }\ - if(ar->block_length==ar->max_block_length){\ - if (ar->blocks_count>=ar->max_blocks_count) throw(ERR_MAXLENGTH);\ - ar->block_length=0;\ -create_block:\ - ar->values[ar->blocks_count]=malloc(ar->max_block_length*sizeof(type));\ - ar->blocks_count++;\ - }\ - ar->values[ar->blocks_count-1][ar->block_length]=element;\ - ar->block_length++;\ -}\ +void __Autoarr_add_##type(Autoarr_##type* ar, type element){ \ + if(!ar->values){ \ + ar->values=malloc(ar->max_blocks_count*sizeof(type*)); \ + goto create_block; \ + } \ + if(ar->block_length==ar->max_block_length){ \ + if (ar->blocks_count>=ar->max_blocks_count) throw(ERR_MAXLENGTH); \ + ar->block_length=0; \ +create_block: \ + ar->values[ar->blocks_count]=malloc(ar->max_block_length*sizeof(type)); \ + ar->blocks_count++; \ + } \ + ar->values[ar->blocks_count-1][ar->block_length]=element; \ + ar->block_length++; \ +} \ \ -type __Autoarr_get_##type(Autoarr_##type* ar, u32 index){\ - if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\ - return ar->values[index/ar->max_block_length][index%ar->max_block_length];\ -}\ +type __Autoarr_get_##type(Autoarr_##type* ar, u32 index){ \ + if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \ + return ar->values[index/ar->max_block_length][index%ar->max_block_length]; \ +} \ \ -type* __Autoarr_getptr_##type(Autoarr_##type* ar, u32 index){\ - if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\ - return ar->values[index/ar->max_block_length]+(index%ar->max_block_length);\ -}\ +type* __Autoarr_getptr_##type(Autoarr_##type* ar, u32 index){ \ + if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \ + return ar->values[index/ar->max_block_length]+(index%ar->max_block_length); \ +} \ \ -void __Autoarr_set_##type(Autoarr_##type* ar, u32 index, type element){\ - if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX);\ - ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\ -}\ +void __Autoarr_set_##type(Autoarr_##type* ar, u32 index, type element){ \ + if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \ + ar->values[index/ar->max_block_length][index%ar->max_block_length]=element; \ +} \ \ -void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr){\ - for(u16 i=0; iblocks_count;i++)\ +void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr){ \ + for(u16 i=0; iblocks_count;i++) \ free(ar->values[i]); \ - free(ar->values);\ - if(freePtr) free(ar);\ -}\ -void ____Autoarr_free_##type(void* ar){\ - __Autoarr_free_##type((Autoarr_##type*)ar, false);\ -}\ + free(ar->values); \ + if(freePtr) free(ar); \ +} \ +void ____Autoarr_free_##type(void* ar){ \ + __Autoarr_free_##type((Autoarr_##type*)ar, false); \ +} \ \ -type* __Autoarr_toArray_##type(Autoarr_##type* ar){\ - u32 length=Autoarr_length(ar);\ - type* array=malloc(length * sizeof(type));\ - for(u32 i=0; ish_builder #define tabs shared->sh_tabs @@ -46,7 +46,7 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){ else if(u.typeId==ktid_name(bool)){ StringBuilder_append_cptr(b, u.Bool ? "true" : "false"); } - else if(u.typeId==ktid_Null){ + else if(Unitype_isUniNull(u)){ safethrow("Null isn't supported in DtsodV24",;); } else if(u.typeId==ktid_ptrName(Autoarr_Unitype)){ diff --git a/src/Filesystem/file.c b/src/Filesystem/file.c index 7e7c496..c58aa4f 100644 --- a/src/Filesystem/file.c +++ b/src/Filesystem/file.c @@ -2,7 +2,7 @@ #include "../String/StringBuilder.h" #include "io_includes.h" -ktid_define(File); +kt_define(File, (freeMembers_t)file_close, NULL) bool file_exists(const char* path){ if(path[0]=='.'){ @@ -61,10 +61,10 @@ Maybe file_close(File* file){ return MaybeNull; } -#define ioWriteCheck()\ - if(rezult==EOF)\ - safethrow(ERR_IO_EOF,;);\ - if(rezult!=0)\ +#define ioWriteCheck() \ + if(rezult==EOF) \ + safethrow(ERR_IO_EOF,;); \ + if(rezult!=0) \ safethrow(ERR_IO,;); Maybe file_writeChar(File* file, char byte){ diff --git a/src/Filesystem/file.h b/src/Filesystem/file.h index af18d11..3ab151d 100644 --- a/src/Filesystem/file.h +++ b/src/Filesystem/file.h @@ -9,14 +9,14 @@ extern "C" { #include "../String/string.h" typedef FILE File; -ktid_declare(File); +kt_declare(File); bool file_exists(const char* path); ///@return Maybe Maybe file_delete(const char* path, bool recursive); -PACK_ENUM(FileOpenMode, +PACKED_ENUM(FileOpenMode, // open a file for reading FileOpenMode_Read=1, // (re)create a file for writing diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index 82e92ca..b780619 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -1,6 +1,6 @@ #include "Hashtable.h" -ktid_define(Hashtable); +kt_define(Hashtable, __Hashtable_free, NULL); // amount of rows static const u16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521}; @@ -95,7 +95,7 @@ Unitype Hashtable_get(Hashtable* ht, char* key){ bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){ Unitype u=Hashtable_get(ht,key); *output=u; - return u.typeId!=ktid_Null; + return Unitype_isUniNull(u); } void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){ diff --git a/src/Hashtable/Hashtable.h b/src/Hashtable/Hashtable.h index 76e288a..00390b9 100644 --- a/src/Hashtable/Hashtable.h +++ b/src/Hashtable/Hashtable.h @@ -4,14 +4,14 @@ extern "C" { #endif +#include "../base/base.h" #include "../HashFunctions/hash.h" #include "KeyValuePair.h" -typedef struct Hashtable{ +STRUCT(Hashtable, u8 hein; // height=HT_HEIGHTS[hein] Autoarr(KVPair)** rows; // Autoarr[height] -} Hashtable; -ktid_declare(Hashtable); +) Hashtable* Hashtable_create(); void Hashtable_free(Hashtable* ht); @@ -33,12 +33,12 @@ Unitype* Hashtable_getptr(Hashtable* ht, char* key); Unitype Hashtable_get(Hashtable* ht, char* key); bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output); -#define Hashtable_foreach(HT, EL, codeblock)({\ - u16 hmax=Hashtable_height(HT);\ - for(u16 h=0; hrows[h];\ - Autoarr_foreach(AR, EL, codeblock);\ - }\ +#define Hashtable_foreach(HT, EL, codeblock)({ \ + u16 hmax=Hashtable_height(HT); \ + for(u16 h=0; hrows[h]; \ + Autoarr_foreach(AR, EL, codeblock); \ + } \ }) #if __cplusplus diff --git a/src/Hashtable/KeyValuePair.c b/src/Hashtable/KeyValuePair.c index 7e80ede..c3e4d85 100644 --- a/src/Hashtable/KeyValuePair.c +++ b/src/Hashtable/KeyValuePair.c @@ -1,6 +1,6 @@ #include "KeyValuePair.h" -ktid_define(KVPair); +kt_define(KVPair, __KVPair_free, NULL); Autoarr_define(KVPair) diff --git a/src/Hashtable/KeyValuePair.h b/src/Hashtable/KeyValuePair.h index 280f0d6..b29e4bb 100644 --- a/src/Hashtable/KeyValuePair.h +++ b/src/Hashtable/KeyValuePair.h @@ -7,11 +7,10 @@ extern "C" { #include "../base/base.h" #include "../Autoarr/Autoarr.h" -typedef struct KVPair{ +STRUCT(KVPair, char* key; Unitype value; -} KVPair; -ktid_declare(KVPair); +) Autoarr_declare(KVPair) diff --git a/src/SearchTree/SearchTree.c b/src/SearchTree/SearchTree.c index 81b7083..5433696 100644 --- a/src/SearchTree/SearchTree.c +++ b/src/SearchTree/SearchTree.c @@ -1,12 +1,11 @@ #include "SearchTree.h" -ktid_define(STNode); +kt_define(STNode, __STNode_free, NULL); STNode* STNode_create(){ STNode* node=malloc(sizeof(STNode)); node->branches=NULL; - node->value.typeId=ktid_Null; - node->value.UInt64=0; + node->value=UniNull; return node; } diff --git a/src/SearchTree/SearchTree.h b/src/SearchTree/SearchTree.h index bc8785f..ba72486 100644 --- a/src/SearchTree/SearchTree.h +++ b/src/SearchTree/SearchTree.h @@ -7,11 +7,10 @@ extern "C" { #include "../base/base.h" #include "../String/string.h" -typedef struct SearchTreeNode{ - struct SearchTreeNode**** branches; // *STNode[8][8][4] +STRUCT(STNode, + struct STNode**** branches; // *STNode[8][8][4] Unitype value; -} STNode; -ktid_declare(STNode); +) STNode* STNode_create(); void STNode_free(STNode* node); diff --git a/src/String/StringBuilder.c b/src/String/StringBuilder.c index 3ee6aa8..82a6bbf 100644 --- a/src/String/StringBuilder.c +++ b/src/String/StringBuilder.c @@ -1,8 +1,6 @@ #include "StringBuilder.h" -Autoarr_define(string) - -ktid_define(StringBuilder); +kt_define(StringBuilder, __StringBuilder_free, NULL); #define BL_C 32 #define BL_L 1024 diff --git a/src/String/StringBuilder.h b/src/String/StringBuilder.h index 227df3f..e860f8e 100644 --- a/src/String/StringBuilder.h +++ b/src/String/StringBuilder.h @@ -7,13 +7,10 @@ extern "C" { #include "../Autoarr/Autoarr.h" #include "string.h" -Autoarr_declare(string) - -typedef struct StringBuilder{ +STRUCT(StringBuilder, Autoarr(string)* compl_bufs; Autoarr(i8)* curr_buf; -} StringBuilder; -ktid_declare(StringBuilder); +) StringBuilder* StringBuilder_create(void); void StringBuilder_free(StringBuilder* b); diff --git a/src/String/string.c b/src/String/string.c index 5d2246a..36c2684 100644 --- a/src/String/string.c +++ b/src/String/string.c @@ -1,7 +1,8 @@ #include "string.h" -ktid_define(string); -Array_define(string); +kt_define(string, NULL, NULL); +Array_define(string) +Autoarr_define(string) // copies str content to new char pointer value (adding '\0' at the end) char* string_extract(string str){ diff --git a/src/String/string.h b/src/String/string.h index 3fe98cb..2e96fe8 100644 --- a/src/String/string.h +++ b/src/String/string.h @@ -6,15 +6,17 @@ extern "C" { #include "../base/base.h" #include "../Array/Array.h" +#include "../Autoarr/Autoarr.h" // my fixed length string struct // doesn't store '\0' at the end -typedef struct string{ +STRUCT(string, char* ptr; // char pointer u64 length; // amount of chars in ptr value -} string; -ktid_declare(string); -Array_declare(string); +) + +Array_declare(string) +Autoarr_declare(string) static const string stringNull={NULL,0}; diff --git a/src/base/endian.h b/src/base/endian.h index 5fb2603..5863309 100644 --- a/src/base/endian.h +++ b/src/base/endian.h @@ -5,12 +5,13 @@ extern "C" { #endif #include "std.h" +#include "type_system/typedef_macros.h" -PACK_ENUM(Endian, +PACKED_ENUM(Endian, UnknownEndian=0, LittleEndian=1, BigEndian=2 -); +) Endian getEndian(); diff --git a/src/base/errors.h b/src/base/errors.h index bb1dce6..2608ec0 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -5,9 +5,9 @@ extern "C" { #endif #include "std.h" -#include "type_system/unitype.h" +#include "type_system/type_system.h" -PACK_ENUM(ErrorId, +PACKED_ENUM(ErrorId, SUCCESS, // not an error ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR, @@ -20,10 +20,10 @@ char* errname(ErrorId err); char* __genErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname); char* __extendErrMsg(const char* errmsg, const char* srcfile, i32 line, const char* funcname); -typedef struct Maybe{ +STRUCT(Maybe, Unitype value; char* errmsg; -} Maybe; +) // return it if func doesn't return anything // .value .errmsg @@ -42,47 +42,47 @@ void printMaybe(Maybe e); char* __doNothing(char* a); char* __unknownErr( ); -#define __stringify_err(E) _Generic(\ - (E),\ - char*: __doNothing,\ - int: errname,\ - default: __unknownErr\ +#define __stringify_err(E) _Generic( \ + (E), \ + char*: __doNothing, \ + int: errname, \ + default: __unknownErr \ )(E) #if __cplusplus #define throw_id(E) __EXIT(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__))) #define throw_msg(E) __EXIT(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__))) -#define safethrow_id(E, FREEMEM) { FREEMEM;\ - __RETURN_EXCEPTION(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__)));\ +#define safethrow_id(E, FREEMEM) { FREEMEM; \ + __RETURN_EXCEPTION(((char*)__genErrMsg(errname(E), __FILE__,__LINE__,__func__))); \ } -#define safethrow_msg(E, FREEMEM) { FREEMEM;\ - __RETURN_EXCEPTION(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__)));\ +#define safethrow_msg(E, FREEMEM) { FREEMEM; \ + __RETURN_EXCEPTION(((char*)__genErrMsg(E, __FILE__,__LINE__,__func__))); \ } -#define try_cpp(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){\ - freeMem;\ - _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ - return _rezult;\ +#define try_cpp(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){ \ + freeMem; \ + _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__); \ + return _rezult; \ } #else #define throw(E) __EXIT(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))) -#define safethrow(E, FREEMEM) { FREEMEM;\ - __RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__)));\ +#define safethrow(E, FREEMEM) { FREEMEM; \ + __RETURN_EXCEPTION(((char*)__genErrMsg((__stringify_err(E)), __FILE__,__LINE__,__func__))); \ } -#define try(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){\ - freeMem;\ - _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ - return _rezult;\ +#define try(_funcCall, _rezult, freeMem) Maybe _rezult=_funcCall; if(_rezult.errmsg){ \ + freeMem; \ + _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__); \ + return _rezult; \ } #endif -#define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\ - _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ - __EXIT(_rezult.errmsg);\ +#define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){ \ + _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__); \ + __EXIT(_rezult.errmsg); \ } #if __cplusplus diff --git a/src/base/optime.h b/src/base/optime.h index 8f5bd2d..f8bc411 100644 --- a/src/base/optime.h +++ b/src/base/optime.h @@ -2,40 +2,40 @@ #include "std.h" -#define __optime_print(opname, t)\ - char tnames[3][3]={"s\0","ms","us"};\ - i32 tni=0;\ - if(t>1000000){\ - t/=1000000;\ - tni=0;\ - } else if(t>1000){\ - t/=1000;\ - tni=1;\ - } else tni=2;\ - kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%f \e[93m%s\n",\ +#define __optime_print(opname, t) \ + char tnames[3][3]={"s\0","ms","us"}; \ + i32 tni=0; \ + if(t>1000000){ \ + t/=1000000; \ + tni=0; \ + } else if(t>1000){ \ + t/=1000; \ + tni=1; \ + } else tni=2; \ + kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%f \e[93m%s\n", \ opname, t, tnames[tni]); #ifdef CLOCK_REALTIME /// executes codeblock and prints execution time /// u64 op_i is counter of the internal loop /// uses non-standard high-precision clock -#define optime(opname,repeats,codeblock) ({\ - struct timespec start, stop;\ - clock_gettime(CLOCK_REALTIME, &start);\ - for(u64 op_i=0;op_i<(u64)repeats;op_i++)\ - (codeblock);\ - clock_gettime(CLOCK_REALTIME, &stop);\ - f64 t=(f64)(stop.tv_sec-start.tv_sec)*1000000+(f64)(stop.tv_nsec-start.tv_nsec)/1000;\ - __optime_print(opname,t)\ +#define optime(opname,repeats,codeblock) ({ \ + struct timespec start, stop; \ + clock_gettime(CLOCK_REALTIME, &start); \ + for(u64 op_i=0;op_i<(u64)repeats;op_i++) \ + (codeblock); \ + clock_gettime(CLOCK_REALTIME, &stop); \ + f64 t=(f64)(stop.tv_sec-start.tv_sec)*1000000+(f64)(stop.tv_nsec-start.tv_nsec)/1000; \ + __optime_print(opname,t) \ }) #else /// uses standard low precision clock -#define optime(opname,repeats,codeblock) ({\ - clock_t start=clock();\ - for(u64 op_i=0;op_i<(u64)repeats;op_i++)\ - (codeblock);\ - clock_t stop=clock();\ - f64 t=(f64)(stop-start)/CLOCKS_PER_SEC*1000000;\ - __optime_print(opname,t)\ +#define optime(opname,repeats,codeblock) ({ \ + clock_t start=clock(); \ + for(u64 op_i=0;op_i<(u64)repeats;op_i++) \ + (codeblock); \ + clock_t stop=clock(); \ + f64 t=(f64)(stop-start)/CLOCKS_PER_SEC*1000000; \ + __optime_print(opname,t) \ }) #endif diff --git a/src/base/std.h b/src/base/std.h index d102a70..1fc07e1 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -22,6 +22,7 @@ typedef int64_t i64; typedef uint64_t u64; typedef float f32; typedef double f64; +typedef void* Pointer; // Usually bool from stdbool.h is defined as macro, // so in other macros like ktid_##TYPE it will be replaced by _Bool. @@ -72,27 +73,27 @@ typedef u8 bool; #endif -#define __count_args(\ - a0, a1, a2, a3, a4, a5, a6, a7 ,\ - a8, a9, a10,a11,a12,a13,a14,a15,\ - a16,a17,a18,a19,a20,a21,a22,a23,\ - a24,a25,a26,a27,a28,a29,a30,a31,\ - a32,a33,a34,a35,a36,a37,a38,a39,\ - a40,a41,a42,a43,a44,a45,a46,a47,\ - a48,a49,a50,a51,a52,a53,a54,a55,\ - a56,a57,a58,a59,a60,a61,a62,a63,\ +#define __count_args( \ + a0, a1, a2, a3, a4, a5, a6, a7 , \ + a8, a9, a10,a11,a12,a13,a14,a15, \ + a16,a17,a18,a19,a20,a21,a22,a23, \ + a24,a25,a26,a27,a28,a29,a30,a31, \ + a32,a33,a34,a35,a36,a37,a38,a39, \ + a40,a41,a42,a43,a44,a45,a46,a47, \ + a48,a49,a50,a51,a52,a53,a54,a55, \ + a56,a57,a58,a59,a60,a61,a62,a63, \ a64,...) a64 // Macro for counting variadic arguments (max 64) // (see usage in kprint.h) -#define count_args(ARGS...) __count_args(\ - ARGS,\ - 64,63,62,61,60,59,58,57,\ - 56,55,54,53,52,51,50,49,\ - 48,47,46,45,44,43,42,41,\ - 40,39,38,37,36,35,34,33,\ - 32,31,30,29,28,27,26,25,\ - 24,23,22,21,20,19,18,17,\ - 16,15,14,13,12,11,10,9,\ +#define count_args(ARGS...) __count_args( \ + ARGS, \ + 64,63,62,61,60,59,58,57, \ + 56,55,54,53,52,51,50,49, \ + 48,47,46,45,44,43,42,41, \ + 40,39,38,37,36,35,34,33, \ + 32,31,30,29,28,27,26,25, \ + 24,23,22,21,20,19,18,17, \ + 16,15,14,13,12,11,10,9, \ 8, 7, 6, 5, 4, 3, 2, 1, 0) /* @@ -113,16 +114,12 @@ You can even embed it into macro in header (see kprint.h) #define PRAGMA_WARNING_POP _PRAGMA(GCC diagnostic pop) #define W_INT_CONVERSION "-Wint-conversion" #endif -#define WARNING_DISABLE(WARNING, CODE)\ - PRAGMA_WARNING_PUSH\ - PRAGMA_WARNING_DISABLE(WARNING)\ - CODE;\ +#define WARNING_DISABLE(WARNING, CODE) \ + PRAGMA_WARNING_PUSH \ + PRAGMA_WARNING_DISABLE(WARNING) \ + CODE; \ PRAGMA_WARNING_POP -#define PACK_ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME {\ - ENUM_MEMBERS\ -} __attribute__((__packed__)) ENUM_NAME; - #if __cplusplus } #endif \ No newline at end of file diff --git a/src/base/type_system/README.md b/src/base/type_system/README.md index 2606a85..66f54b0 100644 --- a/src/base/type_system/README.md +++ b/src/base/type_system/README.md @@ -4,17 +4,21 @@ For using some kerep capabilities, such as generic structs, unitype, and kprint, ## type id -Every registered type has its own id (`ktid`), which should be declared in header file and defined in source file. -Example: +Every registered type has its own `ktDescriptor` and `ktid` is an index of the descriptor in descriptors array. +Descriptor should be declared in header file. +Following macro declares `typedef struct` and `ktDescriptor` ```c //someStruct.h -typedef struct { } someStruct; -ktid_declare(someStruct); +STRUCT(someStruct, + i32 i; i32 j; i32 k; +); ``` +then you need to define descriptor in a source file ```c //someStruct.c -ktid_define(someStruct); +kt_define(someStruct); ``` +and register it. ## type descriptors diff --git a/src/base/type_system/base_toString.c b/src/base/type_system/base_toString.c index 42deca9..56b0291 100644 --- a/src/base/type_system/base_toString.c +++ b/src/base/type_system/base_toString.c @@ -2,18 +2,20 @@ #include "../base.h" #include "../../kprint/kprint_format.h" + +// accepts char* (ptr to char) and char** (ptr to string) char* __toString_char(void* c, u32 fmt) { - //*c=char + // *c=char* + if(kp_fmt_dataFormat(fmt)==kp_s){ + return cptr_copy(*(char**)c); // to avoid segmentation fault on free() when *c allocalet on stack + } + // *c=char if(kp_fmt_dataFormat(fmt)==kp_c){ char* cc=malloc(2); cc[0]=*(char*)c; cc[1]=0; return cc; } - // *c=cstring - else if(kp_fmt_dataFormat(fmt)==kp_s){ - return cptr_copy(*(char**)c); - } else throw(ERR_FORMAT); } @@ -61,23 +63,23 @@ char* toString_u64(u64 n, bool withPostfix, bool uppercase){ return cptr_copy((char*)str+i); } -#define _toString_float_impl(bufsize, maxPrecision) {\ - char str[bufsize];\ - if(precision>maxPrecision)\ - throw("too big precision");\ - if(precision==0)\ - precision=toString_float_default_precision;\ - i32 cn=IFMSC(\ - sprintf_s(str, bufsize, "%.*f", precision, n),\ - sprintf(str, "%.*f", precision, n)\ - );\ - /* remove trailing zeroes except .0*/\ - while(str[cn-1]=='0' && str[cn-2]!='.')\ - cn--;\ - if(withPostfix)\ - str[cn++]= uppercase ? 'F' : 'f';\ - str[cn]='\0';\ - return cptr_copy(str);\ +#define _toString_float_impl(bufsize, maxPrecision) { \ + char str[bufsize]; \ + if(precision>maxPrecision) \ + throw("too big precision"); \ + if(precision==0) \ + precision=toString_float_default_precision; \ + i32 cn=IFMSC( \ + sprintf_s(str, bufsize, "%.*f", precision, n), \ + sprintf(str, "%.*f", precision, n) \ + ); \ + /* remove trailing zeroes except .0*/ \ + while(str[cn-1]=='0' && str[cn-2]!='.') \ + cn--; \ + if(withPostfix) \ + str[cn++]= uppercase ? 'F' : 'f'; \ + str[cn]='\0'; \ + return cptr_copy(str); \ } char* toString_f32(f32 n, u8 precision, bool withPostfix, bool uppercase) @@ -86,15 +88,15 @@ char* toString_f32(f32 n, u8 precision, bool withPostfix, bool uppercase) char* toString_f64(f64 n, u8 precision, bool withPostfix, bool uppercase) _toString_float_impl(512, toString_f64_max_precision) -#define byte_to_bits(byte) {\ - str[cn++]='0' + (u8)((byte>>7)&1); /* 8th bit */\ - str[cn++]='0' + (u8)((byte>>6)&1); /* 7th bit */\ - str[cn++]='0' + (u8)((byte>>5)&1); /* 6th bit */\ - str[cn++]='0' + (u8)((byte>>4)&1); /* 5th bit */\ - str[cn++]='0' + (u8)((byte>>3)&1); /* 4th bit */\ - str[cn++]='0' + (u8)((byte>>2)&1); /* 3th bit */\ - str[cn++]='0' + (u8)((byte>>1)&1); /* 2th bit */\ - str[cn++]='0' + (u8)((byte>>0)&1); /* 1th bit */\ +#define byte_to_bits(byte) { \ + str[cn++]='0' + (u8)((byte>>7)&1); /* 8th bit */ \ + str[cn++]='0' + (u8)((byte>>6)&1); /* 7th bit */ \ + str[cn++]='0' + (u8)((byte>>5)&1); /* 6th bit */ \ + str[cn++]='0' + (u8)((byte>>4)&1); /* 5th bit */ \ + str[cn++]='0' + (u8)((byte>>3)&1); /* 4th bit */ \ + str[cn++]='0' + (u8)((byte>>2)&1); /* 3th bit */ \ + str[cn++]='0' + (u8)((byte>>1)&1); /* 2th bit */ \ + str[cn++]='0' + (u8)((byte>>0)&1); /* 1th bit */ \ } char* toString_bin(void* _bytes, u32 size, bool inverse, bool withPrefix){ @@ -163,60 +165,72 @@ char* toString_hex(void* _bytes, u32 size, bool inverse, bool withPrefix, bool u } -#define __toString_i32_def(BITS) char* __toString_i##BITS(void* _n, u32 f){\ - switch(kp_fmt_dataFormat(f)){\ - case kp_i: ;\ - i##BITS n=*(i##BITS*)_n;\ - return toString_i64(n);\ - case kp_b:\ - return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));\ - case kp_h:\ - return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));\ - default:\ - kprintf("\n%u\n", kp_fmt_dataFormat(f));\ - throw(ERR_FORMAT);\ - return NULL;\ - }\ +#define __toString_i32_def(BITS) char* __toString_i##BITS(void* _n, u32 f){ \ + switch(kp_fmt_dataFormat(f)){ \ + case kp_i: ; \ + i##BITS n=*(i##BITS*)_n; \ + return toString_i64(n); \ + case kp_b: \ + return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f)); \ + case kp_h: \ + return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f)); \ + default: \ + kprintf("\n%u\n", kp_fmt_dataFormat(f)); \ + throw(ERR_FORMAT); \ + return NULL; \ + } \ } __toString_i32_def(8) __toString_i32_def(16) __toString_i32_def(32) __toString_i32_def(64) -#define __toString_u_def(BITS) char* __toString_u##BITS(void* _n, u32 f){\ - switch(kp_fmt_dataFormat(f)){\ - case kp_u: ;\ - u##BITS n=*(u##BITS*)_n;\ - return toString_u64(n, kp_fmt_withPostfix(f), kp_fmt_isUpper(f));\ - case kp_b:\ - return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));\ - case kp_h:\ - return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));\ - default:\ - kprintf("\n%u\n", kp_fmt_dataFormat(f));\ - throw(ERR_FORMAT);\ - return NULL;\ - }\ +#define __toString_u_def(BITS) char* __toString_u##BITS(void* _n, u32 f){ \ + switch(kp_fmt_dataFormat(f)){ \ + case kp_u: ; \ + u##BITS n=*(u##BITS*)_n; \ + return toString_u64(n, kp_fmt_withPostfix(f), kp_fmt_isUpper(f)); \ + case kp_b: \ + return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f)); \ + case kp_h: \ + return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f)); \ + default: \ + kprintf("\n%u\n", kp_fmt_dataFormat(f)); \ + throw(ERR_FORMAT); \ + return NULL; \ + } \ } __toString_u_def(8) __toString_u_def(16) __toString_u_def(32) -__toString_u_def(64) +// __toString_u_def(64) +char* __toString_u64(void* _n, u32 f){ + switch(kp_fmt_dataFormat(f)){ + case kp_u: ; + u64 n=*(u64*)_n; + return toString_u64(n, kp_fmt_withPostfix(f), kp_fmt_isUpper(f)); + case kp_b: + return toString_bin(_n, 64/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f)); + case kp_h: + return toString_hex(_n, 64/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f)); + default: + kprintf("\n%u\n", kp_fmt_dataFormat(f)); throw(ERR_FORMAT); return NULL; } +} -#define __toString_float_def(BITS) char* __toString_f##BITS(void* _n, u32 f){\ - switch(kp_fmt_dataFormat(f)){\ - case kp_f: ;\ - f##BITS n=*(f##BITS*)_n;\ - return toString_f64(n, toString_float_default_precision, kp_fmt_withPostfix(f), kp_fmt_isUpper(f));\ - case kp_b:\ - return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f));\ - case kp_h:\ - return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f));\ - default:\ - kprintf("\n%u\n", kp_fmt_dataFormat(f));\ - throw(ERR_FORMAT);\ - return NULL;\ - }\ +#define __toString_float_def(BITS) char* __toString_f##BITS(void* _n, u32 f){ \ + switch(kp_fmt_dataFormat(f)){ \ + case kp_f: ; \ + f##BITS n=*(f##BITS*)_n; \ + return toString_f64(n, toString_float_default_precision, kp_fmt_withPostfix(f), kp_fmt_isUpper(f)); \ + case kp_b: \ + return toString_bin(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f)); \ + case kp_h: \ + return toString_hex(_n, BITS/8, getEndian()==LittleEndian, kp_fmt_withPrefix(f), kp_fmt_isUpper(f)); \ + default: \ + kprintf("\n%u\n", kp_fmt_dataFormat(f)); \ + throw(ERR_FORMAT); \ + return NULL; \ + } \ } __toString_float_def(32) diff --git a/src/base/type_system/base_toString.h b/src/base/type_system/base_toString.h index e4ede5a..41beca4 100644 --- a/src/base/type_system/base_toString.h +++ b/src/base/type_system/base_toString.h @@ -6,8 +6,8 @@ extern "C" { #include "../errors.h" -// char and cstring -// has different output for fmtChar and fmtString +// accepts char* (ptr to char) and char** (ptr to string) +// uses format kp_s and kp_c to determine what type is argument char* __toString_char(void* c, u32 fmt); // bool diff --git a/src/base/type_system/init.c b/src/base/type_system/init.c index a5757d7..5754d46 100644 --- a/src/base/type_system/init.c +++ b/src/base/type_system/init.c @@ -4,85 +4,92 @@ #include "../../SearchTree/SearchTree.h" #include "../../Hashtable/Hashtable.h" #include "../../String/StringBuilder.h" +#include "../../Filesystem/filesystem.h" #include "base_toString.h" void ktDescriptors_initKerepTypes(){ - // null - __kt_register("Null", sizeof(NULL), NULL, NULL); - ktid_Null=ktid_last; // base types - kt_register(char, NULL, __toString_char); - kt_register(bool, NULL, __toString_bool); - kt_register(f32, NULL, __toString_f32); - kt_register(f64, NULL, __toString_f64); - kt_register(i8, NULL, __toString_i8); - kt_register(u8, NULL, __toString_u8); - kt_register(i16, NULL, __toString_i16); - kt_register(u16, NULL, __toString_u16); - kt_register(i32, NULL, __toString_i32); - kt_register(u32, NULL, __toString_u32); - kt_register(i64, NULL, __toString_i64); - kt_register(u64, NULL, __toString_u64); + kt_register(Pointer); + if(ktid_Pointer!=0) // this can break UnitypeNull + throw("ktid_Pointer!=0, you must init kerep types before any other types"); + + kt_register(char); + kt_register(bool); + kt_register(f32); + kt_register(f64); + kt_register(i8); + kt_register(u8); + kt_register(i16); + kt_register(u16); + kt_register(i32); + kt_register(u32); + kt_register(i64); + kt_register(u64); // ktDescriptor - kt_register(ktDescriptor, NULL, NULL); - + kt_register(ktDescriptor); // base type arrays - kt_register(Array_char, (freeMembers_t)Array_char_free, NULL); - kt_register(Array_bool, (freeMembers_t)Array_bool_free, NULL); - kt_register(Array_f32, (freeMembers_t)Array_f32_free, NULL); - kt_register(Array_f64, (freeMembers_t)Array_f64_free, NULL); - kt_register(Array_i8, (freeMembers_t)Array_i8_free, NULL); - kt_register(Array_u8, (freeMembers_t)Array_u8_free, NULL); - kt_register(Array_i16, (freeMembers_t)Array_i16_free, NULL); - kt_register(Array_u16, (freeMembers_t)Array_u16_free, NULL); - kt_register(Array_i32, (freeMembers_t)Array_i32_free, NULL); - kt_register(Array_u32, (freeMembers_t)Array_u32_free, NULL); - kt_register(Array_i64, (freeMembers_t)Array_i64_free, NULL); - kt_register(Array_u64, (freeMembers_t)Array_u64_free, NULL); + kt_register(Array_char); + kt_register(Array_bool); + kt_register(Array_f32); + kt_register(Array_f64); + kt_register(Array_i8); + kt_register(Array_u8); + kt_register(Array_i16); + kt_register(Array_u16); + kt_register(Array_i32); + kt_register(Array_u32); + kt_register(Array_i64); + kt_register(Array_u64); + kt_register(Array_Pointer); // base type autoarrs - kt_register(Autoarr_char, ____Autoarr_free_char, NULL); - kt_register(Autoarr_bool, ____Autoarr_free_bool, NULL); - kt_register(Autoarr_f32, ____Autoarr_free_f32, NULL); - kt_register(Autoarr_f64, ____Autoarr_free_f64, NULL); - kt_register(Autoarr_i8, ____Autoarr_free_i8, NULL); - kt_register(Autoarr_u8, ____Autoarr_free_u8, NULL); - kt_register(Autoarr_i16, ____Autoarr_free_i16, NULL); - kt_register(Autoarr_u16, ____Autoarr_free_u16, NULL); - kt_register(Autoarr_i32, ____Autoarr_free_i32, NULL); - kt_register(Autoarr_u32, ____Autoarr_free_u32, NULL); - kt_register(Autoarr_i64, ____Autoarr_free_i64, NULL); - kt_register(Autoarr_u64, ____Autoarr_free_u64, NULL); + kt_register(Autoarr_char); + kt_register(Autoarr_bool); + kt_register(Autoarr_f32); + kt_register(Autoarr_f64); + kt_register(Autoarr_i8); + kt_register(Autoarr_u8); + kt_register(Autoarr_i16); + kt_register(Autoarr_u16); + kt_register(Autoarr_i32); + kt_register(Autoarr_u32); + kt_register(Autoarr_i64); + kt_register(Autoarr_u64); + kt_register(Autoarr_Pointer); // Unitype - kt_register(Unitype, __UnitypePtr_free, NULL); - kt_register(Array_Unitype, __Array_Unitype_free_, NULL); - kt_register(Autoarr_Unitype, ____Autoarr_free_Unitype_, NULL); + kt_register(Unitype); + kt_register(Array_Unitype); + kt_register(Autoarr_Unitype); // replacing autogenerated freear() function to custom Autoarr_Unitype* _uar=Autoarr_create(Unitype, 1, 1); _uar->functions->freear=__Autoarr_free_Unitype_; Autoarr_free(_uar, true); - // SearchTreeNode - kt_register(STNode, __STNode_free, NULL); + // STNode + kt_register(STNode); // KeyValuePair - kt_register(KVPair, __KVPair_free, NULL); - kt_register(Autoarr_KVPair, ____Autoarr_free_KVPair_, NULL); + kt_register(KVPair); + kt_register(Autoarr_KVPair); // replacing autogenerated freear() function to custom Autoarr_KVPair* _kvpar=Autoarr_create(KVPair, 1, 1); _kvpar->functions->freear=__Autoarr_free_KVPair_; Autoarr_free(_kvpar, true); // Hashtable - kt_register(Hashtable, __Hashtable_free, NULL); + kt_register(Hashtable); // string - kt_register(string, NULL, NULL); - kt_register(Array_string, (freeMembers_t)Array_string_free, NULL); - kt_register(Autoarr_string, ____Autoarr_free_string, NULL); + kt_register(string); + kt_register(Array_string); + kt_register(Autoarr_string); + // StringBuilder - kt_register(StringBuilder, __StringBuilder_free, NULL); + kt_register(StringBuilder); + + //File + kt_register(File); } diff --git a/src/base/type_system/ktDescriptor.h b/src/base/type_system/ktDescriptor.h index cb34150..8b95115 100644 --- a/src/base/type_system/ktDescriptor.h +++ b/src/base/type_system/ktDescriptor.h @@ -6,16 +6,40 @@ extern "C" { #include "../std.h" #include "ktid.h" +#include "typedef_macros.h" + +#define kt_declare(TYPE)\ + ktid_declare(TYPE);\ + extern ktDescriptor ktDescriptor_##TYPE; \ + extern ktDescriptor ktDescriptor_##TYPE##_Ptr; + +#define kt_define(TYPE, FREE_FUNC, TOSTRING_FUNC)\ + ktid_define(TYPE); \ + ktDescriptor ktDescriptor_##TYPE={ \ + .name=#TYPE, \ + .id=ktid_undefined, \ + .size=sizeof(TYPE), \ + .freeMembers=FREE_FUNC, \ + .toString=TOSTRING_FUNC \ + }; \ + ktDescriptor ktDescriptor_##TYPE##_Ptr={\ + .name=#TYPE "_Ptr", \ + .id=ktid_undefined, \ + .size=sizeof(TYPE), \ + .freeMembers=FREE_FUNC, \ + .toString=TOSTRING_FUNC \ + }; typedef void (*freeMembers_t)(void*); typedef char* (*toString_t)(void* obj, u32 fmt); -typedef struct ktDescriptor{ + +STRUCT(ktDescriptor, char* name; ktid id; u16 size; freeMembers_t freeMembers; // NULL or function which frees all struct members toString_t toString; // NULL or function which generates string representaion of object -} ktDescriptor; +) #if __cplusplus } diff --git a/src/base/type_system/kt_functions.c b/src/base/type_system/kt_functions.c index fb23385..bccaf07 100644 --- a/src/base/type_system/kt_functions.c +++ b/src/base/type_system/kt_functions.c @@ -1,62 +1,57 @@ #include "../../Autoarr/Autoarr.h" +#include "type_system.h" +#include "base_toString.h" -Autoarr_declare(ktDescriptor) -Autoarr_define(ktDescriptor) +kt_define(Pointer, free, __toString_u64); +kt_define(char,NULL, __toString_char); +kt_define(bool,NULL, __toString_bool); +kt_define(f32, NULL, __toString_f32); +kt_define(f64, NULL, __toString_f64); +kt_define(i8, NULL, __toString_i8); +kt_define(u8, NULL, __toString_u8); +kt_define(i16, NULL, __toString_i16); +kt_define(u16, NULL, __toString_u16); +kt_define(i32, NULL, __toString_i32); +kt_define(u32, NULL, __toString_u32); +kt_define(i64, NULL, __toString_i64); +kt_define(u64, NULL, __toString_u64); -ktid ktid_Null=-1; +kt_define(ktDescriptor, NULL, NULL); -ktid_define(char); -ktid_define(bool); -ktid_define(f32); -ktid_define(f64); -ktid_define(i8); -ktid_define(u8); -ktid_define(i16); -ktid_define(u16); -ktid_define(i32); -ktid_define(u32); -ktid_define(i64); -ktid_define(u64); - -ktid_define(ktDescriptor); +typedef ktDescriptor* ktDescriptor_Ptr; // type descriptors are stored here during initialization -Autoarr(ktDescriptor)* __ktDescriptors=NULL; +Autoarr(Pointer)* __descriptorPointers=NULL; // here type descriptors are stored when initialization is complited -ktDescriptor* typeDescriptors=NULL; +ktDescriptor** typeDescriptors=NULL; ktid ktid_last=-1; -typedef enum{ +ENUM(ktDescriptorsState, NotInitialized, Initializing, Initialized -} ktDescriptorsState; +) ktDescriptorsState initState=NotInitialized; void ktDescriptors_beginInit(){ kprintf("\e[94mtype descriptors initializing...\n"); - __ktDescriptors=Autoarr_create(ktDescriptor, 256, 256); - if(__ktDescriptors==NULL) throw(ERR_NULLPTR); + __descriptorPointers=Autoarr_create(Pointer, 256, 256); + if(__descriptorPointers==NULL) + throw(ERR_NULLPTR); } void ktDescriptors_endInit(){ - typeDescriptors=Autoarr_toArray(__ktDescriptors); - Autoarr_free(__ktDescriptors,true); + typeDescriptors=(ktDescriptor**)Autoarr_toArray(__descriptorPointers); + Autoarr_free(__descriptorPointers,true); if(typeDescriptors==NULL) throw(ERR_NULLPTR); kprintf("\e[92minitialized %u type descriptors\n", ktid_last); } -void __kt_register(char* name, i16 size, void (*freeMembers)(void*), char* (*toString)(void*, u32)){ - ktDescriptor typeDesc={ - .name=name, - .size=size, - .id=++ktid_last, - .freeMembers=freeMembers, - .toString=toString - }; - Autoarr_add(__ktDescriptors, typeDesc); +void __kt_register(ktDescriptor* descriptor){ + descriptor->id=++ktid_last; + Autoarr_add(__descriptorPointers, descriptor); } -ktDescriptor ktDescriptor_get(ktid id){ - if(id>ktid_last) { +ktDescriptor* ktDescriptor_get(ktid id){ + if(id>ktid_last || id==ktid_undefined) { kprintf("\ntype id: %u\n",id); throw("invalid type id"); } diff --git a/src/base/type_system/kt_functions.h b/src/base/type_system/kt_functions.h index 8530745..6410d38 100644 --- a/src/base/type_system/kt_functions.h +++ b/src/base/type_system/kt_functions.h @@ -9,39 +9,38 @@ extern "C" { #include "ktDescriptor.h" extern ktid ktid_last; -void __kt_register(char* name, i16 size, void (*freeMembers)(void*), char* (*toString)(void*, u32)); +void __kt_register(ktDescriptor* descriptor); -#define kt_register(TYPE, FREE_MEMBERS_FUNC, TO_STRING_FUNC)\ - __kt_register(#TYPE, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\ - ktid_##TYPE=ktid_last;\ - __kt_register(#TYPE "*", sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\ +#define kt_register(TYPE) \ + __kt_register(&ktDescriptor_##TYPE); \ + ktid_##TYPE=ktid_last; \ + __kt_register(&ktDescriptor_##TYPE##_Ptr); \ ktid_##TYPE##_Ptr=ktid_last; void ktDescriptors_beginInit(); void ktDescriptors_endInit(); /// @param id id of registered type -ktDescriptor ktDescriptor_get(ktid id); +ktDescriptor* ktDescriptor_get(ktid id); // call it to free heap-allocated ktDescriptors array void ktDescriptors_free(); -extern ktid ktid_Null; +kt_declare(Pointer); +kt_declare(char); +kt_declare(bool); +kt_declare(f32); +kt_declare(f64); +kt_declare(i8); +kt_declare(u8); +kt_declare(i16); +kt_declare(u16); +kt_declare(i32); +kt_declare(u32); +kt_declare(i64); +kt_declare(u64); -ktid_declare(char); -ktid_declare(bool); -ktid_declare(f32); -ktid_declare(f64); -ktid_declare(i8); -ktid_declare(u8); -ktid_declare(i16); -ktid_declare(u16); -ktid_declare(i32); -ktid_declare(u32); -ktid_declare(i64); -ktid_declare(u64); - -ktid_declare(ktDescriptor); +kt_declare(ktDescriptor); #if __cplusplus } diff --git a/src/base/type_system/ktid.h b/src/base/type_system/ktid.h index 2076171..9d9c276 100644 --- a/src/base/type_system/ktid.h +++ b/src/base/type_system/ktid.h @@ -5,17 +5,20 @@ extern "C" { #endif #include "../std.h" +#include "typedef_macros.h" + typedef u16 ktid; +static const ktid ktid_undefined=-1; #define ktid_name(TYPE) ktid_##TYPE #define ktid_ptrName(TYPE) ktid_##TYPE##_Ptr -#define ktid_declare(TYPE)\ - extern ktid ktid_##TYPE;\ +#define ktid_declare(TYPE) \ + extern ktid ktid_##TYPE; \ extern ktid ktid_##TYPE##_Ptr; -#define ktid_define(TYPE)\ - ktid ktid_##TYPE=-1;\ +#define ktid_define(TYPE) \ + ktid ktid_##TYPE=-1; \ ktid ktid_##TYPE##_Ptr=-1; #if __cplusplus diff --git a/src/base/type_system/type_system.h b/src/base/type_system/type_system.h index f5e6b1a..28d80e4 100644 --- a/src/base/type_system/type_system.h +++ b/src/base/type_system/type_system.h @@ -1,5 +1,8 @@ +#pragma once + #include "init.h" #include "ktid.h" #include "ktDescriptor.h" #include "kt_functions.h" #include "unitype.h" +#include "typedef_macros.h" diff --git a/src/base/type_system/typedef_macros.h b/src/base/type_system/typedef_macros.h new file mode 100644 index 0000000..08e812a --- /dev/null +++ b/src/base/type_system/typedef_macros.h @@ -0,0 +1,14 @@ +#pragma once + +#define ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME { \ + ENUM_MEMBERS \ +} ENUM_NAME; + +#define PACKED_ENUM(ENUM_NAME, ENUM_MEMBERS...) typedef enum ENUM_NAME { \ + ENUM_MEMBERS \ +} __attribute__((__packed__)) ENUM_NAME; + +#define STRUCT(STRUCT_NAME, STRUCT_MEMBERS...) typedef struct STRUCT_NAME { \ + STRUCT_MEMBERS \ +} STRUCT_NAME; \ +kt_declare(STRUCT_NAME); diff --git a/src/base/type_system/unitype.c b/src/base/type_system/unitype.c index ca52b49..c7eae44 100644 --- a/src/base/type_system/unitype.c +++ b/src/base/type_system/unitype.c @@ -1,52 +1,90 @@ #include "../base.h" +#include "../../kprint/kprint_format.h" -ktid_define(Unitype); + +char* __Unitype_toString(void* _u, u32 fmt){ + return Unitype_toString(*(Unitype*)_u, fmt); +} + +kt_define(Unitype, __UnitypePtr_free, __Unitype_toString); void Unitype_free(Unitype u){ - ktDescriptor type=ktDescriptor_get(u.typeId); - if(type.freeMembers) - type.freeMembers(u.VoidPtr); + if(u.typeId==ktid_undefined){ + if(u.VoidPtr!=NULL) + throw("unitype with undefined typeId has value"); + return; + } + + ktDescriptor* type=ktDescriptor_get(u.typeId); + if(type->freeMembers) + type->freeMembers(u.VoidPtr); if(u.allocatedInHeap) free(u.VoidPtr); } void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); } -char* toString_Unitype(void* _u, u32 fmt){ - Unitype* u=_u; - ktDescriptor type=ktDescriptor_get(u->typeId); - char* valuestr=type.toString(_u, fmt); - char* rezult=cptr_concat("{ type: ", type.name, - ", allocated on heap: ", (u->allocatedInHeap ? "true" : "false"), + +char* Unitype_toString(Unitype u, u32 fmt){ + if(u.typeId==ktid_undefined){ + if(u.VoidPtr!=NULL) + throw("unitype with undefined typeId has value"); + return cptr_copy("{ERROR_TYPE}"); + } + + if(fmt==0) { + if(u.typeId==ktid_name(bool) || + u.typeId==ktid_name(i8) || u.typeId==ktid_name(i16) || + u.typeId==ktid_name(i32) || u.typeId==ktid_name(i64)){ + // auto format set + fmt=kp_i; + // replaces value with pointer to value to pass into toString_i64(void*, u32) + i64 value=u.Int64; + u.VoidPtr=&value; + } + else if(u.typeId==ktid_name(u8) || u.typeId==ktid_name(u16) || + u.typeId==ktid_name(u32) || u.typeId==ktid_name(u64)){ + fmt=kp_u; + u64 value=u.UInt64; + u.VoidPtr=&value; + } + else if(u.typeId==ktid_name(f32) || u.typeId==ktid_name(f64)){ + fmt=kp_f; + f64 value=u.Float64; + u.VoidPtr=&value; + } + else if(u.typeId==ktid_name(char)){ + fmt=kp_c; + i64 value=u.Int64; + u.VoidPtr=&value; + } + else if(u.typeId==ktid_ptrName(char)){ + char* value=u.VoidPtr; + u.VoidPtr=&value; + fmt=kp_s; + } + else if(u.typeId==ktid_name(Pointer)){ + if(u.VoidPtr==NULL) + return cptr_copy("{ UniNull }"); + fmt=kp_h; + } + } + + ktDescriptor* type=ktDescriptor_get(u.typeId); + char* valuestr; + if(type->toString) + valuestr=type->toString(u.VoidPtr, fmt); + else valuestr="ERR_NO_TOSTRING_FUNC"; + char* rezult=cptr_concat("{ type: ", type->name, + ", allocated on heap: ", (u.allocatedInHeap ? "true" : "false"), ", value:", valuestr, " }"); - free(valuestr); + if(type->toString) + free(valuestr); return rezult; } - - -#define BUFSIZE 64 -char* sprintuni(Unitype v){ - char* buf=malloc(BUFSIZE); - ktDescriptor type=ktDescriptor_get(v.typeId); - if(v.typeId==ktid_Null) - sprintf_s(buf, BUFSIZE, "{Null}"); - else if(v.typeId==ktid_name(f64)) - sprintf_s(buf, BUFSIZE, "{%s : %lf}", type.name,v.Float64); - else if(v.typeId==ktid_name(bool) || v.typeId==ktid_name(u64)) - sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%llu", "%lu") "}", type.name,v.UInt64); - else if(v.typeId==ktid_name(i64)) - sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%lld", "%ld") "}", type.name,v.Int64); - else if(v.typeId==ktid_ptrName(char)){ - size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2; - buf=realloc(buf, newBUFSIZE); - sprintf_s(buf, BUFSIZE, "{%s : \"%s\"}", type.name,(char*)v.VoidPtr); - } - else sprintf_s(buf, BUFSIZE, "{%s : %p}", type.name,v.VoidPtr); - return buf; -} - void printuni(Unitype v){ - char* s=sprintuni(v); + char* s=Unitype_toString(v,0); fputs(s, stdout); + fputc('\n',stdout); free(s); } \ No newline at end of file diff --git a/src/base/type_system/unitype.h b/src/base/type_system/unitype.h index d73859d..3d617fe 100644 --- a/src/base/type_system/unitype.h +++ b/src/base/type_system/unitype.h @@ -5,8 +5,9 @@ extern "C" { #endif #include "ktid.h" +#include "typedef_macros.h" -typedef struct Unitype{ +STRUCT(Unitype, union { i64 Int64; u64 UInt64; @@ -17,11 +18,10 @@ typedef struct Unitype{ }; ktid typeId; bool allocatedInHeap; // should Unitype_free call free() to VoidPtr* -} Unitype; -ktid_declare(Unitype); +) -#define __UniDef(FIELD, TYPE, VAL) (Unitype){\ +#define __UniDef(FIELD, TYPE, VAL) (Unitype){ \ .FIELD=VAL, .typeId=ktid_name(TYPE), .allocatedInHeap=false} #define UniInt64(VAL) __UniDef(Int64, i64, VAL) @@ -29,20 +29,23 @@ ktid_declare(Unitype); #define UniFloat64(VAL) __UniDef(Float64, f64, VAL) #define UniBool(VAL) __UniDef(Bool, bool, VAL) -#define UniStackPtr(TYPE, VAL) (Unitype){\ +#define UniStackPtr(TYPE, VAL) (Unitype){ \ .VoidPtr=VAL, .typeId=ktid_ptrName(TYPE), .allocatedInHeap=false} -#define UniHeapPtr(TYPE, VAL) (Unitype){\ +#define UniHeapPtr(TYPE, VAL) (Unitype){ \ .VoidPtr=VAL, .typeId=ktid_ptrName(TYPE), .allocatedInHeap=true} -#define UniNull (Unitype){.Int64=0, .typeId=ktid_Null, .allocatedInHeap=false} + // 0==ktid_Pointer +#define UniNull (Unitype){.Int64=0, .typeId=0, .allocatedInHeap=false} #define UniTrue UniBool(true) #define UniFalse UniBool(false) +#define Unitype_isUniNull(UNI) (UNI.typeId==ktid_Pointer && UNI.VoidPtr==NULL) + // frees VoidPtr value or does nothing if type isn't pointer void Unitype_free(Unitype u); void __UnitypePtr_free(void* u); +char* Unitype_toString(Unitype v, u32 fmt); void printuni(Unitype v); -char* sprintuni(Unitype v); #if __cplusplus } diff --git a/src/kprint/README.md b/src/kprint/README.md index 98464ef..1bdf1d2 100644 --- a/src/kprint/README.md +++ b/src/kprint/README.md @@ -47,4 +47,4 @@ I don't really like printf function (and its variants), so i made safer and more Maybe m=MaybeNull; kprint(kp_fgBlue|kp_s, "Maybe: ", kp_fgGreen|ktid_MaybePtr, &m); ``` - output: Maybe: {value={0, ktid_Null}} + output: Maybe: {value: { Pointer, 0x0 }} diff --git a/src/kprint/kprint.c b/src/kprint/kprint.c index 809de58..351406d 100644 --- a/src/kprint/kprint.c +++ b/src/kprint/kprint.c @@ -15,7 +15,7 @@ ktid __typeFromFormat(kp_fmt f){ case kp_f: return ktid_name(f64); case kp_c: - return ktid_char; + return ktid_name(char); case kp_s: return ktid_ptrName(char); default: @@ -26,12 +26,12 @@ ktid __typeFromFormat(kp_fmt f){ Maybe __next_toString(kp_fmt f, __kp_value_union* object){ // detecting type ktid typeId=__typeFromFormat(f); - if(typeId==-1) - safethrow("typeId is not set, can't autodetect type",;); - ktDescriptor typeDesc=ktDescriptor_get(typeId); - if(!typeDesc.toString) + if(typeId==ktid_undefined) + safethrow("typeId is undefined, can't autodetect type",;); + ktDescriptor* type=ktDescriptor_get(typeId); + if(!type->toString) safethrow("type descriptor doesnt have toString() func",;); - return SUCCESS(UniHeapPtr(char, typeDesc.toString(object, f))); + return SUCCESS(UniHeapPtr(char, type->toString(object, f))); } Maybe check_argsN(u8 n){ @@ -73,7 +73,7 @@ void __kprint(u8 n, kp_fmt* formats, __kp_value_union* objects){ kp_fmt fmt=formats[i]; kprint_setColor(fmt); tryLast(__next_toString(fmt, &objects[i]),maybeStr); - if(fputs(maybeStr.value.VoidPtr, stdout)==EOF)\ + if(fputs(maybeStr.value.VoidPtr, stdout)==EOF) \ throw("can't write string to stdout"); //, Unitype_free(maybeStr.value) Unitype_free(maybeStr.value); @@ -160,14 +160,14 @@ void kprint_setColor(kp_fmt f){ #endif /* Maybe ksprint_ar(u32 count, kp_fmt format, ktid typeId, void* array){ - ktDescriptor typeDesc=ktDescriptor_get(format.typeId); - if(!typeDesc.toString) + ktDescriptor* type=ktDescriptor_get(format.typeId); + if(!type->toString) safethrow("type descriptor doesnt have toString() func",;); StringBuilder* strb=StringBuilder_create(); StringBuilder_append_char(strb, '['); for (u16 e=1; etoString(array+type->size*e, &format); StringBuilder_append_cptr(strb, elStr); StringBuilder_append_char(strb, ','); } diff --git a/src/kprint/kprint.h b/src/kprint/kprint.h index 1efe155..aa591e7 100644 --- a/src/kprint/kprint.h +++ b/src/kprint/kprint.h @@ -29,45 +29,45 @@ static inline __kp_value_union __kpVU_i(i64 f) { return (__kp_value_union){ .i64 #define __kpVU(V) __kpVU_selectType(V) -#define __kp_argsToFmts8(\ - a0, a1, a2, a3, a4, a5, a6, a7,...)\ +#define __kp_argsToFmts8( \ + a0, a1, a2, a3, a4, a5, a6, a7,...) \ ((i32[]){ a0,a2,a4,a6 }) -#define __kp_argsToObjs8(\ - a0, a1, a2, a3, a4, a5, a6, a7,...)\ +#define __kp_argsToObjs8( \ + a0, a1, a2, a3, a4, a5, a6, a7,...) \ ((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7) }) -#define __kp_argsToFmts16(\ - a0, a1, a2, a3, a4, a5, a6, a7,\ - a8, a9, a10,a11,a12,a13,a14,a15,...)\ +#define __kp_argsToFmts16( \ + a0, a1, a2, a3, a4, a5, a6, a7, \ + a8, a9, a10,a11,a12,a13,a14,a15,...) \ ((i32[]){ a0,a2,a4,a6,a8,a10,a12,a14 }) -#define __kp_argsToObjs16(\ - a0, a1, a2, a3, a4, a5, a6, a7,\ - a8, a9, a10,a11,a12,a13,a14,a15,...)\ +#define __kp_argsToObjs16( \ + a0, a1, a2, a3, a4, a5, a6, a7, \ + a8, a9, a10,a11,a12,a13,a14,a15,...) \ ((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7),__kpVU(a9),__kpVU(a11),__kpVU(a13),__kpVU(a15) }) -#define __kp_argsToFmts32(\ - a0, a1, a2, a3, a4, a5, a6, a7,\ - a8, a9, a10,a11,a12,a13,a14,a15,\ - a16,a17,a18,a19,a20,a21,a22,a23,\ - a24,a25,a26,a27,a28,a29,a30,a31,...)\ +#define __kp_argsToFmts32( \ + a0, a1, a2, a3, a4, a5, a6, a7, \ + a8, a9, a10,a11,a12,a13,a14,a15, \ + a16,a17,a18,a19,a20,a21,a22,a23, \ + a24,a25,a26,a27,a28,a29,a30,a31,...) \ ((i32[]){ a0,a2,a4,a6,a8,a10,a12,a14,a16,a18,a20,a22,a24,a26,a28,a30 }) -#define __kp_argsToObjs32(\ - a0, a1, a2, a3, a4, a5, a6, a7,\ - a8, a9, a10,a11,a12,a13,a14,a15,\ - a16,a17,a18,a19,a20,a21,a22,a23,\ - a24,a25,a26,a27,a28,a29,a30,a31,...)\ +#define __kp_argsToObjs32( \ + a0, a1, a2, a3, a4, a5, a6, a7, \ + a8, a9, a10,a11,a12,a13,a14,a15, \ + a16,a17,a18,a19,a20,a21,a22,a23, \ + a24,a25,a26,a27,a28,a29,a30,a31,...) \ ((__kp_value_union[]){ __kpVU(a1),__kpVU(a3),__kpVU(a5),__kpVU(a7),__kpVU(a9),__kpVU(a11),__kpVU(a13),__kpVU(a15),__kpVU(a17),__kpVU(a19),__kpVU(a21),__kpVU(a23),__kpVU(a25),__kpVU(a27),__kpVU(a29),__kpVU(a31) }) #define __32zeroes 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 -#define __kp_argsToArrs(COUNT,ARGS...)\ - (kp_fmt*)(\ - COUNT<=8 ? __kp_argsToFmts8(ARGS) :\ - COUNT<=16 ? __kp_argsToFmts16(ARGS) :\ - __kp_argsToFmts32(ARGS)),\ - (__kp_value_union*)(\ - COUNT<=8 ? __kp_argsToObjs8(ARGS) :\ - COUNT<=16 ? __kp_argsToObjs16(ARGS) :\ +#define __kp_argsToArrs(COUNT,ARGS...) \ + (kp_fmt*)( \ + COUNT<=8 ? __kp_argsToFmts8(ARGS) : \ + COUNT<=16 ? __kp_argsToFmts16(ARGS) : \ + __kp_argsToFmts32(ARGS)), \ + (__kp_value_union*)( \ + COUNT<=8 ? __kp_argsToObjs8(ARGS) : \ + COUNT<=16 ? __kp_argsToObjs16(ARGS) : \ __kp_argsToObjs32(ARGS)) @@ -75,8 +75,8 @@ Maybe __ksprint(u8 n, kp_fmt* formats, __kp_value_union* objects); /// @param ARGS kp_fmt, value, kp_fmt, value... ///@returns Maybe -#define ksprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION,\ - __ksprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes))\ +#define ksprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \ + __ksprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \ ) /*-Wint-conversion warning was produced during value to __kp_value_union conversion*/ @@ -85,8 +85,8 @@ Maybe __kfprint(FILE* fd, u8 n, kp_fmt* formats, __kp_value_union* objects); /// @param FD FILE* /// @param ARGS kp_fmt, value, kp_fmt, value... ///@returns Maybe -#define kfprint(FD, ARGS...) WARNING_DISABLE( W_INT_CONVERSION,\ - __kfprint(FD, count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes))\ +#define kfprint(FD, ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \ + __kfprint(FD, count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \ ) void __kprint(u8 n, kp_fmt* formats, __kp_value_union* objects); @@ -94,8 +94,8 @@ void __kprint(u8 n, kp_fmt* formats, __kp_value_union* objects); ///can use non-catchable throw !!! ///@param ARGS kp_fmt, value, kp_fmt, value... ///@returns void -#define kprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION,\ - __kprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes))\ +#define kprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \ + __kprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \ ) ///@param f bgColor | fgColor diff --git a/src/kprint/kprint_colors.h b/src/kprint/kprint_colors.h index 5fc26f3..18c42b8 100644 --- a/src/kprint/kprint_colors.h +++ b/src/kprint/kprint_colors.h @@ -8,7 +8,7 @@ extern "C" { // ^ ^^^^ // | color num // fgColorSet flag -PACK_ENUM(kp_fgColor, +PACKED_ENUM(kp_fgColor, /// black foreground kp_fgBlack = 0x80000000, /// dark red foreground @@ -46,7 +46,7 @@ PACK_ENUM(kp_fgColor, // 01000000 00000000 00000000 00000000 // ^ ^^^^ // bgColorSet flag color num -PACK_ENUM(kp_bgColor, +PACKED_ENUM(kp_bgColor, /// black background kp_bgBlack = 0x40000000, /// dark red background diff --git a/src/kprint/kprint_format.h b/src/kprint/kprint_format.h index 2c8113d..cd25f92 100644 --- a/src/kprint/kprint_format.h +++ b/src/kprint/kprint_format.h @@ -10,7 +10,7 @@ extern "C" { /// kprint_format typedef u32 kp_fmt; -PACK_ENUM(kp_dataFmt, +PACKED_ENUM(kp_dataFmt, // 00000000 00000000 00000000 00000000 // ^^^^ // type diff --git a/tests/test_hash_functions.c b/tests/test_hash_functions.c index 4ccb244..7ca4fa3 100644 --- a/tests/test_hash_functions.c +++ b/tests/test_hash_functions.c @@ -8,33 +8,33 @@ char data[]="iojihiojopijiugbjmoihftytryfdrh"; -#define test_hashfunc(hasht, hashf)({\ - kprintf("\e[94mfunction: \e[92m" #hashf "\n");\ - hasht h=0;\ - optime("speed test", 1, ({\ - for(u32 i=0; iid, type->name, type->size, type->freeMembers, type->toString); } } From 590790817bdab75e0691b2c349879bff704c5a77 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Mon, 13 Feb 2023 19:26:17 +0600 Subject: [PATCH 14/55] segfault fixed --- .gitignore | 1 + .vscode/tasks.json | 2 +- Makefile | 77 +++++++++++++---- cbuild | 2 +- default.config | 116 +++++++++++++++++--------- src/Autoarr/Autoarr_Unitype.c | 16 ++-- src/Autoarr/Autoarr_Unitype.h | 4 +- src/Autoarr/Autoarr_declare.h | 22 ++--- src/Autoarr/Autoarr_define.h | 38 ++++----- src/DtsodParser/DtsodV24.c | 4 +- src/Hashtable/Hashtable.c | 9 +- src/Hashtable/Hashtable.h | 2 +- src/Hashtable/KeyValuePair.c | 12 +-- src/Hashtable/KeyValuePair.h | 8 +- src/String/StringBuilder.c | 2 +- src/base/type_system/base_toString.c | 5 +- src/base/type_system/base_toString.h | 2 +- src/base/type_system/init.c | 18 ++-- src/base/type_system/unitype.c | 118 +++++++++++++++------------ src/kprint/kprint.c | 8 +- tests/test_dtsod.c | 22 +++-- 21 files changed, 302 insertions(+), 186 deletions(-) diff --git a/.gitignore b/.gitignore index cab1d1a..a48562e 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,5 @@ obj/ .editorconfig *.user *.vcxproj.filters +*.log current.config diff --git a/.vscode/tasks.json b/.vscode/tasks.json index f24315b..4494a57 100644 --- a/.vscode/tasks.json +++ b/.vscode/tasks.json @@ -24,7 +24,7 @@ "focus": true, "panel": "shared", "showReuseMessage": false, - "clear": true + "clean": true } } ] diff --git a/Makefile b/Makefile index 82fdff2..3a38c5c 100644 --- a/Makefile +++ b/Makefile @@ -1,25 +1,68 @@ -###### Build cbuild/default_tasks ####### -all: build_exec +###################################### +###### Build tasks ####### +###################################### -build_exec: - @cbuild/call_task.sh build_exec -build_exec_dbg: - @cbuild/call_task.sh build_exec_dbg +all: build_exec_dbg -build_shared_lib: - @cbuild/call_task.sh build_shared_lib -build_shared_lib_dbg: - @cbuild/call_task.sh build_shared_lib_dbg +# generates different profile info +build_profile: + @cbuild/call_task.sh build_profile 2>&1 | tee make_raw.log -build_static_lib: - @cbuild/call_task.sh build_static_lib -build_static_lib_dbg: - @cbuild/call_task.sh build_static_lib_dbg +# creates executable using profile info generated by build_profile +build_exec: build_profile + @cbuild/call_task.sh build_exec 2>&1 | tee -a make_raw.log -###### Launch cbuild/default_tasks ####### +# creates executable with debug info and no optimizations +build_exec_dbg: + @cbuild/call_task.sh build_exec_dbg 2>&1 | tee make_raw.log + +# creates shared library +build_shared_lib: + @cbuild/call_task.sh build_shared_lib 2>&1 | tee make_raw.log + +# creates shared library with debug symbols and no optimizations +build_shared_lib_dbg: + @cbuild/call_task.sh build_shared_lib_dbg 2>&1 | tee make_raw.log + +# creates static library +build_static_lib: + @cbuild/call_task.sh build_static_lib 2>&1 | tee make_raw.log + +# creates static library with debug symbols and no optimizations +build_static_lib_dbg: + @cbuild/call_task.sh build_static_lib_dbg 2>&1 | tee make_raw.log + +###################################### +###### Launch tasks ####### +###################################### + +# executes $EXEC_FILE exec: build_exec - @cbuild/call_task.sh exec + @cbuild/call_task.sh exec 2>&1 | tee -a make_raw.log +# executes $EXEC_FILE +exec_dbg: build_exec_dbg + @cbuild/call_task.sh exec_dbg 2>&1 | tee -a make_raw.log + +# executes $EXEC_FILE with valgrind memory checker valgrind: build_exec_dbg - @cbuild/call_task.sh valgrind + @cbuild/call_task.sh valgrind 2>&1 | tee -a make_raw.log +###################################### +###### Other tasks ####### +###################################### + +# deletes generated files +clean: + @cbuild/call_task.sh clean 2>&1 | tee make_raw.log + +# removes all unreadable characters copied from stdio +fix_log: + sed 's/[^[:blank:][:print:]]//g' make_raw.log \ + | sed 's/\[0;[0-9][0-9]m//g' \ + | sed 's/\[0;[0-9]m//g' \ + | sed 's/\[[0-9][0-9]m//g' \ + | sed 's/\[[0-9]m//g' \ + | sed 's/ H //g' \ + | sed 's/\[3gH //g' \ + > make_fixed.log diff --git a/cbuild b/cbuild index 1b38b43..a0cdbf5 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit 1b38b43c547246ca358a676cee5eda85598ab949 +Subproject commit a0cdbf5522de6d4803eb135f2f52bd6cac5a3b8a diff --git a/default.config b/default.config index 3719daa..31e6241 100644 --- a/default.config +++ b/default.config @@ -1,12 +1,12 @@ #!/bin/bash -CBUILD_VERSION=4 -CONFIG_VERSION=4 +CBUILD_VERSION=5 +CONFIG_VERSION=5 -PROJECT=kerep -CMP_C=gcc -CMP_CPP=g++ -STD_C=c11 -STD_CPP=c++17 +PROJECT="kerep" +CMP_C="gcc" +CMP_CPP="g++" +STD_C="c11" +STD_CPP="c++17" WARN_C="-Wall -Wno-discarded-qualifiers" WARN_CPP="-Wall" SRC_C="$( find src -name '*.c')" @@ -14,75 +14,117 @@ SRC_CPP="$( find src -name '*.cpp')" TESTS_C="$( find tests -name '*.c')" TESTS_CPP="$(find tests -name '*.cpp')" -OUTDIR=bin -OBJDIR=obj -EXEC_FILE=$PROJECT.com -SHARED_LIB_FILE=$PROJECT.so -STATIC_LIB_FILE=$PROJECT.a +# OBJDIR structure: +# ├── objects - dir where compiled *.o files are stored. cleans every call of build task +# ├── profile - dir where gcc *.gcda profiling info files stored +# ├── libs - there you can put static libs and linker will find them +# └── out - output files are created here and then copied to OUTDIR +OBJDIR="obj" +OUTDIR="bin" +STATIC_LIB_FILE="$PROJECT.a" -case $TASK in - build_exec) - C_ARGS="-O2" +# OS-specific options +case "$OS" in + WINDOWS) + EXEC_FILE="$PROJECT.exe" + SHARED_LIB_FILE="$PROJECT.dll" + ;; + LINUX) + EXEC_FILE="$PROJECT.P" + SHARED_LIB_FILE="$PROJECT.so" + ;; + *) + error "operating system $OS has no configuration variants" + ;; +esac + +# TASKS +case "$TASK" in + # generates different profile info + build_profile) + OUTDIR="$OUTDIR/profile" + # -flto applies more optimizations across object files + # -flto=auto is needed to multithreaded copilation + # -fuse-linker-plugin is required to use static libs with lto, it strips away all + # -pg adds code to executable, that generates file containing function call info (gmon.out) + # -fprofile-generate + C_ARGS="-O2 -flto=auto -fuse-linker-plugin -pg -fprofile-generate -fprofile-prefix-path=$(realpath $OBJDIR)/objects" CPP_ARGS="$C_ARGS" - LINKER_ARGS="" - TASK_SCRIPT=cbuild/default_tasks/build_exec.sh - PRE_TASK_SCRIPT= + LINKER_ARGS="$CPP_ARGS" + PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + TASK_SCRIPT=cbuild/default_tasks/profile.sh POST_TASK_SCRIPT= ;; + # creates executable using profile info generated by build_profile + build_exec) + # -flto applies more optimizations across object files + # -flto=auto is needed to multithreaded copilation + # -fuse-linker-plugin is required to use static libs with lto, it strips away all + C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="$CPP_ARGS" + PRE_TASK_SCRIPT= + TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + POST_TASK_SCRIPT= + ;; + # creates executable with debug info and no optimizations build_exec_dbg) C_ARGS="-O0 -g" CPP_ARGS="$C_ARGS" - LINKER_ARGS="" - TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + LINKER_ARGS="$CPP_ARGS" PRE_TASK_SCRIPT= + TASK_SCRIPT=cbuild/default_tasks/build_exec.sh POST_TASK_SCRIPT= ;; + # creates shared library build_shared_lib) C_ARGS="-O2 -fpic -flto -shared" CPP_ARGS="$C_ARGS" - LINKER_ARGS="-Wl,-soname,$SHARED_LIB_FILE" - TASK_SCRIPT=cbuild/default_tasks/build_shared_lib.sh + LINKER_ARGS="$CPP_ARGS -Wl,-soname,$SHARED_LIB_FILE" PRE_TASK_SCRIPT= + TASK_SCRIPT=cbuild/default_tasks/build_shared_lib.sh POST_TASK_SCRIPT= ;; + # creates shared library with debug symbols and no optimizations build_shared_lib_dbg) C_ARGS="-O0 -g -fpic -shared" CPP_ARGS="$C_ARGS" - LINKER_ARGS="-Wl,-soname,$SHARED_LIB_FILE" - TASK_SCRIPT=cbuild/default_tasks/build_shared_lib.sh + LINKER_ARGS="$CPP_ARGS -Wl,-soname,$SHARED_LIB_FILE" PRE_TASK_SCRIPT= + TASK_SCRIPT=cbuild/default_tasks/build_shared_lib.sh POST_TASK_SCRIPT= ;; + # creates static library build_static_lib) C_ARGS="-O2 -fpic" - CPP_ARGS="$C_ARGS" - TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh + CPP_ARGS="$C_ARGS" PRE_TASK_SCRIPT= + TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh POST_TASK_SCRIPT= ;; + # creates static library with debug symbols and no optimizations build_static_lib_dbg) C_ARGS="-O0 -g" - CPP_ARGS="$C_ARGS" - TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh + CPP_ARGS="$C_ARGS" PRE_TASK_SCRIPT= + TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh POST_TASK_SCRIPT= ;; + # executes $EXEC_FILE exec) TASK_SCRIPT=cbuild/default_tasks/exec.sh ;; + # executes $EXEC_FILE with valgrind memory checker valgrind) VALGRIND_ARGS="-s --log-file=valgrind.log --read-var-info=yes --track-origins=yes --fullpath-after=$PROJECT/ --leak-check=full --show-leak-kinds=all" TASK_SCRIPT=cbuild/default_tasks/valgrind.sh ;; -esac - -case $OS in - WINDOWS) - ;; - LINUX) + # deletes generated files + clean) + TASK_SCRIPT=cbuild/default_tasks/clean.sh ;; + # unknown task *) - printf "${RED}operating system $OS has no configuration variants\n" - exit 1 - ;; + error "task <$TASK> not found" + ;; esac diff --git a/src/Autoarr/Autoarr_Unitype.c b/src/Autoarr/Autoarr_Unitype.c index 98ece1c..c16e531 100644 --- a/src/Autoarr/Autoarr_Unitype.c +++ b/src/Autoarr/Autoarr_Unitype.c @@ -1,12 +1,16 @@ #include "Autoarr.h" -Autoarr_define(Unitype); +Autoarr_define(Unitype) -// right func to clear array of unitype values -void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr){ +u32 free_calls=0; + +// right func to clean array of unitype values +void __Autoarr_Unitype_free_fixed(Autoarr(Unitype)* ar, bool freePtr){ Autoarr_foreach(ar, u,Unitype_free(u)); - __Autoarr_free_Unitype(ar, freePtr); + __Autoarr_Unitype_free_g(ar, freePtr); + free_calls++; + kprintf("free_calls: %u\n", free_calls); } -void ____Autoarr_free_Unitype_(void* ar) { - __Autoarr_free_Unitype_((Autoarr(Unitype)*)ar, false); +void ____Autoarr_Unitype_free_fixed(void* ar) { + __Autoarr_Unitype_free_fixed((Autoarr(Unitype)*)ar, false); } diff --git a/src/Autoarr/Autoarr_Unitype.h b/src/Autoarr/Autoarr_Unitype.h index 645f4e3..b58bdfa 100644 --- a/src/Autoarr/Autoarr_Unitype.h +++ b/src/Autoarr/Autoarr_Unitype.h @@ -10,8 +10,8 @@ extern "C" { Autoarr_declare(Unitype) // this function is injected in kerep_init() -void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr); -void ____Autoarr_free_Unitype_(void* ar); +void __Autoarr_Unitype_free_fixed(Autoarr(Unitype)* ar, bool freePtr); +void ____Autoarr_Unitype_free_fixed(void* ar); #define Autoarr_foreach(ar,elem,codeblock)({ \ if(ar->blocks_count>0) { \ diff --git a/src/Autoarr/Autoarr_declare.h b/src/Autoarr/Autoarr_declare.h index 2f01431..e209115 100644 --- a/src/Autoarr/Autoarr_declare.h +++ b/src/Autoarr/Autoarr_declare.h @@ -10,14 +10,16 @@ extern "C" { \ struct Autoarr_##type; \ \ -STRUCT(__functions_list_t_##type, \ +typedef struct __Autoarr_##type##_functions_list_t { \ void (*add)(struct Autoarr_##type* ar, type element); \ type (*get)(struct Autoarr_##type* ar, u32 index); \ - type* (*getptr)(struct Autoarr_##type* ar, u32 index); \ + type* (*getPtr)(struct Autoarr_##type* ar, u32 index); \ void (*set)(struct Autoarr_##type* ar, u32 index, type element); \ void (*freear)(struct Autoarr_##type* ar, bool freePtr); \ type* (*toArray)(struct Autoarr_##type* ar); \ -) \ +} __Autoarr_##type##_functions_list_t; \ +\ +extern __Autoarr_##type##_functions_list_t __Autoarr_##type##_functions_list; \ \ STRUCT(Autoarr_##type, \ u16 blocks_count; \ @@ -25,23 +27,23 @@ STRUCT(Autoarr_##type, \ u16 block_length; \ u16 max_block_length; \ type** values; \ - __functions_list_t_##type* functions; \ + __Autoarr_##type##_functions_list_t* functions; \ ) \ \ -Autoarr_##type* __Autoarr_create_##type(u16 max_blocks_count, u16 max_block_length); \ -void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr); \ -void ____Autoarr_free_##type(void* ar); +Autoarr_##type* __Autoarr_##type##_create(u16 max_blocks_count, u16 max_block_length); \ +void __Autoarr_##type##_free_g(Autoarr_##type* ar, bool freePtr); \ +void ____Autoarr_##type##_free_g(void* ar); #define Autoarr(type) Autoarr_##type #define Autoarr_create(type, max_blocks_count, max_block_length) \ - __Autoarr_create_##type(max_blocks_count, max_block_length) + __Autoarr_##type##_create(max_blocks_count, max_block_length) #define Autoarr_add(autoarr, element) \ autoarr->functions->add(autoarr, element) #define Autoarr_get(autoarr, index) \ autoarr->functions->get(autoarr,index) -#define Autoarr_getptr(autoarr, index) \ - autoarr->functions->getptr(autoarr,index) +#define Autoarr_getPtr(autoarr, index) \ + autoarr->functions->getPtr(autoarr,index) #define Autoarr_set(autoarr, index, element) \ autoarr->functions->set(autoarr, index, element) #define Autoarr_free(autoarr, freePtr) \ diff --git a/src/Autoarr/Autoarr_define.h b/src/Autoarr/Autoarr_define.h index d37c2bc..94b4747 100644 --- a/src/Autoarr/Autoarr_define.h +++ b/src/Autoarr/Autoarr_define.h @@ -8,9 +8,9 @@ extern "C" { #define Autoarr_define(type) \ \ -kt_define(Autoarr_##type, ____Autoarr_free_##type, NULL); \ +kt_define(Autoarr_##type, ____Autoarr_##type##_free_g, NULL); \ \ -void __Autoarr_add_##type(Autoarr_##type* ar, type element){ \ +void __Autoarr_##type##_add(Autoarr_##type* ar, type element){ \ if(!ar->values){ \ ar->values=malloc(ar->max_blocks_count*sizeof(type*)); \ goto create_block; \ @@ -26,49 +26,49 @@ create_block: \ ar->block_length++; \ } \ \ -type __Autoarr_get_##type(Autoarr_##type* ar, u32 index){ \ +type __Autoarr_##type##_get(Autoarr_##type* ar, u32 index){ \ if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \ return ar->values[index/ar->max_block_length][index%ar->max_block_length]; \ } \ \ -type* __Autoarr_getptr_##type(Autoarr_##type* ar, u32 index){ \ +type* __Autoarr_##type##_getPtr(Autoarr_##type* ar, u32 index){ \ if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \ return ar->values[index/ar->max_block_length]+(index%ar->max_block_length); \ } \ \ -void __Autoarr_set_##type(Autoarr_##type* ar, u32 index, type element){ \ +void __Autoarr_##type##_set(Autoarr_##type* ar, u32 index, type element){ \ if(index>=Autoarr_length(ar)) throw(ERR_WRONGINDEX); \ ar->values[index/ar->max_block_length][index%ar->max_block_length]=element; \ } \ \ -void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr){ \ +void __Autoarr_##type##_free_g(Autoarr_##type* ar, bool freePtr){ \ for(u16 i=0; iblocks_count;i++) \ free(ar->values[i]); \ free(ar->values); \ if(freePtr) free(ar); \ } \ -void ____Autoarr_free_##type(void* ar){ \ - __Autoarr_free_##type((Autoarr_##type*)ar, false); \ +void ____Autoarr_##type##_free_g(void* ar){ \ + __Autoarr_##type##_free_g((Autoarr_##type*)ar, false); \ } \ \ -type* __Autoarr_toArray_##type(Autoarr_##type* ar){ \ +type* __Autoarr_##type##_toArray(Autoarr_##type* ar){ \ u32 length=Autoarr_length(ar); \ type* array=malloc(length * sizeof(type)); \ for(u32 i=0; irows); @@ -72,11 +73,11 @@ void Hashtable_add(Hashtable* ht, char* key, Unitype u){ } // returns null or pointer to value in hashtable -Unitype* Hashtable_getptr(Hashtable* ht, char* key){ +Unitype* Hashtable_getPtr(Hashtable* ht, char* key){ Autoarr(KVPair)* ar=getrow(ht,key,false); u32 arlen=Autoarr_length(ar); for(u32 i=0;ikey)) return &p->value; } return NULL; @@ -99,7 +100,7 @@ bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output){ } void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){ - Unitype* val=Hashtable_getptr(ht, key); + Unitype* val=Hashtable_getPtr(ht, key); if(val) *val=u; else Hashtable_add(ht, key, u); } diff --git a/src/Hashtable/Hashtable.h b/src/Hashtable/Hashtable.h index 00390b9..ba7755f 100644 --- a/src/Hashtable/Hashtable.h +++ b/src/Hashtable/Hashtable.h @@ -28,7 +28,7 @@ void Hashtable_add(Hashtable* ht, char* key, Unitype u); void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u); // returns null or pointer to value in hashtable -Unitype* Hashtable_getptr(Hashtable* ht, char* key); +Unitype* Hashtable_getPtr(Hashtable* ht, char* key); Unitype Hashtable_get(Hashtable* ht, char* key); bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output); diff --git a/src/Hashtable/KeyValuePair.c b/src/Hashtable/KeyValuePair.c index c3e4d85..724f490 100644 --- a/src/Hashtable/KeyValuePair.c +++ b/src/Hashtable/KeyValuePair.c @@ -4,20 +4,20 @@ kt_define(KVPair, __KVPair_free, NULL); Autoarr_define(KVPair) -// proper way to clear a KVP +// proper way to clean a KVP void KVPair_free(KVPair p){ free(p.key); Unitype_free(p.value); } void __KVPair_free(void* p){ KVPair_free(*(KVPair*)p); } -// func for KVP array clearing -void __Autoarr_free_KVPair_(Autoarr_KVPair* ar, bool freePtr){ +// func for KVP array cleaning +void __Autoarr_KVPair_free_fixed(Autoarr_KVPair* ar, bool freePtr){ Autoarr_foreach(ar,k,KVPair_free(k)); - __Autoarr_free_KVPair(ar, freePtr); + __Autoarr_KVPair_free_g(ar, freePtr); } -void ____Autoarr_free_KVPair_(void* ar){ - __Autoarr_free_KVPair_((Autoarr_KVPair*)ar, false); +void ____Autoarr_KVPair_free_fixed(void* ar){ + __Autoarr_KVPair_free_fixed((Autoarr_KVPair*)ar, false); } void printkvp(KVPair p){ diff --git a/src/Hashtable/KeyValuePair.h b/src/Hashtable/KeyValuePair.h index b29e4bb..2d3fb32 100644 --- a/src/Hashtable/KeyValuePair.h +++ b/src/Hashtable/KeyValuePair.h @@ -14,13 +14,13 @@ STRUCT(KVPair, Autoarr_declare(KVPair) -// proper way to clear a KVP +// proper way to clean a KVP void KVPair_free(KVPair p); void __KVPair_free(void* p); -// func to clear KVP array -void __Autoarr_free_KVPair_(Autoarr_KVPair* ar, bool freePtr); -void ____Autoarr_free_KVPair_(void* ar); +// func to clean KVP array +void __Autoarr_KVPair_free_fixed(Autoarr_KVPair* ar, bool freePtr); +void ____Autoarr_KVPair_free_fixed(void* ar); void printkvp(KVPair p); diff --git a/src/String/StringBuilder.c b/src/String/StringBuilder.c index 82a6bbf..ead636b 100644 --- a/src/String/StringBuilder.c +++ b/src/String/StringBuilder.c @@ -68,7 +68,7 @@ void StringBuilder_rmchar(StringBuilder* b){ Autoarr_pop(b->curr_buf) else { if(!b->compl_bufs) throw(ERR_NULLPTR); - string* lastcb=Autoarr_getptr(b->compl_bufs, (Autoarr_length(b->compl_bufs)-1)); + string* lastcb=Autoarr_getPtr(b->compl_bufs, (Autoarr_length(b->compl_bufs)-1)); lastcb->length--; } } diff --git a/src/base/type_system/base_toString.c b/src/base/type_system/base_toString.c index 56b0291..2fc2371 100644 --- a/src/base/type_system/base_toString.c +++ b/src/base/type_system/base_toString.c @@ -3,11 +3,12 @@ #include "../../kprint/kprint_format.h" -// accepts char* (ptr to char) and char** (ptr to string) +// accepts char* (ptr to char) and char* (ptr to string) +// uses format kp_s and kp_c to determine what type is argument char* __toString_char(void* c, u32 fmt) { // *c=char* if(kp_fmt_dataFormat(fmt)==kp_s){ - return cptr_copy(*(char**)c); // to avoid segmentation fault on free() when *c allocalet on stack + return cptr_copy((char*)c); // to avoid segmentation fault on free() when *c allocalet on stack } // *c=char if(kp_fmt_dataFormat(fmt)==kp_c){ diff --git a/src/base/type_system/base_toString.h b/src/base/type_system/base_toString.h index 41beca4..0446ce9 100644 --- a/src/base/type_system/base_toString.h +++ b/src/base/type_system/base_toString.h @@ -6,7 +6,7 @@ extern "C" { #include "../errors.h" -// accepts char* (ptr to char) and char** (ptr to string) +// accepts char* (ptr to char) and char* (ptr to string) // uses format kp_s and kp_c to determine what type is argument char* __toString_char(void* c, u32 fmt); diff --git a/src/base/type_system/init.c b/src/base/type_system/init.c index 5754d46..9078be2 100644 --- a/src/base/type_system/init.c +++ b/src/base/type_system/init.c @@ -63,10 +63,11 @@ void ktDescriptors_initKerepTypes(){ kt_register(Unitype); kt_register(Array_Unitype); kt_register(Autoarr_Unitype); - // replacing autogenerated freear() function to custom - Autoarr_Unitype* _uar=Autoarr_create(Unitype, 1, 1); - _uar->functions->freear=__Autoarr_free_Unitype_; - Autoarr_free(_uar, true); + // replacing autogenerated freear() function to custom + // in autoarr functions list + __Autoarr_Unitype_functions_list.freear=__Autoarr_Unitype_free_fixed; + // and in type descriptor + ktDescriptor_Autoarr_Unitype.freeMembers=____Autoarr_Unitype_free_fixed; // STNode kt_register(STNode); @@ -74,10 +75,11 @@ void ktDescriptors_initKerepTypes(){ // KeyValuePair kt_register(KVPair); kt_register(Autoarr_KVPair); - // replacing autogenerated freear() function to custom - Autoarr_KVPair* _kvpar=Autoarr_create(KVPair, 1, 1); - _kvpar->functions->freear=__Autoarr_free_KVPair_; - Autoarr_free(_kvpar, true); + // replacing autogenerated freear() function to custom + // in autoarr functions list + __Autoarr_KVPair_functions_list.freear=__Autoarr_KVPair_free_fixed; + // and in type descriptor + ktDescriptor_Autoarr_KVPair.freeMembers=____Autoarr_KVPair_free_fixed; // Hashtable kt_register(Hashtable); diff --git a/src/base/type_system/unitype.c b/src/base/type_system/unitype.c index c7eae44..a85c05e 100644 --- a/src/base/type_system/unitype.c +++ b/src/base/type_system/unitype.c @@ -1,90 +1,100 @@ -#include "../base.h" #include "../../kprint/kprint_format.h" +#include "../base.h" - -char* __Unitype_toString(void* _u, u32 fmt){ - return Unitype_toString(*(Unitype*)_u, fmt); +char *__Unitype_toString(void *_u, u32 fmt) +{ + return Unitype_toString(*(Unitype *)_u, fmt); } kt_define(Unitype, __UnitypePtr_free, __Unitype_toString); -void Unitype_free(Unitype u){ - if(u.typeId==ktid_undefined){ - if(u.VoidPtr!=NULL) +void Unitype_free(Unitype u) +{ + if (u.typeId == ktid_undefined) + { + if (u.VoidPtr != NULL) throw("unitype with undefined typeId has value"); return; } - ktDescriptor* type=ktDescriptor_get(u.typeId); - if(type->freeMembers) + ktDescriptor *type = ktDescriptor_get(u.typeId); + if (type->freeMembers) type->freeMembers(u.VoidPtr); - if(u.allocatedInHeap) + if (u.allocatedInHeap) free(u.VoidPtr); } -void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); } +void __UnitypePtr_free(void *u) +{ + Unitype_free(*(Unitype *)u); +} - -char* Unitype_toString(Unitype u, u32 fmt){ - if(u.typeId==ktid_undefined){ - if(u.VoidPtr!=NULL) +char *Unitype_toString(Unitype u, u32 fmt) +{ + if (u.typeId == ktid_undefined) + { + if (u.VoidPtr != NULL) throw("unitype with undefined typeId has value"); return cptr_copy("{ERROR_TYPE}"); } - if(fmt==0) { - if(u.typeId==ktid_name(bool) || - u.typeId==ktid_name(i8) || u.typeId==ktid_name(i16) || - u.typeId==ktid_name(i32) || u.typeId==ktid_name(i64)){ + if (fmt == 0) + { + if (u.typeId == ktid_name(bool) || u.typeId == ktid_name(i8) || u.typeId == ktid_name(i16) || + u.typeId == ktid_name(i32) || u.typeId == ktid_name(i64)) + { // auto format set - fmt=kp_i; + fmt = kp_i; // replaces value with pointer to value to pass into toString_i64(void*, u32) - i64 value=u.Int64; - u.VoidPtr=&value; + i64 value = u.Int64; + u.VoidPtr = &value; } - else if(u.typeId==ktid_name(u8) || u.typeId==ktid_name(u16) || - u.typeId==ktid_name(u32) || u.typeId==ktid_name(u64)){ - fmt=kp_u; - u64 value=u.UInt64; - u.VoidPtr=&value; + else if (u.typeId == ktid_name(u8) || u.typeId == ktid_name(u16) || u.typeId == ktid_name(u32) || + u.typeId == ktid_name(u64)) + { + fmt = kp_u; + u64 value = u.UInt64; + u.VoidPtr = &value; } - else if(u.typeId==ktid_name(f32) || u.typeId==ktid_name(f64)){ - fmt=kp_f; - f64 value=u.Float64; - u.VoidPtr=&value; + else if (u.typeId == ktid_name(f32) || u.typeId == ktid_name(f64)) + { + fmt = kp_f; + f64 value = u.Float64; + u.VoidPtr = &value; } - else if(u.typeId==ktid_name(char)){ - fmt=kp_c; - i64 value=u.Int64; - u.VoidPtr=&value; + else if (u.typeId == ktid_name(char)) + { + fmt = kp_c; + i64 value = u.Int64; + u.VoidPtr = &value; } - else if(u.typeId==ktid_ptrName(char)){ - char* value=u.VoidPtr; - u.VoidPtr=&value; - fmt=kp_s; + else if (u.typeId == ktid_ptrName(char)) + { + fmt = kp_s; } - else if(u.typeId==ktid_name(Pointer)){ - if(u.VoidPtr==NULL) + else if (u.typeId == ktid_name(Pointer)) + { + if (u.VoidPtr == NULL) return cptr_copy("{ UniNull }"); - fmt=kp_h; + fmt = kp_h; } } - ktDescriptor* type=ktDescriptor_get(u.typeId); - char* valuestr; - if(type->toString) - valuestr=type->toString(u.VoidPtr, fmt); - else valuestr="ERR_NO_TOSTRING_FUNC"; - char* rezult=cptr_concat("{ type: ", type->name, - ", allocated on heap: ", (u.allocatedInHeap ? "true" : "false"), - ", value:", valuestr, " }"); - if(type->toString) + ktDescriptor *type = ktDescriptor_get(u.typeId); + char *valuestr; + if (type->toString) + valuestr = type->toString(u.VoidPtr, fmt); + else + valuestr = "ERR_NO_TOSTRING_FUNC"; + char *rezult = cptr_concat("{ type: ", type->name, ", allocated on heap: ", (u.allocatedInHeap ? "true" : "false"), + ", value:", valuestr, " }"); + if (type->toString) free(valuestr); return rezult; } -void printuni(Unitype v){ - char* s=Unitype_toString(v,0); +void printuni(Unitype v) +{ + char *s = Unitype_toString(v, 0); fputs(s, stdout); - fputc('\n',stdout); free(s); } \ No newline at end of file diff --git a/src/kprint/kprint.c b/src/kprint/kprint.c index 351406d..c1e68e6 100644 --- a/src/kprint/kprint.c +++ b/src/kprint/kprint.c @@ -19,15 +19,19 @@ ktid __typeFromFormat(kp_fmt f){ case kp_s: return ktid_ptrName(char); default: - return -1; + return ktid_undefined; } } -Maybe __next_toString(kp_fmt f, __kp_value_union* object){ +Maybe __next_toString(kp_fmt f, void* object){ // detecting type ktid typeId=__typeFromFormat(f); if(typeId==ktid_undefined) safethrow("typeId is undefined, can't autodetect type",;); + + if(typeId==ktid_ptrName(char)) + object=*(char**)object; // dereferencing char** to char* + ktDescriptor* type=ktDescriptor_get(typeId); if(!type->toString) safethrow("type descriptor doesnt have toString() func",;); diff --git a/tests/test_dtsod.c b/tests/test_dtsod.c index 8af576c..71b38f9 100644 --- a/tests/test_dtsod.c +++ b/tests/test_dtsod.c @@ -33,33 +33,39 @@ void print_dtsod(Hashtable* dtsod){ } void test_dtsod(){ - optime(__func__,1,({ + // optime(__func__,1,({ kprintf("\e[96m-------------[test_dtsod]-------------\n"); Hashtable* dtsod; char* s; - optime("deserialize",1,({ + do { + // optime("deserialize",1,({ tryLast(DtsodV24_deserialize(text),r) dtsod=r.value.VoidPtr; - })); + // })); + } while(0); print_dtsod(dtsod); - optime("serialize",1,({ + do { + // optime("serialize",1,({ tryLast(DtsodV24_serialize(dtsod),r) s=r.value.VoidPtr; - })); + // })); + } while(0); DtsodV24_free(dtsod); kprintf("\e[92m%s",s); - optime("reserialize",10,({ + do { + // optime("reserialize",10,({ tryLast(DtsodV24_deserialize(s),r) dtsod=r.value.VoidPtr; free(s); tryLast(DtsodV24_serialize(dtsod),rr) s=rr.value.VoidPtr; DtsodV24_free(dtsod); - })); + // })); + } while(0); free(s); - })); + // })); } \ No newline at end of file From 00970919d18361b1f9844ec89d0682234e9d9a1f Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Mon, 13 Feb 2023 20:41:41 +0600 Subject: [PATCH 15/55] fixedmemory leaks in Autoarr --- .vscode/launch.json | 6 +++--- Makefile | 2 +- cbuild | 2 +- default.config | 2 +- src/Autoarr/Autoarr.c | 28 ++++++++++++------------ src/Autoarr/Autoarr.h | 20 +++++++++++++++-- src/Autoarr/Autoarr_Unitype.c | 16 -------------- src/Autoarr/Autoarr_Unitype.h | 33 ----------------------------- src/Autoarr/Autoarr_declare.h | 11 ++++++---- src/Autoarr/Autoarr_define.h | 24 +++++++++++++++------ src/Hashtable/Hashtable.c | 2 +- src/Hashtable/KeyValuePair.c | 11 +--------- src/Hashtable/KeyValuePair.h | 4 ---- src/String/string.c | 2 +- src/base/std.h | 1 + src/base/type_system/init.c | 12 +---------- src/base/type_system/kt_functions.c | 2 +- tests/test_dtsod.c | 22 +++++++------------ tests/tests.h | 6 +++--- 19 files changed, 81 insertions(+), 125 deletions(-) delete mode 100644 src/Autoarr/Autoarr_Unitype.c delete mode 100644 src/Autoarr/Autoarr_Unitype.h diff --git a/.vscode/launch.json b/.vscode/launch.json index 887a884..44f903a 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -5,7 +5,7 @@ "name": "(gdb) Debug", "type": "cppdbg", "request": "launch", - "program": "${workspaceFolder}/bin/kerep.com", + "program": "${workspaceFolder}/bin/kerep", "cwd": "${workspaceFolder}/bin", "preLaunchTask": "build_exec_dbg", "stopAtEntry": false, @@ -30,7 +30,7 @@ "type": "cppdbg", "request": "launch", "preLaunchTask": "build_exec_dbg", - "program": "${workspaceFolder}/bin/kerep.com", + "program": "${workspaceFolder}/bin/kerep", "cwd": "${workspaceFolder}/bin", "stopAtEntry": false, "externalConsole": false, @@ -48,7 +48,7 @@ "type": "cppvsdbg", "request": "launch", "preLaunchTask": "build_dbg", - "program": "${workspaceFolder}\\bin\\kerep.com", + "program": "${workspaceFolder}\\bin\\kerep", "cwd": "${workspaceFolder}\\bin", "stopAtEntry": false, "console": "integratedTerminal" diff --git a/Makefile b/Makefile index 3a38c5c..7f1ba15 100644 --- a/Makefile +++ b/Makefile @@ -42,7 +42,7 @@ exec: build_exec # executes $EXEC_FILE exec_dbg: build_exec_dbg - @cbuild/call_task.sh exec_dbg 2>&1 | tee -a make_raw.log + @cbuild/call_task.sh exec 2>&1 | tee -a make_raw.log # executes $EXEC_FILE with valgrind memory checker valgrind: build_exec_dbg diff --git a/cbuild b/cbuild index a0cdbf5..8e6b433 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit a0cdbf5522de6d4803eb135f2f52bd6cac5a3b8a +Subproject commit 8e6b4336d0d2741a82758834c7fc8bbd4de85360 diff --git a/default.config b/default.config index 31e6241..a9313e2 100644 --- a/default.config +++ b/default.config @@ -30,7 +30,7 @@ case "$OS" in SHARED_LIB_FILE="$PROJECT.dll" ;; LINUX) - EXEC_FILE="$PROJECT.P" + EXEC_FILE="$PROJECT" SHARED_LIB_FILE="$PROJECT.so" ;; *) diff --git a/src/Autoarr/Autoarr.c b/src/Autoarr/Autoarr.c index b8a57e4..f2159c8 100644 --- a/src/Autoarr/Autoarr.c +++ b/src/Autoarr/Autoarr.c @@ -1,15 +1,17 @@ #include "Autoarr.h" -Autoarr_define(char) -Autoarr_define(bool) -Autoarr_define(f32) -Autoarr_define(f64) -Autoarr_define(u8) -Autoarr_define(i8) -Autoarr_define(u16) -Autoarr_define(i16) -Autoarr_define(u32) -Autoarr_define(i32) -Autoarr_define(u64) -Autoarr_define(i64) -Autoarr_define(Pointer) +Autoarr_define(Pointer, true) +Autoarr_define(char, false) +Autoarr_define(bool, false) +Autoarr_define(f32, false) +Autoarr_define(f64, false) +Autoarr_define(u8, false) +Autoarr_define(i8, false) +Autoarr_define(u16, false) +Autoarr_define(i16, false) +Autoarr_define(u32, false) +Autoarr_define(i32, false) +Autoarr_define(u64, false) +Autoarr_define(i64, false) + +Autoarr_define(Unitype, false) diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h index e75da8f..405e018 100644 --- a/src/Autoarr/Autoarr.h +++ b/src/Autoarr/Autoarr.h @@ -6,8 +6,8 @@ extern "C" { #include "Autoarr_declare.h" #include "Autoarr_define.h" -#include "Autoarr_Unitype.h" +Autoarr_declare(Pointer) Autoarr_declare(char) Autoarr_declare(bool) Autoarr_declare(f32) @@ -20,7 +20,23 @@ Autoarr_declare(i32) Autoarr_declare(u32) Autoarr_declare(i64) Autoarr_declare(u64) -Autoarr_declare(Pointer) + +Autoarr_declare(Unitype) + +#define Autoarr_foreach(ar,elem,codeblock)({ \ + if(ar->blocks_count>0) { \ + typeof(**ar->values) elem; \ + for(u32 blockI=0;blockIblocks_count-1;blockI++) \ + for(u32 elemI=0;elemImax_block_length;elemI++){ \ + elem=ar->values[blockI][elemI]; \ + (codeblock); \ + } \ + for(u32 elemI=0;elemIblock_length;elemI++){ \ + elem=ar->values[ar->blocks_count-1][elemI]; \ + (codeblock); \ + } \ + } \ +}) #if __cplusplus } diff --git a/src/Autoarr/Autoarr_Unitype.c b/src/Autoarr/Autoarr_Unitype.c deleted file mode 100644 index c16e531..0000000 --- a/src/Autoarr/Autoarr_Unitype.c +++ /dev/null @@ -1,16 +0,0 @@ -#include "Autoarr.h" - -Autoarr_define(Unitype) - -u32 free_calls=0; - -// right func to clean array of unitype values -void __Autoarr_Unitype_free_fixed(Autoarr(Unitype)* ar, bool freePtr){ - Autoarr_foreach(ar, u,Unitype_free(u)); - __Autoarr_Unitype_free_g(ar, freePtr); - free_calls++; - kprintf("free_calls: %u\n", free_calls); -} -void ____Autoarr_Unitype_free_fixed(void* ar) { - __Autoarr_Unitype_free_fixed((Autoarr(Unitype)*)ar, false); -} diff --git a/src/Autoarr/Autoarr_Unitype.h b/src/Autoarr/Autoarr_Unitype.h deleted file mode 100644 index b58bdfa..0000000 --- a/src/Autoarr/Autoarr_Unitype.h +++ /dev/null @@ -1,33 +0,0 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "Autoarr_declare.h" -#include "Autoarr_define.h" - -Autoarr_declare(Unitype) - -// this function is injected in kerep_init() -void __Autoarr_Unitype_free_fixed(Autoarr(Unitype)* ar, bool freePtr); -void ____Autoarr_Unitype_free_fixed(void* ar); - -#define Autoarr_foreach(ar,elem,codeblock)({ \ - if(ar->blocks_count>0) { \ - typeof(**ar->values) elem; \ - for(u32 blockI=0;blockIblocks_count-1;blockI++) \ - for(u32 elemI=0;elemImax_block_length;elemI++){ \ - elem=ar->values[blockI][elemI]; \ - (codeblock); \ - } \ - for(u32 elemI=0;elemIblock_length;elemI++){ \ - elem=ar->values[ar->blocks_count-1][elemI]; \ - (codeblock); \ - } \ - } \ -}) - -#if __cplusplus -} -#endif \ No newline at end of file diff --git a/src/Autoarr/Autoarr_declare.h b/src/Autoarr/Autoarr_declare.h index e209115..d996063 100644 --- a/src/Autoarr/Autoarr_declare.h +++ b/src/Autoarr/Autoarr_declare.h @@ -15,7 +15,8 @@ typedef struct __Autoarr_##type##_functions_list_t { \ type (*get)(struct Autoarr_##type* ar, u32 index); \ type* (*getPtr)(struct Autoarr_##type* ar, u32 index); \ void (*set)(struct Autoarr_##type* ar, u32 index, type element); \ - void (*freear)(struct Autoarr_##type* ar, bool freePtr); \ + void (*freeWithMembers)(struct Autoarr_##type* ar, bool freePtr); \ + void (*freeWithoutMembers)(struct Autoarr_##type* ar, bool freePtr); \ type* (*toArray)(struct Autoarr_##type* ar); \ } __Autoarr_##type##_functions_list_t; \ \ @@ -31,8 +32,8 @@ STRUCT(Autoarr_##type, \ ) \ \ Autoarr_##type* __Autoarr_##type##_create(u16 max_blocks_count, u16 max_block_length); \ -void __Autoarr_##type##_free_g(Autoarr_##type* ar, bool freePtr); \ -void ____Autoarr_##type##_free_g(void* ar); +void __Autoarr_##type##_freeWithMembers(Autoarr_##type* ar, bool freePtr); \ +void ____Autoarr_##type##_freeWithMembers(void* ar); #define Autoarr(type) Autoarr_##type @@ -47,7 +48,9 @@ void ____Autoarr_##type##_free_g(void* ar); #define Autoarr_set(autoarr, index, element) \ autoarr->functions->set(autoarr, index, element) #define Autoarr_free(autoarr, freePtr) \ - autoarr->functions->freear(autoarr, freePtr) + autoarr->functions->freeWithMembers(autoarr, freePtr) +#define Autoarr_freeWithoutMembers(autoarr, freePtr) \ + autoarr->functions->freeWithoutMembers(autoarr, freePtr) #define Autoarr_toArray(autoarr) \ autoarr->functions->toArray(autoarr) diff --git a/src/Autoarr/Autoarr_define.h b/src/Autoarr/Autoarr_define.h index 94b4747..6c7df17 100644 --- a/src/Autoarr/Autoarr_define.h +++ b/src/Autoarr/Autoarr_define.h @@ -6,9 +6,9 @@ extern "C" { #include "../base/base.h" -#define Autoarr_define(type) \ +#define Autoarr_define(type, TYPE_IS_PTR) \ \ -kt_define(Autoarr_##type, ____Autoarr_##type##_free_g, NULL); \ +kt_define(Autoarr_##type, ____Autoarr_##type##_freeWithMembers, NULL); \ \ void __Autoarr_##type##_add(Autoarr_##type* ar, type element){ \ if(!ar->values){ \ @@ -41,14 +41,25 @@ void __Autoarr_##type##_set(Autoarr_##type* ar, u32 index, type element){ \ ar->values[index/ar->max_block_length][index%ar->max_block_length]=element; \ } \ \ -void __Autoarr_##type##_free_g(Autoarr_##type* ar, bool freePtr){ \ +void __Autoarr_##type##_freeWithoutMembers(Autoarr_##type* ar, bool freePtr){ \ for(u16 i=0; iblocks_count;i++) \ free(ar->values[i]); \ free(ar->values); \ if(freePtr) free(ar); \ } \ -void ____Autoarr_##type##_free_g(void* ar){ \ - __Autoarr_##type##_free_g((Autoarr_##type*)ar, false); \ +\ +void __Autoarr_##type##_freeWithMembers(Autoarr_##type* ar, bool freePtr){ \ + if(ktDescriptor_##type.freeMembers!=NULL) { \ + Autoarr_foreach(ar, el, ({ \ + void* members_ptr=⪙ \ + if(TYPE_IS_PTR) members_ptr=*(type**)members_ptr; \ + ktDescriptor_##type.freeMembers(members_ptr); \ + })); \ + } \ + __Autoarr_##type##_freeWithoutMembers(ar, freePtr);\ +} \ +void ____Autoarr_##type##_freeWithMembers(void* ar){ \ + __Autoarr_##type##_freeWithMembers((Autoarr_##type*)ar, false); \ } \ \ type* __Autoarr_##type##_toArray(Autoarr_##type* ar){ \ @@ -64,7 +75,8 @@ __Autoarr_##type##_functions_list_t __Autoarr_##type##_functions_list={ \ &__Autoarr_##type##_get, \ &__Autoarr_##type##_getPtr, \ &__Autoarr_##type##_set, \ - &__Autoarr_##type##_free_g, \ + &__Autoarr_##type##_freeWithMembers, \ + &__Autoarr_##type##_freeWithoutMembers, \ &__Autoarr_##type##_toArray \ }; \ \ diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index 2022084..c1d3e20 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -51,7 +51,7 @@ void Hashtable_expand(Hashtable* ht){ } // there is no need to free array values, because they are copied into new array // so dont replace this incorrect auto-generated function - __Autoarr_KVPair_free_g(ar, true); + Autoarr_freeWithoutMembers(ar, true); } free(ht->rows); diff --git a/src/Hashtable/KeyValuePair.c b/src/Hashtable/KeyValuePair.c index 724f490..952ebe8 100644 --- a/src/Hashtable/KeyValuePair.c +++ b/src/Hashtable/KeyValuePair.c @@ -2,7 +2,7 @@ kt_define(KVPair, __KVPair_free, NULL); -Autoarr_define(KVPair) +Autoarr_define(KVPair, false) // proper way to clean a KVP void KVPair_free(KVPair p){ @@ -11,15 +11,6 @@ void KVPair_free(KVPair p){ } void __KVPair_free(void* p){ KVPair_free(*(KVPair*)p); } -// func for KVP array cleaning -void __Autoarr_KVPair_free_fixed(Autoarr_KVPair* ar, bool freePtr){ - Autoarr_foreach(ar,k,KVPair_free(k)); - __Autoarr_KVPair_free_g(ar, freePtr); -} -void ____Autoarr_KVPair_free_fixed(void* ar){ - __Autoarr_KVPair_free_fixed((Autoarr_KVPair*)ar, false); -} - void printkvp(KVPair p){ kprintf("{\"%s\", ",p.key); printuni(p.value); diff --git a/src/Hashtable/KeyValuePair.h b/src/Hashtable/KeyValuePair.h index 2d3fb32..99d8117 100644 --- a/src/Hashtable/KeyValuePair.h +++ b/src/Hashtable/KeyValuePair.h @@ -18,10 +18,6 @@ Autoarr_declare(KVPair) void KVPair_free(KVPair p); void __KVPair_free(void* p); -// func to clean KVP array -void __Autoarr_KVPair_free_fixed(Autoarr_KVPair* ar, bool freePtr); -void ____Autoarr_KVPair_free_fixed(void* ar); - void printkvp(KVPair p); #if __cplusplus diff --git a/src/String/string.c b/src/String/string.c index 36c2684..7e2f084 100644 --- a/src/String/string.c +++ b/src/String/string.c @@ -2,7 +2,7 @@ kt_define(string, NULL, NULL); Array_define(string) -Autoarr_define(string) +Autoarr_define(string, false) // copies str content to new char pointer value (adding '\0' at the end) char* string_extract(string str){ diff --git a/src/base/std.h b/src/base/std.h index 1fc07e1..0055fd1 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -22,6 +22,7 @@ typedef int64_t i64; typedef uint64_t u64; typedef float f32; typedef double f64; +/// anonymous pointer without specified freeMembers() func typedef void* Pointer; // Usually bool from stdbool.h is defined as macro, diff --git a/src/base/type_system/init.c b/src/base/type_system/init.c index 9078be2..7b619cb 100644 --- a/src/base/type_system/init.c +++ b/src/base/type_system/init.c @@ -45,6 +45,7 @@ void ktDescriptors_initKerepTypes(){ kt_register(Array_Pointer); // base type autoarrs + kt_register(Autoarr_Pointer); kt_register(Autoarr_char); kt_register(Autoarr_bool); kt_register(Autoarr_f32); @@ -57,17 +58,11 @@ void ktDescriptors_initKerepTypes(){ kt_register(Autoarr_u32); kt_register(Autoarr_i64); kt_register(Autoarr_u64); - kt_register(Autoarr_Pointer); // Unitype kt_register(Unitype); kt_register(Array_Unitype); kt_register(Autoarr_Unitype); - // replacing autogenerated freear() function to custom - // in autoarr functions list - __Autoarr_Unitype_functions_list.freear=__Autoarr_Unitype_free_fixed; - // and in type descriptor - ktDescriptor_Autoarr_Unitype.freeMembers=____Autoarr_Unitype_free_fixed; // STNode kt_register(STNode); @@ -75,11 +70,6 @@ void ktDescriptors_initKerepTypes(){ // KeyValuePair kt_register(KVPair); kt_register(Autoarr_KVPair); - // replacing autogenerated freear() function to custom - // in autoarr functions list - __Autoarr_KVPair_functions_list.freear=__Autoarr_KVPair_free_fixed; - // and in type descriptor - ktDescriptor_Autoarr_KVPair.freeMembers=____Autoarr_KVPair_free_fixed; // Hashtable kt_register(Hashtable); diff --git a/src/base/type_system/kt_functions.c b/src/base/type_system/kt_functions.c index bccaf07..b73eca0 100644 --- a/src/base/type_system/kt_functions.c +++ b/src/base/type_system/kt_functions.c @@ -2,7 +2,7 @@ #include "type_system.h" #include "base_toString.h" -kt_define(Pointer, free, __toString_u64); +kt_define(Pointer, NULL, __toString_u64); kt_define(char,NULL, __toString_char); kt_define(bool,NULL, __toString_bool); kt_define(f32, NULL, __toString_f32); diff --git a/tests/test_dtsod.c b/tests/test_dtsod.c index 71b38f9..8af576c 100644 --- a/tests/test_dtsod.c +++ b/tests/test_dtsod.c @@ -33,39 +33,33 @@ void print_dtsod(Hashtable* dtsod){ } void test_dtsod(){ - // optime(__func__,1,({ + optime(__func__,1,({ kprintf("\e[96m-------------[test_dtsod]-------------\n"); Hashtable* dtsod; char* s; - do { - // optime("deserialize",1,({ + optime("deserialize",1,({ tryLast(DtsodV24_deserialize(text),r) dtsod=r.value.VoidPtr; - // })); - } while(0); + })); print_dtsod(dtsod); - do { - // optime("serialize",1,({ + optime("serialize",1,({ tryLast(DtsodV24_serialize(dtsod),r) s=r.value.VoidPtr; - // })); - } while(0); + })); DtsodV24_free(dtsod); kprintf("\e[92m%s",s); - do { - // optime("reserialize",10,({ + optime("reserialize",10,({ tryLast(DtsodV24_deserialize(s),r) dtsod=r.value.VoidPtr; free(s); tryLast(DtsodV24_serialize(dtsod),rr) s=rr.value.VoidPtr; DtsodV24_free(dtsod); - // })); - } while(0); + })); free(s); - // })); + })); } \ No newline at end of file diff --git a/tests/tests.h b/tests/tests.h index c65a4ba..616721a 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -28,12 +28,12 @@ inline void test_all(){ test_searchtree(); test_autoarr(); test_autoarrVsVector(); - test_hash_functions(); - test_hashtable(); - test_dtsod(); test_rng_algorithms(); test_kprint_colors(); test_kprint(); + test_hash_functions(); + test_hashtable(); + test_dtsod(); kprintf("\e[96m--------------------------------------\e[0m\n"); })); } From dbf2c2772c832bbb6f8f56b9741a21722daa01a2 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Mon, 13 Feb 2023 21:52:10 +0600 Subject: [PATCH 16/55] cbuild updated --- cbuild | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cbuild b/cbuild index 8e6b433..112fcc0 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit 8e6b4336d0d2741a82758834c7fc8bbd4de85360 +Subproject commit 112fcc04652d6ce65fbde215cd3d1f8935db2a7c From 5bbab0a414e4e150a90b96b444d76a94d9002262 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Wed, 15 Feb 2023 21:44:38 +0600 Subject: [PATCH 17/55] string_copy adds trailing zero byte --- src/String/string.c | 8 +++++--- src/String/string.h | 5 ++++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/src/String/string.c b/src/String/string.c index 7e2f084..2099468 100644 --- a/src/String/string.c +++ b/src/String/string.c @@ -14,14 +14,16 @@ char* string_extract(string str){ return cptr; } -// copies src.ptr content to new string +// copies src.ptr content to new string and adds \0 at the end string string_copy(string src){ - if(!src.ptr) return src; + if(!src.ptr) + return src; string nstr; nstr.length=src.length; - nstr.ptr=malloc(nstr.length); + nstr.ptr=malloc(nstr.length+1); for(u32 i=0;i Date: Wed, 15 Feb 2023 21:45:10 +0600 Subject: [PATCH 18/55] simplified kt initialiation function names --- src/base/type_system/README.md | 8 ++++---- src/base/type_system/init.c | 2 +- src/base/type_system/init.h | 4 ++-- src/base/type_system/ktDescriptor.h | 5 +++++ src/base/type_system/kt_functions.c | 6 +++--- src/base/type_system/kt_functions.h | 6 +++--- src/base/type_system/ktid.h | 2 ++ tests/main.cpp | 8 ++++---- 8 files changed, 24 insertions(+), 17 deletions(-) diff --git a/src/base/type_system/README.md b/src/base/type_system/README.md index 66f54b0..c0066e9 100644 --- a/src/base/type_system/README.md +++ b/src/base/type_system/README.md @@ -26,9 +26,9 @@ Every registered type should have it's own descriptor (`ktDescriptor`). It's a s ## type registration -To finally register a type, you should call macro `kt_register()` between `ktDescriptors_beginInit()` and `ktDescriptors_endInit()`. Better do it at the start of your program. To register all types from kerep, call `ktDescriptors_initKerepTypes()`. +To finally register a type, you should call macro `kt_register()` between `kt_beginInit()` and `kt_endInit()`. Better do it at the start of your program. To register all types from kerep, call `kt_initKerepTypes()`. -You can free internal ktDescriptors storage by calling `ktDescriptors_free()` at exit, if your debugger (valgrind in my case) sees a memory leak. +You can free internal ktDescriptors storage by calling `kt_free()` at exit, if your debugger (valgrind in my case) sees a memory leak. Examples: -+ [ktDescriptors_initKerepTypes()](src/base/type_system/init.c) -+ [kerep types registration](tests/main.cpp) ++ [kerep types registration](src/base/type_system/init.c) ++ [kt_initKerepTypes()](tests/main.cpp) diff --git a/src/base/type_system/init.c b/src/base/type_system/init.c index 7b619cb..fc5ba12 100644 --- a/src/base/type_system/init.c +++ b/src/base/type_system/init.c @@ -7,7 +7,7 @@ #include "../../Filesystem/filesystem.h" #include "base_toString.h" -void ktDescriptors_initKerepTypes(){ +void kt_initKerepTypes(){ // base types kt_register(Pointer); if(ktid_Pointer!=0) // this can break UnitypeNull diff --git a/src/base/type_system/init.h b/src/base/type_system/init.h index d08ef9d..df787ca 100644 --- a/src/base/type_system/init.h +++ b/src/base/type_system/init.h @@ -4,8 +4,8 @@ extern "C" { #endif -// call this between ktDescriptors_beginInit() and ktDescriptors_endInit() -void ktDescriptors_initKerepTypes(); +// call this between kt_beginInit() and kt_endInit() +void kt_initKerepTypes(); #if __cplusplus } diff --git a/src/base/type_system/ktDescriptor.h b/src/base/type_system/ktDescriptor.h index 8b95115..31e4ee8 100644 --- a/src/base/type_system/ktDescriptor.h +++ b/src/base/type_system/ktDescriptor.h @@ -41,6 +41,11 @@ STRUCT(ktDescriptor, toString_t toString; // NULL or function which generates string representaion of object ) +/// gets descriptor for TYPE +#define ktDescriptor_name(TYPE) ktDescriptor_##TYPE +/// gets descriptor for pointer to TYPE +#define ktDescriptor_namePtr(TYPE) ktDescriptor_##TYPE##_Ptr + #if __cplusplus } #endif \ No newline at end of file diff --git a/src/base/type_system/kt_functions.c b/src/base/type_system/kt_functions.c index b73eca0..8ced590 100644 --- a/src/base/type_system/kt_functions.c +++ b/src/base/type_system/kt_functions.c @@ -31,14 +31,14 @@ ENUM(ktDescriptorsState, ) ktDescriptorsState initState=NotInitialized; -void ktDescriptors_beginInit(){ +void kt_beginInit(){ kprintf("\e[94mtype descriptors initializing...\n"); __descriptorPointers=Autoarr_create(Pointer, 256, 256); if(__descriptorPointers==NULL) throw(ERR_NULLPTR); } -void ktDescriptors_endInit(){ +void kt_endInit(){ typeDescriptors=(ktDescriptor**)Autoarr_toArray(__descriptorPointers); Autoarr_free(__descriptorPointers,true); if(typeDescriptors==NULL) throw(ERR_NULLPTR); @@ -58,6 +58,6 @@ ktDescriptor* ktDescriptor_get(ktid id){ return typeDescriptors[id]; } -void ktDescriptors_free(){ +void kt_free(){ free(typeDescriptors); } diff --git a/src/base/type_system/kt_functions.h b/src/base/type_system/kt_functions.h index 6410d38..b131ce2 100644 --- a/src/base/type_system/kt_functions.h +++ b/src/base/type_system/kt_functions.h @@ -17,14 +17,14 @@ void __kt_register(ktDescriptor* descriptor); __kt_register(&ktDescriptor_##TYPE##_Ptr); \ ktid_##TYPE##_Ptr=ktid_last; -void ktDescriptors_beginInit(); -void ktDescriptors_endInit(); +void kt_beginInit(); +void kt_endInit(); /// @param id id of registered type ktDescriptor* ktDescriptor_get(ktid id); // call it to free heap-allocated ktDescriptors array -void ktDescriptors_free(); +void kt_free(); kt_declare(Pointer); kt_declare(char); diff --git a/src/base/type_system/ktid.h b/src/base/type_system/ktid.h index 9d9c276..be08484 100644 --- a/src/base/type_system/ktid.h +++ b/src/base/type_system/ktid.h @@ -10,7 +10,9 @@ extern "C" { typedef u16 ktid; static const ktid ktid_undefined=-1; +/// gets descriptor id for TYPE #define ktid_name(TYPE) ktid_##TYPE +/// gets descriptor id for pointer to TYPE #define ktid_ptrName(TYPE) ktid_##TYPE##_Ptr #define ktid_declare(TYPE) \ diff --git a/tests/main.cpp b/tests/main.cpp index 14e310c..91594d1 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -3,11 +3,11 @@ i32 main(){ if(!setlocale(LC_ALL, "C.UTF8")) kprintf("\e[93msetlocale failed\n"); - ktDescriptors_beginInit(); - ktDescriptors_initKerepTypes(); - ktDescriptors_endInit(); + kt_beginInit(); + kt_initKerepTypes(); + kt_endInit(); test_all(); - ktDescriptors_free(); + kt_free(); kprintf("\e[0m\n"); return 0; } From c528da857fff01f8d8c571bd46ae66ae82dfe31f Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Wed, 1 Mar 2023 12:13:40 +0600 Subject: [PATCH 19/55] nameof --- src/Filesystem/path.h | 2 +- src/base/errors.c | 24 ++++++++++++------------ src/base/std.h | 2 ++ 3 files changed, 15 insertions(+), 13 deletions(-) diff --git a/src/Filesystem/path.h b/src/Filesystem/path.h index 9a0ced3..8b3d1cc 100644 --- a/src/Filesystem/path.h +++ b/src/Filesystem/path.h @@ -15,7 +15,7 @@ static const char path_notSep='\\'; #endif char* __path_concat(u16 n, ...); -/// @brief merges path parts together and places between them +/// @brief merges path parts together and puts between them /// @return new cstr #define path_concat(PATH_PARTS...) __path_concat(count_args(PATH_PARTS), PATH_PARTS) diff --git a/src/base/errors.c b/src/base/errors.c index a93aff6..6f093f1 100644 --- a/src/base/errors.c +++ b/src/base/errors.c @@ -5,18 +5,18 @@ char* errname(ErrorId err){ switch(err){ - case SUCCESS: return "SUCCESS"; - case ERR_MAXLENGTH: return "ERR_MAXLENGTH"; - case ERR_WRONGTYPE: return "ERR_WRONGTYPE"; - case ERR_WRONGINDEX: return "ERR_WRONGINDEX"; - case ERR_NOTIMPLEMENTED: return "ERR_NOTIMPLEMENTED"; - case ERR_NULLPTR: return "ERR_NULLPTR"; - case ERR_ENDOFSTR: return "ERR_ENDOFSTR"; - case ERR_KEYNOTFOUND: return "ERR_KEYNOTFOUND"; - case ERR_FORMAT: return "ERR_FORMAT"; - case ERR_UNEXPECTEDVAL: return "ERR_UNEXPECTEDVAL"; - case ERR_IO: return "ERR_IO"; - case ERR_IO_EOF: return "ERR_IO_EOF"; + case SUCCESS: return nameof(SUCCESS); + case ERR_MAXLENGTH: return nameof(ERR_MAXLENGTH); + case ERR_WRONGTYPE: return nameof(ERR_WRONGTYPE); + case ERR_WRONGINDEX: return nameof(ERR_WRONGINDEX); + case ERR_NOTIMPLEMENTED: return nameof(ERR_NOTIMPLEMENTED); + case ERR_NULLPTR: return nameof(ERR_NULLPTR); + case ERR_ENDOFSTR: return nameof(ERR_ENDOFSTR); + case ERR_KEYNOTFOUND: return nameof(ERR_KEYNOTFOUND); + case ERR_FORMAT: return nameof(ERR_FORMAT); + case ERR_UNEXPECTEDVAL: return nameof(ERR_UNEXPECTEDVAL); + case ERR_IO: return nameof(ERR_IO); + case ERR_IO_EOF: return nameof(ERR_IO_EOF); default: return "UNKNOWN_ERROR"; } } diff --git a/src/base/std.h b/src/base/std.h index 0055fd1..ac4748a 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -37,6 +37,8 @@ typedef u8 bool; #define dbg(N) kprintf("\e[95m%d\n",N) +#define nameof(V) #V + #ifdef _MSC_VER #pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK #define _CRT_SECURE_NO_WARNINGS 1 From 33eb4302c427ee065138aae0a87739dccb3d6f23 Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 8 Mar 2023 17:17:31 +0600 Subject: [PATCH 20/55] WARN_UNUSED_REZULT --- src/base/std.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/base/std.h b/src/base/std.h index ac4748a..14e85c0 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -123,6 +123,9 @@ You can even embed it into macro in header (see kprint.h) CODE; \ PRAGMA_WARNING_POP +/// gcc throws warning on unused function return value +#define WARN_UNUSED_REZULT __attribute__((warn_unused_result)) + #if __cplusplus } #endif \ No newline at end of file From 430b6267d19a04b725b19d01ec04d587d000405d Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 9 Mar 2023 18:59:18 +0600 Subject: [PATCH 21/55] base_toString.h included into base.h --- src/kprint/kprintf.c | 1 - src/kprint/kprintf.h | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/kprint/kprintf.c b/src/kprint/kprintf.c index 52e8201..c4a3bb6 100644 --- a/src/kprint/kprintf.c +++ b/src/kprint/kprintf.c @@ -1,6 +1,5 @@ #include "kprintf.h" #include "../base/base.h" -#include "../base/type_system/base_toString.h" #if defined(_WIN64) || defined(_WIN32) #include diff --git a/src/kprint/kprintf.h b/src/kprint/kprintf.h index 5bb4fa9..d4df677 100644 --- a/src/kprint/kprintf.h +++ b/src/kprint/kprintf.h @@ -4,6 +4,8 @@ extern "C" { #endif +#include "../base/type_system/base_toString.h" + // cross-platform printf analog void kprintf(const char* format, ...); From a0458f6d2ef04ce30f5c951281cd7de91bd48cff Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 9 Mar 2023 22:01:49 +0600 Subject: [PATCH 22/55] kprintf hex --- src/kprint/README.md | 4 +++- src/kprint/kprintf.c | 15 ++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/kprint/README.md b/src/kprint/README.md index 1bdf1d2..e1ad83f 100644 --- a/src/kprint/README.md +++ b/src/kprint/README.md @@ -11,7 +11,9 @@ Unlike in standard printf, `%l...` and `%ll...` placeholders dont depend on size | f32 / f64 | %f | | char | %c | | char[] | %s | -| void\* | %p / %x | +| void\* | %p | +| 32bit or less | %x | +| 64bit | %lx |
diff --git a/src/kprint/kprintf.c b/src/kprint/kprintf.c index c4a3bb6..80c299c 100644 --- a/src/kprint/kprintf.c +++ b/src/kprint/kprintf.c @@ -75,10 +75,19 @@ void kprintf(const char* format, ...){ if((c=format[i++])) goto format_escape_seq; break; - case 'p': + case 'p': ; + void* phex=va_arg(vl, void*); + argstr=toString_hex(&phex,getEndian()==LittleEndian,sizeof(phex),1,0); + break; case 'x': ; - u64 px=va_arg(vl, u64); - argstr=toString_hex(&px,getEndian()==LittleEndian,sizeof(px),1,0); + if(l){ + u64 xhex=va_arg(vl, u64); + argstr=toString_hex(&xhex,getEndian()==LittleEndian,sizeof(xhex),0,1); + } + else { + u32 xhex=va_arg(vl, u32); + argstr=toString_hex(&xhex,getEndian()==LittleEndian,sizeof(xhex),0,1); + } break; case 's': ; char* cptr=va_arg(vl,char*); From 965c784e37b955ec76bb927dbb23ed8779355bac Mon Sep 17 00:00:00 2001 From: timerix Date: Mon, 13 Mar 2023 18:20:29 +0600 Subject: [PATCH 23/55] setlocale --- tests/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/main.cpp b/tests/main.cpp index 91594d1..c6876e2 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -1,7 +1,7 @@ #include "tests.h" i32 main(){ - if(!setlocale(LC_ALL, "C.UTF8")) + if(setlocale(LC_CTYPE, "C.UTF-8")!=0) kprintf("\e[93msetlocale failed\n"); kt_beginInit(); kt_initKerepTypes(); From 0adea8c52ce4d7f51c04cff85a8b4d4871465adc Mon Sep 17 00:00:00 2001 From: timerix Date: Mon, 13 Mar 2023 19:30:13 +0600 Subject: [PATCH 24/55] fixed file_exists and dir_exists --- src/Filesystem/dir.c | 4 ++-- src/Filesystem/file.c | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Filesystem/dir.c b/src/Filesystem/dir.c index c98e732..812c755 100644 --- a/src/Filesystem/dir.c +++ b/src/Filesystem/dir.c @@ -12,13 +12,13 @@ bool dir_exists(const char* path){ #if KFS_USE_WINDOWS_H DWORD dwAttrib = GetFileAttributes(path); return (bool)( - (dwAttrib != INVALID_FILE_ATTRIBUTES) & // file exists + (dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists (dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // file is a directory #else struct stat stats; i32 rez=stat(path, &stats); return (bool)( - (rez!=-1) & // file exists + (rez!=-1) && // file exists (S_ISDIR(stats.st_mode))); // file is a directory #endif } diff --git a/src/Filesystem/file.c b/src/Filesystem/file.c index c58aa4f..b028652 100644 --- a/src/Filesystem/file.c +++ b/src/Filesystem/file.c @@ -15,13 +15,13 @@ bool file_exists(const char* path){ #if KFS_USE_WINDOWS_H DWORD dwAttrib = GetFileAttributes(path); return (bool)( - (dwAttrib != INVALID_FILE_ATTRIBUTES) & // file exists + (dwAttrib != INVALID_FILE_ATTRIBUTES) && // file exists !(dwAttrib & FILE_ATTRIBUTE_DIRECTORY)); // file is not directory #else struct stat stats; i32 rez=stat(path, &stats); return (bool)( - (rez!=-1) & // file exists + (rez!=-1) && // file exists !(S_ISDIR(stats.st_mode))); // file is not directory #endif } From ce44509d046108f1240bbb02d0f9d3c605aa9bd6 Mon Sep 17 00:00:00 2001 From: timerix Date: Mon, 13 Mar 2023 20:39:38 +0600 Subject: [PATCH 25/55] small addition to STRUCT() --- src/Filesystem/file.c | 2 +- src/base/type_system/typedef_macros.h | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Filesystem/file.c b/src/Filesystem/file.c index b028652..6a3358c 100644 --- a/src/Filesystem/file.c +++ b/src/Filesystem/file.c @@ -116,7 +116,7 @@ Maybe file_readAll(File* file, char** allBytes){ rezult=fgetc(file); if(rezult==EOF){ if(ferror(file)) - safethrow(ERR_IO,; StringBuilder_free(sb)); + safethrow(ERR_IO, StringBuilder_free(sb)); break; } buffer[i%sizeof(buffer)]=(char)rezult; diff --git a/src/base/type_system/typedef_macros.h b/src/base/type_system/typedef_macros.h index 08e812a..ccaab8d 100644 --- a/src/base/type_system/typedef_macros.h +++ b/src/base/type_system/typedef_macros.h @@ -8,7 +8,8 @@ ENUM_MEMBERS \ } __attribute__((__packed__)) ENUM_NAME; -#define STRUCT(STRUCT_NAME, STRUCT_MEMBERS...) typedef struct STRUCT_NAME { \ +#define STRUCT(STRUCT_NAME, STRUCT_MEMBERS...) typedef struct STRUCT_NAME STRUCT_NAME; \ +typedef struct STRUCT_NAME { \ STRUCT_MEMBERS \ } STRUCT_NAME; \ kt_declare(STRUCT_NAME); From 2a9214fb795e97aa754ac46346310f94b7e61bfd Mon Sep 17 00:00:00 2001 From: timerix Date: Tue, 14 Mar 2023 01:12:26 +0600 Subject: [PATCH 26/55] path_basename --- src/Filesystem/path.c | 12 ++++++++++++ src/Filesystem/path.h | 6 +++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/Filesystem/path.c b/src/Filesystem/path.c index b3d046e..ef977ce 100644 --- a/src/Filesystem/path.c +++ b/src/Filesystem/path.c @@ -70,3 +70,15 @@ char* path_parentDir(char* dir){ } return copy; } + + +char* path_basename(char* path, bool with_extension){ + i32 nameIndex=cptr_lastIndexOfChar(path, path_sep)+1; + string rezult=string_fromCptr(path+nameIndex); + if(!with_extension){ + i32 extIndex=cptr_lastIndexOfChar(rezult.ptr, '.'); + if(extIndex!=0 && extIndex!=-1) + rezult.length=extIndex; + } + return string_extract(rezult); +} diff --git a/src/Filesystem/path.h b/src/Filesystem/path.h index 8b3d1cc..29571aa 100644 --- a/src/Filesystem/path.h +++ b/src/Filesystem/path.h @@ -31,7 +31,11 @@ char* path_fixSeparators(const char* path); /// @return Maybe Maybe path_throwIfEscapes(const char* path); -char* path_parentDir(char* dir); +///@return path of parent dir +char* path_parentDir(char* path); + +///@return file name +char* path_basename(char* path, bool with_extension); #if __cplusplus } From 8e572a9db3df7535420fc77ef92d86fa0b0c8c92 Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 15 Mar 2023 03:28:27 +0600 Subject: [PATCH 27/55] LinkedList --- src/LinkedList/LinkedList.c | 3 + src/LinkedList/LinkedList.h | 73 +++++++++++++++++++ src/LinkedList/LinkedList_declare.h | 45 ++++++++++++ src/LinkedList/LinkedList_define.h | 109 ++++++++++++++++++++++++++++ 4 files changed, 230 insertions(+) create mode 100644 src/LinkedList/LinkedList.c create mode 100644 src/LinkedList/LinkedList.h create mode 100644 src/LinkedList/LinkedList_declare.h create mode 100644 src/LinkedList/LinkedList_define.h diff --git a/src/LinkedList/LinkedList.c b/src/LinkedList/LinkedList.c new file mode 100644 index 0000000..a6302ed --- /dev/null +++ b/src/LinkedList/LinkedList.c @@ -0,0 +1,3 @@ +#include "LinkedList.h" + +LinkedList_define(Pointer, true) diff --git a/src/LinkedList/LinkedList.h b/src/LinkedList/LinkedList.h new file mode 100644 index 0000000..6283ad7 --- /dev/null +++ b/src/LinkedList/LinkedList.h @@ -0,0 +1,73 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../base/base.h" +#include "LinkedList_declare.h" +#include "LinkedList_define.h" + +// LinkedListNode + +#define LLNode(TYPE) LLNode_##TYPE +#define LinkedList(TYPE) LinkedList_##TYPE + +#define LinkedList_create(TYPE) LinkedList_##TYPE##_create() +#define LinkedList_free(LLIST) ({ LLIST->_functions->freeMembers(LLIST); free(LLIST); }) + +/// inserts NEW_NODE before NEXT_NODE in LLIST +#define LinkedList_insertPrev(LLIST, NEW_NODE, NEXT_NODE) LLIST->_functions->insertPrev(LLIST, NEW_NODE, NEXT_NODE) + +/// inserts NEW_NODE after PREV_NODE in LLIST +#define LinkedList_insertNext(LLIST, NEW_NODE, PREV_NODE) LLIST->_functions->insertNext(LLIST, NEW_NODE, PREV_NODE) + +/// removes node before NEXT_NODE in LLIST +/// if FREE_REMOVED then frees removed node +#define LinkedList_removePrev(LLIST, NEXT_NODE, FREE_REMOVED) LLIST->_functions->removePrev(LLIST, NEXT_NODE, FREE_REMOVED) + +/// removes node after PREV_NODE in LLIST +/// if FREE_REMOVED then frees removed node +#define LinkedList_removeNext(LLIST, PREV_NODE, FREE_REMOVED) LLIST->_functions->removeNext(LLIST, PREV_NODE, FREE_REMOVED) + +///@param FIRST_N first node in enumeration +///@param CURR_N name of iteration variable +///@param CODE code todo in every iteration +#define LLNode_foreach(FIRST_N, CURR_N, CODE...) { \ + typeof(FIRST_N) CURR_N=FIRST_N; \ + while(CURR_N!=NULL) { \ + CODE; \ + CURR_N=CURR_N->next; \ + } \ +} + +///@param FIRST_N first node in enumeration +///@param CURR_N name of iteration variable +///@param CODE code todo in every iteration +#define LLNode_foreachReverse(FIRST_N, CURR_N, CODE...) { \ + typeof(FIRST_N) CURR_N=FIRST_N; \ + while(CURR_N!=NULL) { \ + CODE; \ + CURR_N=CURR_N->prev; \ + } \ +} + +///@param LLIST LinkedList +///@param CURR_N name of iteration variable +///@param CODE code todo in every iteration +#define LinkedList_foreach(LLIST, CURR_N, CODE...) \ + LLNode_foreach(LLIST->first_node, CURR_N, CODE) + +///@param LLIST LinkedList +///@param CURR_N name of iteration variable +///@param CODE code todo in every iteration +#define LinkedList_foreachReverse(LLIST, CURR_N, CODE...) \ + LLNode_foreachReverse(LLIST->last_node, CURR_N, CODE) + + + +LinkedList_declare(Pointer) + +#if __cplusplus +} +#endif diff --git a/src/LinkedList/LinkedList_declare.h b/src/LinkedList/LinkedList_declare.h new file mode 100644 index 0000000..418a722 --- /dev/null +++ b/src/LinkedList/LinkedList_declare.h @@ -0,0 +1,45 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#define LLNode_declare(TYPE)\ +STRUCT(LLNode(TYPE), \ + LLNode(TYPE)* prev; \ + LLNode(TYPE)* next; \ + TYPE value; \ +) \ +\ +LLNode(TYPE)* LLNode_##TYPE##_create(); \ +LLNode(TYPE)* LLNode_##TYPE##_createFromValue(TYPE value); \ +void LLNode_##TYPE##_free(LLNode(TYPE)* node, bool free_value); + + +#define LinkedList_declare(TYPE)\ +LLNode_declare(TYPE) \ +typedef struct LinkedList_##TYPE##_functions_t LinkedList_##TYPE##_functions_t; \ +\ +STRUCT(LinkedList(TYPE), \ + LinkedList_##TYPE##_functions_t* _functions; \ + LLNode(TYPE)* first_node; \ + LLNode(TYPE)* last_node; \ + u32 count; \ +) \ +\ +typedef struct LinkedList_##TYPE##_functions_t { \ + freeMembers_t freeMembers; \ + void (*insertPrev)(LinkedList(TYPE)* llist, LLNode(TYPE)* newNode, LLNode(TYPE)* nextNode); \ + void (*insertNext)(LinkedList(TYPE)* llist, LLNode(TYPE)* newNode, LLNode(TYPE)* prevNode); \ + void (*removePrev)(LinkedList(TYPE)* llist, LLNode(TYPE)* nextNode, bool freeRemoved); \ + void (*removeNext)(LinkedList(TYPE)* llist, LLNode(TYPE)* prevNode, bool freeRemoved); \ +} LinkedList_##TYPE##_functions_t; \ +\ +extern LinkedList_##TYPE##_functions_t _LinkedList_##TYPE##_functions; \ +\ +LinkedList(TYPE)* LinkedList_##TYPE##_create(); \ + + +#if __cplusplus +} +#endif diff --git a/src/LinkedList/LinkedList_define.h b/src/LinkedList/LinkedList_define.h new file mode 100644 index 0000000..2ea4e16 --- /dev/null +++ b/src/LinkedList/LinkedList_define.h @@ -0,0 +1,109 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#define LLNode_define(TYPE, TYPE_IS_PTR)\ +LLNode(TYPE)* LLNode_##TYPE##_create(){ \ + LLNode(TYPE)* node= (LLNode(TYPE)*)malloc(sizeof(*node)); \ + node->prev=NULL; \ + node->next=NULL; \ + node->value=(TYPE){0}; \ + return node; \ +} \ +\ +LLNode(TYPE)* LLNode_##TYPE##_createFromValue(TYPE value){ \ + LLNode(TYPE)* node= (LLNode(TYPE)*)malloc(sizeof(*node)); \ + node->prev=NULL; \ + node->next=NULL; \ + node->value=value; \ + return node; \ +} \ +\ +void LLNode_##TYPE##_freeMembers(void* _node){ \ + LLNode(TYPE)* node=(LLNode(TYPE)*)_node; \ + void* value_ptr=&node->value; \ + if(TYPE_IS_PTR) value_ptr=*(TYPE**)value_ptr; \ + ktDescriptor_##TYPE.freeMembers(value_ptr); \ + if(node->prev) node->prev->next=NULL; \ + if(node->next) node->next->prev=NULL; \ +} \ +\ +void LLNode_##TYPE##_free(LLNode(TYPE)* node, bool free_value){ \ + if(free_value) LLNode_##TYPE##_freeMembers(node); \ + if(node->prev) node->prev->next=NULL; \ + if(node->next) node->next->prev=NULL; \ + free(node); \ +} \ +\ +kt_define(LLNode_##TYPE, LLNode_##TYPE##_freeMembers, NULL) + + +#define LinkedList_define(TYPE, VALUE_IS_PTR)\ +LLNode_define(TYPE, VALUE_IS_PTR) \ +\ +LinkedList(TYPE)* LinkedList_##TYPE##_create(){ \ + LinkedList(TYPE)* l=malloc(sizeof(*l)); \ + l->_functions=&_LinkedList_##TYPE##_functions; \ + l->first_node=NULL; \ + l->last_node=NULL; \ + l->count=0; \ + return l; \ +} \ +\ +void LinkedList_##TYPE##_freeMembers(void* _l){ \ + LinkedList(TYPE)* l=(LinkedList(TYPE)*)_l; \ + if(l->first_node!=NULL) \ + LinkedList_foreach(l, node, LLNode_##TYPE##_free(node, true)); \ + l->first_node=NULL; l->last_node=NULL; l->count=0; \ +} \ +\ +void LinkedList_##TYPE##_insertPrev(LinkedList(TYPE)* llist, LLNode(TYPE)* newNode, LLNode(TYPE)* nextNode){ \ + llist->count++; \ + newNode->prev=nextNode->prev; \ + newNode->next=nextNode; \ + nextNode->prev=newNode; \ +} \ +\ +void LinkedList_##TYPE##_insertNext(LinkedList(TYPE)* llist, LLNode(TYPE)* newNode, LLNode(TYPE)* prevNode){ \ + llist->count++; \ + newNode->prev=prevNode; \ + newNode->next=prevNode->next; \ + prevNode->next=newNode; \ +} \ +\ +void LinkedList_##TYPE##_removePrev(LinkedList(TYPE)* llist, LLNode(TYPE)* nextNode, bool freeRemoved){ \ + llist->count--; \ + LLNode(TYPE)* removedNode=nextNode->prev; \ + LLNode(TYPE)* prevNode=removedNode->prev; \ + nextNode->prev=prevNode; \ + prevNode->next=nextNode; \ + if(freeRemoved) \ + LLNode_##TYPE##_free(removedNode, true); \ +} \ +\ +void LinkedList_##TYPE##_removeNext(LinkedList(TYPE)* llist, LLNode(TYPE)* prevNode, bool freeRemoved){ \ + llist->count--; \ + LLNode(TYPE)* removedNode=prevNode->next; \ + LLNode(TYPE)* nextNode=removedNode->next; \ + prevNode->next=nextNode; \ + nextNode->prev=prevNode; \ + if(freeRemoved) \ + LLNode_##TYPE##_free(removedNode, true); \ +} \ +\ +LinkedList_##TYPE##_functions_t _LinkedList_##TYPE##_functions={ \ + .freeMembers=LinkedList_##TYPE##_freeMembers, \ + .insertPrev=LinkedList_##TYPE##_insertPrev, \ + .insertNext=LinkedList_##TYPE##_insertNext, \ + .removePrev=LinkedList_##TYPE##_removePrev, \ + .removeNext=LinkedList_##TYPE##_removeNext \ +}; \ +\ +kt_define(LinkedList_##TYPE, LinkedList_##TYPE##_freeMembers, NULL) + + +#if __cplusplus +} +#endif From 6c6c4fff8b7d53f21e61f9c19b3773fb0361afa4 Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 15 Mar 2023 05:11:03 +0600 Subject: [PATCH 28/55] File* -> FileHandle (fix of sizeof(File)) --- cbuild | 2 +- src/Filesystem/file.c | 20 ++++++++++---------- src/Filesystem/file.h | 20 ++++++++++---------- src/base/type_system/init.c | 2 +- 4 files changed, 22 insertions(+), 22 deletions(-) diff --git a/cbuild b/cbuild index 112fcc0..9f04507 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit 112fcc04652d6ce65fbde215cd3d1f8935db2a7c +Subproject commit 9f04507bd880343b5a99194d5c744879cdb9efe4 diff --git a/src/Filesystem/file.c b/src/Filesystem/file.c index 6a3358c..d6dfb7c 100644 --- a/src/Filesystem/file.c +++ b/src/Filesystem/file.c @@ -2,7 +2,7 @@ #include "../String/StringBuilder.h" #include "io_includes.h" -kt_define(File, (freeMembers_t)file_close, NULL) +kt_define(FileHandle, (freeMembers_t)file_close, NULL) bool file_exists(const char* path){ if(path[0]=='.'){ @@ -47,13 +47,13 @@ char* FileOpenMode_toStr(FileOpenMode m){ } Maybe file_open(const char* path, FileOpenMode mode){ - File* file=fopen(path, FileOpenMode_toStr(mode)); + FileHandle file=fopen(path, FileOpenMode_toStr(mode)); if(!file) safethrow(cptr_concat("can't open file ", (char*)path),;); - return SUCCESS(UniHeapPtr(File,file)); + return SUCCESS(UniHeapPtr(FileHandle,file)); } -Maybe file_close(File* file){ +Maybe file_close(FileHandle file){ if(!file) safethrow(ERR_NULLPTR,;); if(fclose(file)) @@ -67,13 +67,13 @@ Maybe file_close(File* file){ if(rezult!=0) \ safethrow(ERR_IO,;); -Maybe file_writeChar(File* file, char byte){ +Maybe file_writeChar(FileHandle file, char byte){ i32 rezult=fputc(byte, file); ioWriteCheck(); return MaybeNull; } -Maybe file_writeBuffer(File* file, char* buffer, u64 length){ +Maybe file_writeBuffer(FileHandle file, char* buffer, u64 length){ i32 rezult=0; for(u64 i=0; i +/// @return Maybe Maybe file_open(const char* path, FileOpenMode mode); /// @brief closes file descriptor /// @return Maybe -Maybe file_close(File* file); +Maybe file_close(FileHandle file); /// @brief closes file descriptor /// @param byte byte to write /// @return Maybe -Maybe file_writeChar(File* file, char byte); +Maybe file_writeChar(FileHandle file, char byte); /// @brief closes file descriptor /// @param buffer bytes to write /// @param length buffer length /// @return Maybe -Maybe file_writeBuffer(File* file, char* buffer, u64 length); +Maybe file_writeBuffer(FileHandle file, char* buffer, u64 length); /// @brief writes all cstring array content to file /// @param cptr zero-terminated cstring /// @return Maybe -Maybe file_writeCptr(File* file, char* cptr); +Maybe file_writeCptr(FileHandle file, char* cptr); /// @brief reads single byte from file /// @return Maybe -Maybe file_readChar(File* file); +Maybe file_readChar(FileHandle file); /// @brief reads byte array of specofied length /// @param buffer buffer that will be filled with file bytes /// @param length buffer length /// @return Maybe total number of successfully read bytes (<=length) -Maybe file_readBuffer(File* file, char* buffer, u64 length); +Maybe file_readBuffer(FileHandle file, char* buffer, u64 length); /// @brief reads all bytes from file /// @param allBytes ptr to the file's content will be pushed there /// @return Maybe total number of successfully read bytes -Maybe file_readAll(File* file, char** allBytes); +Maybe file_readAll(FileHandle file, char** allBytes); #if __cplusplus } diff --git a/src/base/type_system/init.c b/src/base/type_system/init.c index fc5ba12..424b759 100644 --- a/src/base/type_system/init.c +++ b/src/base/type_system/init.c @@ -83,5 +83,5 @@ void kt_initKerepTypes(){ kt_register(StringBuilder); //File - kt_register(File); + kt_register(FileHandle); } From a200f2c96563f5782ed7b56de5a5889b7d1c31cd Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 15 Mar 2023 05:52:33 +0600 Subject: [PATCH 29/55] LinkedList new functions and fixes --- src/LinkedList/LinkedList.c | 65 +++++++++++++++++++++++++++++ src/LinkedList/LinkedList.h | 18 ++++++-- src/LinkedList/LinkedList_declare.h | 5 +-- src/LinkedList/LinkedList_define.h | 29 +------------ 4 files changed, 81 insertions(+), 36 deletions(-) diff --git a/src/LinkedList/LinkedList.c b/src/LinkedList/LinkedList.c index a6302ed..26732c6 100644 --- a/src/LinkedList/LinkedList.c +++ b/src/LinkedList/LinkedList.c @@ -1,3 +1,68 @@ #include "LinkedList.h" LinkedList_define(Pointer, true) + +void LinkedList_addToBeginning(void* _llist, void* _new_node) { + LinkedList(Pointer)* llist=_llist; + LLNode(Pointer)* new_node=_new_node; + llist->count++; + if(llist->last_node==NULL){ + if(llist->first_node!=NULL) + throw(ERR_NULLPTR); // last_node can't be null if first_node != null + llist->last_node=new_node; + } + else { + llist->first_node->prev=new_node; + new_node->next=llist->first_node; + } + llist->first_node=new_node; +} + +void LinkedList_addToEnd(void* _llist, void* _new_node) { + LinkedList(Pointer)* llist=_llist; + LLNode(Pointer)* new_node=_new_node; + llist->count++; + if(llist->first_node==NULL) { + if(llist->last_node!=NULL) + throw(ERR_NULLPTR); // first_node can't be null if last_node != null + llist->first_node=new_node; + } + else { + llist->last_node->next=new_node; + new_node->prev=llist->last_node; + } + llist->last_node=new_node; +} + +static inline void _insertNode(LinkedList(Pointer)* llist, + LLNode(Pointer)* prev_node, LLNode(Pointer)* new_node, LLNode(Pointer)* next_node){ + if(prev_node==NULL){ + if(next_node==llist->first_node) + LinkedList_addToBeginning(llist, new_node); + else throw(ERR_NULLPTR); // prev_node is null, but it isn't insertion before first_node + } + else if(next_node==NULL){ + if(prev_node==llist->last_node) + LinkedList_addToEnd(llist, new_node); + else throw(ERR_NULLPTR); // next_node is null, but it isn't insertion after last_node + } + else { + prev_node->next=new_node; + new_node->prev=prev_node; + new_node->next=next_node; + next_node->prev=new_node; + llist->count++; + } +} + +void LinkedList_insertPrev(void* _llist, void* _new_node, void* _next_node){ + LLNode(Pointer)* next_node=_next_node; + LLNode(Pointer)* prev_node=next_node->prev; + _insertNode(_llist, prev_node, _new_node, next_node); +} + +void LinkedList_insertNext(void* _llist, void* _new_node, void* _prev_node){ + LLNode(Pointer)* prev_node=_prev_node; + LLNode(Pointer)* next_node=prev_node->next; + _insertNode(_llist, prev_node, _new_node, next_node); +} diff --git a/src/LinkedList/LinkedList.h b/src/LinkedList/LinkedList.h index 6283ad7..441d6bf 100644 --- a/src/LinkedList/LinkedList.h +++ b/src/LinkedList/LinkedList.h @@ -13,14 +13,20 @@ extern "C" { #define LLNode(TYPE) LLNode_##TYPE #define LinkedList(TYPE) LinkedList_##TYPE +#define LLNode_create(TYPE, VALUE) LLNode_##TYPE##_create(VALUE) + #define LinkedList_create(TYPE) LinkedList_##TYPE##_create() #define LinkedList_free(LLIST) ({ LLIST->_functions->freeMembers(LLIST); free(LLIST); }) + +void LinkedList_addToBeginning(void* _llist, void* _new_node); +void LinkedList_addToEnd(void* _llist, void* _new_node); + /// inserts NEW_NODE before NEXT_NODE in LLIST -#define LinkedList_insertPrev(LLIST, NEW_NODE, NEXT_NODE) LLIST->_functions->insertPrev(LLIST, NEW_NODE, NEXT_NODE) +void LinkedList_insertPrev(void* _llist, void* _new_node, void* _next_node); /// inserts NEW_NODE after PREV_NODE in LLIST -#define LinkedList_insertNext(LLIST, NEW_NODE, PREV_NODE) LLIST->_functions->insertNext(LLIST, NEW_NODE, PREV_NODE) +void LinkedList_insertNext(void* _llist, void* _new_node, void* _prev_node); /// removes node before NEXT_NODE in LLIST /// if FREE_REMOVED then frees removed node @@ -35,9 +41,11 @@ extern "C" { ///@param CODE code todo in every iteration #define LLNode_foreach(FIRST_N, CURR_N, CODE...) { \ typeof(FIRST_N) CURR_N=FIRST_N; \ + typeof(FIRST_N) NEXT_N=FIRST_N; \ while(CURR_N!=NULL) { \ + NEXT_N=CURR_N->next; \ CODE; \ - CURR_N=CURR_N->next; \ + CURR_N=NEXT_N; \ } \ } @@ -46,9 +54,11 @@ extern "C" { ///@param CODE code todo in every iteration #define LLNode_foreachReverse(FIRST_N, CURR_N, CODE...) { \ typeof(FIRST_N) CURR_N=FIRST_N; \ + typeof(FIRST_N) PREV_N=FIRST_N; \ while(CURR_N!=NULL) { \ + PREV_N=CURR_N->prev; \ CODE; \ - CURR_N=CURR_N->prev; \ + CURR_N=PREV_N; \ } \ } diff --git a/src/LinkedList/LinkedList_declare.h b/src/LinkedList/LinkedList_declare.h index 418a722..a0f4c9e 100644 --- a/src/LinkedList/LinkedList_declare.h +++ b/src/LinkedList/LinkedList_declare.h @@ -11,8 +11,7 @@ STRUCT(LLNode(TYPE), \ TYPE value; \ ) \ \ -LLNode(TYPE)* LLNode_##TYPE##_create(); \ -LLNode(TYPE)* LLNode_##TYPE##_createFromValue(TYPE value); \ +LLNode(TYPE)* LLNode_##TYPE##_create(TYPE value); \ void LLNode_##TYPE##_free(LLNode(TYPE)* node, bool free_value); @@ -29,8 +28,6 @@ STRUCT(LinkedList(TYPE), \ \ typedef struct LinkedList_##TYPE##_functions_t { \ freeMembers_t freeMembers; \ - void (*insertPrev)(LinkedList(TYPE)* llist, LLNode(TYPE)* newNode, LLNode(TYPE)* nextNode); \ - void (*insertNext)(LinkedList(TYPE)* llist, LLNode(TYPE)* newNode, LLNode(TYPE)* prevNode); \ void (*removePrev)(LinkedList(TYPE)* llist, LLNode(TYPE)* nextNode, bool freeRemoved); \ void (*removeNext)(LinkedList(TYPE)* llist, LLNode(TYPE)* prevNode, bool freeRemoved); \ } LinkedList_##TYPE##_functions_t; \ diff --git a/src/LinkedList/LinkedList_define.h b/src/LinkedList/LinkedList_define.h index 2ea4e16..f978638 100644 --- a/src/LinkedList/LinkedList_define.h +++ b/src/LinkedList/LinkedList_define.h @@ -5,15 +5,8 @@ extern "C" { #endif #define LLNode_define(TYPE, TYPE_IS_PTR)\ -LLNode(TYPE)* LLNode_##TYPE##_create(){ \ - LLNode(TYPE)* node= (LLNode(TYPE)*)malloc(sizeof(*node)); \ - node->prev=NULL; \ - node->next=NULL; \ - node->value=(TYPE){0}; \ - return node; \ -} \ \ -LLNode(TYPE)* LLNode_##TYPE##_createFromValue(TYPE value){ \ +LLNode(TYPE)* LLNode_##TYPE##_create(TYPE value){ \ LLNode(TYPE)* node= (LLNode(TYPE)*)malloc(sizeof(*node)); \ node->prev=NULL; \ node->next=NULL; \ @@ -26,14 +19,10 @@ void LLNode_##TYPE##_freeMembers(void* _node){ \ void* value_ptr=&node->value; \ if(TYPE_IS_PTR) value_ptr=*(TYPE**)value_ptr; \ ktDescriptor_##TYPE.freeMembers(value_ptr); \ - if(node->prev) node->prev->next=NULL; \ - if(node->next) node->next->prev=NULL; \ } \ \ void LLNode_##TYPE##_free(LLNode(TYPE)* node, bool free_value){ \ if(free_value) LLNode_##TYPE##_freeMembers(node); \ - if(node->prev) node->prev->next=NULL; \ - if(node->next) node->next->prev=NULL; \ free(node); \ } \ \ @@ -59,20 +48,6 @@ void LinkedList_##TYPE##_freeMembers(void* _l){ \ l->first_node=NULL; l->last_node=NULL; l->count=0; \ } \ \ -void LinkedList_##TYPE##_insertPrev(LinkedList(TYPE)* llist, LLNode(TYPE)* newNode, LLNode(TYPE)* nextNode){ \ - llist->count++; \ - newNode->prev=nextNode->prev; \ - newNode->next=nextNode; \ - nextNode->prev=newNode; \ -} \ -\ -void LinkedList_##TYPE##_insertNext(LinkedList(TYPE)* llist, LLNode(TYPE)* newNode, LLNode(TYPE)* prevNode){ \ - llist->count++; \ - newNode->prev=prevNode; \ - newNode->next=prevNode->next; \ - prevNode->next=newNode; \ -} \ -\ void LinkedList_##TYPE##_removePrev(LinkedList(TYPE)* llist, LLNode(TYPE)* nextNode, bool freeRemoved){ \ llist->count--; \ LLNode(TYPE)* removedNode=nextNode->prev; \ @@ -95,8 +70,6 @@ void LinkedList_##TYPE##_removeNext(LinkedList(TYPE)* llist, LLNode(TYPE)* prevN \ LinkedList_##TYPE##_functions_t _LinkedList_##TYPE##_functions={ \ .freeMembers=LinkedList_##TYPE##_freeMembers, \ - .insertPrev=LinkedList_##TYPE##_insertPrev, \ - .insertNext=LinkedList_##TYPE##_insertNext, \ .removePrev=LinkedList_##TYPE##_removePrev, \ .removeNext=LinkedList_##TYPE##_removeNext \ }; \ From a6a6d2d4ae71f2055699cc602a750268e8a33a7b Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 15 Mar 2023 16:54:36 +0600 Subject: [PATCH 30/55] -Wextra warnings fixed --- src/Array/Array.c | 2 +- src/Autoarr/Autoarr.h | 4 ++-- src/DtsodParser/DtsodV24_deserialize.c | 2 ++ src/DtsodParser/DtsodV24_serialize.c | 2 +- src/Filesystem/file.c | 4 +++- src/Filesystem/path.c | 6 +++--- src/Filesystem/path.h | 2 +- src/base/cptr.c | 8 ++++---- src/base/cptr.h | 2 +- src/base/std.h | 3 ++- src/base/type_system/base_toString.c | 4 ++-- src/kprint/kprintf.c | 2 +- 12 files changed, 23 insertions(+), 18 deletions(-) diff --git a/src/Array/Array.c b/src/Array/Array.c index 2d293c2..63d1ead 100644 --- a/src/Array/Array.c +++ b/src/Array/Array.c @@ -17,7 +17,7 @@ Array_define(Pointer) Array_define(Unitype) void Array_Unitype_free_(Array_Unitype* array, bool freeMembers){ - if(freeMembers) for (i32 i=0; ilength; i++) + if(freeMembers) for (u32 i=0; ilength; i++) Unitype_free(array->values[i]); if(array->allocatedOnHeap) free(array->values); diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h index 405e018..45cf50c 100644 --- a/src/Autoarr/Autoarr.h +++ b/src/Autoarr/Autoarr.h @@ -26,12 +26,12 @@ Autoarr_declare(Unitype) #define Autoarr_foreach(ar,elem,codeblock)({ \ if(ar->blocks_count>0) { \ typeof(**ar->values) elem; \ - for(u32 blockI=0;blockIblocks_count-1;blockI++) \ + for(u16 blockI=0;blockIblocks_count-1;blockI++) \ for(u32 elemI=0;elemImax_block_length;elemI++){ \ elem=ar->values[blockI][elemI]; \ (codeblock); \ } \ - for(u32 elemI=0;elemIblock_length;elemI++){ \ + for(u16 elemI=0;elemIblock_length;elemI++){ \ elem=ar->values[ar->blocks_count-1][elemI]; \ (codeblock); \ } \ diff --git a/src/DtsodParser/DtsodV24_deserialize.c b/src/DtsodParser/DtsodV24_deserialize.c index c992909..b755579 100644 --- a/src/DtsodParser/DtsodV24_deserialize.c +++ b/src/DtsodParser/DtsodV24_deserialize.c @@ -272,8 +272,10 @@ Maybe __ReadValue(DeserializeSharedData* shared, bool* readingList){ case ']': if(!readingList) safethrow_wrongchar(c,Unitype_free(value)); *readingList=false; + goto return_value; case ';': case ',': + return_value: if(valueStr.length!=0){ if(!Unitype_isUniNull(value)) safethrow_wrongchar(c,Unitype_free(value)); diff --git a/src/DtsodParser/DtsodV24_serialize.c b/src/DtsodParser/DtsodV24_serialize.c index ac0b1f9..99804a7 100644 --- a/src/DtsodParser/DtsodV24_serialize.c +++ b/src/DtsodParser/DtsodV24_serialize.c @@ -77,7 +77,7 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){ bool hashtableNotBlank=false; Hashtable_foreach(((Hashtable*)u.VoidPtr), __, ({ hashtableNotBlank=true; - if(__.key); // weird way to disable warning + if(__.key) {} // weird way to disable warning break; })); diff --git a/src/Filesystem/file.c b/src/Filesystem/file.c index d6dfb7c..5c38523 100644 --- a/src/Filesystem/file.c +++ b/src/Filesystem/file.c @@ -2,7 +2,9 @@ #include "../String/StringBuilder.h" #include "io_includes.h" -kt_define(FileHandle, (freeMembers_t)file_close, NULL) +void __file_freeMembers(void* _f){ fclose((FileHandle)_f); } + +kt_define(FileHandle, __file_freeMembers, NULL) bool file_exists(const char* path){ if(path[0]=='.'){ diff --git a/src/Filesystem/path.c b/src/Filesystem/path.c index ef977ce..cc5cb6e 100644 --- a/src/Filesystem/path.c +++ b/src/Filesystem/path.c @@ -1,6 +1,6 @@ #include "filesystem.h" -char* __path_concat(u16 n, ...){ +char* __path_concat(u32 n, ...){ char** parts=(char**)malloc(n*sizeof(char*)); u32* lengths=malloc(n*sizeof(u32)); u32 totalLength=0; @@ -56,9 +56,9 @@ Maybe path_throwIfEscapes(const char* path){ char* path_parentDir(char* dir){ char* copy=cptr_copy(dir); - u32 length=cptr_length(copy); + i32 length=cptr_length(copy); i32 i=cptr_lastIndexOfChar(copy,path_sep); - if(i==length-1){ + if(i!=-1 && i==length-1){ copy[length-1]=0; i=cptr_lastIndexOfChar(copy,path_sep); } diff --git a/src/Filesystem/path.h b/src/Filesystem/path.h index 29571aa..65e0f31 100644 --- a/src/Filesystem/path.h +++ b/src/Filesystem/path.h @@ -14,7 +14,7 @@ static const char path_sep='/'; static const char path_notSep='\\'; #endif -char* __path_concat(u16 n, ...); +char* __path_concat(u32 n, ...); /// @brief merges path parts together and puts between them /// @return new cstr #define path_concat(PATH_PARTS...) __path_concat(count_args(PATH_PARTS), PATH_PARTS) diff --git a/src/base/cptr.c b/src/base/cptr.c index d2b07be..ecf4eb4 100644 --- a/src/base/cptr.c +++ b/src/base/cptr.c @@ -50,7 +50,7 @@ bool cptr_endsWith(char* ptr, char* fragment){ u32 cptr_indexOf(char* ptr, char* fragment){ char sc=*ptr; - for(i32 si=0, fi=0; sc!=0; si++){ + for(u32 si=0, fi=0; sc!='\0'; si++){ sc=ptr[si]; if(sc==fragment[fi]){ fi++; @@ -63,7 +63,7 @@ u32 cptr_indexOf(char* ptr, char* fragment){ } u32 cptr_indexOfChar(char* ptr, char fragment){ char sc=*ptr; - for(i32 si=0; sc!=0; si++){ + for(u32 si=0; sc!='\0'; si++){ sc=ptr[si]; if(sc==fragment){ return si; @@ -73,7 +73,7 @@ u32 cptr_indexOfChar(char* ptr, char fragment){ } u32 cptr_lastIndexOf(char* ptr, char* fragment){ char sc=*ptr; - i32 fi_last=cptr_length(fragment)-1; + u32 fi_last=cptr_length(fragment)-1; for(i32 si=cptr_length(ptr)-1, fi=fi_last; si>=0; si--){ sc=ptr[si]; if(sc==fragment[fi]){ @@ -103,7 +103,7 @@ void memcopy(void* from, void* to, u32 size){ ((char*)to)[i]=((char*)from)[i]; } -char* __cptr_concat(u16 n, ...){ +char* __cptr_concat(u32 n, ...){ char** strs=(char**)malloc(n*sizeof(char*)); u32* lengths=malloc(n*sizeof(u32)); u32 totalLength=0; diff --git a/src/base/cptr.h b/src/base/cptr.h index 55b087c..fbd6dd8 100644 --- a/src/base/cptr.h +++ b/src/base/cptr.h @@ -44,7 +44,7 @@ static inline bool cptr_contains(char* ptr, char* fragment){ void memcopy(void* from, void* to, u32 size); -char* __cptr_concat(u16 n, ...); +char* __cptr_concat(u32 n, ...); #define cptr_concat(STR...) __cptr_concat(count_args(STR), STR) #if __cplusplus diff --git a/src/base/std.h b/src/base/std.h index 14e85c0..4e5bda9 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -116,8 +116,9 @@ You can even embed it into macro in header (see kprint.h) #define PRAGMA_WARNING_DISABLE(wName) _PRAGMA(GCC diagnostic ignored wName) #define PRAGMA_WARNING_POP _PRAGMA(GCC diagnostic pop) #define W_INT_CONVERSION "-Wint-conversion" + #define W_IMPLICIT_FALLTHROUGH "-Wimplicit-fallthrough" #endif -#define WARNING_DISABLE(WARNING, CODE) \ +#define WARNING_DISABLE(WARNING, CODE...) \ PRAGMA_WARNING_PUSH \ PRAGMA_WARNING_DISABLE(WARNING) \ CODE; \ diff --git a/src/base/type_system/base_toString.c b/src/base/type_system/base_toString.c index 2fc2371..b63d82d 100644 --- a/src/base/type_system/base_toString.c +++ b/src/base/type_system/base_toString.c @@ -113,7 +113,7 @@ char* toString_bin(void* _bytes, u32 size, bool inverse, bool withPrefix){ for(i32 bn=size-1; bn>=0; bn--) byte_to_bits(bytes[bn]) } else { - for(i32 bn=0; bn Date: Wed, 15 Mar 2023 18:02:22 +0600 Subject: [PATCH 31/55] cbuild --- Makefile | 21 ++++++++++---- cbuild | 2 +- default.config | 76 +++++++++++++++++++++++++++++++++----------------- 3 files changed, 67 insertions(+), 32 deletions(-) diff --git a/Makefile b/Makefile index 7f1ba15..0bd17cd 100644 --- a/Makefile +++ b/Makefile @@ -4,12 +4,8 @@ all: build_exec_dbg -# generates different profile info -build_profile: - @cbuild/call_task.sh build_profile 2>&1 | tee make_raw.log - -# creates executable using profile info generated by build_profile -build_exec: build_profile +# creates executable using profiling info generated by profile +build_exec: profile @cbuild/call_task.sh build_exec 2>&1 | tee -a make_raw.log # creates executable with debug info and no optimizations @@ -48,6 +44,19 @@ exec_dbg: build_exec_dbg valgrind: build_exec_dbg @cbuild/call_task.sh valgrind 2>&1 | tee -a make_raw.log +# generates profiling info +profile: + @cbuild/call_task.sh profile 2>&1 | tee make_raw.log + +# compiles program with -pg and runs it with gprof +# uses gprof2dot python script to generate function call tree +gprof: + @cbuild/call_task.sh gprof 2>&1 | tee make_raw.log + +# compiles executable with sanitizers and executes it to find errors and warnings +sanitize: + @cbuild/call_task.sh sanitize 2>&1 | tee make_raw.log + ###################################### ###### Other tasks ####### ###################################### diff --git a/cbuild b/cbuild index 9f04507..ec42098 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit 9f04507bd880343b5a99194d5c744879cdb9efe4 +Subproject commit ec4209831b25876b3ad95db89eb4034dbb69d38e diff --git a/default.config b/default.config index a9313e2..6958b20 100644 --- a/default.config +++ b/default.config @@ -1,14 +1,14 @@ #!/bin/bash -CBUILD_VERSION=5 -CONFIG_VERSION=5 +CBUILD_VERSION=6 +CONFIG_VERSION=6 PROJECT="kerep" CMP_C="gcc" CMP_CPP="g++" STD_C="c11" -STD_CPP="c++17" -WARN_C="-Wall -Wno-discarded-qualifiers" -WARN_CPP="-Wall" +STD_CPP="c++11" +WARN_C="-Wall -Wno-discarded-qualifiers -Wextra -Wno-unused-parameter" +WARN_CPP="-Wall -Wextra -Wno-unused-parameter" SRC_C="$( find src -name '*.c')" SRC_CPP="$( find src -name '*.cpp')" TESTS_C="$( find tests -name '*.c')" @@ -40,26 +40,13 @@ esac # TASKS case "$TASK" in - # generates different profile info - build_profile) - OUTDIR="$OUTDIR/profile" - # -flto applies more optimizations across object files - # -flto=auto is needed to multithreaded copilation - # -fuse-linker-plugin is required to use static libs with lto, it strips away all - # -pg adds code to executable, that generates file containing function call info (gmon.out) - # -fprofile-generate - C_ARGS="-O2 -flto=auto -fuse-linker-plugin -pg -fprofile-generate -fprofile-prefix-path=$(realpath $OBJDIR)/objects" - CPP_ARGS="$C_ARGS" - LINKER_ARGS="$CPP_ARGS" - PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh - TASK_SCRIPT=cbuild/default_tasks/profile.sh - POST_TASK_SCRIPT= - ;; - # creates executable using profile info generated by build_profile + # creates executable using profiling info if it exists build_exec) # -flto applies more optimizations across object files # -flto=auto is needed to multithreaded copilation - # -fuse-linker-plugin is required to use static libs with lto, it strips away all + # -fuse-linker-plugin is required to use static libs with lto + # -fprofile-use enables compiler to use profiling info files to optimize executable + # -fprofile-prefix-path sets path where profiling info about objects are be saved C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects" CPP_ARGS="$C_ARGS" LINKER_ARGS="$CPP_ARGS" @@ -69,7 +56,7 @@ case "$TASK" in ;; # creates executable with debug info and no optimizations build_exec_dbg) - C_ARGS="-O0 -g" + C_ARGS="-O0 -g3" CPP_ARGS="$C_ARGS" LINKER_ARGS="$CPP_ARGS" PRE_TASK_SCRIPT= @@ -87,7 +74,7 @@ case "$TASK" in ;; # creates shared library with debug symbols and no optimizations build_shared_lib_dbg) - C_ARGS="-O0 -g -fpic -shared" + C_ARGS="-O0 -g3 -fpic -shared" CPP_ARGS="$C_ARGS" LINKER_ARGS="$CPP_ARGS -Wl,-soname,$SHARED_LIB_FILE" PRE_TASK_SCRIPT= @@ -104,7 +91,7 @@ case "$TASK" in ;; # creates static library with debug symbols and no optimizations build_static_lib_dbg) - C_ARGS="-O0 -g" + C_ARGS="-O0 -g3" CPP_ARGS="$C_ARGS" PRE_TASK_SCRIPT= TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh @@ -119,6 +106,45 @@ case "$TASK" in VALGRIND_ARGS="-s --log-file=valgrind.log --read-var-info=yes --track-origins=yes --fullpath-after=$PROJECT/ --leak-check=full --show-leak-kinds=all" TASK_SCRIPT=cbuild/default_tasks/valgrind.sh ;; + # generates profiling info + profile) + OUTDIR="$OUTDIR/profile" + # -flto applies more optimizations across object files + # -flto=auto is needed to multithreaded copilation + # -fuse-linker-plugin is required to use static libs with lto + # -pg adds code to executable, that generates file containing function call info (gmon.out) + # -fprofile-generate generates executable with profiling code + # -fprofile-prefix-path sets path where profiling info about objects will be saved + C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-generate -fprofile-prefix-path=$(realpath $OBJDIR)/objects" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="$CPP_ARGS" + PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + TASK_SCRIPT=cbuild/default_tasks/profile.sh + POST_TASK_SCRIPT= + ;; + # compiles program with -pg and runs it with gprof + # uses gprof2dot python script to generate function call tree (pip install gprof2dot) + # requires graphviz (https://www.graphviz.org/download/source/) + gprof) + OUTDIR="$OUTDIR/gprof" + # -pg adds code to executable, that generates file containing function call info (gmon.out) + C_ARGS="-O2 -flto=auto -fuse-linker-plugin -pg" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="$CPP_ARGS" + PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + TASK_SCRIPT=cbuild/default_tasks/gprof.sh + POST_TASK_SCRIPT= + ;; + # compiles executable with sanitizers and executes it to find errors and warnings + sanitize) + OUTDIR="bin/sanitize" + C_ARGS="-O0 -g3 -fsanitize=undefined,address" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="$CPP_ARGS" + PRE_TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + TASK_SCRIPT=cbuild/default_tasks/exec.sh + POST_TASK_SCRIPT= + ;; # deletes generated files clean) TASK_SCRIPT=cbuild/default_tasks/clean.sh From a2906e2c3aba5d3f94f9eea45bc3e6faed293a7b Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 15 Mar 2023 18:10:23 +0600 Subject: [PATCH 32/55] small fix --- cbuild | 2 +- default.config | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cbuild b/cbuild index ec42098..ca58141 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit ec4209831b25876b3ad95db89eb4034dbb69d38e +Subproject commit ca58141d881482a26e12a390f050570061ab7731 diff --git a/default.config b/default.config index 6958b20..1140a92 100644 --- a/default.config +++ b/default.config @@ -137,7 +137,7 @@ case "$TASK" in ;; # compiles executable with sanitizers and executes it to find errors and warnings sanitize) - OUTDIR="bin/sanitize" + OUTDIR="$OUTDIR/sanitize" C_ARGS="-O0 -g3 -fsanitize=undefined,address" CPP_ARGS="$C_ARGS" LINKER_ARGS="$CPP_ARGS" From 20537813ecbd08041318e832d645a982c09ac7a1 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Wed, 29 Mar 2023 20:01:37 +0600 Subject: [PATCH 33/55] cbuild --- cbuild | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cbuild b/cbuild index ca58141..d72aa0c 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit ca58141d881482a26e12a390f050570061ab7731 +Subproject commit d72aa0c8098fe2a427e50355250f473d70886502 From 9938893ac15165e206b2a3af48701c25d1124765 Mon Sep 17 00:00:00 2001 From: timerix Date: Sat, 1 Apr 2023 13:52:06 +0600 Subject: [PATCH 34/55] more stdlib includes --- cbuild | 2 +- src/base/std.h | 3 +++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/cbuild b/cbuild index d72aa0c..2bebe76 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit d72aa0c8098fe2a427e50355250f473d70886502 +Subproject commit 2bebe76c7e14155392a167bdda61113d0039e188 diff --git a/src/base/std.h b/src/base/std.h index 4e5bda9..ca57fbc 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -8,9 +8,12 @@ extern "C" { #include #include #include +#include #include #include #include +#include +#include typedef int8_t i8; typedef uint8_t u8; From 64634c23711c20805da8f6db796bd54ab5b9b630 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Sun, 2 Apr 2023 20:54:26 +0600 Subject: [PATCH 35/55] enabled sprintf_s macro --- src/base/std.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/base/std.h b/src/base/std.h index ca57fbc..816d328 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -75,7 +75,7 @@ typedef u8 bool; #endif #ifndef sprintf_s - //#define sprintf_s(BUF, BUFSIZE, FORMAT, ...) sprintf(BUF, FORMAT, ## __VA_ARGS__) + #define sprintf_s(BUF, BUFSIZE, FORMAT, ...) sprintf(BUF, FORMAT, ## __VA_ARGS__) #endif From f6864de2c9c3a16c5ac1b56cdf43073490b81a28 Mon Sep 17 00:00:00 2001 From: timerix Date: Tue, 2 May 2023 19:51:48 +0600 Subject: [PATCH 36/55] cbuild --- cbuild | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cbuild b/cbuild index 2bebe76..e83a7af 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit 2bebe76c7e14155392a167bdda61113d0039e188 +Subproject commit e83a7affefe85ec7573ce7465e543747bbf5d477 From 6aaf270aad6dec2accfa49addc8c6fa5cbe8b337 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Thu, 18 May 2023 07:11:06 +0600 Subject: [PATCH 37/55] small kprint header change --- src/kprint/kprint.h | 4 ---- src/kprint/kprint_format.h | 4 ++++ 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/kprint/kprint.h b/src/kprint/kprint.h index aa591e7..69cc63c 100644 --- a/src/kprint/kprint.h +++ b/src/kprint/kprint.h @@ -5,7 +5,6 @@ extern "C" { #endif #include "../base/errors.h" -#include "kprint_colors.h" #include "kprint_format.h" /* @@ -97,9 +96,6 @@ void __kprint(u8 n, kp_fmt* formats, __kp_value_union* objects); #define kprint(ARGS...) WARNING_DISABLE( W_INT_CONVERSION, \ __kprint(count_args(ARGS), __kp_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) \ ) - -///@param f bgColor | fgColor -void kprint_setColor(kp_fmt f); #if __cplusplus } diff --git a/src/kprint/kprint_format.h b/src/kprint/kprint_format.h index cd25f92..33e8aea 100644 --- a/src/kprint/kprint_format.h +++ b/src/kprint/kprint_format.h @@ -6,6 +6,7 @@ extern "C" { #include "../base/std.h" #include "../base/type_system/ktid.h" +#include "kprint_colors.h" /// kprint_format typedef u32 kp_fmt; @@ -44,6 +45,9 @@ PACKED_ENUM(kp_dataFmt, #define kp_fmt_dataFormat(FMT) (kp_dataFmt)(FMT&0x000f0000) #define kp_fmt_ktid(FMT) (ktid)(FMT&0x0000ffff) +///@param f bgColor | fgColor +void kprint_setColor(kp_fmt f); + #if __cplusplus } #endif \ No newline at end of file From 5122af98cf350b6ffeb8bee7797a47148e13734d Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Fri, 19 May 2023 13:37:48 +0600 Subject: [PATCH 38/55] basic android compatibility --- cbuild | 2 +- default.config | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cbuild b/cbuild index e83a7af..dc5947f 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit e83a7affefe85ec7573ce7465e543747bbf5d477 +Subproject commit dc5947f92d310f05a8765242bed6b825c043a182 diff --git a/default.config b/default.config index 1140a92..e57a8df 100644 --- a/default.config +++ b/default.config @@ -7,7 +7,7 @@ CMP_C="gcc" CMP_CPP="g++" STD_C="c11" STD_CPP="c++11" -WARN_C="-Wall -Wno-discarded-qualifiers -Wextra -Wno-unused-parameter" +WARN_C="-Wall -Wno-ignored-qualifiers -Wextra -Wno-unused-parameter" WARN_CPP="-Wall -Wextra -Wno-unused-parameter" SRC_C="$( find src -name '*.c')" SRC_CPP="$( find src -name '*.cpp')" From 8ea44cdc24120ce6e13b0a92767e2002123b1bc1 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Fri, 19 May 2023 13:42:14 +0600 Subject: [PATCH 39/55] sybmodule branch --- .gitmodules | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitmodules b/.gitmodules index dd060e8..c0d4562 100644 --- a/.gitmodules +++ b/.gitmodules @@ -1,3 +1,4 @@ [submodule "cbuild"] path = cbuild url = https://github.com/Timerix22/cbuild.git + branch = main From 26b69f9b7bb7a816b8f048557efe0641428b9e80 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Fri, 19 May 2023 14:09:40 +0600 Subject: [PATCH 40/55] config fixed --- cbuild | 2 +- default.config | 9 ++++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/cbuild b/cbuild index dc5947f..e8e4242 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit dc5947f92d310f05a8765242bed6b825c043a182 +Subproject commit e8e42424d329ef1e75fb827ddf85d55b4cd89169 diff --git a/default.config b/default.config index e57a8df..241c3ca 100644 --- a/default.config +++ b/default.config @@ -7,7 +7,7 @@ CMP_C="gcc" CMP_CPP="g++" STD_C="c11" STD_CPP="c++11" -WARN_C="-Wall -Wno-ignored-qualifiers -Wextra -Wno-unused-parameter" +WARN_C="-Wall -Wno-discarded-qualifiers -Wextra -Wno-unused-parameter" WARN_CPP="-Wall -Wextra -Wno-unused-parameter" SRC_C="$( find src -name '*.c')" SRC_CPP="$( find src -name '*.cpp')" @@ -15,10 +15,9 @@ TESTS_C="$( find tests -name '*.c')" TESTS_CPP="$(find tests -name '*.cpp')" # OBJDIR structure: -# ├── objects - dir where compiled *.o files are stored. cleans every call of build task -# ├── profile - dir where gcc *.gcda profiling info files stored -# ├── libs - there you can put static libs and linker will find them -# └── out - output files are created here and then copied to OUTDIR +# ├── objects/ - dir where compiled *.o files are stored. cleans every call of build task +# ├── profile/ - dir where gcc *.gcda profiling info files stored +# └── libs/ - there you can put static libs and linker will find them OBJDIR="obj" OUTDIR="bin" STATIC_LIB_FILE="$PROJECT.a" From f4e666640e722216ac747740644a910ed573365c Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Fri, 19 May 2023 18:42:08 +0600 Subject: [PATCH 41/55] fixed bug in Autoarr_toArray --- src/Autoarr/Autoarr_define.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Autoarr/Autoarr_define.h b/src/Autoarr/Autoarr_define.h index 6c7df17..ec54f7a 100644 --- a/src/Autoarr/Autoarr_define.h +++ b/src/Autoarr/Autoarr_define.h @@ -64,6 +64,8 @@ void ____Autoarr_##type##_freeWithMembers(void* ar){ \ \ type* __Autoarr_##type##_toArray(Autoarr_##type* ar){ \ u32 length=Autoarr_length(ar); \ + if(length==0) \ + return NULL; \ type* array=malloc(length * sizeof(type)); \ for(u32 i=0; i Date: Fri, 19 May 2023 19:44:39 +0600 Subject: [PATCH 42/55] unused code removal during linkage --- cbuild | 2 +- default.config | 6 ++++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/cbuild b/cbuild index e8e4242..991ee07 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit e8e42424d329ef1e75fb827ddf85d55b4cd89169 +Subproject commit 991ee072b3b0e4507c80dfac873c7d41d06f0b69 diff --git a/default.config b/default.config index 241c3ca..8116b7a 100644 --- a/default.config +++ b/default.config @@ -46,7 +46,9 @@ case "$TASK" in # -fuse-linker-plugin is required to use static libs with lto # -fprofile-use enables compiler to use profiling info files to optimize executable # -fprofile-prefix-path sets path where profiling info about objects are be saved - C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects" + # -fdata-sections -ffunction-sections -Wl,--gc-sections removes unused code + C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects \ +-fdata-sections -ffunction-sections -Wl,--gc-sections" CPP_ARGS="$C_ARGS" LINKER_ARGS="$CPP_ARGS" PRE_TASK_SCRIPT= @@ -82,7 +84,7 @@ case "$TASK" in ;; # creates static library build_static_lib) - C_ARGS="-O2 -fpic" + C_ARGS="-O2 -fpic -fdata-sections -ffunction-sections" CPP_ARGS="$C_ARGS" PRE_TASK_SCRIPT= TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh From d7136055d92e90070b947f1662b3fa6981eecdc0 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Fri, 19 May 2023 23:45:29 +0600 Subject: [PATCH 43/55] 0199 --- cbuild | 2 +- default.config | 5 ++--- src/kprint/README.md | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/cbuild b/cbuild index 991ee07..5e23ef8 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit 991ee072b3b0e4507c80dfac873c7d41d06f0b69 +Subproject commit 5e23ef8156a85a981d60152990bde53d6e8eefe9 diff --git a/default.config b/default.config index 8116b7a..6ef5ff7 100644 --- a/default.config +++ b/default.config @@ -47,8 +47,7 @@ case "$TASK" in # -fprofile-use enables compiler to use profiling info files to optimize executable # -fprofile-prefix-path sets path where profiling info about objects are be saved # -fdata-sections -ffunction-sections -Wl,--gc-sections removes unused code - C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects \ --fdata-sections -ffunction-sections -Wl,--gc-sections" + C_ARGS="-O2 -flto=auto -fuse-linker-plugin -fprofile-use -fprofile-prefix-path=$(realpath $OBJDIR)/objects -fdata-sections -ffunction-sections -Wl,--gc-sections" CPP_ARGS="$C_ARGS" LINKER_ARGS="$CPP_ARGS" PRE_TASK_SCRIPT= @@ -104,7 +103,7 @@ case "$TASK" in ;; # executes $EXEC_FILE with valgrind memory checker valgrind) - VALGRIND_ARGS="-s --log-file=valgrind.log --read-var-info=yes --track-origins=yes --fullpath-after=$PROJECT/ --leak-check=full --show-leak-kinds=all" + VALGRIND_ARGS="-s --read-var-info=yes --track-origins=yes --fullpath-after=$PROJECT/ --leak-check=full --show-leak-kinds=all" TASK_SCRIPT=cbuild/default_tasks/valgrind.sh ;; # generates profiling info diff --git a/src/kprint/README.md b/src/kprint/README.md index e1ad83f..0cba7db 100644 --- a/src/kprint/README.md +++ b/src/kprint/README.md @@ -29,7 +29,7 @@ I don't really like printf function (and its variants), so i made safer and more ## how to use it: + **format construction:** ``` - kp_fmt fmt= kp_fgColor | kp_bgColor | kprint_fdataFmt | flags | ktid; + kp_fmt fmt= kp_fgColor | kp_bgColor | kp_dataFmt | flags | ktid; ``` [more about `kp_fmt`](kp_fmt.md) + fgColor and bgColor can be set to change console output color From 461aef3a0062823df886b69ff84f7b61e3d3137a Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Sat, 20 May 2023 01:40:16 +0600 Subject: [PATCH 44/55] simplified Autoarr/Hashtable _foreach --- src/Autoarr/Autoarr.h | 8 ++++---- src/Autoarr/Autoarr_define.h | 4 ++-- src/DtsodParser/DtsodV24_serialize.c | 12 ++++++------ src/Hashtable/Hashtable.h | 4 ++-- src/LinkedList/LinkedList.h | 2 +- src/String/StringBuilder.c | 12 ++++++------ src/base/errors.h | 2 +- src/base/optime.h | 16 ++++++++-------- src/kprint/kprintf.c | 11 +++++++++-- tests/test_autoarr-vs-vector.cpp | 4 ++-- tests/test_autoarr.c | 4 ++-- tests/test_dtsod.c | 24 ++++++++++++------------ tests/test_hash_functions.c | 20 ++++++++++---------- tests/test_hashtable.c | 4 ++-- tests/test_rng_algorithms.c | 6 +++--- tests/test_safethrow.c | 4 ++-- tests/test_searchtree.c | 4 ++-- tests/test_string.c | 4 ++-- tests/tests.h | 4 ++-- 19 files changed, 78 insertions(+), 71 deletions(-) diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h index 45cf50c..b059d00 100644 --- a/src/Autoarr/Autoarr.h +++ b/src/Autoarr/Autoarr.h @@ -23,20 +23,20 @@ Autoarr_declare(u64) Autoarr_declare(Unitype) -#define Autoarr_foreach(ar,elem,codeblock)({ \ +#define Autoarr_foreach(ar, elem, codeblock...) { \ if(ar->blocks_count>0) { \ typeof(**ar->values) elem; \ for(u16 blockI=0;blockIblocks_count-1;blockI++) \ for(u32 elemI=0;elemImax_block_length;elemI++){ \ elem=ar->values[blockI][elemI]; \ - (codeblock); \ + { codeblock; } \ } \ for(u16 elemI=0;elemIblock_length;elemI++){ \ elem=ar->values[ar->blocks_count-1][elemI]; \ - (codeblock); \ + { codeblock; } \ } \ } \ -}) +} #if __cplusplus } diff --git a/src/Autoarr/Autoarr_define.h b/src/Autoarr/Autoarr_define.h index ec54f7a..a7badc4 100644 --- a/src/Autoarr/Autoarr_define.h +++ b/src/Autoarr/Autoarr_define.h @@ -50,11 +50,11 @@ void __Autoarr_##type##_freeWithoutMembers(Autoarr_##type* ar, bool freePtr){ \ \ void __Autoarr_##type##_freeWithMembers(Autoarr_##type* ar, bool freePtr){ \ if(ktDescriptor_##type.freeMembers!=NULL) { \ - Autoarr_foreach(ar, el, ({ \ + Autoarr_foreach(ar, el, \ void* members_ptr=⪙ \ if(TYPE_IS_PTR) members_ptr=*(type**)members_ptr; \ ktDescriptor_##type.freeMembers(members_ptr); \ - })); \ + ); \ } \ __Autoarr_##type##_freeWithoutMembers(ar, freePtr);\ } \ diff --git a/src/DtsodParser/DtsodV24_serialize.c b/src/DtsodParser/DtsodV24_serialize.c index 99804a7..1c81feb 100644 --- a/src/DtsodParser/DtsodV24_serialize.c +++ b/src/DtsodParser/DtsodV24_serialize.c @@ -55,12 +55,12 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){ AppendTabs(); addc('['); tabs++; - Autoarr_foreach(((Autoarr_Unitype*)(u.VoidPtr)), e, ({ + Autoarr_foreach(((Autoarr_Unitype*)(u.VoidPtr)), e, addc('\n'); AppendTabs(); try(AppendValue(e),__,;); addc(','); - })); + ); StringBuilder_rmchar(b); addc('\n'); tabs--; @@ -75,11 +75,11 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){ else if(u.typeId==ktid_ptrName(Hashtable)){ // check hashtable is blank bool hashtableNotBlank=false; - Hashtable_foreach(((Hashtable*)u.VoidPtr), __, ({ + Hashtable_foreach(((Hashtable*)u.VoidPtr), __, hashtableNotBlank=true; if(__.key) {} // weird way to disable warning break; - })); + ); if(hashtableNotBlank){ addc('\n'); @@ -109,7 +109,7 @@ Maybe __serialize(StringBuilder* _b, u8 _tabs, Hashtable* dtsod){ }; SerializeSharedData* shared=&_shared; - Hashtable_foreach(dtsod, p, ({ + Hashtable_foreach(dtsod, p, AppendTabs(); StringBuilder_append_cptr(b,p.key); addc(':'); @@ -117,7 +117,7 @@ Maybe __serialize(StringBuilder* _b, u8 _tabs, Hashtable* dtsod){ try(AppendValue(p.value),__,;); addc(';'); addc('\n'); - })); + ); return MaybeNull; } diff --git a/src/Hashtable/Hashtable.h b/src/Hashtable/Hashtable.h index ba7755f..48c57b8 100644 --- a/src/Hashtable/Hashtable.h +++ b/src/Hashtable/Hashtable.h @@ -33,13 +33,13 @@ Unitype* Hashtable_getPtr(Hashtable* ht, char* key); Unitype Hashtable_get(Hashtable* ht, char* key); bool Hashtable_try_get(Hashtable* ht, char* key, Unitype* output); -#define Hashtable_foreach(HT, EL, codeblock)({ \ +#define Hashtable_foreach(HT, EL, codeblock...) { \ u16 hmax=Hashtable_height(HT); \ for(u16 h=0; hrows[h]; \ Autoarr_foreach(AR, EL, codeblock); \ } \ -}) +} #if __cplusplus } diff --git a/src/LinkedList/LinkedList.h b/src/LinkedList/LinkedList.h index 441d6bf..f8435d6 100644 --- a/src/LinkedList/LinkedList.h +++ b/src/LinkedList/LinkedList.h @@ -16,7 +16,7 @@ extern "C" { #define LLNode_create(TYPE, VALUE) LLNode_##TYPE##_create(VALUE) #define LinkedList_create(TYPE) LinkedList_##TYPE##_create() -#define LinkedList_free(LLIST) ({ LLIST->_functions->freeMembers(LLIST); free(LLIST); }) +#define LinkedList_free(LLIST) ({ LLIST->_functions->freeMembers(LLIST); free(LLIST); }) void LinkedList_addToBeginning(void* _llist, void* _new_node); diff --git a/src/String/StringBuilder.c b/src/String/StringBuilder.c index ead636b..91139e8 100644 --- a/src/String/StringBuilder.c +++ b/src/String/StringBuilder.c @@ -13,9 +13,9 @@ void complete_buf(StringBuilder* b){ if(!len) return; string str={.length=len, .ptr=malloc(len)}; u32 i=0; - Autoarr_foreach(b->curr_buf, c, ({ + Autoarr_foreach(b->curr_buf, c, str.ptr[i++]=c; - })); + ); Autoarr_add(b->compl_bufs,str); Autoarr_free(b->curr_buf, true); b->curr_buf=Autoarr_create(i8,BL_C,BL_L); @@ -47,17 +47,17 @@ void StringBuilder_free(StringBuilder* b){ string StringBuilder_build(StringBuilder* b){ complete_buf(b); u32 len=0; - Autoarr_foreach(b->compl_bufs, cs, ({ + Autoarr_foreach(b->compl_bufs, cs, len+=cs.length; - })); + ); string str= { .length=len, .ptr=malloc(len+1) }; str.ptr[len]='\0'; u32 i=0; - Autoarr_foreach(b->compl_bufs, cs, ({ + Autoarr_foreach(b->compl_bufs, cs, for(u32 n=0;n \e[92m\"%s\"\n",p); free(p); - })); + ); } \ No newline at end of file diff --git a/tests/tests.h b/tests/tests.h index 616721a..2788cac 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -21,7 +21,7 @@ void test_type_system(); inline void test_all(){ kprintf("\e[97mkerep tests are starting!\n"); - optime(__func__, 1, ({ + optime(__func__, 1, test_type_system(); test_string(); test_safethrow(); @@ -35,7 +35,7 @@ inline void test_all(){ test_hashtable(); test_dtsod(); kprintf("\e[96m--------------------------------------\e[0m\n"); - })); + ); } #define PRINT_SIZEOF(T) kprintf("\e[94m" #T " size: \e[96m" IFWIN("%llu", "%lu") "\n", sizeof(T)) From a7de63bb9d6308429c0ba43882425bb961672adc Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Mon, 22 May 2023 03:06:03 +0600 Subject: [PATCH 45/55] new Hashtable functions --- src/DtsodParser/DtsodV24_deserialize.c | 2 +- src/Hashtable/Hashtable.c | 35 +++++++++++++++++++++++--- src/Hashtable/Hashtable.h | 5 +++- 3 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/DtsodParser/DtsodV24_deserialize.c b/src/DtsodParser/DtsodV24_deserialize.c index b755579..5c201aa 100644 --- a/src/DtsodParser/DtsodV24_deserialize.c +++ b/src/DtsodParser/DtsodV24_deserialize.c @@ -318,7 +318,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) { partOfDollarList=false; Autoarr(Unitype)* list; Unitype lu; - if(Hashtable_try_get(dict,nameCPtr, &lu)){ + if(Hashtable_tryGet(dict,nameCPtr, &lu)){ list=(Autoarr(Unitype)*)lu.VoidPtr; } else{ diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index c1d3e20..c81d399 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -67,11 +67,19 @@ Autoarr(KVPair)* getrow(Hashtable* ht, char* key, bool can_expand){ return ar; } +/// @param key must be heap allocated +/// Hashtable_free will free this pointer void Hashtable_add(Hashtable* ht, char* key, Unitype u){ KVPair p={ .key=key, .value=u }; Autoarr_add(getrow(ht,key,true),p); } +void Hashtable_addMany(Hashtable* ht, KVPair* pair_array, u32 count){ + for(u32 i=0; i Date: Mon, 22 May 2023 03:06:22 +0600 Subject: [PATCH 46/55] new cptr functions --- src/base/cptr.c | 23 +++++++++++++++++++++-- src/base/cptr.h | 3 +++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/base/cptr.c b/src/base/cptr.c index ecf4eb4..5c5436e 100644 --- a/src/base/cptr.c +++ b/src/base/cptr.c @@ -1,4 +1,5 @@ #include "base.h" +#include // returns length of char buffer (without \0) u32 cptr_length(char* str){ @@ -72,7 +73,7 @@ u32 cptr_indexOfChar(char* ptr, char fragment){ return -1; } u32 cptr_lastIndexOf(char* ptr, char* fragment){ - char sc=*ptr; + char sc; u32 fi_last=cptr_length(fragment)-1; for(i32 si=cptr_length(ptr)-1, fi=fi_last; si>=0; si--){ sc=ptr[si]; @@ -86,7 +87,7 @@ u32 cptr_lastIndexOf(char* ptr, char* fragment){ return -1; } u32 cptr_lastIndexOfChar(char* ptr, char fragment){ - char sc=*ptr; + char sc; for(i32 si=cptr_length(ptr)-1; si>=0; si--){ sc=ptr[si]; if(sc==fragment){ @@ -135,3 +136,21 @@ char* __cptr_concat(u32 n, ...){ free(lengths); return output; } + +char* cptr_toLower(char* src) { + u32 length=cptr_length(src); + char *p=malloc(length+1); + p[length]=0; + for(u32 i=0; i Date: Mon, 22 May 2023 03:06:46 +0600 Subject: [PATCH 47/55] some type_system changes --- src/base/type_system/ktDescriptor.h | 10 +++++----- src/base/type_system/kt_functions.c | 4 ++-- src/base/type_system/unitype.h | 17 ++++++++--------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/src/base/type_system/ktDescriptor.h b/src/base/type_system/ktDescriptor.h index 31e4ee8..189160a 100644 --- a/src/base/type_system/ktDescriptor.h +++ b/src/base/type_system/ktDescriptor.h @@ -13,21 +13,21 @@ extern "C" { extern ktDescriptor ktDescriptor_##TYPE; \ extern ktDescriptor ktDescriptor_##TYPE##_Ptr; -#define kt_define(TYPE, FREE_FUNC, TOSTRING_FUNC)\ +#define kt_define(TYPE, FREE_MEMBERS_F, TOSTRING_F)\ ktid_define(TYPE); \ ktDescriptor ktDescriptor_##TYPE={ \ .name=#TYPE, \ .id=ktid_undefined, \ .size=sizeof(TYPE), \ - .freeMembers=FREE_FUNC, \ - .toString=TOSTRING_FUNC \ + .freeMembers=FREE_MEMBERS_F, \ + .toString=TOSTRING_F \ }; \ ktDescriptor ktDescriptor_##TYPE##_Ptr={\ .name=#TYPE "_Ptr", \ .id=ktid_undefined, \ .size=sizeof(TYPE), \ - .freeMembers=FREE_FUNC, \ - .toString=TOSTRING_FUNC \ + .freeMembers=FREE_MEMBERS_F, \ + .toString=TOSTRING_F \ }; typedef void (*freeMembers_t)(void*); diff --git a/src/base/type_system/kt_functions.c b/src/base/type_system/kt_functions.c index 8ced590..b4780c9 100644 --- a/src/base/type_system/kt_functions.c +++ b/src/base/type_system/kt_functions.c @@ -34,11 +34,11 @@ ktDescriptorsState initState=NotInitialized; void kt_beginInit(){ kprintf("\e[94mtype descriptors initializing...\n"); __descriptorPointers=Autoarr_create(Pointer, 256, 256); - if(__descriptorPointers==NULL) - throw(ERR_NULLPTR); } void kt_endInit(){ + if(__descriptorPointers==NULL) + throw(ERR_NULLPTR); typeDescriptors=(ktDescriptor**)Autoarr_toArray(__descriptorPointers); Autoarr_free(__descriptorPointers,true); if(typeDescriptors==NULL) throw(ERR_NULLPTR); diff --git a/src/base/type_system/unitype.h b/src/base/type_system/unitype.h index 3d617fe..aeb2372 100644 --- a/src/base/type_system/unitype.h +++ b/src/base/type_system/unitype.h @@ -21,25 +21,24 @@ STRUCT(Unitype, ) -#define __UniDef(FIELD, TYPE, VAL) (Unitype){ \ - .FIELD=VAL, .typeId=ktid_name(TYPE), .allocatedInHeap=false} +#define __UniDef(FIELD, TYPE, VAL) ((Unitype){ \ + .FIELD=VAL, .typeId=ktid_name(TYPE), .allocatedInHeap=false}) #define UniInt64(VAL) __UniDef(Int64, i64, VAL) #define UniUInt64(VAL) __UniDef(UInt64, u64, VAL) #define UniFloat64(VAL) __UniDef(Float64, f64, VAL) #define UniBool(VAL) __UniDef(Bool, bool, VAL) -#define UniStackPtr(TYPE, VAL) (Unitype){ \ - .VoidPtr=VAL, .typeId=ktid_ptrName(TYPE), .allocatedInHeap=false} -#define UniHeapPtr(TYPE, VAL) (Unitype){ \ - .VoidPtr=VAL, .typeId=ktid_ptrName(TYPE), .allocatedInHeap=true} - +#define UniPtr(TYPE_ID, VAL, ALLOCATED_ON_HEAP)((Unitype){ \ + .VoidPtr=VAL, .typeId=TYPE_ID, .allocatedInHeap=ALLOCATED_ON_HEAP }) +#define UniStackPtr(TYPE, VAL) UniPtr(ktid_ptrName(TYPE), VAL, false) +#define UniHeapPtr(TYPE, VAL) UniPtr(ktid_ptrName(TYPE), VAL, true) // 0==ktid_Pointer -#define UniNull (Unitype){.Int64=0, .typeId=0, .allocatedInHeap=false} +#define UniNull ((Unitype){.Int64=0, .typeId=0, .allocatedInHeap=false}) #define UniTrue UniBool(true) #define UniFalse UniBool(false) -#define Unitype_isUniNull(UNI) (UNI.typeId==ktid_Pointer && UNI.VoidPtr==NULL) +#define Unitype_isUniNull(UNI) (UNI.typeId==0 && UNI.Int64==0) // frees VoidPtr value or does nothing if type isn't pointer void Unitype_free(Unitype u); From 3d941e0688321ff68097408ce042f148a9b17d09 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Wed, 24 May 2023 00:35:32 +0600 Subject: [PATCH 48/55] hashtable bugfixes --- src/Hashtable/Hashtable.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index c81d399..ae84083 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -110,7 +110,7 @@ bool Hashtable_tryGet(Hashtable* ht, char* key, Unitype* output){ bool Hashtable_trySet(Hashtable* ht, char* key, Unitype u){ Unitype* val=Hashtable_getPtr(ht,key); - if(Unitype_isUniNull((*val))) + if(val==NULL) return false; *val=u; return true; @@ -118,7 +118,7 @@ bool Hashtable_trySet(Hashtable* ht, char* key, Unitype u){ bool Hashtable_tryAdd(Hashtable* ht, char* key, Unitype u){ Unitype* val=Hashtable_getPtr(ht,key); - if(Unitype_isUniNull((*val))){ + if(val==NULL){ Hashtable_add(ht, key, u); return true; } @@ -127,9 +127,7 @@ bool Hashtable_tryAdd(Hashtable* ht, char* key, Unitype u){ void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){ Unitype* val=Hashtable_getPtr(ht, key); - // set - if(!Unitype_isUniNull((*val))) - *val=u; - // add - else Hashtable_add(ht, key, u); + if(val==NULL) + Hashtable_add(ht, key, u); // add + else *val=u; // set } From 588453a2b7684e5b9fc98a340808e1f34736b7d3 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Wed, 24 May 2023 02:38:11 +0600 Subject: [PATCH 49/55] dtsod dollar list bug fixed --- src/DtsodParser/DtsodV24_deserialize.c | 3 +++ tests/test_dtsod.c | 6 +++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/DtsodParser/DtsodV24_deserialize.c b/src/DtsodParser/DtsodV24_deserialize.c index 5c201aa..57ac4d0 100644 --- a/src/DtsodParser/DtsodV24_deserialize.c +++ b/src/DtsodParser/DtsodV24_deserialize.c @@ -320,6 +320,9 @@ Maybe __deserialize(char** _text, bool _calledRecursively) { Unitype lu; if(Hashtable_tryGet(dict,nameCPtr, &lu)){ list=(Autoarr(Unitype)*)lu.VoidPtr; + // Key is not used in that case, because it is already added + // to the table with the first dollar list item. + free(nameCPtr); } else{ list=Autoarr_create(Unitype,ARR_BC,ARR_BL); diff --git a/tests/test_dtsod.c b/tests/test_dtsod.c index d5ea59d..e3f846a 100644 --- a/tests/test_dtsod.c +++ b/tests/test_dtsod.c @@ -12,7 +12,11 @@ const char text[]= " text: \"_$\\\"\\\\'''a ыыы000;2;=:%d;```\";\n" " list: [10,20,30,0,0 ];" "};" -"h: { };"; +"h: { };" +"$dollar_list_single: { smth: \"-_-\"; };" +"$dollar_list_many: { i: 0; };" +"$dollar_list_many: { i: 1; };" +"$dollar_list_many: { i: 2; };"; void print_dtsod(Hashtable* dtsod){ kprintf("\e[92m"); From 70807ed22b4c34db247fd8fc1ca1c776dd27ab4c Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Wed, 24 May 2023 02:39:19 +0600 Subject: [PATCH 50/55] added on_exit arg to tryLast() --- src/base/errors.h | 2 +- src/base/type_system/unitype.h | 4 ++++ src/kprint/kprint.c | 4 ++-- tests/test_dtsod.c | 8 ++++---- tests/test_safethrow.c | 2 +- 5 files changed, 12 insertions(+), 8 deletions(-) diff --git a/src/base/errors.h b/src/base/errors.h index 07b8569..a0e228a 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -80,7 +80,7 @@ char* __unknownErr( ); } #endif -#define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){ \ +#define tryLast(_funcCall, _rezult, ON_EXIT) Maybe _rezult=_funcCall; if(_rezult.errmsg){ \ _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__); \ __EXIT(_rezult.errmsg); \ } diff --git a/src/base/type_system/unitype.h b/src/base/type_system/unitype.h index aeb2372..1fef31f 100644 --- a/src/base/type_system/unitype.h +++ b/src/base/type_system/unitype.h @@ -40,6 +40,10 @@ STRUCT(Unitype, #define Unitype_isUniNull(UNI) (UNI.typeId==0 && UNI.Int64==0) +#define UniCheckTypeId(UNI, TYPE_ID) (UNI.typeId==TYPE_ID) +#define UniCheckType(UNI, TYPE) UniCheckTypeId(UNI, ktid_name(TYPE)) +#define UniCheckTypePtr(UNI, TYPE) UniCheckTypeId(UNI, ktid_ptrName(TYPE)) + // frees VoidPtr value or does nothing if type isn't pointer void Unitype_free(Unitype u); void __UnitypePtr_free(void* u); diff --git a/src/kprint/kprint.c b/src/kprint/kprint.c index c1e68e6..f90af51 100644 --- a/src/kprint/kprint.c +++ b/src/kprint/kprint.c @@ -71,12 +71,12 @@ Maybe __kfprint(FILE* file, u8 n, kp_fmt* formats, __kp_value_union* objects){ } void __kprint(u8 n, kp_fmt* formats, __kp_value_union* objects){ - tryLast(check_argsN(n), _); + tryLast(check_argsN(n), _,;); n/=2; for(u8 i=0; i Date: Wed, 24 May 2023 22:46:47 +0600 Subject: [PATCH 51/55] some toString functions --- src/base/type_system/kt_functions.c | 25 +++++++++++++++++++++++- src/base/type_system/kt_functions.h | 2 ++ src/kprint/kprint.c | 30 +++++++++++++++++++++++++++++ src/kprint/kprint_colors.h | 3 +++ 4 files changed, 59 insertions(+), 1 deletion(-) diff --git a/src/base/type_system/kt_functions.c b/src/base/type_system/kt_functions.c index b4780c9..f066c75 100644 --- a/src/base/type_system/kt_functions.c +++ b/src/base/type_system/kt_functions.c @@ -16,7 +16,30 @@ kt_define(u32, NULL, __toString_u32); kt_define(i64, NULL, __toString_i64); kt_define(u64, NULL, __toString_u64); -kt_define(ktDescriptor, NULL, NULL); + +char* ktDescriptor_toString(ktDescriptor* d){ + const char* n="null"; + char *s0 = toString_u64(d->id, 0,0); + char *s1 = toString_u64(d->size, 0,0); + char *s2 = d->toString ? toString_hex(d->toString, sizeof(void*), 0,1,0) : n; + char *s3 = d->freeMembers ? toString_hex(d->freeMembers, sizeof(void*), 0,1,0) : n; + char *rez=cptr_concat("ktDescriptor {" + " name:", d->name, + " id:",s0, + " size:",s1, + " toString:",s2, + " freeMembers:",s3, + " }"); + free(s0); + free(s1); + if(s2!=n) free(s2); + if(s3!=n) free(s3); + return rez; +} + +char* _ktDescriptor_toString(void* _d, u32 fmt) { return ktDescriptor_toString(_d); } + +kt_define(ktDescriptor, NULL, _ktDescriptor_toString); typedef ktDescriptor* ktDescriptor_Ptr; diff --git a/src/base/type_system/kt_functions.h b/src/base/type_system/kt_functions.h index b131ce2..435e28f 100644 --- a/src/base/type_system/kt_functions.h +++ b/src/base/type_system/kt_functions.h @@ -23,6 +23,8 @@ void kt_endInit(); /// @param id id of registered type ktDescriptor* ktDescriptor_get(ktid id); +char* ktDescriptor_toString(ktDescriptor* d); + // call it to free heap-allocated ktDescriptors array void kt_free(); diff --git a/src/kprint/kprint.c b/src/kprint/kprint.c index f90af51..86161ba 100644 --- a/src/kprint/kprint.c +++ b/src/kprint/kprint.c @@ -179,3 +179,33 @@ void kprint_setColor(kp_fmt f){ StringBuilder_append_char(strb, ' '); StringBuilder_append_char(strb, ']'); } */ + +static const char* _kp_colorNames[16]={ + "black", + "dark_red", + "dark_green", + "dark_yellow", + "dark_blue", + "dark_magenta", + "dark_cyan", + "gray", + "dark_gray", + "red", + "green", + "yellow", + "blue", + "magenta", + "cyan", + "white" +}; + +char* kp_bgColor_toString(kp_fmt c){ + u32 color_index=(c&0x00f00000)>>20; + if(color_index>15) throw(ERR_WRONGINDEX); + return _kp_colorNames[color_index]; +} +char* kp_fgColor_toString(kp_fmt c){ + u32 color_index=(c&0x00f00000)>>24; + if(color_index>15) throw(ERR_WRONGINDEX); + return _kp_colorNames[color_index]; +} diff --git a/src/kprint/kprint_colors.h b/src/kprint/kprint_colors.h index 18c42b8..5131651 100644 --- a/src/kprint/kprint_colors.h +++ b/src/kprint/kprint_colors.h @@ -81,6 +81,9 @@ PACKED_ENUM(kp_bgColor, kp_bgWhite = 0x40f00000 ) +char* kp_bgColor_toString(kp_bgColor c); +char* kp_fgColor_toString(kp_fgColor c); + #if __cplusplus } #endif \ No newline at end of file From 3a408735fba92237f2a5cf3532ad5f4e589d0781 Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Wed, 24 May 2023 22:53:31 +0600 Subject: [PATCH 52/55] cbuild v7 --- Makefile | 12 ++++++++++-- cbuild | 2 +- default.config | 33 ++++++++++++++++++++++++++++++--- 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/Makefile b/Makefile index 0bd17cd..3f0f054 100644 --- a/Makefile +++ b/Makefile @@ -49,10 +49,18 @@ profile: @cbuild/call_task.sh profile 2>&1 | tee make_raw.log # compiles program with -pg and runs it with gprof -# uses gprof2dot python script to generate function call tree +# uses gprof2dot python script to generate function call tree (pip install gprof2dot) +# requires graphviz (https://www.graphviz.org/download/source/) gprof: @cbuild/call_task.sh gprof 2>&1 | tee make_raw.log - + +# compiles program and runs it with callgrind (part of valgrind) +# uses gprof2dot python script to generate function call tree (pip install gprof2dot) +# requires graphviz (https://www.graphviz.org/download/source/) +# P.S. detailed rezults can be viewed in KCacheGrind +callgrind: + @cbuild/call_task.sh callgrind 2>&1 | tee make_raw.log + # compiles executable with sanitizers and executes it to find errors and warnings sanitize: @cbuild/call_task.sh sanitize 2>&1 | tee make_raw.log diff --git a/cbuild b/cbuild index 5e23ef8..ef6a3f8 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit 5e23ef8156a85a981d60152990bde53d6e8eefe9 +Subproject commit ef6a3f82c429ec232e1b910eecbdece76de933b3 diff --git a/default.config b/default.config index 6ef5ff7..913fe59 100644 --- a/default.config +++ b/default.config @@ -1,6 +1,6 @@ #!/bin/bash -CBUILD_VERSION=6 -CONFIG_VERSION=6 +CBUILD_VERSION=7 +CONFIG_VERSION=7 PROJECT="kerep" CMP_C="gcc" @@ -14,13 +14,23 @@ SRC_CPP="$( find src -name '*.cpp')" TESTS_C="$( find tests -name '*.c')" TESTS_CPP="$(find tests -name '*.cpp')" +# dir with dependeicy dirs +DEPS_BASEDIR="." +# EXAMPLE: "dependency_dir='build_task out_dir lib_file' +# other_depndency_dir=..." +# Dependencies must be declared on separate lines +# Values can be override by resetting one of dependencies: +# DEPS="$DEPS +# dependency_dir='...'" +DEPS="" + # OBJDIR structure: # ├── objects/ - dir where compiled *.o files are stored. cleans every call of build task # ├── profile/ - dir where gcc *.gcda profiling info files stored # └── libs/ - there you can put static libs and linker will find them OBJDIR="obj" OUTDIR="bin" -STATIC_LIB_FILE="$PROJECT.a" +STATIC_LIB_FILE="lib$PROJECT.a" # OS-specific options case "$OS" in @@ -135,6 +145,20 @@ case "$TASK" in TASK_SCRIPT=cbuild/default_tasks/gprof.sh POST_TASK_SCRIPT= ;; + # compiles program and runs it with callgrind (part of valgrind) + # uses gprof2dot python script to generate function call tree (pip install gprof2dot) + # requires graphviz (https://www.graphviz.org/download/source/) + # P.S. detailed rezults can be viewed in KCacheGrind + callgrind) + OUTDIR="$OUTDIR/callgrind" + # -pg adds code to executable, that generates file containing function call info (gmon.out) + C_ARGS="-O2 -flto=auto -fuse-linker-plugin" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="$CPP_ARGS" + PRE_TASK_SCRIPT=tasks/pre_build.sh + TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + POST_TASK_SCRIPT=cbuild/default_tasks/callgrind.sh + ;; # compiles executable with sanitizers and executes it to find errors and warnings sanitize) OUTDIR="$OUTDIR/sanitize" @@ -149,6 +173,9 @@ case "$TASK" in clean) TASK_SCRIPT=cbuild/default_tasks/clean.sh ;; + # nothing to do + no_task) + ;; # unknown task *) error "task <$TASK> not found" From 176cdb2d622357038ee619f14815a6dd6217706f Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Sat, 27 May 2023 22:05:25 +0600 Subject: [PATCH 53/55] rewrite of cptr functions --- src/Hashtable/Hashtable.c | 4 +- src/base/cptr.c | 185 +++++++++++++++++++++++++------------- src/base/cptr.h | 75 +++++++++++----- tests/test_cptr.c | 161 +++++++++++++++++++++++++++++++++ tests/tests.h | 4 +- 5 files changed, 342 insertions(+), 87 deletions(-) create mode 100644 tests/test_cptr.c diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index ae84083..9c8fe15 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -86,7 +86,7 @@ Unitype* Hashtable_getPtr(Hashtable* ht, char* key){ u32 arlen=Autoarr_length(ar); for(u32 i=0;ikey)) return &p->value; + if(cptr_equals(key,p->key)) return &p->value; } return NULL; } @@ -96,7 +96,7 @@ Unitype Hashtable_get(Hashtable* ht, char* key){ u32 arlen=Autoarr_length(ar); for(u32 i=0;i // returns length of char buffer (without \0) -u32 cptr_length(char* str){ - u32 len=0; - while(*(str++)) len++; - return len; +u32 cptr_length(const char* str){ + const char *const str_first=str; + while(*str) + str++; + return str-str_first; } // allocates new char[] and copies src there -char* cptr_copy(char* src){ +char* cptr_copy(const char* src){ u32 len=cptr_length(src)+1; char* dst=malloc(len); while(len--!=0) @@ -17,16 +19,6 @@ char* cptr_copy(char* src){ return dst; } -// compares two char buffers, NullPtr-friendly -bool cptr_compare(char* key0, char* key1){ - if(!key0) return key1 ? false : true; - if(!key1) return false; - while(*key0&&*key1) - if(*key0++ != *key1++) - return false; - return true; -} - // multiplies char n times char* char_multiply(char c, u32 n){ char* rez=malloc(n+1); @@ -36,63 +28,105 @@ char* char_multiply(char c, u32 n){ return rez; } -bool cptr_startsWith(char* ptr, char* fragment){ - for(char cs=*ptr, cf=*fragment; cf; cs=*++ptr, cf=*++fragment) - if(cs!=cf) return false; - return true; +bool cptr_equals(const char* key0, const char* key1){ + char c0=*key0; + char c1=*key1; + bool eq=c0==c1; + while(c0 && c1 && eq) { + c0=*++key0; + c1=*++key1; + eq=c0==c1; + } + return eq; } -bool cptr_endsWith(char* ptr, char* fragment){ - ptr+=cptr_length(ptr)-cptr_length(fragment); - for(char cs=*ptr, cf=*fragment; cf; cs=*++ptr, cf=*++fragment) - if(cs!=cf) return false; - return true; +bool cptr_startsWith(const char* src, const char* fragment){ + char c0=*src; + char c1=*fragment; + bool eq=c0==c1 && c0 !=0 && c1!=0; + while(c0 && c1 && eq) { + c0=*++src; + c1=*++fragment; + eq=c0==c1; + if(c1==0) + return true; + } + return eq; } -u32 cptr_indexOf(char* ptr, char* fragment){ - char sc=*ptr; - for(u32 si=0, fi=0; sc!='\0'; si++){ - sc=ptr[si]; - if(sc==fragment[fi]){ - fi++; - if(fragment[fi]==0) - return si-fi+1; - } - else fi=0; +bool cptr_endsWith(const char* src, const char* fragment){ + u32 src_len=cptr_length(src); + u32 fr_len=cptr_length(fragment); + if(src_len=0; si--){ - sc=ptr[si]; - if(sc==fragment[fi]){ - if(fi==0) - return si; - fi--; - } - else fi=fi_last; - } - return -1; -} -u32 cptr_lastIndexOfChar(char* ptr, char fragment){ - char sc; - for(i32 si=cptr_length(ptr)-1; si>=0; si--){ - sc=ptr[si]; - if(sc==fragment){ + +i32 cptr_seekCharReverse(const char* src, char fragment, u32 startIndex, u32 seekLength){ + char sc=*src; + if(sc==0 || fragment==0) + return -1; + i32 len=cptr_length(src); + if(startIndex==(u32)-1) + startIndex=len-1; + for(u32 si=startIndex; si<(u32)-1 && si!=len-1-seekLength; si--){ + sc=src[si]; + if(sc==fragment) return si; - } } return -1; } @@ -123,7 +157,7 @@ char* __cptr_concat(u32 n, ...){ // allocating memory for output value char* totality=malloc(totalLength+1); - const char* output=totality; + char* output=totality; totality[totalLength]=0; // copying content of all strings to rezult @@ -137,7 +171,7 @@ char* __cptr_concat(u32 n, ...){ return output; } -char* cptr_toLower(char* src) { +char* cptr_toLower(const char* src) { u32 length=cptr_length(src); char *p=malloc(length+1); p[length]=0; @@ -146,7 +180,7 @@ char* cptr_toLower(char* src) { return p; } -char* cptr_toUpper(char* src) { +char* cptr_toUpper(const char* src) { u32 length=cptr_length(src); char *p=malloc(length+1); p[length]=0; @@ -154,3 +188,28 @@ char* cptr_toUpper(char* src) { p[i]=toupper(src[i]); return p; } + +char* cptr_replaceChar(const char* src, char c_old, char c_new, u32 startIndex, u32 seekLength){ + char* rzlt=cptr_copy(src); + char c=*src; + for(u32 i=startIndex; i inclusion in or -1 if not found +i32 cptr_seek(const char* src, const char* fragment, u32 startIndex, u32 seekLength); -bool cptr_endsWith(char* ptr, char* fragment); +/// @param startIndex -1 ... src length +/// @param seekLength 0 ... -1 +/// @return pos of first inclusion in or -1 if not found +i32 cptr_seekReverse(const char* src, const char* fragment, u32 startIndex, u32 seekLength); + +/// @param startIndex 0 ... src length +/// @param seekLength 0 ... -1 +/// @return pos of first inclusion in or -1 if not found +i32 cptr_seekChar(const char* src, char fragment, u32 startIndex, u32 seekLength); + +/// @param startIndex -1 ... src length +/// @param seekLength 0 ... -1 +/// @return pos of first inclusion in or -1 if not found +i32 cptr_seekCharReverse(const char* src, char fragment, u32 startIndex, u32 seekLength); /// @brief search for in /// @return index of first inclusion or -1 if not found -u32 cptr_indexOf(char* ptr, char* fragment); +static inline i32 cptr_indexOf(const char* src, const char* fragment) +{ return cptr_seek(src, fragment, 0, -1); } + /// @brief search for in /// @return index of first inclusion or -1 if not found -u32 cptr_indexOfChar(char* ptr, char fragment); -/// @brief search for in -/// @return index of last inclusion or -1 if not found -u32 cptr_lastIndexOf(char* ptr, char* fragment); -/// @brief search for in -/// @return index of last inclusion or -1 if not found -u32 cptr_lastIndexOfChar(char* ptr, char fragment); +static inline i32 cptr_indexOfChar(const char* src, char fragment) +{ return cptr_seekChar(src, fragment, 0, -1); } -static inline bool cptr_contains(char* ptr, char* fragment){ - // if(cptr_indexOf(ptr, fragment)==-1) - // return false; - // return true; - return cptr_indexOf(ptr, fragment) +1; +/// @brief search for in +/// @return index of last inclusion or -1 if not found +static inline i32 cptr_lastIndexOf(const char* src, const char* fragment) +{ return cptr_seekReverse(src, fragment, -1, -1); } + +/// @brief search for in +/// @return index of last inclusion or -1 if not found +static inline i32 cptr_lastIndexOfChar(const char* src, char fragment) +{ return cptr_seekCharReverse(src, fragment, -1, -1); } + + +static inline bool cptr_contains(const char* src, const char* fragment){ + return cptr_seek(src, fragment, 0, -1) +1; } void memcopy(void* from, void* to, u32 size); @@ -47,8 +71,17 @@ void memcopy(void* from, void* to, u32 size); char* __cptr_concat(u32 n, ...); #define cptr_concat(STR...) __cptr_concat(count_args(STR), STR) -char* cptr_toLower(char* src); -char* cptr_toUpper(char* src); +char* cptr_toLower(const char* src); +char* cptr_toUpper(const char* src); + +/// @param startIndex 0 ... src length +/// @param seekLength 0 ... -1 +/// @return with replaced by or empty cstring if not found +char* cptr_replace(const char* src, char* str_old, char* str_new, u32 startIndex, u32 seekLength); +/// @param startIndex 0 ... src length +/// @param seekLength 0 ... -1 +/// @return with replaced by or empty cstring if not found +char* cptr_replaceChar(const char* src, char c_old, char c_new, u32 startIndex, u32 seekLength); #if __cplusplus } diff --git a/tests/test_cptr.c b/tests/test_cptr.c new file mode 100644 index 0000000..ff05f4e --- /dev/null +++ b/tests/test_cptr.c @@ -0,0 +1,161 @@ +#include "tests.h" + +/* + TODO +test cptr_seek... +test cptr_...indexOf... +test cptr_replace... + +cases: +*/ + +const char* strings[]={ + "", + "abab", + "ab_ab", + "abab_", + "_abab", + "_ab_ab_", + "_ab_ab", + "_abab_", + "_ab_ab_", + "str_not_containing_a_b", + "" +}; + +#define test_startsWith(str, fragm) \ + kprintf("\e[37m'"str"' starts with '"fragm"'"); \ + if(cptr_startsWith(str,fragm)) kprintf("\e[92m true\n"); \ + else { kprintf("\e[91m false\n"); throw(ERR_UNEXPECTEDVAL); } + +#define test_DoesntStartWith(str, fragm) \ + kprintf("\e[37m'"str"' doesnt start with '"fragm"'"); \ + if(!cptr_startsWith(str,fragm)) kprintf("\e[92m true\n"); \ + else { kprintf("\e[91m false\n"); throw(ERR_UNEXPECTEDVAL); } + +#define test_endsWith(str, fragm) \ + kprintf("\e[37m'"str"' ends with '"fragm"'"); \ + if(cptr_endsWith(str,fragm)) kprintf("\e[92m true\n"); \ + else { kprintf("\e[91m false\n"); throw(ERR_UNEXPECTEDVAL); } + +#define test_DoesntEndWith(str, fragm) \ + kprintf("\e[37m'"str"' doesnt end with '"fragm"'"); \ + if(!cptr_endsWith(str,fragm)) kprintf("\e[92m true\n"); \ + else { kprintf("\e[91m false\n"); throw(ERR_UNEXPECTEDVAL); } + +#define test_seekChar(str, fragm, start, count, expected) \ + kprintf("\e[37mseek "#fragm" in '"str"' startIndex "#start" count "#count); \ + pos=cptr_seekChar(str, fragm, start, count); \ + if(pos == expected) kprintf("\e[92m %i\n", pos); \ + else { kprintf("\e[91m %i\n", pos); throw(ERR_UNEXPECTEDVAL); } + +#define test_seekCharReverse(str, fragm, start, count, expected) \ + kprintf("\e[37mseek reverse "#fragm" in '"str"' startIndex "#start" count "#count); \ + pos=cptr_seekCharReverse(str, fragm, start, count); \ + if(pos == expected) kprintf("\e[92m %i\n", pos); \ + else { kprintf("\e[91m %i\n", pos); throw(ERR_UNEXPECTEDVAL); } + +#define test_seek(str, fragm, start, count, expected) \ + kprintf("\e[37mseek '"fragm"' in '"str"' startIndex "#start" count "#count); \ + pos=cptr_seek(str, fragm, start, count); \ + if(pos == expected) kprintf("\e[92m %i\n", pos); \ + else { kprintf("\e[91m %i\n", pos); throw(ERR_UNEXPECTEDVAL); } + +#define test_seekReverse(str, fragm, start, count, expected) \ + kprintf("\e[37mseek reverse '"fragm"' in '"str"' startIndex "#start" count "#count); \ + pos=cptr_seekReverse(str, fragm, start, count); \ + if(pos == expected) kprintf("\e[92m %i\n", pos); \ + else { kprintf("\e[91m %i\n", pos); throw(ERR_UNEXPECTEDVAL); } + +const int strings_len=sizeof(strings)/sizeof(strings[0]); + +void test_cptr(){ + // optime(__func__,1, + kprintf("\e[96m-------------[test_cptr]--------------\n"); + // compare + kprintf("\e[94m--------------[compare]---------------\n"); + for(int i=0; i Date: Sat, 27 May 2023 22:39:50 +0600 Subject: [PATCH 54/55] cptr_replace --- src/base/cptr.c | 16 +++++++++------- src/base/cptr.h | 10 ++++++++-- tests/test_cptr.c | 15 ++++----------- 3 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/base/cptr.c b/src/base/cptr.c index 5876d1a..07eee43 100644 --- a/src/base/cptr.c +++ b/src/base/cptr.c @@ -189,27 +189,29 @@ char* cptr_toUpper(const char* src) { return p; } -char* cptr_replaceChar(const char* src, char c_old, char c_new, u32 startIndex, u32 seekLength){ +char* cptr_replaceCharIn(const char* src, char c_old, char c_new, u32 startIndex, u32 seekLength){ char* rzlt=cptr_copy(src); - char c=*src; - for(u32 i=startIndex; i0) + StringBuilder_append_string(sb, (string){.ptr=(char*)src, .length=src_remains_len}); string rezult=StringBuilder_build(sb); return rezult.ptr; } diff --git a/src/base/cptr.h b/src/base/cptr.h index fe15bed..509577c 100644 --- a/src/base/cptr.h +++ b/src/base/cptr.h @@ -77,11 +77,17 @@ char* cptr_toUpper(const char* src); /// @param startIndex 0 ... src length /// @param seekLength 0 ... -1 /// @return with replaced by or empty cstring if not found -char* cptr_replace(const char* src, char* str_old, char* str_new, u32 startIndex, u32 seekLength); +char* cptr_replaceIn(const char* src, const char* str_old, const char* str_new, u32 startIndex, u32 seekLength); /// @param startIndex 0 ... src length /// @param seekLength 0 ... -1 /// @return with replaced by or empty cstring if not found -char* cptr_replaceChar(const char* src, char c_old, char c_new, u32 startIndex, u32 seekLength); +char* cptr_replaceCharIn(const char* src, char c_old, char c_new, u32 startIndex, u32 seekLength); + +static inline char* cptr_replace(const char* src, const char* str_old, const char* str_new) +{ return cptr_replaceIn(src, str_old, str_new, 0, -1); } + +static inline char* cptr_replaceChar(const char* src, char c_old, char c_new) +{ return cptr_replaceCharIn(src, c_old, c_new, 0, -1); } #if __cplusplus } diff --git a/tests/test_cptr.c b/tests/test_cptr.c index ff05f4e..83dfadb 100644 --- a/tests/test_cptr.c +++ b/tests/test_cptr.c @@ -1,14 +1,5 @@ #include "tests.h" -/* - TODO -test cptr_seek... -test cptr_...indexOf... -test cptr_replace... - -cases: -*/ - const char* strings[]={ "", "abab", @@ -70,7 +61,7 @@ const char* strings[]={ const int strings_len=sizeof(strings)/sizeof(strings[0]); void test_cptr(){ - // optime(__func__,1, + optime(__func__,1, kprintf("\e[96m-------------[test_cptr]--------------\n"); // compare kprintf("\e[94m--------------[compare]---------------\n"); @@ -157,5 +148,7 @@ void test_cptr(){ test_seekReverse("ab", "", 2, 2, -1) test_seekReverse("ab", "b", 1, 2, 1) test_seekReverse("ab", "b", 1, 1, 1) - // ); + + // TODO cptr_replace + ); } \ No newline at end of file From 53cf9875e6785a30b2313cf294acb89061a92713 Mon Sep 17 00:00:00 2001 From: timerix Date: Thu, 12 Oct 2023 13:59:25 +0600 Subject: [PATCH 55/55] cbuild --- cbuild | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cbuild b/cbuild index ef6a3f8..60fa8c1 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit ef6a3f82c429ec232e1b910eecbdece76de933b3 +Subproject commit 60fa8c11c2673a10f8afbe37bcea13c9be3317b8