From b9600256a044061cd2d410a352bd821bc84e4754 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 16 Sep 2014 11:03:11 -0400 Subject: runtime: remove untyped allocation of ParFor Now it's two allocations. I don't see much downside to that, since the two pieces were in different cache lines anyway. Rename 'conservative' to 'cgo_conservative_type' and make clear that _cgo_allocate is the only allowed user. This depends on CL 141490043, which removes the other use of conservative (in defer). LGTM=dvyukov, iant R=khr, dvyukov, iant CC=golang-codereviews, rlh https://codereview.appspot.com/139610043 --- misc/cgo/test/callback.go | 5 ++++ misc/cgo/test/callback_c_gc.c | 49 ++++++++++++++++++++++++++++++++++++++++ misc/cgo/test/callback_c_gccgo.c | 46 +++++++++++++++++++++++++++++++++++++ misc/cgo/test/cgo_test.go | 1 + misc/cgo/test/exports.go | 6 +++++ 5 files changed, 107 insertions(+) (limited to 'misc') diff --git a/misc/cgo/test/callback.go b/misc/cgo/test/callback.go index 281e79494..a7f1a3ecd 100644 --- a/misc/cgo/test/callback.go +++ b/misc/cgo/test/callback.go @@ -9,6 +9,7 @@ void callback(void *f); void callGoFoo(void); void callGoStackCheck(void); void callPanic(void); +void callCgoAllocate(void); */ import "C" @@ -207,6 +208,10 @@ func testPanicFromC(t *testing.T) { C.callPanic() } +func testAllocateFromC(t *testing.T) { + C.callCgoAllocate() // crashes or exits on failure +} + func testCallbackStack(t *testing.T) { // Make cgo call and callback with different amount of stack stack available. // We do not do any explicit checks, just ensure that it does not crash. diff --git a/misc/cgo/test/callback_c_gc.c b/misc/cgo/test/callback_c_gc.c index 8953b74a6..32bfed0c0 100644 --- a/misc/cgo/test/callback_c_gc.c +++ b/misc/cgo/test/callback_c_gc.c @@ -5,11 +5,15 @@ // +build gc #include "_cgo_export.h" +#include +#include +#include /* Test calling panic from C. This is what SWIG does. */ extern void crosscall2(void (*fn)(void *, int), void *, int); extern void _cgo_panic(void *, int); +extern void _cgo_allocate(void *, int); void callPanic(void) @@ -19,3 +23,48 @@ callPanic(void) crosscall2(_cgo_panic, &a, sizeof a); *(int*)1 = 1; } + +/* Test calling cgo_allocate from C. This is what SWIG does. */ + +typedef struct List List; +struct List +{ + List *next; + int x; +}; + +void +callCgoAllocate(void) +{ + int i; + struct { size_t n; void *ret; } a; + List *l, *head, **tail; + + head = 0; + tail = &head; + for(i=0; i<100; i++) { + a.n = sizeof *l; + crosscall2(_cgo_allocate, &a, sizeof a); + l = a.ret; + l->x = i; + l->next = 0; + *tail = l; + tail = &l->next; + } + + gc(); + + l = head; + for(i=0; i<100; i++) { + if(l->x != i) { + fprintf(stderr, "callCgoAllocate: lost memory\n"); + exit(2); + } + l = l->next; + } + if(l != 0) { + fprintf(stderr, "callCgoAllocate: lost memory\n"); + exit(2); + } +} + diff --git a/misc/cgo/test/callback_c_gccgo.c b/misc/cgo/test/callback_c_gccgo.c index 0ea7296c6..d92dca009 100644 --- a/misc/cgo/test/callback_c_gccgo.c +++ b/misc/cgo/test/callback_c_gccgo.c @@ -5,13 +5,59 @@ // +build gccgo #include "_cgo_export.h" +#include +#include +#include /* Test calling panic from C. This is what SWIG does. */ extern void _cgo_panic(const char *); +extern void *_cgo_allocate(size_t); void callPanic(void) { _cgo_panic("panic from C"); } + +/* Test calling cgo_allocate from C. This is what SWIG does. */ + +typedef struct List List; +struct List +{ + List *next; + int x; +}; + +void +callCgoAllocate(void) +{ + int i; + List *l, *head, **tail; + + head = 0; + tail = &head; + for(i=0; i<100; i++) { + l = _cgo_allocate(sizeof *l); + l->x = i; + l->next = 0; + *tail = l; + tail = &l->next; + } + + gc(); + + l = head; + for(i=0; i<100; i++) { + if(l->x != i) { + fprintf(stderr, "callCgoAllocate: lost memory\n"); + exit(2); + } + l = l->next; + } + if(l != 0) { + fprintf(stderr, "callCgoAllocate: lost memory\n"); + exit(2); + } +} + diff --git a/misc/cgo/test/cgo_test.go b/misc/cgo/test/cgo_test.go index 3cc83060f..3783af061 100644 --- a/misc/cgo/test/cgo_test.go +++ b/misc/cgo/test/cgo_test.go @@ -23,6 +23,7 @@ func TestCallbackPanic(t *testing.T) { testCallbackPanic(t) } func TestCallbackPanicLoop(t *testing.T) { testCallbackPanicLoop(t) } func TestCallbackPanicLocked(t *testing.T) { testCallbackPanicLocked(t) } func TestPanicFromC(t *testing.T) { testPanicFromC(t) } +func TestAllocateFromC(t *testing.T) { testAllocateFromC(t) } func TestZeroArgCallback(t *testing.T) { testZeroArgCallback(t) } func TestBlocking(t *testing.T) { testBlocking(t) } func Test1328(t *testing.T) { test1328(t) } diff --git a/misc/cgo/test/exports.go b/misc/cgo/test/exports.go index f96c60b00..4fe1703a6 100644 --- a/misc/cgo/test/exports.go +++ b/misc/cgo/test/exports.go @@ -5,8 +5,14 @@ package cgotest import "C" +import "runtime" //export ReturnIntLong func ReturnIntLong() (int, C.long) { return 1, 2 } + +//export gc +func gc() { + runtime.GC() +} -- cgit v1.2.1