Skip to content

Instantly share code, notes, and snippets.

Guilherme Lampert glampert

Block or report user

Report or block glampert

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@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 / 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 / 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>
View mini_heap_allocator.cpp
//
// 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.
View tiny_allocator.cpp
//
// 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>
@glampert
glampert / lockless_hash_table.cpp
Created May 29, 2017
Fixed-size concurrent/lockless hash table for integer types using C++11 atomics.
View lockless_hash_table.cpp
// 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>
@glampert
glampert / in_place_function.hpp
Created May 15, 2017
std::function-like functor with an inline buffer that never dynamically allocates. Also doesn't use C++ exceptions.
View in_place_function.hpp
#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 / 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>
@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 / 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.
//
You can’t perform that action at this time.