Debian Codegraph @

freelist.go

raw symbols SCIP
1 package bolt
2
3 import (
4 "fmt"
5 "sort"
6 "unsafe"
7 )
8
9 // freelist represents a list of all pages that are available for allocation.
10 // It also tracks pages that have been freed but are still in use by open transactions.
11 type freelist struct {
12 ids []pgid // all free and available free page ids.
13 pending map[txid][]pgid // mapping of soon-to-be free page ids by tx.
14 cache map[pgid]bool // fast lookup of all free and pending page ids.
15 }
16
17 // newFreelist returns an empty, initialized freelist.
18 func newFreelist() *freelist {
19 return &freelist{
20 pending: make(map[txid][]pgid),
21 cache: make(map[pgid]bool),
22 }
23 }
24
25 // size returns the size of the page after serialization.
26 func (f *freelist) size() int {
27 n := f.count()
28 if n >= 0xFFFF {
29 // The first element will be used to store the count. See freelist.write.
30 n++
31 }
32 return pageHeaderSize + (int(unsafe.Sizeof(pgid(0))) * n)
33 }
34
35 // count returns count of pages on the freelist
36 func (f *freelist) count() int {
37 return f.free_count() + f.pending_count()
38 }
39
40 // free_count returns count of free pages
41 func (f *freelist) free_count() int {
42 return len(f.ids)
43 }
44
45 // pending_count returns count of pending pages
46 func (f *freelist) pending_count() int {
47 var count int
48 for _, list := range f.pending {
49 count += len(list)
50 }
51 return count
52 }
53
54 // copyall copies into dst a list of all free ids and all pending ids in one sorted list.
55 // f.count returns the minimum length required for dst.
56 func (f *freelist) copyall(dst []pgid) {
57 m := make(pgids, 0, f.pending_count())
58 for _, list := range f.pending {
59 m = append(m, list...)
60 }
61 sort.Sort(m)
62 mergepgids(dst, f.ids, m)
63 }
64
65 // allocate returns the starting page id of a contiguous list of pages of a given size.
66 // If a contiguous block cannot be found then 0 is returned.
67 func (f *freelist) allocate(n int) pgid {
68 if len(f.ids) == 0 {
69 return 0
70 }
71
72 var initial, previd pgid
73 for i, id := range f.ids {
74 if id <= 1 {
75 panic(fmt.Sprintf("invalid page allocation: %d", id))
76 }
77
78 // Reset initial page if this is not contiguous.
79 if previd == 0 || id-previd != 1 {
80 initial = id
81 }
82
83 // If we found a contiguous block then remove it and return it.
84 if (id-initial)+1 == pgid(n) {
85 // If we're allocating off the beginning then take the fast path
86 // and just adjust the existing slice. This will use extra memory
87 // temporarily but the append() in free() will realloc the slice
88 // as is necessary.
89 if (i + 1) == n {
90 f.ids = f.ids[i+1:]
91 } else {
92 copy(f.ids[i-n+1:], f.ids[i+1:])
93 f.ids = f.ids[:len(f.ids)-n]
94 }
95
96 // Remove from the free cache.
97 for i := pgid(0); i < pgid(n); i++ {
98 delete(f.cache, initial+i)
99 }
100
101 return initial
102 }
103
104 previd = id
105 }
106 return 0
107 }
108
109 // free releases a page and its overflow for a given transaction id.
110 // If the page is already free then a panic will occur.
111 func (f *freelist) free(txid txid, p *page) {
112 if p.id <= 1 {
113 panic(fmt.Sprintf("cannot free page 0 or 1: %d", p.id))
114 }
115
116 // Free page and all its overflow pages.
117 var ids = f.pending[txid]
118 for id := p.id; id <= p.id+pgid(p.overflow); id++ {
119 // Verify that page is not already free.
120 if f.cache[id] {
121 panic(fmt.Sprintf("page %d already freed", id))
122 }
123
124 // Add to the freelist and cache.
125 ids = append(ids, id)
126 f.cache[id] = true
127 }
128 f.pending[txid] = ids
129 }
130
131 // release moves all page ids for a transaction id (or older) to the freelist.
132 func (f *freelist) release(txid txid) {
133 m := make(pgids, 0)
134 for tid, ids := range f.pending {
135 if tid <= txid {
136 // Move transaction's pending pages to the available freelist.
137 // Don't remove from the cache since the page is still free.
138 m = append(m, ids...)
139 delete(f.pending, tid)
140 }
141 }
142 sort.Sort(m)
143 f.ids = pgids(f.ids).merge(m)
144 }
145
146 // rollback removes the pages from a given pending tx.
147 func (f *freelist) rollback(txid txid) {
148 // Remove page ids from cache.
149 for _, id := range f.pending[txid] {
150 delete(f.cache, id)
151 }
152
153 // Remove pages from pending list.
154 delete(f.pending, txid)
155 }
156
157 // freed returns whether a given page is in the free list.
158 func (f *freelist) freed(pgid pgid) bool {
159 return f.cache[pgid]
160 }
161
162 // read initializes the freelist from a freelist page.
163 func (f *freelist) read(p *page) {
164 // If the page.count is at the max uint16 value (64k) then it's considered
165 // an overflow and the size of the freelist is stored as the first element.
166 idx, count := 0, int(p.count)
167 if count == 0xFFFF {
168 idx = 1
169 count = int(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0])
170 }
171
172 // Copy the list of page ids from the freelist.
173 if count == 0 {
174 f.ids = nil
175 } else {
176 ids := ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[idx:count]
177 f.ids = make([]pgid, len(ids))
178 copy(f.ids, ids)
179
180 // Make sure they're sorted.
181 sort.Sort(pgids(f.ids))
182 }
183
184 // Rebuild the page cache.
185 f.reindex()
186 }
187
188 // write writes the page ids onto a freelist page. All free and pending ids are
189 // saved to disk since in the event of a program crash, all pending ids will
190 // become free.
191 func (f *freelist) write(p *page) error {
192 // Combine the old free pgids and pgids waiting on an open transaction.
193
194 // Update the header flag.
195 p.flags |= freelistPageFlag
196
197 // The page.count can only hold up to 64k elements so if we overflow that
198 // number then we handle it by putting the size in the first element.
199 lenids := f.count()
200 if lenids == 0 {
201 p.count = uint16(lenids)
202 } else if lenids < 0xFFFF {
203 p.count = uint16(lenids)
204 f.copyall(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[:])
205 } else {
206 p.count = 0xFFFF
207 ((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[0] = pgid(lenids)
208 f.copyall(((*[maxAllocSize]pgid)(unsafe.Pointer(&p.ptr)))[1:])
209 }
210
211 return nil
212 }
213
214 // reload reads the freelist from a page and filters out pending items.
215 func (f *freelist) reload(p *page) {
216 f.read(p)
217
218 // Build a cache of only pending pages.
219 pcache := make(map[pgid]bool)
220 for _, pendingIDs := range f.pending {
221 for _, pendingID := range pendingIDs {
222 pcache[pendingID] = true
223 }
224 }
225
226 // Check each page in the freelist and build a new available freelist
227 // with any pages not in the pending lists.
228 var a []pgid
229 for _, id := range f.ids {
230 if !pcache[id] {
231 a = append(a, id)
232 }
233 }
234 f.ids = a
235
236 // Once the available list is rebuilt then rebuild the free cache so that
237 // it includes the available and pending free pages.
238 f.reindex()
239 }
240
241 // reindex rebuilds the free cache based on available and pending free lists.
242 func (f *freelist) reindex() {
243 f.cache = make(map[pgid]bool, len(f.ids))
244 for _, id := range f.ids {
245 f.cache[id] = true
246 }
247 for _, pendingIDs := range f.pending {
248 for _, pendingID := range pendingIDs {
249 f.cache[pendingID] = true
250 }
251 }
252 }