github.com/twelsh-aw/go/src@v0.0.0-20230516233729-a56fe86a7c81/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 if min := unsafe.Sizeof(mlink{}); size < min { 61 size = min 62 } 63 64 f.size = size 65 f.first = first 66 f.arg = arg 67 f.list = nil 68 f.chunk = 0 69 f.nchunk = 0 70 f.nalloc = uint32(_FixAllocChunk / size * size) // Round _FixAllocChunk down to an exact multiple of size to eliminate tail waste 71 f.inuse = 0 72 f.stat = stat 73 f.zero = true 74 } 75 76 func (f *fixalloc) alloc() unsafe.Pointer { 77 if f.size == 0 { 78 print("runtime: use of FixAlloc_Alloc before FixAlloc_Init\n") 79 throw("runtime: internal error") 80 } 81 82 if f.list != nil { 83 v := unsafe.Pointer(f.list) 84 f.list = f.list.next 85 f.inuse += f.size 86 if f.zero { 87 memclrNoHeapPointers(v, f.size) 88 } 89 return v 90 } 91 if uintptr(f.nchunk) < f.size { 92 f.chunk = uintptr(persistentalloc(uintptr(f.nalloc), 0, f.stat)) 93 f.nchunk = f.nalloc 94 } 95 96 v := unsafe.Pointer(f.chunk) 97 if f.first != nil { 98 f.first(f.arg, v) 99 } 100 f.chunk = f.chunk + f.size 101 f.nchunk -= uint32(f.size) 102 f.inuse += f.size 103 return v 104 } 105 106 func (f *fixalloc) free(p unsafe.Pointer) { 107 f.inuse -= f.size 108 v := (*mlink)(p) 109 v.next = f.list 110 f.list = v 111 }