github.com/rsc/go@v0.0.0-20150416155037-e040fd465409/src/runtime/slice.go (about) 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. 4 5 package runtime 6 7 import ( 8 "unsafe" 9 ) 10 11 type slice struct { 12 array unsafe.Pointer 13 len int 14 cap int 15 } 16 17 // TODO: take uintptrs instead of int64s? 18 func makeslice(t *slicetype, len64, cap64 int64) slice { 19 // NOTE: The len > MaxMem/elemsize check here is not strictly necessary, 20 // but it produces a 'len out of range' error instead of a 'cap out of range' error 21 // when someone does make([]T, bignumber). 'cap out of range' is true too, 22 // but since the cap is only being supplied implicitly, saying len is clearer. 23 // See issue 4085. 24 len := int(len64) 25 if len64 < 0 || int64(len) != len64 || t.elem.size > 0 && uintptr(len) > _MaxMem/uintptr(t.elem.size) { 26 panic(errorString("makeslice: len out of range")) 27 } 28 cap := int(cap64) 29 if cap < len || int64(cap) != cap64 || t.elem.size > 0 && uintptr(cap) > _MaxMem/uintptr(t.elem.size) { 30 panic(errorString("makeslice: cap out of range")) 31 } 32 p := newarray(t.elem, uintptr(cap)) 33 return slice{p, len, cap} 34 } 35 36 func growslice(t *slicetype, old slice, n int) slice { 37 if n < 1 { 38 panic(errorString("growslice: invalid n")) 39 } 40 41 cap := old.cap + n 42 if cap < old.cap || t.elem.size > 0 && uintptr(cap) > _MaxMem/uintptr(t.elem.size) { 43 panic(errorString("growslice: cap out of range")) 44 } 45 46 if raceenabled { 47 callerpc := getcallerpc(unsafe.Pointer(&t)) 48 racereadrangepc(old.array, uintptr(old.len*int(t.elem.size)), callerpc, funcPC(growslice)) 49 } 50 51 et := t.elem 52 if et.size == 0 { 53 // append should not create a slice with nil pointer but non-zero len. 54 // We assume that append doesn't need to preserve old.array in this case. 55 return slice{unsafe.Pointer(&zerobase), old.len, cap} 56 } 57 58 newcap := old.cap 59 if newcap+newcap < cap { 60 newcap = cap 61 } else { 62 for { 63 if old.len < 1024 { 64 newcap += newcap 65 } else { 66 newcap += newcap / 4 67 } 68 if newcap >= cap { 69 break 70 } 71 } 72 } 73 74 if uintptr(newcap) >= _MaxMem/uintptr(et.size) { 75 panic(errorString("growslice: cap out of range")) 76 } 77 lenmem := uintptr(old.len) * uintptr(et.size) 78 capmem := roundupsize(uintptr(newcap) * uintptr(et.size)) 79 newcap = int(capmem / uintptr(et.size)) 80 var p unsafe.Pointer 81 if et.kind&kindNoPointers != 0 { 82 p = rawmem(capmem) 83 memmove(p, old.array, lenmem) 84 memclr(add(p, lenmem), capmem-lenmem) 85 } else { 86 // Note: can't use rawmem (which avoids zeroing of memory), because then GC can scan unitialized memory. 87 // TODO(rsc): Use memmove when !needwb(). 88 p = newarray(et, uintptr(newcap)) 89 for i := 0; i < old.len; i++ { 90 typedmemmove(et, add(p, uintptr(i)*et.size), add(old.array, uintptr(i)*et.size)) 91 } 92 } 93 94 return slice{p, old.len, newcap} 95 } 96 97 func slicecopy(to, fm slice, width uintptr) int { 98 if fm.len == 0 || to.len == 0 { 99 return 0 100 } 101 102 n := fm.len 103 if to.len < n { 104 n = to.len 105 } 106 107 if width == 0 { 108 return n 109 } 110 111 if raceenabled { 112 callerpc := getcallerpc(unsafe.Pointer(&to)) 113 pc := funcPC(slicecopy) 114 racewriterangepc(to.array, uintptr(n*int(width)), callerpc, pc) 115 racereadrangepc(fm.array, uintptr(n*int(width)), callerpc, pc) 116 } 117 118 size := uintptr(n) * width 119 if size == 1 { // common case worth about 2x to do here 120 // TODO: is this still worth it with new memmove impl? 121 *(*byte)(to.array) = *(*byte)(fm.array) // known to be a byte pointer 122 } else { 123 memmove(to.array, fm.array, size) 124 } 125 return int(n) 126 } 127 128 func slicestringcopy(to []byte, fm string) int { 129 if len(fm) == 0 || len(to) == 0 { 130 return 0 131 } 132 133 n := len(fm) 134 if len(to) < n { 135 n = len(to) 136 } 137 138 if raceenabled { 139 callerpc := getcallerpc(unsafe.Pointer(&to)) 140 pc := funcPC(slicestringcopy) 141 racewriterangepc(unsafe.Pointer(&to[0]), uintptr(n), callerpc, pc) 142 } 143 144 memmove(unsafe.Pointer(&to[0]), unsafe.Pointer((*stringStruct)(unsafe.Pointer(&fm)).str), uintptr(n)) 145 return n 146 }