From 8b3043b5461a8226c3b261053d48762409b74d81 Mon Sep 17 00:00:00 2001 From: Timerix22 <87668556+Timerix22@users.noreply.github.com> Date: Wed, 24 Aug 2022 13:44:30 +0600 Subject: [PATCH 01/59] Delete src/Network directory --- src/Network/knSocket.c | 1 - src/Network/network.h | 43 ------------------------------------------ 2 files changed, 44 deletions(-) delete mode 100644 src/Network/knSocket.c delete mode 100644 src/Network/network.h diff --git a/src/Network/knSocket.c b/src/Network/knSocket.c deleted file mode 100644 index f0dec47..0000000 --- a/src/Network/knSocket.c +++ /dev/null @@ -1 +0,0 @@ -#include "network.h" \ No newline at end of file diff --git a/src/Network/network.h b/src/Network/network.h deleted file mode 100644 index f877cac..0000000 --- a/src/Network/network.h +++ /dev/null @@ -1,43 +0,0 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "../Hashtable/Hashtable.h" - -#if defined(_MSC_VER) || defined(_WIN64) || defined(_WIN32) - #include "winsock.h" -#else - #include "../Hashtable/Hashtable.h" - #include - #include - #include - #include -#endif - -#define KNPAC_MAX_DATA_SIZE 65503 - -typedef struct knPackage{ - char header[5]; // knpac - uint8 version; // protocol version - uint16 data_size; // size of data block in bytes (1-) - uint32 package_num; // number in sequence of sent packages - uint32 destination_hash; // hash32 of knDestination.name - uint64 data_hash; // hash64 of data - uint8* data; // ptr to data -} knPackage; - -typedef struct knDestination{ - char* name; - -} knDestination; - -typedef struct knSocket { - Hashtable* destinations; - int socketfd; -} knSocket; - -#if __cplusplus -} -#endif \ No newline at end of file From 16dcadb3f36f9989197b07b33f4fbadd9ba1b240 Mon Sep 17 00:00:00 2001 From: Timerix22 <87668556+Timerix22@users.noreply.github.com> Date: Wed, 24 Aug 2022 13:48:48 +0600 Subject: [PATCH 02/59] Delete src/Network directory --- src/Network/README.md | 0 src/Network/knAddress.c | 41 --------------- src/Network/knAddress.h | 32 ------------ src/Network/knPackage.c | 3 -- src/Network/knPackage.h | 46 ----------------- src/Network/knSocket.c | 91 ---------------------------------- src/Network/knSocket.h | 57 --------------------- src/Network/network.h | 19 ------- src/Network/stdSocketHeaders.h | 22 -------- 9 files changed, 311 deletions(-) delete mode 100644 src/Network/README.md delete mode 100644 src/Network/knAddress.c delete mode 100644 src/Network/knAddress.h delete mode 100644 src/Network/knPackage.c delete mode 100644 src/Network/knPackage.h delete mode 100644 src/Network/knSocket.c delete mode 100644 src/Network/knSocket.h delete mode 100644 src/Network/network.h delete mode 100644 src/Network/stdSocketHeaders.h diff --git a/src/Network/README.md b/src/Network/README.md deleted file mode 100644 index e69de29..0000000 diff --git a/src/Network/knAddress.c b/src/Network/knAddress.c deleted file mode 100644 index cd0a8b2..0000000 --- a/src/Network/knAddress.c +++ /dev/null @@ -1,41 +0,0 @@ -#include "knAddress.h" - -Maybe knIPV4Address_fromStr(char* addrStr){ - char* addrStr_src=addrStr; - char* errmsg_extra="wrong char"; - uint8 c; - knIPV4Address addr; - addr.address=0; - uint16 n=0; - for(uint8 i=0; i<4; ){ - c=*addrStr++; - switch (c){ - case '\0': - if(i<3) { - errmsg_extra="end of string"; - goto default_case; - } - case '.': - addr.bytes[i++]=n; - n=0; - break; - case '0': case '1': case '2': case '3': case '4': - case '5': case '6': case '7': case '8': case '9': - n=n*10+c-'0'; - if(n>255) { - errmsg_extra="one part of address > 255"; - goto default_case; - } - break; - default_case: - default: - uint32 errmsgL=cptr_length(addrStr) + 80; - char* errmsg=malloc(errmsgL); - IFMSC(sprintf_s(errmsg, errmsgL, "wrong ip address string: %s\n %s", addrStr_src, errmsg_extra), - sprintf( errmsg, "wrong ip address string: %s\n %s", addrStr_src, errmsg_extra)); - safethrow(errmsg,;); - break; - } - } - return SUCCESS(Uni(UInt64, (uint64)addr.address)); -} diff --git a/src/Network/knAddress.h b/src/Network/knAddress.h deleted file mode 100644 index ff42021..0000000 --- a/src/Network/knAddress.h +++ /dev/null @@ -1,32 +0,0 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "../base/base.h" - - -typedef uint16 knPort; - -typedef union knIPV4Address { - uint32 address; - char bytes[4]; -} knIPV4Address; - -#define knIPV4Address_fromBytes(A, B, C, D) (knIPV4Address){.bytes={A,B,C,D}} - -///@return Maybe as Maybe -Maybe knIPV4Address_fromStr(char* addrStr); - -typedef struct knIPV4Endpoint { - knIPV4Address address; - knPort port; -} knIPV4Endpoint; - -#define knIPV4Endpoint_create(ADDR, PORT) (knIPV4Endpoint){ADDR, PORT} - - -#if __cplusplus -} -#endif \ No newline at end of file diff --git a/src/Network/knPackage.c b/src/Network/knPackage.c deleted file mode 100644 index 630b426..0000000 --- a/src/Network/knPackage.c +++ /dev/null @@ -1,3 +0,0 @@ -#include "knPackage.h" - - diff --git a/src/Network/knPackage.h b/src/Network/knPackage.h deleted file mode 100644 index f5396f0..0000000 --- a/src/Network/knPackage.h +++ /dev/null @@ -1,46 +0,0 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "../base/base.h" - - -#define KNPAC_MAX_DATA_SIZE (65535-sizeof(knPackage)+sizeof(uint8*)) - - -typedef enum __attribute__((__packed__)) knPacVersion { - knPac_V1 -} knPacVersion; - -static const char knPacHeader[5]={'k','n','p','a','c'}; - -typedef struct knPackage { - char header[5]; // knpac - knPacVersion version; // protocol version - uint16 data_size; // size of data block in bytes (1-KNPAC_MAX_DATA_SIZE) - uint32 channel_id; // id of knChannel in socket - uint32 package_num; // number in sequence of sent packages - uint64 data_hash; // hash64 of data - uint8* data; // ptr to data -} knPackage; - -typedef struct knPackageQueueElem knPackageQueueElem; -struct knPackageQueueElem { - union { - knPackage; - knPackage package; - }; - knPackageQueueElem* previousElem; - knPackageQueueElem* nextElem; -}; - -typedef struct knChannel { - knPackageQueueElem* queueStart; -} knChannel; - - -#if __cplusplus -} -#endif \ No newline at end of file diff --git a/src/Network/knSocket.c b/src/Network/knSocket.c deleted file mode 100644 index 68f7f50..0000000 --- a/src/Network/knSocket.c +++ /dev/null @@ -1,91 +0,0 @@ -#include "knSocket.h" -#include "stdSocketHeaders.h" - - -Maybe knSocket_open(knSocketType sockType){ - knSocket* newSocket=malloc(sizeof(knSocket)); - newSocket->type=sockType; - newSocket->channels=NULL; - newSocket->channelsAmount=0; - newSocket->localEndpoint=knIPV4Endpoint_create(knIPV4Address_fromBytes(0,0,0,0),0); - newSocket->remoteEndpoint=newSocket->localEndpoint; - switch(sockType){ - default: - safethrow("unknown socket type", free(newSocket)); - break; - case knSocketType_TCP: - newSocket->socketfd=socket(AF_INET, SOCK_STREAM, 0); - if(newSocket->socketfd==-1) - safethrow("can't create TCP socket", free(newSocket)); - break; - case knSocketType_UDP: - newSocket->socketfd=socket(AF_INET, SOCK_DGRAM, 0); - if(newSocket->socketfd==-1) - safethrow("can't create UDP socket", free(newSocket)); - break; - } - return SUCCESS(UniPtr(knSocketPtr, newSocket)); -} - -Maybe knSocket_close(knSocket* knsocket){ - int rezult= -#if KN_USE_WINSOCK - closesocket -#else - close -#endif - (knsocket->socketfd); - if(rezult==-1) { - safethrow("can't close socket",;); - } - else return SUCCESS(UniNull); -} - -knChannel* __createChannel(){ - knChannel* ch=malloc(sizeof(knChannel)); - ch->queueStart=NULL; - return ch; -} - -Maybe knSocket_createChannel(knSocket* sock){ - if(sock->channelsAmount == 65535) - safethrow("max amount of channels",;); - uint16 channelsAmountPrev=sock->channelsAmount; - sock->channelsAmount++; - if(channelsAmountPrev==0) - sock->channels=malloc(sizeof(knChannel*)); - else - sock->channels=realloc(sock->channels, sock->channelsAmount*sizeof(knChannel*)); - sock->channels[channelsAmountPrev]=__createChannel(); - return SUCCESS(Uni(UInt64, channelsAmountPrev)); -} - -/*Maybe knSocket_bind(knSocket* sock, knIPV4Endpoint localEndp){ - if(sock->localEndpoint.address.address!=0) - safethrow("socket is bound already",;); - struct sockaddr_in addr; - addr.sin_family= - bind(sock->socketfd); - sock->localEndpoint=localEndp; - return SUCCESS(UniNull); -} - -Maybe knSocket_connect(knSocket* sock, knIPV4Endpoint remoteEndp){ - if(sock->remoteEndpoint.address.address!=0) - safethrow("socket is connected already",;); - - sock->remoteEndpoint=remoteEndp; - return SUCCESS(UniNull); -} - -Maybe knSocket_accept(knSocket* sock){ - -} - -Maybe knSocket_send(knSocket* sock, uint16 destinationIndex, uint8* data, uint32 dataLength){ - -} - -Maybe knSocket_recieve(knSocket* sock, uint16 destinationIndex, uint8* buffer, uint32 bufferLength){ - -}*/ diff --git a/src/Network/knSocket.h b/src/Network/knSocket.h deleted file mode 100644 index f9559eb..0000000 --- a/src/Network/knSocket.h +++ /dev/null @@ -1,57 +0,0 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "../base/base.h" -#include "knAddress.h" -#include "knPackage.h" - - -typedef enum __attribute__((__packed__)) knSocketType { - knSocketType_TCP, knSocketType_UDP -} knSocketType; - -typedef struct knSocket { - knSocketType type; - uint16 channelsAmount; - knChannel** channels; - int64 socketfd; - knIPV4Endpoint localEndpoint; - knIPV4Endpoint remoteEndpoint; -} knSocket; - -///@return Maybe new socket -Maybe knSocket_open(knSocketType sockType); - -///@return Maybe error or nothing -Maybe knSocket_close(knSocket* socket); - -///@return Maybe channel index -Maybe knSocket_createChannel(knSocket* socket); - -///sets socket local endpoint -///@return Maybe error or nothing -Maybe knSocket_bind(knSocket* socket, knIPV4Endpoint localEndp); - -///sets socket remote endpoint -///@return Maybe error or nothing -Maybe knSocket_connect(knSocket* socket, knIPV4Endpoint remoteEndp); - -///@return Maybe new socket connected to client -Maybe knSocket_accept(knSocket* socket); - -///@param dataLength 0-4294967295 -///@return Maybe -Maybe knSocket_send(knSocket* socket, uint16 destinationIndex, uint8* data, uint32 dataLength); - -///@param buffer buffer for recieving data -///@param bufferLength 0-4294967295 -///@return Maybe recieved bytes amount -Maybe knSocket_recieve(knSocket* socket, uint16 destinationIndex, uint8* buffer, uint32 bufferLength); - - -#if __cplusplus -} -#endif \ No newline at end of file diff --git a/src/Network/network.h b/src/Network/network.h deleted file mode 100644 index 5c1c308..0000000 --- a/src/Network/network.h +++ /dev/null @@ -1,19 +0,0 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "../base/base.h" - -#if defined(_MSC_VER) || defined(_WIN64) || defined(_WIN32) || 0 - #define KN_USE_WINSOCK 1 -#endif - -#include "knAddress.h" -#include "knPackage.h" -#include "knSocket.h" - -#if __cplusplus -} -#endif \ No newline at end of file diff --git a/src/Network/stdSocketHeaders.h b/src/Network/stdSocketHeaders.h deleted file mode 100644 index c115e1b..0000000 --- a/src/Network/stdSocketHeaders.h +++ /dev/null @@ -1,22 +0,0 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "network.h" - -#if KN_USE_WINSOCK - #include -#else - #include - #include - #include - #include - #include - #include -#endif - -#if __cplusplus -} -#endif \ No newline at end of file From 491929c3c2daf13859fc45775ba064edbd01027e Mon Sep 17 00:00:00 2001 From: Timerix22 <87668556+Timerix22@users.noreply.github.com> Date: Wed, 24 Aug 2022 13:49:26 +0600 Subject: [PATCH 03/59] Delete test_network.c --- tests/test_network.c | 42 ------------------------------------------ 1 file changed, 42 deletions(-) delete mode 100644 tests/test_network.c diff --git a/tests/test_network.c b/tests/test_network.c deleted file mode 100644 index 8df3f07..0000000 --- a/tests/test_network.c +++ /dev/null @@ -1,42 +0,0 @@ -#include "tests.h" -#include "../src/Network/network.h" - -void __test_knIPV4Address_fromStr(char* addrStr, uint8 a, uint8 b, uint8 c, uint8 d){ - tryLast(knIPV4Address_fromStr(addrStr), maybeAddr){ - knIPV4Address addr; - addr.address=(uint32)maybeAddr.value.UInt64; - printf("\e[94mknIPV4Address_fromStr(\e[96m%s\e[94m) -> ", addrStr); - if(maybeAddr.value.UInt64!=knIPV4Address_fromBytes(a,b,c,d).address){ - printf("\e[91m%u.%u.%u.%u\n", - (uint8)addr.bytes[0], (uint8)addr.bytes[1], (uint8)addr.bytes[2], (uint8)addr.bytes[3]); - throw("knIPV4Address_fromStr returned wrong value"); - } - else printf("\e[92m%u.%u.%u.%u\n", - (uint8)addr.bytes[0], (uint8)addr.bytes[1], (uint8)addr.bytes[2], (uint8)addr.bytes[3]); - } -} -#define test_knIPV4Address_fromStr(a,b,c,d) __test_knIPV4Address_fromStr(#a"."#b"."#c"."#d, a,b,c,d) - -void test_network(){ - optime(__func__,1,({ - printf("\e[96m------------[test_network]------------\n"); - - PRINT_SIZEOF(knIPV4Address); - PRINT_SIZEOF(knPort); - PRINT_SIZEOF(knIPV4Endpoint); - PRINT_SIZEOF(knPackage); - PRINT_SIZEOF(knChannel); - PRINT_SIZEOF(knSocket); - - test_knIPV4Address_fromStr(127,0,0,1); - test_knIPV4Address_fromStr(34,255,45,0); - test_knIPV4Address_fromStr(3,3,3,128); - - knSocket* s; - tryLast(knSocket_open(knSocketType_TCP), maybeS) - s=maybeS.value.VoidPtr; - printf("\e[92mTCP socket created\n"); - tryLast(knSocket_close(s);,_); - printf("\e[92mTCP socket closed\n"); - })); -} \ No newline at end of file From 996dcd41deebf44f9d75b3284ada25095e15ba34 Mon Sep 17 00:00:00 2001 From: Timerix22 <87668556+Timerix22@users.noreply.github.com> Date: Wed, 24 Aug 2022 13:50:11 +0600 Subject: [PATCH 04/59] removed test_network() definition --- tests/tests.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/tests.h b/tests/tests.h index eb66a8f..71cc4b1 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -13,10 +13,9 @@ void test_autoarr(); void test_hash_functions(); void test_hashtable(); void test_dtsod(); -void test_network(); #define PRINT_SIZEOF(T) printf("\e[94m" #T " size: \e[96m" IFWIN("%llu", "%lu") "\n", sizeof(T)) #if __cplusplus } -#endif \ No newline at end of file +#endif From 0ce156536c86184c681ebf0687f1b45818d8a019 Mon Sep 17 00:00:00 2001 From: Timerix22 <87668556+Timerix22@users.noreply.github.com> Date: Wed, 24 Aug 2022 13:50:59 +0600 Subject: [PATCH 05/59] removed test_network() call --- tests/main.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tests/main.cpp b/tests/main.cpp index b68a42d..10f54f3 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -8,7 +8,6 @@ void test_all(){ test_hash_functions(); test_hashtable(); test_dtsod(); - test_network(); printf("\e[96m--------------------------------------\e[0m\n"); } From 8234c6e7b512e3672014cc3d6624c55498f3ad5c Mon Sep 17 00:00:00 2001 From: Timerix22 <87668556+Timerix22@users.noreply.github.com> Date: Wed, 24 Aug 2022 13:53:54 +0600 Subject: [PATCH 06/59] Update kerep.vcxproj --- kerep.vcxproj | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/kerep.vcxproj b/kerep.vcxproj index 159f0fe..33807a4 100644 --- a/kerep.vcxproj +++ b/kerep.vcxproj @@ -32,9 +32,6 @@ - - - @@ -44,7 +41,6 @@ - @@ -63,11 +59,6 @@ - - - - - @@ -261,4 +252,4 @@ - \ No newline at end of file + From 2e378d1458bb17134a0837a089daf4074373daab Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 24 Aug 2022 19:18:24 +0600 Subject: [PATCH 07/59] new TypeId system --- src/Autoarr/Autoarr.h | 38 +++++-- src/Autoarr/Autoarr_declare.h | 8 +- src/Autoarr/Autoarr_define.h | 11 ++- src/Hashtable/Hashtable.h | 2 + src/SearchTree/SearchTree.h | 2 + src/base/base.h | 4 +- src/base/init.c | 66 +++++++++++++ src/base/init.h | 12 +++ src/base/std.h | 11 ++- src/base/types.c | 180 +++++++--------------------------- src/base/types.c.old | 113 +++++++++++++++++++++ src/base/types.h | 85 ++++++++-------- src/base/unitype.c | 10 ++ src/base/unitype.h | 50 ++++++++++ 14 files changed, 390 insertions(+), 202 deletions(-) create mode 100644 src/base/init.c create mode 100644 src/base/init.h create mode 100644 src/base/types.c.old create mode 100644 src/base/unitype.c create mode 100644 src/base/unitype.h diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h index 89440a4..725b174 100644 --- a/src/Autoarr/Autoarr.h +++ b/src/Autoarr/Autoarr.h @@ -7,18 +7,44 @@ extern "C" { #include "Autoarr_declare.h" #include "Autoarr_define.h" -Autoarr_declare(uint8) +Autoarr_declare(float32) +Autoarr_declare(float64) Autoarr_declare(int8) -Autoarr_declare(uint16) +Autoarr_declare(uint8) Autoarr_declare(int16) -Autoarr_declare(uint32) +Autoarr_declare(uint16) Autoarr_declare(int32) -Autoarr_declare(uint64) +Autoarr_declare(uint32) Autoarr_declare(int64) -Autoarr_declare(float) -Autoarr_declare(double) +Autoarr_declare(uint64) Autoarr_declare(Unitype) +kerepType_declare(AutoarrChar); +kerepType_declare(AutoarrBool); +kerepType_declare(AutoarrFloat32); +kerepType_declare(AutoarrFloat64); +kerepType_declare(AutoarrInt8); +kerepType_declare(AutoarrUInt8); +kerepType_declare(AutoarrInt16); +kerepType_declare(AutoarrUInt16); +kerepType_declare(AutoarrInt32); +kerepType_declare(AutoarrUInt32); +kerepType_declare(AutoarrInt64); +kerepType_declare(AutoarrUInt64); + +kerepType_declare(AutoarrCharPtr); +kerepType_declare(AutoarrBoolPtr); +kerepType_declare(AutoarrFloat32Ptr); +kerepType_declare(AutoarrFloat64Ptr); +kerepType_declare(AutoarrInt8Ptr); +kerepType_declare(AutoarrUInt8Ptr); +kerepType_declare(AutoarrInt16Ptr); +kerepType_declare(AutoarrUInt16Ptr); +kerepType_declare(AutoarrInt32Ptr); +kerepType_declare(AutoarrUInt32Ptr); +kerepType_declare(AutoarrInt64Ptr); +kerepType_declare(AutoarrUInt64Ptr); + // right func to clear array of unitype values void Autoarr_free_Unitype(Autoarr(Unitype)* ar); diff --git a/src/Autoarr/Autoarr_declare.h b/src/Autoarr/Autoarr_declare.h index 80169bd..9b15bd7 100644 --- a/src/Autoarr/Autoarr_declare.h +++ b/src/Autoarr/Autoarr_declare.h @@ -16,6 +16,7 @@ typedef struct {\ type* (*getptr)(struct Autoarr_##type* ar, uint32 index);\ void (*set)(struct Autoarr_##type* ar, uint32 index, type element);\ void (*_free)(struct Autoarr_##type* ar);\ + type* (*toArray)(struct Autoarr_##type* ar);\ } __functions_list_t_##type;\ \ typedef struct Autoarr_##type{\ @@ -27,11 +28,6 @@ typedef struct Autoarr_##type{\ __functions_list_t_##type* functions;\ } Autoarr_##type;\ \ -void __Autoarr_add_##type(Autoarr_##type* ar, type element);\ -type __Autoarr_get_##type(Autoarr_##type* ar, uint32 index);\ -type* __Autoarr_getptr_##type(Autoarr_##type* ar, uint32 index);\ -void __Autoarr_set_##type(Autoarr_##type* ar, uint32 index, type element);\ -void __Autoarr_free_##type(Autoarr_##type* ar);\ Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length); #define Autoarr(type) Autoarr_##type @@ -48,6 +44,8 @@ Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_bloc autoarr->functions->_free(autoarr) #define Autoarr_create(type, max_blocks_count, max_block_length)\ __Autoarr_create_##type(max_blocks_count, max_block_length) +#define Autoarr_toArray(autoarr)\ + autoarr->functions->toArray(autoarr) #define Autoarr_length(autoarr) \ (uint32)(!autoarr->blocks_count ? 0 : \ diff --git a/src/Autoarr/Autoarr_define.h b/src/Autoarr/Autoarr_define.h index 441c9b6..ffc50e1 100644 --- a/src/Autoarr/Autoarr_define.h +++ b/src/Autoarr/Autoarr_define.h @@ -46,12 +46,21 @@ void __Autoarr_free_##type(Autoarr_##type* ar){\ free(ar);\ }\ \ +type* __Autoarr_toArray_##type(Autoarr_##type* ar){\ + uint32 length=Autoarr_length(ar);\ + type* array=malloc(length * sizeof(type));\ + for(uint32 i=0; i #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; #define dbg(N) printf("\e[95m%d\n",N) - #ifdef _MSC_VER #pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK #define _CRT_SECURE_NO_WARNINGS 1 diff --git a/src/base/types.c b/src/base/types.c index fed1f0b..2f0e1a7 100644 --- a/src/base/types.c +++ b/src/base/types.c @@ -1,154 +1,40 @@ -#include "types.h" -#include "errors.h" #include "../Autoarr/Autoarr.h" -#include "../Hashtable/Hashtable.h" -#include "../SearchTree/SearchTree.h" +#include "unitype.h" -const char* my_type_name(my_type t){ - switch (t) { - case Null: return "Null"; - case Float64: return "Float64"; - case Float32: return "Float32"; - case Bool: return "Bool"; - case Char: return "Char"; - case Int8: return "Int8"; - case UInt8: return "UInt8"; - case Int16: return "Int16"; - case UInt16: return "UInt16"; - case Int32: return "Int32"; - case UInt32: return "UInt32"; - case Int64: return "Int64"; - case UInt64: return "UInt64"; - case Int8Ptr: return "Int8Ptr"; - case UInt8Ptr: return "UInt8Ptr"; - case Int16Ptr: return "Int16Ptr"; - case UInt16Ptr: return "UInt16Ptr"; - case Int32Ptr: return "Int32Ptr"; - case UInt32Ptr: return "UInt32Ptr"; - case Int64Ptr: return "Int64Ptr"; - case UInt64Ptr: return "UInt64Ptr"; - case CharPtr: return "CharPtr"; - case STNodePtr: return "STNodePtr"; - case HashtablePtr: return "HashtablePtr"; - case UniversalType: return "Unitype"; - case AutoarrInt8Ptr: return "AutoarrInt8Ptr"; - case AutoarrUInt8Ptr: return "AutoarrUInt8Ptr"; - case AutoarrInt16Ptr: return "AutoarrInt16Ptr"; - case AutoarrUInt16Ptr: return "AutoarrUInt16Ptr"; - case AutoarrInt32Ptr: return "AutoarrInt32Ptr"; - case AutoarrUInt32Ptr: return "AutoarrUInt32Ptr"; - case AutoarrInt64Ptr: return "AutoarrInt64Ptr"; - case AutoarrUInt64Ptr: return "AutoarrUInt64Ptr"; - case AutoarrUnitypePtr: return "AutoarrUnitypePtr"; - case AutoarrKVPairPtr: return "AutoarrKVPairPtr"; - default: throw(ERR_WRONGTYPE); - } +Autoarr_declare(kerepTypeDescriptor) +Autoarr_define(kerepTypeDescriptor) + +// type descriptors are stored here during initialization +Autoarr(kerepTypeDescriptor)* __kerepTypeDescriptors=NULL; +// here type descriptors are stored when initialization is complited +kerepTypeDescriptor* typeDescriptors=NULL; +kerepTypeId kerepTypeId_last=-1; + +typedef enum{ + NotInitialized, Initializing, Initialized +} kerepTypeDescriptorsState; +kerepTypeDescriptorsState initState=NotInitialized; + +void kerepTypeDescriptors_beginInit(){ + __kerepTypeDescriptors=Autoarr_create(kerepTypeDescriptor, 256, 256); + if(__kerepTypeDescriptors==NULL) throw(ERR_NULLPTR); } -// frees VoidPtr value or does nothing if type isn't pointer -void Unitype_free(Unitype u){ - switch (u.type) { - case Null: - case Float32: - case Float64: - case Char: - case Bool: - case Int8: - case UInt8: - case Int16: - case UInt16: - case Int32: - case UInt32: - case Int64: - case UInt64: - break; - case Int8Ptr: - case UInt8Ptr: - case Int16Ptr: - case UInt16Ptr: - case Int32Ptr: - case UInt32Ptr: - case Int64Ptr: - case UInt64Ptr: - case CharPtr: - free(u.VoidPtr); - break; - case HashtablePtr: - Hashtable_free(u.VoidPtr); - break; - case STNodePtr: - STNode_free(u.VoidPtr); - break; - case AutoarrInt8Ptr: - __Autoarr_free_int8(u.VoidPtr); - break; - case AutoarrUInt8Ptr: - __Autoarr_free_uint8(u.VoidPtr); - break; - case AutoarrInt16Ptr: - __Autoarr_free_int16(u.VoidPtr); - break; - case AutoarrUInt16Ptr: - __Autoarr_free_uint16(u.VoidPtr); - break; - case AutoarrInt32Ptr: - __Autoarr_free_int32(u.VoidPtr); - break; - case AutoarrUInt32Ptr: - __Autoarr_free_uint32(u.VoidPtr); - break; - case AutoarrInt64Ptr: - __Autoarr_free_int64(u.VoidPtr); - break; - case AutoarrUInt64Ptr: - __Autoarr_free_uint64(u.VoidPtr); - break; - case AutoarrUnitypePtr: - Autoarr_free_Unitype(u.VoidPtr); - break; - case AutoarrKVPairPtr: - Autoarr_free_KVPair(u.VoidPtr); - break; - default: throw(ERR_WRONGTYPE); - } +void kerepTypeDescriptors_endInit(){ + typeDescriptors=Autoarr_toArray(__kerepTypeDescriptors); + Autoarr_free(__kerepTypeDescriptors); + if(typeDescriptors==NULL) throw(ERR_NULLPTR); +} +void __kerepType_register(char* name, int16 size, void (*free_members)(void*)){ + kerepTypeDescriptor typeDesc={ + .name=name, + .size=size, + .free_members=free_members, + .id=++kerepTypeId_last + }; + Autoarr_add(__kerepTypeDescriptors, typeDesc); } -#define BUFSIZE 64 -char* sprintuni(Unitype v){ - char* buf=malloc(BUFSIZE); - IFMSC( - switch (v.type) { - case Null: sprintf_s(buf, BUFSIZE, "{Null}");break; - case Float64: sprintf_s(buf, BUFSIZE, "{%s : %lf}", my_type_name(v.type),v.Float64);break; - case Bool: - case UInt64: sprintf_s(buf, BUFSIZE, "{%s : %lu}", my_type_name(v.type),v.UInt64);break; - case Int64: sprintf_s(buf, BUFSIZE, "{%s : %ld}", my_type_name(v.type),v.Int64);break; - case CharPtr: ; - size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2; - buf=realloc(buf, newBUFSIZE); - sprintf_s(buf, newBUFSIZE, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr); - break; - default: sprintf_s(buf, BUFSIZE, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break; - }, - switch (v.type) { - case Null: sprintf(buf, "{Null}"); break; - case Float64: sprintf(buf, "{%s : %lf}", my_type_name(v.type),v.Float64); break; - case Bool: - case UInt64: sprintf(buf, "{%s : " IFWIN("%llu", "%lu") "}", my_type_name(v.type),v.UInt64); break; - case Int64: sprintf(buf, "{%s : " IFWIN("%lld", "%ld") "}", my_type_name(v.type),v.Int64); break; - case CharPtr: ; - size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2; - buf=realloc(buf, newBUFSIZE); - sprintf(buf, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr); - break; - default: sprintf(buf, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break; - } - ); - return buf; +kerepTypeDescriptor typeDescriptor_get(kerepTypeId id){ + return typeDescriptors[id]; } - -void printuni(Unitype v){ - char* s=sprintuni(v); - fputs(s, stdout); - free(s); -} \ No newline at end of file diff --git a/src/base/types.c.old b/src/base/types.c.old new file mode 100644 index 0000000..3c6ef1e --- /dev/null +++ b/src/base/types.c.old @@ -0,0 +1,113 @@ +#include "types.h" +#include "errors.h" +#include "../Autoarr/Autoarr.h" +#include "../Hashtable/Hashtable.h" +#include "../SearchTree/SearchTree.h" + +// frees VoidPtr value or does nothing if type isn't pointer +void Unitype_free(Unitype u){ + switch (u.type) { + case Null: + case Float32: + case Float64: + case Char: + case Bool: + case Int8: + case UInt8: + case Int16: + case UInt16: + case Int32: + case UInt32: + case Int64: + case UInt64: + break; + case Int8Ptr: + case UInt8Ptr: + case Int16Ptr: + case UInt16Ptr: + case Int32Ptr: + case UInt32Ptr: + case Int64Ptr: + case UInt64Ptr: + case CharPtr: + free(u.VoidPtr); + break; + case HashtablePtr: + Hashtable_free(u.VoidPtr); + break; + case STNodePtr: + STNode_free(u.VoidPtr); + break; + case AutoarrInt8Ptr: + __Autoarr_free_int8(u.VoidPtr); + break; + case AutoarrUInt8Ptr: + __Autoarr_free_uint8(u.VoidPtr); + break; + case AutoarrInt16Ptr: + __Autoarr_free_int16(u.VoidPtr); + break; + case AutoarrUInt16Ptr: + __Autoarr_free_uint16(u.VoidPtr); + break; + case AutoarrInt32Ptr: + __Autoarr_free_int32(u.VoidPtr); + break; + case AutoarrUInt32Ptr: + __Autoarr_free_uint32(u.VoidPtr); + break; + case AutoarrInt64Ptr: + __Autoarr_free_int64(u.VoidPtr); + break; + case AutoarrUInt64Ptr: + __Autoarr_free_uint64(u.VoidPtr); + break; + case AutoarrUnitypePtr: + Autoarr_free_Unitype(u.VoidPtr); + break; + case AutoarrKVPairPtr: + Autoarr_free_KVPair(u.VoidPtr); + break; + default: throw(ERR_WRONGTYPE); + } +} + +#define BUFSIZE 64 +char* sprintuni(Unitype v){ + char* buf=malloc(BUFSIZE); + IFMSC( + switch (v.type) { + case Null: sprintf_s(buf, BUFSIZE, "{Null}");break; + case Float64: sprintf_s(buf, BUFSIZE, "{%s : %lf}", my_type_name(v.type),v.Float64);break; + case Bool: + case UInt64: sprintf_s(buf, BUFSIZE, "{%s : %lu}", my_type_name(v.type),v.UInt64);break; + case Int64: sprintf_s(buf, BUFSIZE, "{%s : %ld}", my_type_name(v.type),v.Int64);break; + case CharPtr: ; + size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2; + buf=realloc(buf, newBUFSIZE); + sprintf_s(buf, newBUFSIZE, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr); + break; + default: sprintf_s(buf, BUFSIZE, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break; + }, + switch (v.type) { + case Null: sprintf(buf, "{Null}"); break; + case Float64: sprintf(buf, "{%s : %lf}", my_type_name(v.type),v.Float64); break; + case Bool: + case UInt64: sprintf(buf, "{%s : " IFWIN("%llu", "%lu") "}", my_type_name(v.type),v.UInt64); break; + case Int64: sprintf(buf, "{%s : " IFWIN("%lld", "%ld") "}", my_type_name(v.type),v.Int64); break; + case CharPtr: ; + size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2; + buf=realloc(buf, newBUFSIZE); + sprintf(buf, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr); + break; + default: sprintf(buf, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break; + } + ); + return buf; +} + +void printuni(Unitype v){ + char* s=sprintuni(v); + fputs(s, stdout); + free(s); +} \ No newline at end of file diff --git a/src/base/types.h b/src/base/types.h index e34d95f..2722e17 100644 --- a/src/base/types.h +++ b/src/base/types.h @@ -6,52 +6,55 @@ extern "C" { #include "std.h" -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 enum __attribute__((__packed__)) my_type { - Null, Float32, Float64, Char, Bool, - UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64, - UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr, - CharPtr, STNodePtr, HashtablePtr, - UniversalType, - AutoarrInt8Ptr, AutoarrUInt8Ptr, AutoarrInt16Ptr, AutoarrUInt16Ptr, - AutoarrInt32Ptr, AutoarrUInt32Ptr, AutoarrInt64Ptr, AutoarrUInt64Ptr, - AutoarrUnitypePtr, AutoarrKVPairPtr, knSocketPtr -} my_type; -#define my_type_last knSocketPtr +typedef uint16 kerepTypeId; +typedef struct kerepTypeDescriptor{ + void (*free_members)(void*); // NULL or function which frees all struct members + char* name; + kerepTypeId id; + uint16 size; +} kerepTypeDescriptor; -const char* my_type_name(my_type t); +#define kerepType_declare(NAME)\ + extern kerepTypeId kerepTypeId_##NAME -typedef struct Unitype{ - union { - int64 Int64; - uint64 UInt64; - double Float64; - bool Bool; - void* VoidPtr; - }; - my_type type; -} Unitype; +extern kerepTypeId kerepTypeId_last; +void __kerepType_register(char* name, int16 size, void (*free_members)(void*)); -static const Unitype UniNull={.VoidPtr=NULL,.type=Null}; -static const Unitype UniTrue={.Bool=true,.type=Bool}; -static const Unitype UniFalse={.Bool=false,.type=Bool}; +#define kerepType_register(TYPE, NAME, FREE_MEMBERS_FUNC)\ + __kerepType_register(#NAME, sizeof(TYPE), FREE_MEMBERS_FUNC);\ + kerepTypeId_##NAME=kerepTypeId_last; -#define Uni(TYPE,VAL) (Unitype){.type=TYPE,.TYPE=VAL} -#define UniPtr(TYPE,VAL) (Unitype){.type=TYPE,.VoidPtr=VAL} +void kerepTypeDescriptors_beginInit(); +void kerepTypeDescriptors_endInit(); +kerepTypeDescriptor typeDescriptor_get(kerepTypeId id); -// frees VoidPtr value or does nothing if type isn't pointer -void Unitype_free(Unitype u); -void printuni(Unitype v); -char* sprintuni(Unitype v); +kerepType_declare(Null); + +kerepType_declare(Char); +kerepType_declare(Bool); +kerepType_declare(Float32); +kerepType_declare(Float64); +kerepType_declare(Int8); +kerepType_declare(UInt8); +kerepType_declare(Int16); +kerepType_declare(UInt16); +kerepType_declare(Int32); +kerepType_declare(UInt32); +kerepType_declare(Int64); +kerepType_declare(UInt64); + +kerepType_declare(CharPtr); +kerepType_declare(BoolPtr); +kerepType_declare(Float32Ptr); +kerepType_declare(Float64Ptr); +kerepType_declare(Int8Ptr); +kerepType_declare(UInt8Ptr); +kerepType_declare(Int16Ptr); +kerepType_declare(UInt16Ptr); +kerepType_declare(Int32Ptr); +kerepType_declare(UInt32Ptr); +kerepType_declare(Int64Ptr); +kerepType_declare(UInt64Ptr); #if __cplusplus } diff --git a/src/base/unitype.c b/src/base/unitype.c new file mode 100644 index 0000000..a2feb13 --- /dev/null +++ b/src/base/unitype.c @@ -0,0 +1,10 @@ +#include "unitype.h" + +void Unitype_free(Unitype u){ + kerepTypeDescriptor type=typeDescriptor_get(u.typeId); + if(type.free_members) + type.free_members(u.VoidPtr); + if(u.allocatedInHeap) + free(u.VoidPtr); +} +void __UnitypePtr_free(Unitype* u) { Unitype_free(&u); } \ No newline at end of file diff --git a/src/base/unitype.h b/src/base/unitype.h new file mode 100644 index 0000000..65c953f --- /dev/null +++ b/src/base/unitype.h @@ -0,0 +1,50 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "types.h" + +typedef struct Unitype{ + union { + int64 Int64; + uint64 UInt64; + double Float64; + bool Bool; + void* VoidPtr; + char Bytes[8]; + }; + kerepTypeId typeId; + bool allocatedInHeap; // should Unitype_free call free() to VoidPtr* +} Unitype; +kerepType_declare(Unitype); +kerepType_declare(UnitypePtr); + + +#define __Uni(TYPE,VAL) (Unitype){\ + .TYPE_NAME=VAL, .type=kerepTypeId_##TYPE, .allocatedInHeap=false} + +#define UniInt64(VAL) __Uni(Int64, VAL) +#define UniUInt64(VAL) __Uni(UInt64, VAL) +#define UniFloat64(VAL) __Uni(Float64, VAL) +#define UniBool(VAL) __Uni(Bool, VAL) + +#define UniPtrStack(TYPE_NAME,VAL) (Unitype){\ + .VoidPtr=VAL, .typeId=kerepTypeId_##TYPE_NAME, .allocatedInHeap=false} +#define UniPtrHeap (TYPE_NAME,VAL) (Unitype){\ + .VoidPtr=VAL, .typeId=kerepTypeId_##TYPE_NAME, .allocatedInHeap=true} + +#define UniNull UniPtrStack(Null, NULL) +#define UniTrue UniBool(true) +#define UniFalse UniBool(false) + +// frees VoidPtr value or does nothing if type isn't pointer +void Unitype_free(Unitype u); +void __UnitypePtr_free(Unitype* u); +void printuni(Unitype v); +char* sprintuni(Unitype v); + +#if __cplusplus +} +#endif \ No newline at end of file From d62405ccff6e002ecfa4db6e15787a7c6927608a Mon Sep 17 00:00:00 2001 From: timerix Date: Thu, 25 Aug 2022 01:37:14 +0600 Subject: [PATCH 08/59] registred all types --- src/Autoarr/Autoarr.c | 30 +++--- src/Autoarr/Autoarr.h | 12 ++- src/Autoarr/Autoarr_KVPair_exported.c | 2 +- src/Autoarr/Autoarr_Unitype_exported.c | 2 +- src/Autoarr/Autoarr_declare.h | 13 +-- src/Autoarr/Autoarr_define.h | 7 +- src/DtsodParser/DtsodV24_deserialize.c | 2 +- src/Hashtable/Hashtable.c | 10 +- src/Hashtable/Hashtable.h | 1 + src/Hashtable/KeyValuePair.c | 8 +- src/Hashtable/KeyValuePair.h | 8 +- src/SearchTree/SearchTree.c | 7 +- src/SearchTree/SearchTree.h | 1 + src/String/StringBuilder.c | 10 +- src/String/StringBuilder.h | 3 + src/base/errors.h | 2 +- src/base/init.c | 131 +++++++++++++++---------- src/base/init.h | 4 +- src/base/std.h | 4 +- src/base/types.c | 4 +- src/base/types.c.old | 113 --------------------- src/base/types.h | 3 +- src/base/unitype.c | 33 ++++++- src/base/unitype.h | 4 +- 24 files changed, 201 insertions(+), 213 deletions(-) delete mode 100644 src/base/types.c.old diff --git a/src/Autoarr/Autoarr.c b/src/Autoarr/Autoarr.c index 48d595a..8dde7ab 100644 --- a/src/Autoarr/Autoarr.c +++ b/src/Autoarr/Autoarr.c @@ -1,19 +1,23 @@ #include "Autoarr.h" -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(float); -Autoarr_define(double); -Autoarr_define(Unitype); +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(Unitype) // right func to clear array of unitype values -void Autoarr_free_Unitype(Autoarr(Unitype)* ar){ +void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr){ Autoarr_foreach(ar, u,Unitype_free(u)); - Autoarr_free(ar); + if(freePtr) Autoarr_free(ar, freePtr); } +void ____Autoarr_free_Unitype_(void* ar) { __Autoarr_free_Unitype_((Autoarr(Unitype)*)ar, false); } \ No newline at end of file diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h index 725b174..88459d6 100644 --- a/src/Autoarr/Autoarr.h +++ b/src/Autoarr/Autoarr.h @@ -7,6 +7,8 @@ extern "C" { #include "Autoarr_declare.h" #include "Autoarr_define.h" +Autoarr_declare(char) +Autoarr_declare(bool) Autoarr_declare(float32) Autoarr_declare(float64) Autoarr_declare(int8) @@ -17,7 +19,6 @@ Autoarr_declare(int32) Autoarr_declare(uint32) Autoarr_declare(int64) Autoarr_declare(uint64) -Autoarr_declare(Unitype) kerepType_declare(AutoarrChar); kerepType_declare(AutoarrBool); @@ -45,8 +46,13 @@ kerepType_declare(AutoarrUInt32Ptr); kerepType_declare(AutoarrInt64Ptr); kerepType_declare(AutoarrUInt64Ptr); -// right func to clear array of unitype values -void Autoarr_free_Unitype(Autoarr(Unitype)* ar); +Autoarr_declare(Unitype) +kerepType_declare(AutoarrUnitype); +kerepType_declare(AutoarrUnitypePtr); + +// this function is injected in kerep_init() +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) {\ diff --git a/src/Autoarr/Autoarr_KVPair_exported.c b/src/Autoarr/Autoarr_KVPair_exported.c index 18fa4aa..8bfbd56 100644 --- a/src/Autoarr/Autoarr_KVPair_exported.c +++ b/src/Autoarr/Autoarr_KVPair_exported.c @@ -10,7 +10,7 @@ EXPORT void CALL kerep_Autoarr_KVPair_create(uint16 max_blocks_count, uint16 max } EXPORT void CALL kerep_Autoarr_KVPair_free(Autoarr_KVPair* ar){ - Autoarr_free_KVPair(ar); + Autoarr_free(ar, true); } EXPORT void CALL kerep_Autoarr_KVPair_get(Autoarr_KVPair* ar, uint32 index, KVPair* output){ diff --git a/src/Autoarr/Autoarr_Unitype_exported.c b/src/Autoarr/Autoarr_Unitype_exported.c index 8ad5efd..2aded2e 100644 --- a/src/Autoarr/Autoarr_Unitype_exported.c +++ b/src/Autoarr/Autoarr_Unitype_exported.c @@ -9,7 +9,7 @@ EXPORT void CALL kerep_Autoarr_Unitype_create(uint16 max_blocks_count, uint16 ma } EXPORT void CALL kerep_Autoarr_Unitype_free(Autoarr_Unitype* ar){ - Autoarr_free_Unitype(ar); + Autoarr_free(ar, true); } EXPORT void CALL kerep_Autoarr_Unitype_get(Autoarr_Unitype* ar, uint32 index, Unitype* output){ diff --git a/src/Autoarr/Autoarr_declare.h b/src/Autoarr/Autoarr_declare.h index 9b15bd7..c9434a3 100644 --- a/src/Autoarr/Autoarr_declare.h +++ b/src/Autoarr/Autoarr_declare.h @@ -15,7 +15,7 @@ typedef struct {\ 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);\ - void (*_free)(struct Autoarr_##type* ar);\ + void (*freear)(struct Autoarr_##type* ar, bool freePtr);\ type* (*toArray)(struct Autoarr_##type* ar);\ } __functions_list_t_##type;\ \ @@ -28,10 +28,13 @@ typedef struct Autoarr_##type{\ __functions_list_t_##type* functions;\ } Autoarr_##type;\ \ -Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length); +Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length);\ +void ____Autoarr_free_##type(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) #define Autoarr_add(autoarr, element)\ autoarr->functions->add(autoarr, element) #define Autoarr_get(autoarr, index)\ @@ -40,10 +43,8 @@ Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_bloc autoarr->functions->getptr(autoarr,index) #define Autoarr_set(autoarr, index, element)\ autoarr->functions->set(autoarr, index, element) -#define Autoarr_free(autoarr)\ - autoarr->functions->_free(autoarr) -#define Autoarr_create(type, max_blocks_count, max_block_length)\ - __Autoarr_create_##type(max_blocks_count, max_block_length) +#define Autoarr_free(autoarr, freePtr)\ + autoarr->functions->freear(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 ffc50e1..b70ad86 100644 --- a/src/Autoarr/Autoarr_define.h +++ b/src/Autoarr/Autoarr_define.h @@ -39,11 +39,14 @@ void __Autoarr_set_##type(Autoarr_##type* ar, uint32 index, type element){\ ar->values[index/ar->max_block_length][index%ar->max_block_length]=element;\ }\ \ -void __Autoarr_free_##type(Autoarr_##type* ar){\ +void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr){\ for(uint16 i=0; iblocks_count;i++)\ free(ar->values[i]); \ free(ar->values);\ - free(ar);\ + if(freePtr) free(ar);\ +}\ +void ____Autoarr_free_##type(void* ar){\ + __Autoarr_free_##type((Autoarr_##type*)ar, false);\ }\ \ type* __Autoarr_toArray_##type(Autoarr_##type* ar){\ diff --git a/src/DtsodParser/DtsodV24_deserialize.c b/src/DtsodParser/DtsodV24_deserialize.c index f8dd4d0..6a9862b 100644 --- a/src/DtsodParser/DtsodV24_deserialize.c +++ b/src/DtsodParser/DtsodV24_deserialize.c @@ -154,7 +154,7 @@ Maybe __ReadList(DeserializeSharedData* shared){ Autoarr(Unitype)* list=Autoarr_create(Unitype,ARR_BC,ARR_BL); bool readingList=true; while (true){ - try(ReadValue((&readingList)), val, Autoarr_free_Unitype(list)) + try(ReadValue((&readingList)), val, Autoarr_free(list, true)) Autoarr_add(list,val.value); if (!readingList){ if(val.value.type==Null) diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index 992ef5e..9a914c4 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -17,10 +17,14 @@ Hashtable* Hashtable_create(){ return ht; } -void Hashtable_free(Hashtable* ht){ +void __Hashtable_free(void* _ht){ + Hashtable* ht=_ht; for(uint16 i=0;ihein];i++) - Autoarr_free_KVPair(ht->rows[i]); + Autoarr_free(ht->rows[i], true); free(ht->rows); +} +void Hashtable_free(Hashtable* ht){ + __Hashtable_free(ht); free(ht); } @@ -43,7 +47,7 @@ void Hashtable_expand(Hashtable* ht){ Autoarr(KVPair)* newar=newrows[newrown]; Autoarr_add(newar,p); } - Autoarr_free(ar); + Autoarr_free(ar, true); } free(ht->rows); diff --git a/src/Hashtable/Hashtable.h b/src/Hashtable/Hashtable.h index ef8b651..5bf4a81 100644 --- a/src/Hashtable/Hashtable.h +++ b/src/Hashtable/Hashtable.h @@ -16,6 +16,7 @@ kerepType_declare(HashtablePtr); Hashtable* Hashtable_create(); void Hashtable_free(Hashtable* ht); +void __Hashtable_free(void* ht); // amount of rows uint16 Hashtable_height(Hashtable* ht); diff --git a/src/Hashtable/KeyValuePair.c b/src/Hashtable/KeyValuePair.c index cbe22dd..93ad54a 100644 --- a/src/Hashtable/KeyValuePair.c +++ b/src/Hashtable/KeyValuePair.c @@ -8,11 +8,15 @@ 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){ +void __Autoarr_free_KVPair_(Autoarr_KVPair* ar, bool freePtr){ Autoarr_foreach(ar,k,KVPair_free(k)); - Autoarr_free(ar); + Autoarr_free(ar, freePtr); +} +void ____Autoarr_free_KVPair_(void* ar){ + __Autoarr_free_KVPair_((Autoarr_KVPair*)ar, false); } void printkvp(KVPair p){ diff --git a/src/Hashtable/KeyValuePair.h b/src/Hashtable/KeyValuePair.h index 19cc277..48e606f 100644 --- a/src/Hashtable/KeyValuePair.h +++ b/src/Hashtable/KeyValuePair.h @@ -11,14 +11,20 @@ typedef struct KVPair{ char* key; Unitype value; } KVPair; +kerepType_declare(KVPair); +kerepType_declare(KVPairPtr); Autoarr_declare(KVPair) +kerepType_declare(AutoarrKVPair); +kerepType_declare(AutoarrKVPairPtr); // proper way to clear a KVP void KVPair_free(KVPair p); +void __KVPair_free(void* p); // func to clear KVP array -void Autoarr_free_KVPair(Autoarr_KVPair* ar); +void __Autoarr_free_KVPair_(Autoarr_KVPair* ar, bool freePtr); +void ____Autoarr_free_KVPair_(void* ar); void printkvp(KVPair p); diff --git a/src/SearchTree/SearchTree.c b/src/SearchTree/SearchTree.c index e7cfac6..0c95125 100644 --- a/src/SearchTree/SearchTree.c +++ b/src/SearchTree/SearchTree.c @@ -8,7 +8,8 @@ STNode* STNode_create(){ return node; } -void STNode_free(STNode* node){ +void __STNode_free(void* _node){ + STNode* node=_node; if (!node) throw(ERR_NULLPTR); if(node->branches){ for(uint8 n32 = 0;n32<8;n32++){ @@ -34,6 +35,10 @@ void STNode_free(STNode* node){ Unitype_free(node->value); free(node); } +void STNode_free(STNode* node){ + __STNode_free(node); + free(node); +} typedef struct {uint8 n32, n4, rem;} indexes3; diff --git a/src/SearchTree/SearchTree.h b/src/SearchTree/SearchTree.h index b02cbe0..c0f1c3f 100644 --- a/src/SearchTree/SearchTree.h +++ b/src/SearchTree/SearchTree.h @@ -16,6 +16,7 @@ kerepType_declare(STNodePtr); STNode* STNode_create(); void STNode_free(STNode* node); +void __STNode_free(void* node); void ST_push(STNode* node, char* key, Unitype value); void ST_pushString(STNode* node, string key, Unitype value); diff --git a/src/String/StringBuilder.c b/src/String/StringBuilder.c index c0b46a9..652c9b8 100644 --- a/src/String/StringBuilder.c +++ b/src/String/StringBuilder.c @@ -17,7 +17,7 @@ void complete_buf(StringBuilder* b){ str.ptr[i++]=c; })); Autoarr_add(b->compl_bufs,str); - Autoarr_free(b->curr_buf); + Autoarr_free(b->curr_buf, true); b->curr_buf=Autoarr_create(int8,BL_C,BL_L); } @@ -34,9 +34,13 @@ StringBuilder* StringBuilder_create(){ return b; } +void __StringBuilder_free(void* _b){ + StringBuilder* b=_b; + if(b->compl_bufs) Autoarr_free(b->compl_bufs, true); + Autoarr_free(b->curr_buf, true); +} void StringBuilder_free(StringBuilder* b){ - if(b->compl_bufs) Autoarr_free(b->compl_bufs); - Autoarr_free(b->curr_buf); + __StringBuilder_free(b); free(b); } diff --git a/src/String/StringBuilder.h b/src/String/StringBuilder.h index 8e19420..0fe1696 100644 --- a/src/String/StringBuilder.h +++ b/src/String/StringBuilder.h @@ -13,9 +13,12 @@ typedef struct StringBuilder{ Autoarr(string)* compl_bufs; Autoarr(int8)* curr_buf; } StringBuilder; +kerepType_declare(StringBuilder); +kerepType_declare(StringBuilderPtr); StringBuilder* StringBuilder_create(void); void StringBuilder_free(StringBuilder* b); +void __StringBuilder_free(void* b); // Joins all strings from compl_bufs. // Returns zero-terminated string. // No need to call string_extract()! diff --git a/src/base/errors.h b/src/base/errors.h index de7c713..4109782 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -5,7 +5,7 @@ extern "C" { #endif #include "std.h" -#include "types.h" +#include "unitype.h" typedef enum ErrorId { SUCCESS, // not an error diff --git a/src/base/init.c b/src/base/init.c index f764aad..73073ab 100644 --- a/src/base/init.c +++ b/src/base/init.c @@ -1,66 +1,95 @@ #include "base.h" +#include "../Autoarr/Autoarr.h" +#include "../SearchTree/SearchTree.h" +#include "../Hashtable/Hashtable.h" +#include "../String/StringBuilder.h" -void kerepInit(){ +void kerepTypeDescriptors_initKerepTypes(){ + // null kerepType_register(NULL, Null, NULL); - - kerepType_register(char, Char, NULL); - kerepType_register(bool, Bool, NULL); + // base types + kerepType_register(char, Char, NULL); + kerepType_register(bool, Bool, NULL); kerepType_register(float32, Float32, NULL); kerepType_register(float64, Float64, NULL); - kerepType_register(int8, Int8, NULL); - kerepType_register(uint8, UInt8, NULL); - kerepType_register(int16, Int16, NULL); - kerepType_register(uint16, UInt16, NULL); - kerepType_register(int32, Int32, NULL); - kerepType_register(uint32, UInt32, NULL); - kerepType_register(int64, Int64, NULL); - kerepType_register(uint64, UInt64, NULL); - - kerepType_register(char*, CharPtr, NULL); - kerepType_register(bool*, BoolPtr, NULL); + kerepType_register(int8, Int8, NULL); + kerepType_register(uint8, UInt8, NULL); + kerepType_register(int16, Int16, NULL); + kerepType_register(uint16, UInt16, NULL); + kerepType_register(int32, Int32, NULL); + kerepType_register(uint32, UInt32, NULL); + kerepType_register(int64, Int64, NULL); + kerepType_register(uint64, UInt64, NULL); + // base type pointers + kerepType_register(char*, CharPtr, NULL); + kerepType_register(bool*, BoolPtr, NULL); kerepType_register(float32*, Float32Ptr, NULL); kerepType_register(float64*, Float64Ptr, NULL); - kerepType_register(int8*, Int8Ptr, NULL); - kerepType_register(uint8*, UInt8Ptr, NULL); - kerepType_register(int16*, Int16Ptr, NULL); - kerepType_register(uint16*, UInt16Ptr, NULL); - kerepType_register(int32*, Int32Ptr, NULL); - kerepType_register(uint32*, UInt32Ptr, NULL); - kerepType_register(int64*, Int64Ptr, NULL); - kerepType_register(uint64*, UInt64Ptr, NULL); + kerepType_register(int8*, Int8Ptr, NULL); + kerepType_register(uint8*, UInt8Ptr, NULL); + kerepType_register(int16*, Int16Ptr, NULL); + kerepType_register(uint16*, UInt16Ptr, NULL); + kerepType_register(int32*, Int32Ptr, NULL); + kerepType_register(uint32*, UInt32Ptr, NULL); + kerepType_register(int64*, Int64Ptr, NULL); + kerepType_register(uint64*, UInt64Ptr, NULL); + // base type autoarrs + kerepType_register(Autoarr_char, AutoarrChar, ____Autoarr_free_char); + kerepType_register(Autoarr_bool, AutoarrBool, ____Autoarr_free_bool); + kerepType_register(Autoarr_float32, AutoarrFloat32, ____Autoarr_free_float32); + kerepType_register(Autoarr_float64, AutoarrFloat64, ____Autoarr_free_float64); + kerepType_register(Autoarr_int8, AutoarrInt8, ____Autoarr_free_int8); + kerepType_register(Autoarr_uint8, AutoarrUInt8, ____Autoarr_free_uint8); + kerepType_register(Autoarr_int16, AutoarrInt16, ____Autoarr_free_int16); + kerepType_register(Autoarr_uint16, AutoarrUInt16, ____Autoarr_free_uint16); + kerepType_register(Autoarr_int32, AutoarrInt32, ____Autoarr_free_int32); + kerepType_register(Autoarr_uint32, AutoarrUInt32, ____Autoarr_free_uint32); + kerepType_register(Autoarr_int64, AutoarrInt64, ____Autoarr_free_int64); + kerepType_register(Autoarr_uint64, AutoarrUInt64, ____Autoarr_free_uint64); + // base type autoarr pointers + kerepType_register(Autoarr_char*, AutoarrCharPtr, ____Autoarr_free_char); + kerepType_register(Autoarr_bool*, AutoarrBoolPtr, ____Autoarr_free_bool); + kerepType_register(Autoarr_float32*, AutoarrFloat32Ptr, ____Autoarr_free_float32); + kerepType_register(Autoarr_float64*, AutoarrFloat64Ptr, ____Autoarr_free_float64); + kerepType_register(Autoarr_int8*, AutoarrInt8Ptr, ____Autoarr_free_int8); + kerepType_register(Autoarr_uint8*, AutoarrUInt8Ptr, ____Autoarr_free_uint8); + kerepType_register(Autoarr_int16*, AutoarrInt16Ptr, ____Autoarr_free_int16); + kerepType_register(Autoarr_uint16*, AutoarrUInt16Ptr, ____Autoarr_free_uint16); + kerepType_register(Autoarr_int32*, AutoarrInt32Ptr, ____Autoarr_free_int32); + kerepType_register(Autoarr_uint32*, AutoarrUInt32Ptr, ____Autoarr_free_uint32); + kerepType_register(Autoarr_int64*, AutoarrInt64Ptr, ____Autoarr_free_int64); + kerepType_register(Autoarr_uint64*, AutoarrUInt64Ptr, ____Autoarr_free_uint64); + + // Unitype kerepType_register(Unitype, Unitype, __UnitypePtr_free); kerepType_register(Unitype*, UnitypePtr, __UnitypePtr_free); + kerepType_register(Autoarr_Unitype, AutoarrUnitype, ____Autoarr_free_Unitype_); + kerepType_register(Autoarr_Unitype*, AutoarrUnitypePtr, ____Autoarr_free_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); - kerepType_register(, AutoarrChar, Autoarr_free); - kerepType_register(, AutoarrBool, Autoarr_free); - kerepType_register(, AutoarrFloat32, Autoarr_free); - kerepType_register(, AutoarrFloat64, Autoarr_free); - kerepType_register(, AutoarrInt8, Autoarr_free); - kerepType_register(, AutoarrUInt8, Autoarr_free); - kerepType_register(, AutoarrInt16, Autoarr_free); - kerepType_register(, AutoarrUInt16, Autoarr_free); - kerepType_register(, AutoarrInt32, Autoarr_free); - kerepType_register(, AutoarrUInt32, Autoarr_free); - kerepType_register(, AutoarrInt64, Autoarr_free); - kerepType_register(, AutoarrUInt64, Autoarr_free); + // SearchTreeNode + kerepType_register(STNode, STNode, __STNode_free); + kerepType_register(STNode*, STNodePtr, __STNode_free); - kerepType_register(, AutoarrCharPtr, Autoarr_free); - kerepType_register(, AutoarrBoolPtr, Autoarr_free); - kerepType_register(, AutoarrFloat32Ptr, Autoarr_free); - kerepType_register(, AutoarrFloat64Ptr, Autoarr_free); - kerepType_register(, AutoarrInt8Ptr, Autoarr_free); - kerepType_register(, AutoarrUInt8Ptr, Autoarr_free); - kerepType_register(, AutoarrInt16Ptr, Autoarr_free); - kerepType_register(, AutoarrUInt16Ptr, Autoarr_free); - kerepType_register(, AutoarrInt32Ptr, Autoarr_free); - kerepType_register(, AutoarrUInt32Ptr, Autoarr_free); - kerepType_register(, AutoarrInt64Ptr, Autoarr_free); - kerepType_register(, AutoarrUInt64Ptr, Autoarr_free); + // KeyValuePair + kerepType_register(KVPair, KVPair, __KVPair_free); + kerepType_register(KVPair*, KVPairPtr, __KVPair_free); + kerepType_register(Autoarr_KVPair, AutoarrKVPair, ____Autoarr_free_KVPair_); + kerepType_register(Autoarr_KVPair*, AutoarrKVPairPtr, ____Autoarr_free_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); - kerepType_register(, STNode, STNode_free); - kerepType_register(, STNodePtr, STNode_free); + // Hashtable + kerepType_register(Hashtable, Hashtable, __Hashtable_free); + kerepType_register(Hashtable*, HashtablePtr, __Hashtable_free); - kerepType_register(, Hashtable, Hashtable_free); - kerepType_register(, HashtablePtr, Hashtable_free); + // StringBuilder + kerepType_register(StringBuilder, StringBuilder, __StringBuilder_free); + kerepType_register(StringBuilder*, StringBuilderPtr, __StringBuilder_free); } diff --git a/src/base/init.h b/src/base/init.h index 0faa5f2..dfe5e69 100644 --- a/src/base/init.h +++ b/src/base/init.h @@ -4,8 +4,8 @@ extern "C" { #endif -// call this between kerepTypesInitBegin() and kerepTypesInitEnd() -void kerepInit(); +// call this between kerepTypeDescriptors_beginInit() and kerepTypeDescriptors_endInit() +void kerepTypeDescriptors_initKerepTypes(); #if __cplusplus } diff --git a/src/base/std.h b/src/base/std.h index baf6a86..de0b9e9 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -44,7 +44,6 @@ typedef double float64; #pragma GCC error "unknown compiler" #endif - #ifdef _MSC_VER #define IFWIN(YES, NO) YES #define IFMSC(YES, NO) YES @@ -58,6 +57,9 @@ typedef double float64; #pragma GCC error "unknown compiler" #endif +#ifndef sprintf_s + #define sprintf_s(BUF, BUFSIZE, FORMAT, ...) sprintf(BUF, FORMAT, ## __VA_ARGS__) +#endif #if __cplusplus } diff --git a/src/base/types.c b/src/base/types.c index 2f0e1a7..1c7a055 100644 --- a/src/base/types.c +++ b/src/base/types.c @@ -22,7 +22,7 @@ void kerepTypeDescriptors_beginInit(){ void kerepTypeDescriptors_endInit(){ typeDescriptors=Autoarr_toArray(__kerepTypeDescriptors); - Autoarr_free(__kerepTypeDescriptors); + Autoarr_free(__kerepTypeDescriptors,true); if(typeDescriptors==NULL) throw(ERR_NULLPTR); } void __kerepType_register(char* name, int16 size, void (*free_members)(void*)){ @@ -35,6 +35,6 @@ void __kerepType_register(char* name, int16 size, void (*free_members)(void*)){ Autoarr_add(__kerepTypeDescriptors, typeDesc); } -kerepTypeDescriptor typeDescriptor_get(kerepTypeId id){ +kerepTypeDescriptor kerepTypeDescriptor_get(kerepTypeId id){ return typeDescriptors[id]; } diff --git a/src/base/types.c.old b/src/base/types.c.old deleted file mode 100644 index 3c6ef1e..0000000 --- a/src/base/types.c.old +++ /dev/null @@ -1,113 +0,0 @@ -#include "types.h" -#include "errors.h" -#include "../Autoarr/Autoarr.h" -#include "../Hashtable/Hashtable.h" -#include "../SearchTree/SearchTree.h" - -// frees VoidPtr value or does nothing if type isn't pointer -void Unitype_free(Unitype u){ - switch (u.type) { - case Null: - case Float32: - case Float64: - case Char: - case Bool: - case Int8: - case UInt8: - case Int16: - case UInt16: - case Int32: - case UInt32: - case Int64: - case UInt64: - break; - case Int8Ptr: - case UInt8Ptr: - case Int16Ptr: - case UInt16Ptr: - case Int32Ptr: - case UInt32Ptr: - case Int64Ptr: - case UInt64Ptr: - case CharPtr: - free(u.VoidPtr); - break; - case HashtablePtr: - Hashtable_free(u.VoidPtr); - break; - case STNodePtr: - STNode_free(u.VoidPtr); - break; - case AutoarrInt8Ptr: - __Autoarr_free_int8(u.VoidPtr); - break; - case AutoarrUInt8Ptr: - __Autoarr_free_uint8(u.VoidPtr); - break; - case AutoarrInt16Ptr: - __Autoarr_free_int16(u.VoidPtr); - break; - case AutoarrUInt16Ptr: - __Autoarr_free_uint16(u.VoidPtr); - break; - case AutoarrInt32Ptr: - __Autoarr_free_int32(u.VoidPtr); - break; - case AutoarrUInt32Ptr: - __Autoarr_free_uint32(u.VoidPtr); - break; - case AutoarrInt64Ptr: - __Autoarr_free_int64(u.VoidPtr); - break; - case AutoarrUInt64Ptr: - __Autoarr_free_uint64(u.VoidPtr); - break; - case AutoarrUnitypePtr: - Autoarr_free_Unitype(u.VoidPtr); - break; - case AutoarrKVPairPtr: - Autoarr_free_KVPair(u.VoidPtr); - break; - default: throw(ERR_WRONGTYPE); - } -} - -#define BUFSIZE 64 -char* sprintuni(Unitype v){ - char* buf=malloc(BUFSIZE); - IFMSC( - switch (v.type) { - case Null: sprintf_s(buf, BUFSIZE, "{Null}");break; - case Float64: sprintf_s(buf, BUFSIZE, "{%s : %lf}", my_type_name(v.type),v.Float64);break; - case Bool: - case UInt64: sprintf_s(buf, BUFSIZE, "{%s : %lu}", my_type_name(v.type),v.UInt64);break; - case Int64: sprintf_s(buf, BUFSIZE, "{%s : %ld}", my_type_name(v.type),v.Int64);break; - case CharPtr: ; - size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2; - buf=realloc(buf, newBUFSIZE); - sprintf_s(buf, newBUFSIZE, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr); - break; - default: sprintf_s(buf, BUFSIZE, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break; - }, - switch (v.type) { - case Null: sprintf(buf, "{Null}"); break; - case Float64: sprintf(buf, "{%s : %lf}", my_type_name(v.type),v.Float64); break; - case Bool: - case UInt64: sprintf(buf, "{%s : " IFWIN("%llu", "%lu") "}", my_type_name(v.type),v.UInt64); break; - case Int64: sprintf(buf, "{%s : " IFWIN("%lld", "%ld") "}", my_type_name(v.type),v.Int64); break; - case CharPtr: ; - size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2; - buf=realloc(buf, newBUFSIZE); - sprintf(buf, "{%s : \"%s\"}", my_type_name(v.type),(char*)v.VoidPtr); - break; - default: sprintf(buf, "{%s : %p}", my_type_name(v.type),v.VoidPtr);break; - } - ); - return buf; -} - -void printuni(Unitype v){ - char* s=sprintuni(v); - fputs(s, stdout); - free(s); -} \ No newline at end of file diff --git a/src/base/types.h b/src/base/types.h index 2722e17..2e58287 100644 --- a/src/base/types.h +++ b/src/base/types.h @@ -7,6 +7,7 @@ extern "C" { #include "std.h" typedef uint16 kerepTypeId; + typedef struct kerepTypeDescriptor{ void (*free_members)(void*); // NULL or function which frees all struct members char* name; @@ -26,7 +27,7 @@ void __kerepType_register(char* name, int16 size, void (*free_members)(void*)); void kerepTypeDescriptors_beginInit(); void kerepTypeDescriptors_endInit(); -kerepTypeDescriptor typeDescriptor_get(kerepTypeId id); +kerepTypeDescriptor kerepTypeDescriptor_get(kerepTypeId id); kerepType_declare(Null); diff --git a/src/base/unitype.c b/src/base/unitype.c index a2feb13..1f18751 100644 --- a/src/base/unitype.c +++ b/src/base/unitype.c @@ -1,10 +1,37 @@ -#include "unitype.h" +#include "base.h" void Unitype_free(Unitype u){ - kerepTypeDescriptor type=typeDescriptor_get(u.typeId); + kerepTypeDescriptor type=kerepTypeDescriptor_get(u.typeId); if(type.free_members) type.free_members(u.VoidPtr); if(u.allocatedInHeap) free(u.VoidPtr); } -void __UnitypePtr_free(Unitype* u) { Unitype_free(&u); } \ No newline at end of file +void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); } + +#define BUFSIZE 64 +char* sprintuni(Unitype v){ + char* buf=malloc(BUFSIZE); + kerepTypeDescriptor type=kerepTypeDescriptor_get(v.typeId); + if(v.typeId==kerepTypeId_Null) + sprintf_s(buf, BUFSIZE, "{Null}"); + else if(v.typeId==kerepTypeId_Float64) + sprintf_s(buf, BUFSIZE, "{%s ) %lf}", type.name,v.Float64); + else if(v.typeId==kerepTypeId_Bool || v.typeId==kerepTypeId_UInt64) + sprintf_s(buf, BUFSIZE, "{%s ) " IFWIN("%llu", "%lu") "}", type.name,v.UInt64); + else if(v.typeId==kerepTypeId_Int64) + sprintf_s(buf, BUFSIZE, "{%s ) " IFWIN("%lld", "%ld") "}", type.name,v.Int64); + else if(v.typeId==kerepTypeId_CharPtr){ + 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); + fputs(s, stdout); + free(s); +} \ No newline at end of file diff --git a/src/base/unitype.h b/src/base/unitype.h index 65c953f..0d28ed8 100644 --- a/src/base/unitype.h +++ b/src/base/unitype.h @@ -23,7 +23,7 @@ kerepType_declare(UnitypePtr); #define __Uni(TYPE,VAL) (Unitype){\ - .TYPE_NAME=VAL, .type=kerepTypeId_##TYPE, .allocatedInHeap=false} + .TYPE_NAME=VAL, .typeId=kerepTypeId_##TYPE, .allocatedInHeap=false} #define UniInt64(VAL) __Uni(Int64, VAL) #define UniUInt64(VAL) __Uni(UInt64, VAL) @@ -41,7 +41,7 @@ kerepType_declare(UnitypePtr); // frees VoidPtr value or does nothing if type isn't pointer void Unitype_free(Unitype u); -void __UnitypePtr_free(Unitype* u); +void __UnitypePtr_free(void* u); void printuni(Unitype v); char* sprintuni(Unitype v); From b627affbde23653ea29d689f99562b48680b9a61 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 26 Aug 2022 01:45:02 +0600 Subject: [PATCH 09/59] fixed a lot of broken stuff --- !TODO.md | 3 + make | 1 + make.error.log | 65 +++++++++++ make.log | 71 ++++++++++++ src/Autoarr/Autoarr.h | 52 ++++----- src/DtsodParser/DtsodV24_deserialize.c | 18 +-- src/DtsodParser/DtsodV24_serialize.c | 147 ++++++++++++------------- src/Hashtable/Hashtable.c | 2 +- src/Hashtable/Hashtable.h | 4 +- src/Hashtable/KeyValuePair.h | 8 +- src/SearchTree/SearchTree.c | 2 +- src/SearchTree/SearchTree.h | 4 +- src/String/StringBuilder.h | 4 +- src/base/init.c | 126 ++++++++++----------- src/base/types.h | 62 ++++++----- src/base/unitype.h | 26 ++--- tests/main.cpp | 3 + tests/test_autoarr.c | 2 +- tests/test_dtsod.c | 2 +- tests/test_hash_functions.c | 2 +- tests/test_marshalling.c | 2 +- tests/test_searchtree.c | 12 +- 22 files changed, 380 insertions(+), 238 deletions(-) create mode 100644 !TODO.md create mode 100644 make create mode 100644 make.error.log create mode 100644 make.log diff --git a/!TODO.md b/!TODO.md new file mode 100644 index 0000000..3a6bbe1 --- /dev/null +++ b/!TODO.md @@ -0,0 +1,3 @@ +- add toString() to type descriptor +- find better way to check typeId without hundreds of if() statements +- update autoarr to use type descriptor \ No newline at end of file diff --git a/make b/make new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/make @@ -0,0 +1 @@ + diff --git a/make.error.log b/make.error.log new file mode 100644 index 0000000..b73e8ea --- /dev/null +++ b/make.error.log @@ -0,0 +1,65 @@ +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24.c.o:DtsodV24.c:(.rdata$.refptr.kerepTypeId_Null[.refptr.kerepTypeId_Null]+0x0): undefined reference to `kerepTypeId_Null' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24_deserialize.c.o:DtsodV24_deserialize.c:(.rdata$.refptr.kerepTypeId_AutoarrUnitypePtr[.refptr.kerepTypeId_AutoarrUnitypePtr]+0x0): undefined reference to `kerepTypeId_AutoarrUnitypePtr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24_deserialize.c.o:DtsodV24_deserialize.c:(.rdata$.refptr.kerepTypeId_HashtablePtr[.refptr.kerepTypeId_HashtablePtr]+0x0): undefined reference to `kerepTypeId_HashtablePtr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24_deserialize.c.o:DtsodV24_deserialize.c:(.rdata$.refptr.kerepTypeId_Int64[.refptr.kerepTypeId_Int64]+0x0): undefined reference to `kerepTypeId_Int64' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24_deserialize.c.o:DtsodV24_deserialize.c:(.rdata$.refptr.kerepTypeId_UInt64[.refptr.kerepTypeId_UInt64]+0x0): undefined reference to `kerepTypeId_UInt64' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24_deserialize.c.o:DtsodV24_deserialize.c:(.rdata$.refptr.kerepTypeId_Float64[.refptr.kerepTypeId_Float64]+0x0): undefined reference to `kerepTypeId_Float64' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24_deserialize.c.o:DtsodV24_deserialize.c:(.rdata$.refptr.kerepTypeId_Bool[.refptr.kerepTypeId_Bool]+0x0): undefined reference to `kerepTypeId_Bool' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24_deserialize.c.o:DtsodV24_deserialize.c:(.rdata$.refptr.kerepTypeId_CharPtr[.refptr.kerepTypeId_CharPtr]+0x0): undefined reference to `kerepTypeId_CharPtr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_StringBuilderPtr[.refptr.kerepTypeId_StringBuilderPtr]+0x0): undefined reference to `kerepTypeId_StringBuilderPtr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_StringBuilder[.refptr.kerepTypeId_StringBuilder]+0x0): undefined reference to `kerepTypeId_StringBuilder' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Hashtable[.refptr.kerepTypeId_Hashtable]+0x0): undefined reference to `kerepTypeId_Hashtable' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrKVPairPtr[.refptr.kerepTypeId_AutoarrKVPairPtr]+0x0): undefined reference to `kerepTypeId_AutoarrKVPairPtr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrKVPair[.refptr.kerepTypeId_AutoarrKVPair]+0x0): undefined reference to `kerepTypeId_AutoarrKVPair' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_KVPairPtr[.refptr.kerepTypeId_KVPairPtr]+0x0): undefined reference to `kerepTypeId_KVPairPtr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_KVPair[.refptr.kerepTypeId_KVPair]+0x0): undefined reference to `kerepTypeId_KVPair' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_STNodePtr[.refptr.kerepTypeId_STNodePtr]+0x0): undefined reference to `kerepTypeId_STNodePtr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_STNode[.refptr.kerepTypeId_STNode]+0x0): undefined reference to `kerepTypeId_STNode' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUnitype[.refptr.kerepTypeId_AutoarrUnitype]+0x0): undefined reference to `kerepTypeId_AutoarrUnitype' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UnitypePtr[.refptr.kerepTypeId_UnitypePtr]+0x0): undefined reference to `kerepTypeId_UnitypePtr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Unitype[.refptr.kerepTypeId_Unitype]+0x0): undefined reference to `kerepTypeId_Unitype' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt64Ptr[.refptr.kerepTypeId_AutoarrUInt64Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrUInt64Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt64Ptr[.refptr.kerepTypeId_AutoarrInt64Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrInt64Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt32Ptr[.refptr.kerepTypeId_AutoarrUInt32Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrUInt32Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt32Ptr[.refptr.kerepTypeId_AutoarrInt32Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrInt32Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt16Ptr[.refptr.kerepTypeId_AutoarrUInt16Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrUInt16Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt16Ptr[.refptr.kerepTypeId_AutoarrInt16Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrInt16Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt8Ptr[.refptr.kerepTypeId_AutoarrUInt8Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrUInt8Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt8Ptr[.refptr.kerepTypeId_AutoarrInt8Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrInt8Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrFloat64Ptr[.refptr.kerepTypeId_AutoarrFloat64Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrFloat64Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrFloat32Ptr[.refptr.kerepTypeId_AutoarrFloat32Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrFloat32Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrBoolPtr[.refptr.kerepTypeId_AutoarrBoolPtr]+0x0): undefined reference to `kerepTypeId_AutoarrBoolPtr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrCharPtr[.refptr.kerepTypeId_AutoarrCharPtr]+0x0): undefined reference to `kerepTypeId_AutoarrCharPtr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt64[.refptr.kerepTypeId_AutoarrUInt64]+0x0): undefined reference to `kerepTypeId_AutoarrUInt64' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt64[.refptr.kerepTypeId_AutoarrInt64]+0x0): undefined reference to `kerepTypeId_AutoarrInt64' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt32[.refptr.kerepTypeId_AutoarrUInt32]+0x0): undefined reference to `kerepTypeId_AutoarrUInt32' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt32[.refptr.kerepTypeId_AutoarrInt32]+0x0): undefined reference to `kerepTypeId_AutoarrInt32' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt16[.refptr.kerepTypeId_AutoarrUInt16]+0x0): undefined reference to `kerepTypeId_AutoarrUInt16' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt16[.refptr.kerepTypeId_AutoarrInt16]+0x0): undefined reference to `kerepTypeId_AutoarrInt16' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt8[.refptr.kerepTypeId_AutoarrUInt8]+0x0): undefined reference to `kerepTypeId_AutoarrUInt8' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt8[.refptr.kerepTypeId_AutoarrInt8]+0x0): undefined reference to `kerepTypeId_AutoarrInt8' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrFloat64[.refptr.kerepTypeId_AutoarrFloat64]+0x0): undefined reference to `kerepTypeId_AutoarrFloat64' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrFloat32[.refptr.kerepTypeId_AutoarrFloat32]+0x0): undefined reference to `kerepTypeId_AutoarrFloat32' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrBool[.refptr.kerepTypeId_AutoarrBool]+0x0): undefined reference to `kerepTypeId_AutoarrBool' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrChar[.refptr.kerepTypeId_AutoarrChar]+0x0): undefined reference to `kerepTypeId_AutoarrChar' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UInt64Ptr[.refptr.kerepTypeId_UInt64Ptr]+0x0): undefined reference to `kerepTypeId_UInt64Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Int64Ptr[.refptr.kerepTypeId_Int64Ptr]+0x0): undefined reference to `kerepTypeId_Int64Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UInt32Ptr[.refptr.kerepTypeId_UInt32Ptr]+0x0): undefined reference to `kerepTypeId_UInt32Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Int32Ptr[.refptr.kerepTypeId_Int32Ptr]+0x0): undefined reference to `kerepTypeId_Int32Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UInt16Ptr[.refptr.kerepTypeId_UInt16Ptr]+0x0): undefined reference to `kerepTypeId_UInt16Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Int16Ptr[.refptr.kerepTypeId_Int16Ptr]+0x0): undefined reference to `kerepTypeId_Int16Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UInt8Ptr[.refptr.kerepTypeId_UInt8Ptr]+0x0): undefined reference to `kerepTypeId_UInt8Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Int8Ptr[.refptr.kerepTypeId_Int8Ptr]+0x0): undefined reference to `kerepTypeId_Int8Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Float64Ptr[.refptr.kerepTypeId_Float64Ptr]+0x0): undefined reference to `kerepTypeId_Float64Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Float32Ptr[.refptr.kerepTypeId_Float32Ptr]+0x0): undefined reference to `kerepTypeId_Float32Ptr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_BoolPtr[.refptr.kerepTypeId_BoolPtr]+0x0): undefined reference to `kerepTypeId_BoolPtr' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UInt32[.refptr.kerepTypeId_UInt32]+0x0): undefined reference to `kerepTypeId_UInt32' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Int32[.refptr.kerepTypeId_Int32]+0x0): undefined reference to `kerepTypeId_Int32' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UInt16[.refptr.kerepTypeId_UInt16]+0x0): undefined reference to `kerepTypeId_UInt16' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Int16[.refptr.kerepTypeId_Int16]+0x0): undefined reference to `kerepTypeId_Int16' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UInt8[.refptr.kerepTypeId_UInt8]+0x0): undefined reference to `kerepTypeId_UInt8' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Int8[.refptr.kerepTypeId_Int8]+0x0): undefined reference to `kerepTypeId_Int8' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Float32[.refptr.kerepTypeId_Float32]+0x0): undefined reference to `kerepTypeId_Float32' +C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Char[.refptr.kerepTypeId_Char]+0x0): undefined reference to `kerepTypeId_Char' +collect2.exe: error: ld returned 1 exit status +make: *** [Makefile:3: build_test] Error 1 diff --git a/make.log b/make.log new file mode 100644 index 0000000..68f1bc3 --- /dev/null +++ b/make.log @@ -0,0 +1,71 @@ + H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H =============[build_test]============= +clearing bin +clearing obj +-------------[compile_c]-------------- +compiler: gcc +standard: c11 +warnings: -Wall -Wno-discarded-qualifiers +args: -O2 +sources: src/Autoarr/Autoarr.c +src/Autoarr/Autoarr_KVPair_exported.c +src/Autoarr/Autoarr_Unitype_exported.c +src/base/cptr.c +src/base/errors.c +src/base/init.c +src/base/types.c +src/base/unitype.c +src/DtsodParser/DtsodV24.c +src/DtsodParser/DtsodV24_deserialize.c +src/DtsodParser/DtsodV24_exported.c +src/DtsodParser/DtsodV24_serialize.c +src/HashFunctions/hash.c +src/Hashtable/Hashtable.c +src/Hashtable/KeyValuePair.c +src/SearchTree/SearchTree.c +src/String/string.c +src/String/StringBuilder.c tests/test_autoarr.c +tests/test_dtsod.c +tests/test_hashtable.c +tests/test_hash_functions.c +tests/test_marshalling.c +tests/test_safethrow.c +tests/test_searchtree.c +tests/test_string.c +some error happened +some error happened +some error happened +some error happened +some error happened +some error happened +some error happened +some error happened +some error happened +------------[compile_cpp]------------- +compiler: g++ +standard: c++17 +warnings: -Wall -Wno-unused-variable -Wno-return-type +args: -O2 +sources:  tests/main.cpp +----------------[link]---------------- +args: -O2 +outfile: bin/kerep.com +objects: obj/Autoarr.c.o +obj/Autoarr_KVPair_exported.c.o +obj/Autoarr_Unitype_exported.c.o +obj/cptr.c.o +obj/DtsodV24_exported.c.o +obj/errors.c.o +obj/hash.c.o +obj/init.c.o +obj/KeyValuePair.c.o +obj/main.cpp.o +obj/string.c.o +obj/StringBuilder.c.o +obj/test_autoarr.c.o +obj/test_dtsod.c.o +obj/test_hash_functions.c.o +obj/test_string.c.o +obj/types.c.o +obj/unitype.c.o +some error happened + \ No newline at end of file diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h index 88459d6..d96fcf6 100644 --- a/src/Autoarr/Autoarr.h +++ b/src/Autoarr/Autoarr.h @@ -20,35 +20,35 @@ Autoarr_declare(uint32) Autoarr_declare(int64) Autoarr_declare(uint64) -kerepType_declare(AutoarrChar); -kerepType_declare(AutoarrBool); -kerepType_declare(AutoarrFloat32); -kerepType_declare(AutoarrFloat64); -kerepType_declare(AutoarrInt8); -kerepType_declare(AutoarrUInt8); -kerepType_declare(AutoarrInt16); -kerepType_declare(AutoarrUInt16); -kerepType_declare(AutoarrInt32); -kerepType_declare(AutoarrUInt32); -kerepType_declare(AutoarrInt64); -kerepType_declare(AutoarrUInt64); +kerepTypeId_declare(kerepTypeId_AutoarrChar); +kerepTypeId_declare(kerepTypeId_AutoarrBool); +kerepTypeId_declare(kerepTypeId_AutoarrFloat32); +kerepTypeId_declare(kerepTypeId_AutoarrFloat64); +kerepTypeId_declare(kerepTypeId_AutoarrInt8); +kerepTypeId_declare(kerepTypeId_AutoarrUInt8); +kerepTypeId_declare(kerepTypeId_AutoarrInt16); +kerepTypeId_declare(kerepTypeId_AutoarrUInt16); +kerepTypeId_declare(kerepTypeId_AutoarrInt32); +kerepTypeId_declare(kerepTypeId_AutoarrUInt32); +kerepTypeId_declare(kerepTypeId_AutoarrInt64); +kerepTypeId_declare(kerepTypeId_AutoarrUInt64); -kerepType_declare(AutoarrCharPtr); -kerepType_declare(AutoarrBoolPtr); -kerepType_declare(AutoarrFloat32Ptr); -kerepType_declare(AutoarrFloat64Ptr); -kerepType_declare(AutoarrInt8Ptr); -kerepType_declare(AutoarrUInt8Ptr); -kerepType_declare(AutoarrInt16Ptr); -kerepType_declare(AutoarrUInt16Ptr); -kerepType_declare(AutoarrInt32Ptr); -kerepType_declare(AutoarrUInt32Ptr); -kerepType_declare(AutoarrInt64Ptr); -kerepType_declare(AutoarrUInt64Ptr); +kerepTypeId_declare(kerepTypeId_AutoarrCharPtr); +kerepTypeId_declare(kerepTypeId_AutoarrBoolPtr); +kerepTypeId_declare(kerepTypeId_AutoarrFloat32Ptr); +kerepTypeId_declare(kerepTypeId_AutoarrFloat64Ptr); +kerepTypeId_declare(kerepTypeId_AutoarrInt8Ptr); +kerepTypeId_declare(kerepTypeId_AutoarrUInt8Ptr); +kerepTypeId_declare(kerepTypeId_AutoarrInt16Ptr); +kerepTypeId_declare(kerepTypeId_AutoarrUInt16Ptr); +kerepTypeId_declare(kerepTypeId_AutoarrInt32Ptr); +kerepTypeId_declare(kerepTypeId_AutoarrUInt32Ptr); +kerepTypeId_declare(kerepTypeId_AutoarrInt64Ptr); +kerepTypeId_declare(kerepTypeId_AutoarrUInt64Ptr); Autoarr_declare(Unitype) -kerepType_declare(AutoarrUnitype); -kerepType_declare(AutoarrUnitypePtr); +kerepTypeId_declare(kerepTypeId_AutoarrUnitype); +kerepTypeId_declare(kerepTypeId_AutoarrUnitypePtr); // this function is injected in kerep_init() void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr); diff --git a/src/DtsodParser/DtsodV24_deserialize.c b/src/DtsodParser/DtsodV24_deserialize.c index 6a9862b..cd84b53 100644 --- a/src/DtsodParser/DtsodV24_deserialize.c +++ b/src/DtsodParser/DtsodV24_deserialize.c @@ -98,9 +98,9 @@ Maybe __ReadName(DeserializeSharedData* shared){ case '}': if(!calledRecursively || nameStr.length!=0) safethrow_wrongchar(c,;); - return SUCCESS(UniPtr(CharPtr,NULL)); + return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,NULL)); case ':': - return SUCCESS(UniPtr(CharPtr,string_extract(nameStr))); + return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,string_extract(nameStr))); case '$': if(nameStr.length!=0) safethrow_wrongchar(c,;); @@ -113,7 +113,7 @@ Maybe __ReadName(DeserializeSharedData* shared){ } if(nameStr.length>0) safethrow(ERR_ENDOFSTR,;); - return SUCCESS(UniPtr(CharPtr,NULL)); + return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,NULL)); } #define ReadName() __ReadName(shared) @@ -137,7 +137,7 @@ Maybe __ReadString(DeserializeSharedData* shared){ } else { char* str=StringBuilder_build(b).ptr; - return SUCCESS(UniPtr(CharPtr,str)); + return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,str)); } } else { @@ -157,13 +157,13 @@ Maybe __ReadList(DeserializeSharedData* shared){ try(ReadValue((&readingList)), val, Autoarr_free(list, true)) Autoarr_add(list,val.value); if (!readingList){ - if(val.value.type==Null) + if(val.value.typeId==kerepTypeId_Null) Autoarr_pop(list); break; } } - return SUCCESS(UniPtr(AutoarrUnitypePtr,list)); + return SUCCESS(UniPtrHeap(kerepTypeId_AutoarrUnitypePtr,list)); }; #define ReadList() __ReadList(shared) @@ -275,7 +275,7 @@ Maybe __ReadValue(DeserializeSharedData* shared, bool* readingList){ case ';': case ',': if(valueStr.length!=0){ - if(value.type!=Null) + if(value.typeId!=kerepTypeId_Null) safethrow_wrongchar(c,Unitype_free(value)); try(ParseValue(valueStr),maybeParsed,;) value=maybeParsed.value; @@ -321,7 +321,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) { } else{ list=Autoarr_create(Unitype,ARR_BC,ARR_BL); - Hashtable_add(dict,nameCPtr,UniPtr(AutoarrUnitypePtr,list)); + Hashtable_add(dict,nameCPtr,UniPtrHeap(kerepTypeId_AutoarrUnitypePtr,list)); } Autoarr_add(list,val.value); } @@ -331,7 +331,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) { END: *_text=text; - return SUCCESS(UniPtr(HashtablePtr,dict)); + return SUCCESS(UniPtrHeap(kerepTypeId_HashtablePtr,dict)); } Maybe DtsodV24_deserialize(char* _text) { diff --git a/src/DtsodParser/DtsodV24_serialize.c b/src/DtsodParser/DtsodV24_serialize.c index 4618104..1988acf 100644 --- a/src/DtsodParser/DtsodV24_serialize.c +++ b/src/DtsodParser/DtsodV24_serialize.c @@ -23,81 +23,78 @@ void __AppendTabs(SerializeSharedData* shared) { Maybe __AppendValue(SerializeSharedData* shared, Unitype u); #define AppendValue(UNI) __AppendValue(shared, UNI) Maybe __AppendValue(SerializeSharedData* shared, Unitype u){ - switch(u.type){ - case Int64: - StringBuilder_append_int64(b,u.Int64); - break; - case UInt64: - StringBuilder_append_uint64(b,u.UInt64); - addc('u'); - break; - case Float64: - StringBuilder_append_float64(b,u.Float64); - addc('f'); - break; - case CharPtr: - addc('"'); - char c; - while((c=*(char*)(u.VoidPtr++))){ - if(c=='\"') addc('\\'); - addc(c); - } - addc('"'); - break; - case Bool: - StringBuilder_append_cptr(b, u.Bool ? "true" : "false"); - break; - case Null: - safethrow("Null isn't supported in DtsodV24",;); - case AutoarrUnitypePtr: - if(Autoarr_length(((Autoarr_Unitype*)(u.VoidPtr)))){ - addc('\n'); - AppendTabs(); - addc('['); - tabs++; - Autoarr_foreach(((Autoarr_Unitype*)(u.VoidPtr)), e, ({ - addc('\n'); - AppendTabs(); - try(AppendValue(e),__,;); - addc(','); - })); - StringBuilder_rmchar(b); - addc('\n'); - tabs--; - AppendTabs(); - addc(']'); - } - else { - addc('['); - addc(']'); - } - break; - case HashtablePtr: - // check hashtable is blank - Hashtable_foreach(((Hashtable*)u.VoidPtr), __, ({ - goto hashtableNotBlank; - if(__.key); // weird way to disable warning - })); - - - // blank hashtable - addc('{'); - addc('}'); - break; - - // not blank hashtable - hashtableNotBlank: - addc('\n'); - AppendTabs(); - addc('{'); - addc('\n'); - try(__serialize(b,tabs+1,u.VoidPtr),___,;); - AppendTabs(); - addc('}'); - break; - default: dbg((u.type)); safethrow(ERR_WRONGTYPE,;); + if(u.typeId==kerepTypeId_Int64){ + StringBuilder_append_int64(b,u.Int64); + } + else if(u.typeId==kerepTypeId_UInt64){ + StringBuilder_append_uint64(b,u.UInt64); + addc('u'); + } + else if(u.typeId==kerepTypeId_Float64){ + StringBuilder_append_float64(b,u.Float64); + addc('f'); + } + else if(u.typeId==kerepTypeId_CharPtr){ + addc('"'); + char c; + while((c=*(char*)(u.VoidPtr++))){ + if(c=='\"') addc('\\'); + addc(c); + } + addc('"'); + } + else if(u.typeId==kerepTypeId_Bool){ + StringBuilder_append_cptr(b, u.Bool ? "true" : "false"); + } + else if(u.typeId==kerepTypeId_Null){ + safethrow("Null isn't supported in DtsodV24",;); + } + else if(u.typeId==kerepTypeId_AutoarrUnitypePtr){ + if(Autoarr_length(((Autoarr_Unitype*)(u.VoidPtr)))){ + addc('\n'); + AppendTabs(); + addc('['); + tabs++; + Autoarr_foreach(((Autoarr_Unitype*)(u.VoidPtr)), e, ({ + addc('\n'); + AppendTabs(); + try(AppendValue(e),__,;); + addc(','); + })); + StringBuilder_rmchar(b); + addc('\n'); + tabs--; + AppendTabs(); + addc(']'); + } + else { + addc('['); + addc(']'); + } + } + else if(u.typeId==kerepTypeId_HashtablePtr){ + // check hashtable is blank + Hashtable_foreach(((Hashtable*)u.VoidPtr), __, ({ + goto hashtableNotBlank; + if(__.key); // weird way to disable warning + })); + // blank hashtable + addc('{'); + addc('}'); + // not blank hashtable + hashtableNotBlank: + addc('\n'); + AppendTabs(); + addc('{'); + addc('\n'); + try(__serialize(b,tabs+1,u.VoidPtr),___,;); + AppendTabs(); + addc('}'); + } + else { + dbg((u.typeId)); + safethrow(ERR_WRONGTYPE,;); } - return MaybeNull; }; @@ -125,5 +122,5 @@ Maybe DtsodV24_serialize(Hashtable* dtsod){ StringBuilder* sb=StringBuilder_create(); try(__serialize(sb,0,dtsod),__, StringBuilder_free(sb)); char* str=StringBuilder_build(sb).ptr; - return SUCCESS(UniPtr(CharPtr, str)); + return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr, str)); } diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index 9a914c4..b659ef5 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -92,7 +92,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.type!=Null; + return u.typeId!=kerepTypeId_Null; } void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){ diff --git a/src/Hashtable/Hashtable.h b/src/Hashtable/Hashtable.h index 5bf4a81..58f446d 100644 --- a/src/Hashtable/Hashtable.h +++ b/src/Hashtable/Hashtable.h @@ -11,8 +11,8 @@ typedef struct Hashtable{ uint8 hein; // height=HT_HEIGHTS[hein] Autoarr(KVPair)** rows; // Autoarr[height] } Hashtable; -kerepType_declare(Hashtable); -kerepType_declare(HashtablePtr); +kerepTypeId_declare(kerepTypeId_Hashtable); +kerepTypeId_declare(kerepTypeId_HashtablePtr); Hashtable* Hashtable_create(); void Hashtable_free(Hashtable* ht); diff --git a/src/Hashtable/KeyValuePair.h b/src/Hashtable/KeyValuePair.h index 48e606f..832c6c7 100644 --- a/src/Hashtable/KeyValuePair.h +++ b/src/Hashtable/KeyValuePair.h @@ -11,12 +11,12 @@ typedef struct KVPair{ char* key; Unitype value; } KVPair; -kerepType_declare(KVPair); -kerepType_declare(KVPairPtr); +kerepTypeId_declare(kerepTypeId_KVPair); +kerepTypeId_declare(kerepTypeId_KVPairPtr); Autoarr_declare(KVPair) -kerepType_declare(AutoarrKVPair); -kerepType_declare(AutoarrKVPairPtr); +kerepTypeId_declare(kerepTypeId_AutoarrKVPair); +kerepTypeId_declare(kerepTypeId_AutoarrKVPairPtr); // proper way to clear a KVP void KVPair_free(KVPair p); diff --git a/src/SearchTree/SearchTree.c b/src/SearchTree/SearchTree.c index 0c95125..14a4b4f 100644 --- a/src/SearchTree/SearchTree.c +++ b/src/SearchTree/SearchTree.c @@ -3,7 +3,7 @@ STNode* STNode_create(){ STNode* node=malloc(sizeof(STNode)); node->branches=NULL; - node->value.type=Null; + node->value.typeId=kerepTypeId_Null; node->value.UInt64=0; return node; } diff --git a/src/SearchTree/SearchTree.h b/src/SearchTree/SearchTree.h index c0f1c3f..3d1a8e0 100644 --- a/src/SearchTree/SearchTree.h +++ b/src/SearchTree/SearchTree.h @@ -11,8 +11,8 @@ typedef struct SearchTreeNode{ struct SearchTreeNode**** branches; // *STNode[8][8][4] Unitype value; } STNode; -kerepType_declare(STNode); -kerepType_declare(STNodePtr); +kerepTypeId_declare(kerepTypeId_STNode); +kerepTypeId_declare(kerepTypeId_STNodePtr); STNode* STNode_create(); void STNode_free(STNode* node); diff --git a/src/String/StringBuilder.h b/src/String/StringBuilder.h index 0fe1696..bc30f20 100644 --- a/src/String/StringBuilder.h +++ b/src/String/StringBuilder.h @@ -13,8 +13,8 @@ typedef struct StringBuilder{ Autoarr(string)* compl_bufs; Autoarr(int8)* curr_buf; } StringBuilder; -kerepType_declare(StringBuilder); -kerepType_declare(StringBuilderPtr); +kerepTypeId_declare(kerepTypeId_StringBuilder); +kerepTypeId_declare(kerepTypeId_StringBuilderPtr); StringBuilder* StringBuilder_create(void); void StringBuilder_free(StringBuilder* b); diff --git a/src/base/init.c b/src/base/init.c index 73073ab..cb4c43f 100644 --- a/src/base/init.c +++ b/src/base/init.c @@ -6,90 +6,90 @@ void kerepTypeDescriptors_initKerepTypes(){ // null - kerepType_register(NULL, Null, NULL); + kerepType_register(NULL, kerepTypeId_Null, NULL); // base types - kerepType_register(char, Char, NULL); - kerepType_register(bool, Bool, NULL); - kerepType_register(float32, Float32, NULL); - kerepType_register(float64, Float64, NULL); - kerepType_register(int8, Int8, NULL); - kerepType_register(uint8, UInt8, NULL); - kerepType_register(int16, Int16, NULL); - kerepType_register(uint16, UInt16, NULL); - kerepType_register(int32, Int32, NULL); - kerepType_register(uint32, UInt32, NULL); - kerepType_register(int64, Int64, NULL); - kerepType_register(uint64, UInt64, NULL); + kerepType_register(char, kerepTypeId_Char, NULL); + kerepType_register(bool, kerepTypeId_Bool, NULL); + kerepType_register(float32, kerepTypeId_Float32, NULL); + kerepType_register(float64, kerepTypeId_Float64, NULL); + kerepType_register(int8, kerepTypeId_Int8, NULL); + kerepType_register(uint8, kerepTypeId_UInt8, NULL); + kerepType_register(int16, kerepTypeId_Int16, NULL); + kerepType_register(uint16, kerepTypeId_UInt16, NULL); + kerepType_register(int32, kerepTypeId_Int32, NULL); + kerepType_register(uint32, kerepTypeId_UInt32, NULL); + kerepType_register(int64, kerepTypeId_Int64, NULL); + kerepType_register(uint64, kerepTypeId_UInt64, NULL); // base type pointers - kerepType_register(char*, CharPtr, NULL); - kerepType_register(bool*, BoolPtr, NULL); - kerepType_register(float32*, Float32Ptr, NULL); - kerepType_register(float64*, Float64Ptr, NULL); - kerepType_register(int8*, Int8Ptr, NULL); - kerepType_register(uint8*, UInt8Ptr, NULL); - kerepType_register(int16*, Int16Ptr, NULL); - kerepType_register(uint16*, UInt16Ptr, NULL); - kerepType_register(int32*, Int32Ptr, NULL); - kerepType_register(uint32*, UInt32Ptr, NULL); - kerepType_register(int64*, Int64Ptr, NULL); - kerepType_register(uint64*, UInt64Ptr, NULL); + kerepType_register(char*, kerepTypeId_CharPtr, NULL); + kerepType_register(bool*, kerepTypeId_BoolPtr, NULL); + kerepType_register(float32*, kerepTypeId_Float32Ptr, NULL); + kerepType_register(float64*, kerepTypeId_Float64Ptr, NULL); + kerepType_register(int8*, kerepTypeId_Int8Ptr, NULL); + kerepType_register(uint8*, kerepTypeId_UInt8Ptr, NULL); + kerepType_register(int16*, kerepTypeId_Int16Ptr, NULL); + kerepType_register(uint16*, kerepTypeId_UInt16Ptr, NULL); + kerepType_register(int32*, kerepTypeId_Int32Ptr, NULL); + kerepType_register(uint32*, kerepTypeId_UInt32Ptr, NULL); + kerepType_register(int64*, kerepTypeId_Int64Ptr, NULL); + kerepType_register(uint64*, kerepTypeId_UInt64Ptr, NULL); // base type autoarrs - kerepType_register(Autoarr_char, AutoarrChar, ____Autoarr_free_char); - kerepType_register(Autoarr_bool, AutoarrBool, ____Autoarr_free_bool); - kerepType_register(Autoarr_float32, AutoarrFloat32, ____Autoarr_free_float32); - kerepType_register(Autoarr_float64, AutoarrFloat64, ____Autoarr_free_float64); - kerepType_register(Autoarr_int8, AutoarrInt8, ____Autoarr_free_int8); - kerepType_register(Autoarr_uint8, AutoarrUInt8, ____Autoarr_free_uint8); - kerepType_register(Autoarr_int16, AutoarrInt16, ____Autoarr_free_int16); - kerepType_register(Autoarr_uint16, AutoarrUInt16, ____Autoarr_free_uint16); - kerepType_register(Autoarr_int32, AutoarrInt32, ____Autoarr_free_int32); - kerepType_register(Autoarr_uint32, AutoarrUInt32, ____Autoarr_free_uint32); - kerepType_register(Autoarr_int64, AutoarrInt64, ____Autoarr_free_int64); - kerepType_register(Autoarr_uint64, AutoarrUInt64, ____Autoarr_free_uint64); + kerepType_register(Autoarr_char, kerepTypeId_AutoarrChar, ____Autoarr_free_char); + kerepType_register(Autoarr_bool, kerepTypeId_AutoarrBool, ____Autoarr_free_bool); + kerepType_register(Autoarr_float32, kerepTypeId_AutoarrFloat32, ____Autoarr_free_float32); + kerepType_register(Autoarr_float64, kerepTypeId_AutoarrFloat64, ____Autoarr_free_float64); + kerepType_register(Autoarr_int8, kerepTypeId_AutoarrInt8, ____Autoarr_free_int8); + kerepType_register(Autoarr_uint8, kerepTypeId_AutoarrUInt8, ____Autoarr_free_uint8); + kerepType_register(Autoarr_int16, kerepTypeId_AutoarrInt16, ____Autoarr_free_int16); + kerepType_register(Autoarr_uint16, kerepTypeId_AutoarrUInt16, ____Autoarr_free_uint16); + kerepType_register(Autoarr_int32, kerepTypeId_AutoarrInt32, ____Autoarr_free_int32); + kerepType_register(Autoarr_uint32, kerepTypeId_AutoarrUInt32, ____Autoarr_free_uint32); + kerepType_register(Autoarr_int64, kerepTypeId_AutoarrInt64, ____Autoarr_free_int64); + kerepType_register(Autoarr_uint64, kerepTypeId_AutoarrUInt64, ____Autoarr_free_uint64); // base type autoarr pointers - kerepType_register(Autoarr_char*, AutoarrCharPtr, ____Autoarr_free_char); - kerepType_register(Autoarr_bool*, AutoarrBoolPtr, ____Autoarr_free_bool); - kerepType_register(Autoarr_float32*, AutoarrFloat32Ptr, ____Autoarr_free_float32); - kerepType_register(Autoarr_float64*, AutoarrFloat64Ptr, ____Autoarr_free_float64); - kerepType_register(Autoarr_int8*, AutoarrInt8Ptr, ____Autoarr_free_int8); - kerepType_register(Autoarr_uint8*, AutoarrUInt8Ptr, ____Autoarr_free_uint8); - kerepType_register(Autoarr_int16*, AutoarrInt16Ptr, ____Autoarr_free_int16); - kerepType_register(Autoarr_uint16*, AutoarrUInt16Ptr, ____Autoarr_free_uint16); - kerepType_register(Autoarr_int32*, AutoarrInt32Ptr, ____Autoarr_free_int32); - kerepType_register(Autoarr_uint32*, AutoarrUInt32Ptr, ____Autoarr_free_uint32); - kerepType_register(Autoarr_int64*, AutoarrInt64Ptr, ____Autoarr_free_int64); - kerepType_register(Autoarr_uint64*, AutoarrUInt64Ptr, ____Autoarr_free_uint64); + kerepType_register(Autoarr_char*, kerepTypeId_AutoarrCharPtr, ____Autoarr_free_char); + kerepType_register(Autoarr_bool*, kerepTypeId_AutoarrBoolPtr, ____Autoarr_free_bool); + kerepType_register(Autoarr_float32*, kerepTypeId_AutoarrFloat32Ptr, ____Autoarr_free_float32); + kerepType_register(Autoarr_float64*, kerepTypeId_AutoarrFloat64Ptr, ____Autoarr_free_float64); + kerepType_register(Autoarr_int8*, kerepTypeId_AutoarrInt8Ptr, ____Autoarr_free_int8); + kerepType_register(Autoarr_uint8*, kerepTypeId_AutoarrUInt8Ptr, ____Autoarr_free_uint8); + kerepType_register(Autoarr_int16*, kerepTypeId_AutoarrInt16Ptr, ____Autoarr_free_int16); + kerepType_register(Autoarr_uint16*, kerepTypeId_AutoarrUInt16Ptr, ____Autoarr_free_uint16); + kerepType_register(Autoarr_int32*, kerepTypeId_AutoarrInt32Ptr, ____Autoarr_free_int32); + kerepType_register(Autoarr_uint32*, kerepTypeId_AutoarrUInt32Ptr, ____Autoarr_free_uint32); + kerepType_register(Autoarr_int64*, kerepTypeId_AutoarrInt64Ptr, ____Autoarr_free_int64); + kerepType_register(Autoarr_uint64*, kerepTypeId_AutoarrUInt64Ptr, ____Autoarr_free_uint64); // Unitype - kerepType_register(Unitype, Unitype, __UnitypePtr_free); - kerepType_register(Unitype*, UnitypePtr, __UnitypePtr_free); - kerepType_register(Autoarr_Unitype, AutoarrUnitype, ____Autoarr_free_Unitype_); - kerepType_register(Autoarr_Unitype*, AutoarrUnitypePtr, ____Autoarr_free_Unitype_); + kerepType_register(Unitype, kerepTypeId_Unitype, __UnitypePtr_free); + kerepType_register(Unitype*, kerepTypeId_UnitypePtr, __UnitypePtr_free); + kerepType_register(Autoarr_Unitype, kerepTypeId_AutoarrUnitype, ____Autoarr_free_Unitype_); + kerepType_register(Autoarr_Unitype*, kerepTypeId_AutoarrUnitypePtr, ____Autoarr_free_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 - kerepType_register(STNode, STNode, __STNode_free); - kerepType_register(STNode*, STNodePtr, __STNode_free); + kerepType_register(STNode, kerepTypeId_STNode, __STNode_free); + kerepType_register(STNode*, kerepTypeId_STNodePtr, __STNode_free); // KeyValuePair - kerepType_register(KVPair, KVPair, __KVPair_free); - kerepType_register(KVPair*, KVPairPtr, __KVPair_free); - kerepType_register(Autoarr_KVPair, AutoarrKVPair, ____Autoarr_free_KVPair_); - kerepType_register(Autoarr_KVPair*, AutoarrKVPairPtr, ____Autoarr_free_KVPair_); + kerepType_register(KVPair, kerepTypeId_KVPair, __KVPair_free); + kerepType_register(KVPair*, kerepTypeId_KVPairPtr, __KVPair_free); + kerepType_register(Autoarr_KVPair, kerepTypeId_AutoarrKVPair, ____Autoarr_free_KVPair_); + kerepType_register(Autoarr_KVPair*, kerepTypeId_AutoarrKVPairPtr, ____Autoarr_free_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 - kerepType_register(Hashtable, Hashtable, __Hashtable_free); - kerepType_register(Hashtable*, HashtablePtr, __Hashtable_free); + kerepType_register(Hashtable, kerepTypeId_Hashtable, __Hashtable_free); + kerepType_register(Hashtable*, kerepTypeId_HashtablePtr, __Hashtable_free); // StringBuilder - kerepType_register(StringBuilder, StringBuilder, __StringBuilder_free); - kerepType_register(StringBuilder*, StringBuilderPtr, __StringBuilder_free); + kerepType_register(StringBuilder, kerepTypeId_StringBuilder, __StringBuilder_free); + kerepType_register(StringBuilder*, kerepTypeId_StringBuilderPtr, __StringBuilder_free); } diff --git a/src/base/types.h b/src/base/types.h index 2e58287..8d5dd23 100644 --- a/src/base/types.h +++ b/src/base/types.h @@ -15,47 +15,49 @@ typedef struct kerepTypeDescriptor{ uint16 size; } kerepTypeDescriptor; -#define kerepType_declare(NAME)\ - extern kerepTypeId kerepTypeId_##NAME +#define kerepTypeId_declare(ID_VAR_NAME)\ + extern kerepTypeId ID_VAR_NAME +#define kerepTypeId_define(ID_VAR_NAME)\ + kerepTypeId ID_VAR_NAME=-1 extern kerepTypeId kerepTypeId_last; void __kerepType_register(char* name, int16 size, void (*free_members)(void*)); -#define kerepType_register(TYPE, NAME, FREE_MEMBERS_FUNC)\ - __kerepType_register(#NAME, sizeof(TYPE), FREE_MEMBERS_FUNC);\ - kerepTypeId_##NAME=kerepTypeId_last; +#define kerepType_register(TYPE, ID_VAR_NAME, FREE_MEMBERS_FUNC)\ + __kerepType_register(#ID_VAR_NAME, sizeof(TYPE), FREE_MEMBERS_FUNC);\ + ID_VAR_NAME=kerepTypeId_last; void kerepTypeDescriptors_beginInit(); void kerepTypeDescriptors_endInit(); kerepTypeDescriptor kerepTypeDescriptor_get(kerepTypeId id); -kerepType_declare(Null); +kerepTypeId_declare(kerepTypeId_Null); -kerepType_declare(Char); -kerepType_declare(Bool); -kerepType_declare(Float32); -kerepType_declare(Float64); -kerepType_declare(Int8); -kerepType_declare(UInt8); -kerepType_declare(Int16); -kerepType_declare(UInt16); -kerepType_declare(Int32); -kerepType_declare(UInt32); -kerepType_declare(Int64); -kerepType_declare(UInt64); +kerepTypeId_declare(kerepTypeId_Char); +kerepTypeId_declare(kerepTypeId_Bool); +kerepTypeId_declare(kerepTypeId_Float32); +kerepTypeId_declare(kerepTypeId_Float64); +kerepTypeId_declare(kerepTypeId_Int8); +kerepTypeId_declare(kerepTypeId_UInt8); +kerepTypeId_declare(kerepTypeId_Int16); +kerepTypeId_declare(kerepTypeId_UInt16); +kerepTypeId_declare(kerepTypeId_Int32); +kerepTypeId_declare(kerepTypeId_UInt32); +kerepTypeId_declare(kerepTypeId_Int64); +kerepTypeId_declare(kerepTypeId_UInt64); -kerepType_declare(CharPtr); -kerepType_declare(BoolPtr); -kerepType_declare(Float32Ptr); -kerepType_declare(Float64Ptr); -kerepType_declare(Int8Ptr); -kerepType_declare(UInt8Ptr); -kerepType_declare(Int16Ptr); -kerepType_declare(UInt16Ptr); -kerepType_declare(Int32Ptr); -kerepType_declare(UInt32Ptr); -kerepType_declare(Int64Ptr); -kerepType_declare(UInt64Ptr); +kerepTypeId_declare(kerepTypeId_CharPtr); +kerepTypeId_declare(kerepTypeId_BoolPtr); +kerepTypeId_declare(kerepTypeId_Float32Ptr); +kerepTypeId_declare(kerepTypeId_Float64Ptr); +kerepTypeId_declare(kerepTypeId_Int8Ptr); +kerepTypeId_declare(kerepTypeId_UInt8Ptr); +kerepTypeId_declare(kerepTypeId_Int16Ptr); +kerepTypeId_declare(kerepTypeId_UInt16Ptr); +kerepTypeId_declare(kerepTypeId_Int32Ptr); +kerepTypeId_declare(kerepTypeId_UInt32Ptr); +kerepTypeId_declare(kerepTypeId_Int64Ptr); +kerepTypeId_declare(kerepTypeId_UInt64Ptr); #if __cplusplus } diff --git a/src/base/unitype.h b/src/base/unitype.h index 0d28ed8..146c5d1 100644 --- a/src/base/unitype.h +++ b/src/base/unitype.h @@ -18,24 +18,24 @@ typedef struct Unitype{ kerepTypeId typeId; bool allocatedInHeap; // should Unitype_free call free() to VoidPtr* } Unitype; -kerepType_declare(Unitype); -kerepType_declare(UnitypePtr); +kerepTypeId_declare(kerepTypeId_Unitype); +kerepTypeId_declare(kerepTypeId_UnitypePtr); -#define __Uni(TYPE,VAL) (Unitype){\ - .TYPE_NAME=VAL, .typeId=kerepTypeId_##TYPE, .allocatedInHeap=false} +#define Uni(TYPE, VAL) (Unitype){\ + .TYPE=VAL, .typeId=kerepTypeId_##TYPE, .allocatedInHeap=false} -#define UniInt64(VAL) __Uni(Int64, VAL) -#define UniUInt64(VAL) __Uni(UInt64, VAL) -#define UniFloat64(VAL) __Uni(Float64, VAL) -#define UniBool(VAL) __Uni(Bool, VAL) +#define UniInt64(VAL) Uni(Int64, VAL) +#define UniUInt64(VAL) Uni(UInt64, VAL) +#define UniFloat64(VAL) Uni(Float64, VAL) +#define UniBool(VAL) Uni(Bool, VAL) -#define UniPtrStack(TYPE_NAME,VAL) (Unitype){\ - .VoidPtr=VAL, .typeId=kerepTypeId_##TYPE_NAME, .allocatedInHeap=false} -#define UniPtrHeap (TYPE_NAME,VAL) (Unitype){\ - .VoidPtr=VAL, .typeId=kerepTypeId_##TYPE_NAME, .allocatedInHeap=true} +#define UniPtrStack(ID_VAR_NAME, VAL) (Unitype){\ + .VoidPtr=VAL, .typeId=ID_VAR_NAME, .allocatedInHeap=false} +#define UniPtrHeap(ID_VAR_NAME, VAL) (Unitype){\ + .VoidPtr=VAL, .typeId=ID_VAR_NAME, .allocatedInHeap=true} -#define UniNull UniPtrStack(Null, NULL) +#define UniNull UniPtrStack(kerepTypeId_Null, NULL) #define UniTrue UniBool(true) #define UniFalse UniBool(false) diff --git a/tests/main.cpp b/tests/main.cpp index 10f54f3..13abfcf 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -13,6 +13,9 @@ void test_all(){ int main(){ setlocale(LC_ALL, "en-US.Unicode"); + kerepTypeDescriptors_beginInit(); + kerepTypeDescriptors_initKerepTypes(); + kerepTypeDescriptors_endInit(); printf("\e[92mkerep tests are starting!\e[97m\n"); optime("test_all",1,test_all()); printf("\e[0m\n"); diff --git a/tests/test_autoarr.c b/tests/test_autoarr.c index 11b818d..34dc121 100644 --- a/tests/test_autoarr.c +++ b/tests/test_autoarr.c @@ -47,7 +47,7 @@ void test_autoarr(){ resetar(ar); printf("\e[92mautoarr values reset\n"); printallval(ar); - Autoarr_free(ar); + Autoarr_free(ar, true); printf("\e[92mautoarr deleted\n"); })); } diff --git a/tests/test_dtsod.c b/tests/test_dtsod.c index cf238d2..f14c582 100644 --- a/tests/test_dtsod.c +++ b/tests/test_dtsod.c @@ -18,7 +18,7 @@ void print_dtsod(Hashtable* dtsod){ printf("\e[92m"); Hashtable_foreach(dtsod, p,({ printkvp(p); - if(p.value.type==HashtablePtr){ + if(p.value.typeId==kerepTypeId_HashtablePtr){ printf(": {\n"); Hashtable* sub=p.value.VoidPtr; Hashtable_foreach(sub, _p,({ diff --git a/tests/test_hash_functions.c b/tests/test_hash_functions.c index 10c6aee..b046bfd 100644 --- a/tests/test_hash_functions.c +++ b/tests/test_hash_functions.c @@ -33,7 +33,7 @@ char data[]="iojihiojopijiugbjmoihftytryfdrh"; }\ printf("\e[93m%u \e[94mcollisions detected in %u hashes\n", collisions, COLLISION_TESTS);\ }));\ - Autoarr_free(hashes);\ + Autoarr_free(hashes, true);\ printf("\e[96m--------------------------------------\n");\ }) diff --git a/tests/test_marshalling.c b/tests/test_marshalling.c index 3dde3c0..21c5721 100644 --- a/tests/test_marshalling.c +++ b/tests/test_marshalling.c @@ -4,7 +4,7 @@ EXPORT void CALL test_marshalling(char* text, KVPair** kptr){ KVPair* k=malloc(sizeof(KVPair)); k->key="message"; char* tc=cptr_copy(text); - Unitype u={.VoidPtr=tc, .type=CharPtr}; + Unitype u=UniPtrHeap(kerepTypeId_CharPtr,tc); k->value=u; *kptr=k; } diff --git a/tests/test_searchtree.c b/tests/test_searchtree.c index cff22a5..b38ce7c 100644 --- a/tests/test_searchtree.c +++ b/tests/test_searchtree.c @@ -27,27 +27,27 @@ void test_searchtree(){ STNode* node=STNode_create(); printf("\e[92mnode created\n"); printf("push:\e[94m\n "); - Unitype u={.type=Int64,.Int64=-3}; + Unitype u=UniInt64(-3); printuni(u); ST_push(node,"type", u); printf(" -> type\n "); - u=(Unitype){.type=Int64,.Int64=25}; + u=UniInt64(25); printuni(u); ST_push(node,"time", u); printf(" -> time\n "); - u=(Unitype){.type=Float64,.Float64=-542.00600}; + u=UniFloat64(-542.00600); printuni(u); ST_push(node,"author_id", u); printf(" -> author_id\n "); - u=(Unitype){.type=Int64,.Int64=-31255}; + u=UniInt64(-31255); printuni(u); ST_push(node,"channel_id", u); printf(" -> channel_id\n "); - u=(Unitype){.type=Float64,.Float64=32.2004}; + u=UniPtrHeap(kerepTypeId_CharPtr, cptr_copy("32.2004")); printuni(u); ST_push(node,"message_id", u); printf(" -> message_id\n "); - u=(Unitype){.type=CharPtr,.VoidPtr=cptr_copy("some text UwU")}; + u=UniPtrStack(kerepTypeId_CharPtr,"some text UwU"); printuni(u); ST_push(node,"text", u); printf(" -> text\n"); From 3323c40fa3464d3cc8672ecd040b4ec425568d5f Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 26 Aug 2022 02:05:09 +0600 Subject: [PATCH 10/59] compiles but doesnt work --- src/Autoarr/Autoarr.c | 28 ++++++++++++++++++++++++++++ src/Hashtable/Hashtable.c | 3 +++ src/Hashtable/KeyValuePair.c | 6 +++++- src/SearchTree/SearchTree.c | 3 +++ src/String/StringBuilder.c | 5 +++++ src/String/StringBuilder.h | 2 ++ src/base/init.c | 2 ++ src/base/types.c | 30 +++++++++++++++++++++++++++++- src/base/unitype.c | 3 +++ 9 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/Autoarr/Autoarr.c b/src/Autoarr/Autoarr.c index 8dde7ab..5202b6d 100644 --- a/src/Autoarr/Autoarr.c +++ b/src/Autoarr/Autoarr.c @@ -13,7 +13,35 @@ Autoarr_define(int32) Autoarr_define(uint64) Autoarr_define(int64) +kerepTypeId_define(kerepTypeId_AutoarrChar); +kerepTypeId_define(kerepTypeId_AutoarrBool); +kerepTypeId_define(kerepTypeId_AutoarrFloat32); +kerepTypeId_define(kerepTypeId_AutoarrFloat64); +kerepTypeId_define(kerepTypeId_AutoarrInt8); +kerepTypeId_define(kerepTypeId_AutoarrUInt8); +kerepTypeId_define(kerepTypeId_AutoarrInt16); +kerepTypeId_define(kerepTypeId_AutoarrUInt16); +kerepTypeId_define(kerepTypeId_AutoarrInt32); +kerepTypeId_define(kerepTypeId_AutoarrUInt32); +kerepTypeId_define(kerepTypeId_AutoarrInt64); +kerepTypeId_define(kerepTypeId_AutoarrUInt64); + +kerepTypeId_define(kerepTypeId_AutoarrCharPtr); +kerepTypeId_define(kerepTypeId_AutoarrBoolPtr); +kerepTypeId_define(kerepTypeId_AutoarrFloat32Ptr); +kerepTypeId_define(kerepTypeId_AutoarrFloat64Ptr); +kerepTypeId_define(kerepTypeId_AutoarrInt8Ptr); +kerepTypeId_define(kerepTypeId_AutoarrUInt8Ptr); +kerepTypeId_define(kerepTypeId_AutoarrInt16Ptr); +kerepTypeId_define(kerepTypeId_AutoarrUInt16Ptr); +kerepTypeId_define(kerepTypeId_AutoarrInt32Ptr); +kerepTypeId_define(kerepTypeId_AutoarrUInt32Ptr); +kerepTypeId_define(kerepTypeId_AutoarrInt64Ptr); +kerepTypeId_define(kerepTypeId_AutoarrUInt64Ptr); + Autoarr_define(Unitype) +kerepTypeId_define(kerepTypeId_AutoarrUnitype); +kerepTypeId_define(kerepTypeId_AutoarrUnitypePtr); // right func to clear array of unitype values void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr){ diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index b659ef5..d68c6c7 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -1,5 +1,8 @@ #include "Hashtable.h" +kerepTypeId_define(kerepTypeId_Hashtable); +kerepTypeId_define(kerepTypeId_HashtablePtr); + // amount of rows static const uint16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521}; #define HT_HEIN_MIN 0 diff --git a/src/Hashtable/KeyValuePair.c b/src/Hashtable/KeyValuePair.c index 93ad54a..845ead5 100644 --- a/src/Hashtable/KeyValuePair.c +++ b/src/Hashtable/KeyValuePair.c @@ -1,7 +1,11 @@ #include "KeyValuePair.h" -Autoarr_define(KVPair) +kerepTypeId_define(kerepTypeId_KVPair); +kerepTypeId_define(kerepTypeId_KVPairPtr); +Autoarr_define(KVPair) +kerepTypeId_define(kerepTypeId_AutoarrKVPair); +kerepTypeId_define(kerepTypeId_AutoarrKVPairPtr); // proper way to clear a KVP void KVPair_free(KVPair p){ diff --git a/src/SearchTree/SearchTree.c b/src/SearchTree/SearchTree.c index 14a4b4f..18088ab 100644 --- a/src/SearchTree/SearchTree.c +++ b/src/SearchTree/SearchTree.c @@ -1,5 +1,8 @@ #include "SearchTree.h" +kerepTypeId_define(kerepTypeId_STNode); +kerepTypeId_define(kerepTypeId_STNodePtr); + STNode* STNode_create(){ STNode* node=malloc(sizeof(STNode)); node->branches=NULL; diff --git a/src/String/StringBuilder.c b/src/String/StringBuilder.c index 652c9b8..167e916 100644 --- a/src/String/StringBuilder.c +++ b/src/String/StringBuilder.c @@ -1,6 +1,11 @@ #include "StringBuilder.h" Autoarr_define(string) +kerepTypeId_define(kerepTypeId_AutoarrString); +kerepTypeId_define(kerepTypeId_AutoarrStringPtr); + +kerepTypeId_define(kerepTypeId_StringBuilder); +kerepTypeId_define(kerepTypeId_StringBuilderPtr); #define BL_C 32 #define BL_L 1024 diff --git a/src/String/StringBuilder.h b/src/String/StringBuilder.h index bc30f20..50e63a7 100644 --- a/src/String/StringBuilder.h +++ b/src/String/StringBuilder.h @@ -8,6 +8,8 @@ extern "C" { #include "string.h" Autoarr_declare(string) +kerepTypeId_declare(kerepTypeId_AutoarrString); +kerepTypeId_declare(kerepTypeId_AutoarrStringPtr); typedef struct StringBuilder{ Autoarr(string)* compl_bufs; diff --git a/src/base/init.c b/src/base/init.c index cb4c43f..f838c64 100644 --- a/src/base/init.c +++ b/src/base/init.c @@ -90,6 +90,8 @@ void kerepTypeDescriptors_initKerepTypes(){ kerepType_register(Hashtable*, kerepTypeId_HashtablePtr, __Hashtable_free); // StringBuilder + kerepType_register(Autoarr_string, kerepTypeId_AutoarrString, ____Autoarr_free_string); + kerepType_register(Autoarr_string*, kerepTypeId_AutoarrStringPtr, ____Autoarr_free_string); kerepType_register(StringBuilder, kerepTypeId_StringBuilder, __StringBuilder_free); kerepType_register(StringBuilder*, kerepTypeId_StringBuilderPtr, __StringBuilder_free); } diff --git a/src/base/types.c b/src/base/types.c index 1c7a055..6f08596 100644 --- a/src/base/types.c +++ b/src/base/types.c @@ -4,6 +4,34 @@ Autoarr_declare(kerepTypeDescriptor) Autoarr_define(kerepTypeDescriptor) +kerepTypeId_define(kerepTypeId_Null); + +kerepTypeId_define(kerepTypeId_Char); +kerepTypeId_define(kerepTypeId_Bool); +kerepTypeId_define(kerepTypeId_Float32); +kerepTypeId_define(kerepTypeId_Float64); +kerepTypeId_define(kerepTypeId_Int8); +kerepTypeId_define(kerepTypeId_UInt8); +kerepTypeId_define(kerepTypeId_Int16); +kerepTypeId_define(kerepTypeId_UInt16); +kerepTypeId_define(kerepTypeId_Int32); +kerepTypeId_define(kerepTypeId_UInt32); +kerepTypeId_define(kerepTypeId_Int64); +kerepTypeId_define(kerepTypeId_UInt64); + +kerepTypeId_define(kerepTypeId_CharPtr); +kerepTypeId_define(kerepTypeId_BoolPtr); +kerepTypeId_define(kerepTypeId_Float32Ptr); +kerepTypeId_define(kerepTypeId_Float64Ptr); +kerepTypeId_define(kerepTypeId_Int8Ptr); +kerepTypeId_define(kerepTypeId_UInt8Ptr); +kerepTypeId_define(kerepTypeId_Int16Ptr); +kerepTypeId_define(kerepTypeId_UInt16Ptr); +kerepTypeId_define(kerepTypeId_Int32Ptr); +kerepTypeId_define(kerepTypeId_UInt32Ptr); +kerepTypeId_define(kerepTypeId_Int64Ptr); +kerepTypeId_define(kerepTypeId_UInt64Ptr); + // type descriptors are stored here during initialization Autoarr(kerepTypeDescriptor)* __kerepTypeDescriptors=NULL; // here type descriptors are stored when initialization is complited @@ -31,7 +59,7 @@ void __kerepType_register(char* name, int16 size, void (*free_members)(void*)){ .size=size, .free_members=free_members, .id=++kerepTypeId_last - }; + }; dbg(kerepTypeId_last); Autoarr_add(__kerepTypeDescriptors, typeDesc); } diff --git a/src/base/unitype.c b/src/base/unitype.c index 1f18751..005cee5 100644 --- a/src/base/unitype.c +++ b/src/base/unitype.c @@ -1,5 +1,8 @@ #include "base.h" +kerepTypeId_define(kerepTypeId_Unitype); +kerepTypeId_define(kerepTypeId_UnitypePtr); + void Unitype_free(Unitype u){ kerepTypeDescriptor type=kerepTypeDescriptor_get(u.typeId); if(type.free_members) From 2ad5790a6769109b134e7e6b4c5e330b8c1259dd Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 26 Aug 2022 02:05:42 +0600 Subject: [PATCH 11/59] removed log files --- make | 1 - make.error.log | 65 --------------------------------------------- make.log | 71 -------------------------------------------------- 3 files changed, 137 deletions(-) delete mode 100644 make delete mode 100644 make.error.log delete mode 100644 make.log diff --git a/make b/make deleted file mode 100644 index 8b13789..0000000 --- a/make +++ /dev/null @@ -1 +0,0 @@ - diff --git a/make.error.log b/make.error.log deleted file mode 100644 index b73e8ea..0000000 --- a/make.error.log +++ /dev/null @@ -1,65 +0,0 @@ -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24.c.o:DtsodV24.c:(.rdata$.refptr.kerepTypeId_Null[.refptr.kerepTypeId_Null]+0x0): undefined reference to `kerepTypeId_Null' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24_deserialize.c.o:DtsodV24_deserialize.c:(.rdata$.refptr.kerepTypeId_AutoarrUnitypePtr[.refptr.kerepTypeId_AutoarrUnitypePtr]+0x0): undefined reference to `kerepTypeId_AutoarrUnitypePtr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24_deserialize.c.o:DtsodV24_deserialize.c:(.rdata$.refptr.kerepTypeId_HashtablePtr[.refptr.kerepTypeId_HashtablePtr]+0x0): undefined reference to `kerepTypeId_HashtablePtr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24_deserialize.c.o:DtsodV24_deserialize.c:(.rdata$.refptr.kerepTypeId_Int64[.refptr.kerepTypeId_Int64]+0x0): undefined reference to `kerepTypeId_Int64' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24_deserialize.c.o:DtsodV24_deserialize.c:(.rdata$.refptr.kerepTypeId_UInt64[.refptr.kerepTypeId_UInt64]+0x0): undefined reference to `kerepTypeId_UInt64' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24_deserialize.c.o:DtsodV24_deserialize.c:(.rdata$.refptr.kerepTypeId_Float64[.refptr.kerepTypeId_Float64]+0x0): undefined reference to `kerepTypeId_Float64' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24_deserialize.c.o:DtsodV24_deserialize.c:(.rdata$.refptr.kerepTypeId_Bool[.refptr.kerepTypeId_Bool]+0x0): undefined reference to `kerepTypeId_Bool' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/DtsodV24_deserialize.c.o:DtsodV24_deserialize.c:(.rdata$.refptr.kerepTypeId_CharPtr[.refptr.kerepTypeId_CharPtr]+0x0): undefined reference to `kerepTypeId_CharPtr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_StringBuilderPtr[.refptr.kerepTypeId_StringBuilderPtr]+0x0): undefined reference to `kerepTypeId_StringBuilderPtr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_StringBuilder[.refptr.kerepTypeId_StringBuilder]+0x0): undefined reference to `kerepTypeId_StringBuilder' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Hashtable[.refptr.kerepTypeId_Hashtable]+0x0): undefined reference to `kerepTypeId_Hashtable' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrKVPairPtr[.refptr.kerepTypeId_AutoarrKVPairPtr]+0x0): undefined reference to `kerepTypeId_AutoarrKVPairPtr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrKVPair[.refptr.kerepTypeId_AutoarrKVPair]+0x0): undefined reference to `kerepTypeId_AutoarrKVPair' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_KVPairPtr[.refptr.kerepTypeId_KVPairPtr]+0x0): undefined reference to `kerepTypeId_KVPairPtr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_KVPair[.refptr.kerepTypeId_KVPair]+0x0): undefined reference to `kerepTypeId_KVPair' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_STNodePtr[.refptr.kerepTypeId_STNodePtr]+0x0): undefined reference to `kerepTypeId_STNodePtr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_STNode[.refptr.kerepTypeId_STNode]+0x0): undefined reference to `kerepTypeId_STNode' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUnitype[.refptr.kerepTypeId_AutoarrUnitype]+0x0): undefined reference to `kerepTypeId_AutoarrUnitype' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UnitypePtr[.refptr.kerepTypeId_UnitypePtr]+0x0): undefined reference to `kerepTypeId_UnitypePtr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Unitype[.refptr.kerepTypeId_Unitype]+0x0): undefined reference to `kerepTypeId_Unitype' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt64Ptr[.refptr.kerepTypeId_AutoarrUInt64Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrUInt64Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt64Ptr[.refptr.kerepTypeId_AutoarrInt64Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrInt64Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt32Ptr[.refptr.kerepTypeId_AutoarrUInt32Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrUInt32Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt32Ptr[.refptr.kerepTypeId_AutoarrInt32Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrInt32Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt16Ptr[.refptr.kerepTypeId_AutoarrUInt16Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrUInt16Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt16Ptr[.refptr.kerepTypeId_AutoarrInt16Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrInt16Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt8Ptr[.refptr.kerepTypeId_AutoarrUInt8Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrUInt8Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt8Ptr[.refptr.kerepTypeId_AutoarrInt8Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrInt8Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrFloat64Ptr[.refptr.kerepTypeId_AutoarrFloat64Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrFloat64Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrFloat32Ptr[.refptr.kerepTypeId_AutoarrFloat32Ptr]+0x0): undefined reference to `kerepTypeId_AutoarrFloat32Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrBoolPtr[.refptr.kerepTypeId_AutoarrBoolPtr]+0x0): undefined reference to `kerepTypeId_AutoarrBoolPtr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrCharPtr[.refptr.kerepTypeId_AutoarrCharPtr]+0x0): undefined reference to `kerepTypeId_AutoarrCharPtr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt64[.refptr.kerepTypeId_AutoarrUInt64]+0x0): undefined reference to `kerepTypeId_AutoarrUInt64' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt64[.refptr.kerepTypeId_AutoarrInt64]+0x0): undefined reference to `kerepTypeId_AutoarrInt64' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt32[.refptr.kerepTypeId_AutoarrUInt32]+0x0): undefined reference to `kerepTypeId_AutoarrUInt32' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt32[.refptr.kerepTypeId_AutoarrInt32]+0x0): undefined reference to `kerepTypeId_AutoarrInt32' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt16[.refptr.kerepTypeId_AutoarrUInt16]+0x0): undefined reference to `kerepTypeId_AutoarrUInt16' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt16[.refptr.kerepTypeId_AutoarrInt16]+0x0): undefined reference to `kerepTypeId_AutoarrInt16' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrUInt8[.refptr.kerepTypeId_AutoarrUInt8]+0x0): undefined reference to `kerepTypeId_AutoarrUInt8' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrInt8[.refptr.kerepTypeId_AutoarrInt8]+0x0): undefined reference to `kerepTypeId_AutoarrInt8' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrFloat64[.refptr.kerepTypeId_AutoarrFloat64]+0x0): undefined reference to `kerepTypeId_AutoarrFloat64' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrFloat32[.refptr.kerepTypeId_AutoarrFloat32]+0x0): undefined reference to `kerepTypeId_AutoarrFloat32' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrBool[.refptr.kerepTypeId_AutoarrBool]+0x0): undefined reference to `kerepTypeId_AutoarrBool' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_AutoarrChar[.refptr.kerepTypeId_AutoarrChar]+0x0): undefined reference to `kerepTypeId_AutoarrChar' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UInt64Ptr[.refptr.kerepTypeId_UInt64Ptr]+0x0): undefined reference to `kerepTypeId_UInt64Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Int64Ptr[.refptr.kerepTypeId_Int64Ptr]+0x0): undefined reference to `kerepTypeId_Int64Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UInt32Ptr[.refptr.kerepTypeId_UInt32Ptr]+0x0): undefined reference to `kerepTypeId_UInt32Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Int32Ptr[.refptr.kerepTypeId_Int32Ptr]+0x0): undefined reference to `kerepTypeId_Int32Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UInt16Ptr[.refptr.kerepTypeId_UInt16Ptr]+0x0): undefined reference to `kerepTypeId_UInt16Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Int16Ptr[.refptr.kerepTypeId_Int16Ptr]+0x0): undefined reference to `kerepTypeId_Int16Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UInt8Ptr[.refptr.kerepTypeId_UInt8Ptr]+0x0): undefined reference to `kerepTypeId_UInt8Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Int8Ptr[.refptr.kerepTypeId_Int8Ptr]+0x0): undefined reference to `kerepTypeId_Int8Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Float64Ptr[.refptr.kerepTypeId_Float64Ptr]+0x0): undefined reference to `kerepTypeId_Float64Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Float32Ptr[.refptr.kerepTypeId_Float32Ptr]+0x0): undefined reference to `kerepTypeId_Float32Ptr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_BoolPtr[.refptr.kerepTypeId_BoolPtr]+0x0): undefined reference to `kerepTypeId_BoolPtr' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UInt32[.refptr.kerepTypeId_UInt32]+0x0): undefined reference to `kerepTypeId_UInt32' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Int32[.refptr.kerepTypeId_Int32]+0x0): undefined reference to `kerepTypeId_Int32' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UInt16[.refptr.kerepTypeId_UInt16]+0x0): undefined reference to `kerepTypeId_UInt16' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Int16[.refptr.kerepTypeId_Int16]+0x0): undefined reference to `kerepTypeId_Int16' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_UInt8[.refptr.kerepTypeId_UInt8]+0x0): undefined reference to `kerepTypeId_UInt8' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Int8[.refptr.kerepTypeId_Int8]+0x0): undefined reference to `kerepTypeId_Int8' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Float32[.refptr.kerepTypeId_Float32]+0x0): undefined reference to `kerepTypeId_Float32' -C:/Program Files/Git/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/12.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: obj/init.c.o:init.c:(.rdata$.refptr.kerepTypeId_Char[.refptr.kerepTypeId_Char]+0x0): undefined reference to `kerepTypeId_Char' -collect2.exe: error: ld returned 1 exit status -make: *** [Makefile:3: build_test] Error 1 diff --git a/make.log b/make.log deleted file mode 100644 index 68f1bc3..0000000 --- a/make.log +++ /dev/null @@ -1,71 +0,0 @@ - H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H H =============[build_test]============= -clearing bin -clearing obj --------------[compile_c]-------------- -compiler: gcc -standard: c11 -warnings: -Wall -Wno-discarded-qualifiers -args: -O2 -sources: src/Autoarr/Autoarr.c -src/Autoarr/Autoarr_KVPair_exported.c -src/Autoarr/Autoarr_Unitype_exported.c -src/base/cptr.c -src/base/errors.c -src/base/init.c -src/base/types.c -src/base/unitype.c -src/DtsodParser/DtsodV24.c -src/DtsodParser/DtsodV24_deserialize.c -src/DtsodParser/DtsodV24_exported.c -src/DtsodParser/DtsodV24_serialize.c -src/HashFunctions/hash.c -src/Hashtable/Hashtable.c -src/Hashtable/KeyValuePair.c -src/SearchTree/SearchTree.c -src/String/string.c -src/String/StringBuilder.c tests/test_autoarr.c -tests/test_dtsod.c -tests/test_hashtable.c -tests/test_hash_functions.c -tests/test_marshalling.c -tests/test_safethrow.c -tests/test_searchtree.c -tests/test_string.c -some error happened -some error happened -some error happened -some error happened -some error happened -some error happened -some error happened -some error happened -some error happened -------------[compile_cpp]------------- -compiler: g++ -standard: c++17 -warnings: -Wall -Wno-unused-variable -Wno-return-type -args: -O2 -sources:  tests/main.cpp -----------------[link]---------------- -args: -O2 -outfile: bin/kerep.com -objects: obj/Autoarr.c.o -obj/Autoarr_KVPair_exported.c.o -obj/Autoarr_Unitype_exported.c.o -obj/cptr.c.o -obj/DtsodV24_exported.c.o -obj/errors.c.o -obj/hash.c.o -obj/init.c.o -obj/KeyValuePair.c.o -obj/main.cpp.o -obj/string.c.o -obj/StringBuilder.c.o -obj/test_autoarr.c.o -obj/test_dtsod.c.o -obj/test_hash_functions.c.o -obj/test_string.c.o -obj/types.c.o -obj/unitype.c.o -some error happened - \ No newline at end of file From 03b9ede38287e9b1250e9d5f7d123180564518f7 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 26 Aug 2022 13:58:36 +0600 Subject: [PATCH 12/59] some fixes --- build_scripts/default.config.sh | 2 +- src/Autoarr/Autoarr.c | 2 +- src/Autoarr/Autoarr_declare.h | 1 + src/Hashtable/Hashtable.c | 3 ++- src/Hashtable/KeyValuePair.c | 2 +- src/SearchTree/SearchTree.c | 1 - src/base/unitype.c | 10 +++++----- 7 files changed, 11 insertions(+), 10 deletions(-) diff --git a/build_scripts/default.config.sh b/build_scripts/default.config.sh index fa3755e..bcadc02 100644 --- a/build_scripts/default.config.sh +++ b/build_scripts/default.config.sh @@ -21,7 +21,7 @@ BUILD_TEST_CPP_ARGS="$BUILD_TEST_C_ARGS" BUILD_TEST_LINKER_ARGS="" # build_test_dbg -TEST_DBG_FILE=$TEST_FILE +TEST_DBG_FILE=$TEST_FILE.dbg BUILD_TEST_DBG_C_ARGS="-O0 -g" BUILD_TEST_DBG_CPP_ARGS="$BUILD_TEST_DBG_C_ARGS" BUILD_TEST_DBG_LINKER_ARGS="" diff --git a/src/Autoarr/Autoarr.c b/src/Autoarr/Autoarr.c index 5202b6d..abff22b 100644 --- a/src/Autoarr/Autoarr.c +++ b/src/Autoarr/Autoarr.c @@ -46,6 +46,6 @@ kerepTypeId_define(kerepTypeId_AutoarrUnitypePtr); // right func to clear array of unitype values void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr){ Autoarr_foreach(ar, u,Unitype_free(u)); - if(freePtr) Autoarr_free(ar, freePtr); + __Autoarr_free_Unitype(ar, freePtr); } void ____Autoarr_free_Unitype_(void* ar) { __Autoarr_free_Unitype_((Autoarr(Unitype)*)ar, false); } \ No newline at end of file diff --git a/src/Autoarr/Autoarr_declare.h b/src/Autoarr/Autoarr_declare.h index c9434a3..404a361 100644 --- a/src/Autoarr/Autoarr_declare.h +++ b/src/Autoarr/Autoarr_declare.h @@ -29,6 +29,7 @@ typedef struct Autoarr_##type{\ } Autoarr_##type;\ \ Autoarr_##type* __Autoarr_create_##type(uint16 max_blocks_count, uint16 max_block_length);\ +void __Autoarr_free_##type(Autoarr_##type* ar, bool freePtr);\ void ____Autoarr_free_##type(void* ar); #define Autoarr(type) Autoarr_##type diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index d68c6c7..4b1b921 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -50,7 +50,8 @@ void Hashtable_expand(Hashtable* ht){ Autoarr(KVPair)* newar=newrows[newrown]; Autoarr_add(newar,p); } - Autoarr_free(ar, true); + // there is no need to free array values, because they are copied into new array + __Autoarr_free_KVPair(ar, true); } free(ht->rows); diff --git a/src/Hashtable/KeyValuePair.c b/src/Hashtable/KeyValuePair.c index 845ead5..22c3f5e 100644 --- a/src/Hashtable/KeyValuePair.c +++ b/src/Hashtable/KeyValuePair.c @@ -17,7 +17,7 @@ void __KVPair_free(void* p){ KVPair_free(*(KVPair*)p); } // func for KVP array clearing void __Autoarr_free_KVPair_(Autoarr_KVPair* ar, bool freePtr){ Autoarr_foreach(ar,k,KVPair_free(k)); - Autoarr_free(ar, freePtr); + __Autoarr_free_KVPair(ar, freePtr); } void ____Autoarr_free_KVPair_(void* ar){ __Autoarr_free_KVPair_((Autoarr_KVPair*)ar, false); diff --git a/src/SearchTree/SearchTree.c b/src/SearchTree/SearchTree.c index 18088ab..23aa853 100644 --- a/src/SearchTree/SearchTree.c +++ b/src/SearchTree/SearchTree.c @@ -36,7 +36,6 @@ void __STNode_free(void* _node){ } if(node->value.VoidPtr) Unitype_free(node->value); - free(node); } void STNode_free(STNode* node){ __STNode_free(node); diff --git a/src/base/unitype.c b/src/base/unitype.c index 005cee5..8a555c5 100644 --- a/src/base/unitype.c +++ b/src/base/unitype.c @@ -19,17 +19,17 @@ char* sprintuni(Unitype v){ if(v.typeId==kerepTypeId_Null) sprintf_s(buf, BUFSIZE, "{Null}"); else if(v.typeId==kerepTypeId_Float64) - sprintf_s(buf, BUFSIZE, "{%s ) %lf}", type.name,v.Float64); + sprintf_s(buf, BUFSIZE, "{%s : %lf}", type.name,v.Float64); else if(v.typeId==kerepTypeId_Bool || v.typeId==kerepTypeId_UInt64) - sprintf_s(buf, BUFSIZE, "{%s ) " IFWIN("%llu", "%lu") "}", type.name,v.UInt64); + sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%llu", "%lu") "}", type.name,v.UInt64); else if(v.typeId==kerepTypeId_Int64) - sprintf_s(buf, BUFSIZE, "{%s ) " IFWIN("%lld", "%ld") "}", type.name,v.Int64); + sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%lld", "%ld") "}", type.name,v.Int64); else if(v.typeId==kerepTypeId_CharPtr){ size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2; buf=realloc(buf, newBUFSIZE); - sprintf_s(buf, BUFSIZE, "{%s ) \"%s\"}", type.name,(char*)v.VoidPtr); + sprintf_s(buf, BUFSIZE, "{%s : \"%s\"}", type.name,(char*)v.VoidPtr); } - else sprintf_s(buf, BUFSIZE, "{%s ) %p}", type.name,v.VoidPtr); + else sprintf_s(buf, BUFSIZE, "{%s : %p}", type.name,v.VoidPtr); return buf; } From 45c4f383eef2e25cc2c7bad1ea958b608a0f4f9d Mon Sep 17 00:00:00 2001 From: timerix Date: Sat, 27 Aug 2022 22:44:15 +0600 Subject: [PATCH 13/59] fixed dtsod serilization bug --- src/DtsodParser/DtsodV24_serialize.c | 30 ++++++++++++++++------------ 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/DtsodParser/DtsodV24_serialize.c b/src/DtsodParser/DtsodV24_serialize.c index 1988acf..601c0c0 100644 --- a/src/DtsodParser/DtsodV24_serialize.c +++ b/src/DtsodParser/DtsodV24_serialize.c @@ -74,22 +74,26 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){ } else if(u.typeId==kerepTypeId_HashtablePtr){ // check hashtable is blank + bool hashtableNotBlank=false; Hashtable_foreach(((Hashtable*)u.VoidPtr), __, ({ - goto hashtableNotBlank; + hashtableNotBlank=true; if(__.key); // weird way to disable warning + break; })); - // blank hashtable - addc('{'); - addc('}'); - // not blank hashtable - hashtableNotBlank: - addc('\n'); - AppendTabs(); - addc('{'); - addc('\n'); - try(__serialize(b,tabs+1,u.VoidPtr),___,;); - AppendTabs(); - addc('}'); + + if(hashtableNotBlank){ + addc('\n'); + AppendTabs(); + addc('{'); + addc('\n'); + try(__serialize(b,tabs+1,u.VoidPtr),___,;); + AppendTabs(); + addc('}'); + } + else { + addc('{'); + addc('}'); + } } else { dbg((u.typeId)); From 37e8fdcd6aefb7073f88a73aa33f30bfd0665388 Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 31 Aug 2022 01:09:04 +0600 Subject: [PATCH 14/59] small changes --- build_scripts/default.config.sh | 4 ++-- src/base/std.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/build_scripts/default.config.sh b/build_scripts/default.config.sh index bcadc02..7c2c541 100644 --- a/build_scripts/default.config.sh +++ b/build_scripts/default.config.sh @@ -6,8 +6,8 @@ CMP_C=gcc CMP_CPP=g++ STD_C=c11 STD_CPP=c++17 -WARN_C="-Wall -Wno-discarded-qualifiers" #-Wextra -WARN_CPP="-Wall -Wno-unused-variable -Wno-return-type" #-Wextra +WARN_C="-Wall -Wno-discarded-qualifiers" +WARN_CPP="-Wall" SRC_C="$( find src -name '*.c')" SRC_CPP="$( find src -name '*.cpp')" TESTS_C="$( find tests -name '*.c')" diff --git a/src/base/std.h b/src/base/std.h index de0b9e9..1a69b10 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -38,7 +38,7 @@ typedef double float64; #define CALL #endif #ifndef typeof - #define typeof __typeof__ + #define typeof(X) __typeof__(X) #endif #else #pragma GCC error "unknown compiler" From 982a79a0521402d4401cc40dd839ef14b045f3a6 Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 31 Aug 2022 14:17:48 +0600 Subject: [PATCH 15/59] changed some log messages --- src/base/types.c | 5 ++++- tests/main.cpp | 2 +- tests/test_searchtree.c | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/base/types.c b/src/base/types.c index 6f08596..b96bdb6 100644 --- a/src/base/types.c +++ b/src/base/types.c @@ -44,6 +44,7 @@ typedef enum{ kerepTypeDescriptorsState initState=NotInitialized; void kerepTypeDescriptors_beginInit(){ + printf("\e[94mtype descriptors initializing...\n"); __kerepTypeDescriptors=Autoarr_create(kerepTypeDescriptor, 256, 256); if(__kerepTypeDescriptors==NULL) throw(ERR_NULLPTR); } @@ -52,14 +53,16 @@ void kerepTypeDescriptors_endInit(){ typeDescriptors=Autoarr_toArray(__kerepTypeDescriptors); Autoarr_free(__kerepTypeDescriptors,true); if(typeDescriptors==NULL) throw(ERR_NULLPTR); + printf("\e[92minitialized %u type descriptors\n", kerepTypeId_last); } + void __kerepType_register(char* name, int16 size, void (*free_members)(void*)){ kerepTypeDescriptor typeDesc={ .name=name, .size=size, .free_members=free_members, .id=++kerepTypeId_last - }; dbg(kerepTypeId_last); + }; Autoarr_add(__kerepTypeDescriptors, typeDesc); } diff --git a/tests/main.cpp b/tests/main.cpp index 13abfcf..1f74e68 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -16,7 +16,7 @@ int main(){ kerepTypeDescriptors_beginInit(); kerepTypeDescriptors_initKerepTypes(); kerepTypeDescriptors_endInit(); - printf("\e[92mkerep tests are starting!\e[97m\n"); + printf("\e[97mkerep tests are starting!\n"); optime("test_all",1,test_all()); printf("\e[0m\n"); return 0; diff --git a/tests/test_searchtree.c b/tests/test_searchtree.c index b38ce7c..6309e85 100644 --- a/tests/test_searchtree.c +++ b/tests/test_searchtree.c @@ -6,8 +6,9 @@ void printstnode(STNode* node){ IFWIN("%llu", "%lu") "\n address: %p\n value: ",sizeof(STNode),node); printuni(node->value); + printf("\n"); // prints pointers to all existing branches - /* printf("\n branches: %p\n", node->branches); + /* printf(" branches: %p\n", node->branches); if(node->branches) for(uint8 i=0;i<8;i++){ printf(" \e[90m[%u]=%p\n",i,node->branches[i]); if(node->branches[i]) From eca678026fc33901408adb88dfc290b59f720a4f Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 31 Aug 2022 21:44:14 +0600 Subject: [PATCH 16/59] startsWith() & endsWith() --- src/base/cptr.c | 13 +++++++++++++ src/base/cptr.h | 4 ++++ 2 files changed, 17 insertions(+) diff --git a/src/base/cptr.c b/src/base/cptr.c index 22a6b36..a4080fe 100644 --- a/src/base/cptr.c +++ b/src/base/cptr.c @@ -34,3 +34,16 @@ char* char_multiply(char c, uint32 n){ rez[n]=c; 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_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; +} \ No newline at end of file diff --git a/src/base/cptr.h b/src/base/cptr.h index ccce381..b750842 100644 --- a/src/base/cptr.h +++ b/src/base/cptr.h @@ -18,6 +18,10 @@ bool cptr_compare(char* key0, char* key1); // multiplies char n times char* char_multiply(char c, uint32 n); +bool cptr_startsWith(char* ptr, char* fragment); + +bool cptr_endsWith(char* ptr, char* fragment); + #if __cplusplus } #endif \ No newline at end of file From 2b4046313d51959037ad851ebd62fecbcc58e9cf Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 31 Aug 2022 21:44:56 +0600 Subject: [PATCH 17/59] kprint_format --- src/base/base.h | 1 + src/base/kprint/README.md | 2 + src/base/kprint/data_fmt.txt | 13 ++++++ src/base/kprint/kprint.c | 22 +++++++++++ src/base/kprint/kprint.h | 51 ++++++++++++++++++++++++ src/base/kprint/kprint_colors.h | 55 ++++++++++++++++++++++++++ src/base/kprint/kprint_format.h | 70 +++++++++++++++++++++++++++++++++ src/base/std.h | 1 + 8 files changed, 215 insertions(+) create mode 100644 src/base/kprint/README.md create mode 100644 src/base/kprint/data_fmt.txt create mode 100644 src/base/kprint/kprint.c create mode 100644 src/base/kprint/kprint.h create mode 100644 src/base/kprint/kprint_colors.h create mode 100644 src/base/kprint/kprint_format.h diff --git a/src/base/base.h b/src/base/base.h index cd9346a..c728a94 100644 --- a/src/base/base.h +++ b/src/base/base.h @@ -11,6 +11,7 @@ extern "C" { #include "types.h" #include "unitype.h" #include "init.h" +#include "kprint/kprint.h" #if __cplusplus } diff --git a/src/base/kprint/README.md b/src/base/kprint/README.md new file mode 100644 index 0000000..e375f83 --- /dev/null +++ b/src/base/kprint/README.md @@ -0,0 +1,2 @@ +## kprint +I don't really like printf function, so i made safer and more convinient replacement. \ No newline at end of file diff --git a/src/base/kprint/data_fmt.txt b/src/base/kprint/data_fmt.txt new file mode 100644 index 0000000..31f434f --- /dev/null +++ b/src/base/kprint/data_fmt.txt @@ -0,0 +1,13 @@ +[data types] +int 0x2000 00100000 00000000 +uint + withPostfix + uppercase 0x2001 00100000 00000001 +hex + withPrefix + uppercase 0x2002 00100000 00000010 +bin + withPrefix 0x2003 00100000 00000011 +float + withPostfix + uppercase 0x3000 00110000 00000000 +char 0x3001 00110000 00000001 +string 0x3002 00110000 00000010 +??? 0x3003 00110000 00000011 + +[optional flags] +withPrefix = withPostfix 0x0004 00000000 00000100 +upperase 0x0008 00000000 00001000 \ No newline at end of file diff --git a/src/base/kprint/kprint.c b/src/base/kprint/kprint.c new file mode 100644 index 0000000..ade114d --- /dev/null +++ b/src/base/kprint/kprint.c @@ -0,0 +1,22 @@ +#include "../base.h" + + + +Maybe __ksprint(uint8 n, uint16 sizes[32], ...){ + for(uint8 i=0; i #include #include +#include #include #include #include From 885259f82af70e81c104b9c34c08e6abef692e93 Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 31 Aug 2022 21:48:46 +0600 Subject: [PATCH 18/59] fixed some mistakes --- src/base/kprint/kprint.c | 4 ++-- src/base/kprint/kprint.h | 6 +++--- src/base/kprint/kprint_format.h | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/base/kprint/kprint.c b/src/base/kprint/kprint.c index ade114d..1323c3c 100644 --- a/src/base/kprint/kprint.c +++ b/src/base/kprint/kprint.c @@ -2,7 +2,7 @@ -Maybe __ksprint(uint8 n, uint16 sizes[32], ...){ +/* Maybe __ksprint(uint8 n, uint16 sizes[32], ...){ for(uint8 i=0; i Date: Wed, 31 Aug 2022 21:49:29 +0600 Subject: [PATCH 19/59] removed unneeded `else` from try() --- src/base/errors.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/base/errors.h b/src/base/errors.h index 4109782..06f0017 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -59,12 +59,12 @@ char* __unknownErr( ); freeMem;\ _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ return _rezult;\ - }else + } #define tryLast(_funcCall, _rezult) Maybe _rezult=_funcCall; if(_rezult.errmsg){\ _rezult.errmsg=__extendErrMsg(_rezult.errmsg, __FILE__,__LINE__,__func__);\ __EXIT(_rezult.errmsg);\ - }else + } #endif From 55ab45eb56f7ac9db810c48038203b00e728076a Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Tue, 6 Sep 2022 15:17:58 +0600 Subject: [PATCH 20/59] kprint.h without variadic functions --- src/base/kprint/kprint.h | 60 ++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 18 deletions(-) diff --git a/src/base/kprint/kprint.h b/src/base/kprint/kprint.h index 38bb526..e6ec8f5 100644 --- a/src/base/kprint/kprint.h +++ b/src/base/kprint/kprint.h @@ -8,43 +8,67 @@ extern "C" { #include "kprint_colors.h" #include "kprint_format.h" -#define __count_va_args(\ +#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) a32 -#define count_va_args(ARGS...) __count_va_args(\ +#define count_args(ARGS...) __count_args(\ ARGS,\ 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) -#define __get_va_args_sizes(\ +#define __kprint_argsToFormats8(\ + a0, a1, a2, a3, a4, a5, a6, a7)\ + (kprint_format[]{ a0,a2,a4,a6 }) +#define __kprint_argsToObjects8(\ + a0, a1, a2, a3, a4, a5, a6, a7)\ + (void*[]{ a1,a3,a5,a7 }) + +#define __kprint_argsToFormats16(\ + a0, a1, a2, a3, a4, a5, a6, a7,\ + a8, a9, a10,a11,a12,a13,a14,a15)\ + (kprint_format[]{ a0,a2,a4,a6,a8,a10,a12,a14 }) +#define __kprint_argsToObjects16(\ + a0, a1, a2, a3, a4, a5, a6, a7,\ + a8, a9, a10,a11,a12,a13,a14,a15)\ + (void*[]{ a1,a3,a5,a7,a9,a11,a13,a15 }) + +#define __kprint_argsToFormats32(\ 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)\ - (uint16[]{\ - sizeof(a0), sizeof(a1), sizeof(a2), sizeof(a3), sizeof(a4), sizeof(a5), sizeof(a6), sizeof(a7),\ - sizeof(a8), sizeof(a9), sizeof(a10),sizeof(a11),sizeof(a12),sizeof(a13),sizeof(a14),sizeof(a15),\ - sizeof(a16),sizeof(a17),sizeof(a18),sizeof(a19),sizeof(a20),sizeof(a21),sizeof(a22),sizeof(a23),\ - sizeof(a24),sizeof(a25),sizeof(a26),sizeof(a27),sizeof(a28),sizeof(a29),sizeof(a30),sizeof(a31)\ - }) -#define get_va_args_sizes(ARGS...) __get_va_args_sizes(ARGS,\ - 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) + (kprint_format[]{ a0,a2,a4,a6,a8,a10,a12,a14,a16,a18,a20,a22,a24,a26,a28,a30 } +#define __kprint_argsToObjects32(\ + 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)\ + (void*[]{ a1,a3,a5,a7,a9,a11,a13,a15,a17,a19,a21,a23,a25,a27,a29,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 __kprint_argsToArrs(COUNT,ARGS...)(\ + (COUNT<=8 __kprint_argsToFormats8(ARGS,__32zeroes) :\ + COUNT<=16 __kprint_argsToFormats16(ARGS,__32zeroes) :\ + __kprint_argsToFormats32(ARGS,__32zeroes)),\ + (COUNT<=8 __kprint_argsToObjects8(ARGS,__32zeroes) :\ + COUNT<=16 __kprint_argsToObjects16(ARGS) :\ + __kprint_argsToObjects32(ARGS,__32zeroes)) -Maybe __ksprint(uint8 n, uint16 sizes[32], ...); -#define ksprint(ARGS...) __ksprint(count_va_args(ARGS), get_va_args_sizes(ARGS), ARGS) +Maybe __ksprint(uint8 n, kprint_format* formats, void** objects); +#define ksprint(ARGS...) __ksprint(count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS)) -Maybe __kfprint(FILE* fd, uint8 n, uint16 sizes[32], ...); -#define kfprint(FD, ARGS...) __kfprint(FD, count_va_args(ARGS), get_va_args_sizes(ARGS), ARGS) +Maybe __kfprint(FILE* fd, uint8 n, kprint_format* formats, void** objects); +#define kfprint(FD, ARGS...) __kfprint(FD, count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS)) -void __kprint(uint8 n, uint16 sizes[32], ...); -#define kprint(ARGS...) __kprint(count_va_args(ARGS), get_va_args_sizes(ARGS), ARGS) +void __kprint(uint8 n, kprint_format* formats, void** objects); +#define kprint(ARGS...) __kprint(count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS)) #if __cplusplus } From 3eb1e82f062309aeda1b96b74e0e74f1c50a8fac Mon Sep 17 00:00:00 2001 From: Timerix22 Date: Tue, 6 Sep 2022 15:19:14 +0600 Subject: [PATCH 21/59] script which chmods other scripts --- chmod_scripts.sh | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 chmod_scripts.sh diff --git a/chmod_scripts.sh b/chmod_scripts.sh new file mode 100644 index 0000000..ec71ae9 --- /dev/null +++ b/chmod_scripts.sh @@ -0,0 +1,7 @@ +#!/bin/bash +SCRIPTS="$(find build_scripts -name '*.sh')" +for F in $SCRIPTS +do + echo $F + chmod +x $F +done From e63056d2446cbc5ef28aa42060b7bbcb32ffa76c Mon Sep 17 00:00:00 2001 From: timerix Date: Tue, 6 Sep 2022 15:53:13 +0600 Subject: [PATCH 22/59] some type system updates --- src/DtsodParser/DtsodV24_deserialize.c | 14 +-- src/DtsodParser/DtsodV24_serialize.c | 2 +- src/String/string.c | 3 + src/String/string.h | 2 + src/base/init.c | 136 +++++++++++++------------ src/base/types.c | 15 ++- src/base/types.h | 17 +++- src/base/unitype.c | 4 +- src/base/unitype.h | 16 +-- tests/test_marshalling.c | 2 +- tests/test_searchtree.c | 4 +- 11 files changed, 119 insertions(+), 96 deletions(-) diff --git a/src/DtsodParser/DtsodV24_deserialize.c b/src/DtsodParser/DtsodV24_deserialize.c index cd84b53..8acc293 100644 --- a/src/DtsodParser/DtsodV24_deserialize.c +++ b/src/DtsodParser/DtsodV24_deserialize.c @@ -98,9 +98,9 @@ Maybe __ReadName(DeserializeSharedData* shared){ case '}': if(!calledRecursively || nameStr.length!=0) safethrow_wrongchar(c,;); - return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,NULL)); + return SUCCESS(UniHeap(kerepTypeId_CharPtr,NULL)); case ':': - return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,string_extract(nameStr))); + return SUCCESS(UniHeap(kerepTypeId_CharPtr,string_extract(nameStr))); case '$': if(nameStr.length!=0) safethrow_wrongchar(c,;); @@ -113,7 +113,7 @@ Maybe __ReadName(DeserializeSharedData* shared){ } if(nameStr.length>0) safethrow(ERR_ENDOFSTR,;); - return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,NULL)); + return SUCCESS(UniHeap(kerepTypeId_CharPtr,NULL)); } #define ReadName() __ReadName(shared) @@ -137,7 +137,7 @@ Maybe __ReadString(DeserializeSharedData* shared){ } else { char* str=StringBuilder_build(b).ptr; - return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr,str)); + return SUCCESS(UniHeap(kerepTypeId_CharPtr,str)); } } else { @@ -163,7 +163,7 @@ Maybe __ReadList(DeserializeSharedData* shared){ } } - return SUCCESS(UniPtrHeap(kerepTypeId_AutoarrUnitypePtr,list)); + return SUCCESS(UniHeap(kerepTypeId_AutoarrUnitypePtr,list)); }; #define ReadList() __ReadList(shared) @@ -321,7 +321,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) { } else{ list=Autoarr_create(Unitype,ARR_BC,ARR_BL); - Hashtable_add(dict,nameCPtr,UniPtrHeap(kerepTypeId_AutoarrUnitypePtr,list)); + Hashtable_add(dict,nameCPtr,UniHeap(kerepTypeId_AutoarrUnitypePtr,list)); } Autoarr_add(list,val.value); } @@ -331,7 +331,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) { END: *_text=text; - return SUCCESS(UniPtrHeap(kerepTypeId_HashtablePtr,dict)); + return SUCCESS(UniHeap(kerepTypeId_HashtablePtr,dict)); } Maybe DtsodV24_deserialize(char* _text) { diff --git a/src/DtsodParser/DtsodV24_serialize.c b/src/DtsodParser/DtsodV24_serialize.c index 601c0c0..3ab0346 100644 --- a/src/DtsodParser/DtsodV24_serialize.c +++ b/src/DtsodParser/DtsodV24_serialize.c @@ -126,5 +126,5 @@ Maybe DtsodV24_serialize(Hashtable* dtsod){ StringBuilder* sb=StringBuilder_create(); try(__serialize(sb,0,dtsod),__, StringBuilder_free(sb)); char* str=StringBuilder_build(sb).ptr; - return SUCCESS(UniPtrHeap(kerepTypeId_CharPtr, str)); + return SUCCESS(UniHeap(kerepTypeId_CharPtr, str)); } diff --git a/src/String/string.c b/src/String/string.c index 2926359..e7d92a6 100644 --- a/src/String/string.c +++ b/src/String/string.c @@ -1,5 +1,8 @@ #include "string.h" +kerepTypeId_define(kerepTypeId_string); +kerepTypeId_define(kerepTypeId_stringPtr); + // copies str content to new char pointer value (adding '\0' at the end) char* string_extract(string str){ if(str.length==0) return NULL; diff --git a/src/String/string.h b/src/String/string.h index e2076ee..0b9a658 100644 --- a/src/String/string.h +++ b/src/String/string.h @@ -12,6 +12,8 @@ typedef struct string{ char* ptr; // char pointer uint32 length; // amount of chars in ptr value } string; +kerepTypeId_declare(kerepTypeId_string); +kerepTypeId_declare(kerepTypeId_stringPtr); static const string stringNull={NULL,0}; diff --git a/src/base/init.c b/src/base/init.c index f838c64..cef81d1 100644 --- a/src/base/init.c +++ b/src/base/init.c @@ -6,92 +6,98 @@ void kerepTypeDescriptors_initKerepTypes(){ // null - kerepType_register(NULL, kerepTypeId_Null, NULL); + kerepType_register(NULL, kerepTypeId_Null, NULL, NULL); // base types - kerepType_register(char, kerepTypeId_Char, NULL); - kerepType_register(bool, kerepTypeId_Bool, NULL); - kerepType_register(float32, kerepTypeId_Float32, NULL); - kerepType_register(float64, kerepTypeId_Float64, NULL); - kerepType_register(int8, kerepTypeId_Int8, NULL); - kerepType_register(uint8, kerepTypeId_UInt8, NULL); - kerepType_register(int16, kerepTypeId_Int16, NULL); - kerepType_register(uint16, kerepTypeId_UInt16, NULL); - kerepType_register(int32, kerepTypeId_Int32, NULL); - kerepType_register(uint32, kerepTypeId_UInt32, NULL); - kerepType_register(int64, kerepTypeId_Int64, NULL); - kerepType_register(uint64, kerepTypeId_UInt64, NULL); + kerepType_register(char, kerepTypeId_Char, NULL, NULL); + kerepType_register(bool, kerepTypeId_Bool, NULL, NULL); + kerepType_register(float32, kerepTypeId_Float32, NULL, NULL); + kerepType_register(float64, kerepTypeId_Float64, NULL, NULL); + kerepType_register(int8, kerepTypeId_Int8, NULL, NULL); + kerepType_register(uint8, kerepTypeId_UInt8, NULL, NULL); + kerepType_register(int16, kerepTypeId_Int16, NULL, NULL); + kerepType_register(uint16, kerepTypeId_UInt16, NULL, NULL); + kerepType_register(int32, kerepTypeId_Int32, NULL, NULL); + kerepType_register(uint32, kerepTypeId_UInt32, NULL, NULL); + kerepType_register(int64, kerepTypeId_Int64, NULL, NULL); + kerepType_register(uint64, kerepTypeId_UInt64, NULL, NULL); // base type pointers - kerepType_register(char*, kerepTypeId_CharPtr, NULL); - kerepType_register(bool*, kerepTypeId_BoolPtr, NULL); - kerepType_register(float32*, kerepTypeId_Float32Ptr, NULL); - kerepType_register(float64*, kerepTypeId_Float64Ptr, NULL); - kerepType_register(int8*, kerepTypeId_Int8Ptr, NULL); - kerepType_register(uint8*, kerepTypeId_UInt8Ptr, NULL); - kerepType_register(int16*, kerepTypeId_Int16Ptr, NULL); - kerepType_register(uint16*, kerepTypeId_UInt16Ptr, NULL); - kerepType_register(int32*, kerepTypeId_Int32Ptr, NULL); - kerepType_register(uint32*, kerepTypeId_UInt32Ptr, NULL); - kerepType_register(int64*, kerepTypeId_Int64Ptr, NULL); - kerepType_register(uint64*, kerepTypeId_UInt64Ptr, NULL); + kerepType_register(char*, kerepTypeId_CharPtr, NULL, NULL); + kerepType_register(bool*, kerepTypeId_BoolPtr, NULL, NULL); + kerepType_register(float32*, kerepTypeId_Float32Ptr, NULL, NULL); + kerepType_register(float64*, kerepTypeId_Float64Ptr, NULL, NULL); + kerepType_register(int8*, kerepTypeId_Int8Ptr, NULL, NULL); + kerepType_register(uint8*, kerepTypeId_UInt8Ptr, NULL, NULL); + kerepType_register(int16*, kerepTypeId_Int16Ptr, NULL, NULL); + kerepType_register(uint16*, kerepTypeId_UInt16Ptr, NULL, NULL); + kerepType_register(int32*, kerepTypeId_Int32Ptr, NULL, NULL); + kerepType_register(uint32*, kerepTypeId_UInt32Ptr, NULL, NULL); + kerepType_register(int64*, kerepTypeId_Int64Ptr, NULL, NULL); + kerepType_register(uint64*, kerepTypeId_UInt64Ptr, NULL, NULL); + // kerepTypeDescriptor + kerepType_register(kerepTypeDescriptor, kerepTypeId_kerepTypeDescriptor, NULL, NULL); + kerepType_register(kerepTypeDescriptor*, kerepTypeId_kerepTypeDescriptorPtr, NULL, NULL); // base type autoarrs - kerepType_register(Autoarr_char, kerepTypeId_AutoarrChar, ____Autoarr_free_char); - kerepType_register(Autoarr_bool, kerepTypeId_AutoarrBool, ____Autoarr_free_bool); - kerepType_register(Autoarr_float32, kerepTypeId_AutoarrFloat32, ____Autoarr_free_float32); - kerepType_register(Autoarr_float64, kerepTypeId_AutoarrFloat64, ____Autoarr_free_float64); - kerepType_register(Autoarr_int8, kerepTypeId_AutoarrInt8, ____Autoarr_free_int8); - kerepType_register(Autoarr_uint8, kerepTypeId_AutoarrUInt8, ____Autoarr_free_uint8); - kerepType_register(Autoarr_int16, kerepTypeId_AutoarrInt16, ____Autoarr_free_int16); - kerepType_register(Autoarr_uint16, kerepTypeId_AutoarrUInt16, ____Autoarr_free_uint16); - kerepType_register(Autoarr_int32, kerepTypeId_AutoarrInt32, ____Autoarr_free_int32); - kerepType_register(Autoarr_uint32, kerepTypeId_AutoarrUInt32, ____Autoarr_free_uint32); - kerepType_register(Autoarr_int64, kerepTypeId_AutoarrInt64, ____Autoarr_free_int64); - kerepType_register(Autoarr_uint64, kerepTypeId_AutoarrUInt64, ____Autoarr_free_uint64); + kerepType_register(Autoarr_char, kerepTypeId_AutoarrChar, ____Autoarr_free_char, NULL); + kerepType_register(Autoarr_bool, kerepTypeId_AutoarrBool, ____Autoarr_free_bool, NULL); + kerepType_register(Autoarr_float32, kerepTypeId_AutoarrFloat32, ____Autoarr_free_float32, NULL); + kerepType_register(Autoarr_float64, kerepTypeId_AutoarrFloat64, ____Autoarr_free_float64, NULL); + kerepType_register(Autoarr_int8, kerepTypeId_AutoarrInt8, ____Autoarr_free_int8, NULL); + kerepType_register(Autoarr_uint8, kerepTypeId_AutoarrUInt8, ____Autoarr_free_uint8, NULL); + kerepType_register(Autoarr_int16, kerepTypeId_AutoarrInt16, ____Autoarr_free_int16, NULL); + kerepType_register(Autoarr_uint16, kerepTypeId_AutoarrUInt16, ____Autoarr_free_uint16, NULL); + kerepType_register(Autoarr_int32, kerepTypeId_AutoarrInt32, ____Autoarr_free_int32, NULL); + kerepType_register(Autoarr_uint32, kerepTypeId_AutoarrUInt32, ____Autoarr_free_uint32, NULL); + kerepType_register(Autoarr_int64, kerepTypeId_AutoarrInt64, ____Autoarr_free_int64, NULL); + kerepType_register(Autoarr_uint64, kerepTypeId_AutoarrUInt64, ____Autoarr_free_uint64, NULL); // base type autoarr pointers - kerepType_register(Autoarr_char*, kerepTypeId_AutoarrCharPtr, ____Autoarr_free_char); - kerepType_register(Autoarr_bool*, kerepTypeId_AutoarrBoolPtr, ____Autoarr_free_bool); - kerepType_register(Autoarr_float32*, kerepTypeId_AutoarrFloat32Ptr, ____Autoarr_free_float32); - kerepType_register(Autoarr_float64*, kerepTypeId_AutoarrFloat64Ptr, ____Autoarr_free_float64); - kerepType_register(Autoarr_int8*, kerepTypeId_AutoarrInt8Ptr, ____Autoarr_free_int8); - kerepType_register(Autoarr_uint8*, kerepTypeId_AutoarrUInt8Ptr, ____Autoarr_free_uint8); - kerepType_register(Autoarr_int16*, kerepTypeId_AutoarrInt16Ptr, ____Autoarr_free_int16); - kerepType_register(Autoarr_uint16*, kerepTypeId_AutoarrUInt16Ptr, ____Autoarr_free_uint16); - kerepType_register(Autoarr_int32*, kerepTypeId_AutoarrInt32Ptr, ____Autoarr_free_int32); - kerepType_register(Autoarr_uint32*, kerepTypeId_AutoarrUInt32Ptr, ____Autoarr_free_uint32); - kerepType_register(Autoarr_int64*, kerepTypeId_AutoarrInt64Ptr, ____Autoarr_free_int64); - kerepType_register(Autoarr_uint64*, kerepTypeId_AutoarrUInt64Ptr, ____Autoarr_free_uint64); + kerepType_register(Autoarr_char*, kerepTypeId_AutoarrCharPtr, ____Autoarr_free_char, NULL); + kerepType_register(Autoarr_bool*, kerepTypeId_AutoarrBoolPtr, ____Autoarr_free_bool, NULL); + kerepType_register(Autoarr_float32*, kerepTypeId_AutoarrFloat32Ptr, ____Autoarr_free_float32, NULL); + kerepType_register(Autoarr_float64*, kerepTypeId_AutoarrFloat64Ptr, ____Autoarr_free_float64, NULL); + kerepType_register(Autoarr_int8*, kerepTypeId_AutoarrInt8Ptr, ____Autoarr_free_int8, NULL); + kerepType_register(Autoarr_uint8*, kerepTypeId_AutoarrUInt8Ptr, ____Autoarr_free_uint8, NULL); + kerepType_register(Autoarr_int16*, kerepTypeId_AutoarrInt16Ptr, ____Autoarr_free_int16, NULL); + kerepType_register(Autoarr_uint16*, kerepTypeId_AutoarrUInt16Ptr, ____Autoarr_free_uint16, NULL); + kerepType_register(Autoarr_int32*, kerepTypeId_AutoarrInt32Ptr, ____Autoarr_free_int32, NULL); + kerepType_register(Autoarr_uint32*, kerepTypeId_AutoarrUInt32Ptr, ____Autoarr_free_uint32, NULL); + kerepType_register(Autoarr_int64*, kerepTypeId_AutoarrInt64Ptr, ____Autoarr_free_int64, NULL); + kerepType_register(Autoarr_uint64*, kerepTypeId_AutoarrUInt64Ptr, ____Autoarr_free_uint64, NULL); // Unitype - kerepType_register(Unitype, kerepTypeId_Unitype, __UnitypePtr_free); - kerepType_register(Unitype*, kerepTypeId_UnitypePtr, __UnitypePtr_free); - kerepType_register(Autoarr_Unitype, kerepTypeId_AutoarrUnitype, ____Autoarr_free_Unitype_); - kerepType_register(Autoarr_Unitype*, kerepTypeId_AutoarrUnitypePtr, ____Autoarr_free_Unitype_); + kerepType_register(Unitype, kerepTypeId_Unitype, __UnitypePtr_free, NULL); + kerepType_register(Unitype*, kerepTypeId_UnitypePtr, __UnitypePtr_free, NULL); + kerepType_register(Autoarr_Unitype, kerepTypeId_AutoarrUnitype, ____Autoarr_free_Unitype_, NULL); + kerepType_register(Autoarr_Unitype*, kerepTypeId_AutoarrUnitypePtr, ____Autoarr_free_Unitype_, NULL); // 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 - kerepType_register(STNode, kerepTypeId_STNode, __STNode_free); - kerepType_register(STNode*, kerepTypeId_STNodePtr, __STNode_free); + kerepType_register(STNode, kerepTypeId_STNode, __STNode_free, NULL); + kerepType_register(STNode*, kerepTypeId_STNodePtr, __STNode_free, NULL); // KeyValuePair - kerepType_register(KVPair, kerepTypeId_KVPair, __KVPair_free); - kerepType_register(KVPair*, kerepTypeId_KVPairPtr, __KVPair_free); - kerepType_register(Autoarr_KVPair, kerepTypeId_AutoarrKVPair, ____Autoarr_free_KVPair_); - kerepType_register(Autoarr_KVPair*, kerepTypeId_AutoarrKVPairPtr, ____Autoarr_free_KVPair_); + kerepType_register(KVPair, kerepTypeId_KVPair, __KVPair_free, NULL); + kerepType_register(KVPair*, kerepTypeId_KVPairPtr, __KVPair_free, NULL); + kerepType_register(Autoarr_KVPair, kerepTypeId_AutoarrKVPair, ____Autoarr_free_KVPair_, NULL); + kerepType_register(Autoarr_KVPair*, kerepTypeId_AutoarrKVPairPtr, ____Autoarr_free_KVPair_, NULL); // 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 - kerepType_register(Hashtable, kerepTypeId_Hashtable, __Hashtable_free); - kerepType_register(Hashtable*, kerepTypeId_HashtablePtr, __Hashtable_free); + kerepType_register(Hashtable, kerepTypeId_Hashtable, __Hashtable_free, NULL); + kerepType_register(Hashtable*, kerepTypeId_HashtablePtr, __Hashtable_free, NULL); + // string + kerepType_register(string, kerepTypeId_string, NULL, NULL); + kerepType_register(string*, kerepTypeId_stringPtr, NULL, NULL); + kerepType_register(string, kerepTypeId_AutoarrString, ____Autoarr_free_string, NULL); + kerepType_register(string*, kerepTypeId_AutoarrStringPtr, ____Autoarr_free_string, NULL); // StringBuilder - kerepType_register(Autoarr_string, kerepTypeId_AutoarrString, ____Autoarr_free_string); - kerepType_register(Autoarr_string*, kerepTypeId_AutoarrStringPtr, ____Autoarr_free_string); - kerepType_register(StringBuilder, kerepTypeId_StringBuilder, __StringBuilder_free); - kerepType_register(StringBuilder*, kerepTypeId_StringBuilderPtr, __StringBuilder_free); + kerepType_register(StringBuilder, kerepTypeId_StringBuilder, __StringBuilder_free, NULL); + kerepType_register(StringBuilder*, kerepTypeId_StringBuilderPtr, __StringBuilder_free, NULL); } diff --git a/src/base/types.c b/src/base/types.c index b96bdb6..9bf5ce1 100644 --- a/src/base/types.c +++ b/src/base/types.c @@ -32,6 +32,9 @@ kerepTypeId_define(kerepTypeId_UInt32Ptr); kerepTypeId_define(kerepTypeId_Int64Ptr); kerepTypeId_define(kerepTypeId_UInt64Ptr); +kerepTypeId_define(kerepTypeId_kerepTypeDescriptor); +kerepTypeId_define(kerepTypeId_kerepTypeDescriptorPtr); + // type descriptors are stored here during initialization Autoarr(kerepTypeDescriptor)* __kerepTypeDescriptors=NULL; // here type descriptors are stored when initialization is complited @@ -56,16 +59,18 @@ void kerepTypeDescriptors_endInit(){ printf("\e[92minitialized %u type descriptors\n", kerepTypeId_last); } -void __kerepType_register(char* name, int16 size, void (*free_members)(void*)){ +void __kerepType_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, int32)){ kerepTypeDescriptor typeDesc={ .name=name, .size=size, - .free_members=free_members, - .id=++kerepTypeId_last + .id=++kerepTypeId_last, + .freeMembers=freeMembers, + .toString=toString }; Autoarr_add(__kerepTypeDescriptors, typeDesc); } -kerepTypeDescriptor kerepTypeDescriptor_get(kerepTypeId id){ - return typeDescriptors[id]; +Maybe kerepTypeDescriptor_get(kerepTypeId id){ + if(id>kerepTypeId_last) safethrow("invalid type id",;); + return SUCCESS(UniStack(kerepTypeId_kerepTypeDescriptorPtr, &typeDescriptors[id])); } diff --git a/src/base/types.h b/src/base/types.h index 8d5dd23..96ff861 100644 --- a/src/base/types.h +++ b/src/base/types.h @@ -9,10 +9,12 @@ extern "C" { typedef uint16 kerepTypeId; typedef struct kerepTypeDescriptor{ - void (*free_members)(void*); // NULL or function which frees all struct members char* name; kerepTypeId id; uint16 size; + void (*freeMembers)(void*); // NULL or function which frees all struct members + ///@return Maybe + Maybe (*toString)(void*, int32); // NULL or function which generates string representaion of object } kerepTypeDescriptor; #define kerepTypeId_declare(ID_VAR_NAME)\ @@ -21,15 +23,17 @@ typedef struct kerepTypeDescriptor{ kerepTypeId ID_VAR_NAME=-1 extern kerepTypeId kerepTypeId_last; -void __kerepType_register(char* name, int16 size, void (*free_members)(void*)); +void __kerepType_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, int32)); -#define kerepType_register(TYPE, ID_VAR_NAME, FREE_MEMBERS_FUNC)\ - __kerepType_register(#ID_VAR_NAME, sizeof(TYPE), FREE_MEMBERS_FUNC);\ +#define kerepType_register(TYPE, ID_VAR_NAME, FREE_MEMBERS_FUNC, TO_STRING_FUNC)\ + __kerepType_register(#ID_VAR_NAME, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\ ID_VAR_NAME=kerepTypeId_last; void kerepTypeDescriptors_beginInit(); void kerepTypeDescriptors_endInit(); -kerepTypeDescriptor kerepTypeDescriptor_get(kerepTypeId id); +/// @param id id of registered type +/// @return Maybe +Maybe kerepTypeDescriptor_get(kerepTypeId id); kerepTypeId_declare(kerepTypeId_Null); @@ -59,6 +63,9 @@ kerepTypeId_declare(kerepTypeId_UInt32Ptr); kerepTypeId_declare(kerepTypeId_Int64Ptr); kerepTypeId_declare(kerepTypeId_UInt64Ptr); +kerepTypeId_declare(kerepTypeId_kerepTypeDescriptor); +kerepTypeId_declare(kerepTypeId_kerepTypeDescriptorPtr); + #if __cplusplus } #endif \ No newline at end of file diff --git a/src/base/unitype.c b/src/base/unitype.c index 8a555c5..2167a9c 100644 --- a/src/base/unitype.c +++ b/src/base/unitype.c @@ -5,8 +5,8 @@ kerepTypeId_define(kerepTypeId_UnitypePtr); void Unitype_free(Unitype u){ kerepTypeDescriptor type=kerepTypeDescriptor_get(u.typeId); - if(type.free_members) - type.free_members(u.VoidPtr); + if(type.freeMembers) + type.freeMembers(u.VoidPtr); if(u.allocatedInHeap) free(u.VoidPtr); } diff --git a/src/base/unitype.h b/src/base/unitype.h index 146c5d1..5dfa097 100644 --- a/src/base/unitype.h +++ b/src/base/unitype.h @@ -22,20 +22,20 @@ kerepTypeId_declare(kerepTypeId_Unitype); kerepTypeId_declare(kerepTypeId_UnitypePtr); -#define Uni(TYPE, VAL) (Unitype){\ +#define __UniDef(TYPE, VAL) (Unitype){\ .TYPE=VAL, .typeId=kerepTypeId_##TYPE, .allocatedInHeap=false} -#define UniInt64(VAL) Uni(Int64, VAL) -#define UniUInt64(VAL) Uni(UInt64, VAL) -#define UniFloat64(VAL) Uni(Float64, VAL) -#define UniBool(VAL) Uni(Bool, VAL) +#define UniInt64(VAL) __UniDef(Int64, VAL) +#define UniUInt64(VAL) __UniDef(UInt64, VAL) +#define UniFloat64(VAL) __UniDef(Float64, VAL) +#define UniBool(VAL) __UniDef(Bool, VAL) -#define UniPtrStack(ID_VAR_NAME, VAL) (Unitype){\ +#define UniStack(ID_VAR_NAME, VAL) (Unitype){\ .VoidPtr=VAL, .typeId=ID_VAR_NAME, .allocatedInHeap=false} -#define UniPtrHeap(ID_VAR_NAME, VAL) (Unitype){\ +#define UniHeap(ID_VAR_NAME, VAL) (Unitype){\ .VoidPtr=VAL, .typeId=ID_VAR_NAME, .allocatedInHeap=true} -#define UniNull UniPtrStack(kerepTypeId_Null, NULL) +#define UniNull UniStack(kerepTypeId_Null, NULL) #define UniTrue UniBool(true) #define UniFalse UniBool(false) diff --git a/tests/test_marshalling.c b/tests/test_marshalling.c index 21c5721..74e2129 100644 --- a/tests/test_marshalling.c +++ b/tests/test_marshalling.c @@ -4,7 +4,7 @@ EXPORT void CALL test_marshalling(char* text, KVPair** kptr){ KVPair* k=malloc(sizeof(KVPair)); k->key="message"; char* tc=cptr_copy(text); - Unitype u=UniPtrHeap(kerepTypeId_CharPtr,tc); + Unitype u=UniHeap(kerepTypeId_CharPtr,tc); k->value=u; *kptr=k; } diff --git a/tests/test_searchtree.c b/tests/test_searchtree.c index 6309e85..c67e8fd 100644 --- a/tests/test_searchtree.c +++ b/tests/test_searchtree.c @@ -44,11 +44,11 @@ void test_searchtree(){ printuni(u); ST_push(node,"channel_id", u); printf(" -> channel_id\n "); - u=UniPtrHeap(kerepTypeId_CharPtr, cptr_copy("32.2004")); + u=UniHeap(kerepTypeId_CharPtr, cptr_copy("32.2004")); printuni(u); ST_push(node,"message_id", u); printf(" -> message_id\n "); - u=UniPtrStack(kerepTypeId_CharPtr,"some text UwU"); + u=UniStack(kerepTypeId_CharPtr,"some text UwU"); printuni(u); ST_push(node,"text", u); printf(" -> text\n"); From 07cd352f653fd5697f52da76afa1a4a6a99b73e4 Mon Sep 17 00:00:00 2001 From: timerix Date: Tue, 6 Sep 2022 15:54:44 +0600 Subject: [PATCH 23/59] kprint.c --- src/base/kprint/kprint.c | 112 ++++++++++++++++++++++++++++---- src/base/kprint/kprint_format.h | 3 - 2 files changed, 101 insertions(+), 14 deletions(-) diff --git a/src/base/kprint/kprint.c b/src/base/kprint/kprint.c index 1323c3c..2fb60a8 100644 --- a/src/base/kprint/kprint.c +++ b/src/base/kprint/kprint.c @@ -1,22 +1,112 @@ #include "../base.h" +#include "../../String/StringBuilder.h" +Maybe __next_toString(uint16 size, va_list args, StringBuilder* strb){ + kprint_format format=va_arg(args, kprint_format); + if(!format.dataFmtSet) + safethrow("format is not set", StringBuilder_free(strb)); + // detecting type + if(!format.typeId) switch(format.dataFmt){ + case kprint_fmtInt: + case kprint_fmtHex: + case kprint_fmtBin: + switch(size){ + case 1: format.typeId=kerepTypeId_Int8; break; + case 2: format.typeId=kerepTypeId_Int16; break; + case 4: format.typeId=kerepTypeId_Int32; break; + case 8: format.typeId=kerepTypeId_Int64; break; + default: safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); + } + break; + case kprint_fmtUInt: + switch(size){ + case 1: format.typeId=kerepTypeId_UInt8; break; + case 2: format.typeId=kerepTypeId_UInt16; break; + case 4: format.typeId=kerepTypeId_UInt32; break; + case 8: format.typeId=kerepTypeId_UInt64; break; + default: safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); + } + break; + case kprint_fmtFloat: + switch(size){ + case 4: format.typeId=kerepTypeId_Float32; break; + case 8: format.typeId=kerepTypeId_Float64; break; + default: safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); + } + break; + case kprint_fmtChar: + if(size!=1) + safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); + format.typeId=kerepTypeId_Char; + break; + case kprint_fmtString: + switch(size){ + case sizeof(char*): format.typeId=kerepTypeId_CharPtr; break; + case sizeof(string): format.typeId=kerepTypeId_string; break; + default: safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); + } + break; + default: + safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); + } + try(kerepTypeDescriptor_get(format.typeId),mtd,StringBuilder_free(strb)); + kerepTypeDescriptor typeDesc=*(kerepTypeDescriptor*)mtd.value.VoidPtr; + if(size Date: Tue, 6 Sep 2022 20:06:08 +0600 Subject: [PATCH 24/59] a lot of changes --- src/DtsodParser/DtsodV24_deserialize.c | 6 +- src/base/base.h | 4 +- src/base/cptr.h | 2 +- src/base/errors.h | 2 +- src/base/kprint/README.md | 35 ++++- src/base/kprint/data_fmt.txt | 13 -- src/base/kprint/kprint.c | 151 +++++++++------------ src/base/kprint/kprint.h | 3 + src/base/kprint/kprint_colors.h | 72 +++++----- src/base/kprint/kprint_format.h | 68 ++++------ src/base/kprint/kprint_format.md | 72 ++++++++++ src/base/optime.h | 2 +- src/base/type_system/README.md | 0 src/base/type_system/kerepTypeDescriptor.h | 22 +++ src/base/type_system/kerepTypeId.h | 17 +++ src/base/{ => type_system}/types.c | 6 +- src/base/{ => type_system}/types.h | 22 +-- src/base/{ => type_system}/unitype.c | 8 +- src/base/{ => type_system}/unitype.h | 2 +- tests/test_hashtable.c | 2 +- tests/test_safethrow.c | 2 +- 21 files changed, 294 insertions(+), 217 deletions(-) delete mode 100644 src/base/kprint/data_fmt.txt create mode 100644 src/base/kprint/kprint_format.md create mode 100644 src/base/type_system/README.md create mode 100644 src/base/type_system/kerepTypeDescriptor.h create mode 100644 src/base/type_system/kerepTypeId.h rename src/base/{ => type_system}/types.c (96%) rename src/base/{ => type_system}/types.h (75%) rename src/base/{ => type_system}/unitype.c (81%) rename src/base/{ => type_system}/unitype.h (98%) diff --git a/src/DtsodParser/DtsodV24_deserialize.c b/src/DtsodParser/DtsodV24_deserialize.c index 8acc293..ecf1322 100644 --- a/src/DtsodParser/DtsodV24_deserialize.c +++ b/src/DtsodParser/DtsodV24_deserialize.c @@ -182,7 +182,7 @@ Maybe __ParseValue(DeserializeSharedData* shared, string str){ // Float64 case 'f': { char* _c=string_extract(str); - Unitype rez=Uni(Float64,strtod(_c,NULL)); + Unitype rez=UniFloat64(strtod(_c,NULL)); free(_c); return SUCCESS(rez); } @@ -199,7 +199,7 @@ Maybe __ParseValue(DeserializeSharedData* shared, string str){ safethrow(err,free(_c)); } free(_c); - return SUCCESS(Uni(UInt64,lu)); + return SUCCESS(UniUInt64(lu)); } // Int64 case '0': case '1': case '2': case '3': case '4': @@ -215,7 +215,7 @@ Maybe __ParseValue(DeserializeSharedData* shared, string str){ safethrow(err,free(_c)); } free(_c); - return SUCCESS(Uni(Int64,li)); + return SUCCESS(UniInt64(li)); } // wrong type default: diff --git a/src/base/base.h b/src/base/base.h index c728a94..fce8588 100644 --- a/src/base/base.h +++ b/src/base/base.h @@ -8,8 +8,8 @@ extern "C" { #include "errors.h" #include "cptr.h" #include "optime.h" -#include "types.h" -#include "unitype.h" +#include "type_system/types.h" +#include "type_system/unitype.h" #include "init.h" #include "kprint/kprint.h" diff --git a/src/base/cptr.h b/src/base/cptr.h index b750842..6372931 100644 --- a/src/base/cptr.h +++ b/src/base/cptr.h @@ -4,7 +4,7 @@ extern "C" { #endif -#include "types.h" +#include "std.h" // returns length of char buffer (without \0) uint32 cptr_length(char* str); diff --git a/src/base/errors.h b/src/base/errors.h index 06f0017..324695e 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -5,7 +5,7 @@ extern "C" { #endif #include "std.h" -#include "unitype.h" +#include "type_system/unitype.h" typedef enum ErrorId { SUCCESS, // not an error diff --git a/src/base/kprint/README.md b/src/base/kprint/README.md index e375f83..c02cdac 100644 --- a/src/base/kprint/README.md +++ b/src/base/kprint/README.md @@ -1,2 +1,33 @@ -## kprint -I don't really like printf function, so i made safer and more convinient replacement. \ No newline at end of file +# kprint +I don't really like printf function (and its variants), so i made safer and more convinient replacement. + +| function | returns | arguments | +|----------|---------|-----------| +| kprint | void/throw | kprint_format, void*, kprint_format, void*... | +| ksprint | Maybe | kprint_format, void*, kprint_format, void*... | +| kfprint | Maybe | FILE*, kprint_format, void*, kprint_format, void*... | + +## how to use it: ++ **format construction:** + ``` + kprint_format fmt= kprint_fgColor | kprint_fbgColor | kprint_fdataFmt | flags | kerepTypeId; + ``` + [more about `kprint_format`](kprint_format.md) + + fgColor and bgColor can be set to change console output color + + you should set dataFormat for `int`/`uint`/`float`/`char*` arguments and kerepTypeId for other types + + flags can be set to modify TypeDescriptor.toString() behavior + + don't forget to set TypeDescriptor.toString when registering type, or kprint will crash + ++ **using base type arguments:** + you can just put them into a function + ``` + kprint(kprint_fmtHex | kprint_fmtUppercase | kprint_fmtWithPrefix, 255); + ``` + output: 0xFF ++ **using other registered types:** + should be sent as pointers + ``` + Maybe m=MaybeNull; + kprint(kprint_fgBlue | kprint_fmtString, "Maybe: ", kprint_fgGreen | kerepTypeId_MaybePtr, &m); + ``` + output: Maybe: {value={0, kerepTypeId_Null}} \ No newline at end of file diff --git a/src/base/kprint/data_fmt.txt b/src/base/kprint/data_fmt.txt deleted file mode 100644 index 31f434f..0000000 --- a/src/base/kprint/data_fmt.txt +++ /dev/null @@ -1,13 +0,0 @@ -[data types] -int 0x2000 00100000 00000000 -uint + withPostfix + uppercase 0x2001 00100000 00000001 -hex + withPrefix + uppercase 0x2002 00100000 00000010 -bin + withPrefix 0x2003 00100000 00000011 -float + withPostfix + uppercase 0x3000 00110000 00000000 -char 0x3001 00110000 00000001 -string 0x3002 00110000 00000010 -??? 0x3003 00110000 00000011 - -[optional flags] -withPrefix = withPostfix 0x0004 00000000 00000100 -upperase 0x0008 00000000 00001000 \ No newline at end of file diff --git a/src/base/kprint/kprint.c b/src/base/kprint/kprint.c index 2fb60a8..fc89c72 100644 --- a/src/base/kprint/kprint.c +++ b/src/base/kprint/kprint.c @@ -1,112 +1,87 @@ -#include "../base.h" +/* #include "../base.h" #include "../../String/StringBuilder.h" -Maybe __next_toString(uint16 size, va_list args, StringBuilder* strb){ - kprint_format format=va_arg(args, kprint_format); - if(!format.dataFmtSet) - safethrow("format is not set", StringBuilder_free(strb)); +Maybe __next_toString(kprint_format format, void* object){ // detecting type - if(!format.typeId) switch(format.dataFmt){ + if(!format.typeId) switch((kprint_dataFormat)((uint32)0 | format.dataFmt)){ case kprint_fmtInt: case kprint_fmtHex: - case kprint_fmtBin: - switch(size){ - case 1: format.typeId=kerepTypeId_Int8; break; - case 2: format.typeId=kerepTypeId_Int16; break; - case 4: format.typeId=kerepTypeId_Int32; break; - case 8: format.typeId=kerepTypeId_Int64; break; - default: safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); - } - break; + case kprint_fmtBin: + format.typeId=kerepTypeId_Int64; break; case kprint_fmtUInt: - switch(size){ - case 1: format.typeId=kerepTypeId_UInt8; break; - case 2: format.typeId=kerepTypeId_UInt16; break; - case 4: format.typeId=kerepTypeId_UInt32; break; - case 8: format.typeId=kerepTypeId_UInt64; break; - default: safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); - } - break; + format.typeId=kerepTypeId_UInt64; break; case kprint_fmtFloat: - switch(size){ - case 4: format.typeId=kerepTypeId_Float32; break; - case 8: format.typeId=kerepTypeId_Float64; break; - default: safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); - } - break; + format.typeId=kerepTypeId_Float64; break; case kprint_fmtChar: - if(size!=1) - safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); - format.typeId=kerepTypeId_Char; - break; + format.typeId=kerepTypeId_Char; break; case kprint_fmtString: - switch(size){ - case sizeof(char*): format.typeId=kerepTypeId_CharPtr; break; - case sizeof(string): format.typeId=kerepTypeId_string; break; - default: safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); - } - break; + format.typeId=kerepTypeId_CharPtr; break; default: - safethrow("typeId is not set, can't autodetect type", StringBuilder_free(strb)); + safethrow("typeId is not set, can't autodetect type",;); } - try(kerepTypeDescriptor_get(format.typeId),mtd,StringBuilder_free(strb)); + try(kerepTypeDescriptor_get(format.typeId),mtd,;); kerepTypeDescriptor typeDesc=*(kerepTypeDescriptor*)mtd.value.VoidPtr; - if(size + Maybe (*toString)(void*, int32); // NULL or function which generates string representaion of object +} kerepTypeDescriptor; + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/base/type_system/kerepTypeId.h b/src/base/type_system/kerepTypeId.h new file mode 100644 index 0000000..6f4f4af --- /dev/null +++ b/src/base/type_system/kerepTypeId.h @@ -0,0 +1,17 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../std.h" +typedef uint16 kerepTypeId; + +#define kerepTypeId_declare(ID_VAR_NAME)\ + extern kerepTypeId ID_VAR_NAME +#define kerepTypeId_define(ID_VAR_NAME)\ + kerepTypeId ID_VAR_NAME=-1 + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/base/types.c b/src/base/type_system/types.c similarity index 96% rename from src/base/types.c rename to src/base/type_system/types.c index 9bf5ce1..82d2d6d 100644 --- a/src/base/types.c +++ b/src/base/type_system/types.c @@ -1,5 +1,5 @@ -#include "../Autoarr/Autoarr.h" -#include "unitype.h" +#include "../../Autoarr/Autoarr.h" +#include "types.h" Autoarr_declare(kerepTypeDescriptor) Autoarr_define(kerepTypeDescriptor) @@ -59,7 +59,7 @@ void kerepTypeDescriptors_endInit(){ printf("\e[92minitialized %u type descriptors\n", kerepTypeId_last); } -void __kerepType_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, int32)){ +void __kerepType_register(char* name, int16 size, void (*freeMembers)(void*), Maybe (*toString)(void*, int32)){ kerepTypeDescriptor typeDesc={ .name=name, .size=size, diff --git a/src/base/types.h b/src/base/type_system/types.h similarity index 75% rename from src/base/types.h rename to src/base/type_system/types.h index 96ff861..8cbaa6b 100644 --- a/src/base/types.h +++ b/src/base/type_system/types.h @@ -4,26 +4,13 @@ extern "C" { #endif -#include "std.h" +#include "../std.h" +#include "kerepTypeId.h" +#include "kerepTypeDescriptor.h" -typedef uint16 kerepTypeId; - -typedef struct kerepTypeDescriptor{ - char* name; - kerepTypeId id; - uint16 size; - void (*freeMembers)(void*); // NULL or function which frees all struct members - ///@return Maybe - Maybe (*toString)(void*, int32); // NULL or function which generates string representaion of object -} kerepTypeDescriptor; - -#define kerepTypeId_declare(ID_VAR_NAME)\ - extern kerepTypeId ID_VAR_NAME -#define kerepTypeId_define(ID_VAR_NAME)\ - kerepTypeId ID_VAR_NAME=-1 extern kerepTypeId kerepTypeId_last; -void __kerepType_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, int32)); +void __kerepType_register(char* name, int16 size, void (*freeMembers)(void*), Maybe (*toString)(void*, int32)); #define kerepType_register(TYPE, ID_VAR_NAME, FREE_MEMBERS_FUNC, TO_STRING_FUNC)\ __kerepType_register(#ID_VAR_NAME, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\ @@ -35,6 +22,7 @@ void kerepTypeDescriptors_endInit(); /// @return Maybe Maybe kerepTypeDescriptor_get(kerepTypeId id); + kerepTypeId_declare(kerepTypeId_Null); kerepTypeId_declare(kerepTypeId_Char); diff --git a/src/base/unitype.c b/src/base/type_system/unitype.c similarity index 81% rename from src/base/unitype.c rename to src/base/type_system/unitype.c index 2167a9c..8b44641 100644 --- a/src/base/unitype.c +++ b/src/base/type_system/unitype.c @@ -1,10 +1,11 @@ -#include "base.h" +#include "../base.h" kerepTypeId_define(kerepTypeId_Unitype); kerepTypeId_define(kerepTypeId_UnitypePtr); void Unitype_free(Unitype u){ - kerepTypeDescriptor type=kerepTypeDescriptor_get(u.typeId); + tryLast(kerepTypeDescriptor_get(u.typeId), mType); + kerepTypeDescriptor type=*(kerepTypeDescriptor*)mType.value.VoidPtr; if(type.freeMembers) type.freeMembers(u.VoidPtr); if(u.allocatedInHeap) @@ -15,7 +16,8 @@ void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); } #define BUFSIZE 64 char* sprintuni(Unitype v){ char* buf=malloc(BUFSIZE); - kerepTypeDescriptor type=kerepTypeDescriptor_get(v.typeId); + tryLast(kerepTypeDescriptor_get(v.typeId), mType); + kerepTypeDescriptor type=*(kerepTypeDescriptor*)mType.value.VoidPtr; if(v.typeId==kerepTypeId_Null) sprintf_s(buf, BUFSIZE, "{Null}"); else if(v.typeId==kerepTypeId_Float64) diff --git a/src/base/unitype.h b/src/base/type_system/unitype.h similarity index 98% rename from src/base/unitype.h rename to src/base/type_system/unitype.h index 5dfa097..9f28bf7 100644 --- a/src/base/unitype.h +++ b/src/base/type_system/unitype.h @@ -4,7 +4,7 @@ extern "C" { #endif -#include "types.h" +#include "kerepTypeId.h" typedef struct Unitype{ union { diff --git a/tests/test_hashtable.c b/tests/test_hashtable.c index 8ff1d53..e68d02a 100644 --- a/tests/test_hashtable.c +++ b/tests/test_hashtable.c @@ -47,7 +47,7 @@ char* genkey(uint32 i){ void fill(Hashtable* ht){ for(uint32 i=0;i<100000;i++) - Hashtable_add(ht,genkey(i),Uni(UInt64,i)); + Hashtable_add(ht,genkey(i),UniUInt64(i)); } Unitype gett(Hashtable* ht){ diff --git a/tests/test_safethrow.c b/tests/test_safethrow.c index 4ccd4dc..f37812d 100644 --- a/tests/test_safethrow.c +++ b/tests/test_safethrow.c @@ -1,7 +1,7 @@ #include "tests.h" Maybe dont_throw(){ - return SUCCESS(Uni(UInt64, 9/2)); + return SUCCESS(UniUInt64(9/2)); } Maybe throw_error(){ From 67e1328ee7d28430707425145d0fb6f720c04864 Mon Sep 17 00:00:00 2001 From: timerix Date: Tue, 6 Sep 2022 21:25:03 +0600 Subject: [PATCH 25/59] kerepType -> kt --- src/Autoarr/Autoarr.c | 52 ++++----- src/Autoarr/Autoarr.h | 52 ++++----- src/DtsodParser/DtsodV24_deserialize.c | 18 +-- src/DtsodParser/DtsodV24_serialize.c | 18 +-- src/Hashtable/Hashtable.c | 6 +- src/Hashtable/Hashtable.h | 4 +- src/Hashtable/KeyValuePair.c | 8 +- src/Hashtable/KeyValuePair.h | 8 +- src/SearchTree/SearchTree.c | 6 +- src/SearchTree/SearchTree.h | 4 +- src/String/StringBuilder.c | 8 +- src/String/StringBuilder.h | 8 +- src/String/string.c | 4 +- src/String/string.h | 4 +- src/base/base.h | 4 +- src/base/init.c | 103 ------------------ src/base/init.h | 12 -- src/base/kprint/README.md | 8 +- src/base/kprint/kprint.c | 22 ++-- src/base/kprint/kprint_format.h | 4 +- src/base/kprint/kprint_format.md | 2 +- src/base/type_system/init.c | 103 ++++++++++++++++++ src/base/type_system/init.h | 12 ++ src/base/type_system/kerepTypeId.h | 17 --- .../{kerepTypeDescriptor.h => ktDescriptor.h} | 8 +- src/base/type_system/ktId.h | 17 +++ src/base/type_system/kt_functions.c | 75 +++++++++++++ src/base/type_system/kt_functions.h | 59 ++++++++++ src/base/type_system/type_system.h | 4 + src/base/type_system/types.c | 76 ------------- src/base/type_system/types.h | 59 ---------- src/base/type_system/unitype.c | 22 ++-- src/base/type_system/unitype.h | 12 +- tests/main.cpp | 6 +- tests/test_dtsod.c | 2 +- tests/test_marshalling.c | 2 +- tests/test_searchtree.c | 4 +- 37 files changed, 417 insertions(+), 416 deletions(-) delete mode 100644 src/base/init.c delete mode 100644 src/base/init.h create mode 100644 src/base/type_system/init.c create mode 100644 src/base/type_system/init.h delete mode 100644 src/base/type_system/kerepTypeId.h rename src/base/type_system/{kerepTypeDescriptor.h => ktDescriptor.h} (77%) create mode 100644 src/base/type_system/ktId.h create mode 100644 src/base/type_system/kt_functions.c create mode 100644 src/base/type_system/kt_functions.h create mode 100644 src/base/type_system/type_system.h delete mode 100644 src/base/type_system/types.c delete mode 100644 src/base/type_system/types.h diff --git a/src/Autoarr/Autoarr.c b/src/Autoarr/Autoarr.c index abff22b..a87502c 100644 --- a/src/Autoarr/Autoarr.c +++ b/src/Autoarr/Autoarr.c @@ -13,35 +13,35 @@ Autoarr_define(int32) Autoarr_define(uint64) Autoarr_define(int64) -kerepTypeId_define(kerepTypeId_AutoarrChar); -kerepTypeId_define(kerepTypeId_AutoarrBool); -kerepTypeId_define(kerepTypeId_AutoarrFloat32); -kerepTypeId_define(kerepTypeId_AutoarrFloat64); -kerepTypeId_define(kerepTypeId_AutoarrInt8); -kerepTypeId_define(kerepTypeId_AutoarrUInt8); -kerepTypeId_define(kerepTypeId_AutoarrInt16); -kerepTypeId_define(kerepTypeId_AutoarrUInt16); -kerepTypeId_define(kerepTypeId_AutoarrInt32); -kerepTypeId_define(kerepTypeId_AutoarrUInt32); -kerepTypeId_define(kerepTypeId_AutoarrInt64); -kerepTypeId_define(kerepTypeId_AutoarrUInt64); +ktId_define(ktId_AutoarrChar); +ktId_define(ktId_AutoarrBool); +ktId_define(ktId_AutoarrFloat32); +ktId_define(ktId_AutoarrFloat64); +ktId_define(ktId_AutoarrInt8); +ktId_define(ktId_AutoarrUInt8); +ktId_define(ktId_AutoarrInt16); +ktId_define(ktId_AutoarrUInt16); +ktId_define(ktId_AutoarrInt32); +ktId_define(ktId_AutoarrUInt32); +ktId_define(ktId_AutoarrInt64); +ktId_define(ktId_AutoarrUInt64); -kerepTypeId_define(kerepTypeId_AutoarrCharPtr); -kerepTypeId_define(kerepTypeId_AutoarrBoolPtr); -kerepTypeId_define(kerepTypeId_AutoarrFloat32Ptr); -kerepTypeId_define(kerepTypeId_AutoarrFloat64Ptr); -kerepTypeId_define(kerepTypeId_AutoarrInt8Ptr); -kerepTypeId_define(kerepTypeId_AutoarrUInt8Ptr); -kerepTypeId_define(kerepTypeId_AutoarrInt16Ptr); -kerepTypeId_define(kerepTypeId_AutoarrUInt16Ptr); -kerepTypeId_define(kerepTypeId_AutoarrInt32Ptr); -kerepTypeId_define(kerepTypeId_AutoarrUInt32Ptr); -kerepTypeId_define(kerepTypeId_AutoarrInt64Ptr); -kerepTypeId_define(kerepTypeId_AutoarrUInt64Ptr); +ktId_define(ktId_AutoarrCharPtr); +ktId_define(ktId_AutoarrBoolPtr); +ktId_define(ktId_AutoarrFloat32Ptr); +ktId_define(ktId_AutoarrFloat64Ptr); +ktId_define(ktId_AutoarrInt8Ptr); +ktId_define(ktId_AutoarrUInt8Ptr); +ktId_define(ktId_AutoarrInt16Ptr); +ktId_define(ktId_AutoarrUInt16Ptr); +ktId_define(ktId_AutoarrInt32Ptr); +ktId_define(ktId_AutoarrUInt32Ptr); +ktId_define(ktId_AutoarrInt64Ptr); +ktId_define(ktId_AutoarrUInt64Ptr); Autoarr_define(Unitype) -kerepTypeId_define(kerepTypeId_AutoarrUnitype); -kerepTypeId_define(kerepTypeId_AutoarrUnitypePtr); +ktId_define(ktId_AutoarrUnitype); +ktId_define(ktId_AutoarrUnitypePtr); // right func to clear array of unitype values void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr){ diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h index d96fcf6..be32c5c 100644 --- a/src/Autoarr/Autoarr.h +++ b/src/Autoarr/Autoarr.h @@ -20,35 +20,35 @@ Autoarr_declare(uint32) Autoarr_declare(int64) Autoarr_declare(uint64) -kerepTypeId_declare(kerepTypeId_AutoarrChar); -kerepTypeId_declare(kerepTypeId_AutoarrBool); -kerepTypeId_declare(kerepTypeId_AutoarrFloat32); -kerepTypeId_declare(kerepTypeId_AutoarrFloat64); -kerepTypeId_declare(kerepTypeId_AutoarrInt8); -kerepTypeId_declare(kerepTypeId_AutoarrUInt8); -kerepTypeId_declare(kerepTypeId_AutoarrInt16); -kerepTypeId_declare(kerepTypeId_AutoarrUInt16); -kerepTypeId_declare(kerepTypeId_AutoarrInt32); -kerepTypeId_declare(kerepTypeId_AutoarrUInt32); -kerepTypeId_declare(kerepTypeId_AutoarrInt64); -kerepTypeId_declare(kerepTypeId_AutoarrUInt64); +ktId_declare(ktId_AutoarrChar); +ktId_declare(ktId_AutoarrBool); +ktId_declare(ktId_AutoarrFloat32); +ktId_declare(ktId_AutoarrFloat64); +ktId_declare(ktId_AutoarrInt8); +ktId_declare(ktId_AutoarrUInt8); +ktId_declare(ktId_AutoarrInt16); +ktId_declare(ktId_AutoarrUInt16); +ktId_declare(ktId_AutoarrInt32); +ktId_declare(ktId_AutoarrUInt32); +ktId_declare(ktId_AutoarrInt64); +ktId_declare(ktId_AutoarrUInt64); -kerepTypeId_declare(kerepTypeId_AutoarrCharPtr); -kerepTypeId_declare(kerepTypeId_AutoarrBoolPtr); -kerepTypeId_declare(kerepTypeId_AutoarrFloat32Ptr); -kerepTypeId_declare(kerepTypeId_AutoarrFloat64Ptr); -kerepTypeId_declare(kerepTypeId_AutoarrInt8Ptr); -kerepTypeId_declare(kerepTypeId_AutoarrUInt8Ptr); -kerepTypeId_declare(kerepTypeId_AutoarrInt16Ptr); -kerepTypeId_declare(kerepTypeId_AutoarrUInt16Ptr); -kerepTypeId_declare(kerepTypeId_AutoarrInt32Ptr); -kerepTypeId_declare(kerepTypeId_AutoarrUInt32Ptr); -kerepTypeId_declare(kerepTypeId_AutoarrInt64Ptr); -kerepTypeId_declare(kerepTypeId_AutoarrUInt64Ptr); +ktId_declare(ktId_AutoarrCharPtr); +ktId_declare(ktId_AutoarrBoolPtr); +ktId_declare(ktId_AutoarrFloat32Ptr); +ktId_declare(ktId_AutoarrFloat64Ptr); +ktId_declare(ktId_AutoarrInt8Ptr); +ktId_declare(ktId_AutoarrUInt8Ptr); +ktId_declare(ktId_AutoarrInt16Ptr); +ktId_declare(ktId_AutoarrUInt16Ptr); +ktId_declare(ktId_AutoarrInt32Ptr); +ktId_declare(ktId_AutoarrUInt32Ptr); +ktId_declare(ktId_AutoarrInt64Ptr); +ktId_declare(ktId_AutoarrUInt64Ptr); Autoarr_declare(Unitype) -kerepTypeId_declare(kerepTypeId_AutoarrUnitype); -kerepTypeId_declare(kerepTypeId_AutoarrUnitypePtr); +ktId_declare(ktId_AutoarrUnitype); +ktId_declare(ktId_AutoarrUnitypePtr); // this function is injected in kerep_init() void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr); diff --git a/src/DtsodParser/DtsodV24_deserialize.c b/src/DtsodParser/DtsodV24_deserialize.c index ecf1322..20107ef 100644 --- a/src/DtsodParser/DtsodV24_deserialize.c +++ b/src/DtsodParser/DtsodV24_deserialize.c @@ -98,9 +98,9 @@ Maybe __ReadName(DeserializeSharedData* shared){ case '}': if(!calledRecursively || nameStr.length!=0) safethrow_wrongchar(c,;); - return SUCCESS(UniHeap(kerepTypeId_CharPtr,NULL)); + return SUCCESS(UniHeap(ktId_CharPtr,NULL)); case ':': - return SUCCESS(UniHeap(kerepTypeId_CharPtr,string_extract(nameStr))); + return SUCCESS(UniHeap(ktId_CharPtr,string_extract(nameStr))); case '$': if(nameStr.length!=0) safethrow_wrongchar(c,;); @@ -113,7 +113,7 @@ Maybe __ReadName(DeserializeSharedData* shared){ } if(nameStr.length>0) safethrow(ERR_ENDOFSTR,;); - return SUCCESS(UniHeap(kerepTypeId_CharPtr,NULL)); + return SUCCESS(UniHeap(ktId_CharPtr,NULL)); } #define ReadName() __ReadName(shared) @@ -137,7 +137,7 @@ Maybe __ReadString(DeserializeSharedData* shared){ } else { char* str=StringBuilder_build(b).ptr; - return SUCCESS(UniHeap(kerepTypeId_CharPtr,str)); + return SUCCESS(UniHeap(ktId_CharPtr,str)); } } else { @@ -157,13 +157,13 @@ Maybe __ReadList(DeserializeSharedData* shared){ try(ReadValue((&readingList)), val, Autoarr_free(list, true)) Autoarr_add(list,val.value); if (!readingList){ - if(val.value.typeId==kerepTypeId_Null) + if(val.value.typeId==ktId_Null) Autoarr_pop(list); break; } } - return SUCCESS(UniHeap(kerepTypeId_AutoarrUnitypePtr,list)); + return SUCCESS(UniHeap(ktId_AutoarrUnitypePtr,list)); }; #define ReadList() __ReadList(shared) @@ -275,7 +275,7 @@ Maybe __ReadValue(DeserializeSharedData* shared, bool* readingList){ case ';': case ',': if(valueStr.length!=0){ - if(value.typeId!=kerepTypeId_Null) + if(value.typeId!=ktId_Null) safethrow_wrongchar(c,Unitype_free(value)); try(ParseValue(valueStr),maybeParsed,;) value=maybeParsed.value; @@ -321,7 +321,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) { } else{ list=Autoarr_create(Unitype,ARR_BC,ARR_BL); - Hashtable_add(dict,nameCPtr,UniHeap(kerepTypeId_AutoarrUnitypePtr,list)); + Hashtable_add(dict,nameCPtr,UniHeap(ktId_AutoarrUnitypePtr,list)); } Autoarr_add(list,val.value); } @@ -331,7 +331,7 @@ Maybe __deserialize(char** _text, bool _calledRecursively) { END: *_text=text; - return SUCCESS(UniHeap(kerepTypeId_HashtablePtr,dict)); + return SUCCESS(UniHeap(ktId_HashtablePtr,dict)); } Maybe DtsodV24_deserialize(char* _text) { diff --git a/src/DtsodParser/DtsodV24_serialize.c b/src/DtsodParser/DtsodV24_serialize.c index 3ab0346..cf48d34 100644 --- a/src/DtsodParser/DtsodV24_serialize.c +++ b/src/DtsodParser/DtsodV24_serialize.c @@ -23,18 +23,18 @@ 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==kerepTypeId_Int64){ + if(u.typeId==ktId_Int64){ StringBuilder_append_int64(b,u.Int64); } - else if(u.typeId==kerepTypeId_UInt64){ + else if(u.typeId==ktId_UInt64){ StringBuilder_append_uint64(b,u.UInt64); addc('u'); } - else if(u.typeId==kerepTypeId_Float64){ + else if(u.typeId==ktId_Float64){ StringBuilder_append_float64(b,u.Float64); addc('f'); } - else if(u.typeId==kerepTypeId_CharPtr){ + else if(u.typeId==ktId_CharPtr){ addc('"'); char c; while((c=*(char*)(u.VoidPtr++))){ @@ -43,13 +43,13 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){ } addc('"'); } - else if(u.typeId==kerepTypeId_Bool){ + else if(u.typeId==ktId_Bool){ StringBuilder_append_cptr(b, u.Bool ? "true" : "false"); } - else if(u.typeId==kerepTypeId_Null){ + else if(u.typeId==ktId_Null){ safethrow("Null isn't supported in DtsodV24",;); } - else if(u.typeId==kerepTypeId_AutoarrUnitypePtr){ + else if(u.typeId==ktId_AutoarrUnitypePtr){ if(Autoarr_length(((Autoarr_Unitype*)(u.VoidPtr)))){ addc('\n'); AppendTabs(); @@ -72,7 +72,7 @@ Maybe __AppendValue(SerializeSharedData* shared, Unitype u){ addc(']'); } } - else if(u.typeId==kerepTypeId_HashtablePtr){ + else if(u.typeId==ktId_HashtablePtr){ // check hashtable is blank bool hashtableNotBlank=false; Hashtable_foreach(((Hashtable*)u.VoidPtr), __, ({ @@ -126,5 +126,5 @@ Maybe DtsodV24_serialize(Hashtable* dtsod){ StringBuilder* sb=StringBuilder_create(); try(__serialize(sb,0,dtsod),__, StringBuilder_free(sb)); char* str=StringBuilder_build(sb).ptr; - return SUCCESS(UniHeap(kerepTypeId_CharPtr, str)); + return SUCCESS(UniHeap(ktId_CharPtr, str)); } diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index 4b1b921..e5eb77d 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -1,7 +1,7 @@ #include "Hashtable.h" -kerepTypeId_define(kerepTypeId_Hashtable); -kerepTypeId_define(kerepTypeId_HashtablePtr); +ktId_define(ktId_Hashtable); +ktId_define(ktId_HashtablePtr); // amount of rows static const uint16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521}; @@ -96,7 +96,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!=kerepTypeId_Null; + return u.typeId!=ktId_Null; } void Hashtable_addOrSet(Hashtable* ht, char* key, Unitype u){ diff --git a/src/Hashtable/Hashtable.h b/src/Hashtable/Hashtable.h index 58f446d..9437ca2 100644 --- a/src/Hashtable/Hashtable.h +++ b/src/Hashtable/Hashtable.h @@ -11,8 +11,8 @@ typedef struct Hashtable{ uint8 hein; // height=HT_HEIGHTS[hein] Autoarr(KVPair)** rows; // Autoarr[height] } Hashtable; -kerepTypeId_declare(kerepTypeId_Hashtable); -kerepTypeId_declare(kerepTypeId_HashtablePtr); +ktId_declare(ktId_Hashtable); +ktId_declare(ktId_HashtablePtr); Hashtable* Hashtable_create(); void Hashtable_free(Hashtable* ht); diff --git a/src/Hashtable/KeyValuePair.c b/src/Hashtable/KeyValuePair.c index 22c3f5e..cb4a2d0 100644 --- a/src/Hashtable/KeyValuePair.c +++ b/src/Hashtable/KeyValuePair.c @@ -1,11 +1,11 @@ #include "KeyValuePair.h" -kerepTypeId_define(kerepTypeId_KVPair); -kerepTypeId_define(kerepTypeId_KVPairPtr); +ktId_define(ktId_KVPair); +ktId_define(ktId_KVPairPtr); Autoarr_define(KVPair) -kerepTypeId_define(kerepTypeId_AutoarrKVPair); -kerepTypeId_define(kerepTypeId_AutoarrKVPairPtr); +ktId_define(ktId_AutoarrKVPair); +ktId_define(ktId_AutoarrKVPairPtr); // proper way to clear a KVP void KVPair_free(KVPair p){ diff --git a/src/Hashtable/KeyValuePair.h b/src/Hashtable/KeyValuePair.h index 832c6c7..666b839 100644 --- a/src/Hashtable/KeyValuePair.h +++ b/src/Hashtable/KeyValuePair.h @@ -11,12 +11,12 @@ typedef struct KVPair{ char* key; Unitype value; } KVPair; -kerepTypeId_declare(kerepTypeId_KVPair); -kerepTypeId_declare(kerepTypeId_KVPairPtr); +ktId_declare(ktId_KVPair); +ktId_declare(ktId_KVPairPtr); Autoarr_declare(KVPair) -kerepTypeId_declare(kerepTypeId_AutoarrKVPair); -kerepTypeId_declare(kerepTypeId_AutoarrKVPairPtr); +ktId_declare(ktId_AutoarrKVPair); +ktId_declare(ktId_AutoarrKVPairPtr); // proper way to clear a KVP void KVPair_free(KVPair p); diff --git a/src/SearchTree/SearchTree.c b/src/SearchTree/SearchTree.c index 23aa853..c174da9 100644 --- a/src/SearchTree/SearchTree.c +++ b/src/SearchTree/SearchTree.c @@ -1,12 +1,12 @@ #include "SearchTree.h" -kerepTypeId_define(kerepTypeId_STNode); -kerepTypeId_define(kerepTypeId_STNodePtr); +ktId_define(ktId_STNode); +ktId_define(ktId_STNodePtr); STNode* STNode_create(){ STNode* node=malloc(sizeof(STNode)); node->branches=NULL; - node->value.typeId=kerepTypeId_Null; + node->value.typeId=ktId_Null; node->value.UInt64=0; return node; } diff --git a/src/SearchTree/SearchTree.h b/src/SearchTree/SearchTree.h index 3d1a8e0..6ae9e74 100644 --- a/src/SearchTree/SearchTree.h +++ b/src/SearchTree/SearchTree.h @@ -11,8 +11,8 @@ typedef struct SearchTreeNode{ struct SearchTreeNode**** branches; // *STNode[8][8][4] Unitype value; } STNode; -kerepTypeId_declare(kerepTypeId_STNode); -kerepTypeId_declare(kerepTypeId_STNodePtr); +ktId_declare(ktId_STNode); +ktId_declare(ktId_STNodePtr); STNode* STNode_create(); void STNode_free(STNode* node); diff --git a/src/String/StringBuilder.c b/src/String/StringBuilder.c index 167e916..461f45d 100644 --- a/src/String/StringBuilder.c +++ b/src/String/StringBuilder.c @@ -1,11 +1,11 @@ #include "StringBuilder.h" Autoarr_define(string) -kerepTypeId_define(kerepTypeId_AutoarrString); -kerepTypeId_define(kerepTypeId_AutoarrStringPtr); +ktId_define(ktId_AutoarrString); +ktId_define(ktId_AutoarrStringPtr); -kerepTypeId_define(kerepTypeId_StringBuilder); -kerepTypeId_define(kerepTypeId_StringBuilderPtr); +ktId_define(ktId_StringBuilder); +ktId_define(ktId_StringBuilderPtr); #define BL_C 32 #define BL_L 1024 diff --git a/src/String/StringBuilder.h b/src/String/StringBuilder.h index 50e63a7..7cb5d91 100644 --- a/src/String/StringBuilder.h +++ b/src/String/StringBuilder.h @@ -8,15 +8,15 @@ extern "C" { #include "string.h" Autoarr_declare(string) -kerepTypeId_declare(kerepTypeId_AutoarrString); -kerepTypeId_declare(kerepTypeId_AutoarrStringPtr); +ktId_declare(ktId_AutoarrString); +ktId_declare(ktId_AutoarrStringPtr); typedef struct StringBuilder{ Autoarr(string)* compl_bufs; Autoarr(int8)* curr_buf; } StringBuilder; -kerepTypeId_declare(kerepTypeId_StringBuilder); -kerepTypeId_declare(kerepTypeId_StringBuilderPtr); +ktId_declare(ktId_StringBuilder); +ktId_declare(ktId_StringBuilderPtr); StringBuilder* StringBuilder_create(void); void StringBuilder_free(StringBuilder* b); diff --git a/src/String/string.c b/src/String/string.c index e7d92a6..cbcb46e 100644 --- a/src/String/string.c +++ b/src/String/string.c @@ -1,7 +1,7 @@ #include "string.h" -kerepTypeId_define(kerepTypeId_string); -kerepTypeId_define(kerepTypeId_stringPtr); +ktId_define(ktId_string); +ktId_define(ktId_stringPtr); // 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 0b9a658..9313dae 100644 --- a/src/String/string.h +++ b/src/String/string.h @@ -12,8 +12,8 @@ typedef struct string{ char* ptr; // char pointer uint32 length; // amount of chars in ptr value } string; -kerepTypeId_declare(kerepTypeId_string); -kerepTypeId_declare(kerepTypeId_stringPtr); +ktId_declare(ktId_string); +ktId_declare(ktId_stringPtr); static const string stringNull={NULL,0}; diff --git a/src/base/base.h b/src/base/base.h index fce8588..9ca2951 100644 --- a/src/base/base.h +++ b/src/base/base.h @@ -8,9 +8,7 @@ extern "C" { #include "errors.h" #include "cptr.h" #include "optime.h" -#include "type_system/types.h" -#include "type_system/unitype.h" -#include "init.h" +#include "type_system/type_system.h" #include "kprint/kprint.h" #if __cplusplus diff --git a/src/base/init.c b/src/base/init.c deleted file mode 100644 index cef81d1..0000000 --- a/src/base/init.c +++ /dev/null @@ -1,103 +0,0 @@ -#include "base.h" -#include "../Autoarr/Autoarr.h" -#include "../SearchTree/SearchTree.h" -#include "../Hashtable/Hashtable.h" -#include "../String/StringBuilder.h" - -void kerepTypeDescriptors_initKerepTypes(){ - // null - kerepType_register(NULL, kerepTypeId_Null, NULL, NULL); - // base types - kerepType_register(char, kerepTypeId_Char, NULL, NULL); - kerepType_register(bool, kerepTypeId_Bool, NULL, NULL); - kerepType_register(float32, kerepTypeId_Float32, NULL, NULL); - kerepType_register(float64, kerepTypeId_Float64, NULL, NULL); - kerepType_register(int8, kerepTypeId_Int8, NULL, NULL); - kerepType_register(uint8, kerepTypeId_UInt8, NULL, NULL); - kerepType_register(int16, kerepTypeId_Int16, NULL, NULL); - kerepType_register(uint16, kerepTypeId_UInt16, NULL, NULL); - kerepType_register(int32, kerepTypeId_Int32, NULL, NULL); - kerepType_register(uint32, kerepTypeId_UInt32, NULL, NULL); - kerepType_register(int64, kerepTypeId_Int64, NULL, NULL); - kerepType_register(uint64, kerepTypeId_UInt64, NULL, NULL); - // base type pointers - kerepType_register(char*, kerepTypeId_CharPtr, NULL, NULL); - kerepType_register(bool*, kerepTypeId_BoolPtr, NULL, NULL); - kerepType_register(float32*, kerepTypeId_Float32Ptr, NULL, NULL); - kerepType_register(float64*, kerepTypeId_Float64Ptr, NULL, NULL); - kerepType_register(int8*, kerepTypeId_Int8Ptr, NULL, NULL); - kerepType_register(uint8*, kerepTypeId_UInt8Ptr, NULL, NULL); - kerepType_register(int16*, kerepTypeId_Int16Ptr, NULL, NULL); - kerepType_register(uint16*, kerepTypeId_UInt16Ptr, NULL, NULL); - kerepType_register(int32*, kerepTypeId_Int32Ptr, NULL, NULL); - kerepType_register(uint32*, kerepTypeId_UInt32Ptr, NULL, NULL); - kerepType_register(int64*, kerepTypeId_Int64Ptr, NULL, NULL); - kerepType_register(uint64*, kerepTypeId_UInt64Ptr, NULL, NULL); - // kerepTypeDescriptor - kerepType_register(kerepTypeDescriptor, kerepTypeId_kerepTypeDescriptor, NULL, NULL); - kerepType_register(kerepTypeDescriptor*, kerepTypeId_kerepTypeDescriptorPtr, NULL, NULL); - - // base type autoarrs - kerepType_register(Autoarr_char, kerepTypeId_AutoarrChar, ____Autoarr_free_char, NULL); - kerepType_register(Autoarr_bool, kerepTypeId_AutoarrBool, ____Autoarr_free_bool, NULL); - kerepType_register(Autoarr_float32, kerepTypeId_AutoarrFloat32, ____Autoarr_free_float32, NULL); - kerepType_register(Autoarr_float64, kerepTypeId_AutoarrFloat64, ____Autoarr_free_float64, NULL); - kerepType_register(Autoarr_int8, kerepTypeId_AutoarrInt8, ____Autoarr_free_int8, NULL); - kerepType_register(Autoarr_uint8, kerepTypeId_AutoarrUInt8, ____Autoarr_free_uint8, NULL); - kerepType_register(Autoarr_int16, kerepTypeId_AutoarrInt16, ____Autoarr_free_int16, NULL); - kerepType_register(Autoarr_uint16, kerepTypeId_AutoarrUInt16, ____Autoarr_free_uint16, NULL); - kerepType_register(Autoarr_int32, kerepTypeId_AutoarrInt32, ____Autoarr_free_int32, NULL); - kerepType_register(Autoarr_uint32, kerepTypeId_AutoarrUInt32, ____Autoarr_free_uint32, NULL); - kerepType_register(Autoarr_int64, kerepTypeId_AutoarrInt64, ____Autoarr_free_int64, NULL); - kerepType_register(Autoarr_uint64, kerepTypeId_AutoarrUInt64, ____Autoarr_free_uint64, NULL); - // base type autoarr pointers - kerepType_register(Autoarr_char*, kerepTypeId_AutoarrCharPtr, ____Autoarr_free_char, NULL); - kerepType_register(Autoarr_bool*, kerepTypeId_AutoarrBoolPtr, ____Autoarr_free_bool, NULL); - kerepType_register(Autoarr_float32*, kerepTypeId_AutoarrFloat32Ptr, ____Autoarr_free_float32, NULL); - kerepType_register(Autoarr_float64*, kerepTypeId_AutoarrFloat64Ptr, ____Autoarr_free_float64, NULL); - kerepType_register(Autoarr_int8*, kerepTypeId_AutoarrInt8Ptr, ____Autoarr_free_int8, NULL); - kerepType_register(Autoarr_uint8*, kerepTypeId_AutoarrUInt8Ptr, ____Autoarr_free_uint8, NULL); - kerepType_register(Autoarr_int16*, kerepTypeId_AutoarrInt16Ptr, ____Autoarr_free_int16, NULL); - kerepType_register(Autoarr_uint16*, kerepTypeId_AutoarrUInt16Ptr, ____Autoarr_free_uint16, NULL); - kerepType_register(Autoarr_int32*, kerepTypeId_AutoarrInt32Ptr, ____Autoarr_free_int32, NULL); - kerepType_register(Autoarr_uint32*, kerepTypeId_AutoarrUInt32Ptr, ____Autoarr_free_uint32, NULL); - kerepType_register(Autoarr_int64*, kerepTypeId_AutoarrInt64Ptr, ____Autoarr_free_int64, NULL); - kerepType_register(Autoarr_uint64*, kerepTypeId_AutoarrUInt64Ptr, ____Autoarr_free_uint64, NULL); - - // Unitype - kerepType_register(Unitype, kerepTypeId_Unitype, __UnitypePtr_free, NULL); - kerepType_register(Unitype*, kerepTypeId_UnitypePtr, __UnitypePtr_free, NULL); - kerepType_register(Autoarr_Unitype, kerepTypeId_AutoarrUnitype, ____Autoarr_free_Unitype_, NULL); - kerepType_register(Autoarr_Unitype*, kerepTypeId_AutoarrUnitypePtr, ____Autoarr_free_Unitype_, NULL); - // 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 - kerepType_register(STNode, kerepTypeId_STNode, __STNode_free, NULL); - kerepType_register(STNode*, kerepTypeId_STNodePtr, __STNode_free, NULL); - - // KeyValuePair - kerepType_register(KVPair, kerepTypeId_KVPair, __KVPair_free, NULL); - kerepType_register(KVPair*, kerepTypeId_KVPairPtr, __KVPair_free, NULL); - kerepType_register(Autoarr_KVPair, kerepTypeId_AutoarrKVPair, ____Autoarr_free_KVPair_, NULL); - kerepType_register(Autoarr_KVPair*, kerepTypeId_AutoarrKVPairPtr, ____Autoarr_free_KVPair_, NULL); - // 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 - kerepType_register(Hashtable, kerepTypeId_Hashtable, __Hashtable_free, NULL); - kerepType_register(Hashtable*, kerepTypeId_HashtablePtr, __Hashtable_free, NULL); - - // string - kerepType_register(string, kerepTypeId_string, NULL, NULL); - kerepType_register(string*, kerepTypeId_stringPtr, NULL, NULL); - kerepType_register(string, kerepTypeId_AutoarrString, ____Autoarr_free_string, NULL); - kerepType_register(string*, kerepTypeId_AutoarrStringPtr, ____Autoarr_free_string, NULL); - // StringBuilder - kerepType_register(StringBuilder, kerepTypeId_StringBuilder, __StringBuilder_free, NULL); - kerepType_register(StringBuilder*, kerepTypeId_StringBuilderPtr, __StringBuilder_free, NULL); -} diff --git a/src/base/init.h b/src/base/init.h deleted file mode 100644 index dfe5e69..0000000 --- a/src/base/init.h +++ /dev/null @@ -1,12 +0,0 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -// call this between kerepTypeDescriptors_beginInit() and kerepTypeDescriptors_endInit() -void kerepTypeDescriptors_initKerepTypes(); - -#if __cplusplus -} -#endif \ No newline at end of file diff --git a/src/base/kprint/README.md b/src/base/kprint/README.md index c02cdac..6961b00 100644 --- a/src/base/kprint/README.md +++ b/src/base/kprint/README.md @@ -10,11 +10,11 @@ I don't really like printf function (and its variants), so i made safer and more ## how to use it: + **format construction:** ``` - kprint_format fmt= kprint_fgColor | kprint_fbgColor | kprint_fdataFmt | flags | kerepTypeId; + kprint_format fmt= kprint_fgColor | kprint_fbgColor | kprint_fdataFmt | flags | ktId; ``` [more about `kprint_format`](kprint_format.md) + fgColor and bgColor can be set to change console output color - + you should set dataFormat for `int`/`uint`/`float`/`char*` arguments and kerepTypeId for other types + + you should set dataFormat for `int`/`uint`/`float`/`char*` arguments and ktId for other types + flags can be set to modify TypeDescriptor.toString() behavior + don't forget to set TypeDescriptor.toString when registering type, or kprint will crash @@ -28,6 +28,6 @@ I don't really like printf function (and its variants), so i made safer and more should be sent as pointers ``` Maybe m=MaybeNull; - kprint(kprint_fgBlue | kprint_fmtString, "Maybe: ", kprint_fgGreen | kerepTypeId_MaybePtr, &m); + kprint(kprint_fgBlue | kprint_fmtString, "Maybe: ", kprint_fgGreen | ktId_MaybePtr, &m); ``` - output: Maybe: {value={0, kerepTypeId_Null}} \ No newline at end of file + output: Maybe: {value={0, ktId_Null}} \ No newline at end of file diff --git a/src/base/kprint/kprint.c b/src/base/kprint/kprint.c index fc89c72..3ef337a 100644 --- a/src/base/kprint/kprint.c +++ b/src/base/kprint/kprint.c @@ -7,20 +7,20 @@ Maybe __next_toString(kprint_format format, void* object){ case kprint_fmtInt: case kprint_fmtHex: case kprint_fmtBin: - format.typeId=kerepTypeId_Int64; break; + format.typeId=ktId_Int64; break; case kprint_fmtUInt: - format.typeId=kerepTypeId_UInt64; break; + format.typeId=ktId_UInt64; break; case kprint_fmtFloat: - format.typeId=kerepTypeId_Float64; break; + format.typeId=ktId_Float64; break; case kprint_fmtChar: - format.typeId=kerepTypeId_Char; break; + format.typeId=ktId_Char; break; case kprint_fmtString: - format.typeId=kerepTypeId_CharPtr; break; + format.typeId=ktId_CharPtr; break; default: safethrow("typeId is not set, can't autodetect type",;); } - try(kerepTypeDescriptor_get(format.typeId),mtd,;); - kerepTypeDescriptor typeDesc=*(kerepTypeDescriptor*)mtd.value.VoidPtr; + try(ktDescriptor_get(format.typeId),mtd,;); + ktDescriptor typeDesc=*(ktDescriptor*)mtd.value.VoidPtr; if(!typeDesc.toString) safethrow("type descriptor doesnt have toString() func",;); try(typeDesc.toString(object, &format), mStr,;); @@ -34,7 +34,7 @@ Maybe __ksprint(uint8 n, kprint_format* formats, void** objects){ StringBuilder_append_cptr(strb, mStr.value.VoidPtr); } char* rezult=StringBuilder_build(strb).ptr; - return SUCCESS(UniHeap(kerepTypeId_CharPtr, rezult)); + return SUCCESS(UniHeap(ktId_CharPtr, rezult)); } Maybe __kfprint(FILE* file, uint8 n, kprint_format* formats, void** objects){ @@ -68,9 +68,9 @@ void kprint_setColor(kprint_format f){ -Maybe ksprint_ar(uint32 count, kprint_format format, kerepTypeId typeId, void* array){ - try(kerepTypeDescriptor_get(format.typeId),mtd,;); - kerepTypeDescriptor typeDesc=*(kerepTypeDescriptor*)mtd.value.VoidPtr; +Maybe ksprint_ar(uint32 count, kprint_format format, ktId typeId, void* array){ + try(ktDescriptor_get(format.typeId),mtd,;); + ktDescriptor typeDesc=*(ktDescriptor*)mtd.value.VoidPtr; if(!typeDesc.toString) safethrow("type descriptor doesnt have toString() func",;); StringBuilder* strb=StringBuilder_create(); diff --git a/src/base/kprint/kprint_format.h b/src/base/kprint/kprint_format.h index 3fcfc68..1087c71 100644 --- a/src/base/kprint/kprint_format.h +++ b/src/base/kprint/kprint_format.h @@ -4,7 +4,7 @@ extern "C" { #endif -#include "../type_system/kerepTypeId.h" +#include "../type_system/ktId.h" typedef enum kprint_dataFormat{ // 00000000 00000000 00000000 00000000 @@ -39,7 +39,7 @@ typedef struct { unsigned char fgColor : 4; unsigned char bgColor : 4; unsigned char dataFmt : 4; - kerepTypeId typeId; + ktId typeId; } kprint_format; #if __cplusplus diff --git a/src/base/kprint/kprint_format.md b/src/base/kprint/kprint_format.md index 9d3da81..fcd4a95 100644 --- a/src/base/kprint/kprint_format.md +++ b/src/base/kprint/kprint_format.md @@ -2,7 +2,7 @@ ``` 00000000 00000000 00000000 00000000 -fgColorSet┘│││└┼┴┘ └┼┴┘└┴┴┤ kerepTypeId +fgColorSet┘│││└┼┴┘ └┼┴┘└┴┴┤ ktId bgColorSet─┘││ │ bgColor └data format prefix┬────┘│ └fgColor postfix └uppercase diff --git a/src/base/type_system/init.c b/src/base/type_system/init.c new file mode 100644 index 0000000..755756a --- /dev/null +++ b/src/base/type_system/init.c @@ -0,0 +1,103 @@ +#include "../base.h" +#include "../../Autoarr/Autoarr.h" +#include "../../SearchTree/SearchTree.h" +#include "../../Hashtable/Hashtable.h" +#include "../../String/StringBuilder.h" + +void ktDescriptors_initKerepTypes(){ + // null + kt_register(NULL, ktId_Null, NULL, NULL); + // base types + kt_register(char, ktId_Char, NULL, NULL); + kt_register(bool, ktId_Bool, NULL, NULL); + kt_register(float32, ktId_Float32, NULL, NULL); + kt_register(float64, ktId_Float64, NULL, NULL); + kt_register(int8, ktId_Int8, NULL, NULL); + kt_register(uint8, ktId_UInt8, NULL, NULL); + kt_register(int16, ktId_Int16, NULL, NULL); + kt_register(uint16, ktId_UInt16, NULL, NULL); + kt_register(int32, ktId_Int32, NULL, NULL); + kt_register(uint32, ktId_UInt32, NULL, NULL); + kt_register(int64, ktId_Int64, NULL, NULL); + kt_register(uint64, ktId_UInt64, NULL, NULL); + // base type pointers + kt_register(char*, ktId_CharPtr, NULL, NULL); + kt_register(bool*, ktId_BoolPtr, NULL, NULL); + kt_register(float32*, ktId_Float32Ptr, NULL, NULL); + kt_register(float64*, ktId_Float64Ptr, NULL, NULL); + kt_register(int8*, ktId_Int8Ptr, NULL, NULL); + kt_register(uint8*, ktId_UInt8Ptr, NULL, NULL); + kt_register(int16*, ktId_Int16Ptr, NULL, NULL); + kt_register(uint16*, ktId_UInt16Ptr, NULL, NULL); + kt_register(int32*, ktId_Int32Ptr, NULL, NULL); + kt_register(uint32*, ktId_UInt32Ptr, NULL, NULL); + kt_register(int64*, ktId_Int64Ptr, NULL, NULL); + kt_register(uint64*, ktId_UInt64Ptr, NULL, NULL); + // ktDescriptor + kt_register(ktDescriptor, ktId_ktDescriptor, NULL, NULL); + kt_register(ktDescriptor*, ktId_ktDescriptorPtr, NULL, NULL); + + // base type autoarrs + kt_register(Autoarr_char, ktId_AutoarrChar, ____Autoarr_free_char, NULL); + kt_register(Autoarr_bool, ktId_AutoarrBool, ____Autoarr_free_bool, NULL); + kt_register(Autoarr_float32, ktId_AutoarrFloat32, ____Autoarr_free_float32, NULL); + kt_register(Autoarr_float64, ktId_AutoarrFloat64, ____Autoarr_free_float64, NULL); + kt_register(Autoarr_int8, ktId_AutoarrInt8, ____Autoarr_free_int8, NULL); + kt_register(Autoarr_uint8, ktId_AutoarrUInt8, ____Autoarr_free_uint8, NULL); + kt_register(Autoarr_int16, ktId_AutoarrInt16, ____Autoarr_free_int16, NULL); + kt_register(Autoarr_uint16, ktId_AutoarrUInt16, ____Autoarr_free_uint16, NULL); + kt_register(Autoarr_int32, ktId_AutoarrInt32, ____Autoarr_free_int32, NULL); + kt_register(Autoarr_uint32, ktId_AutoarrUInt32, ____Autoarr_free_uint32, NULL); + kt_register(Autoarr_int64, ktId_AutoarrInt64, ____Autoarr_free_int64, NULL); + kt_register(Autoarr_uint64, ktId_AutoarrUInt64, ____Autoarr_free_uint64, NULL); + // base type autoarr pointers + kt_register(Autoarr_char*, ktId_AutoarrCharPtr, ____Autoarr_free_char, NULL); + kt_register(Autoarr_bool*, ktId_AutoarrBoolPtr, ____Autoarr_free_bool, NULL); + kt_register(Autoarr_float32*, ktId_AutoarrFloat32Ptr, ____Autoarr_free_float32, NULL); + kt_register(Autoarr_float64*, ktId_AutoarrFloat64Ptr, ____Autoarr_free_float64, NULL); + kt_register(Autoarr_int8*, ktId_AutoarrInt8Ptr, ____Autoarr_free_int8, NULL); + kt_register(Autoarr_uint8*, ktId_AutoarrUInt8Ptr, ____Autoarr_free_uint8, NULL); + kt_register(Autoarr_int16*, ktId_AutoarrInt16Ptr, ____Autoarr_free_int16, NULL); + kt_register(Autoarr_uint16*, ktId_AutoarrUInt16Ptr, ____Autoarr_free_uint16, NULL); + kt_register(Autoarr_int32*, ktId_AutoarrInt32Ptr, ____Autoarr_free_int32, NULL); + kt_register(Autoarr_uint32*, ktId_AutoarrUInt32Ptr, ____Autoarr_free_uint32, NULL); + kt_register(Autoarr_int64*, ktId_AutoarrInt64Ptr, ____Autoarr_free_int64, NULL); + kt_register(Autoarr_uint64*, ktId_AutoarrUInt64Ptr, ____Autoarr_free_uint64, NULL); + + // Unitype + kt_register(Unitype, ktId_Unitype, __UnitypePtr_free, NULL); + kt_register(Unitype*, ktId_UnitypePtr, __UnitypePtr_free, NULL); + kt_register(Autoarr_Unitype, ktId_AutoarrUnitype, ____Autoarr_free_Unitype_, NULL); + kt_register(Autoarr_Unitype*, ktId_AutoarrUnitypePtr, ____Autoarr_free_Unitype_, NULL); + // 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, ktId_STNode, __STNode_free, NULL); + kt_register(STNode*, ktId_STNodePtr, __STNode_free, NULL); + + // KeyValuePair + kt_register(KVPair, ktId_KVPair, __KVPair_free, NULL); + kt_register(KVPair*, ktId_KVPairPtr, __KVPair_free, NULL); + kt_register(Autoarr_KVPair, ktId_AutoarrKVPair, ____Autoarr_free_KVPair_, NULL); + kt_register(Autoarr_KVPair*, ktId_AutoarrKVPairPtr, ____Autoarr_free_KVPair_, NULL); + // 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, ktId_Hashtable, __Hashtable_free, NULL); + kt_register(Hashtable*, ktId_HashtablePtr, __Hashtable_free, NULL); + + // string + kt_register(string, ktId_string, NULL, NULL); + kt_register(string*, ktId_stringPtr, NULL, NULL); + kt_register(string, ktId_AutoarrString, ____Autoarr_free_string, NULL); + kt_register(string*, ktId_AutoarrStringPtr, ____Autoarr_free_string, NULL); + // StringBuilder + kt_register(StringBuilder, ktId_StringBuilder, __StringBuilder_free, NULL); + kt_register(StringBuilder*, ktId_StringBuilderPtr, __StringBuilder_free, NULL); +} diff --git a/src/base/type_system/init.h b/src/base/type_system/init.h new file mode 100644 index 0000000..d08ef9d --- /dev/null +++ b/src/base/type_system/init.h @@ -0,0 +1,12 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +// call this between ktDescriptors_beginInit() and ktDescriptors_endInit() +void ktDescriptors_initKerepTypes(); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/base/type_system/kerepTypeId.h b/src/base/type_system/kerepTypeId.h deleted file mode 100644 index 6f4f4af..0000000 --- a/src/base/type_system/kerepTypeId.h +++ /dev/null @@ -1,17 +0,0 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "../std.h" -typedef uint16 kerepTypeId; - -#define kerepTypeId_declare(ID_VAR_NAME)\ - extern kerepTypeId ID_VAR_NAME -#define kerepTypeId_define(ID_VAR_NAME)\ - kerepTypeId ID_VAR_NAME=-1 - -#if __cplusplus -} -#endif \ No newline at end of file diff --git a/src/base/type_system/kerepTypeDescriptor.h b/src/base/type_system/ktDescriptor.h similarity index 77% rename from src/base/type_system/kerepTypeDescriptor.h rename to src/base/type_system/ktDescriptor.h index b402200..907d002 100644 --- a/src/base/type_system/kerepTypeDescriptor.h +++ b/src/base/type_system/ktDescriptor.h @@ -6,16 +6,16 @@ extern "C" { #include "../std.h" #include "../errors.h" -#include "kerepTypeId.h" +#include "ktId.h" -typedef struct kerepTypeDescriptor{ +typedef struct ktDescriptor{ char* name; - kerepTypeId id; + ktId id; uint16 size; void (*freeMembers)(void*); // NULL or function which frees all struct members ///@return Maybe Maybe (*toString)(void*, int32); // NULL or function which generates string representaion of object -} kerepTypeDescriptor; +} ktDescriptor; #if __cplusplus } diff --git a/src/base/type_system/ktId.h b/src/base/type_system/ktId.h new file mode 100644 index 0000000..8c37985 --- /dev/null +++ b/src/base/type_system/ktId.h @@ -0,0 +1,17 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../std.h" +typedef uint16 ktId; + +#define ktId_declare(ID_VAR_NAME)\ + extern ktId ID_VAR_NAME +#define ktId_define(ID_VAR_NAME)\ + ktId ID_VAR_NAME=-1 + +#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 new file mode 100644 index 0000000..0bc4f7d --- /dev/null +++ b/src/base/type_system/kt_functions.c @@ -0,0 +1,75 @@ +#include "../../Autoarr/Autoarr.h" + +Autoarr_declare(ktDescriptor) +Autoarr_define(ktDescriptor) + +ktId_define(ktId_Null); + +ktId_define(ktId_Char); +ktId_define(ktId_Bool); +ktId_define(ktId_Float32); +ktId_define(ktId_Float64); +ktId_define(ktId_Int8); +ktId_define(ktId_UInt8); +ktId_define(ktId_Int16); +ktId_define(ktId_UInt16); +ktId_define(ktId_Int32); +ktId_define(ktId_UInt32); +ktId_define(ktId_Int64); +ktId_define(ktId_UInt64); + +ktId_define(ktId_CharPtr); +ktId_define(ktId_BoolPtr); +ktId_define(ktId_Float32Ptr); +ktId_define(ktId_Float64Ptr); +ktId_define(ktId_Int8Ptr); +ktId_define(ktId_UInt8Ptr); +ktId_define(ktId_Int16Ptr); +ktId_define(ktId_UInt16Ptr); +ktId_define(ktId_Int32Ptr); +ktId_define(ktId_UInt32Ptr); +ktId_define(ktId_Int64Ptr); +ktId_define(ktId_UInt64Ptr); + +ktId_define(ktId_ktDescriptor); +ktId_define(ktId_ktDescriptorPtr); + +// type descriptors are stored here during initialization +Autoarr(ktDescriptor)* __ktDescriptors=NULL; +// here type descriptors are stored when initialization is complited +ktDescriptor* typeDescriptors=NULL; +ktId ktId_last=-1; + +typedef enum{ + NotInitialized, Initializing, Initialized +} ktDescriptorsState; +ktDescriptorsState initState=NotInitialized; + +void ktDescriptors_beginInit(){ + printf("\e[94mtype descriptors initializing...\n"); + __ktDescriptors=Autoarr_create(ktDescriptor, 256, 256); + if(__ktDescriptors==NULL) throw(ERR_NULLPTR); +} + +void ktDescriptors_endInit(){ + typeDescriptors=Autoarr_toArray(__ktDescriptors); + Autoarr_free(__ktDescriptors,true); + if(typeDescriptors==NULL) throw(ERR_NULLPTR); + printf("\e[92minitialized %u type descriptors\n", ktId_last); +} + +void __kt_register(char* name, int16 size, void (*freeMembers)(void*), Maybe (*toString)(void*, int32)){ + ktDescriptor typeDesc={ + .name=name, + .size=size, + .id=++ktId_last, + .freeMembers=freeMembers, + .toString=toString + }; + Autoarr_add(__ktDescriptors, typeDesc); +} + +Maybe ktDescriptor_get(ktId id){ + if(id>ktId_last) safethrow("invalid type id",;); + return SUCCESS(UniStack(ktId_ktDescriptorPtr, &typeDescriptors[id])); +} diff --git a/src/base/type_system/kt_functions.h b/src/base/type_system/kt_functions.h new file mode 100644 index 0000000..2f13b74 --- /dev/null +++ b/src/base/type_system/kt_functions.h @@ -0,0 +1,59 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../std.h" +#include "ktId.h" +#include "ktDescriptor.h" + + +extern ktId ktId_last; +void __kt_register(char* name, int16 size, void (*freeMembers)(void*), Maybe (*toString)(void*, int32)); + +#define kt_register(TYPE, ID_VAR_NAME, FREE_MEMBERS_FUNC, TO_STRING_FUNC)\ + __kt_register(#ID_VAR_NAME, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\ + ID_VAR_NAME=ktId_last; + +void ktDescriptors_beginInit(); +void ktDescriptors_endInit(); +/// @param id id of registered type +/// @return Maybe +Maybe ktDescriptor_get(ktId id); + + +ktId_declare(ktId_Null); + +ktId_declare(ktId_Char); +ktId_declare(ktId_Bool); +ktId_declare(ktId_Float32); +ktId_declare(ktId_Float64); +ktId_declare(ktId_Int8); +ktId_declare(ktId_UInt8); +ktId_declare(ktId_Int16); +ktId_declare(ktId_UInt16); +ktId_declare(ktId_Int32); +ktId_declare(ktId_UInt32); +ktId_declare(ktId_Int64); +ktId_declare(ktId_UInt64); + +ktId_declare(ktId_CharPtr); +ktId_declare(ktId_BoolPtr); +ktId_declare(ktId_Float32Ptr); +ktId_declare(ktId_Float64Ptr); +ktId_declare(ktId_Int8Ptr); +ktId_declare(ktId_UInt8Ptr); +ktId_declare(ktId_Int16Ptr); +ktId_declare(ktId_UInt16Ptr); +ktId_declare(ktId_Int32Ptr); +ktId_declare(ktId_UInt32Ptr); +ktId_declare(ktId_Int64Ptr); +ktId_declare(ktId_UInt64Ptr); + +ktId_declare(ktId_ktDescriptor); +ktId_declare(ktId_ktDescriptorPtr); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/base/type_system/type_system.h b/src/base/type_system/type_system.h new file mode 100644 index 0000000..f685112 --- /dev/null +++ b/src/base/type_system/type_system.h @@ -0,0 +1,4 @@ + +#include "init.h" +#include "kt_functions.h" +#include "unitype.h" \ No newline at end of file diff --git a/src/base/type_system/types.c b/src/base/type_system/types.c deleted file mode 100644 index 82d2d6d..0000000 --- a/src/base/type_system/types.c +++ /dev/null @@ -1,76 +0,0 @@ -#include "../../Autoarr/Autoarr.h" -#include "types.h" - -Autoarr_declare(kerepTypeDescriptor) -Autoarr_define(kerepTypeDescriptor) - -kerepTypeId_define(kerepTypeId_Null); - -kerepTypeId_define(kerepTypeId_Char); -kerepTypeId_define(kerepTypeId_Bool); -kerepTypeId_define(kerepTypeId_Float32); -kerepTypeId_define(kerepTypeId_Float64); -kerepTypeId_define(kerepTypeId_Int8); -kerepTypeId_define(kerepTypeId_UInt8); -kerepTypeId_define(kerepTypeId_Int16); -kerepTypeId_define(kerepTypeId_UInt16); -kerepTypeId_define(kerepTypeId_Int32); -kerepTypeId_define(kerepTypeId_UInt32); -kerepTypeId_define(kerepTypeId_Int64); -kerepTypeId_define(kerepTypeId_UInt64); - -kerepTypeId_define(kerepTypeId_CharPtr); -kerepTypeId_define(kerepTypeId_BoolPtr); -kerepTypeId_define(kerepTypeId_Float32Ptr); -kerepTypeId_define(kerepTypeId_Float64Ptr); -kerepTypeId_define(kerepTypeId_Int8Ptr); -kerepTypeId_define(kerepTypeId_UInt8Ptr); -kerepTypeId_define(kerepTypeId_Int16Ptr); -kerepTypeId_define(kerepTypeId_UInt16Ptr); -kerepTypeId_define(kerepTypeId_Int32Ptr); -kerepTypeId_define(kerepTypeId_UInt32Ptr); -kerepTypeId_define(kerepTypeId_Int64Ptr); -kerepTypeId_define(kerepTypeId_UInt64Ptr); - -kerepTypeId_define(kerepTypeId_kerepTypeDescriptor); -kerepTypeId_define(kerepTypeId_kerepTypeDescriptorPtr); - -// type descriptors are stored here during initialization -Autoarr(kerepTypeDescriptor)* __kerepTypeDescriptors=NULL; -// here type descriptors are stored when initialization is complited -kerepTypeDescriptor* typeDescriptors=NULL; -kerepTypeId kerepTypeId_last=-1; - -typedef enum{ - NotInitialized, Initializing, Initialized -} kerepTypeDescriptorsState; -kerepTypeDescriptorsState initState=NotInitialized; - -void kerepTypeDescriptors_beginInit(){ - printf("\e[94mtype descriptors initializing...\n"); - __kerepTypeDescriptors=Autoarr_create(kerepTypeDescriptor, 256, 256); - if(__kerepTypeDescriptors==NULL) throw(ERR_NULLPTR); -} - -void kerepTypeDescriptors_endInit(){ - typeDescriptors=Autoarr_toArray(__kerepTypeDescriptors); - Autoarr_free(__kerepTypeDescriptors,true); - if(typeDescriptors==NULL) throw(ERR_NULLPTR); - printf("\e[92minitialized %u type descriptors\n", kerepTypeId_last); -} - -void __kerepType_register(char* name, int16 size, void (*freeMembers)(void*), Maybe (*toString)(void*, int32)){ - kerepTypeDescriptor typeDesc={ - .name=name, - .size=size, - .id=++kerepTypeId_last, - .freeMembers=freeMembers, - .toString=toString - }; - Autoarr_add(__kerepTypeDescriptors, typeDesc); -} - -Maybe kerepTypeDescriptor_get(kerepTypeId id){ - if(id>kerepTypeId_last) safethrow("invalid type id",;); - return SUCCESS(UniStack(kerepTypeId_kerepTypeDescriptorPtr, &typeDescriptors[id])); -} diff --git a/src/base/type_system/types.h b/src/base/type_system/types.h deleted file mode 100644 index 8cbaa6b..0000000 --- a/src/base/type_system/types.h +++ /dev/null @@ -1,59 +0,0 @@ -#pragma once - -#if __cplusplus -extern "C" { -#endif - -#include "../std.h" -#include "kerepTypeId.h" -#include "kerepTypeDescriptor.h" - - -extern kerepTypeId kerepTypeId_last; -void __kerepType_register(char* name, int16 size, void (*freeMembers)(void*), Maybe (*toString)(void*, int32)); - -#define kerepType_register(TYPE, ID_VAR_NAME, FREE_MEMBERS_FUNC, TO_STRING_FUNC)\ - __kerepType_register(#ID_VAR_NAME, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\ - ID_VAR_NAME=kerepTypeId_last; - -void kerepTypeDescriptors_beginInit(); -void kerepTypeDescriptors_endInit(); -/// @param id id of registered type -/// @return Maybe -Maybe kerepTypeDescriptor_get(kerepTypeId id); - - -kerepTypeId_declare(kerepTypeId_Null); - -kerepTypeId_declare(kerepTypeId_Char); -kerepTypeId_declare(kerepTypeId_Bool); -kerepTypeId_declare(kerepTypeId_Float32); -kerepTypeId_declare(kerepTypeId_Float64); -kerepTypeId_declare(kerepTypeId_Int8); -kerepTypeId_declare(kerepTypeId_UInt8); -kerepTypeId_declare(kerepTypeId_Int16); -kerepTypeId_declare(kerepTypeId_UInt16); -kerepTypeId_declare(kerepTypeId_Int32); -kerepTypeId_declare(kerepTypeId_UInt32); -kerepTypeId_declare(kerepTypeId_Int64); -kerepTypeId_declare(kerepTypeId_UInt64); - -kerepTypeId_declare(kerepTypeId_CharPtr); -kerepTypeId_declare(kerepTypeId_BoolPtr); -kerepTypeId_declare(kerepTypeId_Float32Ptr); -kerepTypeId_declare(kerepTypeId_Float64Ptr); -kerepTypeId_declare(kerepTypeId_Int8Ptr); -kerepTypeId_declare(kerepTypeId_UInt8Ptr); -kerepTypeId_declare(kerepTypeId_Int16Ptr); -kerepTypeId_declare(kerepTypeId_UInt16Ptr); -kerepTypeId_declare(kerepTypeId_Int32Ptr); -kerepTypeId_declare(kerepTypeId_UInt32Ptr); -kerepTypeId_declare(kerepTypeId_Int64Ptr); -kerepTypeId_declare(kerepTypeId_UInt64Ptr); - -kerepTypeId_declare(kerepTypeId_kerepTypeDescriptor); -kerepTypeId_declare(kerepTypeId_kerepTypeDescriptorPtr); - -#if __cplusplus -} -#endif \ No newline at end of file diff --git a/src/base/type_system/unitype.c b/src/base/type_system/unitype.c index 8b44641..f727fc7 100644 --- a/src/base/type_system/unitype.c +++ b/src/base/type_system/unitype.c @@ -1,11 +1,11 @@ #include "../base.h" -kerepTypeId_define(kerepTypeId_Unitype); -kerepTypeId_define(kerepTypeId_UnitypePtr); +ktId_define(ktId_Unitype); +ktId_define(ktId_UnitypePtr); void Unitype_free(Unitype u){ - tryLast(kerepTypeDescriptor_get(u.typeId), mType); - kerepTypeDescriptor type=*(kerepTypeDescriptor*)mType.value.VoidPtr; + tryLast(ktDescriptor_get(u.typeId), mType); + ktDescriptor type=*(ktDescriptor*)mType.value.VoidPtr; if(type.freeMembers) type.freeMembers(u.VoidPtr); if(u.allocatedInHeap) @@ -16,17 +16,17 @@ void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); } #define BUFSIZE 64 char* sprintuni(Unitype v){ char* buf=malloc(BUFSIZE); - tryLast(kerepTypeDescriptor_get(v.typeId), mType); - kerepTypeDescriptor type=*(kerepTypeDescriptor*)mType.value.VoidPtr; - if(v.typeId==kerepTypeId_Null) + tryLast(ktDescriptor_get(v.typeId), mType); + ktDescriptor type=*(ktDescriptor*)mType.value.VoidPtr; + if(v.typeId==ktId_Null) sprintf_s(buf, BUFSIZE, "{Null}"); - else if(v.typeId==kerepTypeId_Float64) + else if(v.typeId==ktId_Float64) sprintf_s(buf, BUFSIZE, "{%s : %lf}", type.name,v.Float64); - else if(v.typeId==kerepTypeId_Bool || v.typeId==kerepTypeId_UInt64) + else if(v.typeId==ktId_Bool || v.typeId==ktId_UInt64) sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%llu", "%lu") "}", type.name,v.UInt64); - else if(v.typeId==kerepTypeId_Int64) + else if(v.typeId==ktId_Int64) sprintf_s(buf, BUFSIZE, "{%s : " IFWIN("%lld", "%ld") "}", type.name,v.Int64); - else if(v.typeId==kerepTypeId_CharPtr){ + else if(v.typeId==ktId_CharPtr){ size_t newBUFSIZE=cptr_length(v.VoidPtr) + BUFSIZE/2; buf=realloc(buf, newBUFSIZE); sprintf_s(buf, BUFSIZE, "{%s : \"%s\"}", type.name,(char*)v.VoidPtr); diff --git a/src/base/type_system/unitype.h b/src/base/type_system/unitype.h index 9f28bf7..2fbbdf6 100644 --- a/src/base/type_system/unitype.h +++ b/src/base/type_system/unitype.h @@ -4,7 +4,7 @@ extern "C" { #endif -#include "kerepTypeId.h" +#include "ktId.h" typedef struct Unitype{ union { @@ -15,15 +15,15 @@ typedef struct Unitype{ void* VoidPtr; char Bytes[8]; }; - kerepTypeId typeId; + ktId typeId; bool allocatedInHeap; // should Unitype_free call free() to VoidPtr* } Unitype; -kerepTypeId_declare(kerepTypeId_Unitype); -kerepTypeId_declare(kerepTypeId_UnitypePtr); +ktId_declare(ktId_Unitype); +ktId_declare(ktId_UnitypePtr); #define __UniDef(TYPE, VAL) (Unitype){\ - .TYPE=VAL, .typeId=kerepTypeId_##TYPE, .allocatedInHeap=false} + .TYPE=VAL, .typeId=ktId_##TYPE, .allocatedInHeap=false} #define UniInt64(VAL) __UniDef(Int64, VAL) #define UniUInt64(VAL) __UniDef(UInt64, VAL) @@ -35,7 +35,7 @@ kerepTypeId_declare(kerepTypeId_UnitypePtr); #define UniHeap(ID_VAR_NAME, VAL) (Unitype){\ .VoidPtr=VAL, .typeId=ID_VAR_NAME, .allocatedInHeap=true} -#define UniNull UniStack(kerepTypeId_Null, NULL) +#define UniNull UniStack(ktId_Null, NULL) #define UniTrue UniBool(true) #define UniFalse UniBool(false) diff --git a/tests/main.cpp b/tests/main.cpp index 1f74e68..9136271 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -13,9 +13,9 @@ void test_all(){ int main(){ setlocale(LC_ALL, "en-US.Unicode"); - kerepTypeDescriptors_beginInit(); - kerepTypeDescriptors_initKerepTypes(); - kerepTypeDescriptors_endInit(); + ktDescriptors_beginInit(); + ktDescriptors_initKerepTypes(); + ktDescriptors_endInit(); printf("\e[97mkerep tests are starting!\n"); optime("test_all",1,test_all()); printf("\e[0m\n"); diff --git a/tests/test_dtsod.c b/tests/test_dtsod.c index f14c582..23ef429 100644 --- a/tests/test_dtsod.c +++ b/tests/test_dtsod.c @@ -18,7 +18,7 @@ void print_dtsod(Hashtable* dtsod){ printf("\e[92m"); Hashtable_foreach(dtsod, p,({ printkvp(p); - if(p.value.typeId==kerepTypeId_HashtablePtr){ + if(p.value.typeId==ktId_HashtablePtr){ printf(": {\n"); Hashtable* sub=p.value.VoidPtr; Hashtable_foreach(sub, _p,({ diff --git a/tests/test_marshalling.c b/tests/test_marshalling.c index 74e2129..1614c6c 100644 --- a/tests/test_marshalling.c +++ b/tests/test_marshalling.c @@ -4,7 +4,7 @@ EXPORT void CALL test_marshalling(char* text, KVPair** kptr){ KVPair* k=malloc(sizeof(KVPair)); k->key="message"; char* tc=cptr_copy(text); - Unitype u=UniHeap(kerepTypeId_CharPtr,tc); + Unitype u=UniHeap(ktId_CharPtr,tc); k->value=u; *kptr=k; } diff --git a/tests/test_searchtree.c b/tests/test_searchtree.c index c67e8fd..109c116 100644 --- a/tests/test_searchtree.c +++ b/tests/test_searchtree.c @@ -44,11 +44,11 @@ void test_searchtree(){ printuni(u); ST_push(node,"channel_id", u); printf(" -> channel_id\n "); - u=UniHeap(kerepTypeId_CharPtr, cptr_copy("32.2004")); + u=UniHeap(ktId_CharPtr, cptr_copy("32.2004")); printuni(u); ST_push(node,"message_id", u); printf(" -> message_id\n "); - u=UniStack(kerepTypeId_CharPtr,"some text UwU"); + u=UniStack(ktId_CharPtr,"some text UwU"); printuni(u); ST_push(node,"text", u); printf(" -> text\n"); From a3c8ef6486e7aa9d06a258bc4e83653d98513bf7 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 9 Sep 2022 19:06:35 +0600 Subject: [PATCH 26/59] build_scripts removed --- .../build_configurations/build_shared_lib.sh | 10 --- .../build_configurations/build_static_lib.sh | 10 --- .../build_static_lib_dbg.sh | 10 --- .../build_configurations/build_test.sh | 10 --- .../build_configurations/build_test_dbg.sh | 10 --- build_scripts/build_configurations/test.sh | 8 -- .../build_configurations/test_valgrind.sh | 9 -- build_scripts/colors.sh | 11 --- build_scripts/default.config.sh | 43 --------- build_scripts/functions.sh | 89 ------------------- build_scripts/init.sh | 22 ----- 11 files changed, 232 deletions(-) delete mode 100644 build_scripts/build_configurations/build_shared_lib.sh delete mode 100644 build_scripts/build_configurations/build_static_lib.sh delete mode 100644 build_scripts/build_configurations/build_static_lib_dbg.sh delete mode 100644 build_scripts/build_configurations/build_test.sh delete mode 100644 build_scripts/build_configurations/build_test_dbg.sh delete mode 100644 build_scripts/build_configurations/test.sh delete mode 100644 build_scripts/build_configurations/test_valgrind.sh delete mode 100644 build_scripts/colors.sh delete mode 100644 build_scripts/default.config.sh delete mode 100644 build_scripts/functions.sh delete mode 100644 build_scripts/init.sh diff --git a/build_scripts/build_configurations/build_shared_lib.sh b/build_scripts/build_configurations/build_shared_lib.sh deleted file mode 100644 index dbe33c9..0000000 --- a/build_scripts/build_configurations/build_shared_lib.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -source build_scripts/init.sh - -print "${CYAN}==========[build_shared_lib]==========\n" -clear_dir "$OUTDIR" -clear_dir "$OBJDIR" -compile_c "$BUILD_SHARED_LIB_C_ARGS" "$SRC_C tests/test_marshalling.c" -compile_cpp "$BUILD_SHARED_LIB_CPP_ARGS" "$SRC_CPP" -link "$BUILD_SHARED_LIB_CPP_ARGS $BUILD_SHARED_LIB_LINKER_ARGS" "$SHARED_LIB_FILE" diff --git a/build_scripts/build_configurations/build_static_lib.sh b/build_scripts/build_configurations/build_static_lib.sh deleted file mode 100644 index 692155e..0000000 --- a/build_scripts/build_configurations/build_static_lib.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -source build_scripts/init.sh - -print "${CYAN}==========[build_static_lib]==========\n" -clear_dir "$OUTDIR" -clear_dir "$OBJDIR" -compile_c "$BUILD_STATIC_LIB_C_ARGS" "$SRC_C tests/test_marshalling.c" -compile_cpp "$BUILD_STATIC_LIB_CPP_ARGS" "$SRC_CPP" -pack_static_lib "$STATIC_LIB_FILE" diff --git a/build_scripts/build_configurations/build_static_lib_dbg.sh b/build_scripts/build_configurations/build_static_lib_dbg.sh deleted file mode 100644 index ef8aa3d..0000000 --- a/build_scripts/build_configurations/build_static_lib_dbg.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -source build_scripts/init.sh - -print "${CYAN}==========[build_static_lib_dbg]==========\n" -clear_dir "$OUTDIR" -clear_dir "$OBJDIR" -compile_c "$BUILD_STATIC_LIB_DBG_C_ARGS" "$SRC_C tests/test_marshalling.c" -compile_cpp "$BUILD_STATIC_LIB_DBG_CPP_ARGS" "$SRC_CPP" -pack_static_lib "$STATIC_LIB_DBG_FILE" diff --git a/build_scripts/build_configurations/build_test.sh b/build_scripts/build_configurations/build_test.sh deleted file mode 100644 index 570de5a..0000000 --- a/build_scripts/build_configurations/build_test.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -source build_scripts/init.sh - -print "${CYAN}=============[build_test]=============\n" -clear_dir "$OUTDIR" -clear_dir "$OBJDIR" -compile_c "$BUILD_TEST_C_ARGS" "$SRC_C $TESTS_C" -compile_cpp "$BUILD_TEST_CPP_ARGS" "$SRC_CPP $TESTS_CPP" -link "$BUILD_TEST_CPP_ARGS $BUILD_TEST_LINKER_ARGS" $TEST_FILE diff --git a/build_scripts/build_configurations/build_test_dbg.sh b/build_scripts/build_configurations/build_test_dbg.sh deleted file mode 100644 index ff56aa0..0000000 --- a/build_scripts/build_configurations/build_test_dbg.sh +++ /dev/null @@ -1,10 +0,0 @@ -#!/bin/bash - -source build_scripts/init.sh - -print "${CYAN}===========[build_test_dbg]===========\n" -clear_dir "$OUTDIR" -clear_dir "$OBJDIR" -compile_c "$BUILD_TEST_DBG_C_ARGS" "$SRC_C $TESTS_C" -compile_cpp "$BUILD_TEST_DBG_CPP_ARGS" "$SRC_CPP $TESTS_CPP" -link "$BUILD_TEST_DBG_CPP_ARGS $BUILD_TEST_DBG_LINKER_ARGS" $TEST_DBG_FILE diff --git a/build_scripts/build_configurations/test.sh b/build_scripts/build_configurations/test.sh deleted file mode 100644 index 45ac85c..0000000 --- a/build_scripts/build_configurations/test.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/bin/bash - -source build_scripts/init.sh - -print "${CYAN}================[test]================\n" -cd $OUTDIR -./$TEST_FILE -cd .. diff --git a/build_scripts/build_configurations/test_valgrind.sh b/build_scripts/build_configurations/test_valgrind.sh deleted file mode 100644 index a92df20..0000000 --- a/build_scripts/build_configurations/test_valgrind.sh +++ /dev/null @@ -1,9 +0,0 @@ -#!/bin/bash - -source build_scripts/init.sh - -print "${CYAN}===========[test_valgrind]============\n" -cd $OUTDIR -valgrind $VALGRIND_ARGS ./$TEST_DBG_FILE -cat "valgrind.log" -cd .. diff --git a/build_scripts/colors.sh b/build_scripts/colors.sh deleted file mode 100644 index e3e8363..0000000 --- a/build_scripts/colors.sh +++ /dev/null @@ -1,11 +0,0 @@ -#!/bin/bash - -BLACK='\033[0;30m' -GRAY='\033[0;37m' -WHITE='\033[0;97m' -RED='\033[0;91m' -GREEN='\033[0;92m' -YELLOW='\033[0;93m' -BLUE='\033[0;94m' -PURPLE='\033[0;95m' -CYAN='\033[0;96m' diff --git a/build_scripts/default.config.sh b/build_scripts/default.config.sh deleted file mode 100644 index 7c2c541..0000000 --- a/build_scripts/default.config.sh +++ /dev/null @@ -1,43 +0,0 @@ -#!/bin/bash - -OUTDIR=bin -OBJDIR=obj -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')" -SRC_CPP="$( find src -name '*.cpp')" -TESTS_C="$( find tests -name '*.c')" -TESTS_CPP="$(find tests -name '*.cpp')" -VALGRIND_ARGS="-s --log-file=valgrind.log --read-var-info=yes --track-origins=yes --fullpath-after=kerep/ --leak-check=full --show-leak-kinds=all" - -# build_test -TEST_FILE=kerep.com -BUILD_TEST_C_ARGS="-O2" -BUILD_TEST_CPP_ARGS="$BUILD_TEST_C_ARGS" -BUILD_TEST_LINKER_ARGS="" - -# build_test_dbg -TEST_DBG_FILE=$TEST_FILE.dbg -BUILD_TEST_DBG_C_ARGS="-O0 -g" -BUILD_TEST_DBG_CPP_ARGS="$BUILD_TEST_DBG_C_ARGS" -BUILD_TEST_DBG_LINKER_ARGS="" - -# build_shared_lib -SHARED_LIB_FILE=kerep.so -BUILD_SHARED_LIB_C_ARGS="-O2 -fpic -flto -shared" -BUILD_SHARED_LIB_CPP_ARGS="$BUILD_SHARED_LIB_C_ARGS" -BUILD_SHARED_LIB_LINKER_ARGS="-Wl,-soname,$SHARED_LIB_FILE" - -# build_static_lib -STATIC_LIB_FILE=kerep.a -BUILD_STATIC_LIB_C_ARGS="-O2 -fpic" -BUILD_STATIC_LIB_CPP_ARGS="$BUILD_STATIC_LIB_C_ARGS" - -# build_static_lib_dbg -STATIC_LIB_DBG_FILE="$STATIC_LIB_FILE" -BUILD_STATIC_LIB_DBG_C_ARGS="-O0 -g" -BUILD_STATIC_LIB_DBG_CPP_ARGS="$BUILD_STATIC_LIB_DBG_C_ARGS" diff --git a/build_scripts/functions.sh b/build_scripts/functions.sh deleted file mode 100644 index a2486e0..0000000 --- a/build_scripts/functions.sh +++ /dev/null @@ -1,89 +0,0 @@ -#!/bin/bash - -function print { - printf "$1$GRAY" -} - -function clear_dir { - print "${BLUE}clearing $1\n" - rm -rf $1 - mkdir $1 -} - -function compile { - local cmp=$1 - print "${BLUE}compiler: ${GRAY}$cmp\n" - local std=$2 - print "${BLUE}standard: ${GRAY}$std\n" - local warn=$3 - print "${BLUE}warnings: ${GRAY}$warn\n" - local args=$4 - print "${BLUE}args: ${GRAY}$args\n" - local sources=$5 - print "${BLUE}sources: ${GRAY}$sources\n" - local compilation_error=0 - - for srcfile in $sources - do ( - local object="$OBJDIR/$(basename $srcfile).o" - if ! $($cmp -std=$std $warn $args -c -o $object $srcfile) - then - print "${RED}some error happened\n" - compilation_error=1 - fi - ) & done - wait - - if [ $compilation_error != 0 ] - then - exit 1 - fi -} - -# (args, sources) -function compile_c { - print "${CYAN}-------------[compile_c]--------------\n" - compile $CMP_C $STD_C "$WARN_C" "$1" "$2" -} - -# (args, sources) -function compile_cpp { - print "${CYAN}------------[compile_cpp]-------------\n" - compile $CMP_CPP $STD_CPP "$WARN_CPP" "$1" "$2" -} - -# (args, outfile) -function link { - print "${CYAN}----------------[link]----------------\n" - local args=$1 - print "${BLUE}args: ${GRAY}$args\n" - local outfile=$OUTDIR/$2 - print "${BLUE}outfile: ${GRAY}$outfile\n" - local objects="$(find $OBJDIR -name *.o)" - print "${BLUE}objects: ${GRAY}$objects\n" - if $CMP_CPP $args -o $outfile $(echo $objects | tr '\n' ' ') - then - print "${GREEN}file $CYAN$outfile ${GREEN}created\n" - rm -rf $OBJDIR - else - print "${RED}some error happened\n" - exit 1 - fi -} - -# (outfile) -function pack_static_lib { - print "${CYAN}----------------[link]----------------\n" - local outfile=$OUTDIR/$1 - print "${BLUE}outfile: ${GRAY}$outfile\n" - local objects="$(find $OBJDIR -name *.o)" - print "${BLUE}objects: ${GRAY}$objects\n" - if ar rcs $outfile $(echo $objects | tr '\n' ' ') - then - print "${GREEN}file $CYAN$outfile ${GREEN}created\n" - rm -rf $OBJDIR - else - print "${RED}some error happened\n" - exit 1 - fi -} \ No newline at end of file diff --git a/build_scripts/init.sh b/build_scripts/init.sh deleted file mode 100644 index 868baf5..0000000 --- a/build_scripts/init.sh +++ /dev/null @@ -1,22 +0,0 @@ -#!/bin/bash - -tabs 4 - -source build_scripts/colors.sh -source build_scripts/functions.sh - -if [ ! -f ".config" ]; then - print "${YELLOW}./.config doesn't exists\n" - cp build_scripts/default.config.sh .config - print "${YELLOW}default config created\n" - while true; do - print "${WHITE}continue? (y/n) " - read answ - case $answ in - [Yy] ) break;; - [Nn] ) exit;; - * ) print "${RED}incorrect answer\n";; - esac - done -fi -source .config From e08da5f4a7dbcbe4dc1b10718835d32d1e4c2bec Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 9 Sep 2022 19:06:49 +0600 Subject: [PATCH 27/59] added cbuild --- .gitmodules | 3 +++ Makefile | 32 ++++++++++++++++---------------- 2 files changed, 19 insertions(+), 16 deletions(-) create mode 100644 .gitmodules diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..dd060e8 --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "cbuild"] + path = cbuild + url = https://github.com/Timerix22/cbuild.git diff --git a/Makefile b/Makefile index 49eea86..c00ea21 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,24 @@ -###### Building tasks ####### -build_test: - @build_scripts/build_configurations/build_test.sh - -build_test_dbg: - @build_scripts/build_configurations/build_test_dbg.sh +###### Build cbuild/default_tasks ####### +build_exec: + @cbuild/default_tasks/build_exec.sh +build_exec_dbg: + @cbuild/default_tasks/build_exec_dbg.sh build_shared_lib: - @build_scripts/build_configurations/build_shared_lib.sh + @cbuild/default_tasks/build_shared_lib.sh +build_shared_lib_dbg: + @cbuild/default_tasks/build_shared_lib_dbg.sh build_static_lib: - @build_scripts/build_configurations/build_static_lib.sh - + @cbuild/default_tasks/build_static_lib.sh build_static_lib_dbg: - @build_scripts/build_configurations/build_static_lib_dbg.sh + @cbuild/default_tasks/build_static_lib_dbg.sh -###### Testing tasks ####### -test: build_test - @build_scripts/build_configurations/test.sh +###### Launch cbuild/default_tasks ####### +exec: build_exec + @cbuild/default_tasks/exec.sh -test_valgrind: build_test_dbg - @build_scripts/build_configurations/test_valgrind.sh +valgrind: build_exec_dbg + @cbuild/default_tasks/valgrind.sh -all: build_test +all: build_exec From 5ceb60a581961a7d4a63717dd0a40827726c4935 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 9 Sep 2022 19:10:49 +0600 Subject: [PATCH 28/59] .vscode --- .gitignore | 1 - .vscode/.gitignore | 1 + .vscode/launch.json | 34 +++++++++++++++++++++++++++ .vscode/tasks.json | 57 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 .vscode/.gitignore create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json diff --git a/.gitignore b/.gitignore index 6e5b49f..d59895f 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,6 @@ obj/ # user files .old*/ .vs/ -.vscode/ .vshistory/ .editorconfig *.user diff --git a/.vscode/.gitignore b/.vscode/.gitignore new file mode 100644 index 0000000..c3d3a20 --- /dev/null +++ b/.vscode/.gitignore @@ -0,0 +1 @@ +settings.json \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..421e47a --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,34 @@ +{ + "version": "0.2.0", + "configurations": [ + { + "name": "(gdb) Debug", + "type": "cppdbg", + "request": "launch", + "preLaunchTask": "build_exec_dbg", + "program": "${workspaceFolder}/bin/kerep.com", + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "externalConsole": true, + "internalConsoleOptions": "openOnSessionStart", + "miDebuggerPath": "/usr/bin/gdb", + "MIMode": "gdb", + "pipeTransport": { + "debuggerPath": "gdb", + "pipeProgram": "bash", + "pipeArgs": ["-c"], + "pipeCwd": "${workspaceFolder}" + } + }, + { + "name": "(msvc) Debug", + "type": "cppvsdbg", + "request": "launch", + "preLaunchTask": "build_dbg", + "cwd": "${workspaceFolder}\\bin", + "program": "${workspaceFolder}\\bin\\kerep.com", + "stopAtEntry": false, + "console": "integratedTerminal" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..7fb803b --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,57 @@ + +{ + + "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", + "type": "cppbuild", + "command": "make", + "args": [ + "build_exec_dbg" + ], + "options": { + "cwd": "${workspaceFolder}" + }, + "problemMatcher": ["$gcc"], + "group": { + "kind": "build" + }, + "presentation": { + "echo": true, + "reveal": "always", + "focus": true, + "panel": "shared", + "showReuseMessage": false, + "clear": true + } + } + ] + } \ No newline at end of file From 5b7f74b445653198390b33f99319051cfe3b79dd Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 9 Sep 2022 19:55:14 +0600 Subject: [PATCH 29/59] chmod_scripts moved into submodule --- cbuild | 1 + chmod_scripts.sh | 7 ------- 2 files changed, 1 insertion(+), 7 deletions(-) create mode 160000 cbuild delete mode 100644 chmod_scripts.sh diff --git a/cbuild b/cbuild new file mode 160000 index 0000000..3152566 --- /dev/null +++ b/cbuild @@ -0,0 +1 @@ +Subproject commit 315256694e1564200213b388170384cede255838 diff --git a/chmod_scripts.sh b/chmod_scripts.sh deleted file mode 100644 index ec71ae9..0000000 --- a/chmod_scripts.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash -SCRIPTS="$(find build_scripts -name '*.sh')" -for F in $SCRIPTS -do - echo $F - chmod +x $F -done From a7c75cfda21ec31f3eebc8ea415aab86a8410682 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 9 Sep 2022 20:28:15 +0600 Subject: [PATCH 30/59] type system readme --- src/base/type_system/README.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/base/type_system/README.md b/src/base/type_system/README.md index e69de29..843a227 100644 --- a/src/base/type_system/README.md +++ b/src/base/type_system/README.md @@ -0,0 +1,30 @@ +# kerep type system + +For using some kerep capabilities, such as generic structs, unitype, and kprint, types should be *registered*. + +## type id + +Every registered type has its own id (`ktId`), which should be declared in header file and defined in source file. +Example: +```c +//someStruct.h +typedef struct { } someStruct; +ktId_declare(ktId_someStruct); +ktId_declare(ktId_someStructPtr); // pointer to type is another type +``` +```c +//someStruct.c +ktId_define(ktId_someStruct); +ktId_define(ktId_someStructPtr); +``` + +## type descriptors + +Every registered type should have it's own descriptor (`ktDescriptor`). It's a struct, which contains some information about type and pointers to some specific functions for this type (`toString`, `freeMembers`). + +## 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()`. +Examples: ++ [ktDescriptors_initKerepTypes()](src/base/type_system/init.c) ++ [kerep types registration](tests/main.cpp) From 50fea51a3e8bb8f05ad78627f16c9057923147fe Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 9 Sep 2022 20:43:30 +0600 Subject: [PATCH 31/59] cbuild updated --- cbuild | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cbuild b/cbuild index 3152566..8817640 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit 315256694e1564200213b388170384cede255838 +Subproject commit 881764061a019e9484970e530cb437a892f99007 From dbbfb64820d59fb7458c642818673ad90c506228 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 9 Sep 2022 22:12:39 +0600 Subject: [PATCH 32/59] cbuild updated --- Makefile | 16 ++++++++-------- cbuild | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Makefile b/Makefile index c00ea21..c61ed41 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,24 @@ ###### Build cbuild/default_tasks ####### build_exec: - @cbuild/default_tasks/build_exec.sh + @cbuild/call_task.sh build_exec build_exec_dbg: - @cbuild/default_tasks/build_exec_dbg.sh + @cbuild/call_task.sh build_exec_dbg build_shared_lib: - @cbuild/default_tasks/build_shared_lib.sh + @cbuild/call_task.sh build_shared_lib build_shared_lib_dbg: - @cbuild/default_tasks/build_shared_lib_dbg.sh + @cbuild/call_task.sh build_shared_lib_dbg build_static_lib: - @cbuild/default_tasks/build_static_lib.sh + @cbuild/call_task.sh build_static_lib build_static_lib_dbg: - @cbuild/default_tasks/build_static_lib_dbg.sh + @cbuild/call_task.sh build_static_lib_dbg ###### Launch cbuild/default_tasks ####### exec: build_exec - @cbuild/default_tasks/exec.sh + @cbuild/call_task.sh exec valgrind: build_exec_dbg - @cbuild/default_tasks/valgrind.sh + @cbuild/call_task.sh valgrind all: build_exec diff --git a/cbuild b/cbuild index 8817640..ff04da5 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit 881764061a019e9484970e530cb437a892f99007 +Subproject commit ff04da5c198cea82a7948eb882e3cc96c7b5686c From 0f3c1227fe79f06361bd8ebd000ba999bef2242c Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 9 Sep 2022 22:25:08 +0600 Subject: [PATCH 33/59] default.config --- default.config | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 default.config diff --git a/default.config b/default.config new file mode 100644 index 0000000..66a7fe1 --- /dev/null +++ b/default.config @@ -0,0 +1,66 @@ +#!/bin/bash +CONFIG_VER=2 + +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')" +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 + +PRE_BUILD_SCRIPT=tasks/pre_build.sh + +case $TASK in + build_exec) + C_ARGS="-O2" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="" + TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + ;; + build_exec_dbg) + C_ARGS="-O0 -g" + CPP_ARGS="$C_ARGS" + LINKER_ARGS="" + TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + ;; + 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 + ;; + 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 + ;; + build_static_lib) + C_ARGS="-O2 -fpic" + CPP_ARGS="$C_ARGS" + TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh + ;; + build_static_lib_dbg) + C_ARGS="-O0 -g" + CPP_ARGS="$C_ARGS" + TASK_SCRIPT=cbuild/default_tasks/build_static_lib.sh + ;; + exec) + TASK_SCRIPT=cbuild/default_tasks/exec.sh + ;; + 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 From 4291156fd8b07aa1b068fd9e602f777db6f1e7d8 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 9 Sep 2022 23:04:54 +0600 Subject: [PATCH 34/59] type_system.h --- src/base/type_system/type_system.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/base/type_system/type_system.h b/src/base/type_system/type_system.h index f685112..dca30d9 100644 --- a/src/base/type_system/type_system.h +++ b/src/base/type_system/type_system.h @@ -1,4 +1,6 @@ #include "init.h" +#include "ktId.h" +#include "ktDescriptor.h" #include "kt_functions.h" #include "unitype.h" \ No newline at end of file From b1f5fefdaeff1f5bfe7025ad944fd1e5d0ecedc0 Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 21 Sep 2022 00:59:16 +0600 Subject: [PATCH 35/59] cbuild updated --- cbuild | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cbuild b/cbuild index ff04da5..b514eda 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit ff04da5c198cea82a7948eb882e3cc96c7b5686c +Subproject commit b514eda08d1db643ec5025be5819e0dfeaf1ef6c From cbcd48ae66e3135a4ec4b1eab75dddfb31c5373f Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 21 Sep 2022 01:15:50 +0600 Subject: [PATCH 36/59] todo.md removed --- !TODO.md | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 !TODO.md diff --git a/!TODO.md b/!TODO.md deleted file mode 100644 index 3a6bbe1..0000000 --- a/!TODO.md +++ /dev/null @@ -1,3 +0,0 @@ -- add toString() to type descriptor -- find better way to check typeId without hundreds of if() statements -- update autoarr to use type descriptor \ No newline at end of file From d5a60953c3d18b50b00db86577dc75685256d7fd Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 21 Sep 2022 01:16:32 +0600 Subject: [PATCH 37/59] ktId_declare --- src/Autoarr/Autoarr.c | 52 +++++++++++++-------------- src/Autoarr/Autoarr.h | 52 +++++++++++++-------------- src/Hashtable/Hashtable.c | 4 +-- src/Hashtable/Hashtable.h | 4 +-- src/Hashtable/KeyValuePair.c | 8 ++--- src/Hashtable/KeyValuePair.h | 8 ++--- src/SearchTree/SearchTree.c | 4 +-- src/SearchTree/SearchTree.h | 4 +-- src/String/StringBuilder.c | 8 ++--- src/String/StringBuilder.h | 8 ++--- src/String/string.c | 4 +-- src/String/string.h | 4 +-- src/base/type_system/README.md | 8 ++--- src/base/type_system/ktId.h | 8 ++--- src/base/type_system/kt_functions.c | 54 ++++++++++++++--------------- src/base/type_system/kt_functions.h | 54 ++++++++++++++--------------- src/base/type_system/type_system.h | 3 +- src/base/type_system/unitype.c | 4 +-- src/base/type_system/unitype.h | 4 +-- 19 files changed, 147 insertions(+), 148 deletions(-) diff --git a/src/Autoarr/Autoarr.c b/src/Autoarr/Autoarr.c index a87502c..c72a299 100644 --- a/src/Autoarr/Autoarr.c +++ b/src/Autoarr/Autoarr.c @@ -13,35 +13,35 @@ Autoarr_define(int32) Autoarr_define(uint64) Autoarr_define(int64) -ktId_define(ktId_AutoarrChar); -ktId_define(ktId_AutoarrBool); -ktId_define(ktId_AutoarrFloat32); -ktId_define(ktId_AutoarrFloat64); -ktId_define(ktId_AutoarrInt8); -ktId_define(ktId_AutoarrUInt8); -ktId_define(ktId_AutoarrInt16); -ktId_define(ktId_AutoarrUInt16); -ktId_define(ktId_AutoarrInt32); -ktId_define(ktId_AutoarrUInt32); -ktId_define(ktId_AutoarrInt64); -ktId_define(ktId_AutoarrUInt64); +ktId_define(AutoarrChar); +ktId_define(AutoarrBool); +ktId_define(AutoarrFloat32); +ktId_define(AutoarrFloat64); +ktId_define(AutoarrInt8); +ktId_define(AutoarrUInt8); +ktId_define(AutoarrInt16); +ktId_define(AutoarrUInt16); +ktId_define(AutoarrInt32); +ktId_define(AutoarrUInt32); +ktId_define(AutoarrInt64); +ktId_define(AutoarrUInt64); -ktId_define(ktId_AutoarrCharPtr); -ktId_define(ktId_AutoarrBoolPtr); -ktId_define(ktId_AutoarrFloat32Ptr); -ktId_define(ktId_AutoarrFloat64Ptr); -ktId_define(ktId_AutoarrInt8Ptr); -ktId_define(ktId_AutoarrUInt8Ptr); -ktId_define(ktId_AutoarrInt16Ptr); -ktId_define(ktId_AutoarrUInt16Ptr); -ktId_define(ktId_AutoarrInt32Ptr); -ktId_define(ktId_AutoarrUInt32Ptr); -ktId_define(ktId_AutoarrInt64Ptr); -ktId_define(ktId_AutoarrUInt64Ptr); +ktId_define(AutoarrCharPtr); +ktId_define(AutoarrBoolPtr); +ktId_define(AutoarrFloat32Ptr); +ktId_define(AutoarrFloat64Ptr); +ktId_define(AutoarrInt8Ptr); +ktId_define(AutoarrUInt8Ptr); +ktId_define(AutoarrInt16Ptr); +ktId_define(AutoarrUInt16Ptr); +ktId_define(AutoarrInt32Ptr); +ktId_define(AutoarrUInt32Ptr); +ktId_define(AutoarrInt64Ptr); +ktId_define(AutoarrUInt64Ptr); Autoarr_define(Unitype) -ktId_define(ktId_AutoarrUnitype); -ktId_define(ktId_AutoarrUnitypePtr); +ktId_define(AutoarrUnitype); +ktId_define(AutoarrUnitypePtr); // right func to clear array of unitype values void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr){ diff --git a/src/Autoarr/Autoarr.h b/src/Autoarr/Autoarr.h index be32c5c..3a953c0 100644 --- a/src/Autoarr/Autoarr.h +++ b/src/Autoarr/Autoarr.h @@ -20,35 +20,35 @@ Autoarr_declare(uint32) Autoarr_declare(int64) Autoarr_declare(uint64) -ktId_declare(ktId_AutoarrChar); -ktId_declare(ktId_AutoarrBool); -ktId_declare(ktId_AutoarrFloat32); -ktId_declare(ktId_AutoarrFloat64); -ktId_declare(ktId_AutoarrInt8); -ktId_declare(ktId_AutoarrUInt8); -ktId_declare(ktId_AutoarrInt16); -ktId_declare(ktId_AutoarrUInt16); -ktId_declare(ktId_AutoarrInt32); -ktId_declare(ktId_AutoarrUInt32); -ktId_declare(ktId_AutoarrInt64); -ktId_declare(ktId_AutoarrUInt64); +ktId_declare(AutoarrChar); +ktId_declare(AutoarrBool); +ktId_declare(AutoarrFloat32); +ktId_declare(AutoarrFloat64); +ktId_declare(AutoarrInt8); +ktId_declare(AutoarrUInt8); +ktId_declare(AutoarrInt16); +ktId_declare(AutoarrUInt16); +ktId_declare(AutoarrInt32); +ktId_declare(AutoarrUInt32); +ktId_declare(AutoarrInt64); +ktId_declare(AutoarrUInt64); -ktId_declare(ktId_AutoarrCharPtr); -ktId_declare(ktId_AutoarrBoolPtr); -ktId_declare(ktId_AutoarrFloat32Ptr); -ktId_declare(ktId_AutoarrFloat64Ptr); -ktId_declare(ktId_AutoarrInt8Ptr); -ktId_declare(ktId_AutoarrUInt8Ptr); -ktId_declare(ktId_AutoarrInt16Ptr); -ktId_declare(ktId_AutoarrUInt16Ptr); -ktId_declare(ktId_AutoarrInt32Ptr); -ktId_declare(ktId_AutoarrUInt32Ptr); -ktId_declare(ktId_AutoarrInt64Ptr); -ktId_declare(ktId_AutoarrUInt64Ptr); +ktId_declare(AutoarrCharPtr); +ktId_declare(AutoarrBoolPtr); +ktId_declare(AutoarrFloat32Ptr); +ktId_declare(AutoarrFloat64Ptr); +ktId_declare(AutoarrInt8Ptr); +ktId_declare(AutoarrUInt8Ptr); +ktId_declare(AutoarrInt16Ptr); +ktId_declare(AutoarrUInt16Ptr); +ktId_declare(AutoarrInt32Ptr); +ktId_declare(AutoarrUInt32Ptr); +ktId_declare(AutoarrInt64Ptr); +ktId_declare(AutoarrUInt64Ptr); Autoarr_declare(Unitype) -ktId_declare(ktId_AutoarrUnitype); -ktId_declare(ktId_AutoarrUnitypePtr); +ktId_declare(AutoarrUnitype); +ktId_declare(AutoarrUnitypePtr); // this function is injected in kerep_init() void __Autoarr_free_Unitype_(Autoarr(Unitype)* ar, bool freePtr); diff --git a/src/Hashtable/Hashtable.c b/src/Hashtable/Hashtable.c index e5eb77d..9a01ba4 100644 --- a/src/Hashtable/Hashtable.c +++ b/src/Hashtable/Hashtable.c @@ -1,7 +1,7 @@ #include "Hashtable.h" -ktId_define(ktId_Hashtable); -ktId_define(ktId_HashtablePtr); +ktId_define(Hashtable); +ktId_define(HashtablePtr); // amount of rows static const uint16 HT_HEIGHTS[]={17,61,257,1021,4099,16381,65521}; diff --git a/src/Hashtable/Hashtable.h b/src/Hashtable/Hashtable.h index 9437ca2..520c2e4 100644 --- a/src/Hashtable/Hashtable.h +++ b/src/Hashtable/Hashtable.h @@ -11,8 +11,8 @@ typedef struct Hashtable{ uint8 hein; // height=HT_HEIGHTS[hein] Autoarr(KVPair)** rows; // Autoarr[height] } Hashtable; -ktId_declare(ktId_Hashtable); -ktId_declare(ktId_HashtablePtr); +ktId_declare(Hashtable); +ktId_declare(HashtablePtr); Hashtable* Hashtable_create(); void Hashtable_free(Hashtable* ht); diff --git a/src/Hashtable/KeyValuePair.c b/src/Hashtable/KeyValuePair.c index cb4a2d0..07b3edc 100644 --- a/src/Hashtable/KeyValuePair.c +++ b/src/Hashtable/KeyValuePair.c @@ -1,11 +1,11 @@ #include "KeyValuePair.h" -ktId_define(ktId_KVPair); -ktId_define(ktId_KVPairPtr); +ktId_define(KVPair); +ktId_define(KVPairPtr); Autoarr_define(KVPair) -ktId_define(ktId_AutoarrKVPair); -ktId_define(ktId_AutoarrKVPairPtr); +ktId_define(AutoarrKVPair); +ktId_define(AutoarrKVPairPtr); // proper way to clear a KVP void KVPair_free(KVPair p){ diff --git a/src/Hashtable/KeyValuePair.h b/src/Hashtable/KeyValuePair.h index 666b839..ef6a32b 100644 --- a/src/Hashtable/KeyValuePair.h +++ b/src/Hashtable/KeyValuePair.h @@ -11,12 +11,12 @@ typedef struct KVPair{ char* key; Unitype value; } KVPair; -ktId_declare(ktId_KVPair); -ktId_declare(ktId_KVPairPtr); +ktId_declare(KVPair); +ktId_declare(KVPairPtr); Autoarr_declare(KVPair) -ktId_declare(ktId_AutoarrKVPair); -ktId_declare(ktId_AutoarrKVPairPtr); +ktId_declare(AutoarrKVPair); +ktId_declare(AutoarrKVPairPtr); // proper way to clear a KVP void KVPair_free(KVPair p); diff --git a/src/SearchTree/SearchTree.c b/src/SearchTree/SearchTree.c index c174da9..29e772d 100644 --- a/src/SearchTree/SearchTree.c +++ b/src/SearchTree/SearchTree.c @@ -1,7 +1,7 @@ #include "SearchTree.h" -ktId_define(ktId_STNode); -ktId_define(ktId_STNodePtr); +ktId_define(STNode); +ktId_define(STNodePtr); STNode* STNode_create(){ STNode* node=malloc(sizeof(STNode)); diff --git a/src/SearchTree/SearchTree.h b/src/SearchTree/SearchTree.h index 6ae9e74..a11de86 100644 --- a/src/SearchTree/SearchTree.h +++ b/src/SearchTree/SearchTree.h @@ -11,8 +11,8 @@ typedef struct SearchTreeNode{ struct SearchTreeNode**** branches; // *STNode[8][8][4] Unitype value; } STNode; -ktId_declare(ktId_STNode); -ktId_declare(ktId_STNodePtr); +ktId_declare(STNode); +ktId_declare(STNodePtr); STNode* STNode_create(); void STNode_free(STNode* node); diff --git a/src/String/StringBuilder.c b/src/String/StringBuilder.c index 461f45d..8f7cff5 100644 --- a/src/String/StringBuilder.c +++ b/src/String/StringBuilder.c @@ -1,11 +1,11 @@ #include "StringBuilder.h" Autoarr_define(string) -ktId_define(ktId_AutoarrString); -ktId_define(ktId_AutoarrStringPtr); +ktId_define(AutoarrString); +ktId_define(AutoarrStringPtr); -ktId_define(ktId_StringBuilder); -ktId_define(ktId_StringBuilderPtr); +ktId_define(StringBuilder); +ktId_define(StringBuilderPtr); #define BL_C 32 #define BL_L 1024 diff --git a/src/String/StringBuilder.h b/src/String/StringBuilder.h index 7cb5d91..a5f13ef 100644 --- a/src/String/StringBuilder.h +++ b/src/String/StringBuilder.h @@ -8,15 +8,15 @@ extern "C" { #include "string.h" Autoarr_declare(string) -ktId_declare(ktId_AutoarrString); -ktId_declare(ktId_AutoarrStringPtr); +ktId_declare(AutoarrString); +ktId_declare(AutoarrStringPtr); typedef struct StringBuilder{ Autoarr(string)* compl_bufs; Autoarr(int8)* curr_buf; } StringBuilder; -ktId_declare(ktId_StringBuilder); -ktId_declare(ktId_StringBuilderPtr); +ktId_declare(StringBuilder); +ktId_declare(StringBuilderPtr); StringBuilder* StringBuilder_create(void); void StringBuilder_free(StringBuilder* b); diff --git a/src/String/string.c b/src/String/string.c index cbcb46e..93c0ed9 100644 --- a/src/String/string.c +++ b/src/String/string.c @@ -1,7 +1,7 @@ #include "string.h" -ktId_define(ktId_string); -ktId_define(ktId_stringPtr); +ktId_define(string); +ktId_define(stringPtr); // 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 9313dae..caf5e73 100644 --- a/src/String/string.h +++ b/src/String/string.h @@ -12,8 +12,8 @@ typedef struct string{ char* ptr; // char pointer uint32 length; // amount of chars in ptr value } string; -ktId_declare(ktId_string); -ktId_declare(ktId_stringPtr); +ktId_declare(string); +ktId_declare(stringPtr); static const string stringNull={NULL,0}; diff --git a/src/base/type_system/README.md b/src/base/type_system/README.md index 843a227..587291c 100644 --- a/src/base/type_system/README.md +++ b/src/base/type_system/README.md @@ -9,13 +9,13 @@ Example: ```c //someStruct.h typedef struct { } someStruct; -ktId_declare(ktId_someStruct); -ktId_declare(ktId_someStructPtr); // pointer to type is another type +ktId_declare(someStruct); +ktId_declare(someStructPtr); // pointer to type is another type ``` ```c //someStruct.c -ktId_define(ktId_someStruct); -ktId_define(ktId_someStructPtr); +ktId_define(someStruct); +ktId_define(someStructPtr); ``` ## type descriptors diff --git a/src/base/type_system/ktId.h b/src/base/type_system/ktId.h index 8c37985..63f7847 100644 --- a/src/base/type_system/ktId.h +++ b/src/base/type_system/ktId.h @@ -7,10 +7,10 @@ extern "C" { #include "../std.h" typedef uint16 ktId; -#define ktId_declare(ID_VAR_NAME)\ - extern ktId ID_VAR_NAME -#define ktId_define(ID_VAR_NAME)\ - ktId ID_VAR_NAME=-1 +#define ktId_declare(TYPE_NAME)\ + extern ktId ktId_##TYPE_NAME +#define ktId_define(TYPE_NAME)\ + ktId ktId_##TYPE_NAME=-1 #if __cplusplus } diff --git a/src/base/type_system/kt_functions.c b/src/base/type_system/kt_functions.c index 0bc4f7d..c183ff0 100644 --- a/src/base/type_system/kt_functions.c +++ b/src/base/type_system/kt_functions.c @@ -3,36 +3,36 @@ Autoarr_declare(ktDescriptor) Autoarr_define(ktDescriptor) -ktId_define(ktId_Null); +ktId_define(Null); -ktId_define(ktId_Char); -ktId_define(ktId_Bool); -ktId_define(ktId_Float32); -ktId_define(ktId_Float64); -ktId_define(ktId_Int8); -ktId_define(ktId_UInt8); -ktId_define(ktId_Int16); -ktId_define(ktId_UInt16); -ktId_define(ktId_Int32); -ktId_define(ktId_UInt32); -ktId_define(ktId_Int64); -ktId_define(ktId_UInt64); +ktId_define(Char); +ktId_define(Bool); +ktId_define(Float32); +ktId_define(Float64); +ktId_define(Int8); +ktId_define(UInt8); +ktId_define(Int16); +ktId_define(UInt16); +ktId_define(Int32); +ktId_define(UInt32); +ktId_define(Int64); +ktId_define(UInt64); -ktId_define(ktId_CharPtr); -ktId_define(ktId_BoolPtr); -ktId_define(ktId_Float32Ptr); -ktId_define(ktId_Float64Ptr); -ktId_define(ktId_Int8Ptr); -ktId_define(ktId_UInt8Ptr); -ktId_define(ktId_Int16Ptr); -ktId_define(ktId_UInt16Ptr); -ktId_define(ktId_Int32Ptr); -ktId_define(ktId_UInt32Ptr); -ktId_define(ktId_Int64Ptr); -ktId_define(ktId_UInt64Ptr); +ktId_define(CharPtr); +ktId_define(BoolPtr); +ktId_define(Float32Ptr); +ktId_define(Float64Ptr); +ktId_define(Int8Ptr); +ktId_define(UInt8Ptr); +ktId_define(Int16Ptr); +ktId_define(UInt16Ptr); +ktId_define(Int32Ptr); +ktId_define(UInt32Ptr); +ktId_define(Int64Ptr); +ktId_define(UInt64Ptr); -ktId_define(ktId_ktDescriptor); -ktId_define(ktId_ktDescriptorPtr); +ktId_define(ktDescriptor); +ktId_define(ktDescriptorPtr); // type descriptors are stored here during initialization Autoarr(ktDescriptor)* __ktDescriptors=NULL; diff --git a/src/base/type_system/kt_functions.h b/src/base/type_system/kt_functions.h index 2f13b74..b1e588f 100644 --- a/src/base/type_system/kt_functions.h +++ b/src/base/type_system/kt_functions.h @@ -23,36 +23,36 @@ void ktDescriptors_endInit(); Maybe ktDescriptor_get(ktId id); -ktId_declare(ktId_Null); +ktId_declare(Null); -ktId_declare(ktId_Char); -ktId_declare(ktId_Bool); -ktId_declare(ktId_Float32); -ktId_declare(ktId_Float64); -ktId_declare(ktId_Int8); -ktId_declare(ktId_UInt8); -ktId_declare(ktId_Int16); -ktId_declare(ktId_UInt16); -ktId_declare(ktId_Int32); -ktId_declare(ktId_UInt32); -ktId_declare(ktId_Int64); -ktId_declare(ktId_UInt64); +ktId_declare(Char); +ktId_declare(Bool); +ktId_declare(Float32); +ktId_declare(Float64); +ktId_declare(Int8); +ktId_declare(UInt8); +ktId_declare(Int16); +ktId_declare(UInt16); +ktId_declare(Int32); +ktId_declare(UInt32); +ktId_declare(Int64); +ktId_declare(UInt64); -ktId_declare(ktId_CharPtr); -ktId_declare(ktId_BoolPtr); -ktId_declare(ktId_Float32Ptr); -ktId_declare(ktId_Float64Ptr); -ktId_declare(ktId_Int8Ptr); -ktId_declare(ktId_UInt8Ptr); -ktId_declare(ktId_Int16Ptr); -ktId_declare(ktId_UInt16Ptr); -ktId_declare(ktId_Int32Ptr); -ktId_declare(ktId_UInt32Ptr); -ktId_declare(ktId_Int64Ptr); -ktId_declare(ktId_UInt64Ptr); +ktId_declare(CharPtr); +ktId_declare(BoolPtr); +ktId_declare(Float32Ptr); +ktId_declare(Float64Ptr); +ktId_declare(Int8Ptr); +ktId_declare(UInt8Ptr); +ktId_declare(Int16Ptr); +ktId_declare(UInt16Ptr); +ktId_declare(Int32Ptr); +ktId_declare(UInt32Ptr); +ktId_declare(Int64Ptr); +ktId_declare(UInt64Ptr); -ktId_declare(ktId_ktDescriptor); -ktId_declare(ktId_ktDescriptorPtr); +ktId_declare(ktDescriptor); +ktId_declare(ktDescriptorPtr); #if __cplusplus } diff --git a/src/base/type_system/type_system.h b/src/base/type_system/type_system.h index dca30d9..2c20ac0 100644 --- a/src/base/type_system/type_system.h +++ b/src/base/type_system/type_system.h @@ -1,6 +1,5 @@ - #include "init.h" #include "ktId.h" #include "ktDescriptor.h" #include "kt_functions.h" -#include "unitype.h" \ No newline at end of file +#include "unitype.h" diff --git a/src/base/type_system/unitype.c b/src/base/type_system/unitype.c index f727fc7..d4115cd 100644 --- a/src/base/type_system/unitype.c +++ b/src/base/type_system/unitype.c @@ -1,7 +1,7 @@ #include "../base.h" -ktId_define(ktId_Unitype); -ktId_define(ktId_UnitypePtr); +ktId_define(Unitype); +ktId_define(UnitypePtr); void Unitype_free(Unitype u){ tryLast(ktDescriptor_get(u.typeId), mType); diff --git a/src/base/type_system/unitype.h b/src/base/type_system/unitype.h index 2fbbdf6..666ee3e 100644 --- a/src/base/type_system/unitype.h +++ b/src/base/type_system/unitype.h @@ -18,8 +18,8 @@ typedef struct Unitype{ ktId typeId; bool allocatedInHeap; // should Unitype_free call free() to VoidPtr* } Unitype; -ktId_declare(ktId_Unitype); -ktId_declare(ktId_UnitypePtr); +ktId_declare(Unitype); +ktId_declare(UnitypePtr); #define __UniDef(TYPE, VAL) (Unitype){\ From a0a35b30a6b53fcb5f33be5b55c5e4dc9477e01b Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 28 Sep 2022 22:07:17 +0600 Subject: [PATCH 38/59] base_toString --- src/base/cptr.c | 40 ++++++- src/base/cptr.h | 5 + src/base/errors.h | 2 +- src/base/kprint/kprint.c | 6 +- src/base/kprint/kprint.h | 65 +++++------ src/base/kprint/kprint_format.h | 23 ++-- src/base/std.h | 14 +++ src/base/type_system/base_toString.c | 156 +++++++++++++++++++++++++++ src/base/type_system/base_toString.h | 32 ++++++ src/base/type_system/init.c | 1 + src/base/type_system/ktDescriptor.h | 4 +- src/base/type_system/kt_functions.c | 11 +- src/base/type_system/kt_functions.h | 8 +- src/base/type_system/unitype.c | 19 +++- 14 files changed, 316 insertions(+), 70 deletions(-) create mode 100644 src/base/type_system/base_toString.c create mode 100644 src/base/type_system/base_toString.h diff --git a/src/base/cptr.c b/src/base/cptr.c index a4080fe..cba9814 100644 --- a/src/base/cptr.c +++ b/src/base/cptr.c @@ -46,4 +46,42 @@ bool cptr_endsWith(char* ptr, char* fragment){ for(char cs=*ptr, cf=*fragment; cf; cs=*++ptr, cf=*++fragment) if(cs!=cf) return false; return true; -} \ No newline at end of file +} + +void memcopy(void* from, void* to, uint32 size){ + if(from==NULL || to==NULL) + throw(ERR_NULLPTR); + for(uint32 i=0; i>63) + str[--i]='-'; + return cptr_copy((char*)str+i); +} + +char* toString_uint(uint64 n, bool withPostfix, bool uppercase){ + uint64 d=n; + char str[32]; + uint8 i=sizeof(str); + str[--i]=0; + if(withPostfix) + str[--i]= uppercase ? 'U' : 'u'; + while(d!=0){ + str[--i]='0' + d%10; + d/=10; + } + return cptr_copy((char*)str+i); +} + +char* toString_float(float64 n, bool withPostfix, bool uppercase){ + int64 d=n; + float64 r=n-d; + char* strint=toString_int(d); + char strfract[32]; + uint8 i=0; + strfract[i++]='.'; + while(r!=0){ + r*=10.0; + char fc=r; + strfract[i++]=fc; + r-=fc; + } + if(withPostfix) + strfract[i++]= uppercase ? 'F' : 'f'; + strfract[i]=0; + char* str=cptr_concat(strint, strfract); + free(strint); + return str; +} + +char* toString_bin(char* bytes, uint32 size, bool withPrefix){ + char* str=malloc(size*8+1); + uint32 cn=0; + if(withPrefix){ + str[cn++]='0'; + str[cn++]='b'; + } + for(uint32 bn=0; bn>i); + } + str[cn]=0; + return str; +} + +char _4bitsHex(uint8 u, bool uppercase){ + switch(u){ + case 0: case 1: case 2: case 3: case 4: + case 5: case 6: case 7: case 8: case 9: + return '0'+u; + case 0xA: case 0xB: case 0xC: + case 0xD: case 0xE: case 0xF: + return (uppercase ? 'A'-10 : 'a'-10) + u; + default: return 219; + } +} + +char* toString_hex(char* bytes, uint32 size, bool withPrefix, bool uppercase){ + char* str=malloc(size*2); + uint32 cn=0; + if(withPrefix){ + str[cn++]='0'; + str[cn++]='x'; + } + for(uint32 bn=0; bn - Maybe (*toString)(void*, int32); // NULL or function which generates string representaion of object + char* (*toString)(void* obj, int32 fmt); // 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 c183ff0..1f85eea 100644 --- a/src/base/type_system/kt_functions.c +++ b/src/base/type_system/kt_functions.c @@ -58,7 +58,7 @@ void ktDescriptors_endInit(){ printf("\e[92minitialized %u type descriptors\n", ktId_last); } -void __kt_register(char* name, int16 size, void (*freeMembers)(void*), Maybe (*toString)(void*, int32)){ +void __kt_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, int32)){ ktDescriptor typeDesc={ .name=name, .size=size, @@ -69,7 +69,10 @@ void __kt_register(char* name, int16 size, void (*freeMembers)(void*), Maybe (*t Autoarr_add(__ktDescriptors, typeDesc); } -Maybe ktDescriptor_get(ktId id){ - if(id>ktId_last) safethrow("invalid type id",;); - return SUCCESS(UniStack(ktId_ktDescriptorPtr, &typeDescriptors[id])); +ktDescriptor ktDescriptor_get(ktId id){ + if(id>ktId_last) { + printf("\n%u\n",id); + throw("invalid type id"); + } + return typeDescriptors[id]; } diff --git a/src/base/type_system/kt_functions.h b/src/base/type_system/kt_functions.h index b1e588f..cdf5359 100644 --- a/src/base/type_system/kt_functions.h +++ b/src/base/type_system/kt_functions.h @@ -10,7 +10,7 @@ extern "C" { extern ktId ktId_last; -void __kt_register(char* name, int16 size, void (*freeMembers)(void*), Maybe (*toString)(void*, int32)); +void __kt_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, int32)); #define kt_register(TYPE, ID_VAR_NAME, FREE_MEMBERS_FUNC, TO_STRING_FUNC)\ __kt_register(#ID_VAR_NAME, sizeof(TYPE), FREE_MEMBERS_FUNC, TO_STRING_FUNC);\ @@ -18,9 +18,9 @@ void __kt_register(char* name, int16 size, void (*freeMembers)(void*), Maybe (*t void ktDescriptors_beginInit(); void ktDescriptors_endInit(); -/// @param id id of registered type -/// @return Maybe -Maybe ktDescriptor_get(ktId id); + +/// @param id id of registered type +ktDescriptor ktDescriptor_get(ktId id); ktId_declare(Null); diff --git a/src/base/type_system/unitype.c b/src/base/type_system/unitype.c index d4115cd..7bf9f1b 100644 --- a/src/base/type_system/unitype.c +++ b/src/base/type_system/unitype.c @@ -4,8 +4,7 @@ ktId_define(Unitype); ktId_define(UnitypePtr); void Unitype_free(Unitype u){ - tryLast(ktDescriptor_get(u.typeId), mType); - ktDescriptor type=*(ktDescriptor*)mType.value.VoidPtr; + ktDescriptor type=ktDescriptor_get(u.typeId); if(type.freeMembers) type.freeMembers(u.VoidPtr); if(u.allocatedInHeap) @@ -13,11 +12,23 @@ void Unitype_free(Unitype u){ } void __UnitypePtr_free(void* u) { Unitype_free(*(Unitype*)u); } +char* toString_Unitype(void* _u, int32 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"), + ", value:", valuestr, " }"); + free(valuestr); + return rezult; +} + + + #define BUFSIZE 64 char* sprintuni(Unitype v){ char* buf=malloc(BUFSIZE); - tryLast(ktDescriptor_get(v.typeId), mType); - ktDescriptor type=*(ktDescriptor*)mType.value.VoidPtr; + ktDescriptor type=ktDescriptor_get(v.typeId); if(v.typeId==ktId_Null) sprintf_s(buf, BUFSIZE, "{Null}"); else if(v.typeId==ktId_Float64) From 3b00d60907d27d72193281dec4f189eb7aa3c95a Mon Sep 17 00:00:00 2001 From: timerix Date: Mon, 3 Oct 2022 11:57:45 +0600 Subject: [PATCH 39/59] cbuild updated --- .gitignore | 2 +- cbuild | 2 +- default.config | 17 ++++++++++++++--- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index d59895f..cab1d1a 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,4 @@ obj/ .editorconfig *.user *.vcxproj.filters -.config +current.config diff --git a/cbuild b/cbuild index b514eda..bad865d 160000 --- a/cbuild +++ b/cbuild @@ -1 +1 @@ -Subproject commit b514eda08d1db643ec5025be5819e0dfeaf1ef6c +Subproject commit bad865d76c8daf9055d0e82fcd96b5d9c7628cce diff --git a/default.config b/default.config index 66a7fe1..16d1c6b 100644 --- a/default.config +++ b/default.config @@ -1,5 +1,6 @@ #!/bin/bash -CONFIG_VER=2 +CONFIG_VERSION=2 +CBUILD_VERSION=2 PROJECT=kerep CMP_C=gcc @@ -19,42 +20,52 @@ EXEC_FILE=$PROJECT.com SHARED_LIB_FILE=$PROJECT.so STATIC_LIB_FILE=$PROJECT.a -PRE_BUILD_SCRIPT=tasks/pre_build.sh - case $TASK in build_exec) C_ARGS="-O2" CPP_ARGS="$C_ARGS" LINKER_ARGS="" TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + PRE_TASK_SCRIPT=tasks/pre_build.sh + POST_TASK_SCRIPT= ;; build_exec_dbg) C_ARGS="-O0 -g" CPP_ARGS="$C_ARGS" LINKER_ARGS="" TASK_SCRIPT=cbuild/default_tasks/build_exec.sh + PRE_TASK_SCRIPT=tasks/pre_build.sh + POST_TASK_SCRIPT= ;; 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 + PRE_TASK_SCRIPT=tasks/pre_build.sh + POST_TASK_SCRIPT= ;; 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 + PRE_TASK_SCRIPT=tasks/pre_build.sh + 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 + 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 + POST_TASK_SCRIPT= ;; exec) TASK_SCRIPT=cbuild/default_tasks/exec.sh From e9d736e95fdeb0d50546e04501d88180ac4c9870 Mon Sep 17 00:00:00 2001 From: timerix Date: Wed, 5 Oct 2022 20:25:20 +0600 Subject: [PATCH 40/59] autoarr vs vector --- tests/main.cpp | 1 + tests/test_autoarr-vs-vector.cpp | 40 ++++++++++++++++++++++++++++++++ tests/tests.h | 2 ++ 3 files changed, 43 insertions(+) create mode 100644 tests/test_autoarr-vs-vector.cpp diff --git a/tests/main.cpp b/tests/main.cpp index 9136271..39d2405 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -5,6 +5,7 @@ void test_all(){ test_safethrow(); test_searchtree(); test_autoarr(); + test_autoarrVsVector(); test_hash_functions(); test_hashtable(); test_dtsod(); diff --git a/tests/test_autoarr-vs-vector.cpp b/tests/test_autoarr-vs-vector.cpp new file mode 100644 index 0000000..1468463 --- /dev/null +++ b/tests/test_autoarr-vs-vector.cpp @@ -0,0 +1,40 @@ +#include "tests.h" +#include "../src/Autoarr/Autoarr.h" +#include + +int64 _autoarrVsVector(uint16 blockCount, uint16 blockLength){ + uint32 count=blockLength*blockCount; + printf("\e[94mblock count: %u block length: %u count: %llu\n", blockCount, blockLength, (uint64)count); + Autoarr_int64* ar=Autoarr_create(int64, blockCount, blockLength); + std::vector vec=std::vector(); + optime("Autoarr_add", 1, ({ + for(uint32 i=0; i< count; i++) + Autoarr_add(ar, i); + })); + optime("vector_push_back", 1, ({ + for(uint32 i=0; i< count; i++) + vec.push_back(i); + })); + int64 t; + optime("Autoarr_get", 1, ({ + for(uint32 i=0; i< count; i++) + t=Autoarr_get(ar, i); + })); + optime("vector_get", 1, ({ + for(uint32 i=0; i< count; i++) + t=vec[i]; + })); + Autoarr_free(ar, true); + return t; +} + +void test_autoarrVsVector(){ + printf("\e[96m-------[test_autoarr_vs_vector]-------\n"); + _autoarrVsVector(4, 16); + _autoarrVsVector(16, 64); + _autoarrVsVector(32, 32); + _autoarrVsVector(64, 64); + _autoarrVsVector(32, 1024); + _autoarrVsVector(256, 256); + _autoarrVsVector(1024, 1024); +} diff --git a/tests/tests.h b/tests/tests.h index 71cc4b1..584d8c4 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -13,6 +13,8 @@ void test_autoarr(); void test_hash_functions(); void test_hashtable(); void test_dtsod(); +void test_kprint_colors(); +void test_autoarrVsVector(); #define PRINT_SIZEOF(T) printf("\e[94m" #T " size: \e[96m" IFWIN("%llu", "%lu") "\n", sizeof(T)) From 0e6a0f6482d77d521f9fd73d4f6e0f7407c65e73 Mon Sep 17 00:00:00 2001 From: timerix Date: Sun, 23 Oct 2022 17:10:30 +0600 Subject: [PATCH 41/59] some changes in kprint --- src/base/errors.c | 1 + src/base/kprint/README.md | 2 +- src/base/kprint/kprint.c | 123 ++++++++++++++++++++++----- src/base/kprint/kprint.h | 6 +- src/base/kprint/kprint_format.h | 24 +++--- src/base/type_system/base_toString.c | 47 +++++----- src/base/type_system/base_toString.h | 20 ++--- src/base/type_system/ktDescriptor.h | 2 +- src/base/type_system/kt_functions.c | 2 +- src/base/type_system/kt_functions.h | 3 +- src/base/type_system/unitype.c | 2 +- tests/main.cpp | 4 +- tests/test_autoarr-vs-vector.cpp | 4 +- tests/test_kprint_colors.c | 47 ++++++++++ 14 files changed, 205 insertions(+), 82 deletions(-) create mode 100644 tests/test_kprint_colors.c diff --git a/src/base/errors.c b/src/base/errors.c index e3f61a2..5adc0ee 100644 --- a/src/base/errors.c +++ b/src/base/errors.c @@ -12,6 +12,7 @@ char* errname(ErrorId err){ case ERR_NULLPTR: return "ERR_NULLPTR"; case ERR_ENDOFSTR: return "ERR_ENDOFSTR"; case ERR_KEYNOTFOUND: return "ERR_KEYNOTFOUND"; + case ERR_FORMAT: return "ERR_FORMAT"; default: return "UNKNOWN_ERROR"; } } diff --git a/src/base/kprint/README.md b/src/base/kprint/README.md index 6961b00..354d3bd 100644 --- a/src/base/kprint/README.md +++ b/src/base/kprint/README.md @@ -10,7 +10,7 @@ I don't really like printf function (and its variants), so i made safer and more ## how to use it: + **format construction:** ``` - kprint_format fmt= kprint_fgColor | kprint_fbgColor | kprint_fdataFmt | flags | ktId; + kprint_format fmt= kprint_fgColor | kprint_bgColor | kprint_fdataFmt | flags | ktId; ``` [more about `kprint_format`](kprint_format.md) + fgColor and bgColor can be set to change console output color diff --git a/src/base/kprint/kprint.c b/src/base/kprint/kprint.c index f099b69..744fc74 100644 --- a/src/base/kprint/kprint.c +++ b/src/base/kprint/kprint.c @@ -1,29 +1,37 @@ -/* #include "../base.h" +#include "../base.h" #include "../../String/StringBuilder.h" -Maybe __next_toString(kprint_format format, void* object){ - // detecting type - if(!format.typeId) switch((kprint_dataFormat)((uint32)0 | format.dataFmt)){ +ktId __typeFromFormat(kprint_format f){ + ktId typeId=kprint_format_ktId(f); + if(typeId) + return typeId; + switch(kprint_format_dataFormat(f)){ case kprint_fmtInt: case kprint_fmtHex: case kprint_fmtBin: - format.typeId=ktId_Int64; break; + return ktId_Int64; case kprint_fmtUInt: - format.typeId=ktId_UInt64; break; + return ktId_UInt64; case kprint_fmtFloat: - format.typeId=ktId_Float64; break; + return ktId_Float64; case kprint_fmtChar: - format.typeId=ktId_Char; break; + return ktId_Char; case kprint_fmtString: - format.typeId=ktId_CharPtr; break; + return ktId_CharPtr; default: - safethrow("typeId is not set, can't autodetect type",;); + return -1; } - ktDescriptor typeDesc=ktDescriptor_get(format.typeId); +} + +Maybe __next_toString(kprint_format f, void* 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) safethrow("type descriptor doesnt have toString() func",;); - try(typeDesc.toString(object, &format), mStr,;); - return SUCCESS(mStr.value); + return SUCCESS(UniHeap(ktId_CharPtr, typeDesc.toString(object, f))); } Maybe __ksprint(uint8 n, kprint_format* formats, void** objects){ @@ -43,6 +51,7 @@ Maybe __kfprint(FILE* file, uint8 n, kprint_format* formats, void** objects){ safethrow("can't write string to file", Unitype_free(maybeStr.value)); Unitype_free(maybeStr.value); } + fflush(file); return MaybeNull; } @@ -50,24 +59,94 @@ void __kprint(uint8 n, kprint_format* formats, void** objects){ for(uint8 i=0; i +#define FOREGROUND_YELLOW FOREGROUND_GREEN | FOREGROUND_RED + +DWORD kprint_fgColor_toWin(kprint_fgColor f){ + //printf("fg: %x\n", f); + switch(f){ + case kprint_fgBlack: return 0; + case kprint_fgDarkRed: return FOREGROUND_RED; + case kprint_fgDarkGreen: return FOREGROUND_GREEN; + case kprint_fgDarkYellow: return FOREGROUND_GREEN | FOREGROUND_RED; + case kprint_fgDarkBlue: return FOREGROUND_BLUE; + case kprint_fgDarkMagneta: return FOREGROUND_RED | FOREGROUND_BLUE; + case kprint_fgDarkCyan: return FOREGROUND_BLUE | FOREGROUND_GREEN; + case kprint_fgGray: return FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED; + case kprint_fgDarkGray: return FOREGROUND_INTENSITY; + case kprint_fgRed: return FOREGROUND_RED | FOREGROUND_INTENSITY; + case kprint_fgGreen: return FOREGROUND_GREEN | FOREGROUND_INTENSITY; + case kprint_fgYellow: return FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY; + case kprint_fgBlue: return FOREGROUND_BLUE | FOREGROUND_INTENSITY; + case kprint_fgMagneta: return FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY; + case kprint_fgCyan: return FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY; + case kprint_fgWhite: return FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY; + default: throw(ERR_FORMAT); + } +} + +DWORD kprint_bgColor_toWin(kprint_bgColor f){ + //printf("bg: %x\n", f); + switch(f){ + case kprint_bgBlack: return 0; + case kprint_bgDarkRed: return BACKGROUND_RED; + case kprint_bgDarkGreen: return BACKGROUND_GREEN; + case kprint_bgDarkYellow: return BACKGROUND_GREEN | BACKGROUND_RED; + case kprint_bgDarkBlue: return BACKGROUND_BLUE; + case kprint_bgDarkMagneta: return BACKGROUND_RED | BACKGROUND_BLUE; + case kprint_bgDarkCyan: return BACKGROUND_BLUE | BACKGROUND_GREEN; + case kprint_bgGray: return BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED; + case kprint_bgDarkGray: return BACKGROUND_INTENSITY; + case kprint_bgRed: return BACKGROUND_RED | BACKGROUND_INTENSITY; + case kprint_bgGreen: return BACKGROUND_GREEN | BACKGROUND_INTENSITY; + case kprint_bgYellow: return BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY; + case kprint_bgBlue: return BACKGROUND_BLUE | BACKGROUND_INTENSITY; + case kprint_bgMagneta: return BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY; + case kprint_bgCyan: return BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY; + case kprint_bgWhite: return BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY; + default: throw(ERR_FORMAT); + } +} void kprint_setColor(kprint_format f){ - if(!f.bgColorChanged | !f.fgColorChanged) + DWORD color=0; + if(!kprint_format_fgColorChanged(f) & !kprint_format_bgColorChanged(f)) return; + if(kprint_format_fgColorChanged(f)) + color+=kprint_fgColor_toWin(kprint_format_fgColor(f)); + if(kprint_format_bgColorChanged(f)) + color+=kprint_bgColor_toWin(kprint_format_bgColor(f)); + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleTextAttribute(hConsole, color); } +#else +void kprint_setColor(kprint_format f){ + if(kprint_format_fgColorChanged(f)){ + uint8 fg=(f&0x0f000000)>>24; + if(fg<8) fg+=30; + else fg+=90-8; + printf("\e[%um", fg); + } + if(kprint_format_bgColorChanged(f)){ + uint8 bg=(f&0x00f00000)>>20; + if(bg<8) bg+=40; + else bg+=100-8; + printf("\e[%um", bg); + } +} +#endif - - - -Maybe ksprint_ar(uint32 count, kprint_format format, ktId typeId, void* array){ +/* Maybe ksprint_ar(uint32 count, kprint_format format, ktId typeId, void* array){ ktDescriptor typeDesc=ktDescriptor_get(format.typeId); if(!typeDesc.toString) safethrow("type descriptor doesnt have toString() func",;); @@ -75,11 +154,11 @@ Maybe ksprint_ar(uint32 count, kprint_format format, ktId typeId, void* array){ StringBuilder_append_char(strb, '['); for (uint16 e=1; etypeId); char* valuestr=type.toString(_u, fmt); diff --git a/tests/main.cpp b/tests/main.cpp index 39d2405..35967c1 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -9,6 +9,7 @@ void test_all(){ test_hash_functions(); test_hashtable(); test_dtsod(); + test_kprint_colors(); printf("\e[96m--------------------------------------\e[0m\n"); } @@ -18,7 +19,8 @@ int main(){ ktDescriptors_initKerepTypes(); ktDescriptors_endInit(); printf("\e[97mkerep tests are starting!\n"); - optime("test_all",1,test_all()); + // optime("test_all",1,test_all()); + test_kprint_colors(); printf("\e[0m\n"); return 0; } diff --git a/tests/test_autoarr-vs-vector.cpp b/tests/test_autoarr-vs-vector.cpp index 1468463..37aba5e 100644 --- a/tests/test_autoarr-vs-vector.cpp +++ b/tests/test_autoarr-vs-vector.cpp @@ -4,7 +4,7 @@ int64 _autoarrVsVector(uint16 blockCount, uint16 blockLength){ uint32 count=blockLength*blockCount; - printf("\e[94mblock count: %u block length: %u count: %llu\n", blockCount, blockLength, (uint64)count); + printf("\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(); optime("Autoarr_add", 1, ({ @@ -15,7 +15,7 @@ int64 _autoarrVsVector(uint16 blockCount, uint16 blockLength){ for(uint32 i=0; i< count; i++) vec.push_back(i); })); - int64 t; + int64 t=0; optime("Autoarr_get", 1, ({ for(uint32 i=0; i< count; i++) t=Autoarr_get(ar, i); diff --git a/tests/test_kprint_colors.c b/tests/test_kprint_colors.c new file mode 100644 index 0000000..e23b8d9 --- /dev/null +++ b/tests/test_kprint_colors.c @@ -0,0 +1,47 @@ +#include "tests.h" +#if defined(_WIN32)|| defined(_WIN64) + #include +#endif + +#define testColor(COLOR) \ + kprint_setColor(kprint_bgBlack | kprint_fg##COLOR);\ + printf(#COLOR " ");\ + kprint_setColor(kprint_bg##COLOR | kprint_fgGray);\ + printf(#COLOR);\ + kprint_setColor(kprint_bgBlack | kprint_fgBlack);\ + printf("\n"); + +void test_kprint_colors(){ + IFWIN( + ({ + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + for(uint8 col=0; col<255; col++){ + SetConsoleTextAttribute(hConsole, col); + printf("%u ",col); + } + }), + ({ + for(uint8 col=0; col<255; col++) + printf("\e[%um%u ", col, col); + }) + ); + printf("\n"); + + testColor(Black); + testColor(DarkRed); + testColor(DarkGreen); + testColor(DarkYellow); + testColor(DarkBlue); + testColor(DarkMagneta); + testColor(DarkCyan); + testColor(Gray); + testColor(DarkGray); + testColor(Red); + testColor(Green); + testColor(Yellow); + testColor(Blue); + testColor(Magneta); + testColor(Cyan); + testColor(White); + kprint_setColor(kprint_bgBlack | kprint_fgGray); +} \ No newline at end of file From f33804f5b828e9bf6ce362ab30603d8e72f579e4 Mon Sep 17 00:00:00 2001 From: timerix Date: Mon, 24 Oct 2022 03:29:42 +0600 Subject: [PATCH 42/59] added random number generation algorithms --- src/base/errors.c | 1 + src/base/errors.h | 2 +- src/random/krandom.c | 6 ++ src/random/krandom.h | 91 +++++++++++++++++++ src/random/splitmix64.c | 35 +++++++ .../xoroshiro/32bitValue/xoroshiro64star.c | 53 +++++++++++ .../32bitValue/xoroshiro64starstar.c | 49 ++++++++++ .../xoroshiro/64bitValue/xoroshiro128plus.c | 62 +++++++++++++ .../64bitValue/xoroshiro128plusplus.c | 51 +++++++++++ .../64bitValue/xoroshiro128starstar.c | 51 +++++++++++ src/random/xoshiro-xoroshiro.md | 24 +++++ .../xoshiro/32bitValue/xoshiro128plus.c | 58 ++++++++++++ .../xoshiro/32bitValue/xoshiro128plusplus.c | 55 +++++++++++ .../xoshiro/32bitValue/xoshiro128starstar.c | 58 ++++++++++++ .../xoshiro/64bitValue/xoshiro256plus.c | 61 +++++++++++++ .../xoshiro/64bitValue/xoshiro256plusplus.c | 51 +++++++++++ .../xoshiro/64bitValue/xoshiro256starstar.c | 56 ++++++++++++ tests/main.cpp | 5 +- tests/test_rng_algorithms.c | 43 +++++++++ tests/tests.h | 1 + 20 files changed, 810 insertions(+), 3 deletions(-) create mode 100644 src/random/krandom.c create mode 100644 src/random/krandom.h create mode 100644 src/random/splitmix64.c create mode 100644 src/random/xoroshiro/32bitValue/xoroshiro64star.c create mode 100644 src/random/xoroshiro/32bitValue/xoroshiro64starstar.c create mode 100644 src/random/xoroshiro/64bitValue/xoroshiro128plus.c create mode 100644 src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c create mode 100644 src/random/xoroshiro/64bitValue/xoroshiro128starstar.c create mode 100644 src/random/xoshiro-xoroshiro.md create mode 100644 src/random/xoshiro/32bitValue/xoshiro128plus.c create mode 100644 src/random/xoshiro/32bitValue/xoshiro128plusplus.c create mode 100644 src/random/xoshiro/32bitValue/xoshiro128starstar.c create mode 100644 src/random/xoshiro/64bitValue/xoshiro256plus.c create mode 100644 src/random/xoshiro/64bitValue/xoshiro256plusplus.c create mode 100644 src/random/xoshiro/64bitValue/xoshiro256starstar.c create mode 100644 tests/test_rng_algorithms.c diff --git a/src/base/errors.c b/src/base/errors.c index 5adc0ee..4d0d2be 100644 --- a/src/base/errors.c +++ b/src/base/errors.c @@ -13,6 +13,7 @@ char* errname(ErrorId err){ case ERR_ENDOFSTR: return "ERR_ENDOFSTR"; case ERR_KEYNOTFOUND: return "ERR_KEYNOTFOUND"; case ERR_FORMAT: return "ERR_FORMAT"; + case ERR_UNEXPECTEDVAL: return "ERR_UNEXPECTEDVAL"; default: return "UNKNOWN_ERROR"; } } diff --git a/src/base/errors.h b/src/base/errors.h index 7be9aa9..4abc7df 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -11,7 +11,7 @@ typedef enum ErrorId { SUCCESS, // not an error ERR_MAXLENGTH, ERR_WRONGTYPE, ERR_WRONGINDEX, ERR_NOTIMPLEMENTED, ERR_NULLPTR, ERR_ENDOFSTR, - ERR_KEYNOTFOUND, ERR_FORMAT + ERR_KEYNOTFOUND, ERR_FORMAT, ERR_UNEXPECTEDVAL } ErrorId; char* errname(ErrorId err); diff --git a/src/random/krandom.c b/src/random/krandom.c new file mode 100644 index 0000000..3801717 --- /dev/null +++ b/src/random/krandom.c @@ -0,0 +1,6 @@ +#include "krandom.h" + +bool fate(float chance){ + int limit=1/chance + 0.01f; + return rand()%limit == 0; +} diff --git a/src/random/krandom.h b/src/random/krandom.h new file mode 100644 index 0000000..4815468 --- /dev/null +++ b/src/random/krandom.h @@ -0,0 +1,91 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../base/std.h" + +/* +You can choose any algorithm that has required functions: + + some_alg64_state some_alg64_init(uint64 seed); + uint64 some_alg64_next(some_alg64_state); + + #define KRAND_ALG64 some_alg64 + #include "kerep/random/krandom.h" + +The same way it works for 32-bit RNGs: + + some_alg64_state some_alg32_init(uint32 seed); + uint32 some_alg32_next(some_alg64_state); + + #define KRAND_ALG32 some_alg32 + #include "kerep/random/krandom.h" +*/ + +// default rng_next function +#ifndef KRAND_ALG32 + #define KRAND_ALG32 xoroshiro128plus +#endif +#ifndef KRAND_ALG64 + #define KRAND_ALG64 xoshiro128plus +#endif + +typedef void* KRAND_ALG32_state; +typedef void* KRAND_ALG64_state; +#define KRAND_ALG32_next(STATE) xoshiro128plus##_next(STATE) +#define KRAND_ALG32_init(SEED) xoshiro128plus##_init(SEED) +#define KRAND_ALG32_initFromTime() xoshiro128plus##_initFromTime() +#define KRAND_ALG64_next(STATE) xoshiro256plus##_next(STATE) +#define KRAND_ALG64_init(SEED) xoshiro256plus##_init(SEED) +#define KRAND_ALG64_initFromTime() xoshiro256plus##_initFromTime() + + +#define __krand_declare_alg(ALG, VALUE_SIZE)\ + typedef void* ALG##_state;\ + ALG##_state ALG##_init(uint64 seed);\ + static inline ALG##_state ALG##_initFromTime(void) { return ALG##_init(time(NULL)); }\ + uint##VALUE_SIZE ALG##_next(ALG##_state); + +// different algorithm declarations +// for ALG32 + // xoroshiro64 +__krand_declare_alg(xoroshiro64star, 32) +__krand_declare_alg(xoroshiro64starstar, 32) + // xoshiro128 +__krand_declare_alg(xoshiro128plus, 32) +__krand_declare_alg(xoshiro128plusplus, 32) +__krand_declare_alg(xoshiro128starstar, 32) +// for ALG64 + // xoroshiro128 +__krand_declare_alg(xoroshiro128plus, 64) +__krand_declare_alg(xoroshiro128plusplus, 64) +__krand_declare_alg(xoroshiro128starstar, 64) + // xoshiro256 +__krand_declare_alg(xoshiro256plus, 64) +__krand_declare_alg(xoshiro256plusplus, 64) +__krand_declare_alg(xoshiro256starstar, 64) + // splitmix64 +__krand_declare_alg(splitmix64, 64) + + +#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_ALG32_state state, int8 from, int8 to) __krand_next_definition(32) +static inline int16 krand_next16(KRAND_ALG32_state state, int16 from, int16 to) __krand_next_definition(32) +static inline int32 krand_next32(KRAND_ALG32_state state, int32 from, int32 to) __krand_next_definition(32) +static inline int64 krand_next64(KRAND_ALG64_state state, int64 from, int64 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_ALG32_state state) {return (uint32)KRAND_ALG32_next(state)/0xffffffff; } +static inline float64 krand_nextFloat64(KRAND_ALG64_state state) {return KRAND_ALG64_next(state)/0xffffffff; } + + +///@param chance (0-1.0) is probability of success +bool fate(float chance); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/random/splitmix64.c b/src/random/splitmix64.c new file mode 100644 index 0000000..cb89ed4 --- /dev/null +++ b/src/random/splitmix64.c @@ -0,0 +1,35 @@ +#include "krandom.h" + +/* +This is a fixed-increment version of Java 8's SplittableRandom generator +See http://dx.doi.org/10.1145/2714064.2660195 and +http://docs.oracle.com/javase/8/docs/api/java/util/SplittableRandom.html +It is a very fast generator passing BigCrush, and it can be useful if +for some reason you absolutely want 64 bits of state; otherwise, we +rather suggest to use a xoroshiro128+ (for moderately parallel +computations) or xorshift1024* (for massively parallel computations) +generator. +*/ + +// The state can be seeded with any (upto) 64 bit integer value. +typedef uint64 _state_t; + +void* splitmix64_init(uint64 seed){ + _state_t* state=malloc(sizeof(_state_t)); + *state=seed; + return state; +} + +uint64 splitmix64_next(void* _state) { + _state_t* state=_state; + // increment the state variable + *state += 0x9e3779b97f4a7c15; + // copy the state to a working variable + uint64 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 + z = (z ^ (z>>27)) * 0x94d049bb133111eb; + // return the variable xored with itself right bit shifted 31 + return z ^ (z>>31); +} diff --git a/src/random/xoroshiro/32bitValue/xoroshiro64star.c b/src/random/xoroshiro/32bitValue/xoroshiro64star.c new file mode 100644 index 0000000..89bb6cc --- /dev/null +++ b/src/random/xoroshiro/32bitValue/xoroshiro64star.c @@ -0,0 +1,53 @@ +/* Written in 2016 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . */ + +#include "../../krandom.h" + +/* +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 +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 +case) it can be used to generate 32-bit outputs, too. + +We suggest to use a sign test to extract a random Boolean value, and +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) { + return (x << k) | (x >> (32 - k)); +} + +typedef union { + uint64 merged; + uint32 s[2]; +} _state_t; + +uint32 xoroshiro64star_next(void* _state) { + _state_t* state=_state; + const uint32 s0 = state->s[0]; + uint32 s1 = state->s[1]; + const uint32 result = s0 * 0x9E3779BB; + + s1 ^= s0; + state->s[0] = rotl(s0, 26) ^ s1 ^ (s1 << 9); // a, b + state->s[1] = rotl(s1, 13); // c + + return result; +} + +void* xoroshiro64star_init(uint64 seed){ + _state_t* state=malloc(sizeof(_state_t)); + splitmix64_state splitmix=splitmix64_init(seed); + state->merged=splitmix64_next(splitmix); + return state; +} diff --git a/src/random/xoroshiro/32bitValue/xoroshiro64starstar.c b/src/random/xoroshiro/32bitValue/xoroshiro64starstar.c new file mode 100644 index 0000000..6edcd59 --- /dev/null +++ b/src/random/xoroshiro/32bitValue/xoroshiro64starstar.c @@ -0,0 +1,49 @@ +/* Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . */ + +#include "../../krandom.h" + +/* This is xoroshiro64** 1.0, our 32-bit all-purpose, rock-solid, + small-state generator. It is extremely fast and it passes all tests we + are aware of, but its state space is not large enough for any parallel + application. + + For generating just single-precision (i.e., 32-bit) floating-point + numbers, xoroshiro64* is even faster. + + The state must be seeded so that it is not everywhere zero. */ + + +static inline uint32 rotl(const uint32 x, int k) { + return (x << k) | (x >> (32 - k)); +} + +typedef union { + uint64 merged; + uint32 s[2]; +} _state_t; + +uint32 xoroshiro64starstar_next(void* _state) { + _state_t* state=_state; + const uint32 s0 = state->s[0]; + uint32 s1 = state->s[1]; + const uint32 result = rotl(s0 * 0x9E3779BB, 5) * 5; + + s1 ^= s0; + state->s[0] = rotl(s0, 26) ^ s1 ^ (s1 << 9); // a, b + state->s[1] = rotl(s1, 13); // c + + return result; +} + +void* xoroshiro64starstar_init(uint64 seed){ + _state_t* state=malloc(sizeof(_state_t)); + splitmix64_state splitmix=splitmix64_init(seed); + state->merged=splitmix64_next(splitmix); + return state; +} diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128plus.c b/src/random/xoroshiro/64bitValue/xoroshiro128plus.c new file mode 100644 index 0000000..cf70f65 --- /dev/null +++ b/src/random/xoroshiro/64bitValue/xoroshiro128plus.c @@ -0,0 +1,62 @@ +/* Written in 2016-2018 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . */ + +#include "../../krandom.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 mild parallelism. We suggest to use its upper bits for + floating-point 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 + it is usually the case) it can be used to generate 64-bit outputs, too; + moreover, this generator has a very mild Hamming-weight dependency + making our test (http://prng.di.unimi.it/hwd.php) fail after 5 TB of + output; we believe this slight bias cannot affect any application. If + you are concerned, use xoroshiro128++, xoroshiro128** or xoshiro256+. + + We suggest to use a sign test to extract a random Boolean value, and + right shifts to extract subsets of bits. + + 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. + + NOTE: the parameters (a=24, b=16, b=37) of this version give slightly + better results in our test than the 2016 version (a=55, b=14, c=36). +*/ + +static inline uint64 rotl(const uint64 x, int k) { + return (x << k) | (x >> (64 - k)); +} + +typedef union { + uint32 s[2]; +} _state_t; + +uint64 xoroshiro128plus_next(void* _state){ + _state_t* state=_state; + const uint64 s0 = state->s[0]; + uint64 s1 = state->s[1]; + const uint64 result = s0 + s1; + + s1 ^= s0; + state->s[0] = rotl(s0, 24) ^ s1 ^ (s1 << 16); // a, b + state->s[1] = rotl(s1, 37); // c + + return result; +} + +void* xoroshiro128plus_init(uint64 seed){ + _state_t* state=malloc(sizeof(_state_t)); + splitmix64_state splitmix=splitmix64_init(seed); + state->s[0]=splitmix64_next(splitmix); + state->s[1]=splitmix64_next(splitmix); + return state; +} diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c b/src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c new file mode 100644 index 0000000..aaf384d --- /dev/null +++ b/src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c @@ -0,0 +1,51 @@ +/* Written in 2019 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . */ + +#include "../../krandom.h" + +/* This is xoroshiro128++ 1.0, one of our all-purpose, rock-solid, + small-state generators. It is extremely (sub-ns) fast and it passes all + 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 + 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 + 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) { + return (x << k) | (x >> (64 - k)); +} + +typedef union { + uint32 s[2]; +} _state_t; + +uint64 xoroshiro128plusplus_next(void* _state){ + _state_t* state=_state; + const uint64 s0 = state->s[0]; + uint64 s1 = state->s[1]; + const uint64 result = rotl(s0 + s1, 17) + s0; + + s1 ^= s0; + state->s[0] = rotl(s0, 49) ^ s1 ^ (s1 << 21); // a, b + state->s[1] = rotl(s1, 28); // c + + return result; +} + +void* xoroshiro128plusplus_init(uint64 seed){ + _state_t* state=malloc(sizeof(_state_t)); + splitmix64_state splitmix=splitmix64_init(seed); + state->s[0]=splitmix64_next(splitmix); + state->s[1]=splitmix64_next(splitmix); + return state; +} diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128starstar.c b/src/random/xoroshiro/64bitValue/xoroshiro128starstar.c new file mode 100644 index 0000000..d811ab6 --- /dev/null +++ b/src/random/xoroshiro/64bitValue/xoroshiro128starstar.c @@ -0,0 +1,51 @@ +/* Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . */ + +#include "../../krandom.h" + +/* This is xoroshiro128** 1.0, one of our all-purpose, rock-solid, + small-state generators. It is extremely (sub-ns) fast and it passes all + 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 + 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 + 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) { + return (x << k) | (x >> (64 - k)); +} + +typedef union { + uint32 s[2]; +} _state_t; + +uint64 xoroshiro128starstar_next(void* _state){ + _state_t* state=_state; + const uint64 s0 = state->s[0]; + uint64 s1 = state->s[1]; + const uint64 result = rotl(s0 * 5, 7) * 9; + + s1 ^= s0; + state->s[0] = rotl(s0, 24) ^ s1 ^ (s1 << 16); // a, b + state->s[1] = rotl(s1, 37); // c + + return result; +} + +void* xoroshiro128starstar_init(uint64 seed){ + _state_t* state=malloc(sizeof(_state_t)); + splitmix64_state splitmix=splitmix64_init(seed); + state->s[0]=splitmix64_next(splitmix); + state->s[1]=splitmix64_next(splitmix); + return state; +} diff --git a/src/random/xoshiro-xoroshiro.md b/src/random/xoshiro-xoroshiro.md new file mode 100644 index 0000000..269477c --- /dev/null +++ b/src/random/xoshiro-xoroshiro.md @@ -0,0 +1,24 @@ +# Xoshiro/Xoroshiro RNG algorithms +There are a bunch of versions of xoshiro/xoroshiro algorithms, which are created by [David Blackman and Sebastiano Vigna](https://prng.di.unimi.it/) + + +``` +xoroshiro +├── 32bitValue +| ├── xoroshiro64star.c +| └── xoroshiro64starstar.c +└── 64bitValue + ├── xoroshiro128plus.c + ├── xoroshiro128plusplus.c + └── xoroshiro128starstar.c + +xoshiro +├── 32bitValue +│ ├── xoshiro128plus.c +│ ├── xoshiro128plusplus.c +│ └── xoshiro128starstar.c +└── 64bitValue + ├── xoshiro256plus.c + ├── xoshiro256plusplus.c + └── xoshiro256starstar.c +``` diff --git a/src/random/xoshiro/32bitValue/xoshiro128plus.c b/src/random/xoshiro/32bitValue/xoshiro128plus.c new file mode 100644 index 0000000..28c90ad --- /dev/null +++ b/src/random/xoshiro/32bitValue/xoshiro128plus.c @@ -0,0 +1,58 @@ +/* Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . */ + +#include "../../krandom.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**. + 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 + the case) it can be used to generate 32-bit outputs, too. + + We suggest to use a sign test to extract a random Boolean value, and + 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) { + return (x << k) | (x >> (32 - k)); +} + +typedef union { + uint64 merged[2]; + uint32 s[4]; +} _state_t; + +uint32 xoshiro128plus_next(void* _state){ + _state_t* state=_state; + const uint32 result = state->s[0] + state->s[3]; + + const uint32 t = state->s[1] << 9; + + state->s[2] ^= state->s[0]; + state->s[3] ^= state->s[1]; + state->s[1] ^= state->s[2]; + state->s[0] ^= state->s[3]; + + state->s[2] ^= t; + + state->s[3] = rotl(state->s[3], 11); + + return result; +} + +void* xoshiro128plus_init(uint64 seed){ + _state_t* state=malloc(sizeof(_state_t)); + splitmix64_state splitmix=splitmix64_init(seed); + state->merged[0]=splitmix64_next(splitmix); + state->merged[1]=splitmix64_next(splitmix); + return state; +} diff --git a/src/random/xoshiro/32bitValue/xoshiro128plusplus.c b/src/random/xoshiro/32bitValue/xoshiro128plusplus.c new file mode 100644 index 0000000..689dc06 --- /dev/null +++ b/src/random/xoshiro/32bitValue/xoshiro128plusplus.c @@ -0,0 +1,55 @@ +/* Written in 2019 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . */ + +#include "../../krandom.h" + +/* This is xoshiro128++ 1.0, one of our 32-bit all-purpose, rock-solid + generators. It has excellent speed, a state size (128 bits) that is + large enough for mild parallelism, and it passes all tests we are aware + of. + + For generating just single-precision (i.e., 32-bit) floating-point + numbers, xoshiro128+ is even faster. + + The state must be seeded so that it is not everywhere zero. */ + + +static inline uint32 rotl(const uint32 x, int k) { + return (x << k) | (x >> (32 - k)); +} + +typedef union { + uint64 merged[2]; + uint32 s[4]; +} _state_t; + +uint32 xoshiro128plusplus_next(void* _state){ + _state_t* state=_state; + const uint32 result = rotl(state->s[0] + state->s[3], 7) + state->s[0]; + + const uint32 t = state->s[1] << 9; + + state->s[2] ^= state->s[0]; + state->s[3] ^= state->s[1]; + state->s[1] ^= state->s[2]; + state->s[0] ^= state->s[3]; + + state->s[2] ^= t; + + state->s[3] = rotl(state->s[3], 11); + + return result; +} + +void* xoshiro128plusplus_init(uint64 seed){ + _state_t* state=malloc(sizeof(_state_t)); + splitmix64_state splitmix=splitmix64_init(seed); + state->merged[0]=splitmix64_next(splitmix); + state->merged[1]=splitmix64_next(splitmix); + return state; +} diff --git a/src/random/xoshiro/32bitValue/xoshiro128starstar.c b/src/random/xoshiro/32bitValue/xoshiro128starstar.c new file mode 100644 index 0000000..bf458fe --- /dev/null +++ b/src/random/xoshiro/32bitValue/xoshiro128starstar.c @@ -0,0 +1,58 @@ +/* Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . */ + +#include "../../krandom.h" + +/* This is xoshiro128** 1.1, one of our 32-bit all-purpose, rock-solid + generators. It has excellent speed, a state size (128 bits) that is + large enough for mild parallelism, and it passes all tests we are aware + of. + + Note that version 1.0 had mistakenly state->s[0] instead of state->s[1] as state + word passed to the scrambler. + + For generating just single-precision (i.e., 32-bit) floating-point + numbers, xoshiro128+ is even faster. + + The state must be seeded so that it is not everywhere zero. */ + + +static inline uint32 rotl(const uint32 x, int k) { + return (x << k) | (x >> (32 - k)); +} + +typedef union { + uint64 merged[2]; + uint32 s[4]; +} _state_t; + +uint32 xoshiro128starstar_next(void* _state){ + _state_t* state=_state; + const uint32 result = rotl(state->s[1] * 5, 7) * 9; + + const uint32 t = state->s[1] << 9; + + state->s[2] ^= state->s[0]; + state->s[3] ^= state->s[1]; + state->s[1] ^= state->s[2]; + state->s[0] ^= state->s[3]; + + state->s[2] ^= t; + + state->s[3] = rotl(state->s[3], 11); + + return result; +} + +void* xoshiro128starstar_init(uint64 seed){ + _state_t* state=malloc(sizeof(_state_t)); + splitmix64_state splitmix=splitmix64_init(seed); + state->merged[0]=splitmix64_next(splitmix); + state->merged[1]=splitmix64_next(splitmix); + return state; +} diff --git a/src/random/xoshiro/64bitValue/xoshiro256plus.c b/src/random/xoshiro/64bitValue/xoshiro256plus.c new file mode 100644 index 0000000..e6829d4 --- /dev/null +++ b/src/random/xoshiro/64bitValue/xoshiro256plus.c @@ -0,0 +1,61 @@ +/* Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . */ + +#include "../../krandom.h" + +/* This is xoshiro256+ 1.0, our best and fastest generator for floating-point + numbers. We suggest to use its upper bits for floating-point + generation, as it is slightly faster than xoshiro256++/xoshiro256**. It + passes all tests we are aware of except for the lowest three bits, + which might fail linearity tests (and just those), so if low linear + complexity is not considered an issue (as it is usually the case) it + can be used to generate 64-bit outputs, too. + + We suggest to use a sign test to extract a random Boolean value, and + right shifts to extract subsets of bits. + + 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) { + return (x << k) | (x >> (64 - k)); +} + +typedef union { + uint64 s[4]; +} _state_t; + +uint64 xoshiro256plus_next(void* _state){ + _state_t* state=_state; + const uint64 result = state->s[0] + state->s[3]; + + const uint64 t = state->s[1] << 17; + + state->s[2] ^= state->s[0]; + state->s[3] ^= state->s[1]; + state->s[1] ^= state->s[2]; + state->s[0] ^= state->s[3]; + + state->s[2] ^= t; + + state->s[3] = rotl(state->s[3], 45); + + return result; +} + +void* xoshiro256plus_init(uint64 seed){ + _state_t* state=malloc(sizeof(_state_t)); + splitmix64_state splitmix=splitmix64_init(seed); + state->s[0]=splitmix64_next(splitmix); + state->s[1]=splitmix64_next(splitmix); + state->s[2]=splitmix64_next(splitmix); + state->s[3]=splitmix64_next(splitmix); + return state; +} diff --git a/src/random/xoshiro/64bitValue/xoshiro256plusplus.c b/src/random/xoshiro/64bitValue/xoshiro256plusplus.c new file mode 100644 index 0000000..042035c --- /dev/null +++ b/src/random/xoshiro/64bitValue/xoshiro256plusplus.c @@ -0,0 +1,51 @@ +/* Written in 2019 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . */ + +#include "../../krandom.h" + +/* This is xoshiro256++ 1.0, one of our all-purpose, rock-solid generators. + It has excellent (sub-ns) speed, a state (256 bits) that is 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. + + 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) { + return (x << k) | (x>>(64 - k)); +} + +typedef union { + uint64 s[4]; +} _state_t; + +uint64 xoshiro256plusplus_next(void* _state) { + _state_t* state=_state; + const uint64 result=rotl(state->s[0] + state->s[3], 23) + state->s[0]; + const uint64 t=state->s[1] << 17; + state->s[2] ^= state->s[0]; + state->s[3] ^= state->s[1]; + state->s[1] ^= state->s[2]; + state->s[0] ^= state->s[3]; + state->s[2] ^= t; + state->s[3]=rotl(state->s[3], 45); + return result; +} + +void* xoshiro256plusplus_init(uint64 seed){ + _state_t* state=malloc(sizeof(_state_t)); + splitmix64_state splitmix=splitmix64_init(seed); + state->s[0]=splitmix64_next(splitmix); + state->s[1]=splitmix64_next(splitmix); + state->s[2]=splitmix64_next(splitmix); + state->s[3]=splitmix64_next(splitmix); + return state; +} diff --git a/src/random/xoshiro/64bitValue/xoshiro256starstar.c b/src/random/xoshiro/64bitValue/xoshiro256starstar.c new file mode 100644 index 0000000..625aedb --- /dev/null +++ b/src/random/xoshiro/64bitValue/xoshiro256starstar.c @@ -0,0 +1,56 @@ +/* Written in 2018 by David Blackman and Sebastiano Vigna (vigna@acm.org) + +To the extent possible under law, the author has dedicated all copyright +and related and neighboring rights to this software to the public domain +worldwide. This software is distributed without any warranty. + +See . */ + +#include "../../krandom.h" + +/* This is xoshiro256** 1.0, one of our all-purpose, rock-solid + generators. It has excellent (sub-ns) speed, a state (256 bits) that is + 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. + + 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) { + return (x << k) | (x >> (64 - k)); +} + +typedef union { + uint64 s[4]; +} _state_t; + +uint64 xoshiro256starstar_next(void* _state){ + _state_t* state=_state; + const uint64 result = rotl(state->s[1] * 5, 7) * 9; + + const uint64 t = state->s[1] << 17; + + state->s[2] ^= state->s[0]; + state->s[3] ^= state->s[1]; + state->s[1] ^= state->s[2]; + state->s[0] ^= state->s[3]; + + state->s[2] ^= t; + + state->s[3] = rotl(state->s[3], 45); + + return result; +} + +void* xoshiro256starstar_init(uint64 seed){ + _state_t* state=malloc(sizeof(_state_t)); + splitmix64_state splitmix=splitmix64_init(seed); + state->s[0]=splitmix64_next(splitmix); + state->s[1]=splitmix64_next(splitmix); + state->s[2]=splitmix64_next(splitmix); + state->s[3]=splitmix64_next(splitmix); + return state; +} diff --git a/tests/main.cpp b/tests/main.cpp index 35967c1..0ba2387 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -10,9 +10,9 @@ void test_all(){ test_hashtable(); test_dtsod(); test_kprint_colors(); + test_rng_algorithms(); printf("\e[96m--------------------------------------\e[0m\n"); } - int main(){ setlocale(LC_ALL, "en-US.Unicode"); ktDescriptors_beginInit(); @@ -20,7 +20,8 @@ int main(){ ktDescriptors_endInit(); printf("\e[97mkerep tests are starting!\n"); // optime("test_all",1,test_all()); - test_kprint_colors(); + // test_kprint_colors(); + test_rng_algorithms(); printf("\e[0m\n"); return 0; } diff --git a/tests/test_rng_algorithms.c b/tests/test_rng_algorithms.c new file mode 100644 index 0000000..250def8 --- /dev/null +++ b/tests/test_rng_algorithms.c @@ -0,0 +1,43 @@ +#include "tests.h" +#include "../src/random/krandom.h" + + +#define test_alg(ALG, VALUE_SIZE, EXPECTED_FROM_ZERO){\ + printf("\e[94mrng algorithm: \e[96m" #ALG "\n");\ + ALG##_state s= ALG##_init(0);\ + uint##VALUE_SIZE r=ALG##_next(s);\ + printf("\e[97m next from zero seed:");\ + if(r!=EXPECTED_FROM_ZERO){\ + printf("\e[91m %llu\n", (uint64)r);\ + throw(ERR_UNEXPECTEDVAL);\ + }\ + printf("\e[92m %llu\n", (uint64)r);\ + s= ALG##_initFromTime();\ + r=ALG##_next(s);\ + printf("\e[97m next from time seed:\e[92m %llu\n", (uint64)r);\ +} + +void test_rng_algorithms(){ + optime("test_rng_algorithms",1,({ + printf("\e[96m--------[test_rng_algorithms]---------\n"); + // for ALG32 + // xoroshiro64 + test_alg(xoroshiro64star, 32, 932574677ULL) + test_alg(xoroshiro64starstar, 32, 3183060286ULL) + // xoshiro128 + test_alg(xoshiro128plus, 32, 3918949401ULL) + test_alg(xoshiro128plusplus, 32, 1179900579ULL) + test_alg(xoshiro128starstar, 32, 3737715805ULL) + // for ALG64 + // xoroshiro128 + test_alg(xoroshiro128plus, 64, 4778832803ULL) + test_alg(xoroshiro128plusplus, 64, 626373238705583ULL) + test_alg(xoroshiro128starstar, 64, 11897572417920ULL) + // xoshiro256 + test_alg(xoshiro256plus, 64, 15757075719729598363ULL) + test_alg(xoshiro256plusplus, 64, 5987356902031041503ULL) + test_alg(xoshiro256starstar, 64, 11091344671253066420ULL) + // splitmix64 + test_alg(splitmix64, 64, 16294208416658607535ULL) + })); +} \ No newline at end of file diff --git a/tests/tests.h b/tests/tests.h index 584d8c4..db46e93 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -15,6 +15,7 @@ void test_hashtable(); void test_dtsod(); void test_kprint_colors(); void test_autoarrVsVector(); +void test_rng_algorithms(); #define PRINT_SIZEOF(T) printf("\e[94m" #T " size: \e[96m" IFWIN("%llu", "%lu") "\n", sizeof(T)) From 66935ae4372c9e9ff036ae6332e7fe3ea35a9dd5 Mon Sep 17 00:00:00 2001 From: timerix Date: Mon, 24 Oct 2022 04:32:06 +0600 Subject: [PATCH 43/59] no more repeating code in xoshiro/xoroshiro --- src/random/krandom.h | 84 ++++++------------- src/random/{ => splitmix64}/splitmix64.c | 7 +- src/random/splitmix64/splitmix64.h | 19 +++++ src/random/xoroshiro/32bitValue/xoroshiro64.h | 29 +++++++ .../xoroshiro/32bitValue/xoroshiro64star.c | 15 ++-- .../32bitValue/xoroshiro64starstar.c | 16 +--- .../xoroshiro/64bitValue/xoroshiro128.h | 32 +++++++ .../xoroshiro/64bitValue/xoroshiro128plus.c | 14 ++-- .../64bitValue/xoroshiro128plusplus.c | 16 +--- .../64bitValue/xoroshiro128starstar.c | 16 +--- src/random/xoroshiro/xoroshiro.h | 2 + src/random/xoshiro/32bitValue/xoshiro128.h | 33 ++++++++ .../xoshiro/32bitValue/xoshiro128plus.c | 15 ++-- .../xoshiro/32bitValue/xoshiro128plusplus.c | 17 +--- .../xoshiro/32bitValue/xoshiro128starstar.c | 17 +--- src/random/xoshiro/64bitValue/xoshiro256.h | 32 +++++++ .../xoshiro/64bitValue/xoshiro256plus.c | 14 ++-- .../xoshiro/64bitValue/xoshiro256plusplus.c | 18 +--- .../xoshiro/64bitValue/xoshiro256starstar.c | 18 +--- src/random/xoshiro/xoshiro.h | 2 + tests/test_rng_algorithms.c | 2 +- 21 files changed, 214 insertions(+), 204 deletions(-) rename src/random/{ => splitmix64}/splitmix64.c (90%) create mode 100644 src/random/splitmix64/splitmix64.h create mode 100644 src/random/xoroshiro/32bitValue/xoroshiro64.h create mode 100644 src/random/xoroshiro/64bitValue/xoroshiro128.h create mode 100644 src/random/xoroshiro/xoroshiro.h create mode 100644 src/random/xoshiro/32bitValue/xoshiro128.h create mode 100644 src/random/xoshiro/64bitValue/xoshiro256.h create mode 100644 src/random/xoshiro/xoshiro.h diff --git a/src/random/krandom.h b/src/random/krandom.h index 4815468..ce44a39 100644 --- a/src/random/krandom.h +++ b/src/random/krandom.h @@ -5,82 +5,52 @@ extern "C" { #endif #include "../base/std.h" +#include "splitmix64/splitmix64.h" +#include "xoroshiro/xoroshiro.h" +#include "xoshiro/xoshiro.h" /* You can choose any algorithm that has required functions: - some_alg64_state some_alg64_init(uint64 seed); - uint64 some_alg64_next(some_alg64_state); + some_alg32_state some_alg32_init(uint32 seed); + uint32 some_alg32_next(some_alg32_state); - #define KRAND_ALG64 some_alg64 + #define KRAND_ALG32_next some_alg32_next + #define KRAND_ALG32_init some_alg32_init #include "kerep/random/krandom.h" -The same way it works for 32-bit RNGs: - - some_alg64_state some_alg32_init(uint32 seed); - uint32 some_alg32_next(some_alg64_state); - - #define KRAND_ALG32 some_alg32 - #include "kerep/random/krandom.h" +The same way it works for 64-bit RNGs */ // default rng_next function -#ifndef KRAND_ALG32 - #define KRAND_ALG32 xoroshiro128plus +#ifndef KRAND_ALG32_next +#define KRAND_ALG32_next xoshiro128plus##_next #endif -#ifndef KRAND_ALG64 - #define KRAND_ALG64 xoshiro128plus +#ifndef KRAND_ALG32_init +#define KRAND_ALG32_init xoshiro128plus##_init +#endif +#ifndef KRAND_ALG64_next +#define KRAND_ALG64_next xoshiro256plus##_next +#endif +#ifndef KRAND_ALG64_init +#define KRAND_ALG64_init xoshiro256plus##_init #endif -typedef void* KRAND_ALG32_state; -typedef void* KRAND_ALG64_state; -#define KRAND_ALG32_next(STATE) xoshiro128plus##_next(STATE) -#define KRAND_ALG32_init(SEED) xoshiro128plus##_init(SEED) -#define KRAND_ALG32_initFromTime() xoshiro128plus##_initFromTime() -#define KRAND_ALG64_next(STATE) xoshiro256plus##_next(STATE) -#define KRAND_ALG64_init(SEED) xoshiro256plus##_init(SEED) -#define KRAND_ALG64_initFromTime() xoshiro256plus##_initFromTime() - - -#define __krand_declare_alg(ALG, VALUE_SIZE)\ - typedef void* ALG##_state;\ - ALG##_state ALG##_init(uint64 seed);\ - static inline ALG##_state ALG##_initFromTime(void) { return ALG##_init(time(NULL)); }\ - uint##VALUE_SIZE ALG##_next(ALG##_state); - -// different algorithm declarations -// for ALG32 - // xoroshiro64 -__krand_declare_alg(xoroshiro64star, 32) -__krand_declare_alg(xoroshiro64starstar, 32) - // xoshiro128 -__krand_declare_alg(xoshiro128plus, 32) -__krand_declare_alg(xoshiro128plusplus, 32) -__krand_declare_alg(xoshiro128starstar, 32) -// for ALG64 - // xoroshiro128 -__krand_declare_alg(xoroshiro128plus, 64) -__krand_declare_alg(xoroshiro128plusplus, 64) -__krand_declare_alg(xoroshiro128starstar, 64) - // xoshiro256 -__krand_declare_alg(xoshiro256plus, 64) -__krand_declare_alg(xoshiro256plusplus, 64) -__krand_declare_alg(xoshiro256starstar, 64) - // splitmix64 -__krand_declare_alg(splitmix64, 64) - +typedef void* krand_statePtr; +#define KRAND_ALG32_initFromTime xoshiro128plus##_initFromTime +#define KRAND_ALG64_initFromTime xoshiro256plus##_initFromTime #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_ALG32_state state, int8 from, int8 to) __krand_next_definition(32) -static inline int16 krand_next16(KRAND_ALG32_state state, int16 from, int16 to) __krand_next_definition(32) -static inline int32 krand_next32(KRAND_ALG32_state state, int32 from, int32 to) __krand_next_definition(32) -static inline int64 krand_next64(KRAND_ALG64_state state, int64 from, int64 to) __krand_next_definition(64) +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) // divides random number by 2^64 to return a value between 0 and 1 -static inline float32 krand_nextFloat32(KRAND_ALG32_state state) {return (uint32)KRAND_ALG32_next(state)/0xffffffff; } -static inline float64 krand_nextFloat64(KRAND_ALG64_state state) {return KRAND_ALG64_next(state)/0xffffffff; } +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; } ///@param chance (0-1.0) is probability of success diff --git a/src/random/splitmix64.c b/src/random/splitmix64/splitmix64.c similarity index 90% rename from src/random/splitmix64.c rename to src/random/splitmix64/splitmix64.c index cb89ed4..591167c 100644 --- a/src/random/splitmix64.c +++ b/src/random/splitmix64/splitmix64.c @@ -1,4 +1,4 @@ -#include "krandom.h" +#include "splitmix64.h" /* This is a fixed-increment version of Java 8's SplittableRandom generator @@ -12,16 +12,15 @@ generator. */ // The state can be seeded with any (upto) 64 bit integer value. -typedef uint64 _state_t; void* splitmix64_init(uint64 seed){ - _state_t* state=malloc(sizeof(_state_t)); + splitmix64_state* state=malloc(sizeof(splitmix64_state)); *state=seed; return state; } uint64 splitmix64_next(void* _state) { - _state_t* state=_state; + splitmix64_state* state=_state; // increment the state variable *state += 0x9e3779b97f4a7c15; // copy the state to a working variable diff --git a/src/random/splitmix64/splitmix64.h b/src/random/splitmix64/splitmix64.h new file mode 100644 index 0000000..ca9e77f --- /dev/null +++ b/src/random/splitmix64/splitmix64.h @@ -0,0 +1,19 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../../base/base.h" + +typedef uint64 splitmix64_state; +typedef void* splitmix64_statePtr; + +splitmix64_statePtr splitmix64_init(uint64 seed); +static inline splitmix64_statePtr splitmix64_initFromTime(void) { return splitmix64_init(time(NULL)); } + +uint64 splitmix64_next(splitmix64_statePtr); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/random/xoroshiro/32bitValue/xoroshiro64.h b/src/random/xoroshiro/32bitValue/xoroshiro64.h new file mode 100644 index 0000000..e4295fe --- /dev/null +++ b/src/random/xoroshiro/32bitValue/xoroshiro64.h @@ -0,0 +1,29 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../../../base/std.h" +#include "../../splitmix64/splitmix64.h" + +typedef union { + uint64 merged; + uint32 s[2]; +} xoroshiro64_state; +typedef void* xoroshiro64_statePtr; + +xoroshiro64_statePtr xoroshiro64_init(uint64 seed); +#define xoroshiro64star_init xoroshiro64_init +#define xoroshiro64starstar_init xoroshiro64_init + +static inline xoroshiro64_statePtr xoroshiro64_initFromTime(void) { return xoroshiro64_init(time(NULL)); } +#define xoroshiro64star_initFromTime xoroshiro64_initFromTime +#define xoroshiro64starstar_initFromTime xoroshiro64_initFromTime + +uint32 xoroshiro64star_next(xoroshiro64_statePtr); +uint32 xoroshiro64starstar_next(xoroshiro64_statePtr); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/random/xoroshiro/32bitValue/xoroshiro64star.c b/src/random/xoroshiro/32bitValue/xoroshiro64star.c index 89bb6cc..9b909a0 100644 --- a/src/random/xoroshiro/32bitValue/xoroshiro64star.c +++ b/src/random/xoroshiro/32bitValue/xoroshiro64star.c @@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty. See . */ -#include "../../krandom.h" +#include "xoroshiro64.h" /* This is xoroshiro64* 1.0, our best and fastest 32-bit small-state @@ -27,13 +27,8 @@ static inline uint32 rotl(const uint32 x, int k) { return (x << k) | (x >> (32 - k)); } -typedef union { - uint64 merged; - uint32 s[2]; -} _state_t; - uint32 xoroshiro64star_next(void* _state) { - _state_t* state=_state; + xoroshiro64_state* state=_state; const uint32 s0 = state->s[0]; uint32 s1 = state->s[1]; const uint32 result = s0 * 0x9E3779BB; @@ -45,9 +40,9 @@ uint32 xoroshiro64star_next(void* _state) { return result; } -void* xoroshiro64star_init(uint64 seed){ - _state_t* state=malloc(sizeof(_state_t)); - splitmix64_state splitmix=splitmix64_init(seed); +void* xoroshiro64_init(uint64 seed){ + xoroshiro64_state* state=malloc(sizeof(xoroshiro64_state)); + splitmix64_state* splitmix=splitmix64_init(seed); state->merged=splitmix64_next(splitmix); return state; } diff --git a/src/random/xoroshiro/32bitValue/xoroshiro64starstar.c b/src/random/xoroshiro/32bitValue/xoroshiro64starstar.c index 6edcd59..df4432e 100644 --- a/src/random/xoroshiro/32bitValue/xoroshiro64starstar.c +++ b/src/random/xoroshiro/32bitValue/xoroshiro64starstar.c @@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty. See . */ -#include "../../krandom.h" +#include "xoroshiro64.h" /* This is xoroshiro64** 1.0, our 32-bit all-purpose, rock-solid, small-state generator. It is extremely fast and it passes all tests we @@ -23,13 +23,8 @@ static inline uint32 rotl(const uint32 x, int k) { return (x << k) | (x >> (32 - k)); } -typedef union { - uint64 merged; - uint32 s[2]; -} _state_t; - uint32 xoroshiro64starstar_next(void* _state) { - _state_t* state=_state; + xoroshiro64_state* state=_state; const uint32 s0 = state->s[0]; uint32 s1 = state->s[1]; const uint32 result = rotl(s0 * 0x9E3779BB, 5) * 5; @@ -40,10 +35,3 @@ uint32 xoroshiro64starstar_next(void* _state) { return result; } - -void* xoroshiro64starstar_init(uint64 seed){ - _state_t* state=malloc(sizeof(_state_t)); - splitmix64_state splitmix=splitmix64_init(seed); - state->merged=splitmix64_next(splitmix); - return state; -} diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128.h b/src/random/xoroshiro/64bitValue/xoroshiro128.h new file mode 100644 index 0000000..46d02e5 --- /dev/null +++ b/src/random/xoroshiro/64bitValue/xoroshiro128.h @@ -0,0 +1,32 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../../../base/std.h" +#include "../../splitmix64/splitmix64.h" + + +typedef union { + uint32 s[2]; +} xoroshiro128_state; +typedef void* xoroshiro128_statePtr; + +xoroshiro128_statePtr xoroshiro128_init(uint64 seed); +#define xoroshiro128plus_init xoroshiro128_init +#define xoroshiro128plusplus_init xoroshiro128_init +#define xoroshiro128starstar_init xoroshiro128_init + +static inline xoroshiro128_statePtr xoroshiro128_initFromTime(void) { return xoroshiro128_init(time(NULL)); } +#define xoroshiro128plus_initFromTime xoroshiro128_initFromTime +#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); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128plus.c b/src/random/xoroshiro/64bitValue/xoroshiro128plus.c index cf70f65..2846024 100644 --- a/src/random/xoroshiro/64bitValue/xoroshiro128plus.c +++ b/src/random/xoroshiro/64bitValue/xoroshiro128plus.c @@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty. See . */ -#include "../../krandom.h" +#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 @@ -36,12 +36,8 @@ static inline uint64 rotl(const uint64 x, int k) { return (x << k) | (x >> (64 - k)); } -typedef union { - uint32 s[2]; -} _state_t; - uint64 xoroshiro128plus_next(void* _state){ - _state_t* state=_state; + xoroshiro128_state* state=_state; const uint64 s0 = state->s[0]; uint64 s1 = state->s[1]; const uint64 result = s0 + s1; @@ -53,9 +49,9 @@ uint64 xoroshiro128plus_next(void* _state){ return result; } -void* xoroshiro128plus_init(uint64 seed){ - _state_t* state=malloc(sizeof(_state_t)); - splitmix64_state splitmix=splitmix64_init(seed); +void* xoroshiro128_init(uint64 seed){ + xoroshiro128_state* state=malloc(sizeof(xoroshiro128_state)); + splitmix64_state* splitmix=splitmix64_init(seed); state->s[0]=splitmix64_next(splitmix); state->s[1]=splitmix64_next(splitmix); return state; diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c b/src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c index aaf384d..c01fb30 100644 --- a/src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c +++ b/src/random/xoroshiro/64bitValue/xoroshiro128plusplus.c @@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty. See . */ -#include "../../krandom.h" +#include "xoroshiro128.h" /* This is xoroshiro128++ 1.0, one of our all-purpose, rock-solid, small-state generators. It is extremely (sub-ns) fast and it passes all @@ -25,12 +25,8 @@ static inline uint64 rotl(const uint64 x, int k) { return (x << k) | (x >> (64 - k)); } -typedef union { - uint32 s[2]; -} _state_t; - uint64 xoroshiro128plusplus_next(void* _state){ - _state_t* state=_state; + xoroshiro128_state* state=_state; const uint64 s0 = state->s[0]; uint64 s1 = state->s[1]; const uint64 result = rotl(s0 + s1, 17) + s0; @@ -41,11 +37,3 @@ uint64 xoroshiro128plusplus_next(void* _state){ return result; } - -void* xoroshiro128plusplus_init(uint64 seed){ - _state_t* state=malloc(sizeof(_state_t)); - splitmix64_state splitmix=splitmix64_init(seed); - state->s[0]=splitmix64_next(splitmix); - state->s[1]=splitmix64_next(splitmix); - return state; -} diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128starstar.c b/src/random/xoroshiro/64bitValue/xoroshiro128starstar.c index d811ab6..3e9ac72 100644 --- a/src/random/xoroshiro/64bitValue/xoroshiro128starstar.c +++ b/src/random/xoroshiro/64bitValue/xoroshiro128starstar.c @@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty. See . */ -#include "../../krandom.h" +#include "xoroshiro128.h" /* This is xoroshiro128** 1.0, one of our all-purpose, rock-solid, small-state generators. It is extremely (sub-ns) fast and it passes all @@ -25,12 +25,8 @@ static inline uint64 rotl(const uint64 x, int k) { return (x << k) | (x >> (64 - k)); } -typedef union { - uint32 s[2]; -} _state_t; - uint64 xoroshiro128starstar_next(void* _state){ - _state_t* state=_state; + xoroshiro128_state* state=_state; const uint64 s0 = state->s[0]; uint64 s1 = state->s[1]; const uint64 result = rotl(s0 * 5, 7) * 9; @@ -41,11 +37,3 @@ uint64 xoroshiro128starstar_next(void* _state){ return result; } - -void* xoroshiro128starstar_init(uint64 seed){ - _state_t* state=malloc(sizeof(_state_t)); - splitmix64_state splitmix=splitmix64_init(seed); - state->s[0]=splitmix64_next(splitmix); - state->s[1]=splitmix64_next(splitmix); - return state; -} diff --git a/src/random/xoroshiro/xoroshiro.h b/src/random/xoroshiro/xoroshiro.h new file mode 100644 index 0000000..127c937 --- /dev/null +++ b/src/random/xoroshiro/xoroshiro.h @@ -0,0 +1,2 @@ +#include "32bitValue/xoroshiro64.h" +#include "64bitValue/xoroshiro128.h" diff --git a/src/random/xoshiro/32bitValue/xoshiro128.h b/src/random/xoshiro/32bitValue/xoshiro128.h new file mode 100644 index 0000000..ebba10f --- /dev/null +++ b/src/random/xoshiro/32bitValue/xoshiro128.h @@ -0,0 +1,33 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../../../base/std.h" +#include "../../splitmix64/splitmix64.h" + + +typedef union { + uint64 merged[2]; + uint32 s[4]; +} xoshiro128_state; +typedef void* xoshiro128_statePtr; + +xoshiro128_statePtr xoshiro128_init(uint64 seed); +#define xoshiro128plus_init xoshiro128_init +#define xoshiro128plusplus_init xoshiro128_init +#define xoshiro128starstar_init xoshiro128_init + +static inline xoshiro128_statePtr xoshiro128_initFromTime(void) { return xoshiro128_init(time(NULL)); } +#define xoshiro128plus_initFromTime xoshiro128_initFromTime +#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); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/random/xoshiro/32bitValue/xoshiro128plus.c b/src/random/xoshiro/32bitValue/xoshiro128plus.c index 28c90ad..540c71e 100644 --- a/src/random/xoshiro/32bitValue/xoshiro128plus.c +++ b/src/random/xoshiro/32bitValue/xoshiro128plus.c @@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty. See . */ -#include "../../krandom.h" +#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 @@ -26,13 +26,8 @@ static inline uint32 rotl(const uint32 x, int k) { return (x << k) | (x >> (32 - k)); } -typedef union { - uint64 merged[2]; - uint32 s[4]; -} _state_t; - uint32 xoshiro128plus_next(void* _state){ - _state_t* state=_state; + xoshiro128_state* state=_state; const uint32 result = state->s[0] + state->s[3]; const uint32 t = state->s[1] << 9; @@ -49,9 +44,9 @@ uint32 xoshiro128plus_next(void* _state){ return result; } -void* xoshiro128plus_init(uint64 seed){ - _state_t* state=malloc(sizeof(_state_t)); - splitmix64_state splitmix=splitmix64_init(seed); +void* xoshiro128_init(uint64 seed){ + xoshiro128_state* state=malloc(sizeof(xoshiro128_state)); + splitmix64_state* splitmix=splitmix64_init(seed); state->merged[0]=splitmix64_next(splitmix); state->merged[1]=splitmix64_next(splitmix); return state; diff --git a/src/random/xoshiro/32bitValue/xoshiro128plusplus.c b/src/random/xoshiro/32bitValue/xoshiro128plusplus.c index 689dc06..e9be19d 100644 --- a/src/random/xoshiro/32bitValue/xoshiro128plusplus.c +++ b/src/random/xoshiro/32bitValue/xoshiro128plusplus.c @@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty. See . */ -#include "../../krandom.h" +#include "xoshiro128.h" /* This is xoshiro128++ 1.0, one of our 32-bit all-purpose, rock-solid generators. It has excellent speed, a state size (128 bits) that is @@ -23,13 +23,8 @@ static inline uint32 rotl(const uint32 x, int k) { return (x << k) | (x >> (32 - k)); } -typedef union { - uint64 merged[2]; - uint32 s[4]; -} _state_t; - uint32 xoshiro128plusplus_next(void* _state){ - _state_t* state=_state; + xoshiro128_state* state=_state; const uint32 result = rotl(state->s[0] + state->s[3], 7) + state->s[0]; const uint32 t = state->s[1] << 9; @@ -45,11 +40,3 @@ uint32 xoshiro128plusplus_next(void* _state){ return result; } - -void* xoshiro128plusplus_init(uint64 seed){ - _state_t* state=malloc(sizeof(_state_t)); - splitmix64_state splitmix=splitmix64_init(seed); - state->merged[0]=splitmix64_next(splitmix); - state->merged[1]=splitmix64_next(splitmix); - return state; -} diff --git a/src/random/xoshiro/32bitValue/xoshiro128starstar.c b/src/random/xoshiro/32bitValue/xoshiro128starstar.c index bf458fe..40a53d2 100644 --- a/src/random/xoshiro/32bitValue/xoshiro128starstar.c +++ b/src/random/xoshiro/32bitValue/xoshiro128starstar.c @@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty. See . */ -#include "../../krandom.h" +#include "xoshiro128.h" /* This is xoshiro128** 1.1, one of our 32-bit all-purpose, rock-solid generators. It has excellent speed, a state size (128 bits) that is @@ -26,13 +26,8 @@ static inline uint32 rotl(const uint32 x, int k) { return (x << k) | (x >> (32 - k)); } -typedef union { - uint64 merged[2]; - uint32 s[4]; -} _state_t; - uint32 xoshiro128starstar_next(void* _state){ - _state_t* state=_state; + xoshiro128_state* state=_state; const uint32 result = rotl(state->s[1] * 5, 7) * 9; const uint32 t = state->s[1] << 9; @@ -48,11 +43,3 @@ uint32 xoshiro128starstar_next(void* _state){ return result; } - -void* xoshiro128starstar_init(uint64 seed){ - _state_t* state=malloc(sizeof(_state_t)); - splitmix64_state splitmix=splitmix64_init(seed); - state->merged[0]=splitmix64_next(splitmix); - state->merged[1]=splitmix64_next(splitmix); - return state; -} diff --git a/src/random/xoshiro/64bitValue/xoshiro256.h b/src/random/xoshiro/64bitValue/xoshiro256.h new file mode 100644 index 0000000..0cbae44 --- /dev/null +++ b/src/random/xoshiro/64bitValue/xoshiro256.h @@ -0,0 +1,32 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../../../base/std.h" +#include "../../splitmix64/splitmix64.h" + + +typedef union { + uint64 s[4]; +} xoshiro256_state; +typedef void* xoshiro256_statePtr; + +xoshiro256_statePtr xoshiro256_init(uint64 seed); +#define xoshiro256plus_init xoshiro256_init +#define xoshiro256plusplus_init xoshiro256_init +#define xoshiro256starstar_init xoshiro256_init + +static inline xoshiro256_statePtr xoshiro256_initFromTime(void) { return xoshiro256_init(time(NULL)); } +#define xoshiro256plus_initFromTime xoshiro256_initFromTime +#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); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/random/xoshiro/64bitValue/xoshiro256plus.c b/src/random/xoshiro/64bitValue/xoshiro256plus.c index e6829d4..ba034be 100644 --- a/src/random/xoshiro/64bitValue/xoshiro256plus.c +++ b/src/random/xoshiro/64bitValue/xoshiro256plus.c @@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty. See . */ -#include "../../krandom.h" +#include "xoshiro256.h" /* This is xoshiro256+ 1.0, our best and fastest generator for floating-point numbers. We suggest to use its upper bits for floating-point @@ -28,12 +28,8 @@ static inline uint64 rotl(const uint64 x, int k) { return (x << k) | (x >> (64 - k)); } -typedef union { - uint64 s[4]; -} _state_t; - uint64 xoshiro256plus_next(void* _state){ - _state_t* state=_state; + xoshiro256_state* state=_state; const uint64 result = state->s[0] + state->s[3]; const uint64 t = state->s[1] << 17; @@ -50,9 +46,9 @@ uint64 xoshiro256plus_next(void* _state){ return result; } -void* xoshiro256plus_init(uint64 seed){ - _state_t* state=malloc(sizeof(_state_t)); - splitmix64_state splitmix=splitmix64_init(seed); +void* xoshiro256_init(uint64 seed){ + xoshiro256_state* state=malloc(sizeof(xoshiro256_state)); + splitmix64_state* splitmix=splitmix64_init(seed); state->s[0]=splitmix64_next(splitmix); state->s[1]=splitmix64_next(splitmix); state->s[2]=splitmix64_next(splitmix); diff --git a/src/random/xoshiro/64bitValue/xoshiro256plusplus.c b/src/random/xoshiro/64bitValue/xoshiro256plusplus.c index 042035c..77ca943 100644 --- a/src/random/xoshiro/64bitValue/xoshiro256plusplus.c +++ b/src/random/xoshiro/64bitValue/xoshiro256plusplus.c @@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty. See . */ -#include "../../krandom.h" +#include "xoshiro256.h" /* This is xoshiro256++ 1.0, one of our all-purpose, rock-solid generators. It has excellent (sub-ns) speed, a state (256 bits) that is large @@ -23,12 +23,8 @@ static inline uint64 rotl(const uint64 x, int k) { return (x << k) | (x>>(64 - k)); } -typedef union { - uint64 s[4]; -} _state_t; - uint64 xoshiro256plusplus_next(void* _state) { - _state_t* state=_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; state->s[2] ^= state->s[0]; @@ -39,13 +35,3 @@ uint64 xoshiro256plusplus_next(void* _state) { state->s[3]=rotl(state->s[3], 45); return result; } - -void* xoshiro256plusplus_init(uint64 seed){ - _state_t* state=malloc(sizeof(_state_t)); - splitmix64_state splitmix=splitmix64_init(seed); - state->s[0]=splitmix64_next(splitmix); - state->s[1]=splitmix64_next(splitmix); - state->s[2]=splitmix64_next(splitmix); - state->s[3]=splitmix64_next(splitmix); - return state; -} diff --git a/src/random/xoshiro/64bitValue/xoshiro256starstar.c b/src/random/xoshiro/64bitValue/xoshiro256starstar.c index 625aedb..7829096 100644 --- a/src/random/xoshiro/64bitValue/xoshiro256starstar.c +++ b/src/random/xoshiro/64bitValue/xoshiro256starstar.c @@ -6,7 +6,7 @@ worldwide. This software is distributed without any warranty. See . */ -#include "../../krandom.h" +#include "xoshiro256.h" /* This is xoshiro256** 1.0, one of our all-purpose, rock-solid generators. It has excellent (sub-ns) speed, a state (256 bits) that is @@ -23,12 +23,8 @@ static inline uint64 rotl(const uint64 x, int k) { return (x << k) | (x >> (64 - k)); } -typedef union { - uint64 s[4]; -} _state_t; - uint64 xoshiro256starstar_next(void* _state){ - _state_t* state=_state; + xoshiro256_state* state=_state; const uint64 result = rotl(state->s[1] * 5, 7) * 9; const uint64 t = state->s[1] << 17; @@ -44,13 +40,3 @@ uint64 xoshiro256starstar_next(void* _state){ return result; } - -void* xoshiro256starstar_init(uint64 seed){ - _state_t* state=malloc(sizeof(_state_t)); - splitmix64_state splitmix=splitmix64_init(seed); - state->s[0]=splitmix64_next(splitmix); - state->s[1]=splitmix64_next(splitmix); - state->s[2]=splitmix64_next(splitmix); - state->s[3]=splitmix64_next(splitmix); - return state; -} diff --git a/src/random/xoshiro/xoshiro.h b/src/random/xoshiro/xoshiro.h new file mode 100644 index 0000000..121ed3a --- /dev/null +++ b/src/random/xoshiro/xoshiro.h @@ -0,0 +1,2 @@ +#include "32bitValue/xoshiro128.h" +#include "64bitValue/xoshiro256.h" diff --git a/tests/test_rng_algorithms.c b/tests/test_rng_algorithms.c index 250def8..eae4dfb 100644 --- a/tests/test_rng_algorithms.c +++ b/tests/test_rng_algorithms.c @@ -4,7 +4,7 @@ #define test_alg(ALG, VALUE_SIZE, EXPECTED_FROM_ZERO){\ printf("\e[94mrng algorithm: \e[96m" #ALG "\n");\ - ALG##_state s= ALG##_init(0);\ + void* s= ALG##_init(0);\ uint##VALUE_SIZE r=ALG##_next(s);\ printf("\e[97m next from zero seed:");\ if(r!=EXPECTED_FROM_ZERO){\ From d270554dfa53daca6572e9e692f8e33fb83801cd Mon Sep 17 00:00:00 2001 From: timerix Date: Mon, 24 Oct 2022 04:39:18 +0600 Subject: [PATCH 44/59] kprint moved out from base --- src/base/base.h | 1 - src/base/type_system/base_toString.c | 2 +- src/{base => }/kprint/README.md | 0 src/{base => }/kprint/kprint.c | 4 ++-- src/{base => }/kprint/kprint.h | 2 +- src/{base => }/kprint/kprint_colors.h | 0 src/{base => }/kprint/kprint_format.h | 4 ++-- src/{base => }/kprint/kprint_format.md | 0 tests/test_kprint_colors.c | 1 + 9 files changed, 7 insertions(+), 7 deletions(-) rename src/{base => }/kprint/README.md (100%) rename src/{base => }/kprint/kprint.c (99%) rename src/{base => }/kprint/kprint.h (98%) rename src/{base => }/kprint/kprint_colors.h (100%) rename src/{base => }/kprint/kprint_format.h (95%) rename src/{base => }/kprint/kprint_format.md (100%) diff --git a/src/base/base.h b/src/base/base.h index 9ca2951..e884a43 100644 --- a/src/base/base.h +++ b/src/base/base.h @@ -9,7 +9,6 @@ extern "C" { #include "cptr.h" #include "optime.h" #include "type_system/type_system.h" -#include "kprint/kprint.h" #if __cplusplus } diff --git a/src/base/type_system/base_toString.c b/src/base/type_system/base_toString.c index 2d49c9c..ee6d5d6 100644 --- a/src/base/type_system/base_toString.c +++ b/src/base/type_system/base_toString.c @@ -1,6 +1,6 @@ #include "base_toString.h" #include "../cptr.h" -#include "../kprint/kprint_format.h" +#include "../../kprint/kprint_format.h" char* toString_int(int64 n){ int64 d=n; diff --git a/src/base/kprint/README.md b/src/kprint/README.md similarity index 100% rename from src/base/kprint/README.md rename to src/kprint/README.md diff --git a/src/base/kprint/kprint.c b/src/kprint/kprint.c similarity index 99% rename from src/base/kprint/kprint.c rename to src/kprint/kprint.c index 744fc74..28de177 100644 --- a/src/base/kprint/kprint.c +++ b/src/kprint/kprint.c @@ -1,5 +1,5 @@ -#include "../base.h" -#include "../../String/StringBuilder.h" +#include "../String/StringBuilder.h" +#include "kprint.h" ktId __typeFromFormat(kprint_format f){ ktId typeId=kprint_format_ktId(f); diff --git a/src/base/kprint/kprint.h b/src/kprint/kprint.h similarity index 98% rename from src/base/kprint/kprint.h rename to src/kprint/kprint.h index 3acdd2f..68d1d2e 100644 --- a/src/base/kprint/kprint.h +++ b/src/kprint/kprint.h @@ -4,7 +4,7 @@ extern "C" { #endif -#include "../errors.h" +#include "../base/errors.h" #include "kprint_colors.h" #include "kprint_format.h" diff --git a/src/base/kprint/kprint_colors.h b/src/kprint/kprint_colors.h similarity index 100% rename from src/base/kprint/kprint_colors.h rename to src/kprint/kprint_colors.h diff --git a/src/base/kprint/kprint_format.h b/src/kprint/kprint_format.h similarity index 95% rename from src/base/kprint/kprint_format.h rename to src/kprint/kprint_format.h index 9568ac9..0a275ea 100644 --- a/src/base/kprint/kprint_format.h +++ b/src/kprint/kprint_format.h @@ -4,8 +4,8 @@ extern "C" { #endif -#include "../std.h" -#include "../type_system/ktId.h" +#include "../base/std.h" +#include "../base/type_system/ktId.h" typedef enum kprint_dataFormat{ // 00000000 00000000 00000000 00000000 diff --git a/src/base/kprint/kprint_format.md b/src/kprint/kprint_format.md similarity index 100% rename from src/base/kprint/kprint_format.md rename to src/kprint/kprint_format.md diff --git a/tests/test_kprint_colors.c b/tests/test_kprint_colors.c index e23b8d9..487d8f3 100644 --- a/tests/test_kprint_colors.c +++ b/tests/test_kprint_colors.c @@ -1,4 +1,5 @@ #include "tests.h" +#include "../src/kprint/kprint.h" #if defined(_WIN32)|| defined(_WIN64) #include #endif From 4222c3ebf6eb0aae12522cc02832d64bb8cbe405 Mon Sep 17 00:00:00 2001 From: timerix Date: Mon, 24 Oct 2022 15:25:13 +0600 Subject: [PATCH 45/59] base_toString --- src/base/type_system/README.md | 2 ++ src/base/type_system/base_toString.c | 23 +++++++++++++ src/base/type_system/base_toString.h | 7 ++-- src/base/type_system/init.c | 49 ++++++++++++++-------------- src/base/type_system/kt_functions.c | 4 +++ src/base/type_system/kt_functions.h | 2 ++ tests/main.cpp | 1 + 7 files changed, 62 insertions(+), 26 deletions(-) diff --git a/src/base/type_system/README.md b/src/base/type_system/README.md index 587291c..356cd5d 100644 --- a/src/base/type_system/README.md +++ b/src/base/type_system/README.md @@ -25,6 +25,8 @@ 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()`. + +You can free internal ktDescriptors storage by calling `ktDescriptors_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) diff --git a/src/base/type_system/base_toString.c b/src/base/type_system/base_toString.c index ee6d5d6..a4be0ca 100644 --- a/src/base/type_system/base_toString.c +++ b/src/base/type_system/base_toString.c @@ -2,6 +2,26 @@ #include "../cptr.h" #include "../../kprint/kprint_format.h" +char* __toString_char(void* c, uint32 fmt) { + char* cc=malloc(2); + cc[0]=*(char*)c; + cc[1]=0; + return cc; +} + +char* __toString_bool(void* c, uint32 fmt) { + static const char _strbool[4][6]={ "false", "true\0", "False", "True\0" }; + uint8 strind=*(bool*)c==1 + kprint_format_uppercase(fmt)*2; + char* rez=malloc(6); + rez[0]=_strbool[strind][0]; + rez[1]=_strbool[strind][1]; + rez[2]=_strbool[strind][2]; + rez[3]=_strbool[strind][3]; + rez[4]=_strbool[strind][4]; + rez[5]=0; + return rez; +} + char* toString_int(int64 n){ int64 d=n; char str[32]; @@ -151,3 +171,6 @@ __toString_uint_def(64) return NULL;\ }\ } + +__toString_float_def(32) +__toString_float_def(64) diff --git a/src/base/type_system/base_toString.h b/src/base/type_system/base_toString.h index c3831cd..a0ed7ac 100644 --- a/src/base/type_system/base_toString.h +++ b/src/base/type_system/base_toString.h @@ -7,16 +7,19 @@ extern "C" { #include "../errors.h" // functions for base types +char* __toString_char(void* c, uint32 fmt); +char* __toString_bool(void* c, uint32 fmt); + char* toString_int(int64 n); char* __toString_int8(void* n, uint32 fmt); char* __toString_int16(void* n, uint32 fmt); -char* __toString_uint32(void* n, uint32 fmt); +char* __toString_int32(void* n, uint32 fmt); char* __toString_int64(void* n, uint32 fmt); char* toString_uint(uint64 n, bool withPostfix, bool uppercase); char* __toString_uint8(void* n, uint32 fmt); char* __toString_uint16(void* n, uint32 fmt); -char* __toString_uuint32(void* n, uint32 fmt); +char* __toString_uint32(void* n, uint32 fmt); char* __toString_uint64(void* n, uint32 fmt); char* toString_float(float64 n, bool withPostfix, bool uppercase); // very bad implimentation diff --git a/src/base/type_system/init.c b/src/base/type_system/init.c index 3e964dc..0f1b18b 100644 --- a/src/base/type_system/init.c +++ b/src/base/type_system/init.c @@ -9,31 +9,32 @@ void ktDescriptors_initKerepTypes(){ // null kt_register(NULL, ktId_Null, NULL, NULL); // base types - kt_register(char, ktId_Char, NULL, NULL); - kt_register(bool, ktId_Bool, NULL, NULL); - kt_register(float32, ktId_Float32, NULL, NULL); - kt_register(float64, ktId_Float64, NULL, NULL); - kt_register(int8, ktId_Int8, NULL, NULL); - kt_register(uint8, ktId_UInt8, NULL, NULL); - kt_register(int16, ktId_Int16, NULL, NULL); - kt_register(uint16, ktId_UInt16, NULL, NULL); - kt_register(int32, ktId_Int32, NULL, NULL); - kt_register(uint32, ktId_UInt32, NULL, NULL); - kt_register(int64, ktId_Int64, NULL, NULL); - kt_register(uint64, ktId_UInt64, NULL, NULL); + kt_register(char, ktId_Char, NULL, __toString_char); + kt_register(bool, ktId_Bool, NULL, __toString_bool); + kt_register(float32, ktId_Float32, NULL, __toString_float32); + kt_register(float64, ktId_Float64, NULL, __toString_float64); + kt_register(int8, ktId_Int8, NULL, __toString_int8); + kt_register(uint8, ktId_UInt8, NULL, __toString_uint8); + kt_register(int16, ktId_Int16, NULL, __toString_int16); + kt_register(uint16, ktId_UInt16, NULL, __toString_uint16); + kt_register(int32, ktId_Int32, NULL, __toString_int32); + kt_register(uint32, ktId_UInt32, NULL, __toString_uint32); + kt_register(int64, ktId_Int64, NULL, __toString_int64); + kt_register(uint64, ktId_UInt64, NULL, __toString_uint64); // base type pointers - kt_register(char*, ktId_CharPtr, NULL, NULL); - kt_register(bool*, ktId_BoolPtr, NULL, NULL); - kt_register(float32*, ktId_Float32Ptr, NULL, NULL); - kt_register(float64*, ktId_Float64Ptr, NULL, NULL); - kt_register(int8*, ktId_Int8Ptr, NULL, NULL); - kt_register(uint8*, ktId_UInt8Ptr, NULL, NULL); - kt_register(int16*, ktId_Int16Ptr, NULL, NULL); - kt_register(uint16*, ktId_UInt16Ptr, NULL, NULL); - kt_register(int32*, ktId_Int32Ptr, NULL, NULL); - kt_register(uint32*, ktId_UInt32Ptr, NULL, NULL); - kt_register(int64*, ktId_Int64Ptr, NULL, NULL); - kt_register(uint64*, ktId_UInt64Ptr, NULL, NULL); + kt_register(char*, ktId_CharPtr, NULL, __toString_char); + kt_register(bool*, ktId_BoolPtr, NULL, __toString_bool); + kt_register(float32*, ktId_Float32Ptr, NULL, __toString_float32); + kt_register(float64*, ktId_Float64Ptr, NULL, __toString_float64); + kt_register(int8*, ktId_Int8Ptr, NULL, __toString_int8); + kt_register(uint8*, ktId_UInt8Ptr, NULL, __toString_uint8); + kt_register(int16*, ktId_Int16Ptr, NULL, __toString_int16); + kt_register(uint16*, ktId_UInt16Ptr, NULL, __toString_uint16); + kt_register(int32*, ktId_Int32Ptr, NULL, __toString_int32); + kt_register(uint32*, ktId_UInt32Ptr, NULL, __toString_uint32); + kt_register(int64*, ktId_Int64Ptr, NULL, __toString_int64); + kt_register(uint64*, ktId_UInt64Ptr, NULL, __toString_uint64); + // ktDescriptor kt_register(ktDescriptor, ktId_ktDescriptor, NULL, NULL); kt_register(ktDescriptor*, ktId_ktDescriptorPtr, NULL, NULL); diff --git a/src/base/type_system/kt_functions.c b/src/base/type_system/kt_functions.c index a459d2a..5d3d014 100644 --- a/src/base/type_system/kt_functions.c +++ b/src/base/type_system/kt_functions.c @@ -76,3 +76,7 @@ ktDescriptor ktDescriptor_get(ktId id){ } return typeDescriptors[id]; } + +void ktDescriptors_free(){ + free(typeDescriptors); +} diff --git a/src/base/type_system/kt_functions.h b/src/base/type_system/kt_functions.h index 7274727..cf27d3e 100644 --- a/src/base/type_system/kt_functions.h +++ b/src/base/type_system/kt_functions.h @@ -21,6 +21,8 @@ void ktDescriptors_endInit(); /// @param id id of registered type ktDescriptor ktDescriptor_get(ktId id); +// call it to free heap-allocated ktDescriptors array +void ktDescriptors_free(); ktId_declare(Null); diff --git a/tests/main.cpp b/tests/main.cpp index 0ba2387..99c7197 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -22,6 +22,7 @@ int main(){ // optime("test_all",1,test_all()); // test_kprint_colors(); test_rng_algorithms(); + ktDescriptors_free(); printf("\e[0m\n"); return 0; } From bbeb6bea1d9a929f065f8f035ae8bf5094f9be27 Mon Sep 17 00:00:00 2001 From: timerix Date: Mon, 24 Oct 2022 15:59:01 +0600 Subject: [PATCH 46/59] fixed memory leaks --- src/random/krandom.h | 14 +++++++++++--- src/random/splitmix64/splitmix64.h | 3 +++ src/random/xoroshiro/32bitValue/xoroshiro64.h | 6 ++++++ src/random/xoroshiro/32bitValue/xoroshiro64star.c | 1 + src/random/xoroshiro/64bitValue/xoroshiro128.h | 7 +++++++ src/random/xoroshiro/64bitValue/xoroshiro128plus.c | 1 + src/random/xoshiro/32bitValue/xoshiro128.h | 7 +++++++ src/random/xoshiro/32bitValue/xoshiro128plus.c | 1 + src/random/xoshiro/64bitValue/xoshiro256.h | 7 +++++++ src/random/xoshiro/64bitValue/xoshiro256plus.c | 1 + tests/test_rng_algorithms.c | 8 +++++--- 11 files changed, 50 insertions(+), 6 deletions(-) diff --git a/src/random/krandom.h b/src/random/krandom.h index ce44a39..d590486 100644 --- a/src/random/krandom.h +++ b/src/random/krandom.h @@ -12,11 +12,13 @@ extern "C" { /* You can choose any algorithm that has required functions: - some_alg32_state some_alg32_init(uint32 seed); - uint32 some_alg32_next(some_alg32_state); + some_alg32_statePtr some_alg32_init(uint32 seed); + uint32 some_alg32_next(some_alg32_statePtr); + void some_alg32_free(some_alg32_statePtr); - #define KRAND_ALG32_next some_alg32_next #define KRAND_ALG32_init some_alg32_init + #define KRAND_ALG32_next some_alg32_next + #define KRAND_ALG32_free some_alg32_free #include "kerep/random/krandom.h" The same way it works for 64-bit RNGs @@ -29,12 +31,18 @@ The same way it works for 64-bit RNGs #ifndef KRAND_ALG32_init #define KRAND_ALG32_init xoshiro128plus##_init #endif +#ifndef KRAND_ALG32_free +#define KRAND_ALG32_free xoshiro128plus##_free +#endif #ifndef KRAND_ALG64_next #define KRAND_ALG64_next xoshiro256plus##_next #endif #ifndef KRAND_ALG64_init #define KRAND_ALG64_init xoshiro256plus##_init #endif +#ifndef KRAND_ALG64_free +#define KRAND_ALG64_free xoshiro256plus##_free +#endif typedef void* krand_statePtr; #define KRAND_ALG32_initFromTime xoshiro128plus##_initFromTime diff --git a/src/random/splitmix64/splitmix64.h b/src/random/splitmix64/splitmix64.h index ca9e77f..8cf8104 100644 --- a/src/random/splitmix64/splitmix64.h +++ b/src/random/splitmix64/splitmix64.h @@ -13,6 +13,9 @@ splitmix64_statePtr splitmix64_init(uint64 seed); static inline splitmix64_statePtr splitmix64_initFromTime(void) { return splitmix64_init(time(NULL)); } uint64 splitmix64_next(splitmix64_statePtr); +static inline void splitmix64_free(splitmix64_statePtr state) { + free(state); +} #if __cplusplus } diff --git a/src/random/xoroshiro/32bitValue/xoroshiro64.h b/src/random/xoroshiro/32bitValue/xoroshiro64.h index e4295fe..6377bba 100644 --- a/src/random/xoroshiro/32bitValue/xoroshiro64.h +++ b/src/random/xoroshiro/32bitValue/xoroshiro64.h @@ -24,6 +24,12 @@ static inline xoroshiro64_statePtr xoroshiro64_initFromTime(void) { return xoros uint32 xoroshiro64star_next(xoroshiro64_statePtr); uint32 xoroshiro64starstar_next(xoroshiro64_statePtr); +static inline void xoroshiro64_free(xoroshiro64_statePtr state) { + free(state); +} +#define xoroshiro64star_free xoroshiro64_free +#define xoroshiro64starstar_free xoroshiro64_free + #if __cplusplus } #endif \ No newline at end of file diff --git a/src/random/xoroshiro/32bitValue/xoroshiro64star.c b/src/random/xoroshiro/32bitValue/xoroshiro64star.c index 9b909a0..7c2fd3c 100644 --- a/src/random/xoroshiro/32bitValue/xoroshiro64star.c +++ b/src/random/xoroshiro/32bitValue/xoroshiro64star.c @@ -44,5 +44,6 @@ void* xoroshiro64_init(uint64 seed){ xoroshiro64_state* state=malloc(sizeof(xoroshiro64_state)); splitmix64_state* splitmix=splitmix64_init(seed); state->merged=splitmix64_next(splitmix); + splitmix64_free(splitmix); return state; } diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128.h b/src/random/xoroshiro/64bitValue/xoroshiro128.h index 46d02e5..63ed537 100644 --- a/src/random/xoroshiro/64bitValue/xoroshiro128.h +++ b/src/random/xoroshiro/64bitValue/xoroshiro128.h @@ -27,6 +27,13 @@ uint64 xoroshiro128plus_next(xoroshiro128_statePtr); uint64 xoroshiro128plusplus_next(xoroshiro128_statePtr); uint64 xoroshiro128starstar_next(xoroshiro128_statePtr); +static inline void xoroshiro128_free(xoroshiro128_statePtr state) { + free(state); +} +#define xoroshiro128plus_free xoroshiro128_free +#define xoroshiro128plusplus_free xoroshiro128_free +#define xoroshiro128starstar_free xoroshiro128_free + #if __cplusplus } #endif \ No newline at end of file diff --git a/src/random/xoroshiro/64bitValue/xoroshiro128plus.c b/src/random/xoroshiro/64bitValue/xoroshiro128plus.c index 2846024..e01e0b5 100644 --- a/src/random/xoroshiro/64bitValue/xoroshiro128plus.c +++ b/src/random/xoroshiro/64bitValue/xoroshiro128plus.c @@ -54,5 +54,6 @@ void* xoroshiro128_init(uint64 seed){ splitmix64_state* splitmix=splitmix64_init(seed); state->s[0]=splitmix64_next(splitmix); state->s[1]=splitmix64_next(splitmix); + splitmix64_free(splitmix); return state; } diff --git a/src/random/xoshiro/32bitValue/xoshiro128.h b/src/random/xoshiro/32bitValue/xoshiro128.h index ebba10f..02d52e8 100644 --- a/src/random/xoshiro/32bitValue/xoshiro128.h +++ b/src/random/xoshiro/32bitValue/xoshiro128.h @@ -28,6 +28,13 @@ uint32 xoshiro128plus_next(xoshiro128_statePtr); uint32 xoshiro128plusplus_next(xoshiro128_statePtr); uint32 xoshiro128starstar_next(xoshiro128_statePtr); +static inline void xoshiro128_free(xoshiro128_statePtr state) { + free(state); +} +#define xoshiro128plus_free xoshiro128_free +#define xoshiro128plusplus_free xoshiro128_free +#define xoshiro128starstar_free xoshiro128_free + #if __cplusplus } #endif \ No newline at end of file diff --git a/src/random/xoshiro/32bitValue/xoshiro128plus.c b/src/random/xoshiro/32bitValue/xoshiro128plus.c index 540c71e..8cafc8b 100644 --- a/src/random/xoshiro/32bitValue/xoshiro128plus.c +++ b/src/random/xoshiro/32bitValue/xoshiro128plus.c @@ -49,5 +49,6 @@ void* xoshiro128_init(uint64 seed){ splitmix64_state* splitmix=splitmix64_init(seed); state->merged[0]=splitmix64_next(splitmix); state->merged[1]=splitmix64_next(splitmix); + splitmix64_free(splitmix); return state; } diff --git a/src/random/xoshiro/64bitValue/xoshiro256.h b/src/random/xoshiro/64bitValue/xoshiro256.h index 0cbae44..479c6aa 100644 --- a/src/random/xoshiro/64bitValue/xoshiro256.h +++ b/src/random/xoshiro/64bitValue/xoshiro256.h @@ -27,6 +27,13 @@ uint64 xoshiro256plus_next(xoshiro256_statePtr); uint64 xoshiro256plusplus_next(xoshiro256_statePtr); uint64 xoshiro256starstar_next(xoshiro256_statePtr); +static inline void xoshiro256_free(xoshiro256_statePtr state) { + free(state); +} +#define xoshiro256plus_free xoshiro256_free +#define xoshiro256plusplus_free xoshiro256_free +#define xoshiro256starstar_free xoshiro256_free + #if __cplusplus } #endif \ No newline at end of file diff --git a/src/random/xoshiro/64bitValue/xoshiro256plus.c b/src/random/xoshiro/64bitValue/xoshiro256plus.c index ba034be..066ef8a 100644 --- a/src/random/xoshiro/64bitValue/xoshiro256plus.c +++ b/src/random/xoshiro/64bitValue/xoshiro256plus.c @@ -53,5 +53,6 @@ void* xoshiro256_init(uint64 seed){ state->s[1]=splitmix64_next(splitmix); state->s[2]=splitmix64_next(splitmix); state->s[3]=splitmix64_next(splitmix); + splitmix64_free(splitmix); return state; } diff --git a/tests/test_rng_algorithms.c b/tests/test_rng_algorithms.c index eae4dfb..dfc0a6a 100644 --- a/tests/test_rng_algorithms.c +++ b/tests/test_rng_algorithms.c @@ -8,13 +8,15 @@ uint##VALUE_SIZE r=ALG##_next(s);\ printf("\e[97m next from zero seed:");\ if(r!=EXPECTED_FROM_ZERO){\ - printf("\e[91m %llu\n", (uint64)r);\ + printf("\e[91m " IFWIN("%llu\n","%lu\n"), (uint64)r);\ throw(ERR_UNEXPECTEDVAL);\ }\ - printf("\e[92m %llu\n", (uint64)r);\ + printf("\e[92m " IFWIN("%llu\n","%lu\n"), (uint64)r);\ + ALG##_free(s);\ s= ALG##_initFromTime();\ r=ALG##_next(s);\ - printf("\e[97m next from time seed:\e[92m %llu\n", (uint64)r);\ + ALG##_free(s);\ + printf("\e[97m next from time seed:\e[92m " IFWIN("%llu\n","%lu\n"), (uint64)r);\ } void test_rng_algorithms(){ From 49f1931a348c973b286802693409b667f3db5e38 Mon Sep 17 00:00:00 2001 From: timerix Date: Mon, 24 Oct 2022 17:40:08 +0600 Subject: [PATCH 47/59] kprint now works --- default.config | 4 +-- src/base/cptr.c | 2 +- src/base/type_system/base_toString.c | 41 ++++++++++++++++------------ src/base/type_system/base_toString.h | 1 + src/base/type_system/init.c | 24 ++++++++-------- src/base/type_system/kt_functions.c | 2 +- src/kprint/kprint.c | 18 +++++++----- src/kprint/kprint.h | 24 ++++++++++------ tests/main.cpp | 4 +-- tests/test_kprint_colors.c | 2 ++ 10 files changed, 71 insertions(+), 51 deletions(-) diff --git a/default.config b/default.config index 16d1c6b..37c5883 100644 --- a/default.config +++ b/default.config @@ -1,5 +1,5 @@ #!/bin/bash -CONFIG_VERSION=2 +CONFIG_VERSION=3 CBUILD_VERSION=2 PROJECT=kerep @@ -7,7 +7,7 @@ CMP_C=gcc CMP_CPP=g++ STD_C=c11 STD_CPP=c++17 -WARN_C="-Wall -Wno-discarded-qualifiers" +WARN_C="-Wall -Wno-discarded-qualifiers -Wno-int-conversion" WARN_CPP="-Wall" SRC_C="$( find src -name '*.c')" SRC_CPP="$( find src -name '*.cpp')" diff --git a/src/base/cptr.c b/src/base/cptr.c index cba9814..53872cd 100644 --- a/src/base/cptr.c +++ b/src/base/cptr.c @@ -52,7 +52,7 @@ void memcopy(void* from, void* to, uint32 size){ if(from==NULL || to==NULL) throw(ERR_NULLPTR); for(uint32 i=0; i"); } char* toString_bin(char* bytes, uint32 size, bool withPrefix){ diff --git a/src/base/type_system/base_toString.h b/src/base/type_system/base_toString.h index a0ed7ac..5700302 100644 --- a/src/base/type_system/base_toString.h +++ b/src/base/type_system/base_toString.h @@ -8,6 +8,7 @@ extern "C" { // functions for base types char* __toString_char(void* c, uint32 fmt); +char* __toString_charPtr(void* c, uint32 fmt); char* __toString_bool(void* c, uint32 fmt); char* toString_int(int64 n); diff --git a/src/base/type_system/init.c b/src/base/type_system/init.c index 0f1b18b..91f592e 100644 --- a/src/base/type_system/init.c +++ b/src/base/type_system/init.c @@ -22,18 +22,18 @@ void ktDescriptors_initKerepTypes(){ kt_register(int64, ktId_Int64, NULL, __toString_int64); kt_register(uint64, ktId_UInt64, NULL, __toString_uint64); // base type pointers - kt_register(char*, ktId_CharPtr, NULL, __toString_char); - kt_register(bool*, ktId_BoolPtr, NULL, __toString_bool); - kt_register(float32*, ktId_Float32Ptr, NULL, __toString_float32); - kt_register(float64*, ktId_Float64Ptr, NULL, __toString_float64); - kt_register(int8*, ktId_Int8Ptr, NULL, __toString_int8); - kt_register(uint8*, ktId_UInt8Ptr, NULL, __toString_uint8); - kt_register(int16*, ktId_Int16Ptr, NULL, __toString_int16); - kt_register(uint16*, ktId_UInt16Ptr, NULL, __toString_uint16); - kt_register(int32*, ktId_Int32Ptr, NULL, __toString_int32); - kt_register(uint32*, ktId_UInt32Ptr, NULL, __toString_uint32); - kt_register(int64*, ktId_Int64Ptr, NULL, __toString_int64); - kt_register(uint64*, ktId_UInt64Ptr, NULL, __toString_uint64); + kt_register(char*, ktId_CharPtr, NULL, __toString_charPtr); + kt_register(bool*, ktId_BoolPtr, NULL, NULL); + kt_register(float32*, ktId_Float32Ptr, NULL, NULL); + kt_register(float64*, ktId_Float64Ptr, NULL, NULL); + kt_register(int8*, ktId_Int8Ptr, NULL, NULL); + kt_register(uint8*, ktId_UInt8Ptr, NULL, NULL); + kt_register(int16*, ktId_Int16Ptr, NULL, NULL); + kt_register(uint16*, ktId_UInt16Ptr, NULL, NULL); + kt_register(int32*, ktId_Int32Ptr, NULL, NULL); + kt_register(uint32*, ktId_UInt32Ptr, NULL, NULL); + kt_register(int64*, ktId_Int64Ptr, NULL, NULL); + kt_register(uint64*, ktId_UInt64Ptr, NULL, NULL); // ktDescriptor kt_register(ktDescriptor, ktId_ktDescriptor, NULL, NULL); diff --git a/src/base/type_system/kt_functions.c b/src/base/type_system/kt_functions.c index 5d3d014..c3afe91 100644 --- a/src/base/type_system/kt_functions.c +++ b/src/base/type_system/kt_functions.c @@ -71,7 +71,7 @@ void __kt_register(char* name, int16 size, void (*freeMembers)(void*), char* (*t ktDescriptor ktDescriptor_get(ktId id){ if(id>ktId_last) { - printf("\n%u\n",id); + printf("\ntype id: %u\n",id); throw("invalid type id"); } return typeDescriptors[id]; diff --git a/src/kprint/kprint.c b/src/kprint/kprint.c index 28de177..1f1ec21 100644 --- a/src/kprint/kprint.c +++ b/src/kprint/kprint.c @@ -23,7 +23,7 @@ ktId __typeFromFormat(kprint_format f){ } } -Maybe __next_toString(kprint_format f, void* object){ +Maybe __next_toString(kprint_format f, __kprint_value_union* object){ // detecting type ktId typeId=__typeFromFormat(f); if(typeId==-1) @@ -34,19 +34,22 @@ Maybe __next_toString(kprint_format f, void* object){ return SUCCESS(UniHeap(ktId_CharPtr, typeDesc.toString(object, f))); } -Maybe __ksprint(uint8 n, kprint_format* formats, void** objects){ +Maybe __ksprint(uint8 n, kprint_format* formats, __kprint_value_union* objects){ + n/=2; StringBuilder* strb=StringBuilder_create(); for(uint8 i=0; i Date: Mon, 24 Oct 2022 17:44:29 +0600 Subject: [PATCH 48/59] fate() inlined --- src/random/krandom.c | 6 ------ src/random/krandom.h | 5 ++++- 2 files changed, 4 insertions(+), 7 deletions(-) delete mode 100644 src/random/krandom.c diff --git a/src/random/krandom.c b/src/random/krandom.c deleted file mode 100644 index 3801717..0000000 --- a/src/random/krandom.c +++ /dev/null @@ -1,6 +0,0 @@ -#include "krandom.h" - -bool fate(float chance){ - int limit=1/chance + 0.01f; - return rand()%limit == 0; -} diff --git a/src/random/krandom.h b/src/random/krandom.h index d590486..d78b7d6 100644 --- a/src/random/krandom.h +++ b/src/random/krandom.h @@ -62,7 +62,10 @@ static inline float64 krand_nextFloat64(krand_statePtr state) {return KRAND_ALG6 ///@param chance (0-1.0) is probability of success -bool fate(float chance); +static inline bool fate(krand_statePtr state,float chance){ + int limit=1/chance + 0.01f; + return KRAND_ALG32_next(state)%limit == 0; +} #if __cplusplus } From c70544ff979cefa676e02a6ae240401cec2e4d65 Mon Sep 17 00:00:00 2001 From: timerix Date: Mon, 24 Oct 2022 18:06:51 +0600 Subject: [PATCH 49/59] kprintf --- src/base/type_system/base_toString.c | 6 ++- src/kprint/kprint.c | 57 ++++++++++++++++++++++++++++ src/kprint/kprint.h | 3 ++ tests/test_kprint_colors.c | 4 +- 4 files changed, 66 insertions(+), 4 deletions(-) diff --git a/src/base/type_system/base_toString.c b/src/base/type_system/base_toString.c index 80bdc38..faed420 100644 --- a/src/base/type_system/base_toString.c +++ b/src/base/type_system/base_toString.c @@ -76,7 +76,8 @@ char* toString_float(float64 n, bool withPostfix, bool uppercase){ return cptr_copy(""); } -char* toString_bin(char* bytes, uint32 size, bool withPrefix){ +char* toString_bin(void* _bytes, uint32 size, bool withPrefix){ + char* bytes=_bytes; char* str=malloc(size*8 + (withPrefix?2:0) +1); uint32 cn=0; if(withPrefix){ @@ -104,7 +105,8 @@ char _4bitsHex(uint8 u, bool uppercase){ } } -char* toString_hex(char* bytes, uint32 size, bool withPrefix, bool uppercase){ +char* toString_hex(void* _bytes, uint32 size, bool withPrefix, bool uppercase){ + char* bytes=_bytes; char* str=malloc(size*2 + (withPrefix?2:0) + 1); uint32 cn=0; if(withPrefix){ diff --git a/src/kprint/kprint.c b/src/kprint/kprint.c index 1f1ec21..dcca931 100644 --- a/src/kprint/kprint.c +++ b/src/kprint/kprint.c @@ -1,5 +1,6 @@ #include "../String/StringBuilder.h" #include "kprint.h" +#include "../base/type_system/base_toString.h" ktId __typeFromFormat(kprint_format f){ ktId typeId=kprint_format_ktId(f); @@ -166,3 +167,59 @@ void kprint_setColor(kprint_format f){ StringBuilder_append_char(strb, ' '); StringBuilder_append_char(strb, ']'); } */ + +void kprintf(char* format, ...){ + va_list vl; + va_start(vl, format); + char c; + while((c=*format++)){ + if(c=='%'){ + c=*format++; + switch (c) { + case 'u': + toString_uint(va_arg(vl, uint64),0,0); + break; + case 'i': + toString_int(va_arg(vl, uint64)); + break; + case 'f': + toString_float(va_arg(vl, float64),0,0); + break; + /* case 'l': + c=*format++; + switch (c) { + case 'u': + toString_uint(va_arg(vl, uint64),0,0); + break; + case 'i': + toString_int(va_arg(vl, uint64)); + break; + case 'f': + toString_float(va_arg(vl, float64),0,0); + break; + default: + throw(ERR_FORMAT); + } + break; */ + case 'p': + case 'x': + uint64 px=va_arg(vl, uint64); + toString_hex(&px,sizeof(px),1,0); + break; + + default: + throw(ERR_FORMAT); + } + } else if(c=='\e'){ + IFWIN( + ({ + + }), + putc(c,stdout); + ) + } else { + putc(c,stdout); + } + } + va_end(vl); +} diff --git a/src/kprint/kprint.h b/src/kprint/kprint.h index 6e0587a..13905ad 100644 --- a/src/kprint/kprint.h +++ b/src/kprint/kprint.h @@ -65,6 +65,9 @@ Maybe __kfprint(FILE* fd, uint8 n, kprint_format* formats, __kprint_value_union* void __kprint(uint8 n, kprint_format* formats, __kprint_value_union* objects); #define kprint(ARGS...) __kprint(count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) +// cross-platform printf analog +void kprintf(char* format, ...); + // can take (bgColor | fgColor) void kprint_setColor(kprint_format f); diff --git a/tests/test_kprint_colors.c b/tests/test_kprint_colors.c index 86eedc8..3cdb860 100644 --- a/tests/test_kprint_colors.c +++ b/tests/test_kprint_colors.c @@ -13,7 +13,7 @@ printf("\n"); void test_kprint_colors(){ - IFWIN( + /* IFWIN( ({ HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); for(uint8 col=0; col<255; col++){ @@ -26,7 +26,7 @@ void test_kprint_colors(){ printf("\e[%um%u ", col, col); }) ); - printf("\n"); + printf("\n"); */ testColor(Black); testColor(DarkRed); From 4785023126aa8ef06fbab7669d4bccb351f53fff Mon Sep 17 00:00:00 2001 From: timerix Date: Mon, 24 Oct 2022 18:33:43 +0600 Subject: [PATCH 50/59] all printf calls replaced with kprintf --- src/Hashtable/KeyValuePair.c | 4 +- src/base/base.h | 1 + src/base/errors.c | 3 +- src/base/errors.h | 2 +- src/base/optime.h | 4 +- src/base/std.h | 2 +- src/base/type_system/base_toString.c | 8 +-- src/base/type_system/base_toString.h | 4 +- src/base/type_system/kt_functions.c | 6 +-- src/kprint/README.md | 3 ++ src/kprint/kprint.c | 65 ++----------------------- src/kprint/kprint.h | 3 -- src/kprint/kprintf.c | 73 ++++++++++++++++++++++++++++ src/kprint/kprintf.h | 12 +++++ tests/main.cpp | 6 +-- tests/test_autoarr-vs-vector.cpp | 4 +- tests/test_autoarr.c | 18 +++---- tests/test_dtsod.c | 16 +++--- tests/test_hash_functions.c | 10 ++-- tests/test_hashtable.c | 12 ++--- tests/test_kprint_colors.c | 12 ++--- tests/test_marshalling.c | 2 +- tests/test_rng_algorithms.c | 12 ++--- tests/test_safethrow.c | 10 ++-- tests/test_searchtree.c | 50 +++++++++---------- tests/test_string.c | 4 +- tests/tests.h | 2 +- 27 files changed, 189 insertions(+), 159 deletions(-) create mode 100644 src/kprint/kprintf.c create mode 100644 src/kprint/kprintf.h diff --git a/src/Hashtable/KeyValuePair.c b/src/Hashtable/KeyValuePair.c index 07b3edc..667487e 100644 --- a/src/Hashtable/KeyValuePair.c +++ b/src/Hashtable/KeyValuePair.c @@ -24,7 +24,7 @@ void ____Autoarr_free_KVPair_(void* ar){ } void printkvp(KVPair p){ - printf("{\"%s\", ",p.key); + kprintf("{\"%s\", ",p.key); printuni(p.value); - printf("}"); + kprintf("}"); } diff --git a/src/base/base.h b/src/base/base.h index e884a43..7263d4b 100644 --- a/src/base/base.h +++ b/src/base/base.h @@ -9,6 +9,7 @@ extern "C" { #include "cptr.h" #include "optime.h" #include "type_system/type_system.h" +#include "../kprint/kprintf.h" #if __cplusplus } diff --git a/src/base/errors.c b/src/base/errors.c index 4d0d2be..cf49d31 100644 --- a/src/base/errors.c +++ b/src/base/errors.c @@ -1,6 +1,7 @@ #include "std.h" #include "errors.h" #include "cptr.h" +#include "../kprint/kprintf.h" char* errname(ErrorId err){ switch(err){ @@ -47,7 +48,7 @@ void Maybe_free(Maybe e){ } void printMaybe(Maybe e){ - if(e.errmsg) printf("%s\n",e.errmsg); + if(e.errmsg) kprintf("%s\n",e.errmsg); else printuni(e.value); } diff --git a/src/base/errors.h b/src/base/errors.h index 4abc7df..6b59f5d 100644 --- a/src/base/errors.h +++ b/src/base/errors.h @@ -36,7 +36,7 @@ void printMaybe(Maybe e); #define __RETURN_EXCEPTION(ERRMSG) return (Maybe){.errmsg=ERRMSG, .value=UniNull} -#define __EXIT(ERRMSG) ({ printf("\e[91m%s\e[0m \n", ERRMSG); free(ERRMSG); exit(128); }) +#define __EXIT(ERRMSG) ({ kprintf("\e[91m%s\e[0m \n", ERRMSG); free(ERRMSG); exit(128); }) char* __doNothing(char* a); char* __unknownErr( ); diff --git a/src/base/optime.h b/src/base/optime.h index bfe9f4d..f1b1c48 100644 --- a/src/base/optime.h +++ b/src/base/optime.h @@ -11,7 +11,7 @@ (codeblock);\ clock_gettime(CLOCK_REALTIME, &stop);\ double t=(double)(stop.tv_sec-start.tv_sec+(double)(stop.tv_nsec-start.tv_nsec)/1000000000)/repeats;\ - printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ + kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ }) #else // standard low precision clock #define optime(opname,repeats,codeblock) ({\ @@ -20,6 +20,6 @@ (codeblock);\ clock_t stop=clock();\ double t=(double)(stop-start)/CLOCKS_PER_SEC/repeats;\ - printf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ + kprintf("\e[93moperation \e[94m%s\e[93m lasted \e[94m%lf \e[93mseconds\n",opname,t);\ }) #endif diff --git a/src/base/std.h b/src/base/std.h index 061ed62..6e9f37e 100644 --- a/src/base/std.h +++ b/src/base/std.h @@ -24,7 +24,7 @@ typedef uint64_t uint64; typedef float float32; typedef double float64; -#define dbg(N) printf("\e[95m%d\n",N) +#define dbg(N) kprintf("\e[95m%d\n",N) #ifdef _MSC_VER #pragma comment(lib, "mincore_downlevel.lib") // Support OS older than SDK diff --git a/src/base/type_system/base_toString.c b/src/base/type_system/base_toString.c index faed420..178edb0 100644 --- a/src/base/type_system/base_toString.c +++ b/src/base/type_system/base_toString.c @@ -1,5 +1,5 @@ #include "base_toString.h" -#include "../cptr.h" +#include "../base.h" #include "../../kprint/kprint_format.h" char* __toString_char(void* c, uint32 fmt) { @@ -133,7 +133,7 @@ char* toString_hex(void* _bytes, uint32 size, bool withPrefix, bool uppercase){ case kprint_fmtHex:\ return toString_hex(_n, BITS/8, kprint_format_withPrefix(f), kprint_format_uppercase(f));\ default:\ - printf("\n%u\n", kprint_format_dataFormat(f));\ + kprintf("\n%u\n", kprint_format_dataFormat(f));\ throw(ERR_FORMAT);\ return NULL;\ }\ @@ -153,7 +153,7 @@ __toString_int_def(64) case kprint_fmtHex:\ return toString_hex(_n, BITS/8, kprint_format_withPrefix(f), kprint_format_uppercase(f));\ default:\ - printf("\n%u\n", kprint_format_dataFormat(f));\ + kprintf("\n%u\n", kprint_format_dataFormat(f));\ throw(ERR_FORMAT);\ return NULL;\ }\ @@ -173,7 +173,7 @@ __toString_uint_def(64) case kprint_fmtHex:\ return toString_hex(_n, BITS/8, kprint_format_withPrefix(f), kprint_format_uppercase(f));\ default:\ - printf("\n%u\n", kprint_format_dataFormat(f));\ + kprintf("\n%u\n", kprint_format_dataFormat(f));\ throw(ERR_FORMAT);\ return NULL;\ }\ diff --git a/src/base/type_system/base_toString.h b/src/base/type_system/base_toString.h index 5700302..6fa335b 100644 --- a/src/base/type_system/base_toString.h +++ b/src/base/type_system/base_toString.h @@ -28,8 +28,8 @@ char* __toString_float32(void* n, uint32 fmt); char* __toString_float64(void* n, uint32 fmt); // universal functions -char* toString_bin(char* bytes, uint32 size, bool withPrefix); -char* toString_hex(char* bytes, uint32 size, bool withPrefix, bool uppercase); +char* toString_bin(void* bytes, uint32 size, bool withPrefix); +char* toString_hex(void* bytes, uint32 size, bool withPrefix, bool uppercase); #if __cplusplus } diff --git a/src/base/type_system/kt_functions.c b/src/base/type_system/kt_functions.c index c3afe91..fa4a26d 100644 --- a/src/base/type_system/kt_functions.c +++ b/src/base/type_system/kt_functions.c @@ -46,7 +46,7 @@ typedef enum{ ktDescriptorsState initState=NotInitialized; void ktDescriptors_beginInit(){ - printf("\e[94mtype descriptors initializing...\n"); + kprintf("\e[94mtype descriptors initializing...\n"); __ktDescriptors=Autoarr_create(ktDescriptor, 256, 256); if(__ktDescriptors==NULL) throw(ERR_NULLPTR); } @@ -55,7 +55,7 @@ void ktDescriptors_endInit(){ typeDescriptors=Autoarr_toArray(__ktDescriptors); Autoarr_free(__ktDescriptors,true); if(typeDescriptors==NULL) throw(ERR_NULLPTR); - printf("\e[92minitialized %u type descriptors\n", ktId_last); + kprintf("\e[92minitialized %u type descriptors\n", ktId_last); } void __kt_register(char* name, int16 size, void (*freeMembers)(void*), char* (*toString)(void*, uint32)){ @@ -71,7 +71,7 @@ void __kt_register(char* name, int16 size, void (*freeMembers)(void*), char* (*t ktDescriptor ktDescriptor_get(ktId id){ if(id>ktId_last) { - printf("\ntype id: %u\n",id); + kprintf("\ntype id: %u\n",id); throw("invalid type id"); } return typeDescriptors[id]; diff --git a/src/kprint/README.md b/src/kprint/README.md index 354d3bd..c59a66e 100644 --- a/src/kprint/README.md +++ b/src/kprint/README.md @@ -1,3 +1,6 @@ +# kprintf +It is just my variant of printf. + # kprint I don't really like printf function (and its variants), so i made safer and more convinient replacement. diff --git a/src/kprint/kprint.c b/src/kprint/kprint.c index dcca931..d8d609b 100644 --- a/src/kprint/kprint.c +++ b/src/kprint/kprint.c @@ -1,6 +1,5 @@ #include "../String/StringBuilder.h" #include "kprint.h" -#include "../base/type_system/base_toString.h" ktId __typeFromFormat(kprint_format f){ ktId typeId=kprint_format_ktId(f); @@ -78,7 +77,7 @@ void __kprint(uint8 n, kprint_format* formats, __kprint_value_union* objects){ #define FOREGROUND_YELLOW FOREGROUND_GREEN | FOREGROUND_RED DWORD kprint_fgColor_toWin(kprint_fgColor f){ - //printf("fg: %x\n", f); + //kprintf("fg: %x\n", f); switch(f){ case kprint_fgBlack: return 0; case kprint_fgDarkRed: return FOREGROUND_RED; @@ -101,7 +100,7 @@ DWORD kprint_fgColor_toWin(kprint_fgColor f){ } DWORD kprint_bgColor_toWin(kprint_bgColor f){ - //printf("bg: %x\n", f); + //kprintf("bg: %x\n", f); switch(f){ case kprint_bgBlack: return 0; case kprint_bgDarkRed: return BACKGROUND_RED; @@ -140,13 +139,13 @@ void kprint_setColor(kprint_format f){ uint8 fg=(f&0x0f000000)>>24; if(fg<8) fg+=30; else fg+=90-8; - printf("\e[%um", fg); + kprintf("\e[%um", fg); } if(kprint_format_bgColorChanged(f)){ uint8 bg=(f&0x00f00000)>>20; if(bg<8) bg+=40; else bg+=100-8; - printf("\e[%um", bg); + kprintf("\e[%um", bg); } } #endif @@ -167,59 +166,3 @@ void kprint_setColor(kprint_format f){ StringBuilder_append_char(strb, ' '); StringBuilder_append_char(strb, ']'); } */ - -void kprintf(char* format, ...){ - va_list vl; - va_start(vl, format); - char c; - while((c=*format++)){ - if(c=='%'){ - c=*format++; - switch (c) { - case 'u': - toString_uint(va_arg(vl, uint64),0,0); - break; - case 'i': - toString_int(va_arg(vl, uint64)); - break; - case 'f': - toString_float(va_arg(vl, float64),0,0); - break; - /* case 'l': - c=*format++; - switch (c) { - case 'u': - toString_uint(va_arg(vl, uint64),0,0); - break; - case 'i': - toString_int(va_arg(vl, uint64)); - break; - case 'f': - toString_float(va_arg(vl, float64),0,0); - break; - default: - throw(ERR_FORMAT); - } - break; */ - case 'p': - case 'x': - uint64 px=va_arg(vl, uint64); - toString_hex(&px,sizeof(px),1,0); - break; - - default: - throw(ERR_FORMAT); - } - } else if(c=='\e'){ - IFWIN( - ({ - - }), - putc(c,stdout); - ) - } else { - putc(c,stdout); - } - } - va_end(vl); -} diff --git a/src/kprint/kprint.h b/src/kprint/kprint.h index 13905ad..6e0587a 100644 --- a/src/kprint/kprint.h +++ b/src/kprint/kprint.h @@ -65,9 +65,6 @@ Maybe __kfprint(FILE* fd, uint8 n, kprint_format* formats, __kprint_value_union* void __kprint(uint8 n, kprint_format* formats, __kprint_value_union* objects); #define kprint(ARGS...) __kprint(count_args(ARGS), __kprint_argsToArrs(count_args(ARGS),ARGS, __32zeroes)) -// cross-platform printf analog -void kprintf(char* format, ...); - // can take (bgColor | fgColor) void kprint_setColor(kprint_format f); diff --git a/src/kprint/kprintf.c b/src/kprint/kprintf.c new file mode 100644 index 0000000..b5facc4 --- /dev/null +++ b/src/kprint/kprintf.c @@ -0,0 +1,73 @@ +#include "kprintf.h" +#include "../base/base.h" +#include "../base/type_system/base_toString.h" + +void kprintf(const char* format, ...){ + va_list vl; + va_start(vl, format); + char c; + while((c=*format++)){ + if(c=='%'){ + char* argstr=NULL; + c=*format++; + format_escape_seq: + switch (c) { + case 'u': + argstr=toString_uint(va_arg(vl, uint64),0,0); + break; + case 'i': + argstr=toString_int(va_arg(vl, uint64)); + break; + case 'f': + argstr=toString_float(va_arg(vl, float64),0,0); + break; + case 'l': + c=*format++; + goto format_escape_seq; + // switch (c) { + // case 'u': + // argstr=toString_uint(va_arg(vl, uint64),0,0); + // break; + // case 'i': + // argstr=toString_int(va_arg(vl, uint64)); + // break; + // case 'f': + // argstr=toString_float(va_arg(vl, float64),0,0); + // break; + // default: + // throw(ERR_FORMAT); + // } + // break; + case 'p': + case 'x': + uint64 px=va_arg(vl, uint64); + argstr=toString_hex(&px,sizeof(px),1,0); + break; + case 's': + fputs(va_arg(vl,char*), stdout); + break; + case 'c': + argstr=malloc(2); + argstr[0]=va_arg(vl,char); + argstr[1]=0; + break; + default: + throw(ERR_FORMAT); + } + if(argstr){ + fputs(argstr, stdout); + free(argstr); + } + } else if(c=='\e'){ + IFWIN( + ({ + + }), + putc(c,stdout); + ) + } else { + putc(c,stdout); + } + } + va_end(vl); +} diff --git a/src/kprint/kprintf.h b/src/kprint/kprintf.h new file mode 100644 index 0000000..0ca5a68 --- /dev/null +++ b/src/kprint/kprintf.h @@ -0,0 +1,12 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +// cross-platform kprintf analog +void kprintf(const char* format, ...); + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/tests/main.cpp b/tests/main.cpp index ae1beed..d14e62c 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -11,18 +11,18 @@ void test_all(){ test_dtsod(); test_rng_algorithms(); test_kprint_colors(); - printf("\e[96m--------------------------------------\e[0m\n"); + kprintf("\e[96m--------------------------------------\e[0m\n"); } int main(){ setlocale(LC_ALL, "en-US.Unicode"); ktDescriptors_beginInit(); ktDescriptors_initKerepTypes(); ktDescriptors_endInit(); - printf("\e[97mkerep tests are starting!\n"); + kprintf("\e[97mkerep tests are starting!\n"); // optime("test_all",1,test_all()); test_rng_algorithms(); test_kprint_colors(); ktDescriptors_free(); - printf("\e[0m\n"); + kprintf("\e[0m\n"); return 0; } diff --git a/tests/test_autoarr-vs-vector.cpp b/tests/test_autoarr-vs-vector.cpp index 37aba5e..f349435 100644 --- a/tests/test_autoarr-vs-vector.cpp +++ b/tests/test_autoarr-vs-vector.cpp @@ -4,7 +4,7 @@ int64 _autoarrVsVector(uint16 blockCount, uint16 blockLength){ uint32 count=blockLength*blockCount; - printf("\e[94mblock count: %u block length: %u count: " IFWIN("%llu", "%lu") "\n", blockCount, blockLength, (uint64)count); + 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(); optime("Autoarr_add", 1, ({ @@ -29,7 +29,7 @@ int64 _autoarrVsVector(uint16 blockCount, uint16 blockLength){ } void test_autoarrVsVector(){ - printf("\e[96m-------[test_autoarr_vs_vector]-------\n"); + kprintf("\e[96m-------[test_autoarr_vs_vector]-------\n"); _autoarrVsVector(4, 16); _autoarrVsVector(16, 64); _autoarrVsVector(32, 32); diff --git a/tests/test_autoarr.c b/tests/test_autoarr.c index 34dc121..2b3d24d 100644 --- a/tests/test_autoarr.c +++ b/tests/test_autoarr.c @@ -2,7 +2,7 @@ #include "../src/Autoarr/Autoarr.h" static void printautoarr(Autoarr(uint16)* ar){ - printf("\e[94mAutoarr(uint16): " + kprintf("\e[94mAutoarr(uint16): " IFWIN("%llu", "%lu") "\n max_blocks_count: %u\n" " blocks_count: %u\n" @@ -29,25 +29,25 @@ static void resetar(Autoarr(uint16)* ar){ } static void printallval(Autoarr(uint16)* ar){ - printf("\e[90m"); + kprintf("\e[90m"); for (uint16 i=0;i=100?0:(i>=10?1:2)); char* str1=char_multiply(' ',lgs[i]>=100?0:(lgs[i]>=10?1:2)); char* str2=char_multiply('#',lgs[i]/100); - printf("\e[94m length: \e[96m%u %s \e[94mfrequency: \e[96m%u %s \e[90m%s\n",i,str0,lgs[i],str1,str2); + kprintf("\e[94m length: \e[96m%u %s \e[94mfrequency: \e[96m%u %s \e[90m%s\n",i,str0,lgs[i],str1,str2); free(str0); free(str1); free(str2); @@ -63,15 +63,15 @@ Unitype gett(Hashtable* ht){ void test_hashtable(){ optime("test_hashtable",1,({ - printf("\e[96m-----------[test_hashtable]-----------\n"); + kprintf("\e[96m-----------[test_hashtable]-----------\n"); Hashtable* ht=Hashtable_create(); - printf("\e[92mhashtable created\n"); + kprintf("\e[92mhashtable created\n"); print_hashtable(ht); optime("fill",1,fill(ht)); optime("get",1,gett(ht)); printrowgraph(ht); print_hashtable(ht); Hashtable_free(ht); - printf("\e[92mhashtable freed\n"); + kprintf("\e[92mhashtable freed\n"); })); } diff --git a/tests/test_kprint_colors.c b/tests/test_kprint_colors.c index 3cdb860..ac1564c 100644 --- a/tests/test_kprint_colors.c +++ b/tests/test_kprint_colors.c @@ -6,11 +6,11 @@ #define testColor(COLOR) \ kprint_setColor(kprint_bgBlack | kprint_fg##COLOR);\ - printf(#COLOR " ");\ + kprintf(#COLOR " ");\ kprint_setColor(kprint_bg##COLOR | kprint_fgGray);\ - printf(#COLOR);\ + kprintf(#COLOR);\ kprint_setColor(kprint_bgBlack | kprint_fgBlack);\ - printf("\n"); + kprintf("\n"); void test_kprint_colors(){ /* IFWIN( @@ -18,15 +18,15 @@ void test_kprint_colors(){ HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); for(uint8 col=0; col<255; col++){ SetConsoleTextAttribute(hConsole, col); - printf("%u ",col); + kprintf("%u ",col); } }), ({ for(uint8 col=0; col<255; col++) - printf("\e[%um%u ", col, col); + kprintf("\e[%um%u ", col, col); }) ); - printf("\n"); */ + kprintf("\n"); */ testColor(Black); testColor(DarkRed); diff --git a/tests/test_marshalling.c b/tests/test_marshalling.c index 1614c6c..a3646e6 100644 --- a/tests/test_marshalling.c +++ b/tests/test_marshalling.c @@ -10,5 +10,5 @@ EXPORT void CALL test_marshalling(char* text, KVPair** kptr){ } EXPORT void CALL pinvoke_print(char* msg) { - printf("printed from unmanaged code: %s\n", msg); + kprintf("printed from unmanaged code: %s\n", msg); } diff --git a/tests/test_rng_algorithms.c b/tests/test_rng_algorithms.c index dfc0a6a..1af5ef5 100644 --- a/tests/test_rng_algorithms.c +++ b/tests/test_rng_algorithms.c @@ -3,25 +3,25 @@ #define test_alg(ALG, VALUE_SIZE, EXPECTED_FROM_ZERO){\ - printf("\e[94mrng algorithm: \e[96m" #ALG "\n");\ + kprintf("\e[94mrng algorithm: \e[96m" #ALG "\n");\ void* s= ALG##_init(0);\ uint##VALUE_SIZE r=ALG##_next(s);\ - printf("\e[97m next from zero seed:");\ + kprintf("\e[97m next from zero seed:");\ if(r!=EXPECTED_FROM_ZERO){\ - printf("\e[91m " IFWIN("%llu\n","%lu\n"), (uint64)r);\ + kprintf("\e[91m " IFWIN("%llu\n","%lu\n"), (uint64)r);\ throw(ERR_UNEXPECTEDVAL);\ }\ - printf("\e[92m " IFWIN("%llu\n","%lu\n"), (uint64)r);\ + kprintf("\e[92m " IFWIN("%llu\n","%lu\n"), (uint64)r);\ ALG##_free(s);\ s= ALG##_initFromTime();\ r=ALG##_next(s);\ ALG##_free(s);\ - printf("\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"), (uint64)r);\ } void test_rng_algorithms(){ optime("test_rng_algorithms",1,({ - printf("\e[96m--------[test_rng_algorithms]---------\n"); + kprintf("\e[96m--------[test_rng_algorithms]---------\n"); // for ALG32 // xoroshiro64 test_alg(xoroshiro64star, 32, 932574677ULL) diff --git a/tests/test_safethrow.c b/tests/test_safethrow.c index f37812d..14e4e1e 100644 --- a/tests/test_safethrow.c +++ b/tests/test_safethrow.c @@ -14,10 +14,10 @@ Maybe throw_errcode(){ } Maybe test_maybe(){ - printf("\e[94mdont_throw returns \e[92m"); + kprintf("\e[94mdont_throw returns \e[92m"); tryLast(dont_throw(),rez0) printMaybe(rez0); - printf("\n"); + kprintf("\n"); try(throw_error(),rez1,;) printMaybe(rez1); throw("test_maybe failed"); @@ -29,13 +29,13 @@ Maybe b(){ try(c(),_,;) return MaybeNull; } Maybe a(){ try(b(),_,;) return MaybeNull; } void test_safethrow(){ - printf("\e[96m-----------[test_safethrow]-----------\n"); + kprintf("\e[96m-----------[test_safethrow]-----------\n"); optime("test_safethrow", 1, ({ Maybe e=test_maybe(); - printf("\e[94mthrow_error:\n\e[92m"); + kprintf("\e[94mthrow_error:\n\e[92m"); printMaybe(e); Maybe_free(e); - printf("\e[94mthrow_errcode:\n\e[92m"); + kprintf("\e[94mthrow_errcode:\n\e[92m"); e=a(); printMaybe(e); Maybe_free(e); diff --git a/tests/test_searchtree.c b/tests/test_searchtree.c index 109c116..c7312e8 100644 --- a/tests/test_searchtree.c +++ b/tests/test_searchtree.c @@ -2,21 +2,21 @@ #include "../src/SearchTree/SearchTree.h" void printstnode(STNode* node){ - printf("\e[94mSTNode: " + kprintf("\e[94mSTNode: " IFWIN("%llu", "%lu") "\n address: %p\n value: ",sizeof(STNode),node); printuni(node->value); - printf("\n"); + kprintf("\n"); // prints pointers to all existing branches - /* printf(" branches: %p\n", node->branches); + /* kprintf(" branches: %p\n", node->branches); if(node->branches) for(uint8 i=0;i<8;i++){ - printf(" \e[90m[%u]=%p\n",i,node->branches[i]); + kprintf(" \e[90m[%u]=%p\n",i,node->branches[i]); if(node->branches[i]) for (uint8 ii = 0; ii < 8; ii++){ - printf(" \e[90m[%u]=%p\n",ii,node->branches[i][ii]); + kprintf(" \e[90m[%u]=%p\n",ii,node->branches[i][ii]); if(node->branches[i][ii]) for (uint8 iii = 0; iii < 4; iii++) - printf(" \e[90m[%u]=%p\n",iii,node->branches[i][ii][iii]); + kprintf(" \e[90m[%u]=%p\n",iii,node->branches[i][ii][iii]); } } */ @@ -24,51 +24,51 @@ void printstnode(STNode* node){ void test_searchtree(){ optime("test_searchtree",1,({ - printf("\e[96m-----------[test_searchtree]----------\n"); + kprintf("\e[96m-----------[test_searchtree]----------\n"); STNode* node=STNode_create(); - printf("\e[92mnode created\n"); - printf("push:\e[94m\n "); + kprintf("\e[92mnode created\n"); + kprintf("push:\e[94m\n "); Unitype u=UniInt64(-3); printuni(u); ST_push(node,"type", u); - printf(" -> type\n "); + kprintf(" -> type\n "); u=UniInt64(25); printuni(u); ST_push(node,"time", u); - printf(" -> time\n "); + kprintf(" -> time\n "); u=UniFloat64(-542.00600); printuni(u); ST_push(node,"author_id", u); - printf(" -> author_id\n "); + kprintf(" -> author_id\n "); u=UniInt64(-31255); printuni(u); ST_push(node,"channel_id", u); - printf(" -> channel_id\n "); + kprintf(" -> channel_id\n "); u=UniHeap(ktId_CharPtr, cptr_copy("32.2004")); printuni(u); ST_push(node,"message_id", u); - printf(" -> message_id\n "); + kprintf(" -> message_id\n "); u=UniStack(ktId_CharPtr,"some text UwU"); printuni(u); ST_push(node,"text", u); - printf(" -> text\n"); - printf("\e[92mpull:\e[94m"); - printf("\n type -> "); + kprintf(" -> text\n"); + kprintf("\e[92mpull:\e[94m"); + kprintf("\n type -> "); printuni(ST_pull(node,"type")); - printf("\n time -> "); + kprintf("\n time -> "); printuni(ST_pull(node,"time")); - printf("\n author_id -> "); + kprintf("\n author_id -> "); printuni(ST_pull(node,"author_id")); - printf("\n channel_id -> "); + kprintf("\n channel_id -> "); printuni(ST_pull(node,"channel_id")); - printf("\n message_id -> "); + kprintf("\n message_id -> "); printuni(ST_pull(node,"message_id")); - printf("\n text -> "); + kprintf("\n text -> "); printuni(ST_pull(node,"text")); - printf("\n"); - printf("\e[92mfirst node: "); + kprintf("\n"); + kprintf("\e[92mfirst node: "); printstnode(node); STNode_free(node); - printf("\e[92mnode deleted\n"); + kprintf("\e[92mnode deleted\n"); })); } diff --git a/tests/test_string.c b/tests/test_string.c index 9a6aac2..ff861f9 100644 --- a/tests/test_string.c +++ b/tests/test_string.c @@ -3,12 +3,12 @@ void test_string(){ optime(__func__,1,({ - printf("\e[96m-------------[test_string]------------\n"); + kprintf("\e[96m-------------[test_string]------------\n"); char c[]="0123456789abcdef"; string s={.ptr=c, .length=cptr_length(c)}; if(s.length!=sizeof(c)-1) throw("string created with incorrect length"); char* p=string_extract(s); - printf("\e[94mstring_extract() -> \e[92m\"%s\"\n",p); + kprintf("\e[94mstring_extract() -> \e[92m\"%s\"\n",p); free(p); })); } \ No newline at end of file diff --git a/tests/tests.h b/tests/tests.h index db46e93..8941c61 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -17,7 +17,7 @@ void test_kprint_colors(); void test_autoarrVsVector(); void test_rng_algorithms(); -#define PRINT_SIZEOF(T) printf("\e[94m" #T " size: \e[96m" IFWIN("%llu", "%lu") "\n", sizeof(T)) +#define PRINT_SIZEOF(T) kprintf("\e[94m" #T " size: \e[96m" IFWIN("%llu", "%lu") "\n", sizeof(T)) #if __cplusplus } From 121f492b8463b600ed625035245415d9d3ab06b5 Mon Sep 17 00:00:00 2001 From: timerix Date: Mon, 24 Oct 2022 19:13:04 +0600 Subject: [PATCH 51/59] kprintf color parse --- src/kprint/kprint.c | 4 +-- src/kprint/kprintf.c | 70 ++++++++++++++++++++++++++++++++++++++++++-- tests/main.cpp | 4 +-- 3 files changed, 70 insertions(+), 8 deletions(-) diff --git a/src/kprint/kprint.c b/src/kprint/kprint.c index d8d609b..907e208 100644 --- a/src/kprint/kprint.c +++ b/src/kprint/kprint.c @@ -139,13 +139,13 @@ void kprint_setColor(kprint_format f){ uint8 fg=(f&0x0f000000)>>24; if(fg<8) fg+=30; else fg+=90-8; - kprintf("\e[%um", fg); + printf("\e[%um", fg); } if(kprint_format_bgColorChanged(f)){ uint8 bg=(f&0x00f00000)>>20; if(bg<8) bg+=40; else bg+=100-8; - kprintf("\e[%um", bg); + printf("\e[%um", bg); } } #endif diff --git a/src/kprint/kprintf.c b/src/kprint/kprintf.c index b5facc4..2208b3d 100644 --- a/src/kprint/kprintf.c +++ b/src/kprint/kprintf.c @@ -2,11 +2,56 @@ #include "../base/base.h" #include "../base/type_system/base_toString.h" +#if defined(_WIN64) || defined(_WIN32) +#include + +DWORD unixColorToWin(uint8 c){ + switch(c){ + //foreground + case 30: return 0; + case 31: return FOREGROUND_RED; + case 32: return FOREGROUND_GREEN; + case 33: return FOREGROUND_GREEN | FOREGROUND_RED; + case 34: return FOREGROUND_BLUE; + case 35: return FOREGROUND_RED | FOREGROUND_BLUE; + case 36: return FOREGROUND_BLUE | FOREGROUND_GREEN; + case 37: return FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED; + case 90: return FOREGROUND_INTENSITY; + case 91: return FOREGROUND_RED | FOREGROUND_INTENSITY; + case 92: return FOREGROUND_GREEN | FOREGROUND_INTENSITY; + case 93: return FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY; + case 94: return FOREGROUND_BLUE | FOREGROUND_INTENSITY; + case 95: return FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY; + case 96: return FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY; + case 97: return FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY; + //background + case 40: return 0; + case 41: return BACKGROUND_RED; + case 42: return BACKGROUND_GREEN; + case 43: return BACKGROUND_GREEN | BACKGROUND_RED; + case 44: return BACKGROUND_BLUE; + case 45: return BACKGROUND_RED | BACKGROUND_BLUE; + case 46: return BACKGROUND_BLUE | BACKGROUND_GREEN; + case 47: return BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED; + case 100: return BACKGROUND_INTENSITY; + case 101: return BACKGROUND_RED | BACKGROUND_INTENSITY; + case 102: return BACKGROUND_GREEN | BACKGROUND_INTENSITY; + case 103: return BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY; + case 104: return BACKGROUND_BLUE | BACKGROUND_INTENSITY; + case 105: return BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY; + case 106: return BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY; + case 107: return BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY; + default: return 0; + } +} +#endif + void kprintf(const char* format, ...){ va_list vl; va_start(vl, format); char c; while((c=*format++)){ + iteration_start: if(c=='%'){ char* argstr=NULL; c=*format++; @@ -48,7 +93,7 @@ void kprintf(const char* format, ...){ break; case 'c': argstr=malloc(2); - argstr[0]=va_arg(vl,char); + argstr[0]=(char)va_arg(vl,int); argstr[1]=0; break; default: @@ -61,10 +106,29 @@ void kprintf(const char* format, ...){ } else if(c=='\e'){ IFWIN( ({ - + if((c=*format++)=='['){ + uint16 colorUnix=0; + for(int8 i=0; i<6 && c!=0; i++){ + c=*format++; + switch (c){ + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + colorUnix=colorUnix*10+c-'0'; + break; + case 'm': + DWORD colorWin=unixColorToWin(colorUnix); + HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); + SetConsoleTextAttribute(hConsole, colorWin); + c=*format++; + goto iteration_start; + default: + goto iteration_start; + } + } + } }), putc(c,stdout); - ) + ); } else { putc(c,stdout); } diff --git a/tests/main.cpp b/tests/main.cpp index d14e62c..e36df44 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -19,9 +19,7 @@ int main(){ ktDescriptors_initKerepTypes(); ktDescriptors_endInit(); kprintf("\e[97mkerep tests are starting!\n"); - // optime("test_all",1,test_all()); - test_rng_algorithms(); - test_kprint_colors(); + optime("test_all",1,test_all()); ktDescriptors_free(); kprintf("\e[0m\n"); return 0; From 88815452bcdf7078a09d599bec97f00839d2c388 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 28 Oct 2022 00:39:47 +0600 Subject: [PATCH 52/59] fixed kprintf --- src/kprint/kprintf.c | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/src/kprint/kprintf.c b/src/kprint/kprintf.c index 2208b3d..e7b184b 100644 --- a/src/kprint/kprintf.c +++ b/src/kprint/kprintf.c @@ -5,7 +5,7 @@ #if defined(_WIN64) || defined(_WIN32) #include -DWORD unixColorToWin(uint8 c){ +WORD unixColorToWin(uint8 c){ switch(c){ //foreground case 30: return 0; @@ -49,12 +49,11 @@ DWORD unixColorToWin(uint8 c){ void kprintf(const char* format, ...){ va_list vl; va_start(vl, format); - char c; - while((c=*format++)){ - iteration_start: + uint32 i=0; + for(char c=format[i++]; c!=0; c=format[i++]){ if(c=='%'){ char* argstr=NULL; - c=*format++; + c=format[i++]; format_escape_seq: switch (c) { case 'u': @@ -67,8 +66,9 @@ void kprintf(const char* format, ...){ argstr=toString_float(va_arg(vl, float64),0,0); break; case 'l': - c=*format++; - goto format_escape_seq; + if((c=format[i++])) + goto format_escape_seq; + break; // switch (c) { // case 'u': // argstr=toString_uint(va_arg(vl, uint64),0,0); @@ -89,7 +89,11 @@ void kprintf(const char* format, ...){ argstr=toString_hex(&px,sizeof(px),1,0); break; case 's': - fputs(va_arg(vl,char*), stdout); + char* cptr=va_arg(vl,char*); + if(!cptr) + cptr=""; + if(*cptr) + fputs(cptr, stdout); break; case 'c': argstr=malloc(2); @@ -106,23 +110,22 @@ void kprintf(const char* format, ...){ } else if(c=='\e'){ IFWIN( ({ - if((c=*format++)=='['){ - uint16 colorUnix=0; - for(int8 i=0; i<6 && c!=0; i++){ - c=*format++; + if((c=format[i++])=='['){ + uint8 colorUnix=0; + for(int8 n=0; n<6 && c!=0; n++){ + c=format[i++]; switch (c){ case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': colorUnix=colorUnix*10+c-'0'; break; case 'm': - DWORD colorWin=unixColorToWin(colorUnix); + WORD colorWin=unixColorToWin(colorUnix); HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); SetConsoleTextAttribute(hConsole, colorWin); - c=*format++; - goto iteration_start; + goto end_iteration; default: - goto iteration_start; + goto end_iteration; } } } @@ -132,6 +135,9 @@ void kprintf(const char* format, ...){ } else { putc(c,stdout); } + #if defined(_WIN64) || defined(_WIN32) + end_iteration: + #endif } va_end(vl); } From 1632c78300a9de049dbcba7dd20c0b042fd35491 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 28 Oct 2022 02:09:10 +0600 Subject: [PATCH 53/59] some fixes --- src/base/type_system/base_toString.c | 11 +++++++---- src/kprint/kprintf.c | 6 +++++- tests/main.cpp | 6 ++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/src/base/type_system/base_toString.c b/src/base/type_system/base_toString.c index 178edb0..02be90f 100644 --- a/src/base/type_system/base_toString.c +++ b/src/base/type_system/base_toString.c @@ -85,7 +85,7 @@ char* toString_bin(void* _bytes, uint32 size, bool withPrefix){ str[cn++]='b'; } for(uint32 bn=0; bn>i); } @@ -100,8 +100,11 @@ char _4bitsHex(uint8 u, bool uppercase){ return '0'+u; case 0xA: case 0xB: case 0xC: case 0xD: case 0xE: case 0xF: - return (uppercase ? 'A'-10 : 'a'-10) + u; - default: return 219; + return (uppercase ? 'A' : 'a') + u -10; + default: + dbg(u); + throw("incorrect number"); + return 219; } } @@ -114,7 +117,7 @@ char* toString_hex(void* _bytes, uint32 size, bool withPrefix, bool uppercase){ str[cn++]='x'; } for(uint32 bn=0; bn',stdout); throw(ERR_FORMAT); } if(argstr){ diff --git a/tests/main.cpp b/tests/main.cpp index e36df44..6338d3b 100644 --- a/tests/main.cpp +++ b/tests/main.cpp @@ -14,13 +14,15 @@ void test_all(){ kprintf("\e[96m--------------------------------------\e[0m\n"); } int main(){ - setlocale(LC_ALL, "en-US.Unicode"); + if(!setlocale(LC_ALL, "C.UTF8")) + kprintf("\e[93msetlocale failed\n"); ktDescriptors_beginInit(); ktDescriptors_initKerepTypes(); ktDescriptors_endInit(); kprintf("\e[97mkerep tests are starting!\n"); - optime("test_all",1,test_all()); + //optime("test_all",1,test_all()); ktDescriptors_free(); + kprintf("ъъъъ"); kprintf("\e[0m\n"); return 0; } From a3dac3e756404dd5d489cbd78c3af728ff46d112 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 28 Oct 2022 11:02:29 +0600 Subject: [PATCH 54/59] fixed memory leak in cptr_concat --- src/base/cptr.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/base/cptr.c b/src/base/cptr.c index 53872cd..0086edd 100644 --- a/src/base/cptr.c +++ b/src/base/cptr.c @@ -56,8 +56,8 @@ void memcopy(void* from, void* to, uint32 size){ } char* __cptr_concat(uint16 n, ...){ - char** strs=(char**)malloc(n*8); - uint32* lengths=malloc(n*4); + char** strs=(char**)malloc(n*sizeof(char*)); + uint32* lengths=malloc(n*sizeof(uint32)); uint32 totalLength=0; // reading args from va_list @@ -83,5 +83,7 @@ char* __cptr_concat(uint16 n, ...){ totality+=lengths[k]; } + free(strs); + free(lengths); return output; } From f8a610ace078866d8b6e783c3c21d0865ff5e4d1 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 28 Oct 2022 11:56:59 +0600 Subject: [PATCH 55/59] Autoarr readme --- src/Autoarr/README.md | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 src/Autoarr/README.md diff --git a/src/Autoarr/README.md b/src/Autoarr/README.md new file mode 100644 index 0000000..f72df35 --- /dev/null +++ b/src/Autoarr/README.md @@ -0,0 +1,9 @@ +# Autoarr struct +`Autoarr` means Automatically resizing array. It is my implementation of dyynamic array. If you want to use `Autoarr` of some type, it should be declared in header file by macro `Autoarr_declare` and defined in source file by `Autoarr_define`. + +Examples: +[Hashtable.h](src/Hashtable/Hashtable.h) +[Hashtable.c](src/Hashtable/Hashtable.c) + +### Autoarr_*_exported +Contains definitions for functions, exported for using in C# kerep wrapper. From e2ecfc3d27a03706b98a94a6004c0d7d1af92184 Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 28 Oct 2022 11:57:18 +0600 Subject: [PATCH 56/59] Array struct --- src/Array/Array.h | 40 +++++++++++++++++++++++++++++++++++++ src/Array/Array_declare.h | 40 +++++++++++++++++++++++++++++++++++++ src/Array/README.md | 2 ++ src/base/type_system/init.c | 29 +++++++++++++++++++++++++++ 4 files changed, 111 insertions(+) create mode 100644 src/Array/Array.h create mode 100644 src/Array/Array_declare.h create mode 100644 src/Array/README.md diff --git a/src/Array/Array.h b/src/Array/Array.h new file mode 100644 index 0000000..ac9dabc --- /dev/null +++ b/src/Array/Array.h @@ -0,0 +1,40 @@ +#include "Array_declare.h" + +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) + +ktId_declare(ArrayChar); +ktId_declare(ArrayBool); +ktId_declare(ArrayFloat32); +ktId_declare(ArrayFloat64); +ktId_declare(ArrayInt8); +ktId_declare(ArrayUInt8); +ktId_declare(ArrayInt16); +ktId_declare(ArrayUInt16); +ktId_declare(ArrayInt32); +ktId_declare(ArrayUInt32); +ktId_declare(ArrayInt64); +ktId_declare(ArrayUInt64); + +ktId_declare(ArrayCharPtr); +ktId_declare(ArrayBoolPtr); +ktId_declare(ArrayFloat32Ptr); +ktId_declare(ArrayFloat64Ptr); +ktId_declare(ArrayInt8Ptr); +ktId_declare(ArrayUInt8Ptr); +ktId_declare(ArrayInt16Ptr); +ktId_declare(ArrayUInt16Ptr); +ktId_declare(ArrayInt32Ptr); +ktId_declare(ArrayUInt32Ptr); +ktId_declare(ArrayInt64Ptr); +ktId_declare(ArrayUInt64Ptr); diff --git a/src/Array/Array_declare.h b/src/Array/Array_declare.h new file mode 100644 index 0000000..4f50f53 --- /dev/null +++ b/src/Array/Array_declare.h @@ -0,0 +1,40 @@ +#pragma once + +#if __cplusplus +extern "C" { +#endif + +#include "../base/base.h" + +#define Array_declare(type)\ +typedef struct Array_##type{\ + type* values;\ + uint32 length;\ + bool allocatedOnHeap;\ +} Array_##type;\ +\ +\ +static inline Array_##type Array_##type##_allocValues(uint32 length){\ + return (Array_##type) {\ + .values=malloc(sizeof(type)*length),\ + .length=length,\ + .allocatedOnHeap=true\ + };\ +}\ +\ +static inline Array_##type Array_##type##_fromBuffer(buffer, bufferLength, allocatedOnHeap){\ + return (Array_##type) {\ + .values=buffer,\ + .length=bufferLength,\ + .allocatedOnHeap=allocatedOnHeap\ + };\ +}\ +\ +static inline void Array_##type##_freeValues(Array_##type* array){\ + if(array->allocatedOnHeap)\ + free(array->values);\ +} + +#if __cplusplus +} +#endif \ No newline at end of file diff --git a/src/Array/README.md b/src/Array/README.md new file mode 100644 index 0000000..931ff09 --- /dev/null +++ b/src/Array/README.md @@ -0,0 +1,2 @@ +# Array struct +This struct stores array pointer and length. If you want to use `Array` of some type, it should be declared in header file by macro `Array_declare`. It uses `static inline` functions, so all definitions will be generated in header. diff --git a/src/base/type_system/init.c b/src/base/type_system/init.c index 91f592e..729da9f 100644 --- a/src/base/type_system/init.c +++ b/src/base/type_system/init.c @@ -1,4 +1,5 @@ #include "../base.h" +#include "../../Array/Array.h" #include "../../Autoarr/Autoarr.h" #include "../../SearchTree/SearchTree.h" #include "../../Hashtable/Hashtable.h" @@ -39,6 +40,34 @@ void ktDescriptors_initKerepTypes(){ kt_register(ktDescriptor, ktId_ktDescriptor, NULL, NULL); kt_register(ktDescriptor*, ktId_ktDescriptorPtr, NULL, NULL); + + // base type arrays + kt_register(Array_char, ktId_ArrayChar, Array_char_freeValues, NULL); + kt_register(Array_bool, ktId_ArrayBool, Array_bool_freeValues, NULL); + kt_register(Array_float32, ktId_ArrayFloat32, Array_float32_freeValues, NULL); + kt_register(Array_float64, ktId_ArrayFloat64, Array_float64_freeValues, NULL); + kt_register(Array_int8, ktId_ArrayInt8, Array_int8_freeValues, NULL); + kt_register(Array_uint8, ktId_ArrayUInt8, Array_uint8_freeValues, NULL); + kt_register(Array_int16, ktId_ArrayInt16, Array_int16_freeValues, NULL); + kt_register(Array_uint16, ktId_ArrayUInt16, Array_uint16_freeValues, NULL); + kt_register(Array_int32, ktId_ArrayInt32, Array_int32_freeValues, NULL); + kt_register(Array_uint32, ktId_ArrayUInt32, Array_uint32_freeValues, NULL); + kt_register(Array_int64, ktId_ArrayInt64, Array_int64_freeValues, NULL); + kt_register(Array_uint64, ktId_ArrayUInt64, Array_uint64_freeValues, NULL); + // base type array pointers + kt_register(Array_char*, ktId_ArrayCharPtr, Array_char_freeValues, NULL); + kt_register(Array_bool*, ktId_ArrayBoolPtr, Array_bool_freeValues, NULL); + kt_register(Array_float32*, ktId_ArrayFloat32Ptr, Array_float32_freeValues, NULL); + kt_register(Array_float64*, ktId_ArrayFloat64Ptr, Array_float64_freeValues, NULL); + kt_register(Array_int8*, ktId_ArrayInt8Ptr, Array_int8_freeValues, NULL); + kt_register(Array_uint8*, ktId_ArrayUInt8Ptr, Array_uint8_freeValues, NULL); + kt_register(Array_int16*, ktId_ArrayInt16Ptr, Array_int16_freeValues, NULL); + kt_register(Array_uint16*, ktId_ArrayUInt16Ptr, Array_uint16_freeValues, NULL); + kt_register(Array_int32*, ktId_ArrayInt32Ptr, Array_int32_freeValues, NULL); + kt_register(Array_uint32*, ktId_ArrayUInt32Ptr, Array_uint32_freeValues, NULL); + kt_register(Array_int64*, ktId_ArrayInt64Ptr, Array_int64_freeValues, NULL); + kt_register(Array_uint64*, ktId_ArrayUInt64Ptr, Array_uint64_freeValues, NULL); + // base type autoarrs kt_register(Autoarr_char, ktId_AutoarrChar, ____Autoarr_free_char, NULL); kt_register(Autoarr_bool, ktId_AutoarrBool, ____Autoarr_free_bool, NULL); From 5e35cb6721954197beb8ffc6bdc0fceb05463e1e Mon Sep 17 00:00:00 2001 From: timerix Date: Fri, 28 Oct 2022 11:57:43 +0600 Subject: [PATCH 57/59] path_concat --- src/Filesystem/filesystem.c | 38 +++++++++++++++++++++++++++++++++++++ src/Filesystem/filesystem.h | 22 +++++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/Filesystem/filesystem.c create mode 100644 src/Filesystem/filesystem.h diff --git a/src/Filesystem/filesystem.c b/src/Filesystem/filesystem.c new file mode 100644 index 0000000..fe535da --- /dev/null +++ b/src/Filesystem/filesystem.c @@ -0,0 +1,38 @@ +#include "filesystem.h" + +char* __path_concat(uint16 n, char* prev_part, ...){ + char** parts=(char**)malloc(n*sizeof(char*)); + uint32* lengths=malloc(n*sizeof(uint32)); + uint32 totalLength=0; + + // reading args from va_list + va_list vl; + va_start(vl, prev_part); + for(uint16 i=0; i Date: Fri, 28 Oct 2022 12:09:24 +0600 Subject: [PATCH 58/59] fixed Array functions --- src/Array/Array_declare.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Array/Array_declare.h b/src/Array/Array_declare.h index 4f50f53..6d12ead 100644 --- a/src/Array/Array_declare.h +++ b/src/Array/Array_declare.h @@ -16,13 +16,13 @@ typedef struct Array_##type{\ \ static inline Array_##type Array_##type##_allocValues(uint32 length){\ return (Array_##type) {\ - .values=malloc(sizeof(type)*length),\ + .values=(type##*)malloc(sizeof(type)*length),\ .length=length,\ .allocatedOnHeap=true\ };\ }\ \ -static inline Array_##type Array_##type##_fromBuffer(buffer, bufferLength, allocatedOnHeap){\ +static inline Array_##type Array_##type##_fromBuffer(type##* buffer, uint32 bufferLength, bool allocatedOnHeap){\ return (Array_##type) {\ .values=buffer,\ .length=bufferLength,\ From b327f037e7f4e0a1f5a963a1583f3107f5650fd5 Mon Sep 17 00:00:00 2001 From: timerix Date: Sun, 30 Oct 2022 18:51:49 +0600 Subject: [PATCH 59/59] fixed one typo --- src/kprint/kprint.c | 8 ++++---- src/kprint/kprint_colors.h | 8 ++++---- src/kprint/kprint_format.md | 8 ++++---- tests/test_kprint_colors.c | 4 ++-- 4 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/kprint/kprint.c b/src/kprint/kprint.c index 907e208..97186fb 100644 --- a/src/kprint/kprint.c +++ b/src/kprint/kprint.c @@ -84,7 +84,7 @@ DWORD kprint_fgColor_toWin(kprint_fgColor f){ case kprint_fgDarkGreen: return FOREGROUND_GREEN; case kprint_fgDarkYellow: return FOREGROUND_GREEN | FOREGROUND_RED; case kprint_fgDarkBlue: return FOREGROUND_BLUE; - case kprint_fgDarkMagneta: return FOREGROUND_RED | FOREGROUND_BLUE; + case kprint_fgDarkMagenta: return FOREGROUND_RED | FOREGROUND_BLUE; case kprint_fgDarkCyan: return FOREGROUND_BLUE | FOREGROUND_GREEN; case kprint_fgGray: return FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_RED; case kprint_fgDarkGray: return FOREGROUND_INTENSITY; @@ -92,7 +92,7 @@ DWORD kprint_fgColor_toWin(kprint_fgColor f){ case kprint_fgGreen: return FOREGROUND_GREEN | FOREGROUND_INTENSITY; case kprint_fgYellow: return FOREGROUND_GREEN | FOREGROUND_RED | FOREGROUND_INTENSITY; case kprint_fgBlue: return FOREGROUND_BLUE | FOREGROUND_INTENSITY; - case kprint_fgMagneta: return FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY; + case kprint_fgMagenta: return FOREGROUND_BLUE | FOREGROUND_RED | FOREGROUND_INTENSITY; case kprint_fgCyan: return FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY; case kprint_fgWhite: return FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY; default: throw(ERR_FORMAT); @@ -107,7 +107,7 @@ DWORD kprint_bgColor_toWin(kprint_bgColor f){ case kprint_bgDarkGreen: return BACKGROUND_GREEN; case kprint_bgDarkYellow: return BACKGROUND_GREEN | BACKGROUND_RED; case kprint_bgDarkBlue: return BACKGROUND_BLUE; - case kprint_bgDarkMagneta: return BACKGROUND_RED | BACKGROUND_BLUE; + case kprint_bgDarkMagenta: return BACKGROUND_RED | BACKGROUND_BLUE; case kprint_bgDarkCyan: return BACKGROUND_BLUE | BACKGROUND_GREEN; case kprint_bgGray: return BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_RED; case kprint_bgDarkGray: return BACKGROUND_INTENSITY; @@ -115,7 +115,7 @@ DWORD kprint_bgColor_toWin(kprint_bgColor f){ case kprint_bgGreen: return BACKGROUND_GREEN | BACKGROUND_INTENSITY; case kprint_bgYellow: return BACKGROUND_GREEN | BACKGROUND_RED | BACKGROUND_INTENSITY; case kprint_bgBlue: return BACKGROUND_BLUE | BACKGROUND_INTENSITY; - case kprint_bgMagneta: return BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY; + case kprint_bgMagenta: return BACKGROUND_BLUE | BACKGROUND_RED | BACKGROUND_INTENSITY; case kprint_bgCyan: return BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY; case kprint_bgWhite: return BACKGROUND_RED | BACKGROUND_BLUE | BACKGROUND_GREEN | BACKGROUND_INTENSITY; default: throw(ERR_FORMAT); diff --git a/src/kprint/kprint_colors.h b/src/kprint/kprint_colors.h index f0ce2eb..028ec91 100644 --- a/src/kprint/kprint_colors.h +++ b/src/kprint/kprint_colors.h @@ -15,7 +15,7 @@ typedef enum kprint_fgColor{ kprint_fgDarkGreen = 0x82000000, kprint_fgDarkYellow = 0x83000000, kprint_fgDarkBlue = 0x84000000, - kprint_fgDarkMagneta= 0x85000000, + kprint_fgDarkMagenta= 0x85000000, kprint_fgDarkCyan = 0x86000000, kprint_fgGray = 0x87000000, kprint_fgDarkGray = 0x88000000, @@ -23,7 +23,7 @@ typedef enum kprint_fgColor{ kprint_fgGreen = 0x8a000000, kprint_fgYellow = 0x8b000000, kprint_fgBlue = 0x8c000000, - kprint_fgMagneta = 0x8d000000, + kprint_fgMagenta = 0x8d000000, kprint_fgCyan = 0x8e000000, kprint_fgWhite = 0x8f000000 } kprint_fgColor; @@ -37,7 +37,7 @@ typedef enum kprint_bgColor{ kprint_bgDarkGreen = 0x40200000, kprint_bgDarkYellow = 0x40300000, kprint_bgDarkBlue = 0x40400000, - kprint_bgDarkMagneta= 0x40500000, + kprint_bgDarkMagenta= 0x40500000, kprint_bgDarkCyan = 0x40600000, kprint_bgGray = 0x40700000, kprint_bgDarkGray = 0x40800000, @@ -45,7 +45,7 @@ typedef enum kprint_bgColor{ kprint_bgGreen = 0x40a00000, kprint_bgYellow = 0x40b00000, kprint_bgBlue = 0x40c00000, - kprint_bgMagneta = 0x40d00000, + kprint_bgMagenta = 0x40d00000, kprint_bgCyan = 0x40e00000, kprint_bgWhite = 0x40f00000 } kprint_bgColor; diff --git a/src/kprint/kprint_format.md b/src/kprint/kprint_format.md index fcd4a95..d47cfd1 100644 --- a/src/kprint/kprint_format.md +++ b/src/kprint/kprint_format.md @@ -19,7 +19,7 @@ bgColorSet─┘││ │ bgColor └data format | DarkGreen | 0x82000000 | 10000010 00000000... | | DarkYellow | 0x83000000 | 10000011 00000000... | | DarkBlue | 0x84000000 | 10000100 00000000... | -| DarkMagneta | 0x85000000 | 10000101 00000000... | +| DarkMagenta | 0x85000000 | 10000101 00000000... | | DarkCyan | 0x86000000 | 10000110 00000000... | | Gray | 0x87000000 | 10000111 00000000... | | DarkGray | 0x88000000 | 10001000 00000000... | @@ -27,7 +27,7 @@ bgColorSet─┘││ │ bgColor └data format | Green | 0x8a000000 | 10001010 00000000... | | Yellow | 0x8b000000 | 10001011 00000000... | | Blue | 0x8c000000 | 10001100 00000000... | -| Magneta | 0x8d000000 | 10001101 00000000... | +| Magenta | 0x8d000000 | 10001101 00000000... | | Cyan | 0x8e000000 | 10001110 00000000... | | White | 0x8f000000 | 10001111 00000000... | @@ -39,7 +39,7 @@ bgColorSet─┘││ │ bgColor └data format | DarkGreen | 0x40200000 | 01000000 00100000... | | DarkYellow | 0x40300000 | 01000000 00110000... | | DarkBlue | 0x40400000 | 01000000 01000000... | -| DarkMagneta | 0x40500000 | 01000000 01010000... | +| DarkMagenta | 0x40500000 | 01000000 01010000... | | DarkCyan | 0x40600000 | 01000000 01100000... | | Gray | 0x40700000 | 01000000 01110000... | | DarkGray | 0x40800000 | 01000000 10000000... | @@ -47,7 +47,7 @@ bgColorSet─┘││ │ bgColor └data format | Green | 0x40a00000 | 01000000 10100000... | | Yellow | 0x40b00000 | 01000000 10110000... | | Blue | 0x40c00000 | 01000000 11000000... | -| Magneta | 0x40d00000 | 01000000 11010000... | +| Magenta | 0x40d00000 | 01000000 11010000... | | Cyan | 0x40e00000 | 01000000 11100000... | | White | 0x40f00000 | 01000000 11110000... | diff --git a/tests/test_kprint_colors.c b/tests/test_kprint_colors.c index ac1564c..afe3640 100644 --- a/tests/test_kprint_colors.c +++ b/tests/test_kprint_colors.c @@ -33,7 +33,7 @@ void test_kprint_colors(){ testColor(DarkGreen); testColor(DarkYellow); testColor(DarkBlue); - testColor(DarkMagneta); + testColor(DarkMagenta); testColor(DarkCyan); testColor(Gray); testColor(DarkGray); @@ -41,7 +41,7 @@ void test_kprint_colors(){ testColor(Green); testColor(Yellow); testColor(Blue); - testColor(Magneta); + testColor(Magenta); testColor(Cyan); testColor(White); kprint_setColor(kprint_bgBlack | kprint_fgGray);