Debian Codegraph @

libgo/go/runtime/mfixalloc.go

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