Skip to content

Instantly share code, notes, and snippets.

@glampert
glampert / asan_guarded_allocator.cpp
Created Sep 11, 2017
Simple allocator wrapper that adds ASan-poisoned header and footer buffers to each allocation.
View asan_guarded_allocator.cpp
// ----------------------------------------------------------------------------
// c++ -std=c++11 -Wall -Wextra -Wshadow -Wunused -pedantic -fsanitize=address -fno-omit-frame-pointer asan_guarded_allocator.cpp
#include <cassert>
#include <cstdio>
#include <cstdint>
#include <cstdlib>
#include <unordered_map>
@glampert
glampert / cplusrust_option_result.cpp
Created Jan 7, 2018
Mostly complete and usable C++ version of Rust's Option<T> and Result<T,E> using only standard C++14.
View cplusrust_option_result.cpp
// Mostly complete and usable C++ version of Rust's Option<T> and Result<T,E> using only standard C++14, for fun.
// This code is in the public domain.
///////////////////////////////////////////////////////////////////////////////
// Option.hpp
///////////////////////////////////////////////////////////////////////////////
#pragma once
// ----------------------------------------------------------------------------
// Includes
@glampert
glampert / fast_itoa.cpp
Created May 8, 2018
Fast integer to ASCII string conversion functions.
View fast_itoa.cpp
#include <cassert>
#include <cstdint>
#include <cstdarg>
#include <cstdio>
#include <cmath>
#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
#include <intrin.h>
@glampert
glampert / simple_lru_cache.cpp
Created May 7, 2017
Simple LRU cache backed by a hash table and a fixed-size circular queue.
View simple_lru_cache.cpp
// ----------------------------------------------------------------------------
// Least Recently Used cache backed by a hash table
// (unprdered_map) and a fixed-size circular queue.
//
// License:
// Public Domain.
// Feel free to copy, distribute, and modify this file as you see fit.
// ----------------------------------------------------------------------------
@glampert
glampert / Memset.cpp
Created Feb 16, 2020
Silly memset function with optimizations for 4,8,16-byte aligned types.
View Memset.cpp
#include <cstdio>
#include <cstdint>
#include <cassert>
#include <xmmintrin.h>
///////////////////////////////////////////////////////////////////////////////
namespace detail
{
@glampert
glampert / stack_machine.cpp
Created Mar 22, 2017
1-register stack caching test for stack-based interpreters.
View stack_machine.cpp
//
// Simple stack-based virtual machine tests.
//
// I use two slightly different strategies to manage the
// interpreter stack: First is using the "naive" approach
// of keeping a stack pointer that is bumped/decremented
// for every instruction that has stack operands. The second
// approach uses a "stack cache" register to cache the
// Top-of-Stack (TOS) value.
//
@glampert
glampert / atomic_slist_128bits_cas.cpp
Created Mar 20, 2017
Atomic/lockless linked list using 128-bits Compare And Swap to solve the A-B-A problem.
View atomic_slist_128bits_cas.cpp
// --------------------------------------------------------------------------------------
// Atomic singly-linked intrusive list using 128-bits Compare And Swap (AKA: DCAS).
// Keeps a version counter with the list head to prevent the A-B-A problem.
//
// Based on the implementation found in moodycamel.com:
// http://moodycamel.com/blog/2014/solving-the-aba-problem-for-lock-free-free-lists
//
// My implementation uses raw GCC/Clang atomics intrinsics. While in theory
// std::atomic of a struct of exactly 16 bytes and properly aligned could
@glampert
glampert / linear_allocator.cpp
Created Nov 13, 2016
Stack-like linear allocator - uses std::atomic to allow concurrent allocations from different threads.
View linear_allocator.cpp
// ============================================================================
// This code is in the Public Domain - I claim no copyrights over it.
// Use at your own leisure and risk.
//
// Compiled and tested with:
// c++ -std=c++11 -fno-exceptions -Wall -Wextra -pedantic -O3 linear_allocator.cpp
// ============================================================================
#include <cassert>
#include <cstdint>