From ab607fc39b6dfc766f7481c33e5f1cf35a2f55d9 Mon Sep 17 00:00:00 2001 From: stderr64 Date: Thu, 24 Oct 2024 23:04:43 +0300 Subject: Recreated repository --- big_sqlite_db/bigdb.pl | 81 +++++++++++++++++++++++++++ bigarray/bigarray.c | 17 ++++++ bigarray/bigarray.cpp | 22 ++++++++ bigarray/bigarray.go | 14 +++++ bigarray/bigarray.pl | 21 +++++++ binary_config/binary_config.cpp | 119 ++++++++++++++++++++++++++++++++++++++++ binary_config/compile.sh | 5 ++ cpp_alloc_test/Makefile | 7 +++ cpp_alloc_test/alloctest.cpp | 87 +++++++++++++++++++++++++++++ perl_http_server/server.pl | 40 ++++++++++++++ perl_number_sort/number_sort.pl | 49 +++++++++++++++++ sdltest1/rpl.bmp | Bin 0 -> 6220938 bytes sdltest1/sdltest.c | 79 ++++++++++++++++++++++++++ 13 files changed, 541 insertions(+) create mode 100755 big_sqlite_db/bigdb.pl create mode 100644 bigarray/bigarray.c create mode 100644 bigarray/bigarray.cpp create mode 100644 bigarray/bigarray.go create mode 100755 bigarray/bigarray.pl create mode 100644 binary_config/binary_config.cpp create mode 100755 binary_config/compile.sh create mode 100644 cpp_alloc_test/Makefile create mode 100644 cpp_alloc_test/alloctest.cpp create mode 100755 perl_http_server/server.pl create mode 100755 perl_number_sort/number_sort.pl create mode 100644 sdltest1/rpl.bmp create mode 100644 sdltest1/sdltest.c diff --git a/big_sqlite_db/bigdb.pl b/big_sqlite_db/bigdb.pl new file mode 100755 index 0000000..429970d --- /dev/null +++ b/big_sqlite_db/bigdb.pl @@ -0,0 +1,81 @@ +#!/usr/bin/perl +use POSIX; +use IO::File; +use DBI; +use DBD::SQLite; + +srand(); + +if ( scalar(@ARGV) < 3 ){ + print( "Usage:".__FILE__." [database file] [rows to create] [rows before each commit]\n" ); + exit( 0 ); +} + +sub gen_random{ + return int( rand($_[0]) ); +} + +my $g_db = DBI->connect( "DBI:SQLite:dbname=".$ARGV[0], "", "" ) or die "Failed to open database"; + +sub tx_reset{ + $g_db->do( "COMMIT;" ) or die "Failed to commit"; + $g_db->do( "BEGIN;" ) or die "Failed to start transaction"; + return; +} + +my $tbl_create = $g_db->prepare( "create table if not exists testtable(rnd_num integer);" ) or die "Failed to prepare query"; +$tbl_create->execute() or die "Failed to execute query"; +$tbl_create->finish() or die "Failed to finish query"; +undef $tbl_create; + +my $idx_create = $g_db->prepare( "create index if not exists testidx on testtable(rnd_num);" ) or die "Failed to prepare query"; +$idx_create->execute() or die "Failed to execute query"; +$idx_create->finish() or die "Failed to finish query"; +undef $idx_create; + +my $c_row_count = 0; +my $c_uncommitted = 0; +my $setcounter = 0; + +my $iquery = $g_db->prepare( "insert into testtable(rnd_num) values(?);" ) or die "Failed to prepare insert query"; + +my $rnd_val = gen_random( 100 ); + +$g_db->do( "pragma synchronous = off;" ) or die "Failed to disable fsync"; + +$g_db->do( "BEGIN;" ) or die "Failed to start transaction"; + +for ( ; $c_row_count < int($ARGV[1]); ){ + for ( ; $setcounter < 100000; $setcounter++ ){ + $iquery->bind_param( 1, $rnd_val, $SQL_INTEGER ) or die "Failed to bind"; + $iquery->execute() or die "Failed to execute query"; + $iquery->finish() or die "Failed to finish query"; + print( "value = ".$rnd_val."\tcount = ".$c_row_count."\n" ); + $rnd_val = gen_random( 100 ); + $c_row_count++; + $c_uncommitted++; + if ( $c_uncommitted >= int($ARGV[2]) ){ + print( "Commit threshold reached, comitting and starting new transaction\n" ); + tx_reset(); + print( "Commit finished and new transaction started\n" ); + $c_uncommitted = 0; + } + } + print( "value = ".$rnd_val."\tcount = ".$c_row_count."\n" ); + print( "Generated set of 100000 rows, waiting 1 second before next\n" ); + $setcounter = 0; + sleep( 1 ); +} + +$g_db->do( "COMMIT;" ) or die "Failed to do final commit"; + +$g_db->disconnect() or die "Failed to disconnect"; + +undef $iquery; +undef $c_row_count; +undef $c_uncommitted; +undef $g_db; +undef $rnd_val; +undef $setcounter; + +exit( 0 ); diff --git a/bigarray/bigarray.c b/bigarray/bigarray.c new file mode 100644 index 0000000..74bcb56 --- /dev/null +++ b/bigarray/bigarray.c @@ -0,0 +1,17 @@ +#include +#include +#include +#include +#include + +int main( int argc, char *args[] ){ + uint64_t bigarray[500000] = {}; + uint64_t ncount = 0; + for ( ; ncount < 500000; ncount++ ) + bigarray[ncount] = ncount; + ncount = 0; + for ( ; ncount < 500000; ncount++ ) + fprintf( stdout, "%lli\n", ncount ); + memset( bigarray, 0, sizeof(bigarray) ); + return EXIT_SUCCESS; +} diff --git a/bigarray/bigarray.cpp b/bigarray/bigarray.cpp new file mode 100644 index 0000000..5516db5 --- /dev/null +++ b/bigarray/bigarray.cpp @@ -0,0 +1,22 @@ +#include +#include +#include +#include +#include + +using namespace std; + +int main( int argc, char *args[] ){ + std::array bigarray = {}; + uint64_t acount = 0; + for ( ; acount < 500000; acount++ ) + bigarray[acount] = acount; + acount = 0; + uint64_t arr_size = (uint64_t)bigarray.size(); + for ( ; acount < arr_size; acount++ ) + std::cout << bigarray[acount] << "\n"; + acount = 0; + arr_size = 0; + bigarray = {}; + return EXIT_SUCCESS; +} diff --git a/bigarray/bigarray.go b/bigarray/bigarray.go new file mode 100644 index 0000000..8bf9d00 --- /dev/null +++ b/bigarray/bigarray.go @@ -0,0 +1,14 @@ +package main + +func main(){ + var bigarray [500000]uint64 + var anum uint64 = 0 + for ; anum < 500000; anum++ { + bigarray[anum] = anum + } + anum = 0 + for ; anum < 500000; anum++ { + println( bigarray[anum] ) + } + return +} diff --git a/bigarray/bigarray.pl b/bigarray/bigarray.pl new file mode 100755 index 0000000..6e662f1 --- /dev/null +++ b/bigarray/bigarray.pl @@ -0,0 +1,21 @@ +#!/usr/bin/perl +use POSIX; + +my @bigarray = (); + +my $acount = 0; + +for ( ; $acount < 500000; $acount++ ){ + push( @bigarray, $acount ); +} + +$acount = 0; + +for ( ; $acount < 500000; $acount++ ){ + print( $bigarray[$acount]."\n" ); +} + +undef $acount; +undef @bigarray; + +exit( 0 ); diff --git a/binary_config/binary_config.cpp b/binary_config/binary_config.cpp new file mode 100644 index 0000000..1acbd3d --- /dev/null +++ b/binary_config/binary_config.cpp @@ -0,0 +1,119 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +struct config_data{ + unsigned char testvalue1[255]; + unsigned char testvalue2[255]; +}; + +struct config_data g_config = {}; + +bool write_test_config( std::fstream *cfg_file, struct config_data *cdata ){ + if ( cfg_file == NULL || !cfg_file->is_open() ) + return false; + if ( cdata == NULL ) + return false; + char *tmp_copy = (char*)calloc( sizeof(*cdata), 1 ); + if ( tmp_copy == NULL ) + return false; + std::cout << "Allocated memory for writing config: " << (sizeof(*cdata) * 1) << " bytes\n"; + memcpy( (void*)tmp_copy, (const void*)cdata, sizeof(*cdata) ); + cfg_file->write( tmp_copy, sizeof(*cdata) ); + cfg_file->flush(); + free( tmp_copy ); + return true; +} + +void print_config( std::fstream *cfg, struct config_data *cdt ){ + if ( !cfg->is_open() ) + return; + if ( cdt == NULL ) + return; + size_t cfg_size = 0; + cfg->seekg( 0, std::ios::end ); + cfg_size = (size_t)cfg->tellg(); + cfg->seekg( 0, std::ios::beg ); + if ( cfg_size < sizeof(*cdt) || cfg_size > sizeof(*cdt) ){ + std::cerr << "Error: config file isn't valid\n"; + return; + } + char *cbytes = (char*)calloc( cfg_size, sizeof(char) ); + if ( cbytes == NULL ) + return; + std::cout << "Allocated memory for reading config: " << (cfg_size * sizeof(char)) << " bytes\n"; + cfg->read( cbytes, cfg_size ); + memcpy( (void*)cdt, (const void*)cbytes, cfg_size ); + cfg_size = 0; + free( cbytes ); + std::cout << "testvalue1 = " << cdt->testvalue1 << "\ntestvalue2 = " << cdt->testvalue2 << "\n"; + return; +} + +int main( int argc, char *args[] ){ + if ( argc <= 1 || args[1] == NULL ){ + std::cout << "Usage: " << args[0] << " [config file]\n"; + exit( EXIT_SUCCESS ); + } + memset( (void*)&g_config, 0, sizeof(struct config_data) ); + std::cout << "Opening test config file " << args[1] << "\n"; + std::fstream *ocfg = new std::fstream( args[1], std::ios::in | std::ios::out | std::ios::binary | std::ios::app ); + if ( !ocfg->good() ){ + std::cerr << "Failed to open given config file (" << strerror(errno) << ")\n"; + exit( EXIT_FAILURE ); + } + std::cout << "Opened test config file " << args[1] << "\n"; + char *tv1 = readline( "Test value 1: " ); + if ( tv1 == NULL ){ + std::cerr << "Error: no value given\n"; + ocfg->close(); + exit( EXIT_FAILURE ); + } + if ( (size_t)strlen(tv1) > 255 ){ + std::cerr << "Error: test value 1 is too long\n"; + free( tv1 ); + ocfg->close(); + exit( EXIT_FAILURE ); + } + memcpy( (void*)&g_config.testvalue1, (const void*)tv1, strlen(tv1) ); + free( tv1 ); + char *tv2 = readline( "Test value 2: " ); + if ( tv2 == NULL ){ + std::cerr << "Error: no value given\n"; + ocfg->close(); + exit( EXIT_FAILURE ); + } + if ( (size_t)strlen(tv2) > 255 ){ + std::cerr << "Error: test value 2 is too long\n"; + ocfg->close(); + free( tv2 ); + exit( EXIT_FAILURE ); + } + memcpy( (void*)&g_config.testvalue2, (const void*)tv2, strlen(tv2) ); + free( tv2 ); + std::cout << "Writing test config file\n"; + if ( !write_test_config(ocfg, &g_config) ){ + std::cerr << "Error: failed to write test config\n"; + ocfg->close(); + exit( EXIT_FAILURE ); + } + std::cout << "Test config written\n"; + std::cout << "Clearing config from memory before reading it back\n"; + memset( (void*)&g_config, 0, sizeof(g_config) ); + std::cout << "Config cleared from memory\n"; + std::cout << "Printing out the test config by reading it from written file\n"; + print_config( ocfg, &g_config ); + std::cout << "Clearing config from memory\n"; + memset( (void*)&g_config, 0, sizeof(g_config) ); + std::cout << "Config cleared from memory\n"; + std::cout << "Closing config file\n"; + ocfg->close(); + std::cout << "Config closed, exiting\n"; + exit( EXIT_SUCCESS ); +} diff --git a/binary_config/compile.sh b/binary_config/compile.sh new file mode 100755 index 0000000..229d6e7 --- /dev/null +++ b/binary_config/compile.sh @@ -0,0 +1,5 @@ +#!/usr/bin/env bash + +echo 'Compiling..' +/usr/bin/g++ -Wall -pedantic -Werror binary_config.cpp -o binary_config -lreadline +echo 'Finished compiling' diff --git a/cpp_alloc_test/Makefile b/cpp_alloc_test/Makefile new file mode 100644 index 0000000..cc90ca6 --- /dev/null +++ b/cpp_alloc_test/Makefile @@ -0,0 +1,7 @@ +CC=g++ + +rel: + $(CC) -pedantic -Werror -Wall -O2 alloctest.cpp -o alloctest + +debug: + $(CC) -pedantic -Werror -Wall -O2 -ggdb alloctest.cpp -o alloctest diff --git a/cpp_alloc_test/alloctest.cpp b/cpp_alloc_test/alloctest.cpp new file mode 100644 index 0000000..0671901 --- /dev/null +++ b/cpp_alloc_test/alloctest.cpp @@ -0,0 +1,87 @@ +#include +#include +#include +#include +#include +#include + +class AllocTest{ + private: std::string *in_class_str = NULL; + private: std::string *local_str_ptr = NULL; + + public: AllocTest(){ + return; + } + + public: void is_heap_or_stack( void *mem_addr ){ + if ( mem_addr == NULL ){ + std::cout << "Error: address is NULL\n"; + return; + } + if ( ((size_t)mem_addr << 8) >= 0x55000000000000 && ((size_t)mem_addr << 8) <= 0x5fffffffffffff ){ + std::cout << "Address is in heap memory area (address is " << mem_addr << ")\n"; + return; + } + else if ( ((size_t)mem_addr << 8) >= 0x7f000000000000 ){ + std::cout << "Address is in stack memory area (address is " << mem_addr << ")\n"; + return; + } + else + { + std::cout << "Unknown memory area (address is " << mem_addr << ")\n"; + return; + } + } + + public: void test_local_std_string(){ + std::string local_str( "test string" ); + this->local_str_ptr = &local_str; + this->is_heap_or_stack( static_cast(&local_str) ); + return; + } + + public: void local_std_string_exists(){ + if ( this->local_str_ptr == NULL ){ + std::cout << "Local std::string no longer exists in memory\n"; + return; + } + std::cout << "Local std::string is still non-NULL value: " << this->local_str_ptr->c_str() << "\n"; + return; + } + + public: void test_new_std_string(){ + std::string *teststr = new std::string( "string created with new keyword" ); + this->is_heap_or_stack( static_cast(teststr) ); + delete teststr; + return; + } + + public: void test_in_class_str(){ + this->in_class_str = new std::string( "string created into class" ); + this->is_heap_or_stack( static_cast(this->in_class_str) ); + delete this->in_class_str; + return; + } + + public: ~AllocTest(){ + if ( this->local_str_ptr != NULL ) + this->local_str_ptr = NULL; + return; + } +}; + +int main( int argc, char *args[] ){ + AllocTest *alloc_test = new AllocTest(); + std::cout << "Testing class memory address\n"; + alloc_test->is_heap_or_stack( alloc_test ); + std::cout << "Testing locally created std::string\n"; + alloc_test->test_local_std_string(); + std::cout << "Checking if local std::string still exists in memory\n"; + alloc_test->local_std_string_exists(); + std::cout << "Testing std::string created with new keyword\n"; + alloc_test->test_new_std_string(); + std::cout << "Testing std::string created into class\n"; + alloc_test->test_in_class_str(); + delete alloc_test; + exit( EXIT_SUCCESS ); +} diff --git a/perl_http_server/server.pl b/perl_http_server/server.pl new file mode 100755 index 0000000..3fe3035 --- /dev/null +++ b/perl_http_server/server.pl @@ -0,0 +1,40 @@ +#!/usr/bin/perl +use POSIX; +use IO::Socket; + +if ( scalar(@ARGV) < 2 ){ + print( "Usage: ".__FILE__." [IP] [port]\n" ); + exit( 0 ); +} + +my $resp = "HTTP/1.1 200 OK\r\nServer: perlhttp\r\nContent-Type: text/html\r\n\r\n

