github.com/razvanm/vanadium-go-1.3@v0.0.0-20160721203343-4a65068e5915/misc/cgo/test/callback_c_gccgo.c (about)

     1  // Copyright 2013 The Go Authors.  All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // +build gccgo
     6  
     7  #include "_cgo_export.h"
     8  #include <stdint.h>
     9  #include <stdio.h>
    10  #include <stdlib.h>
    11  
    12  /* Test calling panic from C.  This is what SWIG does.  */
    13  
    14  extern void _cgo_panic(const char *);
    15  extern void *_cgo_allocate(size_t);
    16  
    17  void
    18  callPanic(void)
    19  {
    20  	_cgo_panic("panic from C");
    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