github.com/zach-klippenstein/go@v0.0.0-20150108044943-fcfbeb3adf58/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 sliceStruct 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 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"))
    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 sliceStruct{p, len, cap}
    34  }
    35  
    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"))
    40  	}
    41  
    42  	cap64 := int64(old.cap) + n
    43  	cap := int(cap64)
    44  
    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"))
    47  	}
    48  
    49  	if raceenabled {
    50  		callerpc := getcallerpc(unsafe.Pointer(&t))
    51  		racereadrangepc(old.array, uintptr(old.len*int(t.elem.size)), callerpc, funcPC(growslice))
    52  	}
    53  
    54  	et := t.elem
    55  	if et.size == 0 {
    56  		return sliceStruct{old.array, old.len, cap}
    57  	}
    58  
    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
    68  			}
    69  			if newcap >= cap {
    70  				break
    71  			}
    72  		}
    73  	}
    74  
    75  	if uintptr(newcap) >= _MaxMem/uintptr(et.size) {
    76  		panic(errorString("growslice: cap out of range"))
    77  	}
    78  	lenmem := uintptr(old.len) * uintptr(et.size)
    79  	capmem := roundupsize(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  		memmove(p, old.array, lenmem)
    85  		memclr(add(p, lenmem), capmem-lenmem)
    86  	} else {
    87  		// Note: can't use rawmem (which avoids zeroing of memory), because then GC can scan unitialized memory.
    88  		// TODO(rsc): Use memmove when !needwb().
    89  		p = newarray(et, uintptr(newcap))
    90  		for i := 0; i < old.len; i++ {
    91  			typedmemmove(et, add(p, uintptr(i)*et.size), add(old.array, uintptr(i)*et.size))
    92  		}
    93  	}
    94  
    95  	return sliceStruct{p, old.len, newcap}
    96  }
    97  
    98  func slicecopy(to sliceStruct, fm sliceStruct, width uintptr) int {
    99  	if fm.len == 0 || to.len == 0 {
   100  		return 0
   101  	}
   102  
   103  	n := fm.len
   104  	if to.len < n {
   105  		n = to.len
   106  	}
   107  
   108  	if width == 0 {
   109  		return n
   110  	}
   111  
   112  	if raceenabled {
   113  		callerpc := getcallerpc(unsafe.Pointer(&to))
   114  		pc := funcPC(slicecopy)
   115  		racewriterangepc(to.array, uintptr(n*int(width)), callerpc, pc)
   116  		racereadrangepc(fm.array, uintptr(n*int(width)), callerpc, pc)
   117  	}
   118  
   119  	size := uintptr(n) * width
   120  	if size == 1 { // common case worth about 2x to do here
   121  		// TODO: is this still worth it with new memmove impl?
   122  		*(*byte)(to.array) = *(*byte)(fm.array) // known to be a byte pointer
   123  	} else {
   124  		memmove(to.array, fm.array, size)
   125  	}
   126  	return int(n)
   127  }
   128  
   129  func slicestringcopy(to []byte, fm string) int {
   130  	if len(fm) == 0 || len(to) == 0 {
   131  		return 0
   132  	}
   133  
   134  	n := len(fm)
   135  	if len(to) < n {
   136  		n = len(to)
   137  	}
   138  
   139  	if raceenabled {
   140  		callerpc := getcallerpc(unsafe.Pointer(&to))
   141  		pc := funcPC(slicestringcopy)
   142  		racewriterangepc(unsafe.Pointer(&to[0]), uintptr(n), callerpc, pc)
   143  	}
   144  
   145  	memmove(unsafe.Pointer(&to[0]), unsafe.Pointer((*stringStruct)(unsafe.Pointer(&fm)).str), uintptr(n))
   146  	return n
   147  }