1 // Copyright 2009 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.
10 "runtime/internal/math"
11 "runtime/internal/sys"
21 // A notInHeapSlice is a slice backed by go:notinheap memory.
22 type notInHeapSlice struct {
28 func panicmakeslicelen() {
29 panic(errorString("makeslice: len out of range"))
32 func panicmakeslicecap() {
33 panic(errorString("makeslice: cap out of range"))
36 // makeslicecopy allocates a slice of "tolen" elements of type "et",
37 // then copies "fromlen" elements of type "et" into that new allocation from "from".
38 func makeslicecopy(et *_type, tolen int, fromlen int, from unsafe.Pointer) unsafe.Pointer {
39 var tomem, copymem uintptr
40 if uintptr(tolen) > uintptr(fromlen) {
42 tomem, overflow = math.MulUintptr(et.size, uintptr(tolen))
43 if overflow || tomem > maxAlloc || tolen < 0 {
46 copymem = et.size * uintptr(fromlen)
48 // fromlen is a known good length providing and equal or greater than tolen,
49 // thereby making tolen a good slice length too as from and to slices have the
50 // same element width.
51 tomem = et.size * uintptr(tolen)
57 to = mallocgc(tomem, nil, false)
59 memclrNoHeapPointers(add(to, copymem), tomem-copymem)
62 // Note: can't use rawmem (which avoids zeroing of memory), because then GC can scan uninitialized memory.
63 to = mallocgc(tomem, et, true)
64 if copymem > 0 && writeBarrier.enabled {
65 // Only shade the pointers in old.array since we know the destination slice to
66 // only contains nil pointers because it has been cleared during alloc.
67 bulkBarrierPreWriteSrcOnly(uintptr(to), uintptr(from), copymem)
72 callerpc := getcallerpc()
73 pc := abi.FuncPCABIInternal(makeslicecopy)
74 racereadrangepc(from, copymem, callerpc, pc)
77 msanread(from, copymem)
80 memmove(to, from, copymem)
85 func makeslice(et *_type, len, cap int) unsafe.Pointer {
86 mem, overflow := math.MulUintptr(et.size, uintptr(cap))
87 if overflow || mem > maxAlloc || len < 0 || len > cap {
88 // NOTE: Produce a 'len out of range' error instead of a
89 // 'cap out of range' error when someone does make([]T, bignumber).
90 // 'cap out of range' is true too, but since the cap is only being
91 // supplied implicitly, saying len is clearer.
92 // See golang.org/issue/4085.
93 mem, overflow := math.MulUintptr(et.size, uintptr(len))
94 if overflow || mem > maxAlloc || len < 0 {
100 return mallocgc(mem, et, true)
103 func makeslice64(et *_type, len64, cap64 int64) unsafe.Pointer {
105 if int64(len) != len64 {
110 if int64(cap) != cap64 {
114 return makeslice(et, len, cap)
117 func unsafeslice(et *_type, ptr unsafe.Pointer, len int) {
123 panic(errorString("unsafe.Slice: ptr is nil and len is not zero"))
126 mem, overflow := math.MulUintptr(et.size, uintptr(len))
127 if overflow || mem > maxAlloc || len < 0 {
128 panicunsafeslicelen()
132 func unsafeslice64(et *_type, ptr unsafe.Pointer, len64 int64) {
134 if int64(len) != len64 {
135 panicunsafeslicelen()
137 unsafeslice(et, ptr, len)
140 func unsafeslicecheckptr(et *_type, ptr unsafe.Pointer, len64 int64) {
141 unsafeslice64(et, ptr, len64)
143 // Check that underlying array doesn't straddle multiple heap objects.
144 // unsafeslice64 has already checked for overflow.
145 if checkptrStraddles(ptr, uintptr(len64)*et.size) {
146 throw("checkptr: unsafe.Slice result straddles multiple allocations")
150 func panicunsafeslicelen() {
151 panic(errorString("unsafe.Slice: len out of range"))
154 // growslice handles slice growth during append.
155 // It is passed the slice element type, the old slice, and the desired new minimum capacity,
156 // and it returns a new slice with at least that capacity, with the old data
158 // The new slice's length is set to the old slice's length,
159 // NOT to the new requested capacity.
160 // This is for codegen convenience. The old slice's length is used immediately
161 // to calculate where to write new values during an append.
162 // TODO: When the old backend is gone, reconsider this decision.
163 // The SSA backend might prefer the new length or to return only ptr/cap and save stack space.
164 func growslice(et *_type, old slice, cap int) slice {
166 callerpc := getcallerpc()
167 racereadrangepc(old.array, uintptr(old.len*int(et.size)), callerpc, abi.FuncPCABIInternal(growslice))
170 msanread(old.array, uintptr(old.len*int(et.size)))
174 panic(errorString("growslice: cap out of range"))
178 // append should not create a slice with nil pointer but non-zero len.
179 // We assume that append doesn't need to preserve old.array in this case.
180 return slice{unsafe.Pointer(&zerobase), old.len, cap}
184 doublecap := newcap + newcap
191 // Check 0 < newcap to detect overflow
192 // and prevent an infinite loop.
193 for 0 < newcap && newcap < cap {
196 // Set newcap to the requested cap when
197 // the newcap calculation overflowed.
205 var lenmem, newlenmem, capmem uintptr
206 // Specialize for common values of et.size.
207 // For 1 we don't need any division/multiplication.
208 // For sys.PtrSize, compiler will optimize division/multiplication into a shift by a constant.
209 // For powers of 2, use a variable shift.
212 lenmem = uintptr(old.len)
213 newlenmem = uintptr(cap)
214 capmem = roundupsize(uintptr(newcap))
215 overflow = uintptr(newcap) > maxAlloc
217 case et.size == goarch.PtrSize:
218 lenmem = uintptr(old.len) * goarch.PtrSize
219 newlenmem = uintptr(cap) * goarch.PtrSize
220 capmem = roundupsize(uintptr(newcap) * goarch.PtrSize)
221 overflow = uintptr(newcap) > maxAlloc/goarch.PtrSize
222 newcap = int(capmem / goarch.PtrSize)
223 case isPowerOfTwo(et.size):
225 if goarch.PtrSize == 8 {
226 // Mask shift for better code generation.
227 shift = uintptr(sys.Ctz64(uint64(et.size))) & 63
229 shift = uintptr(sys.Ctz32(uint32(et.size))) & 31
231 lenmem = uintptr(old.len) << shift
232 newlenmem = uintptr(cap) << shift
233 capmem = roundupsize(uintptr(newcap) << shift)
234 overflow = uintptr(newcap) > (maxAlloc >> shift)
235 newcap = int(capmem >> shift)
237 lenmem = uintptr(old.len) * et.size
238 newlenmem = uintptr(cap) * et.size
239 capmem, overflow = math.MulUintptr(et.size, uintptr(newcap))
240 capmem = roundupsize(capmem)
241 newcap = int(capmem / et.size)
244 // The check of overflow in addition to capmem > maxAlloc is needed
245 // to prevent an overflow which can be used to trigger a segfault
246 // on 32bit architectures with this example program:
248 // type T [1<<27 + 1]int64
254 // s = append(s, d, d, d, d)
255 // print(len(s), "\n")
257 if overflow || capmem > maxAlloc {
258 panic(errorString("growslice: cap out of range"))
263 p = mallocgc(capmem, nil, false)
264 // The append() that calls growslice is going to overwrite from old.len to cap (which will be the new length).
265 // Only clear the part that will not be overwritten.
266 memclrNoHeapPointers(add(p, newlenmem), capmem-newlenmem)
268 // Note: can't use rawmem (which avoids zeroing of memory), because then GC can scan uninitialized memory.
269 p = mallocgc(capmem, et, true)
270 if lenmem > 0 && writeBarrier.enabled {
271 // Only shade the pointers in old.array since we know the destination slice p
272 // only contains nil pointers because it has been cleared during alloc.
273 bulkBarrierPreWriteSrcOnly(uintptr(p), uintptr(old.array), lenmem-et.size+et.ptrdata)
276 memmove(p, old.array, lenmem)
278 return slice{p, old.len, newcap}
281 func isPowerOfTwo(x uintptr) bool {
285 // slicecopy is used to copy from a string or slice of pointerless elements into a slice.
286 func slicecopy(toPtr unsafe.Pointer, toLen int, fromPtr unsafe.Pointer, fromLen int, width uintptr) int {
287 if fromLen == 0 || toLen == 0 {
300 size := uintptr(n) * width
302 callerpc := getcallerpc()
303 pc := abi.FuncPCABIInternal(slicecopy)
304 racereadrangepc(fromPtr, size, callerpc, pc)
305 racewriterangepc(toPtr, size, callerpc, pc)
308 msanread(fromPtr, size)
309 msanwrite(toPtr, size)
312 if size == 1 { // common case worth about 2x to do here
313 // TODO: is this still worth it with new memmove impl?
314 *(*byte)(toPtr) = *(*byte)(fromPtr) // known to be a byte pointer
316 memmove(toPtr, fromPtr, size)