Skip to content

Instantly share code, notes, and snippets.

View wangbj's full-sized avatar

Baojun Wang wangbj

View GitHub Profile
import Data.Ord
import Data.Char
padLeft xs x k = replicate k x ++ xs
padRight xs x k = xs ++ replicate k x
addHelper xs = fixer . foldr go (0, []) . zip xs
where go (x, y) (carry, zs) | t >= 10 = (1, chr (t - 10 + ord '0'): zs)
| t < 10 = (0, chr (t + ord '0'): zs)
#include <sys/types.h>
#include <sys/auxv.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* run: ./a.out --long-option-one=1 --long-option-two=test -- abc d e f "" */
#include <iostream>
#include <string>
#include <cstdlib>
#include <cstdio>
#include "cxxopts.hpp"
int main(int argc, char* argv[])
{
@wangbj
wangbj / trie.rs
Last active August 31, 2019 18:41
rust bytestring trie
use std::collections::{HashMap};
#[derive(Debug)]
struct Trie<T> {
un: HashMap<usize, (Option<T>, Box<Trie<T>>)>,
}
impl <T> Trie<T> {
fn new() -> Self {
Trie {
```
0000000000021190 <alloc::raw_vec::RawVec<T,A>::reserve_internal>: (0x00007fffffffb450+0x118+0x128+0x98+0x98+0x58+0x50+0x50+0x60)
00000000000247c0 <alloc::raw_vec::RawVec<T,A>::reserve>: (0x00007fffffffb450+0x118+0x128+0x98+0x98+0x58+0x50+0x50+0x60+0x290)
0000000000055ca0 <alloc::vec::Vec<T>::reserve>: ((0x00007fffffffb450+0x118+0x128+0x98+0x98+0x58+0x50+0x50+0x60+0x290+0x50)
0000000000053fb0 <alloc::vec::Vec<T>::extend_desugared>:((0x00007fffffffb450+0x118+0x128+0x98+0x98+0x58+0x50+0x50+0x60+0x290+0x50+0x20)
@wangbj
wangbj / pipe-poll.c
Created June 27, 2019 01:13
epoll on pipe fds
/* build with `-std=c11 -O -pthread -D_GNU_SOURCE=1` */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE 1
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/epoll.h>
#include <unistd.h>
@wangbj
wangbj / condp2.c
Created June 26, 2019 22:01
pthread cond var test, block on child thread
/* build with `-O -pthread -D_GNU_SOURCE=1` */
#include <unistd.h>
#include <pthread.h>
#include <assert.h>
static pthread_cond_t run_first = PTHREAD_COND_INITIALIZER;
static pthread_mutex_t cond_mutex = PTHREAD_MUTEX_INITIALIZER;
static void* second_thread(void* param) {
assert(pthread_cond_wait(&run_first, &cond_mutex) == 0);
@wangbj
wangbj / condp.c
Created June 26, 2019 22:00
pthread cond var test, wait on main thread
/* build with `-O -pthread -D_GNU_SOURCE=1` */
#include <unistd.h>
#include <pthread.h>
#include <assert.h>
static pthread_cond_t run_first = PTHREAD_COND_INITIALIZER;
static void* second_thread(void* param) {
assert(write(STDOUT_FILENO, "second\n", 7) == 7);
return NULL;
@wangbj
wangbj / systrace-echo.md
Created June 14, 2019 20:44
systrace echo tool
./target/debug/systrace --tool=/home/parfunc/git/systrace/target/debug/libecho.so --preloader=/home/parfunc/git/systrace/target/debug/libpreloader.so ./tests/test1.sh
[pid 31902] openat(AT_FDCWD, "/dev/tty", O_NONBLOCK) = 3
[pid 31902] close(3) = 0
[pid 31902] openat(AT_FDCWD, "/usr/lib/locale/locale-archive", O_CLOEXEC) = 3
[pid 31902] fstat(3, 0x7ffff771aac0) = 0
[pid 31902] mmap(NULL, 0x4b3220, PROT_READ, MAP_PRIVATE, 3, 0) = 0x7ffff595a000
[pid 31902] close(3) = 0
@wangbj
wangbj / static-pie-ld_preload.md
Created May 20, 2019 22:53
LD_PRELOAD a binary compiled with `-static-pie`

Source code:

#include <stdio.h>
#include <time.h>

#ifndef TEST
time_t time(time_t* tp){
	if (tp)
 *tp = 744847200;