Golang 数据结构之 Slice (三)

前言

Golang 数据结构之 Slice (二)
上一篇文章介绍了一下扩容的基本情况,这一篇文章分析要 growslice 函数的源码

源码

我们看看 growslice函数的源码,可以分成三部分:

func growslice(et *_type, old slice, cap int) slice {
   
	if raceenabled {
   
		callerpc := getcallerpc(unsafe.Pointer(&et))
		racereadrangepc(old.array, uintptr(old.len*int(et.size)), callerpc, funcPC(growslice))
	}
	if msanenabled {
   
		msanread(old.array, uintptr(old.len*int(et.size)))
	}

	if et.size == 0 {
   
		if cap < old.cap {
   
			panic(errorString("growslice: cap out of range"))
		}
		// append should not create a slice with nil pointer but non-zero len.
		// We assume that append doesn't need to preserve old.array in this case.
		return slice{
   unsafe.Pointer(&zerobase), old.len, cap}
	}

	newcap := old.cap
	doublecap := newcap + newcap
	if cap > doublecap {
   
		newcap = cap
	} else {
   
		if old.len < 1024 {
   
			newcap = doublecap
		} else {
   
			for newcap < cap {
   
				newcap += newcap / 4
			}
		}
	}

	var lenmem, newlenmem, capmem uintptr
	const ptrSize = unsafe.Sizeof((*byte)(nil))
	switch et.size {
   
	case 1:
		lenmem = uintptr(old.len)
		newlenmem = uintptr(cap)
		capmem = roundupsize(uintptr(newcap
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值