PACK() macro

This commit is contained in:
timerix 2022-08-14 13:23:39 +06:00
parent 4e162bd474
commit 7ba7201d3a
13 changed files with 51 additions and 25 deletions

View File

@ -32,6 +32,8 @@
<ClCompile Include="src\HashFunctions\hash.c" /> <ClCompile Include="src\HashFunctions\hash.c" />
<ClCompile Include="src\Hashtable\Hashtable.c" /> <ClCompile Include="src\Hashtable\Hashtable.c" />
<ClCompile Include="src\Hashtable\KeyValuePair.c" /> <ClCompile Include="src\Hashtable\KeyValuePair.c" />
<ClCompile Include="src\Network\knAddress.c" />
<ClCompile Include="src\Network\knPackage.c" />
<ClCompile Include="src\Network\knSocket.c" /> <ClCompile Include="src\Network\knSocket.c" />
<ClCompile Include="src\SearchTree\SearchTree.c" /> <ClCompile Include="src\SearchTree\SearchTree.c" />
<ClCompile Include="src\String\string.c" /> <ClCompile Include="src\String\string.c" />
@ -44,6 +46,7 @@
<ClCompile Include="tests\test_hashtable2.cpp" /> <ClCompile Include="tests\test_hashtable2.cpp" />
<ClCompile Include="tests\test_hash_functions.c" /> <ClCompile Include="tests\test_hash_functions.c" />
<ClCompile Include="tests\test_marshalling.c" /> <ClCompile Include="tests\test_marshalling.c" />
<ClCompile Include="tests\test_network.c" />
<ClCompile Include="tests\test_safethrow.c" /> <ClCompile Include="tests\test_safethrow.c" />
<ClCompile Include="tests\test_searchtree.c" /> <ClCompile Include="tests\test_searchtree.c" />
<ClCompile Include="tests\test_string.c" /> <ClCompile Include="tests\test_string.c" />
@ -64,7 +67,11 @@
<ClInclude Include="src\Hashtable2\Hashtable2.hpp" /> <ClInclude Include="src\Hashtable2\Hashtable2.hpp" />
<ClInclude Include="src\Hashtable\Hashtable.h" /> <ClInclude Include="src\Hashtable\Hashtable.h" />
<ClInclude Include="src\Hashtable\KeyValuePair.h" /> <ClInclude Include="src\Hashtable\KeyValuePair.h" />
<ClInclude Include="src\Network\knAddress.h" />
<ClInclude Include="src\Network\knPackage.h" />
<ClInclude Include="src\Network\knSocket.h" />
<ClInclude Include="src\Network\network.h" /> <ClInclude Include="src\Network\network.h" />
<ClInclude Include="src\Network\stdSocketHeaders.h" />
<ClInclude Include="src\SearchTree\SearchTree.h" /> <ClInclude Include="src\SearchTree\SearchTree.h" />
<ClInclude Include="src\String\string.h" /> <ClInclude Include="src\String\string.h" />
<ClInclude Include="src\String\StringBuilder.h" /> <ClInclude Include="src\String\StringBuilder.h" />
@ -75,7 +82,7 @@
<Keyword>Win32Proj</Keyword> <Keyword>Win32Proj</Keyword>
<ProjectGuid>{52f0bd29-a3cb-47ce-b25d-ceaf5dfb2d73}</ProjectGuid> <ProjectGuid>{52f0bd29-a3cb-47ce-b25d-ceaf5dfb2d73}</ProjectGuid>
<RootNamespace>kerep</RootNamespace> <RootNamespace>kerep</RootNamespace>
<WindowsTargetPlatformVersion>10.0.18362.0</WindowsTargetPlatformVersion> <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
@ -218,6 +225,8 @@
<CompileAs>Default</CompileAs> <CompileAs>Default</CompileAs>
<LanguageStandard>stdcpp20</LanguageStandard> <LanguageStandard>stdcpp20</LanguageStandard>
<DebugInformationFormat>None</DebugInformationFormat> <DebugInformationFormat>None</DebugInformationFormat>
<TreatAngleIncludeAsExternal>true</TreatAngleIncludeAsExternal>
<DisableAnalyzeExternal>true</DisableAnalyzeExternal>
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>

View File

@ -1,9 +1,9 @@
#include "Autoarr.h" #include "Autoarr.h"
Autoarr_define(uint8) Autoarr_define(uint8);
Autoarr_define(int8) Autoarr_define(int8);
Autoarr_define(uint16) Autoarr_define(uint16);
Autoarr_define(int16) Autoarr_define(int16);
Autoarr_define(uint32); Autoarr_define(uint32);
Autoarr_define(int32); Autoarr_define(int32);
Autoarr_define(uint64); Autoarr_define(uint64);

