allocator.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright 2018-present the CoreDHCP Authors. All rights reserved
  2. // This source code is licensed under the MIT license found in the
  3. // LICENSE file in the root directory of this source tree.
  4. // Package allocators provides the interface and the algorithm(s) for allocation of ipv6
  5. // prefixes of various sizes within a larger prefix.
  6. // There are many many parallels with memory allocation.
  7. package allocators
  8. import (
  9. "fmt"
  10. "net"
  11. )
  12. // Allocator is the interface to the address allocator. It only finds and
  13. // allocates blocks and is not concerned with lease-specific questions like
  14. // expiration (ie garbage collection needs to be handled separately)
  15. type Allocator interface {
  16. // Allocate finds a suitable prefix of the given size and returns it.
  17. //
  18. // hint is a prefix, which the client desires especially, and that the
  19. // allocator MAY try to return; the allocator SHOULD try to return a prefix of
  20. // the same size as the given hint prefix. The allocator MUST NOT return an
  21. // error if a prefix was successfully assigned, even if the prefix has nothing
  22. // in common with the hinted prefix
  23. Allocate(hint net.IPNet) (net.IPNet, error)
  24. // Free returns the prefix containing the given network to the pool
  25. //
  26. // Free may return a DoubleFreeError if the prefix being returned was not
  27. // previously allocated
  28. Free(net.IPNet) error
  29. }
  30. // ErrDoubleFree is an error type returned by Allocator.Free() when a
  31. // non-allocated block is passed
  32. type ErrDoubleFree struct {
  33. Loc net.IPNet
  34. }
  35. // String returns a human-readable error message for a DoubleFree error
  36. func (err *ErrDoubleFree) Error() string {
  37. return fmt.Sprint("Attempted to free unallocated block at ", err.Loc.String())
  38. }