Skip to content

Commit 25363de

Browse files
committed
doc: add doc about C types that we map to uintptr instead of ptr
Update #22906 Update #21897 Change-Id: I73709b2fdac6981d4bc2f7dab0767f2dd7be3be5 Reviewed-on: https://go-review.googlesource.com/82917 Reviewed-by: Brad Fitzpatrick <[email protected]>
1 parent 36aa2b0 commit 25363de

File tree

2 files changed

+46
-4
lines changed

2 files changed

+46
-4
lines changed

doc/go1.10.html

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,6 @@ <h3 id="cgo">Cgo</h3>
262262

263263
<p>
264264
TODO: CL 70890 "permit passing string values directly between Go and C."
265-
<br>
266-
TODO: CL 66332 "special case C ptr types to use uintptr."
267265
</p>
268266

269267
<p>
@@ -279,6 +277,31 @@ <h3 id="cgo">Cgo</h3>
279277
variable or else the built-in default.
280278
</p>
281279

280+
<p>
281+
Cgo now translates some C types that would normally map to a pointer
282+
type in Go, to a <code>uintptr</code> instead. These types include
283+
the <code>CFTypeRef</code> hierarchy in Darwin's CoreFoundation
284+
framework and the <code>jobject</code> hierarchy in Java's JNI
285+
interface.
286+
</p>
287+
288+
<p>
289+
These types must be <code>uintptr</code> on the Go side because they
290+
would otherwise confuse the Go garbage collector; they are sometimes
291+
not really pointers but data structures encoded in a pointer type.
292+
</p>
293+
294+
<p>
295+
Because of this change, values of the affected types need to be
296+
zero-initialized with the constant <code>0</code> instead of the
297+
constant <code>nil</code>. Go 1.10 provides <code>gofix</code>
298+
modules to help with that rewrite:
299+
<pre>
300+
go tool fix -r cftype <pkg>
301+
go tool fix -r jni <pkg>
302+
</pre>
303+
</p>
304+
282305
<p>
283306
For more details, see the <a href="/cmd/cgo/">cgo documentation</a>.
284307
</p>

src/cmd/cgo/doc.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -403,16 +403,35 @@ the CF*Ref types from the CoreFoundation library on Darwin, including:
403403
CFXMLParserRef
404404
CFXMLTreeRef
405405
406+
Also the object types from Java's JNI interface:
407+
408+
jobject
409+
jclass
410+
jthrowable
411+
jstring
412+
jarray
413+
jbooleanArray
414+
jbyteArray
415+
jcharArray
416+
jshortArray
417+
jintArray
418+
jlongArray
419+
jfloatArray
420+
jdoubleArray
421+
jobjectArray
422+
jweak
423+
406424
These types are uintptr on the Go side because they would otherwise
407425
confuse the Go garbage collector; they are sometimes not really
408426
pointers but data structures encoded in a pointer type. All operations
409427
on these types must happen in C. The proper constant to initialize an
410428
empty such reference is 0, not nil.
411429
412-
This special case was introduced in Go 1.10. For auto-updating code
413-
from Go 1.9 and earlier, use the cftype rewrite in the Go fix tool:
430+
These special cases were introduced in Go 1.10. For auto-updating code
431+
from Go 1.9 and earlier, use the cftype or jni rewrites in the Go fix tool:
414432
415433
go tool fix -r cftype <pkg>
434+
go tool fix -r jni <pkg>
416435
417436
It will replace nil with 0 in the appropriate places.
418437

0 commit comments

Comments
 (0)