Skip to content

Instantly share code, notes, and snippets.

@neomantra
neomantra / bson_float_test.c
Created June 13, 2017 05:38
BSON Float test
/// cc -o bson_float_test bson_float_test.c -lbson-1.0.0
#include <stdio.h>
#include <libbson-1.0/bson.h>
int main(int argc, const char* argv[])
{
bson_t b;
bson_init(&b);
@neomantra
neomantra / tuple_iter_inline.cpp
Created December 15, 2016 17:04
experiment with tuple iteration, boost::fusion versus manual template recursion.
#include <iostream>
#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
struct One {
int value() const { return 1+rand(); }
};
struct Two {
int value() const { return 2+rand(); }
@neomantra
neomantra / tuple_iter_inline.cpp
Created December 15, 2016 17:04
experiment with tuple iteration, boost::fusion versus manual template recursion.
#include <iostream>
#include <boost/fusion/adapted/std_tuple.hpp>
#include <boost/fusion/algorithm/iteration/for_each.hpp>
struct One {
int value() const { return 1+rand(); }
};
struct Two {
int value() const { return 2+rand(); }
local ffi = require("ffi")
local new = ffi.new
--[[
All numeric calculations are performed with doubles. Using
floats for storage saves memory (for big arrays). But arithmetic
is usually slower due to the required float<->double conversions.
]]
local Matrix
local MatrixStructure = ffi.typeof("double[16]")
@neomantra
neomantra / bcname.lua
Created April 10, 2014 15:00
Convert LuaJIT bytecode number to name.
-- Converts a LuaJIT bytecode number to name
--
-- usage: luajit bcname.lua bytecode_number [bytecode_number2 [...]]
--
-- example:
-- $ luajit-2.1 bcname.lua 71 72
-- VARG
-- ISNEXT
--
-- From: http://www.freelists.org/post/luajit/frames-and-tail-calls,1
@neomantra
neomantra / output.txt
Last active August 29, 2015 13:56
FFI serializer fake metamethod
$ luajit ~/Desktop/serialize_metamethod.lua
2 =%= 3
cdata<struct 98>: 0x0004d738
@neomantra
neomantra / FFI_metamethod_play.lua
Created February 27, 2014 14:35
Exploring accessing metamethods with FFI objects
local ffi = require 'ffi'
local C = ffi.C
-- in reality this would be some hashing library,
-- but for this example they are dumb functions
local hasher = function(x) return tonumber(x) end
local H = {
hash = hasher,
@neomantra
neomantra / alloc_test.lua
Last active April 19, 2017 21:22
LuaJIT experiments in trying to grab memory outside of the low 32-bits of memory using jemalloc. Looks like jemalloc on Linux is a silver bullet!
#!/usr/bin/env luajit
-- iteratively allocates of `size` bytes using a specified allocator
-- intended to exercise malloc and jemalloc to see where they are allocating memory
-- requires the luajit-jemalloc module
local num_iters = 5
local alloc = 'malloc'
local size = arg[1] and tonumber(arg[1]) or 1000
if arg[2] and arg[2] == 'jemalloc' then alloc = 'jemalloc' end
@neomantra
neomantra / thing-o-matic-command-line-update.md
Created January 14, 2014 04:30
Thing-o-matic Firmware Upgrade from command-line. Using the ReplicatorG and MakerWare I was getting errors like "upload failed".... and endlessly pressing reset and upload. This worked without any tricky timing. I did this on OSX, but I don't know why it wouldn't work similarly on another OS...

After spending two hours fucking with pushing my reset-button and clicking upload in both ReplicatorG and Makerware, I figured out how to do it from the command line. The command line was determined from running ReplicatorG on the console and trying to upgrade the firmware.

Install ReplicatorG

Download and install it from http://www.makerbot.com/sailfish/install/

Get the 4.4 firmware from Makerbot

wget http://firmware.makerbot.com/firmware/MB-mb24-2560-Sailfish-v4.4-r1029.hex
@neomantra
neomantra / spawn.lua
Last active December 22, 2017 09:38
Using LuaJIT FFI, spawn a Linux command in the background.
-- Spawn a command in the background, optionally redirecting stderr and stdout
--
-- requiring this file returns a function(cmd_line, stdout_redirect, stderr_redirect)
--
-- `cmd_line` is the command with possible arguments
-- optional `stdout_redirect` is io.stdout, io.stderr, or a filename. default/nil is io.stdout
-- optional `stderr_redirect` is io.stdout, io.stderr, or a filename. default/nil is io.stderr
--
-- Example:
-- luajit -e 'require("spawn")("cat /etc/network/interfaces", "foo1", io.stdout)'