Skip to content

Instantly share code, notes, and snippets.

View aperezdc's full-sized avatar
🛋️
Couch-potatoing

Adrian Perez aperezdc

🛋️
Couch-potatoing
View GitHub Profile
@aperezdc
aperezdc / callfunction.js
Created December 3, 2014 19:07
Compare speed of f.call(...) vs. %_CallFunction(..., f) in V8
// Run with: d8 --allow-natives-syntax callfunction.js
function f1(n, cb, thisArg) {
for (var i = 0; i < n; i++) {
cb.call(thisArg, i);
}
}
function f2(n, cb, thisArg) {
for (var i = 0; i < n; i++) {
@aperezdc
aperezdc / atomic.h
Created October 11, 2014 12:55
Replacement for the Linux kernel asm/atomic.h header using GCC built-ins
#ifndef _ATOMIC_H
#define _ATOMIC_H
/* Check GCC version, just to be safe */
#if !defined(__GNUC__) || (__GNUC__ < 4) || (__GNUC_MINOR__ < 1)
# error atomic.h works only with GCC newer than version 4.1
#endif /* GNUC >= 4.1 */
/**
@aperezdc
aperezdc / styles.css.in
Last active August 29, 2015 14:06
One-line CSS pre-processor àla LESS / SCSS / SASS
/*
* Run this through the C/C++ preprocessor like this:
*
* cpp -nostdinc -w -P input.css.in | sed 'y/$/#/' > output.css
*
* Note that the C/C++ preprocessor always interprets octothorpe characters
* at beggining of lines as preprocessor directives. Use sollar signs instead
* to avoid the trouble. Dollar signs are changed into octothorpes using
* "sed" after passing the source through the preprocessor.
*
@aperezdc
aperezdc / Makefile
Created January 15, 2014 19:41
Example on how to measure memory usage using the V8 counters (or any other thing from the counters).
CXXFLAGS += -Wall -std=gnu++11 $(OPT_CXXFLAGS)
LDFLAGS += -lv8
CC = $(CXX)
all: v8-mem-accounting
v8-mem-accounting: v8-mem-accounting.o
clean:
$(RM) v8-mem-accounting v8-mem-accounting.o
@aperezdc
aperezdc / memcount.cc
Last active December 29, 2015 09:29
Tool for simple peak system heap memory tracking. Tested with glibc (Linux), may work with any other reasonable *nix system.
/*
* memcount.cc
* Copyright (C) 2013 Adrian Perez <aperez@igalia.com>
*
* Distributed under terms of the MIT license.
*
* Building:
* =========
*
* g++ -std=c++11 -shared -o memcount.so memcount.cc -fPIC -DTRACK_MEM
@aperezdc
aperezdc / clock.cc
Last active December 27, 2015 11:39
Simple micro-benchmark to know which is faster: a virtual method invocation, or calling through a function pointer + userdata pointer (the userdata is meant to replace the usage of subclasses and also replaces passing the “this” pointer implicitly in virtual method calls).
/*
* clock.cc
* Copyright (C) 2013 Adrian Perez <aperez@igalia.com>
*
* Distributed under terms of the MIT license.
*/
#ifndef _POSIX_C_SOURCE
#define _POSIX_C_SOURCE 199309L
#endif // !_POSIX_C_SOURCE
@aperezdc
aperezdc / array-custom-allocation.cc
Created November 1, 2013 15:53
Custom allocation in C++ for arrays without using the built-in new[] and delete[] operators.
/*
* array-custom-allocation.cc
* Copyright (C) 2013 Adrian Perez <aperez@igalia.com>
*
* Distributed under terms of the MIT license.
*/
#include <cstdio>
#include <cstdlib>
#include <cstdarg>
@aperezdc
aperezdc / rand31.rs
Created September 28, 2013 21:21
RAND31 random number generator in Rust. Suitable for generating white noise for audio applications.
/*
* rand31.rs
* Copyright (C) 2013 Adrian Perez <aperez@igalia.com>
*
* Implements the rand31 random number generation algorithm, which is
* particularly suitable for use as white noise for sound applications.
*
* For more information on the algorithm, please refer to:
* http://www.firstptr.com.au/dsp/rand31/
*
@aperezdc
aperezdc / rand31.d
Created September 18, 2013 16:15
RAND31 random number generator in D. Suitable for generating white noise for audio applications.
/*
* rand31.d
* Copyright (C) 2013 Adrian Perez <aperez@igalia.com>
*
* Implements the rand31 random number generation algorithm, which is
* particularly suitable for use as white noise for sound applications.
*
* For more information on the algorithm, please refer to:
* http://www.firstpr.com.au/dsp/rand31/
*
@aperezdc
aperezdc / encore-cm10.2-sdswap.sh
Last active December 22, 2015 21:29
Edits the the Nook Color initial ramdisk of Android 4.3-based ROMs (e.g. CyanogenMod 10.2) to mount the internal eMMC data partition to be used as an “SD card”. You need to run this script from an Unix system (e.g. GNU/Linux) with your device connected. The tools “adb” and “mkimage” (from u-Boot) must be installed.
#! /bin/bash
#
# android-4.3-encore-sdswap-ramdisk
# Copyright (C) 2013 Adrian Perez <aperez@igalia.com>
#
# Distributed under terms of the MIT license.
#
set -e
devices=$(adb devices | sed -e 1d -e '/^[:space:]$/d')