Skip to content

Instantly share code, notes, and snippets.

View saxbophone's full-sized avatar
🏳️‍⚧️

saxbophone

🏳️‍⚧️
View GitHub Profile
@saxbophone
saxbophone / seizable-reloaded.c++
Last active March 6, 2025 23:43
Reloaded version of my Seizable™ concept
#include <mutex> // locks
#include <shared_mutex> // shared_mutex
#include <utility> // exposing structured-binding support for SynchroniseAll
template <typename T>
class Synchronised {
public:
using Mutex = std::shared_mutex;
using ReadOnlyLock = std::shared_lock<Mutex>;
@saxbophone
saxbophone / fuse-shim.c++
Last active January 11, 2025 04:28
A shim using a healthy dose of C++20 concepts and preprocessor macros to allow creating a FUSE filesystem driver using public static class methods
#include <cstddef>
// #include <cstdint>
#include <concepts>
#include <type_traits>
#define FUSE_USE_VERSION 29
#define _FILE_OFFSET_BITS 64
#include <fuse.h>
@saxbophone
saxbophone / manual_flow_constructs.c++
Created July 5, 2024 22:20
Manually recreating flow-control constructs using lambdas and goto
template <typename Predicate, typename Body>
constexpr void While(Predicate pred, Body body) {
goto predicate;
loop:
body();
predicate:
if (pred()) { goto loop; }
}
template <typename Body, typename Predicate>
@saxbophone
saxbophone / placement_new.c++
Created June 30, 2024 20:21
Placement new fun! Better make sure you align them correctly! 😈 (for a more thorough design, might need a bookkeeping array so we can reällocate)
#include <array>
#include <cstdint>
#include <iostream>
#include <string>
std::byte storage[10'000];
int main() {
uint32_t* x = new(&storage[0]) uint32_t{1234};
@saxbophone
saxbophone / packed_utf32.cpp
Last active March 17, 2024 02:13
Taking advantage of the fact that Unicode only uses 21 bits to pack 3 UTF-32 code units into one 64-bit word (63 bits with one wasted/spare).
#include <string>
#pragma pack(push, 1)
struct Chunk {
char32_t first : 21;
char32_t second : 21;
char32_t third : 21;
operator std::u32string() const {
return {first, second, third};
}
@saxbophone
saxbophone / build-clang.sh
Last active February 24, 2024 11:39
my clang build command-line
time CC=gcc-10.2 CXX=g++-10.2 cmake -S ../llvm-project/llvm -B build -DCMAKE_BUILD_TYPE=Release
-DLLVM_PARALLEL_COMPILE_JOBS=4 -DLLVM_PARALLEL_LINK_JOBS=1 -DLLVM_ENABLE_RUNTIMES="" -DLLVM_ENABLE_PROJECTS=clang
-DLLVM_CCACHE_BUILD=ON -DLLVM_ENABLE_EH=ON -DLLVM_ENABLE_LTO=On -DLLVM_ENABLE_RTTI=ON -DLLVM_INCLUDE_TESTS=OFF
-DLLVM_LINK_LLVM_DYLIB=ON -DCMAKE_C_FLAGS_RELEASE="-O3 -march=native" -DCMAKE_CXX_FLAGS_RELEASE="-O3 -march=native"
-DLLVM_ENABLE_LIBXML2=OFF -DCMAKE_CXX_STANDARD=20
@saxbophone
saxbophone / autotools-to-cmake-header.c++
Created February 19, 2024 18:36
For transforming autoheader defines into CMake configure_file() defines
#include <iostream>
#include <regex>
#include <string>
int main() {
std::regex find_undefs{"(?:#undef\\s+)([_A-z][_0-9A-z]*)"};
std::cout << std::regex_replace("#undef _1FOO_BAR_BAZ1234_", find_undefs, "#cmakedefine $1 1") << std::endl;
}
@saxbophone
saxbophone / seizable.c++
Last active February 14, 2024 21:25
Seizable --a thread-safe convenience wrapper around std::shared_mutex that allows read-write lock semantics via getters and setters, as well as the ability to "seize" the protected value in order to operate on it more efficiently using reference semantics
#include <mutex>
#include <shared_mutex>
#include <type_traits>
// TOOD: reïmplement all methods non-inline so the prototype is nice and readable
template <typename T>
class Seizable {
public:
// TODO: change to std::shared_timed_mutex and add additional time-based locking methods
@saxbophone
saxbophone / convert.sh
Created February 1, 2024 22:42
Converts git refs to base-91 for brevity
git ls-remote --tags --refs --sort='v:refname' https://github.com/llvm/llvm-project.git | while read -r line; do
sha=$(echo $line | awk '{print substr($1, 1, 8)}');
ref=$(echo $line | awk '{print $2}');
b91=$(echo $sha | xxd -r -p | base91);
echo "$sha = $b91 ($ref)";
done
@saxbophone
saxbophone / lotto.c++
Created October 7, 2023 14:20
C++ Lottery Machine implementation
#include <cstddef>
#include <numeric>
#include <random>
#include <stdexcept>
#include <vector>
class LotteryMachine {
public:
LotteryMachine(std::size_t number_of_balls)