美文网首页
5-数据类型内存结构-chan

5-数据类型内存结构-chan

作者: 浩玥当空 | 来源:发表于2019-06-03 22:44 被阅读0次

5. chan

var var_chan = make(chan int ,1)
func func1(){
...
var var2_chan = make(chan int ,3)
...
}

1、通过runtime.makechan()创建channel。

2、第一个参数为chan类型信息
3、返回值类型为*hchan
即: chan变量实际为一个hchan指针
由全局变量也可以看出。

func makechan(t *chantype, size int) *hchan {
    ...
    var c *hchan
    ...
    return c
}
    "".var_chan SBSS size=8

        0x014f 00335 (type.go:48)       LEAQ    type.chan int(SB), AX
        0x0156 00342 (type.go:48)       PCDATA  $2, $0
        0x0156 00342 (type.go:48)       MOVQ    AX, (SP)
        0x015a 00346 (type.go:48)       MOVQ    $3, 8(SP)
        0x0163 00355 (type.go:48)       CALL    runtime.makechan(SB)
        0x0168 00360 (type.go:48)       PCDATA  $2, $1
        0x0168 00360 (type.go:48)       MOVQ    16(SP), AX  //返回值为 *hchan 类型
        0x016d 00365 (type.go:48)       PCDATA  $2, $0
        0x016d 00365 (type.go:48)       PCDATA  $0, $5
        0x016d 00365 (type.go:48)       MOVQ    AX, "".var2_chan+144(SP)

chan类型信息

type chantype struct {  //64 字节
    typ  _type      //48 字节
    elem *_type     //8  字节
    dir  uintptr    //8 字节
}

类型信息对应汇编

type..namedata.*chan int- SRODATA dupok size=12
        0x0000 00 00 09 2a 63 68 61 6e 20 69 6e 74              ...*chan int
type.*chan int SRODATA dupok size=56
        0x0000 08 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00  ................
        0x0010 ed 7b ed 3b 00 08 08 36 00 00 00 00 00 00 00 00  .{.;...6........
        0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
        0x0030 00 00 00 00 00 00 00 00                          ........
        rel 24+8 t=1 runtime.algarray+80
        rel 32+8 t=1 runtime.gcbits.01+0
        rel 40+4 t=5 type..namedata.*chan int-+0
        rel 48+8 t=1 type.chan int+0
type.chan int SRODATA dupok size=64
        0x0000 08 00 00 00 00 00 00 00 08 00 00 00 00 00 00 00  ................
        0x0010 91 55 cb 71 02 08 08 32 00 00 00 00 00 00 00 00  .U.q...2........
        0x0020 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
        0x0030 00 00 00 00 00 00 00 00 03 00 00 00 00 00 00 00  ................
        rel 24+8 t=1 runtime.algarray+80
        rel 32+8 t=1 runtime.gcbits.01+0
        rel 40+4 t=5 type..namedata.*chan int-+0
        rel 44+4 t=6 type.*chan int+0
        rel 48+8 t=1 type.int+0

5.1 通过runtime.makechan

src\runtime\chan.go
func makechan(t *chantype, size int) *hchan {
    elem := t.elem

    // compiler checks this but be safe.
    if elem.size >= 1<<16 {
        throw("makechan: invalid channel element type")
    }
    if hchanSize%maxAlign != 0 || elem.align > maxAlign {
        throw("makechan: bad alignment")
    }

    mem, overflow := math.MulUintptr(elem.size, uintptr(size))
    if overflow || mem > maxAlloc-hchanSize || size < 0 {
        panic(plainError("makechan: size out of range"))
    }

    // Hchan does not contain pointers interesting for GC when elements stored in buf do not contain pointers.
    // buf points into the same allocation, elemtype is persistent.
    // SudoG's are referenced from their owning thread so they can't be collected.
    // TODO(dvyukov,rlh): Rethink when collector can move allocated objects.
    var c *hchan
    switch {
    case mem == 0:
        // Queue or element size is zero.
        c = (*hchan)(mallocgc(hchanSize, nil, true))
        // Race detector uses this location for synchronization.
        c.buf = c.raceaddr()
    case elem.kind&kindNoPointers != 0:
        // Elements do not contain pointers.
        // Allocate hchan and buf in one call.
        c = (*hchan)(mallocgc(hchanSize+mem, nil, true))
        c.buf = add(unsafe.Pointer(c), hchanSize)
    default:
        // Elements contain pointers.
        c = new(hchan)
        c.buf = mallocgc(mem, elem, true)
    }

    c.elemsize = uint16(elem.size)
    c.elemtype = elem
    c.dataqsiz = uint(size)

    if debugChan {
        print("makechan: chan=", c, "; elemsize=", elem.size, "; elemalg=", elem.alg, "; dataqsiz=", size, "\n")
    }
    return c
}

相关文章

  • 5-数据类型内存结构-chan

    5. chan 1、通过runtime.makechan()创建channel。 2、第一个参数为chan类型信息...

  • C语言中内存对其规则

    C语言中结构体内存对齐规则 对齐规则: 内存偏移为该数据类型的最小整数倍 总体占用内存为结构体中最大数据类型的整数...

  • chan深入理解之源码分析

    chan的理解 chan用于协程间通信,结构体如下,代码位置为go/src/runtime/chan.go 从结构...

  • 内存管理:部分基础知识

    一、内存分区二、常用数据类型占用内存大小三、给对象分配内存 1、给结构体分配内存及内存对齐 2、内存分配完后,内存...

  • chan

    chan 不带缓存 make(chan 数据类型) 进和出都会阻塞. 读和写同时存在,才会同时退出阻塞。如果只...

  • JS数据类型

    -基本数据类型:结构简单,存储在栈内存里面,操作的是值 ①,’字符串 string :'' "" 'a' "a" ...

  • 5.2共用体

    共用体也称联合,可以看出一种特殊的结构。和结构一样,共用体也可以包括多种数据类型,但在共用体中,各种数据类型在内存...

  • java内存模型理解

    java内存模型理解 JVM 内存结构:堆、栈、方法区等等。。 原子性:对基本数据类型的变量和赋值操作才是原子性的...

  • iOS 总结笔记(一)

    编译器给结构体开辟空间 首先找到结构体中最宽的基本数据类型,然后寻找内存地址能是该基本数据类型的整倍的位置,作为结...

  • C结构体、共用体与C++基础1

    1、结构体 结构体是C编程中一种用户自定义的数据类型,类似于Java的JavaBean 当结构体需要内存过大,使用...

网友评论

      本文标题:5-数据类型内存结构-chan

      本文链接:https://www.haomeiwen.com/subject/vdfkxctx.html