iface.go 9.64 KB
Newer Older
1 2 3 4 5 6
// Copyright 2014 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 runtime

7
import "unsafe"
8 9 10 11 12 13

const (
	hashSize = 1009
)

var (
Russ Cox's avatar
Russ Cox committed
14
	ifaceLock mutex // lock for accessing hash
15 16 17 18 19 20 21 22 23 24 25 26
	hash      [hashSize]*itab
)

// fInterface is our standard non-empty interface.  We use it instead
// of interface{f()} in function prototypes because gofmt insists on
// putting lots of newlines in the otherwise concise interface{f()}.
type fInterface interface {
	f()
}

func getitab(inter *interfacetype, typ *_type, canfail bool) *itab {
	if len(inter.mhdr) == 0 {
27
		throw("internal error - misuse of itab")
28 29 30 31 32 33 34 35
	}

	// easy case
	x := typ.x
	if x == nil {
		if canfail {
			return nil
		}
36
		panic(&TypeAssertionError{"", *typ._string, *inter.typ._string, *inter.mhdr[0].name})
37 38 39 40 41 42 43 44 45 46 47 48 49 50
	}

	// compiler has provided some good hash codes for us.
	h := inter.typ.hash
	h += 17 * typ.hash
	// TODO(rsc): h += 23 * x.mhash ?
	h %= hashSize

	// look twice - once without lock, once with.
	// common case will be no lock contention.
	var m *itab
	var locked int
	for locked = 0; locked < 2; locked++ {
		if locked != 0 {
Russ Cox's avatar
Russ Cox committed
51
			lock(&ifaceLock)
52
		}
53
		for m = (*itab)(atomicloadp(unsafe.Pointer(&hash[h]))); m != nil; m = m.link {
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
			if m.inter == inter && m._type == typ {
				if m.bad != 0 {
					m = nil
					if !canfail {
						// this can only happen if the conversion
						// was already done once using the , ok form
						// and we have a cached negative result.
						// the cached result doesn't record which
						// interface function was missing, so jump
						// down to the interface check, which will
						// do more work but give a better error.
						goto search
					}
				}
				if locked != 0 {
Russ Cox's avatar
Russ Cox committed
69
					unlock(&ifaceLock)
70 71 72 73 74 75
				}
				return m
			}
		}
	}

76
	m = (*itab)(persistentalloc(unsafe.Sizeof(itab{})+uintptr(len(inter.mhdr)-1)*ptrSize, 0, &memstats.other_sys))
77 78 79 80 81 82 83 84 85 86 87 88
	m.inter = inter
	m._type = typ

search:
	// both inter and typ have method sorted by name,
	// and interface names are unique,
	// so can iterate over both in lock step;
	// the loop is O(ni+nt) not O(ni*nt).
	ni := len(inter.mhdr)
	nt := len(x.mhdr)
	j := 0
	for k := 0; k < ni; k++ {
89
		i := &inter.mhdr[k]
90 91 92 93
		iname := i.name
		ipkgpath := i.pkgpath
		itype := i._type
		for ; j < nt; j++ {
94
			t := &x.mhdr[j]
95
			if t.mtyp == itype && (t.name == iname || *t.name == *iname) && t.pkgpath == ipkgpath {
96
				if m != nil {
97
					*(*unsafe.Pointer)(add(unsafe.Pointer(&m.fun[0]), uintptr(k)*ptrSize)) = t.ifn
98 99 100 101 102 103 104
				}
				goto nextimethod
			}
		}
		// didn't find method
		if !canfail {
			if locked != 0 {
Russ Cox's avatar
Russ Cox committed
105
				unlock(&ifaceLock)
106 107 108 109 110 111 112 113
			}
			panic(&TypeAssertionError{"", *typ._string, *inter.typ._string, *iname})
		}
		m.bad = 1
		break
	nextimethod:
	}
	if locked == 0 {
114
		throw("invalid itab locking")
115 116
	}
	m.link = hash[h]
117
	atomicstorep(unsafe.Pointer(&hash[h]), unsafe.Pointer(m))
Russ Cox's avatar
Russ Cox committed
118
	unlock(&ifaceLock)
119 120 121 122 123 124 125 126
	if m.bad != 0 {
		return nil
	}
	return m
}

func typ2Itab(t *_type, inter *interfacetype, cache **itab) *itab {
	tab := getitab(inter, t, false)
127
	atomicstorep(unsafe.Pointer(cache), unsafe.Pointer(tab))
128 129 130
	return tab
}

131
func convT2E(t *_type, elem unsafe.Pointer, x unsafe.Pointer) (e interface{}) {
132
	ep := (*eface)(unsafe.Pointer(&e))
133
	if isDirectIface(t) {
134
		ep._type = t
135
		typedmemmove(t, unsafe.Pointer(&ep.data), elem)
136
	} else {
137 138 139
		if x == nil {
			x = newobject(t)
		}
140 141
		// TODO: We allocate a zeroed object only to overwrite it with
		// actual data.  Figure out how to avoid zeroing.  Also below in convT2I.
142
		typedmemmove(t, x, elem)
143 144 145 146 147 148
		ep._type = t
		ep.data = x
	}
	return
}

