blob: 92e13b5eb37b536cf1b46ff12928c0c247d5d0c8 [file] [edit]
// Copyright 2023 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 abi
import (
"internal/goarch"
"unsafe"
)
// The first word of every non-empty interface type contains an *ITab.
// It records the underlying concrete type (Type), the interface type it
// is implementing (Inter), and some ancillary information.
//
// allocated in non-garbage-collected memory
type ITab struct {
Inter *InterfaceType
Type *Type
Hash uint32 // copy of Type.Hash. Used for type switches.
Fun [1]uintptr // variable sized. fun[0]==0 means Type does not implement Inter.
}
// Size returns the size of the itab in memory.
func (it *ITab) Size() int {
size := int(unsafe.Sizeof(ITab{}))
if it.Fun[0] == 0 {
return size
}
return size + (len(it.Inter.Methods)-1)*goarch.PtrSize
}
// EmptyInterface describes the layout of a "interface{}" or a "any."
// These are represented differently than non-empty interface, as the first
// word always points to an abi.Type.
type EmptyInterface struct {
Type *Type
Data unsafe.Pointer
}
// NonEmptyInterface describes the layout of an interface that contains any methods.
type NonEmptyInterface struct {
ITab *ITab
Data unsafe.Pointer
}
// CommonInterface describes the layout of both [EmptyInterface] and [NonEmptyInterface].
type CommonInterface struct {
// Either an *ITab or a *Type, unexported to avoid accidental use.
_ unsafe.Pointer
Data unsafe.Pointer
}