1 // Copyright 2014 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.
14 flagNoScan = _FlagNoScan
15 flagNoZero = _FlagNoZero
17 maxTinySize = _TinySize
18 tinySizeClass = _TinySizeClass
19 maxSmallSize = _MaxSmallSize
21 pageShift = _PageShift
25 bitsPerPointer = _BitsPerPointer
27 pointersPerByte = _PointersPerByte
28 maxGCMask = _MaxGCMask
30 bitsPointer = _BitsPointer
31 bitsScalar = _BitsScalar
33 mSpanInUse = _MSpanInUse
35 concurrentSweep = _ConcurrentSweep
38 // Page number (address>>pageShift)
41 // base address for all 0-byte allocations
44 // Allocate an object of size bytes.
45 // Small objects are allocated from the per-P cache's free lists.
46 // Large objects (> 32 kB) are allocated straight from the heap.
47 func mallocgc(size uintptr, typ *_type, flags uint32) unsafe.Pointer {
49 return unsafe.Pointer(&zerobase)
53 if flags&flagNoScan == 0 && typ == nil {
54 gothrow("malloc missing type")
57 // This function must be atomic wrt GC, but for performance reasons
58 // we don't acquirem/releasem on fast path. The code below does not have
59 // split stack checks, so it can't be preempted by GC.
60 // Functions like roundup/add are inlined. And systemstack/racemalloc are nosplit.
61 // If debugMalloc = true, these assumptions are checked below.
64 if mp.mallocing != 0 {
65 gothrow("malloc deadlock")
69 mp.curg.stackguard0 = ^uintptr(0xfff) | 0xbad
76 if size <= maxSmallSize {
77 if flags&flagNoScan != 0 && size < maxTinySize {
80 // Tiny allocator combines several tiny allocation requests
81 // into a single memory block. The resulting memory block
82 // is freed when all subobjects are unreachable. The subobjects
83 // must be FlagNoScan (don't have pointers), this ensures that
84 // the amount of potentially wasted memory is bounded.
86 // Size of the memory block used for combining (maxTinySize) is tunable.
87 // Current setting is 16 bytes, which relates to 2x worst case memory
88 // wastage (when all but one subobjects are unreachable).
89 // 8 bytes would result in no wastage at all, but provides less
90 // opportunities for combining.
91 // 32 bytes provides more opportunities for combining,
92 // but can lead to 4x worst case wastage.
93 // The best case winning is 8x regardless of block size.
95 // Objects obtained from tiny allocator must not be freed explicitly.
96 // So when an object will be freed explicitly, we ensure that
97 // its size >= maxTinySize.
99 // SetFinalizer has a special case for objects potentially coming
100 // from tiny allocator, it such case it allows to set finalizers
101 // for an inner byte of a memory block.
103 // The main targets of tiny allocator are small strings and
104 // standalone escaping variables. On a json benchmark
105 // the allocator reduces number of allocations by ~12% and
106 // reduces heap size by ~20%.
107 tinysize := uintptr(c.tinysize)
108 if size <= tinysize {
109 tiny := unsafe.Pointer(c.tiny)
110 // Align tiny pointer for required (conservative) alignment.
112 tiny = roundup(tiny, 8)
113 } else if size&3 == 0 {
114 tiny = roundup(tiny, 4)
115 } else if size&1 == 0 {
116 tiny = roundup(tiny, 2)
118 size1 := size + (uintptr(tiny) - uintptr(unsafe.Pointer(c.tiny)))
119 if size1 <= tinysize {
120 // The object fits into existing tiny block.
122 c.tiny = (*byte)(add(x, size))
123 c.tinysize -= uintptr(size1)
127 if mp.mallocing == 0 {
128 gothrow("bad malloc")
132 mp.curg.stackguard0 = mp.curg.stack.lo + _StackGuard
134 // Note: one releasem for the acquirem just above.
135 // The other for the acquirem at start of malloc.
142 // Allocate a new maxTinySize block.
143 s = c.alloc[tinySizeClass]
147 mCache_Refill(c, tinySizeClass)
149 s = c.alloc[tinySizeClass]
154 //TODO: prefetch v.next
155 x = unsafe.Pointer(v)
156 (*[2]uint64)(x)[0] = 0
157 (*[2]uint64)(x)[1] = 0
158 // See if we need to replace the existing tiny block with the new one
159 // based on amount of remaining free space.
160 if maxTinySize-size > tinysize {
161 c.tiny = (*byte)(add(x, size))
162 c.tinysize = uintptr(maxTinySize - size)
168 sizeclass = size_to_class8[(size+7)>>3]
170 sizeclass = size_to_class128[(size-1024+127)>>7]
172 size = uintptr(class_to_size[sizeclass])
173 s = c.alloc[sizeclass]
177 mCache_Refill(c, int32(sizeclass))
179 s = c.alloc[sizeclass]
185 x = unsafe.Pointer(v)
186 if flags&flagNoZero == 0 {
188 if size > 2*ptrSize && ((*[2]uintptr)(x))[1] != 0 {
189 memclr(unsafe.Pointer(v), size)
193 c.local_cachealloc += intptr(size)
197 s = largeAlloc(size, uint32(flags))
199 x = unsafe.Pointer(uintptr(s.start << pageShift))
200 size = uintptr(s.elemsize)
203 if flags&flagNoScan != 0 {
204 // All objects are pre-marked as noscan.
208 // If allocating a defer+arg block, now that we've picked a malloc size
209 // large enough to hold everything, cut the "asked for" size down to
210 // just the defer header, so that the GC bitmap will record the arg block
211 // as containing nothing at all (as if it were unused space at the end of
212 // a malloc block caused by size rounding).
213 // The defer arg areas are scanned as part of scanstack.
214 if typ == deferType {
215 size0 = unsafe.Sizeof(_defer{})
218 // From here till marked label marking the object as allocated
219 // and storing type info in the GC bitmap.
221 arena_start := uintptr(unsafe.Pointer(mheap_.arena_start))
222 off := (uintptr(x) - arena_start) / ptrSize
223 xbits := (*uint8)(unsafe.Pointer(arena_start - off/wordsPerBitmapByte - 1))
224 shift := (off % wordsPerBitmapByte) * gcBits
225 if debugMalloc && ((*xbits>>shift)&(bitMask|bitPtrMask)) != bitBoundary {
226 println("runtime: bits =", (*xbits>>shift)&(bitMask|bitPtrMask))
227 gothrow("bad bits in markallocated")
233 // It's one word and it has pointers, it must be a pointer.
234 *xbits |= (bitsPointer << 2) << shift
237 if typ.kind&kindGCProg != 0 {
238 nptr := (uintptr(typ.size) + ptrSize - 1) / ptrSize
241 masksize *= 2 // repeated
243 masksize = masksize * pointersPerByte / 8 // 4 bits per word
244 masksize++ // unroll flag in the beginning
245 if masksize > maxGCMask && typ.gc[1] != 0 {
246 // If the mask is too large, unroll the program directly
247 // into the GC bitmap. It's 7 times slower than copying
248 // from the pre-unrolled mask, but saves 1/16 of type size
249 // memory for the mask.
251 unrollgcproginplace_m(x, typ, size, size0)
255 ptrmask = (*uint8)(unsafe.Pointer(uintptr(typ.gc[0])))
256 // Check whether the program is already unrolled.
257 if uintptr(atomicloadp(unsafe.Pointer(ptrmask)))&0xff == 0 {
262 ptrmask = (*uint8)(add(unsafe.Pointer(ptrmask), 1)) // skip the unroll flag byte
264 ptrmask = (*uint8)(unsafe.Pointer(typ.gc[0])) // pointer to unrolled mask
266 if size == 2*ptrSize {
267 *xbits = *ptrmask | bitBoundary
270 te = uintptr(typ.size) / ptrSize
271 // If the type occupies odd number of words, its mask is repeated.
275 // Copy pointer bitmask into the bitmap.
276 for i := uintptr(0); i < size0; i += 2 * ptrSize {
277 v := *(*uint8)(add(unsafe.Pointer(ptrmask), ti))
285 if i+ptrSize == size0 {
286 v &^= uint8(bitPtrMask << 4)
290 xbits = (*byte)(add(unsafe.Pointer(xbits), ^uintptr(0)))
292 if size0%(2*ptrSize) == 0 && size0 < size {
293 // Mark the word after last object's word as bitsDead.
294 *xbits = bitsDead << 2
304 if mp.mallocing == 0 {
305 gothrow("bad malloc")
309 mp.curg.stackguard0 = mp.curg.stack.lo + _StackGuard
311 // Note: one releasem for the acquirem just above.
312 // The other for the acquirem at start of malloc.
317 if debug.allocfreetrace != 0 {
318 tracealloc(x, size, typ)
321 if rate := MemProfileRate; rate > 0 {
322 if size < uintptr(rate) && int32(size) < c.next_sample {
323 c.next_sample -= int32(size)
326 profilealloc(mp, x, size)
331 if memstats.heap_alloc >= memstats.next_gc {
338 // implementation of new builtin
339 func newobject(typ *_type) unsafe.Pointer {
341 if typ.kind&kindNoPointers != 0 {
344 return mallocgc(uintptr(typ.size), typ, flags)
347 // implementation of make builtin for slices
348 func newarray(typ *_type, n uintptr) unsafe.Pointer {
350 if typ.kind&kindNoPointers != 0 {
353 if int(n) < 0 || (typ.size > 0 && n > _MaxMem/uintptr(typ.size)) {
354 panic("runtime: allocation size out of range")
356 return mallocgc(uintptr(typ.size)*n, typ, flags)
359 // rawmem returns a chunk of pointerless memory. It is
361 func rawmem(size uintptr) unsafe.Pointer {
362 return mallocgc(size, nil, flagNoScan|flagNoZero)
365 // round size up to next size class
366 func goroundupsize(size uintptr) uintptr {
367 if size < maxSmallSize {
369 return uintptr(class_to_size[size_to_class8[(size+7)>>3]])
371 return uintptr(class_to_size[size_to_class128[(size-1024+127)>>7]])
373 if size+pageSize < size {
376 return (size + pageSize - 1) &^ pageMask
379 func profilealloc(mp *m, x unsafe.Pointer, size uintptr) {
381 rate := MemProfileRate
382 if size < uintptr(rate) {
383 // pick next profile time
384 // If you change this, also change allocmcache.
385 if rate > 0x3fffffff { // make 2*rate not overflow
388 next := int32(fastrand1()) % (2 * int32(rate))
389 // Subtract the "remainder" of the current allocation.
390 // Otherwise objects that are close in size to sampling rate
391 // will be under-sampled, because we consistently discard this remainder.
392 next -= (int32(size) - c.next_sample)
399 mProf_Malloc(x, size)
402 // force = 1 - do GC regardless of current heap usage
403 // force = 2 - go GC and eager sweep
404 func gogc(force int32) {
405 // The gc is turned off (via enablegc) until the bootstrap has completed.
406 // Also, malloc gets called in the guts of a number of libraries that might be
407 // holding locks. To avoid deadlocks during stoptheworld, don't bother
408 // trying to run gc while holding a lock. The next mallocgc without a lock
409 // will do the gc instead.
411 if gp := getg(); gp == mp.g0 || mp.locks > 1 || !memstats.enablegc || panicking != 0 || gcpercent < 0 {
418 semacquire(&worldsema, false)
420 if force == 0 && memstats.heap_alloc < memstats.next_gc {
421 // typically threads which lost the race to grab
422 // worldsema exit here when gc is done.
423 semrelease(&worldsema)
427 // Ok, we're doing it! Stop everybody else
428 startTime := nanotime()
432 systemstack(stoptheworld)
433 if mp != acquirem() {
434 gothrow("gogc: rescheduled")
439 // Run gc on the g0 stack. We do this so that the g stack
440 // we're currently running on will no longer change. Cuts
441 // the root set down a bit (g0 stacks are not scanned, and
442 // we don't need to scan gc's internal state). We also
443 // need to switch to g0 so we can shrink the stack.
445 if debug.gctrace > 1 {
448 for i := 0; i < n; i++ {
450 startTime = nanotime()
452 // switch to g0, call gc, then switch back
453 eagersweep := force >= 2
455 gc_m(startTime, eagersweep)
461 semrelease(&worldsema)
462 systemstack(starttheworld)
466 // now that gc is done, kick off finalizer thread if needed
467 if !concurrentSweep {
468 // give the queued finalizers, if any, a chance to run
473 // GC runs a garbage collection.
479 var noptrdata struct{}
480 var enoptrbss struct{}
482 // SetFinalizer sets the finalizer associated with x to f.
483 // When the garbage collector finds an unreachable block
484 // with an associated finalizer, it clears the association and runs
485 // f(x) in a separate goroutine. This makes x reachable again, but
486 // now without an associated finalizer. Assuming that SetFinalizer
487 // is not called again, the next time the garbage collector sees
488 // that x is unreachable, it will free x.
490 // SetFinalizer(x, nil) clears any finalizer associated with x.
492 // The argument x must be a pointer to an object allocated by
493 // calling new or by taking the address of a composite literal.
494 // The argument f must be a function that takes a single argument
495 // to which x's type can be assigned, and can have arbitrary ignored return
496 // values. If either of these is not true, SetFinalizer aborts the
499 // Finalizers are run in dependency order: if A points at B, both have
500 // finalizers, and they are otherwise unreachable, only the finalizer
501 // for A runs; once A is freed, the finalizer for B can run.
502 // If a cyclic structure includes a block with a finalizer, that
503 // cycle is not guaranteed to be garbage collected and the finalizer
504 // is not guaranteed to run, because there is no ordering that
505 // respects the dependencies.
507 // The finalizer for x is scheduled to run at some arbitrary time after
508 // x becomes unreachable.
509 // There is no guarantee that finalizers will run before a program exits,
510 // so typically they are useful only for releasing non-memory resources
511 // associated with an object during a long-running program.
512 // For example, an os.File object could use a finalizer to close the
513 // associated operating system file descriptor when a program discards
514 // an os.File without calling Close, but it would be a mistake
515 // to depend on a finalizer to flush an in-memory I/O buffer such as a
516 // bufio.Writer, because the buffer would not be flushed at program exit.
518 // It is not guaranteed that a finalizer will run if the size of *x is
521 // It is not guaranteed that a finalizer will run for objects allocated
522 // in initializers for package-level variables. Such objects may be
523 // linker-allocated, not heap-allocated.
525 // A single goroutine runs all finalizers for a program, sequentially.
526 // If a finalizer must run for a long time, it should do so by starting
528 func SetFinalizer(obj interface{}, finalizer interface{}) {
529 e := (*eface)(unsafe.Pointer(&obj))
532 gothrow("runtime.SetFinalizer: first argument is nil")
534 if etyp.kind&kindMask != kindPtr {
535 gothrow("runtime.SetFinalizer: first argument is " + *etyp._string + ", not pointer")
537 ot := (*ptrtype)(unsafe.Pointer(etyp))
539 gothrow("nil elem type!")
542 // find the containing object
543 _, base, _ := findObject(e.data)
546 // 0-length objects are okay.
547 if e.data == unsafe.Pointer(&zerobase) {
551 // Global initializers might be linker-allocated.
552 // var Foo = &Object{}
554 // runtime.SetFinalizer(Foo, nil)
556 // The segments are, in order: text, rodata, noptrdata, data, bss, noptrbss.
557 if uintptr(unsafe.Pointer(&noptrdata)) <= uintptr(e.data) && uintptr(e.data) < uintptr(unsafe.Pointer(&enoptrbss)) {
560 gothrow("runtime.SetFinalizer: pointer not in allocated block")
564 // As an implementation detail we allow to set finalizers for an inner byte
565 // of an object if it could come from tiny alloc (see mallocgc for details).
566 if ot.elem == nil || ot.elem.kind&kindNoPointers == 0 || ot.elem.size >= maxTinySize {
567 gothrow("runtime.SetFinalizer: pointer not at beginning of allocated block")
571 f := (*eface)(unsafe.Pointer(&finalizer))
574 // switch to system stack and remove finalizer
576 removefinalizer(e.data)
581 if ftyp.kind&kindMask != kindFunc {
582 gothrow("runtime.SetFinalizer: second argument is " + *ftyp._string + ", not a function")
584 ft := (*functype)(unsafe.Pointer(ftyp))
585 ins := *(*[]*_type)(unsafe.Pointer(&ft.in))
586 if ft.dotdotdot || len(ins) != 1 {
587 gothrow("runtime.SetFinalizer: cannot pass " + *etyp._string + " to finalizer " + *ftyp._string)
594 case fint.kind&kindMask == kindPtr:
595 if (fint.x == nil || fint.x.name == nil || etyp.x == nil || etyp.x.name == nil) && (*ptrtype)(unsafe.Pointer(fint)).elem == ot.elem {
596 // ok - not same type, but both pointers,
597 // one or the other is unnamed, and same element type, so assignable.
600 case fint.kind&kindMask == kindInterface:
601 ityp := (*interfacetype)(unsafe.Pointer(fint))
602 if len(ityp.mhdr) == 0 {
603 // ok - satisfies empty interface
606 if _, ok := assertE2I2(ityp, obj); ok {
610 gothrow("runtime.SetFinalizer: cannot pass " + *etyp._string + " to finalizer " + *ftyp._string)
612 // compute size needed for return parameters
614 for _, t := range *(*[]*_type)(unsafe.Pointer(&ft.out)) {
615 nret = round(nret, uintptr(t.align)) + uintptr(t.size)
617 nret = round(nret, ptrSize)
619 // make sure we have a finalizer goroutine
623 if !addfinalizer(e.data, (*funcval)(f.data), nret, fint, ot) {
624 gothrow("runtime.SetFinalizer: finalizer already set")
629 // round n up to a multiple of a. a must be a power of 2.
630 func round(n, a uintptr) uintptr {
631 return (n + a - 1) &^ (a - 1)
634 // Look up pointer v in heap. Return the span containing the object,
635 // the start of the object, and the size of the object. If the object
636 // does not exist, return nil, nil, 0.
637 func findObject(v unsafe.Pointer) (s *mspan, x unsafe.Pointer, n uintptr) {
640 if ptrSize == 4 && c.local_nlookup >= 1<<30 {
641 // purge cache stats to prevent overflow
648 arena_start := uintptr(unsafe.Pointer(mheap_.arena_start))
649 arena_used := uintptr(unsafe.Pointer(mheap_.arena_used))
650 if uintptr(v) < arena_start || uintptr(v) >= arena_used {
653 p := uintptr(v) >> pageShift
654 q := p - arena_start>>pageShift
655 s = *(**mspan)(add(unsafe.Pointer(mheap_.spans), q*ptrSize))
659 x = unsafe.Pointer(uintptr(s.start) << pageShift)
661 if uintptr(v) < uintptr(x) || uintptr(v) >= uintptr(unsafe.Pointer(s.limit)) || s.state != mSpanInUse {
667 n = uintptr(s.elemsize)
668 if s.sizeclass != 0 {
669 x = add(x, (uintptr(v)-uintptr(x))/n*n)
674 var fingCreate uint32
677 // start the finalizer goroutine exactly once
678 if fingCreate == 0 && cas(&fingCreate, 0, 1) {
683 // This is the goroutine that runs all of the finalizers
699 goparkunlock(&finlock, "finalizer wait")
708 for i := int32(0); i < fb.cnt; i++ {
709 f := (*finalizer)(add(unsafe.Pointer(&fb.fin), uintptr(i)*unsafe.Sizeof(finalizer{})))
711 framesz := unsafe.Sizeof((interface{})(nil)) + uintptr(f.nret)
712 if framecap < framesz {
713 // The frame does not contain pointers interesting for GC,
714 // all not yet finalized objects are stored in finq.
715 // If we do not mark it as FlagNoScan,
716 // the last finalized object is not collected.
717 frame = mallocgc(framesz, nil, flagNoScan)
722 gothrow("missing type in runfinq")
724 switch f.fint.kind & kindMask {
726 // direct use of pointer
727 *(*unsafe.Pointer)(frame) = f.arg
729 ityp := (*interfacetype)(unsafe.Pointer(f.fint))
730 // set up with empty interface
731 (*eface)(frame)._type = &f.ot.typ
732 (*eface)(frame).data = f.arg
733 if len(ityp.mhdr) != 0 {
734 // convert to interface with methods
735 // this conversion is guaranteed to succeed - we checked in SetFinalizer
736 *(*fInterface)(frame) = assertE2I(ityp, *(*interface{})(frame))
739 gothrow("bad kind in runfinq")
741 reflectcall(unsafe.Pointer(f.fn), frame, uint32(framesz), uint32(framesz))
743 // drop finalizer queue references to finalized object
759 var persistent struct {
765 // Wrapper around sysAlloc that can allocate small chunks.
766 // There is no associated free operation.
767 // Intended for things like function/type/debug-related persistent data.
768 // If align is 0, uses default align (currently 8).
769 func persistentalloc(size, align uintptr, stat *uint64) unsafe.Pointer {
772 maxBlock = 64 << 10 // VM reservation granularity is 64K on windows
776 if align&(align-1) != 0 {
777 gothrow("persistentalloc: align is not a power of 2")
779 if align > _PageSize {
780 gothrow("persistentalloc: align is too large")
786 if size >= maxBlock {
787 return sysAlloc(size, stat)
790 lock(&persistent.lock)
791 persistent.pos = roundup(persistent.pos, align)
792 if uintptr(persistent.pos)+size > uintptr(persistent.end) {
793 persistent.pos = sysAlloc(chunk, &memstats.other_sys)
794 if persistent.pos == nil {
795 unlock(&persistent.lock)
796 gothrow("runtime: cannot allocate memory")
798 persistent.end = add(persistent.pos, chunk)
801 persistent.pos = add(persistent.pos, size)
802 unlock(&persistent.lock)
804 if stat != &memstats.other_sys {
805 xadd64(stat, int64(size))
806 xadd64(&memstats.other_sys, -int64(size))