Skip to content

Instantly share code, notes, and snippets.

View pmttavara's full-sized avatar

Phillip Trudeau pmttavara

View GitHub Profile
@pmttavara
pmttavara / hello_world.c
Created April 10, 2019 06:53
Simple C Win32 Hello World program without preprocessor directives or library imports. Build with `cl *.c`.
__pragma(comment(linker, "/entry:main"));
__pragma(comment(linker, "/subsystem:console"));
void *(*GetProcAddress)(void *module, const char *name);
void *(*LoadLibraryA)(const char *name);
void (*ExitProcess)(unsigned int return_code);
int (*MessageBoxA)(void *hWnd, const char *lpText, const char *lpCaption, unsigned int uType);
static inline void load_proc(void *lib, void *fn, char *name) {
*(void **)fn = GetProcAddress(lib, name);
@pmttavara
pmttavara / dead_simple_debug_allocator.c
Created March 31, 2019 07:33
Debug allocator that catches leaks, use-after-free, double-free, buffer overrun, invalid/mismatched free. Windows x64. Simple but slow (no page reuse).
// libs: Kernel32.lib User32.lib DbgHelp.lib msvcrt.lib (unless you have your own printf)
// includes: Windows.h (LEAN_AND_MEAN is ok) WinUser.h DbgHelp.h stdio.h
// call SymInitialize() at startup and debug_check_leaks at shutdown
#ifndef NDEBUG
static void print_stack_trace(void **stack, u64 stack_count, const char *indent) {
struct {
SYMBOL_INFO si;
@pmttavara
pmttavara / readme.txt
Last active December 7, 2018 10:27
eddems (PuzzleScript Script)
Play this game by pasting the script in http://www.puzzlescript.net/editor.html
@pmttavara
pmttavara / defer.hpp
Created March 25, 2018 07:27
The definitive defer implementation for C++. Syntax: defer { statements; };
#ifndef defer
struct defer_dummy {};
template <class F> struct deferrer { F f; ~deferrer() { f(); } };
template <class F> deferrer<F> operator*(defer_dummy, F f) { return {f}; }
#define DEFER_(LINE) zz_defer##LINE
#define DEFER(LINE) DEFER_(LINE)
#define defer auto DEFER(__LINE__) = defer_dummy{} *[&]()
#endif // defer
@pmttavara
pmttavara / good_assert.h
Last active May 18, 2023 12:33
Better assert() for Windows - public domain
#ifndef good_assert /* Better assert() for Windows - public domain */
#ifdef _WIN32
#ifdef __cplusplus
extern "C" {
#endif
#ifndef _WINDOWS_
__declspec(dllimport) int __stdcall ExitProcess(unsigned int a);
__declspec(dllimport) int __stdcall MessageBoxA(void *a, const char *b,
const char *c, unsigned int d);
#endif /* _WINDOWS_ */