@@ -331,7 +331,7 @@ func (p *Package) writeDefsFunc(fc, fgo2 *os.File, n *Name) {
331
331
}
332
332
333
333
// Builtins defined in the C prolog.
334
- inProlog := name == "CString" || name == "GoString" || name == "GoStringN" || name == "GoBytes"
334
+ inProlog := name == "CString" || name == "GoString" || name == "GoStringN" || name == "GoBytes" || name == "_CMalloc"
335
335
336
336
if * gccgo {
337
337
// Gccgo style hooks.
@@ -476,9 +476,27 @@ func (p *Package) writeOutput(f *File, srcfile string) {
476
476
fgcc .Close ()
477
477
}
478
478
479
+ // fixGo convers the internal Name.Go field into the name we should show
480
+ // to users in error messages. There's only one for now: on input we rewrite
481
+ // C.malloc into C._CMalloc, so change it back here.
482
+ func fixGo (name string ) string {
483
+ if name == "_CMalloc" {
484
+ return "malloc"
485
+ }
486
+ return name
487
+ }
488
+
489
+ var isBuiltin = map [string ]bool {
490
+ "_Cfunc_CString" : true ,
491
+ "_Cfunc_GoString" : true ,
492
+ "_Cfunc_GoStringN" : true ,
493
+ "_Cfunc_GoBytes" : true ,
494
+ "_Cfunc__CMalloc" : true ,
495
+ }
496
+
479
497
func (p * Package ) writeOutputFunc (fgcc * os.File , n * Name ) {
480
498
name := n .Mangle
481
- if name == "_Cfunc_CString" || name == "_Cfunc_GoString" || name == "_Cfunc_GoStringN" || name == "_Cfunc_GoBytes" || p .Written [name ] {
499
+ if isBuiltin [ name ] || p .Written [name ] {
482
500
// The builtins are already defined in the C prolog, and we don't
483
501
// want to duplicate function definitions we've already done.
484
502
return
@@ -1101,6 +1119,8 @@ __cgo_size_assert(double, 8)
1101
1119
`
1102
1120
1103
1121
const builtinProlog = `
1122
+ #include <sys/types.h> /* for size_t below */
1123
+
1104
1124
/* Define intgo when compiling with GCC. */
1105
1125
#ifdef __PTRDIFF_TYPE__
1106
1126
typedef __PTRDIFF_TYPE__ intgo;
@@ -1116,6 +1136,7 @@ _GoString_ GoString(char *p);
1116
1136
_GoString_ GoStringN(char *p, int l);
1117
1137
_GoBytes_ GoBytes(void *p, int n);
1118
1138
char *CString(_GoString_);
1139
+ void *_CMalloc(size_t);
1119
1140
`
1120
1141
1121
1142
const cProlog = `
@@ -1153,6 +1174,13 @@ void
1153
1174
p[s.len] = 0;
1154
1175
FLUSH(&p);
1155
1176
}
1177
+
1178
+ void
1179
+ ·_Cfunc__CMalloc(uintptr n, int8 *p)
1180
+ {
1181
+ p = runtime·cmalloc(n);
1182
+ FLUSH(&p);
1183
+ }
1156
1184
`
1157
1185
1158
1186
const cPrologGccgo = `
@@ -1193,6 +1221,14 @@ Slice GoBytes(char *p, int32_t n) {
1193
1221
struct __go_string s = { (const unsigned char *)p, n };
1194
1222
return __go_string_to_byte_array(s);
1195
1223
}
1224
+
1225
+ extern void runtime_throw(const char *):
1226
+ void *Cmalloc(size_t n) {
1227
+ void *p = malloc(n);
1228
+ if(p == NULL)
1229
+ runtime_throw("runtime: C malloc failed");
1230
+ return p;
1231
+ }
1196
1232
`
1197
1233
1198
1234
func (p * Package ) gccExportHeaderProlog () string {
0 commit comments