2015-09-24 Vladimir Makarov <vmakarov@redhat.com>
[official-gcc.git] / libgo / go / runtime / slice.go
blob171087d7f6fe51c0c243e77d6338c04b4eff7282
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.
5 package runtime
7 import (
8 "unsafe"
11 type sliceStruct struct {
12 array unsafe.Pointer
13 len int
14 cap int
17 // TODO: take uintptrs instead of int64s?
18 func makeslice(t *slicetype, len64 int64, cap64 int64) sliceStruct {
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"))
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"))
32 p := newarray(t.elem, uintptr(cap))
33 return sliceStruct{p, len, cap}
36 // TODO: take uintptr instead of int64?
37 func growslice(t *slicetype, old sliceStruct, n int64) sliceStruct {
38 if n < 1 {
39 panic(errorString("growslice: invalid n"))
42 cap64 := int64(old.cap) + n
43 cap := int(cap64)
45 if int64(cap) != cap64 || cap < old.cap || t.elem.size > 0 && uintptr(cap) > maxmem/uintptr(t.elem.size) {
46 panic(errorString("growslice: cap out of range"))
49 if raceenabled {
50 callerpc := getcallerpc(unsafe.Pointer(&t))
51 racereadrangepc(old.array, uintptr(old.len*int(t.elem.size)), callerpc, funcPC(growslice))
54 et := t.elem
55 if et.size == 0 {
56 return sliceStruct{old.array, old.len, cap}
59 newcap := old.cap
60 if newcap+newcap < cap {
61 newcap = cap
62 } else {
63 for {
64 if old.len < 1024 {
65 newcap += newcap
66 } else {
67 newcap += newcap / 4
69 if newcap >= cap {
70 break
75 if uintptr(newcap) >= maxmem/uintptr(et.size) {
76 panic(errorString("growslice: cap out of range"))
78 lenmem := uintptr(old.len) * uintptr(et.size)
79 capmem := goroundupsize(uintptr(newcap) * uintptr(et.size))
80 newcap = int(capmem / uintptr(et.size))
81 var p unsafe.Pointer
82 if et.kind&kindNoPointers != 0 {
83 p = rawmem(capmem)
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 p = newarray(et, uintptr(newcap))
89 memmove(p, old.array, lenmem)
91 return sliceStruct{p, old.len, newcap}
94 func slicecopy(to sliceStruct, fm sliceStruct, width uintptr) int {
95 if fm.len == 0 || to.len == 0 || width == 0 {
96 return 0
99 n := fm.len
100 if to.len < n {
101 n = to.len
104 if raceenabled {
105 callerpc := getcallerpc(unsafe.Pointer(&to))
106 pc := funcPC(slicecopy)
107 racewriterangepc(to.array, uintptr(n*int(width)), callerpc, pc)
108 racereadrangepc(fm.array, uintptr(n*int(width)), callerpc, pc)
111 size := uintptr(n) * width
112 if size == 1 { // common case worth about 2x to do here
113 // TODO: is this still worth it with new memmove impl?
114 *(*byte)(to.array) = *(*byte)(fm.array) // known to be a byte pointer
115 } else {
116 memmove(to.array, fm.array, size)
118 return int(n)
121 func slicestringcopy(to []byte, fm string) int {
122 if len(fm) == 0 || len(to) == 0 {
123 return 0
126 n := len(fm)
127 if len(to) < n {
128 n = len(to)
131 if raceenabled {
132 callerpc := getcallerpc(unsafe.Pointer(&to))
133 pc := funcPC(slicestringcopy)
134 racewriterangepc(unsafe.Pointer(&to[0]), uintptr(n), callerpc, pc)
137 memmove(unsafe.Pointer(&to[0]), unsafe.Pointer((*stringStruct)(unsafe.Pointer(&fm)).str), uintptr(n))
138 return n