diff options
author | Ondřej Surý <ondrej@sury.org> | 2012-03-26 16:50:58 +0200 |
---|---|---|
committer | Ondřej Surý <ondrej@sury.org> | 2012-03-26 16:50:58 +0200 |
commit | 519725bb3c075ee2462c929f5997cb068e18466a (patch) | |
tree | 5b162e8488ad147a645048c073577821b4a2bee9 /misc/cgo | |
parent | 842623c5dd2819d980ca9c58048d6bc6ed82475f (diff) | |
download | golang-upstream-weekly/2012.03.22.tar.gz |
Imported Upstream version 2012.03.22upstream-weekly/2012.03.22
Diffstat (limited to 'misc/cgo')
-rw-r--r-- | misc/cgo/gmp/Makefile | 38 | ||||
-rw-r--r-- | misc/cgo/gmp/fib.go | 4 | ||||
-rw-r--r-- | misc/cgo/gmp/gmp.go | 24 | ||||
-rw-r--r-- | misc/cgo/gmp/pi.go | 6 | ||||
-rw-r--r-- | misc/cgo/life/Makefile | 35 | ||||
-rw-r--r-- | misc/cgo/life/main.go | 4 | ||||
-rwxr-xr-x | misc/cgo/life/test.bash | 7 | ||||
-rw-r--r-- | misc/cgo/stdio/Makefile | 17 | ||||
-rw-r--r-- | misc/cgo/stdio/chain.go | 4 | ||||
-rw-r--r-- | misc/cgo/stdio/fib.go | 4 | ||||
-rw-r--r-- | misc/cgo/stdio/file.go | 2 | ||||
-rw-r--r-- | misc/cgo/stdio/hello.go | 4 | ||||
-rwxr-xr-x | misc/cgo/stdio/test.bash | 9 | ||||
-rw-r--r-- | misc/cgo/test/backdoor/backdoor.go | 7 | ||||
-rw-r--r-- | misc/cgo/test/backdoor/runtime.c (renamed from misc/cgo/test/runtime.c) | 4 | ||||
-rw-r--r-- | misc/cgo/test/basic.go | 4 | ||||
-rw-r--r-- | misc/cgo/test/callback.go | 8 | ||||
-rw-r--r-- | misc/cgo/test/callback_c.c | 20 | ||||
-rw-r--r-- | misc/cgo/test/issue1328.go | 2 | ||||
-rw-r--r-- | misc/cgo/test/issue1560.go | 5 | ||||
-rw-r--r-- | misc/cgo/testso/Makefile | 22 | ||||
-rw-r--r-- | misc/cgo/testso/cgoso.go | 5 | ||||
-rw-r--r-- | misc/cgo/testso/cgoso_c.c | 2 | ||||
-rw-r--r-- | misc/cgo/testso/main.go | 4 | ||||
-rwxr-xr-x | misc/cgo/testso/test.bash | 7 |
25 files changed, 100 insertions, 148 deletions
diff --git a/misc/cgo/gmp/Makefile b/misc/cgo/gmp/Makefile deleted file mode 100644 index d9390c146..000000000 --- a/misc/cgo/gmp/Makefile +++ /dev/null @@ -1,38 +0,0 @@ -# Copyright 2009 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. - -include ../../../src/Make.inc - -TARG=gmp - -# Can have plain GOFILES too, but this example doesn't. - -CGOFILES=\ - gmp.go - -CGO_LDFLAGS=-lgmp - -# To add flags necessary for locating the library or its include files, -# set CGO_CFLAGS or CGO_LDFLAGS. For example, to use an -# alternate installation of the library: -# CGO_CFLAGS=-I/home/rsc/gmp32/include -# CGO_LDFLAGS+=-L/home/rsc/gmp32/lib -# Note the += on the second line. - -CLEANFILES+=pi fib - -include ../../../src/Make.pkg - -# Simple test programs - -# Computes 1000 digits of pi; single-threaded. -pi: install pi.go - $(GC) $(GCFLAGS) $(GCIMPORTS) pi.go - $(LD) -o $@ pi.$O - -# Computes 200 Fibonacci numbers; multi-threaded. -fib: install fib.go - $(GC) $(GCFLAGS) $(GCIMPORTS) fib.go - $(LD) -o $@ fib.$O - diff --git a/misc/cgo/gmp/fib.go b/misc/cgo/gmp/fib.go index 3eda39e17..18434beaf 100644 --- a/misc/cgo/gmp/fib.go +++ b/misc/cgo/gmp/fib.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build ignore + // Compute Fibonacci numbers with two goroutines // that pass integers back and forth. No actual // concurrency, just threads and synchronization @@ -10,7 +12,7 @@ package main import ( - big "gmp" + big "." "runtime" ) diff --git a/misc/cgo/gmp/gmp.go b/misc/cgo/gmp/gmp.go index 9325d8bfd..3bcf99151 100644 --- a/misc/cgo/gmp/gmp.go +++ b/misc/cgo/gmp/gmp.go @@ -98,8 +98,20 @@ Go to hang on to a reference to the pointer until C is done with it. */ package gmp -// #include <gmp.h> -// #include <stdlib.h> +/* +#cgo LDFLAGS: -lgmp +#include <gmp.h> +#include <stdlib.h> + +// gmp 5.0.0+ changed the type of the 3rd argument to mp_bitcnt_t, +// so, to support older versions, we wrap these two functions. +void _mpz_mul_2exp(mpz_ptr a, mpz_ptr b, unsigned long n) { + mpz_mul_2exp(a, b, n); +} +void _mpz_div_2exp(mpz_ptr a, mpz_ptr b, unsigned long n) { + mpz_div_2exp(a, b, n); +} +*/ import "C" import ( @@ -182,12 +194,12 @@ func (z *Int) SetInt64(x int64) *Int { func (z *Int) SetString(s string, base int) error { z.doinit() if base < 2 || base > 36 { - return os.EINVAL + return os.ErrInvalid } p := C.CString(s) defer C.free(unsafe.Pointer(p)) if C.mpz_set_str(&z.i[0], p, C.int(base)) < 0 { - return os.EINVAL + return os.ErrInvalid } return nil } @@ -265,7 +277,7 @@ func (z *Int) Mod(x, y *Int) *Int { func (z *Int) Lsh(x *Int, s uint) *Int { x.doinit() z.doinit() - C.mpz_mul_2exp(&z.i[0], &x.i[0], C.mp_bitcnt_t(s)) + C._mpz_mul_2exp(&z.i[0], &x.i[0], C.ulong(s)) return z } @@ -273,7 +285,7 @@ func (z *Int) Lsh(x *Int, s uint) *Int { func (z *Int) Rsh(x *Int, s uint) *Int { x.doinit() z.doinit() - C.mpz_div_2exp(&z.i[0], &x.i[0], C.mp_bitcnt_t(s)) + C._mpz_div_2exp(&z.i[0], &x.i[0], C.ulong(s)) return z } diff --git a/misc/cgo/gmp/pi.go b/misc/cgo/gmp/pi.go index 3e40624cf..1914cf214 100644 --- a/misc/cgo/gmp/pi.go +++ b/misc/cgo/gmp/pi.go @@ -1,3 +1,5 @@ +// +build ignore + /* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: @@ -38,8 +40,8 @@ POSSIBILITY OF SUCH DAMAGE. package main import ( + big "." "fmt" - big "gmp" "runtime" ) @@ -100,5 +102,5 @@ func main() { } } - fmt.Printf("\n%d calls; bit sizes: %d %d %d\n", runtime.Cgocalls(), numer.Len(), accum.Len(), denom.Len()) + fmt.Printf("\n%d calls; bit sizes: %d %d %d\n", runtime.NumCgoCall(), numer.Len(), accum.Len(), denom.Len()) } diff --git a/misc/cgo/life/Makefile b/misc/cgo/life/Makefile deleted file mode 100644 index 1568a67f6..000000000 --- a/misc/cgo/life/Makefile +++ /dev/null @@ -1,35 +0,0 @@ -# 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. - -include ../../../src/Make.inc - -TARG=life - -CGOFILES=\ - life.go\ - -CGO_OFILES=\ - c-life.o\ - -ifeq ($(GOOS),windows) -ifeq ($(GOARCH),amd64) -CGO_OFILES+=\ - lib64_libmingwex_a-wassert.o\ - lib64_libmingw32_a-mingw_helpers.o\ - -lib64_libmingwex_a-wassert.o: - ar -x /mingw/x86_64-w64-mingw32/lib/libmingwex.a lib64_libmingwex_a-wassert.o - -lib64_libmingw32_a-mingw_helpers.o: - ar -x /mingw/x86_64-w64-mingw32/lib/libmingw32.a lib64_libmingw32_a-mingw_helpers.o -endif -endif - -CLEANFILES+=life - -include ../../../src/Make.pkg - -life: install main.go - $(GC) $(GCFLAGS) $(GCIMPORTS) main.go - $(LD) -o $@ main.$O diff --git a/misc/cgo/life/main.go b/misc/cgo/life/main.go index 9cfed434b..47ae0e18c 100644 --- a/misc/cgo/life/main.go +++ b/misc/cgo/life/main.go @@ -2,14 +2,16 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build ignore + // Run the game of life in C using Go for parallelization. package main import ( + "." "flag" "fmt" - "life" ) const MAXDIM = 100 diff --git a/misc/cgo/life/test.bash b/misc/cgo/life/test.bash index 5c5fba1a9..bb483522c 100755 --- a/misc/cgo/life/test.bash +++ b/misc/cgo/life/test.bash @@ -4,8 +4,11 @@ # license that can be found in the LICENSE file. set -e -gomake life +go build -o life main.go + echo '*' life >run.out ./life >>run.out diff run.out golden.out -gomake clean + +rm -f life + diff --git a/misc/cgo/stdio/Makefile b/misc/cgo/stdio/Makefile deleted file mode 100644 index 586132b3c..000000000 --- a/misc/cgo/stdio/Makefile +++ /dev/null @@ -1,17 +0,0 @@ -# Copyright 2009 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. - -include ../../../src/Make.inc - -TARG=stdio -CGOFILES=\ - file.go\ - -CLEANFILES+=hello fib chain run.out - -include ../../../src/Make.pkg - -%: install %.go - $(GC) $(GCFLAGS) $(GCIMPORTS) $*.go - $(LD) -o $@ $*.$O diff --git a/misc/cgo/stdio/chain.go b/misc/cgo/stdio/chain.go index c188b2dd9..1cf0b1fe5 100644 --- a/misc/cgo/stdio/chain.go +++ b/misc/cgo/stdio/chain.go @@ -2,13 +2,15 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build ignore + // Pass numbers along a chain of threads. package main import ( + "../stdio" "runtime" - "stdio" "strconv" ) diff --git a/misc/cgo/stdio/fib.go b/misc/cgo/stdio/fib.go index 431d9cefe..6d3ccfd52 100644 --- a/misc/cgo/stdio/fib.go +++ b/misc/cgo/stdio/fib.go @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build ignore + // Compute Fibonacci numbers with two goroutines // that pass integers back and forth. No actual // concurrency, just threads and synchronization @@ -10,8 +12,8 @@ package main import ( + "../stdio" "runtime" - "stdio" "strconv" ) diff --git a/misc/cgo/stdio/file.go b/misc/cgo/stdio/file.go index ab1e88436..6e7d479ad 100644 --- a/misc/cgo/stdio/file.go +++ b/misc/cgo/stdio/file.go @@ -28,7 +28,7 @@ var Stderr = (*File)(C.stderr) // Test reference to library symbol. // Stdout and stderr are too special to be a reliable test. -var myerr = C.sys_errlist +//var = C.environ func (f *File) WriteString(s string) { p := C.CString(s) diff --git a/misc/cgo/stdio/hello.go b/misc/cgo/stdio/hello.go index 58fc6d574..4ab3c7447 100644 --- a/misc/cgo/stdio/hello.go +++ b/misc/cgo/stdio/hello.go @@ -2,9 +2,11 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build ignore + package main -import "stdio" +import "../stdio" func main() { stdio.Stdout.WriteString(stdio.Greeting + "\n") diff --git a/misc/cgo/stdio/test.bash b/misc/cgo/stdio/test.bash index 82e3f7b45..21829fa31 100755 --- a/misc/cgo/stdio/test.bash +++ b/misc/cgo/stdio/test.bash @@ -4,7 +4,10 @@ # license that can be found in the LICENSE file. set -e -gomake hello fib chain +go build hello.go +go build fib.go +go build chain.go + echo '*' hello >run.out ./hello >>run.out echo '*' fib >>run.out @@ -12,4 +15,6 @@ echo '*' fib >>run.out echo '*' chain >>run.out ./chain >>run.out diff run.out golden.out -gomake clean + +rm -f hello fib chain + diff --git a/misc/cgo/test/backdoor/backdoor.go b/misc/cgo/test/backdoor/backdoor.go new file mode 100644 index 000000000..efe4f01f4 --- /dev/null +++ b/misc/cgo/test/backdoor/backdoor.go @@ -0,0 +1,7 @@ +// Copyright 2012 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. + +package backdoor + +func LockedOSThread() bool // in runtime.c diff --git a/misc/cgo/test/runtime.c b/misc/cgo/test/backdoor/runtime.c index e087c7622..54e6a1ef8 100644 --- a/misc/cgo/test/runtime.c +++ b/misc/cgo/test/backdoor/runtime.c @@ -3,6 +3,8 @@ // license that can be found in the LICENSE file. // Expose some runtime functions for testing. +// Must be in a non-cgo-using package so that +// the go command compiles this file with 6c, not gcc. typedef char bool; @@ -14,7 +16,7 @@ FLUSH(void*) } void -·lockedOSThread(bool b) +·LockedOSThread(bool b) { b = runtime·lockedOSThread(); FLUSH(&b); diff --git a/misc/cgo/test/basic.go b/misc/cgo/test/basic.go index cd6d88168..70ec5e43a 100644 --- a/misc/cgo/test/basic.go +++ b/misc/cgo/test/basic.go @@ -55,7 +55,7 @@ int add(int x, int y) { */ import "C" import ( - "os" + "syscall" "testing" "unsafe" ) @@ -110,7 +110,7 @@ func testErrno(t *testing.T) { C.fclose(f) t.Fatalf("C.fopen: should fail") } - if err != os.ENOENT { + if err != syscall.ENOENT { t.Fatalf("C.fopen: unexpected error: %v", err) } } diff --git a/misc/cgo/test/callback.go b/misc/cgo/test/callback.go index ef852561b..e6a1462b3 100644 --- a/misc/cgo/test/callback.go +++ b/misc/cgo/test/callback.go @@ -6,14 +6,12 @@ package cgotest /* void callback(void *f); -void callGoFoo(void) { - extern void goFoo(void); - goFoo(); -} +void callGoFoo(void); */ import "C" import ( + "./backdoor" "runtime" "testing" "unsafe" @@ -43,7 +41,7 @@ func testCallbackGC(t *testing.T) { nestedCall(runtime.GC) } -func lockedOSThread() bool // in runtime.c +var lockedOSThread = backdoor.LockedOSThread func testCallbackPanic(t *testing.T) { // Make sure panic during callback unwinds properly. diff --git a/misc/cgo/test/callback_c.c b/misc/cgo/test/callback_c.c index c296d70e0..47f07301b 100644 --- a/misc/cgo/test/callback_c.c +++ b/misc/cgo/test/callback_c.c @@ -15,3 +15,23 @@ callback(void *f) goCallback(f); data[sizeof(data)-1] = 0; } + +void +callGoFoo(void) +{ + extern void goFoo(void); + goFoo(); +} + +void +IntoC(void) +{ + BackIntoGo(); +} + +void +twoSleep(int n) +{ + BackgroundSleep(n); + sleep(n); +} diff --git a/misc/cgo/test/issue1328.go b/misc/cgo/test/issue1328.go index e01207dd9..e1796d6f7 100644 --- a/misc/cgo/test/issue1328.go +++ b/misc/cgo/test/issue1328.go @@ -7,7 +7,7 @@ package cgotest import "testing" // extern void BackIntoGo(void); -// void IntoC() { BackIntoGo(); } +// void IntoC(void); import "C" //export BackIntoGo diff --git a/misc/cgo/test/issue1560.go b/misc/cgo/test/issue1560.go index 833b14ae6..3faa966e7 100644 --- a/misc/cgo/test/issue1560.go +++ b/misc/cgo/test/issue1560.go @@ -10,10 +10,7 @@ package cgotest unsigned int sleep(unsigned int seconds); extern void BackgroundSleep(int); -void twoSleep(int n) { - BackgroundSleep(n); - sleep(n); -} +void twoSleep(int); */ import "C" diff --git a/misc/cgo/testso/Makefile b/misc/cgo/testso/Makefile deleted file mode 100644 index e472cf212..000000000 --- a/misc/cgo/testso/Makefile +++ /dev/null @@ -1,22 +0,0 @@ -# Copyright 2011 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. - -include ../../../src/Make.inc - -TARG=cgosotest - -CGO_DEPS+=libcgoso.so -CGO_LDFLAGS+=-lcgoso -L. -CLEANFILES+=out libcgoso.so -CGOFILES=\ - cgoso.go\ - -include ../../../src/Make.pkg - -libcgoso.so: cgoso_c.c - gcc cgoso_c.c -fPIC -o $@ $(_CGO_CFLAGS_$(GOARCH)) $(_CGO_LDFLAGS_$(GOOS)) - -out: install main.go - $(GC) $(GCFLAGS) $(GCIMPORTS) main.go - $(LD) -o $@ main.$O diff --git a/misc/cgo/testso/cgoso.go b/misc/cgo/testso/cgoso.go index 6eb9f40e3..44fb616c1 100644 --- a/misc/cgo/testso/cgoso.go +++ b/misc/cgo/testso/cgoso.go @@ -4,7 +4,10 @@ package cgosotest -//void sofunc(void); +/* +#cgo LDFLAGS: -L. -lcgosotest +void sofunc(void); +*/ import "C" func Test() { diff --git a/misc/cgo/testso/cgoso_c.c b/misc/cgo/testso/cgoso_c.c index e29f7e807..8c15a6b9f 100644 --- a/misc/cgo/testso/cgoso_c.c +++ b/misc/cgo/testso/cgoso_c.c @@ -2,6 +2,8 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build ignore + void sofunc(void) { extern void goCallback(void); diff --git a/misc/cgo/testso/main.go b/misc/cgo/testso/main.go index 672ab262b..88aa4322d 100644 --- a/misc/cgo/testso/main.go +++ b/misc/cgo/testso/main.go @@ -2,9 +2,11 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. +// +build ignore + package main -import "cgosotest" +import "." func main() { cgosotest.Test() diff --git a/misc/cgo/testso/test.bash b/misc/cgo/testso/test.bash index f275eb572..ecef873c8 100755 --- a/misc/cgo/testso/test.bash +++ b/misc/cgo/testso/test.bash @@ -4,6 +4,7 @@ # license that can be found in the LICENSE file. set -e -gomake out -LD_LIBRARY_PATH=. ./out -gomake clean +gcc $(go env GOGCCFLAGS) -shared -o libcgosotest.so cgoso_c.c +go build main.go +LD_LIBRARY_PATH=. ./main +rm -f libcgosotest.so main |