Skip to content

Instantly share code, notes, and snippets.

View CAFxX's full-sized avatar

Carlo Alberto Ferraris CAFxX

View GitHub Profile
@CAFxX
CAFxX / MacroCompression.cpp
Created November 22, 2011 16:39
Macro compression for LLVM
/*
Macro compression transform for LLVM
====================================
Description
-----------
This pass identifies duplicated code sequences in the whole module and replaces
them with equivalent "macros". To keep things simple, macros are defined as
functions made up of two instructions.
@CAFxX
CAFxX / cache.phpi
Created December 18, 2011 16:58
PHP remote file/URL cache
<?php
/*
A simple cache for remote files/URLs
2011 Carlo Alberto Ferraris <[email protected]>
===================================================
The cache function allows you to get the contents of a remote URL at
most once every N seconds. It also supports the If-Modified-Since HTTP
header to avoid fetching the resource again if it hasn't changed.
@CAFxX
CAFxX / filemap.c
Created February 16, 2012 14:15
filemap
#ifdef __cplusplus
extern "C" {
#endif
#include <sys/mman.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
@CAFxX
CAFxX / get_first_unique_char.c
Created February 23, 2012 15:10
Find the first non-repeated character in a string
#include <stdlib.h>
unsigned char* get_first_unique_char(unsigned char* str, const int len) {
int pos[256] __attribute__ ((__aligned__(64))), i, p;
if (str == NULL || len <= 0)
return NULL;
for (i=0; i<256; i++)
pos[i] = -1;
for (i=0; i<len; i++)
pos[str[i]] = ( pos[str[i]] == -1 ? i : -2 );
@CAFxX
CAFxX / LLVM-BB-trellis.html
Created July 20, 2012 08:30
LLVM fuzzer: BB trellis
<html><body><script>
const nBB = 256; // number of basic blocks to generate
const nI = 1; // number of instructions per basic block
const nVar = 16; // number of variables defined
const nBr = 16; // number of targets of the switch (for bThreaded==false)
const bThreaded = true; // simulate threaded code (implies nBr=nBB)
var i, vars="";
for (i=0; i<nVar; i++)
@CAFxX
CAFxX / MQReasonCodes.java
Created August 31, 2012 12:25
Convert IBM Websphere MQ reason codes to their human-readable description string
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import com.ibm.mq.constants.CMQC;
public class MQReasonCodes {
/* Use reflection on the CMQC class to fetch a human readable description of the
MQ reason code (e.g. getReason(2035) will return "MQRC_NOT_AUTHORIZED"). If the
reason code rc is not found, null will be returned */
@CAFxX
CAFxX / log10.c
Last active March 18, 2023 19:30
Fast integer log10 in C
#include <stdint.h>
/*
Fast 64bit integer log10
WARNING: calling ilog10c(0) yields undefined behaviour!
On x64 this compiles down to:
pushq %rbp
@CAFxX
CAFxX / stack.go
Last active August 29, 2015 14:23 — forked from bemasher/stack.go
package main
import (
"fmt"
)
type Stack struct {
top *Element
size int
}
@CAFxX
CAFxX / persistent_pipes_linux.md
Last active September 2, 2024 12:08
Persistent pipes/circular buffers for Linux

📂 Persistent "pipes" in Linux

In a project I'm working on I ran into the requirement of having some sort of persistent FIFO buffer or pipe in Linux, i.e. something file-like that could accept writes from a process and persist it to disk until a second process reads (and acknowledges) it. The persistence should be both across process restarts as well as OS restarts.

AFAICT unfortunately in the Linux world such a primitive does not exist (named pipes/FIFOs do not persist

@CAFxX
CAFxX / shrinkmap.go
Last active January 29, 2020 03:06
ShrinkMap
package shrinkmap
import "reflect"
// ShrinkMap shrinks the capacity of the supplied map. It panics if m is not a
// map. ShrinkMap may temporarily require double the amount of memory used by m.
// It is recommended not to hold iterators to the map while it is being shrunk.
// So much fail because of no generics. T_T
func ShrinkMap(m interface{}) interface{} {
M := reflect.ValueOf(m)