Skip to content

Instantly share code, notes, and snippets.

@glampert
glampert / spinlock_mutex.cpp
Created November 13, 2016 21:10
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.
// ============================================================================
// 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>
// ================================================================================================
// -*- 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
//
// Overriding a member method of a template class/struct by defining a specialized implementation.
//
#include <cstdio>
#include <typeinfo>
template<typename T>
struct Foo
{
//
// 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 / simple_heap.hpp
Created May 13, 2017 15:22
Bucketed heap allocator. Each allocation size has a corresponding bucket which has a list of pages. Pages are split into fixed-size chunks.
#pragma once
// ========================================================
#include <algorithm>
#include <array>
#include <cstddef>
#include <cstdint>
#include <cstring>
@glampert
glampert / in_place_function.hpp
Created May 15, 2017 00:06
std::function-like functor with an inline buffer that never dynamically allocates. Also doesn't use C++ exceptions.
#pragma once
// ============================================================================
// About:
// std::function-like functor that has an inline buffer to store the
// callable object. It is guaranteed to never allocate. If the callable
// is too big, a static_assert is triggered. The size of the functor is
// defined as a template argument, so it can be of any size. It also doesn't
// use C++ exceptions.
//
@glampert
glampert / lockless_hash_table.cpp
Created May 29, 2017 02:02
Fixed-size concurrent/lockless hash table for integer types using C++11 atomics.
// License:
// Public Domain. Feel free to copy, distribute, and modify this file as you see fit.
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <cstdio>
#include <array>
#include <atomic>
//
// Simple allocator for small memory blocks (from 1 to 255 bytes).
// It keeps freelists of small blocks, built on top of 8K pages.
// The allocator only grows - empty pages are never freed back
// to the system.
//
// This allocator could easily be made lockless with atomic freelists.
//
#include <cstdint>
//
// General purpose minimal heap allocator.
//
// Keeps a freelist of chunks of arbitrary size. First round of allocations
// will use the whole heap block before it starts going into the freelist.
// Freeing just pushes the block back into the freelist and is constant time.
// Allocating might search the freelist if the whole heap was already allocated
// once.
//
// To reduce fragmentation we could merge free nodes either on free or allocate.
@glampert
glampert / memory_pools.hpp
Created February 8, 2017 00:45
Templated C++ memory pools.
// ================================================================================================
// -*- 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,