-
Notifications
You must be signed in to change notification settings - Fork 287
/
mark_bits.go
172 lines (149 loc) · 4.17 KB
/
mark_bits.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Copyright (c) 2018 Tigera, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package markbits
import (
"errors"
"sync"
log "github.com/sirupsen/logrus"
)
// MarkBitsManager provides set of functions to manage an uint32 mark bits based on a given mark mask.
type MarkBitsManager struct {
name string
mask uint32
numBitsAllocated int
numFreeBits int
mutex sync.Mutex
}
func NewMarkBitsManager(markMask uint32, markName string) *MarkBitsManager {
numBitsFound := 0
for shift := uint(0); shift < 32; shift++ {
bit := uint32(1) << shift
if markMask&bit > 0 {
numBitsFound += 1
}
}
return &MarkBitsManager{
name: markName,
mask: markMask,
numBitsAllocated: 0,
numFreeBits: numBitsFound,
}
}
func (mc *MarkBitsManager) GetMask() uint32 {
return mc.mask
}
// Allocate next mark bit.
func (mc *MarkBitsManager) NextSingleBitMark() (uint32, error) {
mc.mutex.Lock()
defer mc.mutex.Unlock()
mark, err := mc.nthMark(mc.numBitsAllocated)
if err != nil {
return 0, err
}
mc.numFreeBits--
mc.numBitsAllocated++
return mark, nil
}
func (mc *MarkBitsManager) AvailableMarkBitCount() int {
mc.mutex.Lock()
defer mc.mutex.Unlock()
return mc.numFreeBits
}
// Allocate a block of bits given a requested size.
// Performance is not a priority here since it is just a start of day operation.
// Return allocated mark and how many bits allocated.
// It is up to the caller to check the result.
func (mc *MarkBitsManager) NextBlockBitsMark(size int) (uint32, int) {
mark := uint32(0)
for allocated := 0; allocated < size; allocated++ {
if bit, err := mc.NextSingleBitMark(); err != nil {
log.WithFields(log.Fields{
"Name": mc.name,
"MarkMask": mc.mask,
"requestedMarkBlockSize": size,
}).Warning("Not enough mark bits available.")
// Return as many bits as possible.
return mark, allocated
} else {
mark |= bit
}
}
return mark, size
}
// Return Nth mark bit without allocation.
func (mc *MarkBitsManager) nthMark(n int) (uint32, error) {
numBitsFound := 0
for shift := uint(0); shift < 32; shift++ {
candidate := uint32(1) << shift
if mc.mask&candidate > 0 {
if numBitsFound == n {
return candidate, nil
}
numBitsFound++
}
}
return 0, errors.New("No mark bit found")
}
// Return how many free position number left.
func (mc *MarkBitsManager) CurrentFreeNumberOfMark() int {
if mc.numFreeBits > 0 {
return int(uint64(1) << uint64(mc.numFreeBits))
}
return 0
}
// Return a mark given a position number.
func (mc *MarkBitsManager) MapNumberToMark(n int) (uint32, error) {
number := uint32(n)
mark := uint32(0)
numBitsFound := uint32(0)
for shift := uint(0); shift < 32 && number > 0; shift++ {
candidate := uint32(1) << shift
if mc.mask&candidate > 0 {
value := number & (uint32(1) << numBitsFound)
if value > 0 {
mark |= candidate
number -= value
}
numBitsFound++
}
}
if number > 0 {
log.WithFields(log.Fields{
"Name": mc.name,
"MarkMask": mc.mask,
"requestedMapNumber": n,
}).Warn("Not enough mark bits available.")
return 0, errors.New("Not enough mark bits available")
}
return mark, nil
}
// Return a position number given a mark.
func (mc *MarkBitsManager) MapMarkToNumber(mark uint32) (int, error) {
if mark&mc.mask != mark {
// mark bit not compatible.
return 0, errors.New("Mark bit not compatible")
}
number := 0
numBitsFound := uint32(0)
for shift := uint(0); shift < 32; shift++ {
bit := uint32(1) << shift
if mc.mask&bit > 0 {
if bit&mark > 0 {
number += int(uint32(1) << numBitsFound)
}
numBitsFound++
}
}
return number, nil
}