Skip to content

Instantly share code, notes, and snippets.

@Nexuapex
Nexuapex / msvcrt_heap_exc.cc
Last active December 24, 2015 01:29
There's this handy flag on Win32 heap objects called HEAP_GENERATE_EXCEPTIONS, which causes the heap manager to raise a structured exception instead of returning null on a failed allocation. But the C runtime creates its heap behind the scenes and doesn't allow you access to it, and you can't add this flag after creation anyway. Unless you are t…
#include <malloc.h>
#include <windows.h>
static HANDLE FindCrtHeap()
{
// Pick a block that we know is in the CRT heap.
//
_HEAPINFO crtEntry = {};
if (_HEAPOK != _heapwalk(&crtEntry))
@Nexuapex
Nexuapex / resource_table.cc
Created April 26, 2016 06:06
Sketch of a few ways to store 8 or 16 elements indexed by a packed byte array.
#include <xmmintrin.h>
#include <stdint.h>
struct ResourceTable8
{
uint64_t type_bits_;
uintptr_t elements_[8];
inline uint64_t mask_for_type(uint8_t type)
{
@Nexuapex
Nexuapex / workerpool.py
Created January 8, 2015 03:15
Simple Python MPMC queue and thread pool.
from __future__ import absolute_import
import sys
import threading
import collections
class WorkerPool(object):
"""A thread pool that processes items from an unbounded work queue, for
simple multi-producer, multi-consumer processes.
@Nexuapex
Nexuapex / bit_cast.h
Created April 1, 2018 19:32
Minimal-dependencies implementation of bit_cast.
#include <string.h>
#if defined(__has_attribute)
#if __has_attribute(always_inline)
#define ALWAYS_INLINE __attribute__((always_inline))
#endif
#endif
#if defined(_MSC_VER)
// To reduce debug build overhead:
// Abseil absl::flat_hash_map
// Analysis of the critical path for perfectly predicted load hit.
// https://godbolt.org/z/P_ksaa
//
// x86_64
// Instruction latencies have been entirely ignored.
# rdi = desired key
mov rax, qword ptr [rip + kHashSeed] # [a00] rax = per-process seed (with entropy)
add rax, rdi # [a01] rax = hash so far