Source file src/internal/abi/iface.go
1 // Copyright 2023 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package abi 6 7 import ( 8 "internal/goarch" 9 "unsafe" 10 ) 11 12 // The first word of every non-empty interface type contains an *ITab. 13 // It records the underlying concrete type (Type), the interface type it 14 // is implementing (Inter), and some ancillary information. 15 // 16 // allocated in non-garbage-collected memory 17 type ITab struct { 18 Inter *InterfaceType 19 Type *Type 20 Hash uint32 // copy of Type.Hash. Used for type switches. 21 Fun [1]uintptr // variable sized. fun[0]==0 means Type does not implement Inter. 22 } 23 24 // Size returns the size of the itab in memory. 25 func (it *ITab) Size() int { 26 size := int(unsafe.Sizeof(ITab{})) 27 if it.Fun[0] == 0 { 28 return size 29 } 30 return size + (len(it.Inter.Methods)-1)*goarch.PtrSize 31 } 32 33 // EmptyInterface describes the layout of a "interface{}" or a "any." 34 // These are represented differently than non-empty interface, as the first 35 // word always points to an abi.Type. 36 type EmptyInterface struct { 37 Type *Type 38 Data unsafe.Pointer 39 } 40 41 // NonEmptyInterface describes the layout of an interface that contains any methods. 42 type NonEmptyInterface struct { 43 ITab *ITab 44 Data unsafe.Pointer 45 } 46 47 // CommonInterface describes the layout of both [EmptyInterface] and [NonEmptyInterface]. 48 type CommonInterface struct { 49 // Either an *ITab or a *Type, unexported to avoid accidental use. 50 _ unsafe.Pointer 51 52 Data unsafe.Pointer 53 } 54