Skip to content

Instantly share code, notes, and snippets.

View akiradeveloper's full-sized avatar

Akira Hayakawa akiradeveloper

  • Yokohama
View GitHub Profile
@akiradeveloper
akiradeveloper / output
Created September 12, 2012 12:45
0-length array and member alignment
#include <stdio.h>
#include <stdlib.h>
struct Hoge {
char c;
int a[];
};
int main(void){
struct Hoge *hoge = malloc(sizeof(struct Hoge) + sizeof(int) * 10);
@akiradeveloper
akiradeveloper / container_of.c
Created September 15, 2012 13:47
container_ofの実験
#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type, member) );})
#include <stdio.h>
#include <stdlib.h>
struct T1 {
@akiradeveloper
akiradeveloper / output
Created September 17, 2012 04:53
binary christmas
require "date"
d1 = Date.new(2012, 2, -1)
d2 = Date.new(2014, 12, 25)
def binary?(n)
if(n < 2)
return true if n == 1
return false
end
@akiradeveloper
akiradeveloper / gist:3761569
Created September 21, 2012 13:49
bitfield struct size
#include <stdio.h>
typedef unsigned char typeS;
struct S {
typeS a: 1;
typeS b: 1;
typeS c: 1;
typeS d: 1;
typeS e: 1;
typeS f: 1;
@akiradeveloper
akiradeveloper / output
Created September 21, 2012 14:20
_Bool mistery
#include <stdio.h>
/*
using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.4.5-8' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr --program-suffix=-4.4 --enable-shared --enable-multiarch --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc --with-arch-32=i586 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 (Debian 4.4.5-8)
*/
@akiradeveloper
akiradeveloper / gist:3806330
Created September 30, 2012 09:30
Reader Monad chaining
import Control.Monad.Instances
let f = (*2) >>= ¥a -> (*3) >>= ¥b -> return (a+b) :: Int -> Int
f 10 #=> 50
Memo:
Reader monad has a context that is passed some value and returns a value - a function, function as monad is called Reader. We can combinate functions and construct a bigger one.
(*2) is a function and therefore a monadic value in Reader monad context.
@akiradeveloper
akiradeveloper / gist:3806530
Created September 30, 2012 11:39
private member emulation in C language
// What is the best solution? ...
// using macro .. ?
#include <stdio.h>
struct PrivateHoge {
int y;
};
struct PublicHoge {
@akiradeveloper
akiradeveloper / gist:3850387
Created October 8, 2012 02:16
Applicative Style
Prelude Control.Applicative> :t (<*)
(<*) :: Applicative f => f a -> f b -> f a
Prelude Control.Applicative> :t (*>)
(*>) :: Applicative f => f a -> f b -> f b
Prelude Control.Applicative> :t (<**>)
(<**>) :: Applicative f => f a -> f (a -> b) -> f b
Prelude Control.Applicative> :t (<$)
(<$) :: Functor f => a -> f b -> f a
@akiradeveloper
akiradeveloper / gist:4527215
Created January 14, 2013 01:39
dm-lcに実装したdm_safe_io_retryがうまくいった. ioの長さも表示させた方がいいかな.
[ 2905.041900] device-mapper: lc: io err occurs err(-5), err_bits(1)
[ 2905.041903] device-mapper: lc: rw(1), sector(100160376), dev(253:1)
[ 2906.041540] device-mapper: lc: io has just turned fail to OK.
[ 2906.041546] device-mapper: lc: rw(1), sector(100160376), dev(253:1)
@akiradeveloper
akiradeveloper / gist:4696341
Created February 2, 2013 06:51
2.6.30が悲しみのカーネルであることを分かっていただきたい・・・. こんなのサポートしたくないよ・・・.
--2.6.30.1--
/home/akira/src/dm-lc/dm-lc.c:154: error: implicit declaration of function 'INIT_WORK_ONSTACK'
/home/akira/src/dm-lc/dm-lc.c:1665: error: expected declaration specifiers or '...' before 'iterate_devices_callout_fn'
/home/akira/src/dm-lc/dm-lc.c:1671: error: implicit declaration of function 'fn'
/home/akira/src/dm-lc/dm-lc.c:1676: error: implicit declaration of function 'blk_limits_io_min'
/home/akira/src/dm-lc/dm-lc.c:1677: error: implicit declaration of function 'blk_limits_io_opt'
/home/akira/src/dm-lc/dm-lc.c:1709: error: unknown field 'io_hints' specified in initializer
/home/akira/src/dm-lc/dm-lc.c:1710: error: unknown field 'iterate_devices' specified in initializer
/home/akira/src/dm-lc/dm-lc.c:1993: error: too few arguments to function 'dm_io_client_create'
--2.6.32.60--