github.com/ice-blockchain/go/src@v0.0.0-20240403114104-1564d284e521/runtime/mfixalloc.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 // Fixed-size object allocator. Returned memory is not zeroed. 6 // 7 // See malloc.go for overview. 8 9 package runtime 10 11 import ( 12 "runtime/internal/sys" 13 "unsafe" 14 ) 15 16 // fixalloc is a simple free-list allocator for fixed size objects. 17 // Malloc uses a FixAlloc wrapped around sysAlloc to manage its 18 // mcache and mspan objects. 19 // 20 // Memory returned by fixalloc.alloc is zeroed by default, but the 21 // caller may take responsibility for zeroing allocations by setting 22 // the zero flag to false. This is only safe if the memory never 23 // contains heap pointers. 24 // 25 // The caller is responsible for locking around FixAlloc calls. 26 // Callers can keep state in the object but the first word is 27 // smashed by freeing and reallocating. 28 // 29 // Consider marking fixalloc'd types not in heap by embedding 30 // runtime/internal/sys.NotInHeap. 31 type fixalloc struct { 32 size uintptr 33 first func(arg, p unsafe.Pointer) // called first time p is returned 34 arg unsafe.Pointer 35 list *mlink 36 chunk uintptr // use uintptr instead of unsafe.Pointer to avoid write barriers 37 nchunk uint32 // bytes remaining in current chunk 38 nalloc uint32 // size of new chunks in bytes 39 inuse uintptr // in-use bytes now 40 stat *sysMemStat 41 zero bool // zero allocations 42 } 43 44 // A generic linked list of blocks. (Typically the block is bigger than sizeof(MLink).) 45 // Since assignments to mlink.next will result in a write barrier being performed 46 // this cannot be used by some of the internal GC structures. For example when 47 // the sweeper is placing an unmarked object on the free list it does not want the 48 // write barrier to be called since that could result in the object being reachable. 49 type mlink struct { 50 _ sys.NotInHeap 51 next *mlink 52 } 53 54 // Initialize f to allocate objects of the given size, 55 // using the allocator to obtain chunks of memory. 56 func (f *fixalloc) init(size uintptr, first func(arg, p unsafe.Pointer), arg unsafe.Pointer, stat *sysMemStat) { 57 if size > _FixAllocChunk { 58 throw("runtime: fixalloc size too large") 59 } 60 size = max(size, unsafe.Sizeof(mlink{})) 61 62 f.size = size 63 f.first = first 64 f.arg = arg 65 f.list = nil 66 f.chunk = 0 67 f.nchunk = 0 68 f.nalloc = uint32(_FixAllocChunk / size * size) // Round _FixAllocChunk down to an exact multiple of size to eliminate tail waste 69 f.inuse = 0 70 f.stat = stat 71 f.zero = true 72 } 73 74 func (f *fixalloc) alloc() unsafe.Pointer { 75 if f.size == 0 { 76 print("runtime: use of FixAlloc_Alloc before FixAlloc_Init\n") 77 throw("runtime: internal error") 78 } 79 80 if f.list != nil { 81 v := unsafe.Pointer(f.list) 82 f.list = f.list.next 83 f.inuse += f.size 84 if f.zero { 85 memclrNoHeapPointers(v, f.size) 86 } 87 return v 88 } 89 if uintptr(f.nchunk) < f.size { 90 f.chunk = uintptr(persistentalloc(uintptr(f.nalloc), 0, f.stat)) 91 f.nchunk = f.nalloc 92 } 93 94 v := unsafe.Pointer(f.chunk) 95 if f.first != nil { 96 f.first(f.arg, v) 97 } 98 f.chunk = f.chunk + f.size 99 f.nchunk -= uint32(f.size) 100 f.inuse += f.size 101 return v 102 } 103 104 func (f *fixalloc) free(p unsafe.Pointer) { 105 f.inuse -= f.size 106 v := (*mlink)(p) 107 v.next = f.list 108 f.list = v 109 }