Hi there

"; + +my $ssock = IO::Socket->new( + "LocalHost" => $ARGV[0], + "LocalPort" => $ARGV[1], + "Listen" => 10, + "Blocking" => 1, + "Type" => IO::Socket::SOCK_STREAM, + "Domain" => IO::Socket::AF_INET, + "Proto" => "tcp", + "ReusePort" => 1, + "ReuseAddr" => 1 +) or die "Failed to create socket"; + +my $req = ""; +my $psock = ""; + +print( "Listening on http://".$ARGV[0].":".$ARGV[1]."\n" ); + +while ( $psock = $ssock->accept() ){ + print( "Request from ".$psock->peerhost()."\n" ); + $psock->recv( $req, 524288 ); + $psock->send( $resp ); + $psock->shutdown( SHUT_RDWR ); + $req = ""; +} + +undef $req; +undef $ssock; +undef $psock; +exit( 0 ); diff --git a/perl_number_sort/number_sort.pl b/perl_number_sort/number_sort.pl new file mode 100755 index 0000000..1190f4f --- /dev/null +++ b/perl_number_sort/number_sort.pl @@ -0,0 +1,49 @@ +#!/usr/bin/perl +use POSIX; + +if ( scalar(@ARGV) < 1 ){ + print( "Usage: ".__FILE__." [amount of numbers]\n" ); + exit( 0 ); +} + +if ( int($ARGV[0]) > 999 ){ + print( "Only maximum of 999 numbers can be generated\n" ); + exit( 0 ); +} + +srand(); + +my @numbers = (); +my @numbers_sorted = (); + +sub sort_fn{ + if ( $a < $b ){ + return -1; + } + elsif ( $a > $b ){ + return 1; + } + else + { + return 0; + } +} + +print( "Generating numbers\n" ); +for ( my $ncount = 0; $ncount < int($ARGV[0]); $ncount++ ){ + push( @numbers, int(rand(999)) ); +} +print( "Numbers generated\n" ); + +if ( defined($ncount) ){ + undef $ncount; +} + +@numbers_sorted = sort( sort_fn @numbers ); + +print( "Unsorted:\n".join(", ", @numbers)."\n\n" ); +print( "Sorted:\n".join(", ", @numbers_sorted)."\n" ); + +undef @numbers_sorted; +undef @numbers; +exit( 0 ); diff --git a/sdltest1/rpl.bmp b/sdltest1/rpl.bmp new file mode 100644 index 0000000..f7816d8 Binary files /dev/null and b/sdltest1/rpl.bmp differ diff --git a/sdltest1/sdltest.c b/sdltest1/sdltest.c new file mode 100644 index 0000000..b084892 --- /dev/null +++ b/sdltest1/sdltest.c @@ -0,0 +1,79 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +SDL_Window *swin = NULL; +SDL_Surface *ssurf = NULL; +SDL_Renderer *srender = NULL; +SDL_Texture *stex = NULL; + +SDL_Event s_event; + +int main( int argc, char *args[] ){ + if ( SDL_Init(SDL_INIT_EVERYTHING) != 0 ){ + fprintf( stderr, "Error initializing SDL (%s)\n", SDL_GetError() ); + return EXIT_FAILURE; + } + if ( (ssurf = SDL_LoadBMP("./rpl.bmp")) == NULL ){ + fprintf( stderr, "Error loading BMP file (%s)\n", SDL_GetError() ); + SDL_Quit(); + return EXIT_FAILURE; + } + swin = SDL_CreateWindow( "Test window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE ); + if ( swin == NULL ){ + fprintf( stderr, "Error creating SDL window (%s)\n", SDL_GetError() ); + SDL_DestroyWindow( swin ); + memset( ssurf, 0, sizeof(*ssurf) ); + SDL_Quit(); + return EXIT_FAILURE; + } + if ( (srender = SDL_CreateRenderer(swin, -1, SDL_RENDERER_ACCELERATED)) == NULL ){ + fprintf( stderr, "Error creating renderer (%s)\n", SDL_GetError() ); + SDL_DestroyWindow( swin ); + memset( ssurf, 0, sizeof(*ssurf) ); + SDL_Quit(); + return EXIT_FAILURE; + } + if ( SDL_RenderSetViewport(srender, NULL) != 0 ){ + fprintf( stderr, "Error setting viewport (%s)\n", SDL_GetError() ); + SDL_DestroyRenderer( srender ); + SDL_DestroyWindow( swin ); + memset( ssurf, 0, sizeof(*ssurf) ); + SDL_Quit(); + return EXIT_FAILURE; + } + if ( (stex = SDL_CreateTextureFromSurface(srender, ssurf)) == NULL ){ + fprintf( stderr, "Error creating texture from surface data (%s)\n", SDL_GetError() ); + SDL_DestroyRenderer( srender ); + SDL_DestroyWindow( swin ); + memset( ssurf, 0, sizeof(*ssurf) ); + SDL_Quit(); + return EXIT_FAILURE; + } + while ( true ){ + if ( SDL_WaitEvent(&s_event) == 1 ){ + if ( s_event.type == SDL_QUIT ) + break; + } + if ( SDL_RenderCopy(srender, stex, NULL, NULL) != 0 ) + break; + SDL_RenderPresent( srender ); + } + SDL_DestroyTexture( stex ); + SDL_DestroyRenderer( srender ); + memset( ssurf, 0, sizeof(*ssurf) ); + SDL_DestroyWindow( swin ); + SDL_Quit(); + return EXIT_SUCCESS; +} -- cgit v1.2.3-86-g962b