Skip to content

Instantly share code, notes, and snippets.

View GavinRay97's full-sized avatar

Gavin Ray GavinRay97

View GitHub Profile
@GavinRay97
GavinRay97 / README.md
Created January 6, 2023 18:33
Modern C++ env setup WIP

Setting up a Modern C++20/23 Environment

This document describes configuring a development environment for modern C++. Some of the flags and warnings in this document are not common knowledge, or found in the documentation of the respective toolchain.

Compiler

Below is the compiler configuration we'll use

Common Flags

@GavinRay97
GavinRay97 / plugin.cpp
Created December 31, 2022 01:44
C++ Design-by-Contract [[invariant]] GCC plugin (working)
#include <iostream>
// clang-format off
#include <gcc-plugin.h>
#include <context.h>
#include <plugin-version.h>
#include <tree.h>
#include <gimple.h>
#include <tree-pass.h>
#include <gimple-iterator.h>
@GavinRay97
GavinRay97 / invariant.cpp
Last active December 30, 2022 17:28
GCC [[invariant]] plugin for Design by Contract (WIP)
#include <iostream>
// clang-format off
#include <gcc-plugin.h>
#include <context.h>
#include <plugin-version.h>
#include <tree.h>
#include <gimple.h>
#include <tree-pass.h>
#include <gimple-iterator.h>
@GavinRay97
GavinRay97 / postgres-wire-protocol-state-chart.dot
Created December 24, 2022 21:32
Postgres wire protcol -- Graphviz state chart
digraph state_machine {
rankdir=LR;
size="8,5"
node [shape = doublecircle, style=filled, fillcolor=lightblue]; WaitingForQuery;
node [shape = doublecircle, style=filled, fillcolor=maroon] GotErrorResponseDuringSimpleQuery;
node [shape = circle, style=filled, fillcolor=lightgrey];
InitializingBackend -> GotCommandComplete [ label = "PG3ReadyForQuery", color=green ];
InitializingBackend -> InitializingBackend [ label = "PG3BackendKeyData", color=orange ];
GotDataRow -> GotErrorResponseDuringSimpleQuery [ label = "PG3ErrorResponse", color=red ];
GotDataRow -> GotCommandComplete [ label = "PG3CommandComplete", color=green ];
@GavinRay97
GavinRay97 / postgres-wire-protocol-types.json
Last active December 23, 2022 21:37
Postgres wire-protocol types as a JSON document, for whatever you like
{
"AuthenticationOk": {
"type": "backend",
"fields": [
{
"name": "Byte1('R')",
"type": "Byte1",
"description": "Identifies the message as an authentication request.",
"value": "'R'"
},
@GavinRay97
GavinRay97 / main.ts
Created December 23, 2022 21:20
Postgres wire protocol extraction from DOM on docs page
// Regex for lines like "Terminate (F)"
// Where the first group is the name of the message and the second is the kind (Backend/Frontend)
const pgMessageTypeRegex = /^(?<command>\w+) \((?<kind>\w)\)$/gm
// Regex for the type of a variable, like:
// - Int16, Int32, Byten, Byte4, String
// - Byte1('S')
// - Int32(4)
// - Int16[N]
// Where:
@GavinRay97
GavinRay97 / main.ts
Created December 23, 2022 20:22
Postgres wire protocol doc translator + code-generator platform (dumps wire protocol to structured JSON, allows easy codegen of types)
import fs from "node:fs"
const postgresWireProtocolDocs = `
AuthenticationOk (B)
Byte1('R')
Identifies the message as an authentication request.
Int32(8)
Length of message contents in bytes, including self.
@GavinRay97
GavinRay97 / pgtypes.java
Created December 23, 2022 20:20
Autogenerated PG Wire Protocol code example
// Generated by blah blah blah
public class PostgresTypes {
public static class Backend {
public static class AuthenticationOk {
/** Identifies the message as an authentication request. */
byte field1_Byte1 = 'R';
/** Length of message contents in bytes, including self. */
int field2_Int32 = 8;
@GavinRay97
GavinRay97 / main.cpp
Last active January 13, 2024 01:45
[C++] Atomic Tagged Pointer (uint16 version counter + 3-bit tag/storage)
#include <atomic>
#include <cassert>
#include <cstdint>
#include <cstdio>
#include <optional>
// A word-aligned, atomic tagged pointer.
// Uses both the upper 16 bits for storage, and the lower 3 bits for tagging.
//
// 64 48 32 16
@GavinRay97
GavinRay97 / install.sh-session
Created December 12, 2022 21:07
Haskell: Install GHC from Nightly CI bindists with ghcup
# Info from: https://discourse.haskell.org/t/pre-hftp-ghc-nightly-releases/5094/5
$ URL='https://gitlab.haskell.org/ghc/ghc/-/jobs/artifacts/master/raw/ghc-x86_64-linux-fedora33-release.tar.xz?job=x86_64-linux-fedora33-release'
$ ghcup install ghc -u $URL head