2015-06-12 15:03:33 -04:00
|
|
|
// Package bitseq provides a structure and utilities for representing long bitmask
|
|
|
|
// as sequence of run-lenght encoded blocks. It operates direclty on the encoded
|
|
|
|
// representation, it does not decode/encode.
|
|
|
|
package bitseq
|
|
|
|
|
|
|
|
import (
|
2015-06-18 22:14:03 -04:00
|
|
|
"encoding/binary"
|
2015-06-12 15:03:33 -04:00
|
|
|
"fmt"
|
2015-06-14 13:21:59 -04:00
|
|
|
"sync"
|
2015-06-12 18:06:42 -04:00
|
|
|
|
2015-06-15 14:43:02 -04:00
|
|
|
"github.com/docker/libnetwork/datastore"
|
2015-06-24 15:10:54 -04:00
|
|
|
"github.com/docker/libnetwork/types"
|
2015-06-12 15:03:33 -04:00
|
|
|
)
|
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
// block sequence constants
|
2015-06-12 15:03:33 -04:00
|
|
|
// If needed we can think of making these configurable
|
|
|
|
const (
|
2015-06-24 13:04:34 -04:00
|
|
|
blockLen = uint32(32)
|
2015-06-12 15:03:33 -04:00
|
|
|
blockBytes = blockLen / 8
|
2015-06-24 13:04:34 -04:00
|
|
|
blockMAX = uint32(1<<blockLen - 1)
|
|
|
|
blockFirstBit = uint32(1) << (blockLen - 1)
|
|
|
|
invalidPos = blockMAX
|
2015-06-12 15:03:33 -04:00
|
|
|
)
|
|
|
|
|
2015-06-24 15:10:54 -04:00
|
|
|
var (
|
|
|
|
errNoBitAvailable = fmt.Errorf("no bit available")
|
|
|
|
)
|
|
|
|
|
2015-06-13 16:35:43 -04:00
|
|
|
// Handle contains the sequece representing the bitmask and its identifier
|
|
|
|
type Handle struct {
|
2015-06-16 17:46:51 -04:00
|
|
|
bits uint32
|
|
|
|
unselected uint32
|
2015-06-24 11:52:53 -04:00
|
|
|
head *sequence
|
2015-06-16 17:46:51 -04:00
|
|
|
app string
|
|
|
|
id string
|
|
|
|
dbIndex uint64
|
2015-06-18 18:13:38 -04:00
|
|
|
dbExists bool
|
2015-06-16 17:46:51 -04:00
|
|
|
store datastore.DataStore
|
2015-06-14 13:21:59 -04:00
|
|
|
sync.Mutex
|
2015-06-13 16:35:43 -04:00
|
|
|
}
|
|
|
|
|
2015-06-14 13:21:59 -04:00
|
|
|
// NewHandle returns a thread-safe instance of the bitmask handler
|
2015-06-16 17:46:51 -04:00
|
|
|
func NewHandle(app string, ds datastore.DataStore, id string, numElements uint32) (*Handle, error) {
|
2015-06-15 14:43:02 -04:00
|
|
|
h := &Handle{
|
2015-06-16 17:46:51 -04:00
|
|
|
app: app,
|
|
|
|
id: id,
|
|
|
|
store: ds,
|
|
|
|
bits: numElements,
|
|
|
|
unselected: numElements,
|
2015-06-24 11:52:53 -04:00
|
|
|
head: &sequence{
|
|
|
|
block: 0x0,
|
|
|
|
count: getNumBlocks(numElements),
|
2015-06-13 16:35:43 -04:00
|
|
|
},
|
|
|
|
}
|
2015-06-15 21:28:00 -04:00
|
|
|
|
|
|
|
if h.store == nil {
|
2015-06-16 17:46:51 -04:00
|
|
|
return h, nil
|
2015-06-15 21:28:00 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Register for status changes
|
2015-06-15 14:43:02 -04:00
|
|
|
h.watchForChanges()
|
2015-06-15 21:28:00 -04:00
|
|
|
|
|
|
|
// Get the initial status from the ds if present.
|
2015-07-04 19:48:09 -04:00
|
|
|
if err := h.store.GetObject(datastore.Key(h.Key()...), h); err != nil && err != datastore.ErrKeyNotFound {
|
2015-06-18 18:13:38 -04:00
|
|
|
return nil, err
|
2015-06-15 21:28:00 -04:00
|
|
|
}
|
2015-06-18 15:06:11 -04:00
|
|
|
|
2015-07-04 19:48:09 -04:00
|
|
|
return h, nil
|
2015-06-13 16:35:43 -04:00
|
|
|
}
|
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
// sequence represents a recurring sequence of 32 bits long bitmasks
|
|
|
|
type sequence struct {
|
|
|
|
block uint32 // block is a symbol representing 4 byte long allocation bitmask
|
|
|
|
count uint32 // number of consecutive blocks (symbols)
|
|
|
|
next *sequence // next sequence
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a string representation of the block sequence starting from this block
|
2015-06-24 11:52:53 -04:00
|
|
|
func (s *sequence) toString() string {
|
2015-06-12 15:03:33 -04:00
|
|
|
var nextBlock string
|
2015-06-24 11:52:53 -04:00
|
|
|
if s.next == nil {
|
2015-06-12 15:03:33 -04:00
|
|
|
nextBlock = "end"
|
|
|
|
} else {
|
2015-06-24 11:52:53 -04:00
|
|
|
nextBlock = s.next.toString()
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
2015-06-24 11:52:53 -04:00
|
|
|
return fmt.Sprintf("(0x%x, %d)->%s", s.block, s.count, nextBlock)
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAvailableBit returns the position of the first unset bit in the bitmask represented by this sequence
|
2015-09-17 18:16:21 -04:00
|
|
|
func (s *sequence) getAvailableBit(from uint32) (uint32, uint32, error) {
|
2015-06-24 11:52:53 -04:00
|
|
|
if s.block == blockMAX || s.count == 0 {
|
2015-09-17 18:16:21 -04:00
|
|
|
return invalidPos, invalidPos, errNoBitAvailable
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
2015-09-17 18:16:21 -04:00
|
|
|
bits := from
|
|
|
|
bitSel := blockFirstBit >> from
|
2015-06-24 11:52:53 -04:00
|
|
|
for bitSel > 0 && s.block&bitSel != 0 {
|
2015-06-12 15:03:33 -04:00
|
|
|
bitSel >>= 1
|
|
|
|
bits++
|
|
|
|
}
|
2015-06-24 13:04:34 -04:00
|
|
|
return bits / 8, bits % 8, nil
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
|
2015-06-15 21:28:00 -04:00
|
|
|
// GetCopy returns a copy of the linked list rooted at this node
|
2015-06-24 11:52:53 -04:00
|
|
|
func (s *sequence) getCopy() *sequence {
|
|
|
|
n := &sequence{block: s.block, count: s.count}
|
2015-06-15 21:28:00 -04:00
|
|
|
pn := n
|
2015-06-24 11:52:53 -04:00
|
|
|
ps := s.next
|
2015-06-15 21:28:00 -04:00
|
|
|
for ps != nil {
|
2015-06-24 11:52:53 -04:00
|
|
|
pn.next = &sequence{block: ps.block, count: ps.count}
|
|
|
|
pn = pn.next
|
|
|
|
ps = ps.next
|
2015-06-15 21:28:00 -04:00
|
|
|
}
|
|
|
|
return n
|
|
|
|
}
|
|
|
|
|
2015-06-12 15:03:33 -04:00
|
|
|
// Equal checks if this sequence is equal to the passed one
|
2015-06-24 11:52:53 -04:00
|
|
|
func (s *sequence) equal(o *sequence) bool {
|
2015-06-12 15:03:33 -04:00
|
|
|
this := s
|
|
|
|
other := o
|
|
|
|
for this != nil {
|
|
|
|
if other == nil {
|
|
|
|
return false
|
|
|
|
}
|
2015-06-24 11:52:53 -04:00
|
|
|
if this.block != other.block || this.count != other.count {
|
2015-06-12 15:03:33 -04:00
|
|
|
return false
|
|
|
|
}
|
2015-06-24 11:52:53 -04:00
|
|
|
this = this.next
|
|
|
|
other = other.next
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
// Check if other is longer than this
|
|
|
|
if other != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2015-06-12 18:06:42 -04:00
|
|
|
// ToByteArray converts the sequence into a byte array
|
2015-06-24 11:52:53 -04:00
|
|
|
func (s *sequence) toByteArray() ([]byte, error) {
|
2015-06-12 18:06:42 -04:00
|
|
|
var bb []byte
|
|
|
|
|
|
|
|
p := s
|
|
|
|
for p != nil {
|
2015-06-18 22:14:03 -04:00
|
|
|
b := make([]byte, 8)
|
2015-06-24 11:52:53 -04:00
|
|
|
binary.BigEndian.PutUint32(b[0:], p.block)
|
|
|
|
binary.BigEndian.PutUint32(b[4:], p.count)
|
2015-06-18 22:14:03 -04:00
|
|
|
bb = append(bb, b...)
|
2015-06-24 11:52:53 -04:00
|
|
|
p = p.next
|
2015-06-12 18:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return bb, nil
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:10:54 -04:00
|
|
|
// fromByteArray construct the sequence from the byte array
|
2015-06-24 11:52:53 -04:00
|
|
|
func (s *sequence) fromByteArray(data []byte) error {
|
2015-06-12 18:06:42 -04:00
|
|
|
l := len(data)
|
|
|
|
if l%8 != 0 {
|
2015-06-18 15:06:11 -04:00
|
|
|
return fmt.Errorf("cannot deserialize byte sequence of lenght %d (%v)", l, data)
|
2015-06-12 18:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
p := s
|
|
|
|
i := 0
|
|
|
|
for {
|
2015-06-24 11:52:53 -04:00
|
|
|
p.block = binary.BigEndian.Uint32(data[i : i+4])
|
|
|
|
p.count = binary.BigEndian.Uint32(data[i+4 : i+8])
|
2015-06-12 18:06:42 -04:00
|
|
|
i += 8
|
|
|
|
if i == l {
|
|
|
|
break
|
|
|
|
}
|
2015-06-24 11:52:53 -04:00
|
|
|
p.next = &sequence{}
|
|
|
|
p = p.next
|
2015-06-12 18:06:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:10:54 -04:00
|
|
|
func (h *Handle) getCopy() *Handle {
|
|
|
|
return &Handle{
|
|
|
|
bits: h.bits,
|
|
|
|
unselected: h.unselected,
|
|
|
|
head: h.head.getCopy(),
|
|
|
|
app: h.app,
|
|
|
|
id: h.id,
|
|
|
|
dbIndex: h.dbIndex,
|
|
|
|
dbExists: h.dbExists,
|
|
|
|
store: h.store,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-17 18:16:21 -04:00
|
|
|
// SetAnyInRange atomically sets the first unset bit in the specified range in the sequence and returns the corresponding ordinal
|
|
|
|
func (h *Handle) SetAnyInRange(start, end uint32) (uint32, error) {
|
|
|
|
if end-start <= 0 || end >= h.bits {
|
|
|
|
return invalidPos, fmt.Errorf("invalid bit range [%d, %d]", start, end)
|
|
|
|
}
|
|
|
|
if h.Unselected() == 0 {
|
|
|
|
return invalidPos, errNoBitAvailable
|
|
|
|
}
|
|
|
|
return h.set(0, start, end, true, false)
|
|
|
|
}
|
|
|
|
|
2015-06-24 15:10:54 -04:00
|
|
|
// SetAny atomically sets the first unset bit in the sequence and returns the corresponding ordinal
|
|
|
|
func (h *Handle) SetAny() (uint32, error) {
|
|
|
|
if h.Unselected() == 0 {
|
|
|
|
return invalidPos, errNoBitAvailable
|
|
|
|
}
|
2015-09-17 18:16:21 -04:00
|
|
|
return h.set(0, 0, h.bits-1, true, false)
|
2015-06-24 15:10:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set atomically sets the corresponding bit in the sequence
|
|
|
|
func (h *Handle) Set(ordinal uint32) error {
|
|
|
|
if err := h.validateOrdinal(ordinal); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-09-17 18:16:21 -04:00
|
|
|
_, err := h.set(ordinal, 0, 0, false, false)
|
2015-06-24 15:10:54 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unset atomically unsets the corresponding bit in the sequence
|
|
|
|
func (h *Handle) Unset(ordinal uint32) error {
|
|
|
|
if err := h.validateOrdinal(ordinal); err != nil {
|
|
|
|
return err
|
2015-06-18 18:13:38 -04:00
|
|
|
}
|
2015-09-17 18:16:21 -04:00
|
|
|
_, err := h.set(ordinal, 0, 0, false, true)
|
2015-06-24 15:10:54 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsSet atomically checks if the ordinal bit is set. In case ordinal
|
|
|
|
// is outside of the bit sequence limits, false is returned.
|
|
|
|
func (h *Handle) IsSet(ordinal uint32) bool {
|
|
|
|
if err := h.validateOrdinal(ordinal); err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
h.Lock()
|
|
|
|
_, _, err := checkIfAvailable(h.head, ordinal)
|
2015-06-15 14:43:02 -04:00
|
|
|
h.Unlock()
|
2015-06-24 15:10:54 -04:00
|
|
|
return err != nil
|
|
|
|
}
|
2015-06-15 21:28:00 -04:00
|
|
|
|
2015-06-24 15:10:54 -04:00
|
|
|
// set/reset the bit
|
2015-09-17 18:16:21 -04:00
|
|
|
func (h *Handle) set(ordinal, start, end uint32, any bool, release bool) (uint32, error) {
|
2015-06-24 15:10:54 -04:00
|
|
|
var (
|
|
|
|
bitPos uint32
|
|
|
|
bytePos uint32
|
|
|
|
ret uint32
|
|
|
|
err error
|
|
|
|
)
|
2015-06-15 21:28:00 -04:00
|
|
|
|
2015-06-24 15:10:54 -04:00
|
|
|
for {
|
2015-06-15 21:28:00 -04:00
|
|
|
h.Lock()
|
2015-06-24 15:10:54 -04:00
|
|
|
// Get position if available
|
2015-06-16 17:46:51 -04:00
|
|
|
if release {
|
2015-06-24 15:10:54 -04:00
|
|
|
bytePos, bitPos = ordinalToPos(ordinal)
|
2015-06-16 17:46:51 -04:00
|
|
|
} else {
|
2015-06-24 15:10:54 -04:00
|
|
|
if any {
|
2015-09-17 18:16:21 -04:00
|
|
|
bytePos, bitPos, err = getFirstAvailable(h.head, start)
|
2015-06-24 15:10:54 -04:00
|
|
|
ret = posToOrdinal(bytePos, bitPos)
|
2015-09-17 18:16:21 -04:00
|
|
|
if end < ret {
|
|
|
|
err = errNoBitAvailable
|
|
|
|
}
|
2015-06-24 15:10:54 -04:00
|
|
|
} else {
|
|
|
|
bytePos, bitPos, err = checkIfAvailable(h.head, ordinal)
|
|
|
|
ret = ordinal
|
|
|
|
}
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
2015-06-24 15:10:54 -04:00
|
|
|
if err != nil {
|
|
|
|
h.Unlock()
|
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2015-09-04 21:15:54 -04:00
|
|
|
// Create a private copy of h and work on it
|
2015-06-24 15:10:54 -04:00
|
|
|
nh := h.getCopy()
|
2015-06-15 21:28:00 -04:00
|
|
|
h.Unlock()
|
2015-06-24 15:10:54 -04:00
|
|
|
|
|
|
|
nh.head = pushReservation(bytePos, bitPos, nh.head, release)
|
|
|
|
if release {
|
|
|
|
nh.unselected++
|
|
|
|
} else {
|
|
|
|
nh.unselected--
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attempt to write private copy to store
|
|
|
|
if err := nh.writeToStore(); err != nil {
|
|
|
|
if _, ok := err.(types.RetryError); !ok {
|
|
|
|
return ret, fmt.Errorf("internal failure while setting the bit: %v", err)
|
|
|
|
}
|
|
|
|
// Retry
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2015-09-04 21:15:54 -04:00
|
|
|
// Previous atomic push was succesfull. Save private copy to local copy
|
2015-06-24 15:10:54 -04:00
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
h.unselected = nh.unselected
|
|
|
|
h.head = nh.head
|
|
|
|
h.dbExists = nh.dbExists
|
|
|
|
h.dbIndex = nh.dbIndex
|
|
|
|
return ret, nil
|
2015-06-15 21:28:00 -04:00
|
|
|
}
|
2015-06-24 15:10:54 -04:00
|
|
|
}
|
2015-06-15 21:28:00 -04:00
|
|
|
|
2015-06-24 15:10:54 -04:00
|
|
|
// checks is needed because to cover the case where the number of bits is not a multiple of blockLen
|
|
|
|
func (h *Handle) validateOrdinal(ordinal uint32) error {
|
2015-09-17 18:16:21 -04:00
|
|
|
if ordinal >= h.bits {
|
2015-06-24 15:10:54 -04:00
|
|
|
return fmt.Errorf("bit does not belong to the sequence")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-06-16 17:46:51 -04:00
|
|
|
// Destroy removes from the datastore the data belonging to this handle
|
2015-09-22 16:20:55 -04:00
|
|
|
func (h *Handle) Destroy() error {
|
2015-10-03 19:11:50 -04:00
|
|
|
for {
|
|
|
|
if err := h.deleteFromStore(); err != nil {
|
|
|
|
if _, ok := err.(types.RetryError); !ok {
|
|
|
|
return fmt.Errorf("internal failure while destroying the sequence: %v", err)
|
|
|
|
}
|
|
|
|
// Fetch latest
|
|
|
|
if err := h.store.GetObject(datastore.Key(h.Key()...), h); err != nil {
|
|
|
|
if err == datastore.ErrKeyNotFound { // already removed
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("failed to fetch from store when destroying the sequence: %v", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToByteArray converts this handle's data into a byte array
|
|
|
|
func (h *Handle) ToByteArray() ([]byte, error) {
|
|
|
|
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
2015-06-18 22:14:03 -04:00
|
|
|
ba := make([]byte, 8)
|
|
|
|
binary.BigEndian.PutUint32(ba[0:], h.bits)
|
|
|
|
binary.BigEndian.PutUint32(ba[4:], h.unselected)
|
2015-06-24 11:52:53 -04:00
|
|
|
bm, err := h.head.toByteArray()
|
2015-06-16 17:46:51 -04:00
|
|
|
if err != nil {
|
2015-06-18 15:06:11 -04:00
|
|
|
return nil, fmt.Errorf("failed to serialize head: %s", err.Error())
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
ba = append(ba, bm...)
|
|
|
|
|
|
|
|
return ba, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FromByteArray reads his handle's data from a byte array
|
|
|
|
func (h *Handle) FromByteArray(ba []byte) error {
|
2015-06-18 15:06:11 -04:00
|
|
|
if ba == nil {
|
|
|
|
return fmt.Errorf("nil byte array")
|
|
|
|
}
|
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
nh := &sequence{}
|
|
|
|
err := nh.fromByteArray(ba[8:])
|
2015-06-16 17:46:51 -04:00
|
|
|
if err != nil {
|
2015-06-18 15:06:11 -04:00
|
|
|
return fmt.Errorf("failed to deserialize head: %s", err.Error())
|
2015-06-16 17:46:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
h.Lock()
|
|
|
|
h.head = nh
|
2015-06-18 22:14:03 -04:00
|
|
|
h.bits = binary.BigEndian.Uint32(ba[0:4])
|
|
|
|
h.unselected = binary.BigEndian.Uint32(ba[4:8])
|
2015-06-16 17:46:51 -04:00
|
|
|
h.Unlock()
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bits returns the length of the bit sequence
|
|
|
|
func (h *Handle) Bits() uint32 {
|
|
|
|
return h.bits
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unselected returns the number of bits which are not selected
|
|
|
|
func (h *Handle) Unselected() uint32 {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
return h.unselected
|
|
|
|
}
|
|
|
|
|
2015-06-24 18:02:08 -04:00
|
|
|
func (h *Handle) String() string {
|
|
|
|
h.Lock()
|
|
|
|
defer h.Unlock()
|
|
|
|
return fmt.Sprintf("App: %s, ID: %s, DBIndex: 0x%x, bits: %d, unselected: %d, sequence: %s",
|
|
|
|
h.app, h.id, h.dbIndex, h.bits, h.unselected, h.head.toString())
|
|
|
|
}
|
|
|
|
|
2015-09-17 18:16:21 -04:00
|
|
|
// getFirstAvailable looks for the first unset bit in passed mask starting from start
|
|
|
|
func getFirstAvailable(head *sequence, start uint32) (uint32, uint32, error) {
|
|
|
|
// Find sequence which contains the start bit
|
|
|
|
byteStart, bitStart := ordinalToPos(start)
|
|
|
|
current, _, _, inBlockBytePos := findSequence(head, byteStart)
|
|
|
|
|
|
|
|
// Derive the this sequence offsets
|
|
|
|
byteOffset := byteStart - inBlockBytePos
|
|
|
|
bitOffset := inBlockBytePos*8 + bitStart
|
|
|
|
|
2015-06-12 15:03:33 -04:00
|
|
|
for current != nil {
|
2015-06-24 11:52:53 -04:00
|
|
|
if current.block != blockMAX {
|
2015-09-17 18:16:21 -04:00
|
|
|
bytePos, bitPos, err := current.getAvailableBit(bitOffset)
|
|
|
|
return byteOffset + bytePos, bitPos, err
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
2015-09-17 18:16:21 -04:00
|
|
|
// Moving to next block: Reset bit offset.
|
|
|
|
bitOffset = 0
|
|
|
|
byteOffset += current.count * blockBytes
|
2015-06-24 11:52:53 -04:00
|
|
|
current = current.next
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
2015-06-24 15:10:54 -04:00
|
|
|
return invalidPos, invalidPos, errNoBitAvailable
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
// checkIfAvailable checks if the bit correspondent to the specified ordinal is unset
|
|
|
|
// If the ordinal is beyond the sequence limits, a negative response is returned
|
2015-06-24 13:04:34 -04:00
|
|
|
func checkIfAvailable(head *sequence, ordinal uint32) (uint32, uint32, error) {
|
2015-09-17 18:16:21 -04:00
|
|
|
bytePos, bitPos := ordinalToPos(ordinal)
|
2015-06-12 15:03:33 -04:00
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
// Find the sequence containing this byte
|
2015-06-12 15:03:33 -04:00
|
|
|
current, _, _, inBlockBytePos := findSequence(head, bytePos)
|
|
|
|
if current != nil {
|
|
|
|
// Check whether the bit corresponding to the ordinal address is unset
|
2015-06-24 13:04:34 -04:00
|
|
|
bitSel := blockFirstBit >> (inBlockBytePos*8 + bitPos)
|
2015-06-24 11:52:53 -04:00
|
|
|
if current.block&bitSel == 0 {
|
2015-06-13 19:04:06 -04:00
|
|
|
return bytePos, bitPos, nil
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 13:04:34 -04:00
|
|
|
return invalidPos, invalidPos, fmt.Errorf("requested bit is not available")
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Given the byte position and the sequences list head, return the pointer to the
|
|
|
|
// sequence containing the byte (current), the pointer to the previous sequence,
|
|
|
|
// the number of blocks preceding the block containing the byte inside the current sequence.
|
2015-06-24 13:04:34 -04:00
|
|
|
// If bytePos is outside of the list, function will return (nil, nil, 0, invalidPos)
|
|
|
|
func findSequence(head *sequence, bytePos uint32) (*sequence, *sequence, uint32, uint32) {
|
2015-06-24 11:52:53 -04:00
|
|
|
// Find the sequence containing this byte
|
2015-06-12 15:03:33 -04:00
|
|
|
previous := head
|
|
|
|
current := head
|
|
|
|
n := bytePos
|
2015-06-24 13:04:34 -04:00
|
|
|
for current.next != nil && n >= (current.count*blockBytes) { // Nil check for less than 32 addresses masks
|
|
|
|
n -= (current.count * blockBytes)
|
2015-06-12 15:03:33 -04:00
|
|
|
previous = current
|
2015-06-24 11:52:53 -04:00
|
|
|
current = current.next
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If byte is outside of the list, let caller know
|
2015-06-24 13:04:34 -04:00
|
|
|
if n >= (current.count * blockBytes) {
|
|
|
|
return nil, nil, 0, invalidPos
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Find the byte position inside the block and the number of blocks
|
|
|
|
// preceding the block containing the byte inside this sequence
|
2015-06-24 13:04:34 -04:00
|
|
|
precBlocks := n / blockBytes
|
2015-06-12 15:03:33 -04:00
|
|
|
inBlockBytePos := bytePos % blockBytes
|
|
|
|
|
|
|
|
return current, previous, precBlocks, inBlockBytePos
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushReservation pushes the bit reservation inside the bitmask.
|
|
|
|
// Given byte and bit positions, identify the sequence (current) which holds the block containing the affected bit.
|
|
|
|
// Create a new block with the modified bit according to the operation (allocate/release).
|
2015-06-24 11:52:53 -04:00
|
|
|
// Create a new sequence containing the new block and insert it in the proper position.
|
2015-06-12 15:03:33 -04:00
|
|
|
// Remove current sequence if empty.
|
2015-06-24 11:52:53 -04:00
|
|
|
// Check if new sequence can be merged with neighbour (previous/next) sequences.
|
2015-06-12 15:03:33 -04:00
|
|
|
//
|
|
|
|
//
|
2015-06-24 11:52:53 -04:00
|
|
|
// Identify "current" sequence containing block:
|
|
|
|
// [prev seq] [current seq] [next seq]
|
2015-06-12 15:03:33 -04:00
|
|
|
//
|
|
|
|
// Based on block position, resulting list of sequences can be any of three forms:
|
|
|
|
//
|
2015-06-24 11:52:53 -04:00
|
|
|
// block position Resulting list of sequences
|
|
|
|
// A) block is first in current: [prev seq] [new] [modified current seq] [next seq]
|
|
|
|
// B) block is last in current: [prev seq] [modified current seq] [new] [next seq]
|
|
|
|
// C) block is in the middle of current: [prev seq] [curr pre] [new] [curr post] [next seq]
|
2015-06-24 13:04:34 -04:00
|
|
|
func pushReservation(bytePos, bitPos uint32, head *sequence, release bool) *sequence {
|
2015-06-12 15:03:33 -04:00
|
|
|
// Store list's head
|
|
|
|
newHead := head
|
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
// Find the sequence containing this byte
|
2015-06-12 15:03:33 -04:00
|
|
|
current, previous, precBlocks, inBlockBytePos := findSequence(head, bytePos)
|
|
|
|
if current == nil {
|
|
|
|
return newHead
|
|
|
|
}
|
|
|
|
|
|
|
|
// Construct updated block
|
2015-06-24 15:10:54 -04:00
|
|
|
bitSel := blockFirstBit >> (inBlockBytePos*8 + bitPos)
|
2015-06-24 11:52:53 -04:00
|
|
|
newBlock := current.block
|
2015-06-12 15:03:33 -04:00
|
|
|
if release {
|
|
|
|
newBlock &^= bitSel
|
|
|
|
} else {
|
|
|
|
newBlock |= bitSel
|
|
|
|
}
|
|
|
|
|
|
|
|
// Quit if it was a redundant request
|
2015-06-24 11:52:53 -04:00
|
|
|
if current.block == newBlock {
|
2015-06-12 15:03:33 -04:00
|
|
|
return newHead
|
|
|
|
}
|
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
// Current sequence inevitably looses one block, upadate count
|
|
|
|
current.count--
|
2015-06-12 15:03:33 -04:00
|
|
|
|
|
|
|
// Create new sequence
|
2015-06-24 11:52:53 -04:00
|
|
|
newSequence := &sequence{block: newBlock, count: 1}
|
2015-06-12 15:03:33 -04:00
|
|
|
|
|
|
|
// Insert the new sequence in the list based on block position
|
|
|
|
if precBlocks == 0 { // First in sequence (A)
|
2015-06-24 11:52:53 -04:00
|
|
|
newSequence.next = current
|
2015-06-12 15:03:33 -04:00
|
|
|
if current == head {
|
|
|
|
newHead = newSequence
|
|
|
|
previous = newHead
|
|
|
|
} else {
|
2015-06-24 11:52:53 -04:00
|
|
|
previous.next = newSequence
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
removeCurrentIfEmpty(&newHead, newSequence, current)
|
|
|
|
mergeSequences(previous)
|
2015-06-24 11:52:53 -04:00
|
|
|
} else if precBlocks == current.count-2 { // Last in sequence (B)
|
|
|
|
newSequence.next = current.next
|
|
|
|
current.next = newSequence
|
2015-06-12 15:03:33 -04:00
|
|
|
mergeSequences(current)
|
|
|
|
} else { // In between the sequence (C)
|
2015-06-24 11:52:53 -04:00
|
|
|
currPre := &sequence{block: current.block, count: precBlocks, next: newSequence}
|
2015-06-12 15:03:33 -04:00
|
|
|
currPost := current
|
2015-06-24 11:52:53 -04:00
|
|
|
currPost.count -= precBlocks
|
|
|
|
newSequence.next = currPost
|
2015-06-12 15:03:33 -04:00
|
|
|
if currPost == head {
|
|
|
|
newHead = currPre
|
|
|
|
} else {
|
2015-06-24 11:52:53 -04:00
|
|
|
previous.next = currPre
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
// No merging or empty current possible here
|
|
|
|
}
|
|
|
|
|
|
|
|
return newHead
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes the current sequence from the list if empty, adjusting the head pointer if needed
|
2015-06-24 11:52:53 -04:00
|
|
|
func removeCurrentIfEmpty(head **sequence, previous, current *sequence) {
|
|
|
|
if current.count == 0 {
|
2015-06-12 15:03:33 -04:00
|
|
|
if current == *head {
|
2015-06-24 11:52:53 -04:00
|
|
|
*head = current.next
|
2015-06-12 15:03:33 -04:00
|
|
|
} else {
|
2015-06-24 11:52:53 -04:00
|
|
|
previous.next = current.next
|
|
|
|
current = current.next
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-24 11:52:53 -04:00
|
|
|
// Given a pointer to a sequence, it checks if it can be merged with any following sequences
|
2015-06-12 15:03:33 -04:00
|
|
|
// It stops when no more merging is possible.
|
|
|
|
// TODO: Optimization: only attempt merge from start to end sequence, no need to scan till the end of the list
|
2015-06-24 11:52:53 -04:00
|
|
|
func mergeSequences(seq *sequence) {
|
2015-06-12 15:03:33 -04:00
|
|
|
if seq != nil {
|
|
|
|
// Merge all what possible from seq
|
2015-06-24 11:52:53 -04:00
|
|
|
for seq.next != nil && seq.block == seq.next.block {
|
|
|
|
seq.count += seq.next.count
|
|
|
|
seq.next = seq.next.next
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
2015-06-24 11:52:53 -04:00
|
|
|
// Move to next
|
|
|
|
mergeSequences(seq.next)
|
2015-06-12 15:03:33 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNumBlocks(numBits uint32) uint32 {
|
|
|
|
numBlocks := numBits / blockLen
|
|
|
|
if numBits%blockLen != 0 {
|
|
|
|
numBlocks++
|
|
|
|
}
|
|
|
|
return numBlocks
|
|
|
|
}
|
2015-06-24 15:10:54 -04:00
|
|
|
|
|
|
|
func ordinalToPos(ordinal uint32) (uint32, uint32) {
|
|
|
|
return ordinal / 8, ordinal % 8
|
|
|
|
}
|
|
|
|
|
|
|
|
func posToOrdinal(bytePos, bitPos uint32) uint32 {
|
|
|
|
return bytePos*8 + bitPos
|
|
|
|
}
|