Skip to content

Instantly share code, notes, and snippets.

@glampert
glampert / spinlock_mutex.cpp
Created Nov 13, 2016
Implementation of a simple spinlock mutex using std::atomic_flag plus a scoped lock helper that supports recursively locking the spinlock on the same thread.
View spinlock_mutex.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 spinlock_mutex.cpp
// ============================================================================
#include <cassert>
#include <cstdio>
@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>
View debug_allocator.hpp
// ================================================================================================
// -*- C++ -*-
// File: debug_allocator.hpp
// Author: Guilherme R. Lampert
// Created on: 03/12/16
//
// About:
// Virtual memory pages backed debug allocator. Aligns each memory allocation to one
// or more pages and adds an additional header and footer protected pages, ensuring that
View template_method_specialization.cpp
//
// Overriding a member method of a template class/struct by defining a specialized implementation.
//
#include <cstdio>
#include <typeinfo>
template<typename T>
struct Foo
{
View data_race_detection.cpp
//
// Simple unique lock guard to detect concurrent access to unsynchronized global data.
//
// This can be a useful tool to debug data races and identify places where a mutex should
// be introduced. Once the race is fixed, you'll likely want to strip out this code...
//
// --------------------------------------------------------
#include <atomic>
@glampert
glampert / memory_pools.hpp
Created Feb 8, 2017
Templated C++ memory pools.
View memory_pools.hpp
// ================================================================================================
// -*- C++ -*-
// File: memory_pools.hpp
// Author: Guilherme R. Lampert
// Created on: 08/02/17
//
// About:
// Useful templated memory pools of objects (header only).
// This file provides a DynamicPool and a pair of FixedSizePools,
@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 / 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 / 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 / simple_heap.hpp
Created May 13, 2017
Bucketed heap allocator. Each allocation size has a corresponding bucket which has a list of pages. Pages are split into fixed-size chunks.
View simple_heap.hpp
#pragma once
// ========================================================
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>