0
src/Network/README.md Normal file
View File

View File

@ -19,7 +19,7 @@ typedef union knIPV4Address {
///@return Maybe<uint64> as Maybe<knIPV4Address> ///@return Maybe<uint64> as Maybe<knIPV4Address>
Maybe knIPV4Address_fromStr(char* addrStr); Maybe knIPV4Address_fromStr(char* addrStr);
typedef struct __attribute__((__packed__)) knIPV4Endpoint { typedef struct knIPV4Endpoint {
knIPV4Address address; knIPV4Address address;
knPort port; knPort port;
} knIPV4Endpoint; } knIPV4Endpoint;

View File

@ -10,9 +10,9 @@ extern "C" {
#define KNPAC_MAX_DATA_SIZE (65535-sizeof(knPackage)+sizeof(uint8*)) #define KNPAC_MAX_DATA_SIZE (65535-sizeof(knPackage)+sizeof(uint8*))
typedef enum __attribute__((__packed__)) knPacVersion { typedef enum knPacVersion PACK({
knPac_V1 knPac_V1
} knPacVersion; }) knPacVersion;
static const char knPacHeader[5]={'k','n','p','a','c'}; static const char knPacHeader[5]={'k','n','p','a','c'};

View File

@ -2,7 +2,7 @@
#include "stdSocketHeaders.h" #include "stdSocketHeaders.h"
Maybe knSocket_open(knSockType sockType){ Maybe knSocket_open(knSocketType sockType){
knSocket* newSocket=malloc(sizeof(knSocket)); knSocket* newSocket=malloc(sizeof(knSocket));
newSocket->type=sockType; newSocket->type=sockType;
newSocket->channels=NULL; newSocket->channels=NULL;
@ -13,12 +13,12 @@ Maybe knSocket_open(knSockType sockType){
default: default:
safethrow("unknown socket type", free(newSocket)); safethrow("unknown socket type", free(newSocket));
break; break;
case knSockType_TCP: case knSocketType_TCP:
newSocket->socketfd=socket(AF_INET, SOCK_STREAM, 0); newSocket->socketfd=socket(AF_INET, SOCK_STREAM, 0);
if(newSocket->socketfd==-1) if(newSocket->socketfd==-1)
safethrow("can't create TCP socket", free(newSocket)); safethrow("can't create TCP socket", free(newSocket));
break; break;
case knSockType_UDP: case knSocketType_UDP:
newSocket->socketfd=socket(AF_INET, SOCK_DGRAM, 0); newSocket->socketfd=socket(AF_INET, SOCK_DGRAM, 0);
if(newSocket->socketfd==-1) if(newSocket->socketfd==-1)
safethrow("can't create UDP socket", free(newSocket)); safethrow("can't create UDP socket", free(newSocket));

View File

@ -9,12 +9,12 @@ extern "C" {
#include "knPackage.h" #include "knPackage.h"
typedef enum __attribute__((__packed__)) knSockType { typedef enum knSocketType PACK({
knSockType_TCP, knSockType_UDP knSocketType_TCP, knSocketType_UDP
} knSockType; }) knSocketType;
typedef struct knSocket { typedef struct knSocket {
knSockType type; knSocketType type;
uint16 channelsAmount; uint16 channelsAmount;
knChannel* channels; knChannel* channels;
int64 socketfd; int64 socketfd;
@ -23,7 +23,7 @@ typedef struct knSocket {
} knSocket; } knSocket;
///@return Maybe<knSocket*> new socket ///@return Maybe<knSocket*> new socket
Maybe knSocket_open(knSockType sockType); Maybe knSocket_open(knSocketType sockType);
///@return Maybe<void> error or nothing ///@return Maybe<void> error or nothing
Maybe knSocket_close(knSocket* socket); Maybe knSocket_close(knSocket* socket);

View File

@ -13,7 +13,8 @@ extern "C" {
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h> #include <netinet/in.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <netdb.h> #include <netdb.h>
#include <unistd.h>
#endif #endif
#if __cplusplus #if __cplusplus

View File

@ -11,7 +11,6 @@ extern "C" {
#include <locale.h> #include <locale.h>
#include <time.h> #include <time.h>
#include <setjmp.h> #include <setjmp.h>
#include <unistd.h>
#define dbg(N) printf("\e[95m%d\n",N) #define dbg(N) printf("\e[95m%d\n",N)
@ -36,6 +35,7 @@ extern "C" {
#pragma GCC error "unknown compiler" #pragma GCC error "unknown compiler"
#endif #endif
#ifdef _MSC_VER #ifdef _MSC_VER
#define IFWIN(YES, NO) YES #define IFWIN(YES, NO) YES
#define IFMSC(YES, NO) YES #define IFMSC(YES, NO) YES
@ -53,6 +53,15 @@ extern "C" {
#define NULL ((void*)0) #define NULL ((void*)0)
#endif #endif
#ifdef __GNUC__
#define PACK(...) __VA_ARGS__ __attribute__((__packed__))
#elif defined(_MSC_VER)
#define PACK(...) __pragma(pack(push, 1)) __VA_ARGS__ __pragma(pack(pop))
#endif
#if __cplusplus #if __cplusplus
} }
#endif #endif

View File

@ -16,7 +16,7 @@ typedef int64_t int64;
typedef uint64_t uint64; typedef uint64_t uint64;
typedef float float32; typedef float float32;
typedef double float64; typedef double float64;
typedef enum __attribute__((__packed__)) my_type { typedef enum my_type PACK({
Null, Float32, Float64, Char, Bool, Null, Float32, Float64, Char, Bool,
UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64, UInt8, Int8, UInt16, Int16, UInt32, Int32, UInt64, Int64,
UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr, UInt8Ptr, Int8Ptr, UInt16Ptr, Int16Ptr, UInt32Ptr, Int32Ptr, UInt64Ptr, Int64Ptr,
@ -25,7 +25,7 @@ typedef enum __attribute__((__packed__)) my_type {
AutoarrInt8Ptr, AutoarrUInt8Ptr, AutoarrInt16Ptr, AutoarrUInt16Ptr, AutoarrInt8Ptr, AutoarrUInt8Ptr, AutoarrInt16Ptr, AutoarrUInt16Ptr,
AutoarrInt32Ptr, AutoarrUInt32Ptr, AutoarrInt64Ptr, AutoarrUInt64Ptr, AutoarrInt32Ptr, AutoarrUInt32Ptr, AutoarrInt64Ptr, AutoarrUInt64Ptr,
AutoarrUnitypePtr, AutoarrKVPairPtr, knSocketPtr AutoarrUnitypePtr, AutoarrKVPairPtr, knSocketPtr
} my_type; }) my_type;
#define my_type_last knSocketPtr #define my_type_last knSocketPtr
const char* my_type_name(my_type t); const char* my_type_name(my_type t);

View File

@ -20,15 +20,20 @@ void __test_knIPV4Address_fromStr(char* addrStr, uint8 a, uint8 b, uint8 c, uint
void test_network(){ void test_network(){
optime(__func__,1,({ optime(__func__,1,({
printf("\e[96m------------[test_network]------------\n"); printf("\e[96m------------[test_network]------------\n");
if(sizeof(knIPV4Endpoint)!=sizeof(knPort)+sizeof(knIPV4Address))
throw(ERR_WRONGTYPE); 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(127,0,0,1);
test_knIPV4Address_fromStr(34,255,45,0); test_knIPV4Address_fromStr(34,255,45,0);
test_knIPV4Address_fromStr(3,3,3,128); test_knIPV4Address_fromStr(3,3,3,128);
knSocket* s; knSocket* s;
tryLast(knSocket_open(knSockType_TCP), maybeS) tryLast(knSocket_open(knSocketType_TCP), maybeS)
s=maybeS.value.VoidPtr; s=maybeS.value.VoidPtr;
printf("\e[92mTCP socket created\n"); printf("\e[92mTCP socket created\n");
tryLast(knSocket_close(s);,_); tryLast(knSocket_close(s);,_);

View File

@ -7,7 +7,7 @@ void printstnode(STNode* node){
"\n address: %p\n value: ",sizeof(STNode),node); "\n address: %p\n value: ",sizeof(STNode),node);
printuni(node->value); printuni(node->value);
// prints pointers to all existing branches // prints pointers to all existing branches
printf("\n branches: %p\n", node->branches); /* printf("\n branches: %p\n", node->branches);
if(node->branches) for(uint8 i=0;i<8;i++){ if(node->branches) for(uint8 i=0;i<8;i++){
printf(" \e[90m[%u]=%p\n",i,node->branches[i]); printf(" \e[90m[%u]=%p\n",i,node->branches[i]);
if(node->branches[i]) if(node->branches[i])
@ -18,7 +18,7 @@ void printstnode(STNode* node){
printf(" \e[90m[%u]=%p\n",iii,node->branches[i][ii][iii]); printf(" \e[90m[%u]=%p\n",iii,node->branches[i][ii][iii]);
} }
} } */
} }
void test_searchtree(){ void test_searchtree(){

View File

@ -15,6 +15,8 @@ void test_hashtable();
void test_dtsod(); void test_dtsod();
void test_network(); void test_network();
#define PRINT_SIZEOF(T) printf("\e[94m" #T " size: \e[96m" IFWIN("%llu", "%lu") "\n", sizeof(T))
#if __cplusplus #if __cplusplus
} }
#endif #endif