Skip to content

Instantly share code, notes, and snippets.

View pmttavara's full-sized avatar

Phillip Trudeau pmttavara

View GitHub Profile
@pmttavara
pmttavara / console_belongs_to_self.c
Last active December 24, 2022 06:25
Check whether the console is owned by the current process in Win32
static bool terminal_belongs_to_self(void) {
DWORD pid = 0; GetWindowThreadProcessId(GetConsoleWindow(), &pid);
return pid == GetCurrentProcessId();
}
@pmttavara
pmttavara / _readme.md
Last active January 6, 2024 12:41
Spall auto-tracing for MSVC (_penter/_pexit) or Clang (__cyg_profile_func_enter/__cyg_profile_func_exit)
@pmttavara
pmttavara / tscqpc.h
Last active December 27, 2023 07:55
Obtain RDTSC frequency on Win32 and Linux
// SPDX-FileCopyrightText: © 2022 Phillip Trudeau-Tavara <pmttavara@protonmail.com>
// SPDX-License-Identifier: 0BSD
// https://hero.handmade.network/forums/code-discussion/t/7485-queryperformancefrequency_returning_10mhz_bug/2
// https://learn.microsoft.com/en-us/virtualization/hyper-v-on-windows/tlfs/timers#partition-reference-tsc-mechanism
#include <stdbool.h>
#include <stdint.h>
#define WIN32_LEAN_AND_MEAN
@pmttavara
pmttavara / static_site_generator.cpp
Created August 13, 2022 04:06
Static website generator in C++ used to create https://philliptrudeau.com
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <ctype.h>
#include <stdarg.h>
//template <class F> struct deferrer { F f; ~deferrer() { f(); } };
//struct defer_dummy {};
@pmttavara
pmttavara / parsing.cpp
Created July 18, 2022 02:51
Very slow (300 MB/s) syntax highlighter for C++ - WIP from 2019; pulled verbatim from Eden text editor (dead project)
#include "parsing.h"
#include "buffer.h"
#include <ch_stl/time.h>
namespace parsing {
// This is a column-reduction table to map 128 ASCII values to a 11-input space.
// The values in this table are premultiplied with the number of DFA states
// to save one multiply when indexing the state transition table.
#define P (DFA_NUM_STATES)
static const u8 char_type[] = {
@pmttavara
pmttavara / allocator.h
Created February 27, 2021 06:04
Allocator abstraction that works for most use cases, according to Andrei Alexandrescu (+ Jon Blow)
#ifdef __cplusplus
extern "C" {
#endif
typedef struct Memory_Block {
uint8_t *data;
size_t num_bytes;
} Memory_Block;
typedef enum Allocator_Mode {
@pmttavara
pmttavara / enum_strings.cpp
Last active January 6, 2024 12:42
Using "functional X macro" for metaprogramming-like enum name extraction
#include <string.h> // for strcmp
#define Enum_Member(Name, Value) Name = Value,
#define Enum_To_String(Name, Value) if (e == Name) return # Name;
#define Enum_From_String(Name, Value) if (strcmp(str, # Name) == 0) return Name;
#define Define_Enum(EnumName) \
\
@pmttavara
pmttavara / ogl_load.c
Created January 2, 2020 08:37
OpenGL 1.0 loader in Win32
static const char *const GL_funcnames[] = {
"wglDeleteContext",
"wglUseFontBitmapsA",
"wglSwapIntervalEXT",
"glGetError",
"glBlendFunc",
"glEnable",
"glDisable",
"glViewport",
"glVertex3f",
@pmttavara
pmttavara / virtual_array.cpp
Last active December 29, 2019 01:26
Virtual allocated array template
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#define WIN32_LEAN_AND_MEAN
#define VC_EXTRALEAN
#include <windows.h>
template <class T> struct Virtual_Array {
@pmttavara
pmttavara / X_macro_call_by_name.cpp
Last active June 27, 2019 10:09
(for Ryan Fleury's stream) Variant of the X macro that doesn't require undefinition or redefinition of macros.
// Whatever, just example instructions.
#define INSTRUCTION_TYPE_LIST(instruction_type) \
instruction_type(push, sizeof(u32)) \
instruction_type(pop, sizeof(u32)) \
instruction_type(move, sizeof(u32 *) + sizeof(u32 *))
// Examples of usage (I didn't quite catch what the real usage was):
#define instruction_enum_name(name, size) Instruction_Type_ ## name,
#define instr_size(name, size) (size_t)size,