1 // Copyright 2012 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.
9 // Called from C. Returns the Go type *m.
10 func gc_m_ptr(ret *interface{}) {
14 // Called from C. Returns the Go type *g.
15 func gc_g_ptr(ret *interface{}) {
19 // Called from C. Returns the Go type *itab.
20 func gc_itab_ptr(ret *interface{}) {
24 func gc_unixnanotime(now *int64) {
25 sec, nsec := timenow()
26 *now = sec*1e9 + int64(nsec)
30 gogc(2) // force GC and do eager sweep
31 systemstack(scavenge_m)
34 var poolcleanup func()
36 func registerPoolCleanup(f func()) {
42 if poolcleanup != nil {
46 for _, p := range &allp {
50 // clear tinyalloc pool
51 if c := p.mcache; c != nil {
55 // disconnect cached list before dropping it on the floor,
56 // so that a dangling ref to one entry does not pin all of them.
58 for sg = c.sudogcache; sg != nil; sg = sgnext {
66 for i := range p.deferpool {
67 // disconnect cached list before dropping it on the floor,
68 // so that a dangling ref to one entry does not pin all of them.
70 for d = p.deferpool[i]; d != nil; d = dlink {
81 getg().issystem = true
83 for gosweepone() != ^uintptr(0) {
89 // This can happen if a GC runs between
90 // gosweepone returning ^0 above
91 // and the lock being acquired.
96 goparkunlock(&gclock, "GC sweep wait")
101 _PoisonGC = 0xf969696969696969 & (1<<(8*ptrSize) - 1)
102 _PoisonStack = 0x6868686868686868 & (1<<(8*ptrSize) - 1)
105 // NOTE: Really dst *unsafe.Pointer, src unsafe.Pointer,
106 // but if we do that, Go inserts a write barrier on *dst = src.
108 func writebarrierptr(dst *uintptr, src uintptr) {
110 writebarrierptr_nostore(dst, src)
113 // Like writebarrierptr, but the store has already been applied.
116 func writebarrierptr_nostore(dst *uintptr, src uintptr) {
117 if getg() == nil { // very low-level startup
121 if src != 0 && (src < _PageSize || src == _PoisonGC || src == _PoisonStack) {
122 systemstack(func() { gothrow("bad pointer in write barrier") })
126 if mp.inwb || mp.dying > 0 {
139 func writebarrierstring(dst *[2]uintptr, src [2]uintptr) {
140 writebarrierptr(&dst[0], src[0])
145 func writebarrierslice(dst *[3]uintptr, src [3]uintptr) {
146 writebarrierptr(&dst[0], src[0])
152 func writebarrieriface(dst *[2]uintptr, src [2]uintptr) {
153 writebarrierptr(&dst[0], src[0])
154 writebarrierptr(&dst[1], src[1])
157 //go:generate go run wbfat_gen.go -- wbfat.go
159 // The above line generates multiword write barriers for
160 // all the combinations of ptr+scalar up to four words.
161 // The implementations are written to wbfat.go.
164 func writebarrierfat(typ *_type, dst, src unsafe.Pointer) {
165 mask := loadPtrMask(typ)
166 nptr := typ.size / ptrSize
167 for i := uintptr(0); i < nptr; i += 2 {
169 if (bits>>2)&_BitsMask == _BitsPointer {
170 writebarrierptr((*uintptr)(dst), *(*uintptr)(src))
172 *(*uintptr)(dst) = *(*uintptr)(src)
174 dst = add(dst, ptrSize)
175 src = add(src, ptrSize)
180 if (bits>>2)&_BitsMask == _BitsPointer {
181 writebarrierptr((*uintptr)(dst), *(*uintptr)(src))
183 *(*uintptr)(dst) = *(*uintptr)(src)
185 dst = add(dst, ptrSize)
186 src = add(src, ptrSize)
191 func writebarriercopy(typ *_type, dst, src slice) int {
199 dstp := unsafe.Pointer(dst.array)
200 srcp := unsafe.Pointer(src.array)
202 if uintptr(srcp) < uintptr(dstp) && uintptr(srcp)+uintptr(n)*typ.size > uintptr(dstp) {
203 // Overlap with src before dst.
204 // Copy backward, being careful not to move dstp/srcp
205 // out of the array they point into.
206 dstp = add(dstp, uintptr(n-1)*typ.size)
207 srcp = add(srcp, uintptr(n-1)*typ.size)
210 writebarrierfat(typ, dstp, srcp)
214 dstp = add(dstp, -typ.size)
215 srcp = add(srcp, -typ.size)
218 // Copy forward, being careful not to move dstp/srcp
219 // out of the array they point into.
222 writebarrierfat(typ, dstp, srcp)
226 dstp = add(dstp, typ.size)
227 srcp = add(srcp, typ.size)