OLD | NEW |
1 // Copyright 2013 The Go Authors. All rights reserved. | 1 // Copyright 2013 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // +build gccgo | 5 // +build gccgo |
6 | 6 |
7 #include "_cgo_export.h" | 7 #include "_cgo_export.h" |
| 8 #include <stdint.h> |
| 9 #include <stdio.h> |
| 10 #include <stdlib.h> |
8 | 11 |
9 /* Test calling panic from C. This is what SWIG does. */ | 12 /* Test calling panic from C. This is what SWIG does. */ |
10 | 13 |
11 extern void _cgo_panic(const char *); | 14 extern void _cgo_panic(const char *); |
| 15 extern void *_cgo_allocate(size_t); |
12 | 16 |
13 void | 17 void |
14 callPanic(void) | 18 callPanic(void) |
15 { | 19 { |
16 _cgo_panic("panic from C"); | 20 _cgo_panic("panic from C"); |
17 } | 21 } |
| 22 |
| 23 /* Test calling cgo_allocate from C. This is what SWIG does. */ |
| 24 |
| 25 typedef struct List List; |
| 26 struct List |
| 27 { |
| 28 List *next; |
| 29 int x; |
| 30 }; |
| 31 |
| 32 void |
| 33 callCgoAllocate(void) |
| 34 { |
| 35 int i; |
| 36 List *l, *head, **tail; |
| 37 ········ |
| 38 // Make sure this doesn't crash. |
| 39 // And make sure it returns non-nil. |
| 40 if(_cgo_allocate(0) == 0) { |
| 41 fprintf(stderr, "callCgoAllocate: alloc 0 returned nil\n"); |
| 42 exit(2); |
| 43 } |
| 44 |
| 45 head = 0; |
| 46 tail = &head; |
| 47 for(i=0; i<100; i++) { |
| 48 l = _cgo_allocate(sizeof *l); |
| 49 l->x = i; |
| 50 l->next = 0; |
| 51 *tail = l; |
| 52 tail = &l->next; |
| 53 } |
| 54 ········ |
| 55 gc(); |
| 56 ········ |
| 57 l = head; |
| 58 for(i=0; i<100; i++) { |
| 59 if(l->x != i) { |
| 60 fprintf(stderr, "callCgoAllocate: lost memory\n"); |
| 61 exit(2); |
| 62 } |
| 63 l = l->next; |
| 64 } |
| 65 if(l != 0) { |
| 66 fprintf(stderr, "callCgoAllocate: lost memory\n"); |
| 67 exit(2); |
| 68 } |
| 69 } |
| 70 |
OLD | NEW |