Skip to content

Instantly share code, notes, and snippets.

@neomantra
neomantra / gist:1125619
Created August 4, 2011 16:55
mongol API sketch
-- mongol is based on the mongo C driver:
-- http://api.mongodb.org/c/current/
-- BSON types are marshalled just like luamongo:
-- http://code.google.com/p/luamongo/wiki/BsonTypes
-- new empty BSON object
b = bson.new()
-- new BSON object
@neomantra
neomantra / luajit-ev.lua
Created November 30, 2011 16:56
spike of luajit-ev
-- LuaJIT binding to libev (http://libev.schmorp.de/)
--
-- uses almost-identical API to lua-ev (https://github.com/brimworks/lua-ev)
-- Author: Evan Wies <neomantra at gmail>
--
local ffi = require('ffi')
local bit = require("bit")
local band, bor = bit.band, bit.bor
@neomantra
neomantra / gist:1540828
Created December 30, 2011 18:00
luamongo run_command
/*
* res,err = db:run_command(dbname, lua_table or json_str, options)
*/
static int dbclient_run_command(lua_State *L) {
DBClientBase *dbclient = userdata_to_dbclient(L, 1);
const char *ns = luaL_checkstring(L, 2);
int options = luaL_tointeger(L, 4); // if it is invalid it returns 0
BSONObj command; // arg 3
try {
@neomantra
neomantra / gist:1540945
Created December 30, 2011 18:35
luamongo dbclient_get_last_error
/*
* str = db:get_last_error()
*/
static int dbclient_get_last_error(lua_State *L) {
DBClientBase *dbclient = userdata_to_dbclient(L, 1);
string result = dbclient->getLastError();
lua_pushlstring(L, result.c_str(), result.size());
return 1;
}
ffi.cdef([[
struct ifaddrs {
struct ifaddrs *ifa_next; /* Next item in list */
char *ifa_name; /* Name of interface */
unsigned int ifa_flags; /* Flags from SIOCGIFFLAGS */
struct sockaddr *ifa_addr; /* Address of interface */
struct sockaddr *ifa_netmask; /* Netmask of interface */
union {
struct sockaddr *ifu_broadaddr; /* Broadcast address of interface */
struct sockaddr *ifu_dstaddr; /* Point-to-point destination address */
#include <cstdint>
#include <iostream>
#include <unordered_map>
struct Foo
{
int a;
};
uint64_t some_func();
@neomantra
neomantra / permute_string.lua
Last active December 14, 2015 14:39
permute_string: returns a table of all permutations of `str`
--- returns a table of all permutations of `str`
local function permute_string( str )
local string_byte, string_char = string.byte, string.char
local src = {}
for i = 1, #str do
src[i] = string_byte( str, i )
end
local perm = {}
@neomantra
neomantra / nanomsg.lua
Last active January 14, 2016 09:18
LuaJIT FFI binding for nanomsg
--[[
nanomsg LuaJIT FFI binding
Copyright (c) 2013 Evan Wies <evan at neomantra dot net>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom
@neomantra
neomantra / nanomsg_echo_client.c
Created March 18, 2013 21:33
Sample echo client for nanomsg
// gcc -o echo_client -g examples/echo_client.c -I ../nanomsg/src -L ../nanomsg/build -l nanomsg
// LD_LIBRARY_PATH="$LD_LIBRARY_PATH:../nanomsg/build" ./echo_client foobar
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include "nn.h"
#include "reqrep.h"
int main( int argc, const char* argv[] )
@neomantra
neomantra / get_interrupt_affinity.sh
Created August 22, 2013 14:27
Scans for smp affinity for the interrupts on the passed interfaces.
#!/bin/bash
usage()
{
cat >&2 <<EOF
usage: $0 [-h] iface [iface2 [...]]
Scans for smp affinity for the interrupts on the passed interfaces.
Must be run as root.