149
func convT2I(t *_type, inter *interfacetype, cache **itab, elem unsafe.Pointer, x unsafe.Pointer) (i fInterface) {
150
	tab := (*itab)(atomicloadp(unsafe.Pointer(cache)))
151 152
	if tab == nil {
		tab = getitab(inter, t, false)
153
		atomicstorep(unsafe.Pointer(cache), unsafe.Pointer(tab))
154 155
	}
	pi := (*iface)(unsafe.Pointer(&i))
156
	if isDirectIface(t) {
157
		pi.tab = tab
158
		typedmemmove(t, unsafe.Pointer(&pi.data), elem)
159
	} else {
160 161 162
		if x == nil {
			x = newobject(t)
		}
163
		typedmemmove(t, x, elem)
164 165 166 167 168 169
		pi.tab = tab
		pi.data = x
	}
	return
}

170 171 172 173 174 175 176 177
func panicdottype(have, want, iface *_type) {
	haveString := ""
	if have != nil {
		haveString = *have._string
	}
	panic(&TypeAssertionError{*iface._string, haveString, *want._string, ""})
}

178
func assertI2T(t *_type, i fInterface, r unsafe.Pointer) {
179 180 181 182 183 184 185 186
	ip := (*iface)(unsafe.Pointer(&i))
	tab := ip.tab
	if tab == nil {
		panic(&TypeAssertionError{"", "", *t._string, ""})
	}
	if tab._type != t {
		panic(&TypeAssertionError{*tab.inter.typ._string, *tab._type._string, *t._string, ""})
	}
187 188 189 190 191 192
	if r != nil {
		if isDirectIface(t) {
			writebarrierptr((*uintptr)(r), uintptr(ip.data))
		} else {
			typedmemmove(t, r, ip.data)
		}
193 194 195
	}
}

196
func assertI2T2(t *_type, i fInterface, r unsafe.Pointer) bool {
197 198 199
	ip := (*iface)(unsafe.Pointer(&i))
	tab := ip.tab
	if tab == nil || tab._type != t {
200 201 202 203
		if r != nil {
			memclr(r, uintptr(t.size))
		}
		return false
204
	}
205 206 207 208 209 210
	if r != nil {
		if isDirectIface(t) {
			writebarrierptr((*uintptr)(r), uintptr(ip.data))
		} else {
			typedmemmove(t, r, ip.data)
		}
211
	}
212
	return true
213 214
}

215
func assertE2T(t *_type, e interface{}, r unsafe.Pointer) {
216 217 218 219 220 221 222
	ep := (*eface)(unsafe.Pointer(&e))
	if ep._type == nil {
		panic(&TypeAssertionError{"", "", *t._string, ""})
	}
	if ep._type != t {
		panic(&TypeAssertionError{"", *ep._type._string, *t._string, ""})
	}
223 224 225 226 227 228
	if r != nil {
		if isDirectIface(t) {
			writebarrierptr((*uintptr)(r), uintptr(ep.data))
		} else {
			typedmemmove(t, r, ep.data)
		}
229 230 231
	}
}

232 233
var testingAssertE2T2GC bool

234
// The compiler ensures that r is non-nil.
235
func assertE2T2(t *_type, e interface{}, r unsafe.Pointer) bool {
236 237 238
	if testingAssertE2T2GC {
		GC()
	}
239 240
	ep := (*eface)(unsafe.Pointer(&e))
	if ep._type != t {
241
		memclr(r, uintptr(t.size))
242
		return false
243
	}
244 245 246 247
	if isDirectIface(t) {
		writebarrierptr((*uintptr)(r), uintptr(ep.data))
	} else {
		typedmemmove(t, r, ep.data)
248
	}
249
	return true
250 251 252 253 254 255 256 257 258 259 260 261 262 263
}

func convI2E(i fInterface) (r interface{}) {
	ip := (*iface)(unsafe.Pointer(&i))
	tab := ip.tab
	if tab == nil {
		return
	}
	rp := (*eface)(unsafe.Pointer(&r))
	rp._type = tab._type
	rp.data = ip.data
	return
}

264
func assertI2E(inter *interfacetype, i fInterface, r *interface{}) {
265 266 267 268 269 270
	ip := (*iface)(unsafe.Pointer(&i))
	tab := ip.tab
	if tab == nil {
		// explicit conversions require non-nil interface value.
		panic(&TypeAssertionError{"", "", *inter.typ._string, ""})
	}
271
	rp := (*eface)(unsafe.Pointer(r))
272 273 274 275 276
	rp._type = tab._type
	rp.data = ip.data
	return
}

