package freelistimport ()type array struct { *shared ids []common.Pgid// all free and available free page ids.}func ( *array) ( common.Pgids) { .ids = .reindex()}func ( *array) ( common.Txid, int) common.Pgid {iflen(.ids) == 0 {return0 }var , common.Pgidfor , := range .ids {if <= 1 {panic(fmt.Sprintf("invalid page allocation: %d", )) }// Reset initial page if this is not contiguous.if == 0 || - != 1 { = }// If we found a contiguous block then remove it and return it.if (-)+1 == common.Pgid() {// If we're allocating off the beginning then take the fast path // and just adjust the existing slice. This will use extra memory // temporarily but the append() in free() will realloc the slice // as is necessary.if ( + 1) == { .ids = .ids[+1:] } else {copy(.ids[-+1:], .ids[+1:]) .ids = .ids[:len(.ids)-] }// Remove from the free cache.for := common.Pgid(0); < common.Pgid(); ++ {delete(.cache, +) } .allocs[] = return } = }return0}func ( *array) () int {returnlen(.ids)}func ( *array) () common.Pgids {return .ids}func ( *array) ( common.Pgids) {sort.Sort()common.Verify(func() { := make(map[common.Pgid]struct{})for , := range .ids {// The existing f.ids shouldn't have duplicated free ID.if , := []; {panic(fmt.Sprintf("detected duplicated free page ID: %d in existing f.ids: %v", , .ids)) } [] = struct{}{} } := common.Pgid(0)for , := range {// The ids shouldn't have duplicated free ID. Note page 0 and 1 // are reserved for meta pages, so they can never be free page IDs.if == {panic(fmt.Sprintf("detected duplicated free ID: %d in ids: %v", , )) } = // The ids shouldn't have any overlap with the existing f.ids.if , := []; {panic(fmt.Sprintf("detected overlapped free page ID: %d between ids: %v and existing f.ids: %v", , , .ids)) } } }) .ids = common.Pgids(.ids).Merge()}func () Interface { := &array{shared: newShared(),ids: []common.Pgid{}, } .Interface = return}
The pages are generated with Goldsv0.8.7. (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu.
PR and bug reports are welcome and can be submitted to the issue list.
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds.