void runtime·makeslice(SliceType* t, int64 len, int64 cap, Slice ret) { ret.array = 0; ret.len = 0; ret.cap = 0; FLUSH(&ret); #line 24 "C:\Users\ADMINI~1\AppData\Local\Temp\2\makerelease686069423\go\src\pkg\runtime\slice.goc" // NOTE: The len > MaxMem/elemsize check here is not strictly necessary, // but it produces a 'len out of range' error instead of a 'cap out of range' error // when someone does make([]T, bignumber). 'cap out of range' is true too, // but since the cap is only being supplied implicitly, saying len is clearer. // See issue 4085. if(len < 0 || (intgo)len != len || t->elem->size > 0 && len > MaxMem / t->elem->size) runtime·panicstring("makeslice: len out of range"); if(cap < len || (intgo)cap != cap || t->elem->size > 0 && cap > MaxMem / t->elem->size) runtime·panicstring("makeslice: cap out of range"); makeslice1(t, len, cap, &ret); if(debug) { runtime·printf("makeslice(%S, %D, %D); ret=", *t->string, len, cap); runtime·printslice(ret); } FLUSH(&ret); }
void runtime·makeslice(SliceType* t, int64 len, int64 cap, Slice ret) { #line 24 "/home/pi/go_build/hg/go/src/pkg/runtime/slice.goc" // NOTE: The len > MaxMem/elemsize check here is not strictly necessary, // but it produces a 'len out of range' error instead of a 'cap out of range' error // when someone does make([]T, bignumber). 'cap out of range' is true too, // but since the cap is only being supplied implicitly, saying len is clearer. // See issue 4085. if(len < 0 || (intgo)len != len || t->elem->size > 0 && len > MaxMem / t->elem->size) runtime·panicstring("makeslice: len out of range"); if(cap < len || (intgo)cap != cap || t->elem->size > 0 && cap > MaxMem / t->elem->size) runtime·panicstring("makeslice: cap out of range"); makeslice1(t, len, cap, &ret); if(debug) { runtime·printf("makeslice(%S, %D, %D); ret=", *t->string, len, cap); runtime·printslice(ret); } FLUSH(&ret); }
// see also unsafe·NewArray // makeslice(typ *Type, len, cap int64) (ary []any); void runtime·makeslice(SliceType *t, int64 len, int64 cap, Slice ret) { if(len < 0 || (int32)len != len) runtime·panicstring("makeslice: len out of range"); if(cap < len || (int32)cap != cap || cap > ((uintptr)-1) / t->elem->size) runtime·panicstring("makeslice: cap out of range"); makeslice1(t, len, cap, &ret); if(debug) { runtime·printf("makeslice(%S, %D, %D); ret=", *t->string, len, cap); runtime·printslice(ret); } }
static void growslice1(SliceType *t, Slice x, int32 newcap, Slice *ret) { int32 m; m = x.cap; if(m == 0) m = newcap; else { do { if(x.len < 1024) m += m; else m += m/4; } while(m < newcap); } makeslice1(t, x.len, m, ret); runtime·memmove(ret->array, x.array, ret->len * t->elem->size); }
static void growslice1(SliceType *t, Slice x, intgo newcap, Slice *ret) { intgo m; m = x.cap; // Using newcap directly for m+m < newcap handles // both the case where m == 0 and also the case where // m+m/4 wraps around, in which case the loop // below might never terminate. if(m+m < newcap) m = newcap; else { do { if(x.len < 1024) m += m; else m += m/4; } while(m < newcap); } makeslice1(t, x.len, m, ret); runtime·memmove(ret->array, x.array, ret->len * t->elem->size); }
// make([]T, len, cap)会调用到这个函数来。注意这里的ret是整个结构体传进来的。所以按go的函数调用协议,返回值在栈中的布局其实是一个结构体而不是结构体指针。 // see also unsafe·NewArray // makeslice(typ *Type, len, cap int64) (ary []any); void runtime·makeslice(SliceType *t, int64 len, int64 cap, Slice ret) { // NOTE: The len > MaxMem/elemsize check here is not strictly necessary, // but it produces a 'len out of range' error instead of a 'cap out of range' error // when someone does make([]T, bignumber). 'cap out of range' is true too, // but since the cap is only being supplied implicitly, saying len is clearer. // See issue 4085. //(intgo)len != len是将len强制转换为go的int(依赖于机器位数,32或64),这就说明make([]T, len, cap)上面支持的len其实是int类型的 //也就意味着,在32位机器上,传一个很大的数,超过32位能表示的范围是会出错的。64位机器上没什么感觉。 if(len < 0 || (intgo)len != len || t->elem->size > 0 && len > MaxMem / t->elem->size) runtime·panicstring("makeslice: len out of range"); if(cap < len || (intgo)cap != cap || t->elem->size > 0 && cap > MaxMem / t->elem->size) runtime·panicstring("makeslice: cap out of range"); makeslice1(t, len, cap, &ret); if(debug) { runtime·printf("makeslice(%S, %D, %D); ret=", *t->string, len, cap); runtime·printslice(ret); } }