Skip to content

Instantly share code, notes, and snippets.

@airekans
airekans / json_bnf.md
Last active August 29, 2015 14:21
Simple JSON BNF
JSON := OBJECT | ARRAY | NUMBER | STRING | BOOL | NULL

OBJECT := '{' KVS '}'
KVS := KEY ':' VALUE KVS2 | $
KVS2 := ',' KEY ':' VALUE KVS2 | $
KEY := NUMBER | STRING | BOOL
VALUE := JSON

ARRAY := '[' ELEMS ']'
@airekans
airekans / expr_tree.cpp
Created April 30, 2015 01:30
A simple expression tree evaluation algorithm imlementation
struct ExprNode
{
enum Type
{
OPERATOR,
NUMBER
};
enum OP
{
@airekans
airekans / note.md
Last active August 29, 2015 14:17
Linux 2.6.10 source code reading

fork sys_call

  1. task_struct definition: include/linux/sched.h 表示进程
  2. thread_info 表示线程,里边有task_struct的指针
@airekans
airekans / test_sse.cpp
Created March 8, 2015 04:19
A test program for vector extension in gcc. The sse version is not correct, just for testing
#include <iostream>
#include <algorithm>
using namespace std;
void int_to_str(unsigned int num, char* buffer, unsigned int size)
{
static const char* digits = "0123456789";
char* buf_ptr = buffer;
do
@airekans
airekans / test_cache.cpp
Created March 7, 2015 17:09
A cache testing program from CSAPP
#include <cstdlib>
#include <cstring>
#define LOC(m, i, j, row_size) m[(i) * (row_size) + (j)]
void matrix_mul1(int* a, int* b, int* c, int size)
{
int res = 0, i, j, k;
for (i = 0; i < size; ++i)
{
@airekans
airekans / gperftools.md
Created February 27, 2015 15:05
gperftools输出的含义

gperftools在结束的时候会有类似下面的输出:

PROFILE: interrupts/evictions/bytes = 123/1/1231231

在看了gperftools的代码之后,这几个数字的具体含义如下:

  1. interrupts代表收到信号的次数。由于gperftools是基于setitimer的,所以这个数字就是signal handler调用的次数。
  2. evictions代表把profile信息flush到磁盘的次数。在gperftools里面会有一个固定大小的buffer用来存储profile信息,而当这些信息超出大小的时候,会flush一部分信息到磁盘。
  3. bytes表示profile输出文件的大小。
@airekans
airekans / test_builtin_expect_asm.cpp
Created January 5, 2015 10:24
test __builtin_expect in GCC
// Use the following command to compile, -O2 is necessary
// g++ test_builtin_expect_asm.cpp -O2 -S -o test_builtin_expect_asm.S
// You can see that the branch expected will not jump.
#include <cstdlib>
#include <cstring>
#include <cstdio>
#define likely(x) __builtin_expect(!!(x), 1)
@airekans
airekans / calcdims.cpp
Created December 26, 2014 07:41
Template Meta Programming to calculate array dimension
// Reference: http://stackoverflow.com/a/12261808/1208658
#include <iostream>
#include <vector>
using namespace std;
template<typename T>
struct CalcDims
{
@airekans
airekans / likely.c
Created December 24, 2014 09:05
likely/unlikely macro in C
#ifdef HAVE_BUILTIN_EXPECT
#define LIKELY(x) __builtin_expect(!!(x), 1)
#define UNLIKELY(x) __builtin_expect(!!(x), 0)
#else
#define LIKELY(x) (x)
#define UNLIKELY(x) (x)
#endif
@airekans
airekans / test_printf.cpp
Last active August 29, 2015 14:12
Performance comparison of various printf implementations
#include <sys/time.h>
#include <sstream>
#include <iostream>
#include <string>
#include <wx/string.h>
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
using namespace std;