Skip to content

Instantly share code, notes, and snippets.

@depp
depp / test.cpp
Last active August 29, 2015 14:02
#include <iostream>
#include <vector>
#undef CUSTOM_BUFFER
#if defined CUSTOM_BUFFER
static const int BUFFER_SIZE = 1024 * 16;
#endif
int main()
@depp
depp / gist:87a5f1079556eb0ed07e
Created November 7, 2014 22:06
Why you shouldn't call super(self.__class__, self).__init__()
class A(object):
def __init__(self):
print('A')
class B(A):
def __init__(self):
print('B')
super(self.__class__, self).__init__()
class C(B):
@depp
depp / Makefile
Created February 12, 2015 22:59
Copy memory, reversing bits
run: test.o main.o
cc -o $@ $^
test.o: test.c
cc -Wall -Wextra -O3 -c $< -o $@
main.o: main.c
cc -Wall -Wextra -O2 -c $< -o $@
@depp
depp / test.cpp
Last active August 29, 2015 14:17
As you can see, only extern_func() is accessible from other translation units.
$ c++ -std=c++11 -c test.cpp
$ nm -g test.o | c++filt
0000000000000000 T extern_func()
$ nm test.o | c++filt
0000000000000000 T extern_func()
0000000000000006 t static_func()
000000000000000c t (anonymous namespace)::anonymous_namespace_func()
@depp
depp / test3.c
Created May 1, 2015 06:37
Data race
~ $ cc -fPIC -pie -fsanitize=thread -pthread test3.c -Wall -Wextra -g
~ $ ./a.out 5
i am thread 1 .created a new thread(132118272) in the itaration 0
i am thread 1 .created a new thread(123725568) in the itaration 1
==================
WARNING: ThreadSanitizer: data race (pid=20232)
Read of size 4 at 0x7ffcdeefaadc by thread T1:
#0 thread /home/depp/test3.c:10 (a.out+0x000000000c14)
#1 <null> <null> (libtsan.so.0+0x000000023519)
@depp
depp / Enum.hs
Created April 1, 2012 23:07
Demonstration of incorrect (Enum x, Enum y) => Enum (x, y) instance
-- See http://stackoverflow.com/questions/9967790/instance-in-haskell
instance (Enum x, Enum y) => Enum (x, y) where
fromEnum (x,y) =
k^2 + 2*j + if permuted then 1 else 0
where
k = max (fromEnum x) (fromEnum y)
j = min (fromEnum x) (fromEnum y)
permuted = (fromEnum y) > (fromEnum x)
toEnum n =
import os, time
def list_modules(path):
for name in os.listdir(path):
base, ext = os.path.splitext(name)
if '.' in base or base.startswith('__'):
continue
if ext == '.py':
yield base
elif not ext:
if not os.path.exists(os.path.join(base, '__init__.py')):
@depp
depp / gist:8378663
Created January 12, 2014 00:05
Iterate over a string
fn main() {
let mut x = "hello".chars();
while (true) {
match x.next() {
Some('o') => return,
Some(c) => println!("Char: {}", c),
None => return
}
}
}
@depp
depp / gtk.c
Created February 13, 2013 04:34
/* Copyright 2012 Dietrich Epp <depp@zdome.net> */
#if defined(__GNUC__) && \
((__GNUC__ == 4 && __GNUC_MINOR >= 6) || __GNUC__ > 4)
#define HAVE_DPUSH 1
#endif
#include "keycode/keycode.h"
#include "keycode/keytable.h"
#include "sg/audio_system.h"
#include "sg/clock.h"
@depp
depp / add.cpp
Created February 15, 2018 16:48
Bignum addition in inline assembly
// See: https://stackoverflow.com/a/48812277/82294
template <int N>
void add(unsigned long long *dest, unsigned long long *src) {
__asm__(
"movq (%1), %%rax"
"\n\taddq %%rax, (%0)"
"\n.local add_offset"
"\n.set add_offset,0"
"\n.rept %P2"