This repository was archived by the owner on Mar 9, 2019. It is now read-only.
File tree Expand file tree Collapse file tree 3 files changed +23
-6
lines changed
Expand file tree Collapse file tree 3 files changed +23
-6
lines changed Original file line number Diff line number Diff line change @@ -166,12 +166,16 @@ func (f *freelist) read(p *page) {
166166 }
167167
168168 // Copy the list of page ids from the freelist.
169- ids := ((* [maxAllocSize ]pgid )(unsafe .Pointer (& p .ptr )))[idx :count ]
170- f .ids = make ([]pgid , len (ids ))
171- copy (f .ids , ids )
169+ if count == 0 {
170+ f .ids = nil
171+ } else {
172+ ids := ((* [maxAllocSize ]pgid )(unsafe .Pointer (& p .ptr )))[idx :count ]
173+ f .ids = make ([]pgid , len (ids ))
174+ copy (f .ids , ids )
172175
173- // Make sure they're sorted.
174- sort .Sort (pgids (f .ids ))
176+ // Make sure they're sorted.
177+ sort .Sort (pgids (f .ids ))
178+ }
175179
176180 // Rebuild the page cache.
177181 f .reindex ()
@@ -189,7 +193,9 @@ func (f *freelist) write(p *page) error {
189193
190194 // The page.count can only hold up to 64k elements so if we overflow that
191195 // number then we handle it by putting the size in the first element.
192- if len (ids ) < 0xFFFF {
196+ if len (ids ) == 0 {
197+ p .count = uint16 (len (ids ))
198+ } else if len (ids ) < 0xFFFF {
193199 p .count = uint16 (len (ids ))
194200 copy (((* [maxAllocSize ]pgid )(unsafe .Pointer (& p .ptr )))[:], ids )
195201 } else {
Original file line number Diff line number Diff line change @@ -201,6 +201,11 @@ func (n *node) write(p *page) {
201201 }
202202 p .count = uint16 (len (n .inodes ))
203203
204+ // Stop here if there are no items to write.
205+ if p .count == 0 {
206+ return
207+ }
208+
204209 // Loop over each item and write it to the page.
205210 b := (* [maxAllocSize ]byte )(unsafe .Pointer (& p .ptr ))[n .pageElementSize ()* len (n .inodes ):]
206211 for i , item := range n .inodes {
Original file line number Diff line number Diff line change @@ -62,6 +62,9 @@ func (p *page) leafPageElement(index uint16) *leafPageElement {
6262
6363// leafPageElements retrieves a list of leaf nodes.
6464func (p * page ) leafPageElements () []leafPageElement {
65+ if p .count == 0 {
66+ return nil
67+ }
6568 return ((* [0x7FFFFFF ]leafPageElement )(unsafe .Pointer (& p .ptr )))[:]
6669}
6770
@@ -72,6 +75,9 @@ func (p *page) branchPageElement(index uint16) *branchPageElement {
7275
7376// branchPageElements retrieves a list of branch nodes.
7477func (p * page ) branchPageElements () []branchPageElement {
78+ if p .count == 0 {
79+ return nil
80+ }
7581 return ((* [0x7FFFFFF ]branchPageElement )(unsafe .Pointer (& p .ptr )))[:]
7682}
7783
You can’t perform that action at this time.
0 commit comments