Add numerical ids manager

Signed-off-by: Alessandro Boch <aboch@docker.com>
This commit is contained in:
Alessandro Boch
2015-06-13 16:04:06 -07:00
parent eb66d4456e
commit b512536cb3
5 changed files with 194 additions and 13 deletions
+8 -8
View File
@@ -134,13 +134,13 @@ func (s *Sequence) FromByteArray(data []byte) error {
}
// GetFirstAvailable returns the byte and bit position of the first unset bit
func (h *Handle) GetFirstAvailable() (int, int) {
func (h *Handle) GetFirstAvailable() (int, int, error) {
return GetFirstAvailable(h.Head)
}
// 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
func (h *Handle) CheckIfAvailable(ordinal int) (int, int) {
func (h *Handle) CheckIfAvailable(ordinal int) (int, int, error) {
return CheckIfAvailable(h.Head, ordinal)
}
@@ -150,23 +150,23 @@ func (h *Handle) PushReservation(bytePos, bitPos int, release bool) {
}
// GetFirstAvailable looks for the first unset bit in passed mask
func GetFirstAvailable(head *Sequence) (int, int) {
func GetFirstAvailable(head *Sequence) (int, int, error) {
byteIndex := 0
current := head
for current != nil {
if current.Block != blockMAX {
bytePos, bitPos := current.GetAvailableBit()
return byteIndex + bytePos, bitPos
return byteIndex + bytePos, bitPos, nil
}
byteIndex += int(current.Count * blockBytes)
current = current.Next
}
return -1, -1
return -1, -1, fmt.Errorf("no bit available")
}
// 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
func CheckIfAvailable(head *Sequence, ordinal int) (int, int) {
func CheckIfAvailable(head *Sequence, ordinal int) (int, int, error) {
bytePos := ordinal / 8
bitPos := ordinal % 8
@@ -177,11 +177,11 @@ func CheckIfAvailable(head *Sequence, ordinal int) (int, int) {
// Check whether the bit corresponding to the ordinal address is unset
bitSel := uint32(blockFirstBit >> uint(inBlockBytePos*8+bitPos))
if current.Block&bitSel == 0 {
return bytePos, bitPos
return bytePos, bitPos, nil
}
}
return -1, -1
return -1, -1, fmt.Errorf("requested bit is not available")
}
// Given the byte position and the sequences list head, return the pointer to the
+2 -2
View File
@@ -127,7 +127,7 @@ func TestGetFirstAvailable(t *testing.T) {
}
for n, i := range input {
bytePos, bitPos := GetFirstAvailable(i.mask)
bytePos, bitPos, _ := GetFirstAvailable(i.mask)
if bytePos != i.bytePos || bitPos != i.bitPos {
t.Fatalf("Error in (%d) getFirstAvailable(). Expected (%d, %d). Got (%d, %d)", n, i.bytePos, i.bitPos, bytePos, bitPos)
}
@@ -201,7 +201,7 @@ func TestCheckIfAvailable(t *testing.T) {
}
for n, i := range input {
bytePos, bitPos := CheckIfAvailable(i.head, i.ordinal)
bytePos, bitPos, _ := CheckIfAvailable(i.head, i.ordinal)
if bytePos != i.bytePos || bitPos != i.bitPos {
t.Fatalf("Error in (%d) checkIfAvailable(ord:%d). Expected (%d, %d). Got (%d, %d)", n, i.ordinal, i.bytePos, i.bitPos, bytePos, bitPos)
}