Skip to content

Instantly share code, notes, and snippets.

View eatonphil's full-sized avatar

Phil Eaton eatonphil

View GitHub Profile
@eatonphil
eatonphil / thread.ml
Last active July 12, 2026 02:54
Basic thread logic based on condition variables in Poly/ML
(*
* Tested in Poly/ML. To compile: `polyc thread.ml && ./a.out`
*
* Heavily influenced by: http://pages.cs.wisc.edu/~remzi/OSTEP/threads-cv.pdf
* Poly/ML Thread documentation here: http://www.polyml.org/documentation/Reference/Threads.html
* Poly/ML Thread implementation here: https://github.com/polyml/polyml/blob/master/basis/Thread.sml
*)
val done = ref false;
val m = Thread.Mutex.mutex();
@eatonphil
eatonphil / functions.c
Last active May 26, 2026 15:34
Introduction to "Fun" C (using GCC)
/**
* This are a collection of examples for C 201.
* These combine concepts you may or may not be
* familiar with and are especially useful for
* students new to C. There is a lot of really
* cool stuff you can do in C without any cool
* languages.
*
* This is file in particular is an introduction
* to fun function usage in C.
- stock: EBAY
url: https://www.ebay.com/
founded: 1995
country: USA
twitter: https://x.com/ebay
linkedin: https://www.linkedin.com/company/ebay/
tagline: Find what you ❤️ Sell what you don’t 👋.
headquarters: San Jose
organization: eBay
rounds:
@eatonphil
eatonphil / psql-srv.py
Last active March 30, 2026 07:39 — forked from matteobertozzi/psql-srv.py
postgres "server" wire protocol example (ported python3)
# th30z@u1310:[Desktop]$ psql -h localhost -p 55432
# Password:
# psql (9.1.10, server 0.0.0)
# WARNING: psql version 9.1, server version 0.0.
# Some psql features might not work.
# Type "help" for help.
#
# th30z=> select foo;
# a | b
# ---+---
@eatonphil
eatonphil / bst.rs
Created November 27, 2023 17:27
Binary Search Tree in Rust
struct BST<K: Ord + Clone, V: Ord + Clone> {
key: Option<K>,
value: Option<V>,
left: Option<Box<BST<K, V>>>,
right: Option<Box<BST<K, V>>>,
}
impl<K: Ord + Clone, V: Ord + Clone> BST<K, V> {
fn init() -> Box<BST<K, V>> {
return Box::new(BST::<K, V> {
@eatonphil
eatonphil / Minimal-MariaDB-UDF-In-C.md
Last active October 19, 2024 12:13
Minimal UDF for MariaDB in C

Assume that you've got MariaDB built and running locally. Let's say the install directory is in $MARIADB_LOCAL_INSTALL.

Enter the following into udf_minimal.c:

#include "mariadb.h"

my_bool mysum_init(UDF_INIT* initid, UDF_ARGS* args, char* message);
long long mysum(UDF_INIT* initid, UDF_ARGS* args, char* result,
              unsigned long* length, char* is_null, char* error);
@eatonphil
eatonphil / getreq.sml
Created February 27, 2016 22:59
Example of HTTP GET request in SML
(*
* Tested on Poly/ML.
* Usage:
* $ polyc getreq.sml
* $ ./a.out
*)
exception E of string
fun request domain path =
@eatonphil
eatonphil / Returning-Data-From-rnd_next().md
Last active June 11, 2024 07:09
Returning data from rnd_next() in a custom storage engine.

rnd_next(uchar* buf) for a storage engine sets up the current row of data. If there is data, it returns a 0. Otherwise it returns a HA_ERR_END_OF_FILE or other error.

Row data is written into the uchar* buf field. You might imagine you would return structured data for a row and the API would be like rnd_next(Fields**, field_count int). But that's not how the API works. You write the fields as bytes directly into uchar* buf.

The first X bytes are a NULL bitmap. One bit for each NULL-able column, padded out to a full byte. After that is each value for each field. For a table with two INT columns (they'd both be NULL-able), you'd write 9 bytes to uchar* buf. The first byte would be a NULL bitmap of all zeroes if both values in the row were not NULL. The next 4 bytes would be the first column's value. The last 4 bytes would be the second column's value.

Here's a diff for the BLACKHOLE engine, hardcoding a single row response: https://github.com/MariaDB/server/compare/11.4...eatonphil:mariadb:e0

@eatonphil
eatonphil / btree.py
Created August 27, 2023 16:47
Python In-memory B-Tree
import math
import uuid
class BTree:
def __init__(self, order=3):
self.root = BTreeNode(order)
def insert(self, toinsert):
all_elements = self.list()
@eatonphil
eatonphil / server.ml
Last active April 20, 2024 10:42
A simple threaded web server in SML
(*
* Tested on PolyML. `polyc server.ml && ./a.out`
*)
fun println s =
(print (s ^ "\n"); TextIO.flushOut TextIO.stdOut)
fun readAll conn req =
let val ntoread = Socket.Ctl.getNREAD conn in
if ntoread > 0