Created
December 31, 2011 08:18
-
-
Save erickt/1543358 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<stdio.h> | |
#include<string.h> | |
void print_str(char* s) { | |
printf("print_str: %p '%s'\n", s, s); | |
} | |
typedef struct { void* p0; void* p1; unsigned u; } foo; | |
foo mk_foo() { | |
foo f; | |
f.p0 = strdup("hello"); | |
f.p1 = strdup("foo"); | |
f.u = 1; | |
printf("mk_foo: sizeof(foo) == %lu f.p0: %p f.p1: %p\n", | |
sizeof(f), | |
f.p0, | |
f.p1); | |
return f; | |
} | |
void print_foop(foo* f) { | |
printf("print_foop:\nf.p0: %p '%s'\nf.p1: %p '%s'\nf.u:%u\n\n", | |
f->p0, | |
(char*)f->p0, | |
f->p1, | |
(char*)f->p1, | |
f->u); | |
} | |
void print_foo(foo f) { | |
printf("print_foo:\nf.p0: %p '%s'\nf.p1: %p '%s'\nf.u:%u\n\n", | |
f.p0, | |
(char*)f.p0, | |
f.p1, | |
(char*)f.p1, | |
f.u); | |
} | |
typedef struct { void* p; unsigned u; } bar; | |
bar mk_bar() { | |
bar f; | |
f.p = strdup("hello bar"); | |
f.u = 2; | |
printf("mk_foo: sizeof(foo) == %lu %p\n", sizeof(f), f.p); | |
return f; | |
} | |
void print_barp(bar* b) { | |
printf("print_barp:\nb.p: %p '%s'\nb.u: %u\n", | |
b->p, | |
(char*)b->p, | |
b->u); | |
} | |
void print_bar(bar b) { | |
printf("print_bar:\nb.p: %p '%s'\nb.u: %u\n", | |
b.p, | |
(char*)b.p, | |
b.u); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sizeof foo: 24 | |
mk_foo: sizeof(foo) == 24 f.p0: 0x101000060 f.p1: 0x101000070 | |
print_str: 0x101000060 'hello' | |
print_str: 0x101000070 'foo' | |
rust: f.p0: 101000060 | |
rust: f.p1: 101000070 | |
rust: f.u: 1 | |
print_foop: | |
f.p0: 0x101000060 'hello' | |
f.p1: 0x101000070 'foo' | |
f.u:1 | |
print_foo: | |
f.p0: 0x100603e60 '?ǀ' | |
f.p1: 0x10180c850 '`' | |
f.u:25216992 | |
sizeof bar: 16 | |
mk_foo: sizeof(foo) == 16 0x101000080 | |
print_str: 0x101000080 'hello bar' | |
rust: b.p: 101000080 | |
rust: b.u: 2 | |
print_barp: | |
b.p: 0x101000080 'hello bar' | |
b.u: 2 | |
print_bar: | |
b.p: 0x101000080 'hello bar' | |
b.u: 2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
all: libfoo.a | |
foo.o: foo.c | |
gcc -c -fPIC -o $@ $< | |
libfoo.a: foo.o | |
ar -r $@ $< |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use std; | |
import std::io; | |
type foo_t = { | |
p0: *ctypes::void, | |
p1: *ctypes::void, | |
u: ctypes::unsigned | |
}; | |
type bar_t = { | |
p: *ctypes::void, | |
u: ctypes::unsigned | |
}; | |
#[link_args = "-L."] | |
#[abi = "cdecl"] | |
native mod foo { | |
fn print_str(s: *ctypes::void); | |
fn mk_foo() -> foo_t; | |
fn print_foo(++f: foo_t); | |
fn print_foop(f: foo_t); | |
fn mk_bar() -> bar_t; | |
fn print_bar(++f: bar_t); | |
fn print_barp(f: bar_t); | |
} | |
fn main() unsafe { | |
io::println(#fmt("sizeof foo: %u\n", sys::size_of::<foo_t>())); | |
let f = foo::mk_foo(); | |
foo::print_str(f.p0); | |
foo::print_str(f.p1); | |
io::println(""); | |
io::println(#fmt("rust: f.p0: %s", int::to_str(unsafe::reinterpret_cast(f.p0), 16u))); | |
io::println(#fmt("rust: f.p1: %s", int::to_str(unsafe::reinterpret_cast(f.p1), 16u))); | |
io::println(#fmt("rust: f.u: %u", f.u as uint)); | |
io::println(""); | |
foo::print_foop(f); | |
foo::print_foo(f); | |
io::println(#fmt("sizeof bar: %u\n", sys::size_of::<bar_t>())); | |
let b = foo::mk_bar(); | |
foo::print_str(b.p); | |
io::println(""); | |
io::println(#fmt("rust: b.p: %s", int::to_str(unsafe::reinterpret_cast(b.p), 16u))); | |
io::println(#fmt("rust: b.u: %u", b.u as uint)); | |
io::println(""); | |
foo::print_barp(b); | |
foo::print_bar(b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment