Skip to content

Instantly share code, notes, and snippets.

View DavidEGrayson's full-sized avatar

David Grayson DavidEGrayson

View GitHub Profile
@DavidEGrayson
DavidEGrayson / generic_hash_lookups_in_c.md
Last active June 26, 2025 21:13
Limitations of _Generic in C make me need two different functions for hash lookups

I'm writing a general-purpose hash table library in C. Given a user-defined struct type named H whose first member is named key, I want the user to be able to use variables of type H* to represent an array of type H, with an associated hash table that allows quick lookups of elements in the array by the value of the key.

The key can be anything. Right now I'm supporting three types of keys:

  • arbitary data with a fixed byte size
  • a pointer to a null-terminated string
  • a size_t followed by a pointer to arbitary data with that size
@DavidEGrayson
DavidEGrayson / test_vsnprintf.c
Last active February 9, 2024 21:46
Quick test of vsnprintf to make sure it is standards-compliant
// This is a small C program that tests your version of vsnprintf to see if it is
// compliant with the C99 standard.
//
// The script gives successful results (compliant vsnprintf) on these environments:
// - MSYS2's MINGW64 environment (GCC 13.2.0)
//
// I also expect successful results on Linux, macOS and modern UCRT-based environments
// provided by Microsoft.
//
// From the MSDN documentation of vsnprintf:
@DavidEGrayson
DavidEGrayson / rp2040_msys2.md
Last active August 30, 2024 00:51
Building and debugging RP2 firmware in Windows with MSYS2 + GCC + GDB + OpenOCD + PicoProbe + SWD

THIS TUTORIAL IS UNDER CONSTRUCTION. I'm updating it to support Pico SDK 2.0.0 and the new RP2 chips!

This is an opinionated tutorial that shows you how to set up a development environment for the RP2 series of microcontrollers (RP2040, RP2350A, RP2350B, RP2354A, RP2354B) on Windows using only open source tools. You should start at the beginning of this tutorial, but you don't have to follow ALL of the instructions: you can stop after completing any level and leave with something that is useful.

Level 0: Set up MSYS2

Download and install [MSYS2], the best system for developing open source software on Windows.

Use "Edit environment variables for your account" in the Windows Control Panel to add an environment variable for your user named HOME with a value like C:\Users\david (but with david replaced by your actual Windows user name). Do NOT add an extra slash on the end. MSYS2 uses this variable to determine where your home directory is. If you don't set it, MSYS2 defaults to using a home

Submodule URL

There are three locations where git keeps track of submodule URL:

  1. .gitmodules - This is a regular file that is committed in the repository.
  2. .git/config
  3. .git/modules/somepath/config
These commands have an effect on the three URLs:

@DavidEGrayson
DavidEGrayson / advent.rb
Created December 22, 2022 07:00
Advent of code 2022 Day 22
map = ['']
instructions = []
File.foreach('input.txt') do |line|
md = line.match(/\d/)
if md
line.scan(/(\d+|R|L)/) do |m|
if 'LR'.include?(m[0])
instructions << m[0].to_sym
else
instructions << m[0].to_i
@DavidEGrayson
DavidEGrayson / advent.rb
Created December 21, 2022 06:36
Advent of code 2022 Day 20 Part 2
$input = {}
File.foreach('input.txt') do |line|
if md = line.match(/(\w+): (\d+)/)
$input[md[1].to_sym] = md[2].to_i
elsif md = line.match(/(\w+): (\w+) (.) (\w+)/)
$input[md[1].to_sym] = [md[3].to_sym, md[2].to_sym, md[4].to_sym]
else
$stderr.puts "Unrecognized line: #{line}"
exit 1
end
@DavidEGrayson
DavidEGrayson / advent.rb
Last active December 15, 2022 04:55
Advent of Code 2022 Day 14: Falling sand (using recursion)
$width = 1000
$height = 200
$world = (' ' * $width + "\n") * $height
def coords_in_bounds?(coords)
(0...$width).include?(coords[0]) && (0...$height).include?(coords[1])
end
def read(coords)
raise if !coords_in_bounds?(coords)
@DavidEGrayson
DavidEGrayson / vscode_config_from_gcc_command.rb
Created September 12, 2022 22:20
Convert GCC command in VSCode configuration
#!/usr/bin/ruby
# Takes a command line for GCC (or similar) on as a file or on its
# standard input, extracts the preprocessor macros defined and the
# include paths, and outputs them in a format suitable for
# pasting into c_cpp_properties.json so that VSCode can understand
# your C/C++ project better.
require 'json'
input = ARGF.read
@DavidEGrayson
DavidEGrayson / gcc-11-arm-darwin.patch
Created June 11, 2021 21:57
GCC 11 patch for ARM darwin
diff -ur gcc-11.1.0/gcc/config/host-darwin.c gcc-11.1.0-fixed/gcc/config/host-darwin.c
--- gcc-11.1.0/gcc/config/host-darwin.c 2021-04-27 03:00:13.000000000 -0700
+++ gcc-11.1.0-fixed/gcc/config/host-darwin.c 2021-06-11 14:49:13.754000000 -0700
@@ -22,6 +22,10 @@
#include "coretypes.h"
#include "diagnostic-core.h"
#include "config/host-darwin.h"
+#include "hosthooks.h"
+#include "hosthooks-def.h"
+
diff --git a/code/15_hello_triangle.cpp b/code/15_hello_triangle.cpp
index 532520e..31f2bd7 100644
--- a/code/15_hello_triangle.cpp
+++ b/code/15_hello_triangle.cpp
@@ -642,12 +642,15 @@ private:
void drawFrame() {
vkWaitForFences(device, 1, &inFlightFences[currentFrame], VK_TRUE, UINT64_MAX);
+ VkFence waited = inFlightFences[currentFrame];