2020-10-29 06:55:39 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
type slabEntry struct {
|
|
|
|
index int
|
2021-11-03 22:16:02 +00:00
|
|
|
handler
|
2020-10-29 06:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (entry slabEntry) isInvalid() bool {
|
|
|
|
return entry.index != -1
|
|
|
|
}
|
|
|
|
|
|
|
|
// slab is an implementation of the internal handler free list.
|
|
|
|
type slab struct {
|
|
|
|
Entries []slabEntry
|
|
|
|
free int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *slab) Put(entry handler) int {
|
|
|
|
if s.free == len(s.Entries) {
|
|
|
|
index := len(s.Entries)
|
2021-11-03 22:16:02 +00:00
|
|
|
s.Entries = append(s.Entries, slabEntry{-1, entry})
|
2020-10-29 06:55:39 +00:00
|
|
|
s.free++
|
|
|
|
return index
|
|
|
|
}
|
|
|
|
|
|
|
|
next := s.Entries[s.free].index
|
2021-11-03 22:16:02 +00:00
|
|
|
s.Entries[s.free] = slabEntry{-1, entry}
|
2020-10-29 06:55:39 +00:00
|
|
|
|
|
|
|
i := s.free
|
|
|
|
s.free = next
|
|
|
|
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *slab) Get(i int) handler {
|
|
|
|
return s.Entries[i].handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *slab) Pop(i int) handler {
|
|
|
|
popped := s.Entries[i].handler
|
2021-11-03 22:16:02 +00:00
|
|
|
s.Entries[i] = slabEntry{s.free, handler{}}
|
2020-10-29 06:55:39 +00:00
|
|
|
s.free = i
|
|
|
|
return popped
|
|
|
|
}
|