mirror of
https://github.com/golang/go
synced 2024-11-23 11:50:09 -07:00
fc46931442
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://golang.org/cl/139610043
71 lines
1.2 KiB
C
71 lines
1.2 KiB
C
// Copyright 2013 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.
|
|
|
|
// +build gc
|
|
|
|
#include "_cgo_export.h"
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
|
|
/* 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)
|
|
{
|
|
struct { const char *p; } a;
|
|
a.p = "panic from C";
|
|
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);
|
|
}
|
|
}
|
|
|