From 7839460855129c37fd4c1cd68365a001f7a45783 Mon Sep 17 00:00:00 2001 From: Cristian Herghelegiu Date: Sun, 22 Dec 2024 02:05:23 +0200 Subject: [PATCH 1/3] Reduce Linux dependencies --- examples/examples_base64.h | 9 ++++----- examples/examples_buffer.h | 1 - examples/examples_event.h | 3 +-- examples/examples_hash.h | 1 - examples/examples_jobs_engine.h | 1 - examples/examples_lock_queue.h | 5 ++--- examples/examples_prio_queue.h | 1 - examples/examples_spinlock.h | 1 - examples/examples_time_queue.h | 3 +-- examples/examples_util.h | 5 ++--- examples/examples_worker_threads.h | 1 - include/base64.h | 4 ++-- include/buffer.h | 26 +++++++++++++------------- include/critical_section.h | 7 +++---- include/impl/base_buffer_impl.h | 12 ++++++------ include/util_rand.h | 16 ++++++++-------- include/util_time.h | 6 +++--- main.cpp | 19 +++++++++---------- 18 files changed, 54 insertions(+), 67 deletions(-) diff --git a/examples/examples_base64.h b/examples/examples_base64.h index b0853f0..adfa994 100644 --- a/examples/examples_base64.h +++ b/examples/examples_base64.h @@ -3,7 +3,6 @@ #include #include #include -#include #include "../include/base64.h" @@ -17,8 +16,8 @@ namespace examples::base64 { constexpr std::string_view str{"hello world"}; - std::string b64 = small::tobase64(str); - auto vb64 = small::tobase64>(str.data(), str.size()); + std::string b64 = small::tobase64(str); + auto vb64 = small::tobase64>(str.data(), str.size()); std::cout << "base64(\"" << str << "\") is " << b64 << "\n"; std::cout << "base64 as vector(\"" << str << "\") is "; @@ -27,8 +26,8 @@ namespace examples::base64 { } std::cout << "\n"; - std::string decoded = small::frombase64(vb64); - auto decodedvd64 = small::frombase64>(b64); + std::string decoded = small::frombase64(vb64); + auto decodedvd64 = small::frombase64>(b64); std::cout << "decoded base64 is \"" << decoded << "\"\n"; diff --git a/examples/examples_buffer.h b/examples/examples_buffer.h index 53b0f2a..fd174ec 100644 --- a/examples/examples_buffer.h +++ b/examples/examples_buffer.h @@ -3,7 +3,6 @@ #include #include #include -#include #include "../include/buffer.h" diff --git a/examples/examples_event.h b/examples/examples_event.h index 3a30341..6ca13e4 100644 --- a/examples/examples_event.h +++ b/examples/examples_event.h @@ -2,7 +2,6 @@ #include #include #include -#include #include "../include/event.h" #include "../include/util.h" @@ -34,7 +33,7 @@ namespace examples::event { }; // create thread - const int iterations = 3; + const int iterations = 3; std::thread t[3]; for (size_t i = 0; i < sizeof(t) / sizeof(t[0]); ++i) { t[i] = std::thread(fn_t, i, iterations, std::ref(e)); diff --git a/examples/examples_hash.h b/examples/examples_hash.h index eb8d1f4..3588bce 100644 --- a/examples/examples_hash.h +++ b/examples/examples_hash.h @@ -3,7 +3,6 @@ #include #include #include -#include #include "../include/hash.h" diff --git a/examples/examples_jobs_engine.h b/examples/examples_jobs_engine.h index 602dc86..0872cd6 100644 --- a/examples/examples_jobs_engine.h +++ b/examples/examples_jobs_engine.h @@ -5,7 +5,6 @@ #include #include #include -#include #include "../include/jobs_engine.h" diff --git a/examples/examples_lock_queue.h b/examples/examples_lock_queue.h index 5306bfd..26ba3f6 100644 --- a/examples/examples_lock_queue.h +++ b/examples/examples_lock_queue.h @@ -2,7 +2,6 @@ #include #include #include -#include #include "../include/lock_queue.h" @@ -40,12 +39,12 @@ namespace examples::lock_queue { ret = q.wait_pop_front(&e); std::cout << "ret=" << static_cast(ret) << ", pop " << e.first << "," << e.second << std::endl; - e = {}; + e = {}; ret = q.wait_pop_front(&e); std::cout << "ret=" << static_cast(ret) << ", pop " << e.first << "," << e.second << std::endl; // force exit signaled - e = {}; + e = {}; ret = q.wait_pop_front(&e); std::cout << "ret=" << static_cast(ret) << "\n"; diff --git a/examples/examples_prio_queue.h b/examples/examples_prio_queue.h index 2ae92dd..44b6861 100644 --- a/examples/examples_prio_queue.h +++ b/examples/examples_prio_queue.h @@ -2,7 +2,6 @@ #include #include #include -#include #include "../include/prio_queue.h" diff --git a/examples/examples_spinlock.h b/examples/examples_spinlock.h index ffb4719..ec05a0a 100644 --- a/examples/examples_spinlock.h +++ b/examples/examples_spinlock.h @@ -3,7 +3,6 @@ #include #include #include -#include #include "../include/spinlock.h" diff --git a/examples/examples_time_queue.h b/examples/examples_time_queue.h index c8ba0a2..47b2a1a 100644 --- a/examples/examples_time_queue.h +++ b/examples/examples_time_queue.h @@ -2,7 +2,6 @@ #include #include #include -#include #include "../include/time_queue.h" @@ -47,7 +46,7 @@ namespace examples::time_queue { std::cout << "ret=" << static_cast(ret) << " as timeout\n"; for (; ret != small::EnumLock::kExit;) { - ret = q.wait_pop(&e); + ret = q.wait_pop(&e); auto elapsed = small::timeDiffMs(timeStart); switch (ret) { case small::EnumLock::kElement: diff --git a/examples/examples_util.h b/examples/examples_util.h index c85761a..9b4112a 100644 --- a/examples/examples_util.h +++ b/examples/examples_util.h @@ -4,7 +4,6 @@ #include #include #include -#include #include "../include/util.h" @@ -22,8 +21,8 @@ namespace examples::util { m[std::string(b)] = 2; constexpr const char *a = "a"; - m[a] = 1; - m["A"] = 3; // this will have override the "a" + m[a] = 1; + m["A"] = 3; // this will have override the "a" std::cout << "current map values "; for (auto &[key, val] : m) { diff --git a/examples/examples_worker_threads.h b/examples/examples_worker_threads.h index f67e652..aa0df46 100644 --- a/examples/examples_worker_threads.h +++ b/examples/examples_worker_threads.h @@ -5,7 +5,6 @@ #include #include #include -#include #include "../include/worker_thread.h" diff --git a/include/base64.h b/include/base64.h index d452895..df887eb 100644 --- a/include/base64.h +++ b/include/base64.h @@ -22,7 +22,7 @@ namespace small { template inline T tobase64(const char *src, const std::size_t src_length) { - T base64; + T base64; std::size_t base64_size = base64impl::get_base64_size(src_length); base64.resize(base64_size); @@ -64,7 +64,7 @@ namespace small { template inline T frombase64(const char *base64, const std::size_t base64_length) { - T decoded; + T decoded; std::size_t decoded_size = base64impl::get_decodedbase64_size(base64_length); decoded.resize(decoded_size); diff --git a/include/buffer.h b/include/buffer.h index 43f7b0c..1062c3a 100644 --- a/include/buffer.h +++ b/include/buffer.h @@ -167,9 +167,9 @@ namespace small { { if (this != &o) { clear_buffer(); - m_config = o.m_config; - m_chunk_buffer_data = o.m_chunk_buffer_data; - m_chunk_buffer_length = o.m_chunk_buffer_length; + m_config = o.m_config; + m_chunk_buffer_data = o.m_chunk_buffer_data; + m_chunk_buffer_length = o.m_chunk_buffer_length; m_chunk_buffer_alloc_size = o.m_chunk_buffer_alloc_size; setup_buffer(m_chunk_buffer_data, m_chunk_buffer_length); o.init(); @@ -189,9 +189,9 @@ namespace small { } else if (m_chunk_buffer_data == get_empty_buffer() && o.m_chunk_buffer_data == o.get_empty_buffer()) { /*do nothing*/ } else if (m_chunk_buffer_data != get_empty_buffer() && o.m_chunk_buffer_data == o.get_empty_buffer()) { o.m_chunk_buffer_data = m_chunk_buffer_data; - m_chunk_buffer_data = (char *)(get_empty_buffer()); + m_chunk_buffer_data = (char *)(get_empty_buffer()); } else if (m_chunk_buffer_data == get_empty_buffer() && o.m_chunk_buffer_data != o.get_empty_buffer()) { - m_chunk_buffer_data = o.m_chunk_buffer_data; + m_chunk_buffer_data = o.m_chunk_buffer_data; o.m_chunk_buffer_data = (char *)o.get_empty_buffer(); } setup_buffer(m_chunk_buffer_data, m_chunk_buffer_length); @@ -202,9 +202,9 @@ namespace small { // init inline void init() { - m_config.chunk_size = std::max(m_config.chunk_size, std::size_t(1)); - m_chunk_buffer_data = (char *)get_empty_buffer(); - m_chunk_buffer_length = 0; + m_config.chunk_size = std::max(m_config.chunk_size, std::size_t(1)); + m_chunk_buffer_data = (char *)get_empty_buffer(); + m_chunk_buffer_length = 0; m_chunk_buffer_alloc_size = 0; setup_buffer(m_chunk_buffer_data, m_chunk_buffer_length); } @@ -212,7 +212,7 @@ namespace small { // free_chunk_buffer inline void free_chunk_buffer() { - m_chunk_buffer_length = 0; + m_chunk_buffer_length = 0; m_chunk_buffer_alloc_size = 0; if (m_chunk_buffer_data && (m_chunk_buffer_data != get_empty_buffer())) { buffer::free(m_chunk_buffer_data); @@ -226,7 +226,7 @@ namespace small { const auto chunk_size = m_config.chunk_size; // we always append a '\0' to the end so we can use as string std::size_t new_alloc_size = ((new_size + sizeof(char) /*for '\0'*/ + (chunk_size - 1)) / chunk_size) * chunk_size; - bool reallocate = false; + bool reallocate = false; if (shrink) { reallocate = (m_chunk_buffer_alloc_size == 0) || (new_alloc_size != m_chunk_buffer_alloc_size); // we need another size } else { @@ -235,7 +235,7 @@ namespace small { // (re)allocate if (reallocate) { - m_chunk_buffer_data = (m_chunk_buffer_alloc_size == 0) ? (char *)malloc(new_alloc_size) : (char *)realloc(m_chunk_buffer_data, new_alloc_size); + m_chunk_buffer_data = (m_chunk_buffer_alloc_size == 0) ? (char *)malloc(new_alloc_size) : (char *)realloc(m_chunk_buffer_data, new_alloc_size); m_chunk_buffer_alloc_size = new_alloc_size; } @@ -252,7 +252,7 @@ namespace small { // !! override functions void clear_impl() override { - m_chunk_buffer_length = 0; + m_chunk_buffer_length = 0; m_chunk_buffer_data[0] = '\0'; setup_buffer(m_chunk_buffer_data, m_chunk_buffer_length); } @@ -298,7 +298,7 @@ namespace small { // chunk size config_buffer m_config{}; // buffer use char* instead of vector because it is much faster - char *m_chunk_buffer_data{}; + char *m_chunk_buffer_data{}; std::size_t m_chunk_buffer_length{}; std::size_t m_chunk_buffer_alloc_size{}; }; diff --git a/include/critical_section.h b/include/critical_section.h index 4e5b6ab..1cb1b16 100644 --- a/include/critical_section.h +++ b/include/critical_section.h @@ -2,7 +2,7 @@ #include "spinlock.h" -// +// // small::critical_section lock; // ... // { @@ -11,8 +11,7 @@ // // do your work // ... // } -// -namespace small -{ +// +namespace small { using critical_section = spinlock; } \ No newline at end of file diff --git a/include/impl/base_buffer_impl.h b/include/impl/base_buffer_impl.h index d791aa3..50f2e80 100644 --- a/include/impl/base_buffer_impl.h +++ b/include/impl/base_buffer_impl.h @@ -17,10 +17,10 @@ namespace small { { protected: // base_buffer (allocates in chunks) - base_buffer() = default; + base_buffer() = default; base_buffer(const base_buffer &) = delete; - base_buffer(base_buffer &&) = delete; - virtual ~base_buffer() = default; + base_buffer(base_buffer &&) = delete; + virtual ~base_buffer() = default; public: // clang-format off @@ -154,7 +154,7 @@ namespace small { inline int compare(const char *s, std::size_t s_length) const { const std::size_t this_size = size(); - int cmp = memcmp(data(), s, std::min(this_size, s_length)); + int cmp = memcmp(data(), s, std::min(this_size, s_length)); if (cmp != 0) { // different @@ -297,7 +297,7 @@ namespace small { // !! after every function call setup buffer data inline void setup_buffer(char *buffer_data, std::size_t buffer_length) { - m_buffer_data = buffer_data; + m_buffer_data = buffer_data; m_buffer_length = buffer_length; } @@ -453,7 +453,7 @@ namespace small { // base_buffer empty char m_empty_buffer[1]{'\0'}; // base_buffer use char* instead of vector because it is much faster - char *m_buffer_data{nullptr}; + char *m_buffer_data{nullptr}; std::size_t m_buffer_length{0}; }; diff --git a/include/util_rand.h b/include/util_rand.h index 24f4a72..4eae5b5 100644 --- a/include/util_rand.h +++ b/include/util_rand.h @@ -9,31 +9,31 @@ namespace small { // inline unsigned char rand8() { - std::random_device rd; // a seed source for the random number engine - std::mt19937 gen(rd()); // mersenne_twister_engine seeded with rd() + std::random_device rd; // a seed source for the random number engine + std::mt19937 gen(rd()); // mersenne_twister_engine seeded with rd() std::uniform_int_distribution<> dis(0, 255); return static_cast(dis(gen)); } inline unsigned short int rand16() { - std::random_device rd; // a seed source for the random number engine - std::mt19937 gen(rd()); // mersenne_twister_engine seeded with rd() + std::random_device rd; // a seed source for the random number engine + std::mt19937 gen(rd()); // mersenne_twister_engine seeded with rd() std::uniform_int_distribution<> dis(0, 65535); return static_cast(dis(gen)); } inline unsigned long rand32() { - std::random_device rd; // a seed source for the random number engine - std::mt19937 gen(rd()); // mersenne_twister_engine seeded with rd() + std::random_device rd; // a seed source for the random number engine + std::mt19937 gen(rd()); // mersenne_twister_engine seeded with rd() return gen(); } inline unsigned long long rand64() { - std::random_device rd; // a seed source for the random number engine - std::mt19937_64 gen(rd()); // mersenne_twister_engine seeded with rd() + std::random_device rd; // a seed source for the random number engine + std::mt19937_64 gen(rd()); // mersenne_twister_engine seeded with rd() return gen(); } diff --git a/include/util_time.h b/include/util_time.h index b6e91ae..ef66f00 100644 --- a/include/util_time.h +++ b/include/util_time.h @@ -52,10 +52,10 @@ namespace small { // inline std::string toISOString(std::chrono::system_clock::time_point time = std::chrono::system_clock::now()) { - auto tt = std::chrono::system_clock::to_time_t(time); + auto tt = std::chrono::system_clock::to_time_t(time); std::tm tt_tm; // = *std::gmtime(&tt) is not thread safe -#ifdef _WIN32 - gmtime_s(&&tt_tm, &tt); +#ifdef _WIN32 || _WIN64 + gmtime_s(&tt_tm, &tt); #else gmtime_r(&tt, &tt_tm); #endif diff --git a/main.cpp b/main.cpp index a0c78d7..f0555e9 100644 --- a/main.cpp +++ b/main.cpp @@ -3,7 +3,6 @@ #include #include #include -#include #include "examples/examples_base64.h" #include "examples/examples_buffer.h" @@ -21,19 +20,19 @@ int main() { std::cout << "Hello. Here are the default small cpp utils examples\n\n"; - examples::spinlock::Example1(); - examples::hash::Example1(); + // examples::spinlock::Example1(); + // examples::hash::Example1(); examples::base64::Example1(); - examples::util::Example1(); - examples::buffer::Example1(); - examples::event::Example1(); - examples::lock_queue::Example1(); - examples::time_queue::Example1(); - examples::prio_queue::Example1(); + // examples::util::Example1(); + // examples::buffer::Example1(); + // examples::event::Example1(); + // examples::lock_queue::Example1(); + // examples::time_queue::Example1(); + // examples::prio_queue::Example1(); examples::worker_thread::Example1(); examples::worker_thread::Example2(); - examples::worker_thread::Example3_Perf(); + // examples::worker_thread::Example3_Perf(); examples::jobs_engine::Example1(); examples::jobs_engine::Example2(); From 9ff2d3ce17d3bf8ff37967f947de0f5cb51d4c0e Mon Sep 17 00:00:00 2001 From: Cristian Herghelegiu Date: Sun, 22 Dec 2024 02:08:16 +0200 Subject: [PATCH 2/3] Reduce Linux dependencies --- main.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/main.cpp b/main.cpp index f0555e9..fc6c7cc 100644 --- a/main.cpp +++ b/main.cpp @@ -20,19 +20,19 @@ int main() { std::cout << "Hello. Here are the default small cpp utils examples\n\n"; - // examples::spinlock::Example1(); - // examples::hash::Example1(); + examples::spinlock::Example1(); + examples::hash::Example1(); examples::base64::Example1(); - // examples::util::Example1(); - // examples::buffer::Example1(); - // examples::event::Example1(); - // examples::lock_queue::Example1(); - // examples::time_queue::Example1(); - // examples::prio_queue::Example1(); + examples::util::Example1(); + examples::buffer::Example1(); + examples::event::Example1(); + examples::lock_queue::Example1(); + examples::time_queue::Example1(); + examples::prio_queue::Example1(); examples::worker_thread::Example1(); examples::worker_thread::Example2(); - // examples::worker_thread::Example3_Perf(); + examples::worker_thread::Example3_Perf(); examples::jobs_engine::Example1(); examples::jobs_engine::Example2(); From 29c8ab6812241e32d74f67fedf9e11cbf6ade9a5 Mon Sep 17 00:00:00 2001 From: Cristian Herghelegiu Date: Sun, 22 Dec 2024 02:15:23 +0200 Subject: [PATCH 3/3] Reduce Linux dependencies --- .github/workflows/sonarqube.yml | 66 --------------------------------- 1 file changed, 66 deletions(-) delete mode 100644 .github/workflows/sonarqube.yml diff --git a/.github/workflows/sonarqube.yml b/.github/workflows/sonarqube.yml deleted file mode 100644 index 12f6698..0000000 --- a/.github/workflows/sonarqube.yml +++ /dev/null @@ -1,66 +0,0 @@ -# This workflow uses actions that are not certified by GitHub. -# They are provided by a third-party and are governed by -# separate terms of service, privacy policy, and support -# documentation. - -# This workflow helps you trigger a SonarQube analysis of your code and populates -# GitHub Code Scanning alerts with the vulnerabilities found. -# (this feature is available starting from SonarQube 9.7, Developer Edition and above) - -# 1. Make sure you add a valid GitHub configuration to your SonarQube (Administration > DevOps platforms > GitHub) - -# 2. Import your project on SonarQube -# * Add your repository as a new project by clicking "Create project" from your homepage. -# -# 3. Select GitHub Actions as your CI and follow the tutorial -# * a. Generate a new token and add it to your GitHub repository's secrets using the name SONAR_TOKEN -# (On SonarQube, click on your avatar on top-right > My account > Security or ask your administrator) -# -# * b. Copy/paste your SonarQube host URL to your GitHub repository's secrets using the name SONAR_HOST_URL -# -# * c. Copy/paste the project Key into the args parameter below -# (You'll find this information in SonarQube by following the tutorial or by clicking on Project Information at the top-right of your project's homepage) - -# Feel free to take a look at our documentation (https://docs.sonarqube.org/latest/analysis/github-integration/) -# or reach out to our community forum if you need some help (https://community.sonarsource.com/c/sq/10) - -name: SonarQube analysis - -on: - push: - branches: [ "main", "feature/*" ] - pull_request: - branches: [ "main" ] - workflow_dispatch: - -permissions: - pull-requests: read # allows SonarQube to decorate PRs with analysis results - -jobs: - Analysis: - runs-on: ubuntu-latest - - steps: - - name: Analyze with SonarQube - - # You can pin the exact commit or the version. - # uses: SonarSource/sonarqube-scan-action@v1.1.0 - uses: SonarSource/sonarqube-scan-action@7295e71c9583053f5bf40e9d4068a0c974603ec8 - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information - SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} # Generate a token on SonarQube, add it to the secrets of this repo with the name SONAR_TOKEN (Settings > Secrets > Actions > add new repository secret) - SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }} # add the URL of your instance to the secrets of this repo with the name SONAR_HOST_URL (Settings > Secrets > Actions > add new repository secret) - with: - # Additional arguments for the sonarcloud scanner - args: - # Unique key of your project. You can find it in SonarQube > [my project] > Project Information (top-right menu) - # mandatory - -Dsonar.projectKey= - # Comma-separated paths to directories containing main source files. - #-Dsonar.sources= # optional, default is project base directory - # When you need the analysis to take place in a directory other than the one from which it was launched - #-Dsonar.projectBaseDir= # optional, default is . - # Comma-separated paths to directories containing test source files. - #-Dsonar.tests= # optional. For more info about Code Coverage, please refer to https://docs.sonarcloud.io/enriching/test-coverage/overview/ - # Adds more detail to both client and server-side analysis logs, activating DEBUG mode for the scanner, and adding client-side environment variables and system properties to the server-side log of analysis report processing. - #-Dsonar.verbose= # optional, default is false