| 59 | } |
| 60 | |
| 61 | func (f *hashMap) Allocate(txid common.Txid, n int) common.Pgid { |
| 62 | if n == 0 { |
| 63 | return 0 |
| 64 | } |
| 65 | |
| 66 | // if we have a exact size match just return short path |
| 67 | if bm, ok := f.freemaps[uint64(n)]; ok { |
| 68 | for pid := range bm { |
| 69 | // remove the span |
| 70 | f.delSpan(pid, uint64(n)) |
| 71 | |
| 72 | f.allocs[pid] = txid |
| 73 | |
| 74 | for i := common.Pgid(0); i < common.Pgid(n); i++ { |
| 75 | delete(f.cache, pid+i) |
| 76 | } |
| 77 | return pid |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | // lookup the map to find larger span |
| 82 | for size, bm := range f.freemaps { |
| 83 | if size < uint64(n) { |
| 84 | continue |
| 85 | } |
| 86 | |
| 87 | for pid := range bm { |
| 88 | // remove the initial |
| 89 | f.delSpan(pid, size) |
| 90 | |
| 91 | f.allocs[pid] = txid |
| 92 | |
| 93 | remain := size - uint64(n) |
| 94 | |
| 95 | // add remain span |
| 96 | f.addSpan(pid+common.Pgid(n), remain) |
| 97 | |
| 98 | for i := common.Pgid(0); i < common.Pgid(n); i++ { |
| 99 | delete(f.cache, pid+i) |
| 100 | } |
| 101 | return pid |
| 102 | } |
| 103 | } |
| 104 | |
| 105 | return 0 |
| 106 | } |
| 107 | |
| 108 | func (f *hashMap) FreeCount() int { |
| 109 | common.Verify(func() { |