277
// The compiler ensures that r is non-nil.
278
func assertI2E2(inter *interfacetype, i fInterface, r *interface{}) bool {
279 280 281
	ip := (*iface)(unsafe.Pointer(&i))
	tab := ip.tab
	if tab == nil {
282
		return false
283
	}
284 285 286
	rp := (*eface)(unsafe.Pointer(r))
	rp._type = tab._type
	rp.data = ip.data
287
	return true
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
}

func convI2I(inter *interfacetype, i fInterface) (r fInterface) {
	ip := (*iface)(unsafe.Pointer(&i))
	tab := ip.tab
	if tab == nil {
		return
	}
	rp := (*iface)(unsafe.Pointer(&r))
	if tab.inter == inter {
		rp.tab = tab
		rp.data = ip.data
		return
	}
	rp.tab = getitab(inter, tab._type, false)
	rp.data = ip.data
	return
}

307
func assertI2I(inter *interfacetype, i fInterface, r *fInterface) {
308 309 310 311 312 313
	ip := (*iface)(unsafe.Pointer(&i))
	tab := ip.tab
	if tab == nil {
		// explicit conversions require non-nil interface value.
		panic(&TypeAssertionError{"", "", *inter.typ._string, ""})
	}
314
	rp := (*iface)(unsafe.Pointer(r))
315 316 317 318 319 320 321 322 323
	if tab.inter == inter {
		rp.tab = tab
		rp.data = ip.data
		return
	}
	rp.tab = getitab(inter, tab._type, false)
	rp.data = ip.data
}

324
func assertI2I2(inter *interfacetype, i fInterface, r *fInterface) bool {
325 326 327
	ip := (*iface)(unsafe.Pointer(&i))
	tab := ip.tab
	if tab == nil {
328 329 330 331
		if r != nil {
			*r = nil
		}
		return false
332
	}
333 334 335 336 337 338 339 340 341 342 343
	if tab.inter != inter {
		tab = getitab(inter, tab._type, true)
		if tab == nil {
			if r != nil {
				*r = nil
			}
			return false
		}
	}
	if r != nil {
		rp := (*iface)(unsafe.Pointer(r))
344 345 346
		rp.tab = tab
		rp.data = ip.data
	}
347
	return true
348 349
}

350
func assertE2I(inter *interfacetype, e interface{}, r *fInterface) {
351 352 353 354 355 356
	ep := (*eface)(unsafe.Pointer(&e))
	t := ep._type
	if t == nil {
		// explicit conversions require non-nil interface value.
		panic(&TypeAssertionError{"", "", *inter.typ._string, ""})
	}
357
	rp := (*iface)(unsafe.Pointer(r))
358 359 360 361
	rp.tab = getitab(inter, t, false)
	rp.data = ep.data
}

362 363
var testingAssertE2I2GC bool

364
func assertE2I2(inter *interfacetype, e interface{}, r *fInterface) bool {
365 366 367
	if testingAssertE2I2GC {
		GC()
	}
368 369 370
	ep := (*eface)(unsafe.Pointer(&e))
	t := ep._type
	if t == nil {
371 372 373 374
		if r != nil {
			*r = nil
		}
		return false
375 376 377
	}
	tab := getitab(inter, t, true)
	if tab == nil {
378 379 380 381
		if r != nil {
			*r = nil
		}
		return false
382
	}
383 384 385 386 387 388
	if r != nil {
		rp := (*iface)(unsafe.Pointer(r))
		rp.tab = tab
		rp.data = ep.data
	}
	return true
389 390
}

Russ Cox's avatar
Russ Cox committed
391
//go:linkname reflect_ifaceE2I reflect.ifaceE2I
392
func reflect_ifaceE2I(inter *interfacetype, e interface{}, dst *fInterface) {
393
	assertE2I(inter, e, dst)
394 395
}

396
func assertE2E(inter *interfacetype, e interface{}, r *interface{}) {
397 398 399 400 401
	ep := (*eface)(unsafe.Pointer(&e))
	if ep._type == nil {
		// explicit conversions require non-nil interface value.
		panic(&TypeAssertionError{"", "", *inter.typ._string, ""})
	}
402
	*r = e
403 404
}

405
// The compiler ensures that r is non-nil.
406
func assertE2E2(inter *interfacetype, e interface{}, r *interface{}) bool {
407 408
	ep := (*eface)(unsafe.Pointer(&e))
	if ep._type == nil {
409
		*r = nil
410
		return false
411
	}
412
	*r = e
413
	return true
414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
}

func ifacethash(i fInterface) uint32 {
	ip := (*iface)(unsafe.Pointer(&i))
	tab := ip.tab
	if tab == nil {
		return 0
	}
	return tab._type.hash
}

func efacethash(e interface{}) uint32 {
	ep := (*eface)(unsafe.Pointer(&e))
	t := ep._type
	if t == nil {
		return 0
	}
	return t.hash
}
433 434

func iterate_itabs(fn func(*itab)) {
Russ Cox's avatar
Russ Cox committed
435
	for _, h := range &hash {
436 437 438 439 440
		for ; h != nil; h = h.link {
			fn(h)
		}
	}
}