github.com/q45/go@v0.0.0-20151101211701-a4fb8c13db3f/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  // growslice_n is a variant of growslice that takes the number of new elements
    37  // instead of the new minimum capacity.
    38  // TODO(rsc): This is used by append(slice, slice...).
    39  // The compiler should change that code to use growslice directly (issue #11419).
    40  func growslice_n(t *slicetype, old slice, n int) slice {
    41  	if n < 1 {
    42  		panic(errorString("growslice: invalid n"))
    43  	}
    44  	return growslice(t, old, old.cap+n)
    45  }
    46  
    47  // growslice handles slice growth during append.
    48  // It is passed the slice type, the old slice, and the desired new minimum capacity,
    49  // and it returns a new slice with at least that capacity, with the old data
    50  // copied into it.
    51  func growslice(t *slicetype, old slice, cap int) slice {
    52  	if cap < old.cap || t.elem.size > 0 && uintptr(cap) > _MaxMem/uintptr(t.elem.size) {
    53  		panic(errorString("growslice: cap out of range"))
    54  	}
    55  
    56  	if raceenabled {
    57  		callerpc := getcallerpc(unsafe.Pointer(&t))
    58  		racereadrangepc(old.array, uintptr(old.len*int(t.elem.size)), callerpc, funcPC(growslice))
    59  	}
    60  	if msanenabled {
    61  		msanread(old.array, uintptr(old.len*int(t.elem.size)))
    62  	}
    63  
    64  	et := t.elem
    65  	if et.size == 0 {
    66  		// append should not create a slice with nil pointer but non-zero len.
    67  		// We assume that append doesn't need to preserve old.array in this case.
    68  		return slice{unsafe.Pointer(&zerobase), old.len, cap}
    69  	}
    70  
    71  	newcap := old.cap
    72  	if newcap+newcap < cap {
    73  		newcap = cap
    74  	} else {
    75  		for {
    76  			if old.len < 1024 {
    77  				newcap += newcap
    78  			} else {
    79  				newcap += newcap / 4
    80  			}
    81  			if newcap >= cap {
    82  				break
    83  			}
    84  		}
    85  	}
    86  
    87  	if uintptr(newcap) >= _MaxMem/uintptr(et.size) {
    88  		panic(errorString("growslice: cap out of range"))
    89  	}
    90  	lenmem := uintptr(old.len) * uintptr(et.size)
    91  	capmem := roundupsize(uintptr(newcap) * uintptr(et.size))
    92  	newcap = int(capmem / uintptr(et.size))
    93  	var p unsafe.Pointer
    94  	if et.kind&kindNoPointers != 0 {
    95  		p = rawmem(capmem)
    96  		memmove(p, old.array, lenmem)
    97  		memclr(add(p, lenmem), capmem-lenmem)
    98  	} else {
    99  		// Note: can't use rawmem (which avoids zeroing of memory), because then GC can scan uninitialized memory.
   100  		p = newarray(et, uintptr(newcap))
   101  		if !writeBarrierEnabled {
   102  			memmove(p, old.array, lenmem)
   103  		} else {
   104  			for i := uintptr(0); i < lenmem; i += et.size {
   105  				typedmemmove(et, add(p, i), add(old.array, i))
   106  			}
   107  		}
   108  	}
   109  
   110  	return slice{p, old.len, newcap}
   111  }
   112  
   113  func slicecopy(to, fm slice, width uintptr) int {
   114  	if fm.len == 0 || to.len == 0 {
   115  		return 0
   116  	}
   117  
   118  	n := fm.len
   119  	if to.len < n {
   120  		n = to.len
   121  	}
   122  
   123  	if width == 0 {
   124  		return n
   125  	}
   126  
   127  	if raceenabled {
   128  		callerpc := getcallerpc(unsafe.Pointer(&to))
   129  		pc := funcPC(slicecopy)
   130  		racewriterangepc(to.array, uintptr(n*int(width)), callerpc, pc)
   131  		racereadrangepc(fm.array, uintptr(n*int(width)), callerpc, pc)
   132  	}
   133  	if msanenabled {
   134  		msanwrite(to.array, uintptr(n*int(width)))
   135  		msanread(fm.array, uintptr(n*int(width)))
   136  	}
   137  
   138  	size := uintptr(n) * width
   139  	if size == 1 { // common case worth about 2x to do here
   140  		// TODO: is this still worth it with new memmove impl?
   141  		*(*byte)(to.array) = *(*byte)(fm.array) // known to be a byte pointer
   142  	} else {
   143  		memmove(to.array, fm.array, size)
   144  	}
   145  	return int(n)
   146  }
   147  
   148  func slicestringcopy(to []byte, fm string) int {
   149  	if len(fm) == 0 || len(to) == 0 {
   150  		return 0
   151  	}
   152  
   153  	n := len(fm)
   154  	if len(to) < n {
   155  		n = len(to)
   156  	}
   157  
   158  	if raceenabled {
   159  		callerpc := getcallerpc(unsafe.Pointer(&to))
   160  		pc := funcPC(slicestringcopy)
   161  		racewriterangepc(unsafe.Pointer(&to[0]), uintptr(n), callerpc, pc)
   162  	}
   163  	if msanenabled {
   164  		msanwrite(unsafe.Pointer(&to[0]), uintptr(n))
   165  	}
   166  
   167  	memmove(unsafe.Pointer(&to[0]), unsafe.Pointer(stringStructOf(&fm).str), uintptr(n))
   168  	return n
   169  }