diff options
author | Ondřej Surý <ondrej@sury.org> | 2011-01-17 12:40:45 +0100 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2011-01-17 12:40:45 +0100 |
commit | 3e45412327a2654a77944249962b3652e6142299 (patch) | |
tree | bc3bf69452afa055423cbe0c5cfa8ca357df6ccf /misc/cgo | |
parent | c533680039762cacbc37db8dc7eed074c3e497be (diff) | |
download | golang-3e45412327a2654a77944249962b3652e6142299.tar.gz |
Imported Upstream version 2011.01.12upstream/2011.01.12
Diffstat (limited to 'misc/cgo')
-rw-r--r-- | misc/cgo/gmp/Makefile | 2 | ||||
-rw-r--r-- | misc/cgo/life/Makefile | 20 | ||||
-rw-r--r-- | misc/cgo/life/c-life.c | 2 | ||||
-rw-r--r-- | misc/cgo/life/golden.out | 17 | ||||
-rw-r--r-- | misc/cgo/life/life.go | 2 | ||||
-rw-r--r-- | misc/cgo/life/life.h | 1 | ||||
-rw-r--r-- | misc/cgo/life/main.go | 2 | ||||
-rwxr-xr-x | misc/cgo/life/test.bash | 11 | ||||
-rw-r--r-- | misc/cgo/stdio/Makefile | 11 | ||||
-rw-r--r-- | misc/cgo/stdio/align.go | 78 | ||||
-rw-r--r-- | misc/cgo/stdio/chain.go | 4 | ||||
-rw-r--r-- | misc/cgo/stdio/fib.go | 2 | ||||
-rw-r--r-- | misc/cgo/stdio/file.go | 28 | ||||
-rw-r--r-- | misc/cgo/stdio/hello.go | 23 | ||||
-rwxr-xr-x | misc/cgo/stdio/test.bash | 5 | ||||
-rw-r--r-- | misc/cgo/stdio/test.go | 144 | ||||
-rw-r--r-- | misc/cgo/stdio/test1.go | 29 |
17 files changed, 337 insertions, 44 deletions
diff --git a/misc/cgo/gmp/Makefile b/misc/cgo/gmp/Makefile index ad5db33c2..fc6209f27 100644 --- a/misc/cgo/gmp/Makefile +++ b/misc/cgo/gmp/Makefile @@ -2,7 +2,7 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -include ../../../src/Make.$(GOARCH) +include ../../../src/Make.inc TARG=gmp diff --git a/misc/cgo/life/Makefile b/misc/cgo/life/Makefile index cbcdc9927..5a10380ed 100644 --- a/misc/cgo/life/Makefile +++ b/misc/cgo/life/Makefile @@ -2,30 +2,20 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -include ../../../src/Make.$(GOARCH) +include ../../../src/Make.inc TARG=life CGOFILES=\ - life.go + life.go\ -LDPATH_freebsd=-Wl,-R,`pwd` -LDPATH_linux=-Wl,-R,`pwd` -LDPATH_darwin= +CGO_OFILES=\ + c-life.o\ -CGO_LDFLAGS=_cgo_export.o c-life.so $(LDPATH_$(GOOS)) -CGO_DEPS=_cgo_export.o c-life.so - -CLEANFILES += life +CLEANFILES+=life include ../../../src/Make.pkg -c-life.o: c-life.c _cgo_export.h - gcc $(_CGO_CFLAGS_$(GOARCH)) -g -c -fPIC $(CFLAGS) c-life.c - -c-life.so: c-life.o - gcc $(_CGO_CFLAGS_$(GOARCH)) -o $@ c-life.o $(_CGO_LDFLAGS_$(GOOS)) - life: install main.go $(GC) main.go $(LD) -o $@ main.$O diff --git a/misc/cgo/life/c-life.c b/misc/cgo/life/c-life.c index 71555a9c7..657245595 100644 --- a/misc/cgo/life/c-life.c +++ b/misc/cgo/life/c-life.c @@ -6,6 +6,8 @@ #include "life.h" #include "_cgo_export.h" +const int MYCONST = 0; + // Do the actual manipulation of the life board in C. This could be // done easily in Go, we are just using C for demonstration // purposes. diff --git a/misc/cgo/life/golden.out b/misc/cgo/life/golden.out new file mode 100644 index 000000000..539d2106d --- /dev/null +++ b/misc/cgo/life/golden.out @@ -0,0 +1,17 @@ +* life + + + XXX XXX + + + + + + + + XXX XXX + + + + + diff --git a/misc/cgo/life/life.go b/misc/cgo/life/life.go index 036802853..ec000ce3a 100644 --- a/misc/cgo/life/life.go +++ b/misc/cgo/life/life.go @@ -23,7 +23,7 @@ var chans [4]chan bool //export GoStart // Double return value is just for testing. func GoStart(i, xdim, ydim, xstart, xend, ystart, yend C.int, a *C.int, n *C.int) (int, int) { - c := make(chan bool) + c := make(chan bool, int(C.MYCONST)) go func() { C.DoStep(xdim, ydim, xstart, xend, ystart, yend, a, n) c <- true diff --git a/misc/cgo/life/life.h b/misc/cgo/life/life.h index b6e94cf1d..b2011b25f 100644 --- a/misc/cgo/life/life.h +++ b/misc/cgo/life/life.h @@ -4,3 +4,4 @@ extern void Step(int, int, int *, int *); extern void DoStep(int, int, int, int, int, int, int *, int *); +extern const int MYCONST; diff --git a/misc/cgo/life/main.go b/misc/cgo/life/main.go index 7c2c0c73e..9cfed434b 100644 --- a/misc/cgo/life/main.go +++ b/misc/cgo/life/main.go @@ -29,7 +29,7 @@ func main() { } } - life.Run(*gen, *dim, *dim, &a) + life.Run(*gen, *dim, *dim, a[:]) for i := 0; i < *dim; i++ { for j := 0; j < *dim; j++ { diff --git a/misc/cgo/life/test.bash b/misc/cgo/life/test.bash new file mode 100755 index 000000000..5c5fba1a9 --- /dev/null +++ b/misc/cgo/life/test.bash @@ -0,0 +1,11 @@ +#!/bin/sh +# Copyright 2010 The Go Authors. All rights reserved. +# Use of this source code is governed by a BSD-style +# license that can be found in the LICENSE file. + +set -e +gomake life +echo '*' life >run.out +./life >>run.out +diff run.out golden.out +gomake clean diff --git a/misc/cgo/stdio/Makefile b/misc/cgo/stdio/Makefile index 2e3d46631..fc925e607 100644 --- a/misc/cgo/stdio/Makefile +++ b/misc/cgo/stdio/Makefile @@ -2,16 +2,19 @@ # Use of this source code is governed by a BSD-style # license that can be found in the LICENSE file. -include ../../../src/Make.$(GOARCH) +include ../../../src/Make.inc TARG=stdio CGOFILES=\ - file.go + align.go\ + file.go\ + test.go\ + test1.go\ CLEANFILES+=hello fib chain run.out include ../../../src/Make.pkg %: install %.go - $(QUOTED_GOBIN)/$(GC) $*.go - $(QUOTED_GOBIN)/$(LD) -o $@ $*.$O + $(GC) $*.go + $(LD) -o $@ $*.$O diff --git a/misc/cgo/stdio/align.go b/misc/cgo/stdio/align.go new file mode 100644 index 000000000..6cdfd902f --- /dev/null +++ b/misc/cgo/stdio/align.go @@ -0,0 +1,78 @@ +package stdio + +/* +#include <stdio.h> + +typedef unsigned char Uint8; +typedef unsigned short Uint16; + +typedef enum { + MOD1 = 0x0000, + MODX = 0x8000 +} SDLMod; + +typedef enum { + A = 1, + B = 322, + SDLK_LAST +} SDLKey; + +typedef struct SDL_keysym { + Uint8 scancode; + SDLKey sym; + SDLMod mod; + Uint16 unicode; +} SDL_keysym; + +typedef struct SDL_KeyboardEvent { + Uint8 typ; + Uint8 which; + Uint8 state; + SDL_keysym keysym; +} SDL_KeyboardEvent; + +void makeEvent(SDL_KeyboardEvent *event) { + unsigned char *p; + int i; + + p = (unsigned char*)event; + for (i=0; i<sizeof *event; i++) { + p[i] = i; + } +} + +int same(SDL_KeyboardEvent* e, Uint8 typ, Uint8 which, Uint8 state, Uint8 scan, SDLKey sym, SDLMod mod, Uint16 uni) { + return e->typ == typ && e->which == which && e->state == state && e->keysym.scancode == scan && e->keysym.sym == sym && e->keysym.mod == mod && e->keysym.unicode == uni; +} + +void cTest(SDL_KeyboardEvent *event) { + printf("C: %#x %#x %#x %#x %#x %#x %#x\n", event->typ, event->which, event->state, + event->keysym.scancode, event->keysym.sym, event->keysym.mod, event->keysym.unicode); + fflush(stdout); +} + +*/ +import "C" + +import ( + "fmt" + "syscall" +) + +func TestAlign() { + if syscall.ARCH == "amd64" { + // alignment is known to be broken on amd64. + // http://code.google.com/p/go/issues/detail?id=609 + return + } + var evt C.SDL_KeyboardEvent + C.makeEvent(&evt) + if C.same(&evt, evt.typ, evt.which, evt.state, evt.keysym.scancode, evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode) == 0 { + fmt.Println("*** bad alignment") + C.cTest(&evt) + fmt.Printf("Go: %#x %#x %#x %#x %#x %#x %#x\n", + evt.typ, evt.which, evt.state, evt.keysym.scancode, + evt.keysym.sym, evt.keysym.mod, evt.keysym.unicode) + fmt.Println(evt) + } +} diff --git a/misc/cgo/stdio/chain.go b/misc/cgo/stdio/chain.go index dd5e01542..c2b105072 100644 --- a/misc/cgo/stdio/chain.go +++ b/misc/cgo/stdio/chain.go @@ -22,7 +22,7 @@ func link(left chan<- int, right <-chan int) { runtime.LockOSThread() for { v := <-right - stdio.Puts(strconv.Itoa(v)) + stdio.Stdout.WriteString(strconv.Itoa(v) + "\n") left <- 1+v } } @@ -38,6 +38,6 @@ func main() { for i := 0; i < R; i++ { right <- 0 x := <-leftmost - stdio.Puts(strconv.Itoa(x)) + stdio.Stdout.WriteString(strconv.Itoa(x) + "\n") } } diff --git a/misc/cgo/stdio/fib.go b/misc/cgo/stdio/fib.go index 63ae04988..c02e31fd8 100644 --- a/misc/cgo/stdio/fib.go +++ b/misc/cgo/stdio/fib.go @@ -26,7 +26,7 @@ func fibber(c, out chan int64, i int64) { } for { j := <-c - stdio.Puts(strconv.Itoa64(j)) + stdio.Stdout.WriteString(strconv.Itoa64(j) + "\n") out <- j <-out i += j diff --git a/misc/cgo/stdio/file.go b/misc/cgo/stdio/file.go index 7d1f22280..021cbf909 100644 --- a/misc/cgo/stdio/file.go +++ b/misc/cgo/stdio/file.go @@ -10,33 +10,35 @@ see ../gmp/gmp.go. package stdio -// TODO(rsc): Remove fflushstdout when C.fflush(C.stdout) works in cgo. - /* #include <stdio.h> #include <stdlib.h> +#include <sys/stat.h> +#include <errno.h> -void fflushstdout(void) { fflush(stdout); } +char* greeting = "hello, world"; */ import "C" import "unsafe" -/* type File C.FILE var Stdout = (*File)(C.stdout) var Stderr = (*File)(C.stderr) -func (f *File) WriteString(s string) { - p := C.CString(s); - C.fputs(p, (*C.FILE)(f)); - C.free(p); -} -*/ +// Test reference to library symbol. +// Stdout and stderr are too special to be a reliable test. +var myerr = C.sys_errlist -func Puts(s string) { +func (f *File) WriteString(s string) { p := C.CString(s) - C.puts(p) + C.fputs(p, (*C.FILE)(f)) C.free(unsafe.Pointer(p)) - C.fflushstdout() + f.Flush() +} + +func (f *File) Flush() { + C.fflush((*C.FILE)(f)) } + +var Greeting = C.GoString(C.greeting) diff --git a/misc/cgo/stdio/hello.go b/misc/cgo/stdio/hello.go index 47f9de02f..9cb6e6884 100644 --- a/misc/cgo/stdio/hello.go +++ b/misc/cgo/stdio/hello.go @@ -4,9 +4,26 @@ package main -import "stdio" +import ( + "os" + "stdio" +) func main() { - // stdio.Stdout.WriteString("hello, world\n"); - stdio.Puts("hello, world") + stdio.Stdout.WriteString(stdio.Greeting + "\n") + + l := stdio.Atol("123") + if l != 123 { + println("Atol 123: ", l) + panic("bad atol") + } + + n, err := stdio.Strtol("asdf", 123) + if n != 0 || err != os.EINVAL { + println("Strtol: ", n, err) + panic("bad atoi2") + } + + stdio.TestAlign() + stdio.TestEnum() } diff --git a/misc/cgo/stdio/test.bash b/misc/cgo/stdio/test.bash index b8b5f6911..82e3f7b45 100755 --- a/misc/cgo/stdio/test.bash +++ b/misc/cgo/stdio/test.bash @@ -4,8 +4,7 @@ # license that can be found in the LICENSE file. set -e -GOBIN="${GOBIN:-$HOME/bin}" -"$GOBIN"/gomake hello fib chain +gomake hello fib chain echo '*' hello >run.out ./hello >>run.out echo '*' fib >>run.out @@ -13,4 +12,4 @@ echo '*' fib >>run.out echo '*' chain >>run.out ./chain >>run.out diff run.out golden.out -"$GOBIN"/gomake clean +gomake clean diff --git a/misc/cgo/stdio/test.go b/misc/cgo/stdio/test.go new file mode 100644 index 000000000..8f21603ca --- /dev/null +++ b/misc/cgo/stdio/test.go @@ -0,0 +1,144 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file contains test cases for cgo. + +package stdio + +/* +#include <stdio.h> +#include <stdlib.h> +#include <sys/stat.h> +#include <errno.h> + +#define SHIFT(x, y) ((x)<<(y)) +#define KILO SHIFT(1, 10) + +enum E { + Enum1 = 1, + Enum2 = 2, +}; + +typedef unsigned char uuid_t[20]; + +void uuid_generate(uuid_t x) { + x[0] = 0; +} + +struct S { + int x; +}; + +extern enum E myConstFunc(struct S* const ctx, int const id, struct S **const filter); + +enum E myConstFunc(struct S *const ctx, int const id, struct S **const filter) { return 0; } + +// issue 1222 +typedef union { + long align; +} xxpthread_mutex_t; + +struct ibv_async_event { + union { + int x; + } element; +}; + +struct ibv_context { + xxpthread_mutex_t mutex; +}; +*/ +import "C" +import ( + "os" + "unsafe" +) + +const EINVAL = C.EINVAL /* test #define */ + +var KILO = C.KILO + +func uuidgen() { + var uuid C.uuid_t + C.uuid_generate(&uuid[0]) +} + +func Size(name string) (int64, os.Error) { + var st C.struct_stat + p := C.CString(name) + _, err := C.stat(p, &st) + C.free(unsafe.Pointer(p)) + if err != nil { + return 0, err + } + return int64(C.ulong(st.st_size)), nil +} + +func Strtol(s string, base int) (int, os.Error) { + p := C.CString(s) + n, err := C.strtol(p, nil, C.int(base)) + C.free(unsafe.Pointer(p)) + return int(n), err +} + +func Atol(s string) int { + p := C.CString(s) + n := C.atol(p) + C.free(unsafe.Pointer(p)) + return int(n) +} + +func TestConst() { + C.myConstFunc(nil, 0, nil) +} + +func TestEnum() { + if C.Enum1 != 1 || C.Enum2 != 2 { + println("bad enum", C.Enum1, C.Enum2) + } +} + +func TestAtol() { + l := Atol("123") + if l != 123 { + println("Atol 123: ", l) + panic("bad atol") + } +} + +func TestErrno() { + n, err := Strtol("asdf", 123) + if n != 0 || err != os.EINVAL { + println("Strtol: ", n, err) + panic("bad strtol") + } +} + +func TestMultipleAssign() { + p := C.CString("123") + n, m := C.strtol(p, nil, 345), C.strtol(p, nil, 10) + if n != 0 || m != 234 { + println("Strtol x2: ", n, m) + panic("bad strtol x2") + } + C.free(unsafe.Pointer(p)) +} + +var ( + uint = (C.uint)(0) + ulong C.ulong + char C.char +) + +type Context struct { + ctx *C.struct_ibv_context +} + +func Test() { + TestAlign() + TestAtol() + TestEnum() + TestErrno() + TestConst() +} diff --git a/misc/cgo/stdio/test1.go b/misc/cgo/stdio/test1.go new file mode 100644 index 000000000..dce2ef83c --- /dev/null +++ b/misc/cgo/stdio/test1.go @@ -0,0 +1,29 @@ +// Copyright 2010 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +// This file contains test cases for cgo. + +package stdio + +/* +// issue 1222 +typedef union { + long align; +} xxpthread_mutex_t; + +struct ibv_async_event { + union { + int x; + } element; +}; + +struct ibv_context { + xxpthread_mutex_t mutex; +}; +*/ +import "C" + +type AsyncEvent struct { + event C.struct_ibv_async_event +} |