...
Source file
src/runtime/mfixalloc.go
Documentation: runtime
1
2
3
4
5
6
7
8
9 package runtime
10
11 import "unsafe"
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 type fixalloc struct {
28 size uintptr
29 first func(arg, p unsafe.Pointer)
30 arg unsafe.Pointer
31 list *mlink
32 chunk uintptr
33 nchunk uint32
34 inuse uintptr
35 stat *sysMemStat
36 zero bool
37 }
38
39
40
41
42
43
44
45
46 type mlink struct {
47 next *mlink
48 }
49
50
51
52 func (f *fixalloc) init(size uintptr, first func(arg, p unsafe.Pointer), arg unsafe.Pointer, stat *sysMemStat) {
53 f.size = size
54 f.first = first
55 f.arg = arg
56 f.list = nil
57 f.chunk = 0
58 f.nchunk = 0
59 f.inuse = 0
60 f.stat = stat
61 f.zero = true
62 }
63
64 func (f *fixalloc) alloc() unsafe.Pointer {
65 if f.size == 0 {
66 print("runtime: use of FixAlloc_Alloc before FixAlloc_Init\n")
67 throw("runtime: internal error")
68 }
69
70 if f.list != nil {
71 v := unsafe.Pointer(f.list)
72 f.list = f.list.next
73 f.inuse += f.size
74 if f.zero {
75 memclrNoHeapPointers(v, f.size)
76 }
77 return v
78 }
79 if uintptr(f.nchunk) < f.size {
80 f.chunk = uintptr(persistentalloc(_FixAllocChunk, 0, f.stat))
81 f.nchunk = _FixAllocChunk
82 }
83
84 v := unsafe.Pointer(f.chunk)
85 if f.first != nil {
86 f.first(f.arg, v)
87 }
88 f.chunk = f.chunk + f.size
89 f.nchunk -= uint32(f.size)
90 f.inuse += f.size
91 return v
92 }
93
94 func (f *fixalloc) free(p unsafe.Pointer) {
95 f.inuse -= f.size
96 v := (*mlink)(p)
97 v.next = f.list
98 f.list = v
99 }
100
View as plain text