Skip to content

Instantly share code, notes, and snippets.

View pallas's full-sized avatar

Derrick Lyndon Pallas pallas

View GitHub Profile
@pallas
pallas / intrusive_queue.hh
Last active August 29, 2015 13:57
C++ intrusive queue
// All rights reserved,
// Derrick Pallas
// License: zlib
#ifndef INTRUSIVE_QUEUE_H
#define INTRUSIVE_QUEUE_H
#include <cassert>
#include <cstddef>
@pallas
pallas / countable.hh
Last active August 29, 2015 13:57
C++ reference counter base, with hook
// All rights reserved,
// Derrick Pallas
// License: zlib
#ifndef COUNTABLE_H
#define COUNTABLE_H
#include <cassert>
template <class T>
@pallas
pallas / intrusive_order.hh
Last active August 29, 2015 13:58
C++ intrusive ordered queue + fast tail inserts
// All rights reserved,
// Derrick Pallas
// License: zlib
#ifndef INTRUSIVE_ORDER_H
#define INTRUSIVE_ORDER_H
#include <cassert>
#include <cstddef>
#include <algorithm>
@pallas
pallas / magic_mask.h
Created July 19, 2015 20:54
C++ template to generate a magic mask with alternating runs of 0s & 1s
#include <cstdint>
template <typename T>
inline T magic_mask(int_fast8_t n) {
T base = T(1)<<(int_fast16_t(1)<<n);
return (~T(0))/(base*base-1)*(base-1);
}
@pallas
pallas / minkowski_question_mark.cc
Last active December 17, 2015 06:28
Minkowski question mark function
// All rights reserved,
// Derrick Pallas
// License: zlib
#include <cmath>
#include <cstdlib>
template <typename fp_type>
fp_type minkowski_question_mark(const fp_type fp) {
float a0 = floor(fp);
@pallas
pallas / try.hh
Created November 21, 2016 00:52
C++ macros for trying libc calls, throwing std::runtime_error on errno
#ifndef TRY_H
#define TRY_H
#include <cstdio>
#include <cstring>
#include <stdexcept>
#include <errno.h>
#define TRY(f, ...) ({ \
@pallas
pallas / container_of.hh
Last active November 22, 2016 04:05
C++ member-to-object for classes with standard layout
// All rights reserved,
// Derrick Pallas
// License: zlib
#ifndef CONTAINER_OF
#define CONTAINER_OF
#include <cstddef>
template <class T, typename M>
@pallas
pallas / courier-pem.mk
Created January 14, 2017 21:40
Will regenerate courier.pem & restart mail daemons when Let's Encrypt certificate renews.
BASE?=/etc/letsencrypt
DOMAIN?=mx.example.com
DHBITS:=4096
LIVE:=$(BASE)/live/$(DOMAIN)
PRIVKEY:=$(LIVE)/privkey.pem
FULLCHAIN:=$(LIVE)/fullchain.pem
.PHONY: default
@pallas
pallas / Makefile
Last active August 23, 2017 20:54
Basic Makefile template
default: all
DEBUG ?= -g -ggdb -DDEBUG
ifeq ($(DEBUG),)
override DEBUG := -DNDEBUG
endif
override LDFLAGS += -lstdc++
override CFLAGS += $(DEBUG) -MD -MP
override CXXFLAGS += $(DEBUG) -MD -MP
#!/bin/bash
# Author: Derrick Pallas
# License: zlib
BASE='dblock-set'
TEMP=${BASE}$$
ipset create -exist "$BASE" hash:ip --netmask 24 || exit 1
iptables -nL INPUT | grep -q "$BASE" ||
(
( iptables -N "$BASE" || iptables -F "$BASE" ) &&