Skip to content

Instantly share code, notes, and snippets.

@mmozeiko
mmozeiko / FlushFileCache.c
Last active March 14, 2024 00:14
flush file cache on Windows
// clang.exe -Os -nostdlib -fuse-ld=lld -Wl,-fixed,-subsystem:console FlushFileCache.c -o FlushFileCache.exe
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#pragma comment (lib, "kernel32.lib")
#pragma comment (lib, "advapi32.lib")
#define error(str) do { DWORD written; WriteFile(GetStdHandle(STD_ERROR_HANDLE), str, sizeof(str)-1, &written, NULL); } while (0)
@d7samurai
d7samurai / .readme.md
Last active March 27, 2024 17:50
Minimal D3D11 pt2

Minimal D3D11 pt2

Follow-up to Minimal D3D11, adding instanced rendering. As before: An uncluttered Direct3D 11 setup & basic rendering primer / API familiarizer. Complete, runnable Windows application contained in a single function and laid out in a linear, step-by-step fashion. No modern C++ / OOP / obscuring cruft.

The main difference here is that the hollow cube is rendered using DrawIndexedInstanced (which saves a lot of vertices compared to the original, so model data is now small enough to be included in the source without being too much in the way), but also all trigonometry and matrix math is moved to the vertex shader, further simplifying the main code.

Each instance is merely this piece of geometry, consisting of 4 triangles:

instanced1

..which is th

@pervognsen
pervognsen / shift_dfa.md
Last active January 27, 2024 19:54
Shift-based DFAs

A traditional table-based DFA implementation looks like this:

uint8_t table[NUM_STATES][256]

uint8_t run(const uint8_t *start, const uint8_t *end, uint8_t state) {
    for (const uint8_t *s = start; s != end; s++)
        state = table[state][*s];
    return state;
}
@mmozeiko
mmozeiko / win32_d3d11.c
Last active April 14, 2024 21:46
setting up and using D3D11 in C
// example how to set up D3D11 rendering on Windows in C
#define COBJMACROS
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <d3d11.h>
#include <dxgi1_3.h>
#include <d3dcompiler.h>
#include <dxgidebug.h>
@takaswie
takaswie / pcm-rw-interleaved.c
Last active December 3, 2023 15:49
ALSA PCM application to utilize intermediate buffer inner kernel space
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <stdint.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
@vegard
vegard / kernel-dev.md
Last active May 3, 2024 04:55
Getting started with Linux kernel development

Getting started with Linux kernel development

Prerequisites

The Linux kernel is written in C, so you should have at least a basic understanding of C before diving into kernel work. You don't need expert level C knowledge, since you can always pick some things up underway, but it certainly helps to know the language and to have written some userspace C programs already.

It will also help to be a Linux user. If you have never used Linux before, it's probably a good idea to download a distro and get comfortable with it before you start doing kernel work.

Lastly, knowing git is not actually required, but can really help you (since you can dig through changelogs and search for information you'll need). At a minimum you should probably be able to clone the git repository to a local directory.

Multi-dimensional array views for systems programmers

As C programmers, most of us think of pointer arithmetic for multi-dimensional arrays in a nested way:

The address for a 1-dimensional array is base + x. The address for a 2-dimensional array is base + x + y*x_size for row-major layout and base + y + x*y_size for column-major layout. The address for a 3-dimensional array is base + x + (y + z*y_size)*x_size for row-column-major layout. And so on.

/* ===========================================================================
*
* LIBRARY
*
* =========================================================================== */
/* Proof of Concept GUI:
* - PoC UI implementation in ~2.5kLOC of C89 (ANSI C)
* => Core solutions has no external dependencies (removing standard library dependency is trival)
* => Does not use or require any special language constructs or macro constructs just pointers and enums
* - Combines both "retained" and "immediate mode" UI by providing control over update frequency
@nicowilliams
nicowilliams / fork-is-evil-vfork-is-good-afork-would-be-better.md
Last active November 5, 2023 12:14
fork() is evil; vfork() is goodness; afork() would be better; clone() is stupid

I recently happened upon a very interesting implementation of popen() (different API, same idea) called popen-noshell using clone(2), and so I opened an issue requesting use of vfork(2) or posix_spawn() for portability. It turns out that on Linux there's an important advantage to using clone(2). I think I should capture the things I wrote there in a better place. A gist, a blog, whatever.

This is not a paper. I assume reader familiarity with fork() in particular and Unix in general, though, of course, I link to relevant wiki pages, so if the unfamiliar reader is willing to go down the rabbit hole, they should be able to come ou

// Despite appearances to the contrary, this does proper error handling. Any error cascades into a failure of the wglGetProcAddress.
int init_opengl() {
HWND window = CreateWindowA("edit", "", WS_OVERLAPPEDWINDOW, 0, 0, 0, 0, NULL, NULL, NULL, NULL);
PIXELFORMATDESCRIPTOR format_desc = {
.nSize = sizeof(PIXELFORMATDESCRIPTOR),
.nVersion = 1,
.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL
};