File: seq_darwin.go.support

package info (click to toggle)
golang-golang-x-mobile 0.0~git20250520.a1d9079%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,784 kB
  • sloc: objc: 1,512; java: 1,489; ansic: 1,159; xml: 365; asm: 34; sh: 14; makefile: 5
file content (91 lines) | stat: -rw-r--r-- 2,214 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2016 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.

package main

// Go support functions for Objective-C. Note that this
// file is copied into and compiled with the generated
// bindings.

/*
#cgo CFLAGS: -x objective-c -fobjc-arc -fmodules -fblocks -Werror
#cgo LDFLAGS: -framework Foundation

#include <stdint.h>
#include <stdlib.h>
#include "seq.h"
*/
import "C"

import (
	"unsafe"

	"golang.org/x/mobile/bind/seq"
)

// DestroyRef is called by Objective-C to inform Go it is done with a reference.
//export DestroyRef
func DestroyRef(refnum C.int32_t) {
	seq.Delete(int32(refnum))
}

// encodeString copies a Go string and returns it as a nstring.
func encodeString(s string) C.nstring {
	n := C.int(len(s))
	if n == 0 {
		return C.nstring{}
	}
	ptr := C.malloc(C.size_t(n))
	if ptr == nil {
		panic("encodeString: malloc failed")
	}
	copy((*[1<<31 - 1]byte)(ptr)[:n], s)
	return C.nstring{ptr: ptr, len: n}
}

// decodeString converts a nstring to a Go string. The
// data in str is freed after use.
func decodeString(str C.nstring) string {
	if str.ptr == nil {
		return ""
	}
	s := C.GoStringN((*C.char)(str.ptr), str.len)
	C.free(str.ptr)
	return s
}

// fromSlice converts a slice to a nbyteslice.
// If cpy is set, a malloc'ed copy of the data is returned.
func fromSlice(s []byte, cpy bool) C.nbyteslice {
	if s == nil || len(s) == 0 {
		return C.nbyteslice{}
	}
	ptr, n := unsafe.Pointer(&s[0]), C.int(len(s))
	if cpy {
		nptr := C.malloc(C.size_t(n))
		if nptr == nil {
			panic("fromSlice: malloc failed")
		}
		copy((*[1<<31 - 1]byte)(nptr)[:n], (*[1<<31 - 1]byte)(ptr)[:n])
		ptr = nptr
	}
	return C.nbyteslice{ptr: ptr, len: n}
}

// toSlice takes a nbyteslice and returns a byte slice with the data. If cpy is
// set, the slice contains a copy of the data. If not, the generated Go code
// calls releaseByteSlice after use.
func toSlice(s C.nbyteslice, cpy bool) []byte {
	if s.ptr == nil || s.len == 0 {
		return nil
	}
	var b []byte
	if cpy {
		b = C.GoBytes(s.ptr, C.int(s.len))
		C.free(s.ptr)
	} else {
		b = (*[1<<31 - 1]byte)(unsafe.Pointer(s.ptr))[:s.len:s.len]
	}
	return b
}