slice.go 3.6 KB

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