A universal allocator framework and some allocator implementations
Allocator interface is as below, it has 4 universal methods,
Assign
is for assigning the using objects in rangeAllocate
is for allocating an valid object from rangeRelease
is for releasing an object from rangeHas
is for testing whether an object is in range
type Interface interface {
Assign(RangeID) error
Allocate() (RangeID, interface{}, error)
Release(RangeID) error
Has(RangeID) bool
}
Range interface is as below, it has 2 universal methods,
Contains
is for testing whether an object is in rangeFirst
is for returning an iterator which can traverse the whole range
type Range interface {
Contains(RangeID) bool
First() RangeIterator
}
RangeIterator interface is as below, it has 3 universal methods, the 3 methods can help us traverse the whole range.
type RangeIterator interface {
Get() (RangeID, interface{})
Next()
InRange() bool
}
There is an universal implementation to generate an Allocator from an Range.
You can use func NewAllocator(ranger Range) Interface {}
method to get this.
There are also some common allocators implemented through the interfaces and methods above.